summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/jsapi/WebrtcGlobalStatsHistory.h
blob: aa820e787daa54ebba0cbf1dcff1d376e99c3545 (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
/* 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/. */

#pragma once

#include <memory>
#include "domstubs.h"
#include "mozilla/LinkedList.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/DOMString.h"
#include "mozilla/dom/RTCStatsReportBinding.h"
#include "nsDOMNavigationTiming.h"
#include "nsHashKeys.h"
#include "nsTHashMap.h"

namespace mozilla::dom {
class WebrtcGlobalStatisticsHistoryCallback;
struct RTCStatsReportInternal;

struct WebrtcGlobalStatsHistory {
  // History preferences
  struct Pref {
    static auto Enabled() -> bool;
    static auto PollIntervalMs() -> uint32_t;
    static auto StorageWindowS() -> uint32_t;
    static auto PruneAfterM() -> uint32_t;
    static auto ClosedStatsToRetain() -> uint32_t;
    Pref() = delete;
    ~Pref() = delete;
  };

  struct Entry {
    NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Entry)
    // We need to wrap the report in an element
    struct ReportElement : public LinkedListElement<ReportElement> {
      UniquePtr<RTCStatsReportInternal> report;
      auto Timestamp() const -> DOMHighResTimeStamp;
      virtual ~ReportElement() = default;
    };
    // And likewise for the SDP history
    struct SdpElement : public LinkedListElement<SdpElement> {
      RTCSdpHistoryEntryInternal sdp;
      auto Timestamp() const -> DOMHighResTimeStamp;
      virtual ~SdpElement() = default;
    };

    explicit Entry(const nsString& aPcid, const bool aIsLongTermStatsDisabled)
        : mPcid(aPcid), mIsLongTermStatsDisabled(aIsLongTermStatsDisabled) {}

    nsString mPcid;
    AutoCleanLinkedList<ReportElement> mReports;
    AutoCleanLinkedList<SdpElement> mSdp;
    bool mIsLongTermStatsDisabled;
    bool mIsClosed = false;

    auto Since(const Maybe<DOMHighResTimeStamp>& aAfter) const
        -> nsTArray<RTCStatsReportInternal>;
    auto SdpSince(const Maybe<DOMHighResTimeStamp>& aAfter) const
        -> RTCSdpHistoryInternal;

    static auto MakeReportElement(UniquePtr<RTCStatsReportInternal> aReport)
        -> ReportElement*;
    static auto MakeSdpElementsSince(
        Sequence<RTCSdpHistoryEntryInternal>&& aSdpHistory,
        const Maybe<DOMHighResTimeStamp>& aSdpAfter)
        -> AutoCleanLinkedList<SdpElement>;
    auto Prune(const DOMHighResTimeStamp aBefore) -> void;

   private:
    virtual ~Entry() = default;
  };
  using StatsMap = nsTHashMap<nsStringHashKey, RefPtr<Entry> >;
  static auto InitHistory(const nsAString& aPcId,
                          const bool aIsLongTermStatsDisabled) -> void;
  static auto Record(UniquePtr<RTCStatsReportInternal> aReport) -> void;
  static auto CloseHistory(const nsAString& aPcId) -> void;
  static auto GetHistory(const nsAString& aPcId) -> Maybe<RefPtr<Entry> >;
  static auto Clear() -> void;
  static auto PcIds() -> dom::Sequence<nsString>;

  WebrtcGlobalStatsHistory() = delete;

 private:
  static auto Get() -> StatsMap&;
};
}  // namespace mozilla::dom