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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
/* 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 "TelemetryProbesReporter.h"
#include <cmath>
#include "FrameStatistics.h"
#include "mozilla/EMEUtils.h"
#include "mozilla/Logging.h"
#include "mozilla/Telemetry.h"
#include "mozilla/StaticPrefs_media.h"
#include "nsThreadUtils.h"
namespace mozilla {
LazyLogModule gTelemetryProbesReporterLog("TelemetryProbesReporter");
#define LOG(msg, ...) \
MOZ_LOG(gTelemetryProbesReporterLog, LogLevel::Debug, \
("TelemetryProbesReporter=%p, " msg, this, ##__VA_ARGS__))
static const char* ToVisibilityStr(
TelemetryProbesReporter::Visibility aVisibility) {
switch (aVisibility) {
case TelemetryProbesReporter::Visibility::eVisible:
return "visible";
case TelemetryProbesReporter::Visibility::eInvisible:
return "invisible";
default:
MOZ_ASSERT_UNREACHABLE("invalid visibility");
return "unknown";
}
}
TelemetryProbesReporter::TelemetryProbesReporter(
TelemetryProbesReporterOwner* aOwner)
: mOwner(aOwner) {
MOZ_ASSERT(mOwner);
}
void TelemetryProbesReporter::OnPlay(Visibility aVisibility) {
AssertOnMainThreadAndNotShutdown();
if (mTotalPlayTime.IsStarted()) {
return;
}
LOG("Start time accumulation for total play time");
mTotalPlayTime.Start();
mOwner->DispatchAsyncTestingEvent(u"moztotalplaytimestarted"_ns);
if (aVisibility == Visibility::eInvisible) {
StartInvisibleVideoTimeAcculator();
}
}
void TelemetryProbesReporter::OnPause(Visibility aVisibility) {
AssertOnMainThreadAndNotShutdown();
if (!mTotalPlayTime.IsStarted()) {
return;
}
if (aVisibility == Visibility::eInvisible) {
PauseInvisibleVideoTimeAcculator();
}
LOG("Pause time accumulation for total play time");
mTotalPlayTime.Pause();
mOwner->DispatchAsyncTestingEvent(u"moztotalplaytimepaused"_ns);
ReportTelemetry();
}
void TelemetryProbesReporter::OnVisibilityChanged(Visibility aVisibility) {
AssertOnMainThreadAndNotShutdown();
if (mMediaElementVisibility == aVisibility) {
return;
}
mMediaElementVisibility = aVisibility;
LOG("Corresponding media element visibility change=%s",
ToVisibilityStr(aVisibility));
if (mMediaElementVisibility == Visibility::eInvisible) {
StartInvisibleVideoTimeAcculator();
} else {
PauseInvisibleVideoTimeAcculator();
}
}
void TelemetryProbesReporter::OnDecodeSuspended() {
AssertOnMainThreadAndNotShutdown();
// Suspended time should only be counted after starting accumulating invisible
// time.
if (!mInvisibleVideoPlayTime.IsStarted()) {
return;
}
LOG("Start time accumulation for video decoding suspension");
mVideoDecodeSuspendedTime.Start();
mOwner->DispatchAsyncTestingEvent(u"mozvideodecodesuspendedstarted"_ns);
}
void TelemetryProbesReporter::OnDecodeResumed() {
AssertOnMainThreadAndNotShutdown();
if (!mVideoDecodeSuspendedTime.IsStarted()) {
return;
}
LOG("Pause time accumulation for video decoding suspension");
mVideoDecodeSuspendedTime.Pause();
mOwner->DispatchAsyncTestingEvent(u"mozvideodecodesuspendedpaused"_ns);
}
void TelemetryProbesReporter::OnShutdown() {
AssertOnMainThreadAndNotShutdown();
LOG("Shutdown");
OnPause(Visibility::eInvisible);
mOwner = nullptr;
}
void TelemetryProbesReporter::StartInvisibleVideoTimeAcculator() {
AssertOnMainThreadAndNotShutdown();
if (!mTotalPlayTime.IsStarted() || mInvisibleVideoPlayTime.IsStarted() ||
!HasOwnerHadValidVideo()) {
return;
}
LOG("Start time accumulation for invisible video");
mInvisibleVideoPlayTime.Start();
mOwner->DispatchAsyncTestingEvent(u"mozinvisibleplaytimestarted"_ns);
}
void TelemetryProbesReporter::PauseInvisibleVideoTimeAcculator() {
AssertOnMainThreadAndNotShutdown();
if (!mInvisibleVideoPlayTime.IsStarted()) {
return;
}
OnDecodeResumed();
LOG("Pause time accumulation for invisible video");
mInvisibleVideoPlayTime.Pause();
mOwner->DispatchAsyncTestingEvent(u"mozinvisibleplaytimepaused"_ns);
}
bool TelemetryProbesReporter::HasOwnerHadValidVideo() const {
// Checking both image and display dimensions helps address cases such as
// suspending, where we use a null decoder. In that case a null decoder
// produces 0x0 video frames, which might cause layout to resize the display
// size, but the image dimensions would be still non-null.
const VideoInfo info = mOwner->GetMediaInfo().mVideo;
return (info.mDisplay.height > 0 && info.mDisplay.width > 0) ||
(info.mImage.height > 0 && info.mImage.width > 0);
}
void TelemetryProbesReporter::AssertOnMainThreadAndNotShutdown() const {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mOwner, "Already shutdown?");
}
void TelemetryProbesReporter::ReportTelemetry() {
AssertOnMainThreadAndNotShutdown();
ReportResultForVideo();
}
void TelemetryProbesReporter::ReportResultForVideo() {
// We don't want to know the result for video without valid video frames.
if (!HasOwnerHadValidVideo()) {
return;
}
const double totalPlayTimeS = mTotalPlayTime.GetAndClearTotal();
const double invisiblePlayTimeS = mInvisibleVideoPlayTime.GetAndClearTotal();
const double videoDecodeSuspendTimeS =
mVideoDecodeSuspendedTime.GetAndClearTotal();
// No need to report result for video that didn't start playing.
if (totalPlayTimeS == 0.0) {
return;
}
MOZ_ASSERT(totalPlayTimeS >= invisiblePlayTimeS);
LOG("VIDEO_PLAY_TIME_S = %f", totalPlayTimeS);
Telemetry::Accumulate(Telemetry::VIDEO_PLAY_TIME_MS,
SECONDS_TO_MS(totalPlayTimeS));
LOG("VIDEO_HIDDEN_PLAY_TIME_S = %f", invisiblePlayTimeS);
Telemetry::Accumulate(Telemetry::VIDEO_HIDDEN_PLAY_TIME_MS,
SECONDS_TO_MS(invisiblePlayTimeS));
// Report result for video using CDM
auto keySystem = mOwner->GetKeySystem();
if (keySystem) {
if (IsClearkeyKeySystem(*keySystem)) {
LOG("VIDEO_CLEARKEY_PLAY_TIME_S = %f", totalPlayTimeS);
Telemetry::Accumulate(Telemetry::VIDEO_CLEARKEY_PLAY_TIME_MS,
SECONDS_TO_MS(totalPlayTimeS));
} else if (IsWidevineKeySystem(*keySystem)) {
LOG("VIDEO_WIDEVINE_PLAY_TIME_S = %f", totalPlayTimeS);
Telemetry::Accumulate(Telemetry::VIDEO_WIDEVINE_PLAY_TIME_MS,
SECONDS_TO_MS(totalPlayTimeS));
}
}
// Keyed by audio+video or video alone, and by a resolution range.
const MediaInfo& info = mOwner->GetMediaInfo();
nsCString key(info.HasAudio() ? "AV," : "V,");
static const struct {
int32_t mH;
const char* mRes;
} sResolutions[] = {{240, "0<h<=240"}, {480, "240<h<=480"},
{576, "480<h<=576"}, {720, "576<h<=720"},
{1080, "720<h<=1080"}, {2160, "1080<h<=2160"}};
const char* resolution = "h>2160";
int32_t height = info.mVideo.mImage.height;
for (const auto& res : sResolutions) {
if (height <= res.mH) {
resolution = res.mRes;
break;
}
}
key.AppendASCII(resolution);
const uint32_t hiddenPercentage =
lround(invisiblePlayTimeS / totalPlayTimeS * 100.0);
Telemetry::Accumulate(Telemetry::VIDEO_HIDDEN_PLAY_TIME_PERCENTAGE, key,
hiddenPercentage);
// Also accumulate all percentages in an "All" key.
Telemetry::Accumulate(Telemetry::VIDEO_HIDDEN_PLAY_TIME_PERCENTAGE, "All"_ns,
hiddenPercentage);
LOG("VIDEO_HIDDEN_PLAY_TIME_PERCENTAGE = %u, keys: '%s' and 'All'",
hiddenPercentage, key.get());
const uint32_t videoDecodeSuspendPercentage =
lround(videoDecodeSuspendTimeS / totalPlayTimeS * 100.0);
Telemetry::Accumulate(Telemetry::VIDEO_INFERRED_DECODE_SUSPEND_PERCENTAGE,
key, videoDecodeSuspendPercentage);
Telemetry::Accumulate(Telemetry::VIDEO_INFERRED_DECODE_SUSPEND_PERCENTAGE,
"All"_ns, videoDecodeSuspendPercentage);
LOG("VIDEO_INFERRED_DECODE_SUSPEND_PERCENTAGE = %u, keys: '%s' and 'All'",
videoDecodeSuspendPercentage, key.get());
ReportResultForVideoFrameStatistics(totalPlayTimeS, key);
mOwner->DispatchAsyncTestingEvent(u"mozreportedtelemetry"_ns);
}
void TelemetryProbesReporter::ReportResultForVideoFrameStatistics(
double aTotalPlayTimeS, const nsCString& key) {
FrameStatistics* stats = mOwner->GetFrameStatistics();
if (!stats) {
return;
}
FrameStatisticsData data = stats->GetFrameStatisticsData();
if (data.mInterKeyframeCount != 0) {
const uint32_t average_ms = uint32_t(
std::min<uint64_t>(lround(double(data.mInterKeyframeSum_us) /
double(data.mInterKeyframeCount) / 1000.0),
UINT32_MAX));
Telemetry::Accumulate(Telemetry::VIDEO_INTER_KEYFRAME_AVERAGE_MS, key,
average_ms);
Telemetry::Accumulate(Telemetry::VIDEO_INTER_KEYFRAME_AVERAGE_MS, "All"_ns,
average_ms);
LOG("VIDEO_INTER_KEYFRAME_AVERAGE_MS = %u, keys: '%s' and 'All'",
average_ms, key.get());
const uint32_t max_ms = uint32_t(std::min<uint64_t>(
(data.mInterKeyFrameMax_us + 500) / 1000, UINT32_MAX));
Telemetry::Accumulate(Telemetry::VIDEO_INTER_KEYFRAME_MAX_MS, key, max_ms);
Telemetry::Accumulate(Telemetry::VIDEO_INTER_KEYFRAME_MAX_MS, "All"_ns,
max_ms);
LOG("VIDEO_INTER_KEYFRAME_MAX_MS = %u, keys: '%s' and 'All'", max_ms,
key.get());
} else {
// Here, we have played *some* of the video, but didn't get more than 1
// keyframe. Report '0' if we have played for longer than the video-
// decode-suspend delay (showing recovery would be difficult).
const uint32_t suspendDelay_ms =
StaticPrefs::media_suspend_bkgnd_video_delay_ms();
if (uint32_t(aTotalPlayTimeS * 1000.0) > suspendDelay_ms) {
Telemetry::Accumulate(Telemetry::VIDEO_INTER_KEYFRAME_MAX_MS, key, 0);
Telemetry::Accumulate(Telemetry::VIDEO_INTER_KEYFRAME_MAX_MS, "All"_ns,
0);
LOG("VIDEO_INTER_KEYFRAME_MAX_MS = 0 (only 1 keyframe), keys: '%s' and "
"'All'",
key.get());
}
}
const uint64_t parsedFrames = stats->GetParsedFrames();
if (parsedFrames) {
const uint64_t droppedFrames = stats->GetDroppedFrames();
MOZ_ASSERT(droppedFrames <= parsedFrames);
// Dropped frames <= total frames, so 'percentage' cannot be higher than
// 100 and therefore can fit in a uint32_t (that Telemetry takes).
const uint32_t percentage = 100 * droppedFrames / parsedFrames;
LOG("DROPPED_FRAMES_IN_VIDEO_PLAYBACK = %u", percentage);
Telemetry::Accumulate(Telemetry::VIDEO_DROPPED_FRAMES_PROPORTION,
percentage);
}
}
double TelemetryProbesReporter::GetTotalPlayTimeInSeconds() const {
return mTotalPlayTime.PeekTotal();
}
double TelemetryProbesReporter::GetInvisibleVideoPlayTimeInSeconds() const {
return mInvisibleVideoPlayTime.PeekTotal();
}
double TelemetryProbesReporter::GetVideoDecodeSuspendedTimeInSeconds() const {
return mVideoDecodeSuspendedTime.PeekTotal();
}
#undef LOG
} // namespace mozilla
|