summaryrefslogtreecommitdiffstats
path: root/dom/media/utils/TelemetryProbesReporter.h
blob: 73a73f040338c69198f42d8f81761612e810ae4f (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
/* 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/. */

#ifndef DOM_TelemetryProbesReporter_H_
#define DOM_TelemetryProbesReporter_H_

#include "MediaInfo.h"
#include "mozilla/Maybe.h"
#include "mozilla/AwakeTimeStamp.h"
#include "AudioChannelService.h"
#include "nsISupportsImpl.h"

namespace mozilla {
class FrameStatistics;

class TelemetryProbesReporterOwner {
 public:
  virtual Maybe<nsAutoString> GetKeySystem() const = 0;
  virtual MediaInfo GetMediaInfo() const = 0;
  virtual FrameStatistics* GetFrameStatistics() const = 0;
  virtual bool IsEncrypted() const = 0;
  virtual void DispatchAsyncTestingEvent(const nsAString& aName) = 0;
};

enum class MediaContent : uint8_t {
  MEDIA_HAS_NOTHING = (0 << 0),
  MEDIA_HAS_VIDEO = (1 << 0),
  MEDIA_HAS_AUDIO = (1 << 1),
  MEDIA_HAS_COLOR_DEPTH_ABOVE_8 = (1 << 2),
};

MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(MediaContent)

/**
 * This class is used for collecting and reporting telemetry probes for
 * its owner which should inherit from TelemetryProbesReporterOwner. We use it
 * for HTMLMediaElement, and each element has one corresponding reporter.
 */
class TelemetryProbesReporter final {
 public:
  explicit TelemetryProbesReporter(TelemetryProbesReporterOwner* aOwner);
  ~TelemetryProbesReporter() = default;

  enum class Visibility {
    eInitial,
    eVisible,
    eInvisible,
  };

  static MediaContent MediaInfoToMediaContent(const MediaInfo& aInfo);

  using AudibleState = dom::AudioChannelService::AudibleState;

  // State transitions
  void OnPlay(Visibility aVisibility, MediaContent aContent, bool aIsMuted);
  void OnPause(Visibility aVisibility);
  void OnShutdown();

  void OnVisibilityChanged(Visibility aVisibility);
  void OnAudibleChanged(AudibleState aAudible);
  void OnMediaContentChanged(MediaContent aContent);
  void OnMutedChanged(bool aMuted);
  void OnDecodeSuspended();
  void OnDecodeResumed();

  double GetTotalVideoPlayTimeInSeconds() const;
  double GetTotalVideoHDRPlayTimeInSeconds() const;
  double GetVisibleVideoPlayTimeInSeconds() const;
  double GetInvisibleVideoPlayTimeInSeconds() const;
  double GetVideoDecodeSuspendedTimeInSeconds() const;

  double GetTotalAudioPlayTimeInSeconds() const;
  double GetInaudiblePlayTimeInSeconds() const;
  double GetAudiblePlayTimeInSeconds() const;
  double GetMutedPlayTimeInSeconds() const;

 private:
  void StartInvisibleVideoTimeAccumulator();
  void PauseInvisibleVideoTimeAccumulator();
  void StartInaudibleAudioTimeAccumulator();
  void PauseInaudibleAudioTimeAccumulator();
  void StartMutedAudioTimeAccumulator();
  void PauseMutedAudioTimeAccumulator();
  bool HasOwnerHadValidVideo() const;
  bool HasOwnerHadValidMedia() const;
  void AssertOnMainThreadAndNotShutdown() const;

  void ReportTelemetry();
  void ReportResultForVideo();
  void ReportResultForAudio();
  void ReportResultForVideoFrameStatistics(double aTotalPlayTimeS,
                                           const nsCString& key);

  // Helper class to measure times for playback telemetry stats
  class TimeDurationAccumulator {
   public:
    TimeDurationAccumulator() = default;
    void Start() {
      if (IsStarted()) {
        return;
      }
      mStartTime = Some(AwakeTimeStamp::NowLoRes());
    }
    void Pause() {
      if (!IsStarted()) {
        return;
      }
      mSum = (AwakeTimeStamp::NowLoRes() - mStartTime.value());
      mStartTime = Nothing();
    }
    bool IsStarted() const { return mStartTime.isSome(); }

    double GetAndClearTotal() {
      MOZ_ASSERT(!IsStarted(), "only call this when accumulator is paused");
      double total = mSum.ToSeconds();
      mStartTime = Nothing();
      mSum = AwakeTimeDuration();
      return total;
    }

    double PeekTotal() const {
      if (!IsStarted()) {
        return mSum.ToSeconds();
      }
      return (AwakeTimeStamp::NowLoRes() - mStartTime.value()).ToSeconds();
    }

   private:
    Maybe<AwakeTimeStamp> mStartTime;
    AwakeTimeDuration mSum;
  };

  // The owner is HTMLMediaElement that is guaranteed being always alive during
  // our whole life cycle.
  TelemetryProbesReporterOwner* mOwner;

  // Total time an element has spent on playing video.
  TimeDurationAccumulator mTotalVideoPlayTime;

  // Total time an element has spent on playing video that has a color depth
  // greater than 8, which is likely HDR video.
  TimeDurationAccumulator mTotalVideoHDRPlayTime;

  // Total time an element has spent on playing audio
  TimeDurationAccumulator mTotalAudioPlayTime;

  // Total time a VIDEO element has spent playing while the corresponding media
  // element is invisible.
  TimeDurationAccumulator mInvisibleVideoPlayTime;

  // Total time an element has spent playing audio that was not audible
  TimeDurationAccumulator mInaudibleAudioPlayTime;

  // Total time an element with an audio track has spent muted
  TimeDurationAccumulator mMutedAudioPlayTime;

  // Total time a VIDEO has spent in video-decode-suspend mode.
  TimeDurationAccumulator mVideoDecodeSuspendedTime;

  Visibility mMediaElementVisibility = Visibility::eInitial;

  MediaContent mMediaContent = MediaContent::MEDIA_HAS_NOTHING;

  bool mIsPlaying = false;

  bool mIsMuted = false;
};

}  // namespace mozilla

#endif  // DOM_TelemetryProbesReporter_H_