summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/jsapi/WebrtcGlobalStatsHistory.cpp
blob: 1d576b5dca0303a1231885adc134f2f5485930f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "WebrtcGlobalStatsHistory.h"
#include <memory>

#include "domstubs.h"
#include "mozilla/LinkedList.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/RTCStatsReportBinding.h"  // for RTCStatsReportInternal
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/StaticPrefs_media.h"
#include "mozilla/fallible.h"
#include "mozilla/mozalloc_oom.h"
#include "nsDOMNavigationTiming.h"

namespace mozilla::dom {

constexpr auto SEC_TO_MS(const DOMHighResTimeStamp sec) -> DOMHighResTimeStamp {
  return sec * 1000.0;
}

constexpr auto MIN_TO_MS(const DOMHighResTimeStamp min) -> DOMHighResTimeStamp {
  return SEC_TO_MS(min * 60.0);
}

// Prefs
auto WebrtcGlobalStatsHistory::Pref::Enabled() -> bool {
  return mozilla::StaticPrefs::media_aboutwebrtc_hist_enabled();
}

auto WebrtcGlobalStatsHistory::Pref::PollIntervalMs() -> uint32_t {
  return mozilla::StaticPrefs::media_aboutwebrtc_hist_poll_interval_ms();
}

auto WebrtcGlobalStatsHistory::Pref::StorageWindowS() -> uint32_t {
  return mozilla::StaticPrefs::media_aboutwebrtc_hist_storage_window_s();
}

auto WebrtcGlobalStatsHistory::Pref::PruneAfterM() -> uint32_t {
  return mozilla::StaticPrefs::media_aboutwebrtc_hist_prune_after_m();
}

auto WebrtcGlobalStatsHistory::Pref::ClosedStatsToRetain() -> uint32_t {
  return mozilla::StaticPrefs::media_aboutwebrtc_hist_closed_stats_to_retain();
}

auto WebrtcGlobalStatsHistory::Get() -> WebrtcGlobalStatsHistory::StatsMap& {
  static StaticAutoPtr<StatsMap> sHist;
  if (!sHist) {
    sHist = new StatsMap();
    ClearOnShutdown(&sHist);
  }
  return *sHist;
}

auto WebrtcGlobalStatsHistory::Entry::ReportElement::Timestamp() const
    -> DOMHighResTimeStamp {
  return report->mTimestamp;
}

auto WebrtcGlobalStatsHistory::Entry::SdpElement::Timestamp() const
    -> DOMHighResTimeStamp {
  return sdp.mTimestamp;
}

auto WebrtcGlobalStatsHistory::Entry::MakeReportElement(
    UniquePtr<RTCStatsReportInternal> aReport)
    -> WebrtcGlobalStatsHistory::Entry::ReportElement* {
  auto* elem = new ReportElement();
  elem->report = std::move(aReport);
  // We don't want to store a copy of the SDP history with each stats entry.
  // SDP History is stored seperately, see MakeSdpElements.
  elem->report->mSdpHistory.Clear();
  return elem;
}

auto WebrtcGlobalStatsHistory::Entry::MakeSdpElementsSince(
    Sequence<RTCSdpHistoryEntryInternal>&& aSdpHistory,
    const Maybe<DOMHighResTimeStamp>& aSdpAfter)
    -> AutoCleanLinkedList<WebrtcGlobalStatsHistory::Entry::SdpElement> {
  AutoCleanLinkedList<WebrtcGlobalStatsHistory::Entry::SdpElement> result;
  for (auto& sdpHist : aSdpHistory) {
    if (!aSdpAfter || aSdpAfter.value() < sdpHist.mTimestamp) {
      auto* element = new SdpElement();
      element->sdp = sdpHist;
      result.insertBack(element);
    }
  }
  return result;
}

template <typename T>
auto FindFirstEntryAfter(const T* first,
                         const Maybe<DOMHighResTimeStamp>& aAfter) -> const T* {
  const auto* current = first;
  while (aAfter && current && current->Timestamp() <= aAfter.value()) {
    current = current->getNext();
  }
  return current;
}

template <typename T>
auto CountElementsToEndInclusive(const LinkedListElement<T>* elem) -> size_t {
  size_t count = 0;
  const auto* cursor = elem;
  while (cursor && cursor->isInList()) {
    count++;
    cursor = cursor->getNext();
  }
  return count;
}

auto WebrtcGlobalStatsHistory::Entry::Since(
    const Maybe<DOMHighResTimeStamp>& aAfter) const
    -> nsTArray<RTCStatsReportInternal> {
  nsTArray<RTCStatsReportInternal> results;
  const auto* cursor = FindFirstEntryAfter(mReports.getFirst(), aAfter);
  const auto count = CountElementsToEndInclusive(cursor);
  if (!results.SetCapacity(count, fallible)) {
    mozalloc_handle_oom(0);
  }
  while (cursor) {
    results.AppendElement(RTCStatsReportInternal(*cursor->report));
    cursor = cursor->getNext();
  }
  return results;
}

auto WebrtcGlobalStatsHistory::Entry::SdpSince(
    const Maybe<DOMHighResTimeStamp>& aAfter) const -> RTCSdpHistoryInternal {
  RTCSdpHistoryInternal results;
  results.mPcid = mPcid;
  // If no timestamp was passed copy the entire history
  const auto* cursor = FindFirstEntryAfter(mSdp.getFirst(), aAfter);
  const auto count = CountElementsToEndInclusive(cursor);
  if (!results.mSdpHistory.SetCapacity(count, fallible)) {
    mozalloc_handle_oom(0);
  }
  while (cursor) {
    if (!results.mSdpHistory.AppendElement(
            RTCSdpHistoryEntryInternal(cursor->sdp), fallible)) {
      mozalloc_handle_oom(0);
    }
    cursor = cursor->getNext();
  }
  return results;
}

auto WebrtcGlobalStatsHistory::Entry::Prune(const DOMHighResTimeStamp aBefore)
    -> void {
  // Clear everything in the case that we don't keep stats
  if (mIsLongTermStatsDisabled) {
    mReports.clear();
  }
  // Clear everything before the cutoff
  for (auto* element = mReports.getFirst();
       element && element->report->mTimestamp < aBefore;
       element = mReports.getFirst()) {
    delete mReports.popFirst();
  }
  // I don't think we should prune SDPs but if we did it would look like this:
  // Note: we always keep the most recent SDP
}

auto WebrtcGlobalStatsHistory::InitHistory(const nsAString& aPcId,
                                           const bool aIsLongTermStatsDisabled)
    -> void {
  MOZ_ASSERT(XRE_IsParentProcess());
  if (WebrtcGlobalStatsHistory::Get().MaybeGet(aPcId)) {
    return;
  }
  WebrtcGlobalStatsHistory::Get().GetOrInsertNew(aPcId, nsString(aPcId),
                                                 aIsLongTermStatsDisabled);
};

auto WebrtcGlobalStatsHistory::Record(UniquePtr<RTCStatsReportInternal> aReport)
    -> void {
  MOZ_ASSERT(XRE_IsParentProcess());
  // Use the report timestamp as "now" for determining time depth
  // based pruning.
  const auto now = aReport->mTimestamp;
  const auto earliest = now - SEC_TO_MS(Pref::StorageWindowS());

  // Store closed state before moving the report
  const auto closed = aReport->mClosed;
  const auto pcId = aReport->mPcid;

  auto history = WebrtcGlobalStatsHistory::GetHistory(aReport->mPcid);
  if (history && Pref::Enabled()) {
    auto entry = history.value();
    // Remove expired entries
    entry->Prune(earliest);
    // Find new SDP entries
    auto sdpAfter = Maybe<DOMHighResTimeStamp>(Nothing());
    if (auto* lastSdp = entry->mSdp.getLast(); lastSdp) {
      sdpAfter = Some(lastSdp->Timestamp());
    }
    entry->mSdp.extendBack(
        Entry::MakeSdpElementsSince(std::move(aReport->mSdpHistory), sdpAfter));
    // Reports must be in ascending order by mTimestamp
    const auto* latest = entry->mReports.getLast();
    // Maintain sorted order
    if (!latest || latest->report->mTimestamp < aReport->mTimestamp) {
      entry->mReports.insertBack(Entry::MakeReportElement(std::move(aReport)));
    }
  }
  // Close the history if needed
  if (closed) {
    CloseHistory(pcId);
  }
}

auto WebrtcGlobalStatsHistory::CloseHistory(const nsAString& aPcId) -> void {
  MOZ_ASSERT(XRE_IsParentProcess());
  auto maybeHist = WebrtcGlobalStatsHistory::Get().MaybeGet(aPcId);
  if (!maybeHist) {
    return;
  }
  {
    auto&& hist = maybeHist.value();
    hist->mIsClosed = true;

    if (hist->mIsLongTermStatsDisabled) {
      WebrtcGlobalStatsHistory::Get().Remove(aPcId);
      return;
    }
  }
  size_t remainingClosedStatsToRetain =
      WebrtcGlobalStatsHistory::Pref::ClosedStatsToRetain();
  WebrtcGlobalStatsHistory::Get().RemoveIf([&](auto& iter) {
    auto& entry = iter.Data();
    if (!entry->mIsClosed) {
      return false;
    }
    if (entry->mIsLongTermStatsDisabled) {
      return true;
    }
    if (remainingClosedStatsToRetain > 0) {
      remainingClosedStatsToRetain -= 1;
      return false;
    }
    return true;
  });
}

auto WebrtcGlobalStatsHistory::Clear() -> void {
  MOZ_ASSERT(XRE_IsParentProcess());
  WebrtcGlobalStatsHistory::Get().RemoveIf([](auto& aIter) {
    // First clear all the closed histories.
    if (aIter.Data()->mIsClosed) {
      return true;
    }
    // For all remaining histories clear their stored reports
    aIter.Data()->mReports.clear();
    // As an optimization we don't clear the SDP, because that would
    // be reconstitued in the very next stats gathering polling period.
    // Those are potentially large allocations which we can skip.
    return false;
  });
}

auto WebrtcGlobalStatsHistory::PcIds() -> dom::Sequence<nsString> {
  MOZ_ASSERT(XRE_IsParentProcess());
  dom::Sequence<nsString> pcIds;
  for (const auto& pcId : WebrtcGlobalStatsHistory::Get().Keys()) {
    if (!pcIds.AppendElement(pcId, fallible)) {
      mozalloc_handle_oom(0);
    }
  }
  return pcIds;
}

auto WebrtcGlobalStatsHistory::GetHistory(const nsAString& aPcId)
    -> Maybe<RefPtr<Entry> > {
  MOZ_ASSERT(XRE_IsParentProcess());
  const auto pcid = NS_ConvertUTF16toUTF8(aPcId);

  return WebrtcGlobalStatsHistory::Get().MaybeGet(aPcId);
}
}  // namespace mozilla::dom