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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
media / mojo / services / watch_time_recorder.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/mojo/services/watch_time_recorder.h"
#include <cmath>
#include "base/containers/contains.h"
#include "base/containers/flat_set.h"
#include "base/hash/hash.h"
#include "base/metrics/histogram_functions.h"
#include "base/ranges/algorithm.h"
#include "media/base/limits.h"
#include "media/base/video_codecs.h"
#include "media/base/video_decoder.h"
#include "media/base/watch_time_keys.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
namespace media {
// The minimum amount of media playback which can elapse before we'll report
// watch time metrics for a playback.
constexpr base::TimeDelta kMinimumElapsedWatchTime =
base::Seconds(limits::kMinimumElapsedWatchTimeSecs);
static void RecordWatchTimeInternal(
std::string_view key,
base::TimeDelta value,
base::TimeDelta minimum = kMinimumElapsedWatchTime) {
DCHECK(!key.empty());
base::UmaHistogramCustomTimes(key, value, minimum, base::Hours(10), 50);
}
static void RecordMeanTimeBetweenRebuffers(std::string_view key,
base::TimeDelta value) {
DCHECK(!key.empty());
// There are a maximum of 5 underflow events possible in a given 7s watch time
// period, so the minimum value is 1.4s.
RecordWatchTimeInternal(key, value, base::Seconds(1.4));
}
static void RecordDiscardedWatchTime(std::string_view key,
base::TimeDelta value) {
DCHECK(!key.empty());
base::UmaHistogramCustomTimes(key, value, base::TimeDelta(),
kMinimumElapsedWatchTime, 50);
}
static void RecordRebuffersCount(std::string_view key, int underflow_count) {
DCHECK(!key.empty());
base::UmaHistogramCounts100(key, underflow_count);
}
WatchTimeRecorder::WatchTimeUkmRecord::WatchTimeUkmRecord(
mojom::SecondaryPlaybackPropertiesPtr properties)
: secondary_properties(std::move(properties)) {}
WatchTimeRecorder::WatchTimeUkmRecord::WatchTimeUkmRecord(
WatchTimeUkmRecord&& record) = default;
WatchTimeRecorder::WatchTimeUkmRecord::~WatchTimeUkmRecord() = default;
WatchTimeRecorder::WatchTimeRecorder(mojom::PlaybackPropertiesPtr properties,
ukm::SourceId source_id,
bool is_top_frame,
uint64_t player_id)
: properties_(std::move(properties)),
source_id_(source_id),
is_top_frame_(is_top_frame),
player_id_(player_id),
extended_metrics_keys_(
{{WatchTimeKey::kAudioSrc, kMeanTimeBetweenRebuffersAudioSrc,
kRebuffersCountAudioSrc, kDiscardedWatchTimeAudioSrc},
{WatchTimeKey::kAudioMse, kMeanTimeBetweenRebuffersAudioMse,
kRebuffersCountAudioMse, kDiscardedWatchTimeAudioMse},
{WatchTimeKey::kAudioEme, kMeanTimeBetweenRebuffersAudioEme,
kRebuffersCountAudioEme, kDiscardedWatchTimeAudioEme},
{WatchTimeKey::kAudioVideoSrc,
kMeanTimeBetweenRebuffersAudioVideoSrc,
kRebuffersCountAudioVideoSrc, kDiscardedWatchTimeAudioVideoSrc},
{WatchTimeKey::kAudioVideoMse,
kMeanTimeBetweenRebuffersAudioVideoMse,
kRebuffersCountAudioVideoMse, kDiscardedWatchTimeAudioVideoMse},
{WatchTimeKey::kAudioVideoEme,
kMeanTimeBetweenRebuffersAudioVideoEme,
kRebuffersCountAudioVideoEme, kDiscardedWatchTimeAudioVideoEme}}) {}
WatchTimeRecorder::~WatchTimeRecorder() {
FinalizeWatchTime({});
RecordUkmPlaybackData();
}
void WatchTimeRecorder::RecordWatchTime(WatchTimeKey key,
base::TimeDelta watch_time) {
watch_time_info_[key] = watch_time;
}
void WatchTimeRecorder::FinalizeWatchTime(
const std::vector<WatchTimeKey>& keys_to_finalize) {
// If the filter set is empty, treat that as finalizing all keys; otherwise
// only the listed keys will be finalized.
const bool should_finalize_everything = keys_to_finalize.empty();
// Record metrics to be finalized, but do not erase them yet; they are still
// needed by for UKM and MTBR recording below.
for (auto& kv : watch_time_info_) {
if (!should_finalize_everything &&
!base::Contains(keys_to_finalize, kv.first)) {
continue;
}
// Report only certain keys to UMA and only if they have at met the minimum
// watch time requirement. Otherwise, for SRC/MSE/EME keys, log them to the
// discard metric.
std::string_view key_str = ConvertWatchTimeKeyToStringForUma(kv.first);
if (ShouldRecordUma() && !key_str.empty()) {
if (kv.second >= kMinimumElapsedWatchTime) {
RecordWatchTimeInternal(key_str, kv.second);
} else if (kv.second.is_positive()) {
auto it = base::ranges::find(extended_metrics_keys_, kv.first,
&ExtendedMetricsKeyMap::watch_time_key);
if (it != extended_metrics_keys_.end())
RecordDiscardedWatchTime(it->discard_key, kv.second);
}
}
// At finalize, update the aggregate entry.
if (!ukm_records_.empty())
ukm_records_.back().aggregate_watch_time_info[kv.first] += kv.second;
}
// If we're not finalizing everything, we're done after removing keys.
if (!should_finalize_everything) {
for (auto key : keys_to_finalize)
watch_time_info_.erase(key);
return;
}
// Check for watch times entries that have corresponding MTBR entries and
// report the MTBR value using watch_time / |underflow_count|. Do this only
// for foreground reporters since we only have UMA keys for foreground.
if (ShouldRecordUma() && !properties_->is_background &&
!properties_->is_muted) {
for (auto& mapping : extended_metrics_keys_) {
auto it = watch_time_info_.find(mapping.watch_time_key);
if (it == watch_time_info_.end() || it->second < kMinimumElapsedWatchTime)
continue;
if (underflow_count_) {
RecordMeanTimeBetweenRebuffers(mapping.mtbr_key,
it->second / underflow_count_);
}
RecordRebuffersCount(mapping.smooth_rate_key, underflow_count_);
}
}
// Ensure values are cleared in case the reporter is reused.
if (!ukm_records_.empty()) {
auto& last_record = ukm_records_.back();
last_record.total_underflow_count += underflow_count_;
last_record.total_completed_underflow_count += completed_underflow_count_;
last_record.total_underflow_duration += underflow_duration_;
last_record.total_video_frames_decoded += video_frames_decoded_;
last_record.total_video_frames_dropped += video_frames_dropped_;
}
video_frames_decoded_ = video_frames_dropped_ = 0;
underflow_count_ = completed_underflow_count_ = 0;
underflow_duration_ = base::TimeDelta();
watch_time_info_.clear();
}
void WatchTimeRecorder::OnError(const PipelineStatus& status) {
pipeline_status_ = status.code();
}
void WatchTimeRecorder::UpdateSecondaryProperties(
mojom::SecondaryPlaybackPropertiesPtr secondary_properties) {
bool last_record_was_unfinalized = false;
if (!ukm_records_.empty()) {
auto& last_record = ukm_records_.back();
// Skip unchanged property updates.
if (secondary_properties->Equals(*last_record.secondary_properties))
return;
// If a property just changes from an unknown to a known value, allow the
// update without creating a whole new record. Not checking
// audio_encryption_scheme and video_encryption_scheme as we want to
// capture changes in encryption schemes.
if (last_record.secondary_properties->audio_codec == AudioCodec::kUnknown ||
last_record.secondary_properties->video_codec == VideoCodec::kUnknown ||
last_record.secondary_properties->audio_codec_profile ==
AudioCodecProfile::kUnknown ||
last_record.secondary_properties->video_codec_profile ==
VIDEO_CODEC_PROFILE_UNKNOWN ||
last_record.secondary_properties->audio_decoder ==
AudioDecoderType::kUnknown ||
last_record.secondary_properties->video_decoder ==
VideoDecoderType::kUnknown) {
auto temp_props = last_record.secondary_properties.Clone();
if (last_record.secondary_properties->audio_codec == AudioCodec::kUnknown)
temp_props->audio_codec = secondary_properties->audio_codec;
if (last_record.secondary_properties->video_codec == VideoCodec::kUnknown)
temp_props->video_codec = secondary_properties->video_codec;
if (last_record.secondary_properties->audio_codec_profile ==
AudioCodecProfile::kUnknown) {
temp_props->audio_codec_profile =
secondary_properties->audio_codec_profile;
}
if (last_record.secondary_properties->video_codec_profile ==
VIDEO_CODEC_PROFILE_UNKNOWN) {
temp_props->video_codec_profile =
secondary_properties->video_codec_profile;
}
if (last_record.secondary_properties->audio_decoder ==
AudioDecoderType::kUnknown) {
temp_props->audio_decoder = secondary_properties->audio_decoder;
}
if (last_record.secondary_properties->video_decoder ==
VideoDecoderType::kUnknown) {
temp_props->video_decoder = secondary_properties->video_decoder;
}
if (temp_props->Equals(*secondary_properties)) {
last_record.secondary_properties = std::move(temp_props);
return;
}
}
// Flush any existing watch time for the current UKM record. The client is
// responsible for ensuring recent watch time has been reported before
// updating the secondary properties.
for (auto& kv : watch_time_info_)
last_record.aggregate_watch_time_info[kv.first] += kv.second;
last_record.total_underflow_count += underflow_count_;
last_record.total_completed_underflow_count += completed_underflow_count_;
last_record.total_underflow_duration += underflow_duration_;
last_record.total_video_frames_decoded += video_frames_decoded_;
last_record.total_video_frames_dropped += video_frames_dropped_;
// If we flushed any watch time or underflow counts which hadn't been
// finalized we'll need to ensure the eventual Finalize() correctly accounts
// for those values at the time of the secondary property update.
last_record_was_unfinalized =
!watch_time_info_.empty() || underflow_count_ ||
completed_underflow_count_ || !underflow_duration_.is_zero() ||
video_frames_decoded_ || video_frames_dropped_;
}
ukm_records_.emplace_back(std::move(secondary_properties));
// We're still in the middle of ongoing watch time updates. So offset the
// future records by their current values; this is done by setting the initial
// value of each unfinalized record to the negative of its current value.
//
// These values will be made positive by the next Finalize() call; which is
// guaranteed to be called at least one more time; either at destruction or by
// the client. This ensures we report the correct amount of watch time that
// has elapsed since the secondary properties were updated.
//
// E.g., consider the case where there's a pending watch time entry for
// kAudioAll=10s and the next RecordWatchTime() call would be kAudioAll=25s.
// Without offsetting, if UpdateSecondaryProperties() is called before the
// next RecordWatchTime() we'll end up recording kAudioAll=25s as the amount
// of watch time for the new set of secondary properties, which isn't correct.
// We instead want to report kAudioAll = 25s - 10s = 15s.
if (last_record_was_unfinalized) {
auto& last_record = ukm_records_.back();
last_record.total_underflow_count = -underflow_count_;
last_record.total_completed_underflow_count = -completed_underflow_count_;
last_record.total_underflow_duration = -underflow_duration_;
last_record.total_video_frames_decoded = -video_frames_decoded_;
last_record.total_video_frames_dropped = -video_frames_dropped_;
for (auto& kv : watch_time_info_)
last_record.aggregate_watch_time_info[kv.first] = -kv.second;
}
}
void WatchTimeRecorder::SetAutoplayInitiated(bool value) {
DCHECK(!autoplay_initiated_.has_value() || value == autoplay_initiated_);
autoplay_initiated_ = value;
}
void WatchTimeRecorder::OnDurationChanged(base::TimeDelta duration) {
duration_ = duration;
}
void WatchTimeRecorder::UpdateVideoDecodeStats(uint32_t video_frames_decoded,
uint32_t video_frames_dropped) {
video_frames_decoded_ = video_frames_decoded;
video_frames_dropped_ = video_frames_dropped;
}
void WatchTimeRecorder::UpdateUnderflowCount(int32_t total_count) {
underflow_count_ = total_count;
}
void WatchTimeRecorder::UpdateUnderflowDuration(
int32_t total_completed_count,
base::TimeDelta total_duration) {
completed_underflow_count_ = total_completed_count;
underflow_duration_ = total_duration;
}
void WatchTimeRecorder::RecordUkmPlaybackData() {
// UKM may be unavailable in content_shell or other non-chrome/ builds; it
// may also be unavailable if browser shutdown has started; so this may be a
// nullptr. If it's unavailable, UKM reporting will be skipped.
ukm::UkmRecorder* ukm_recorder = ukm::UkmRecorder::Get();
if (!ukm_recorder)
return;
// Round duration to the most significant digit in milliseconds for privacy.
std::optional<uint64_t> clamped_duration_ms;
if (duration_ != kNoTimestamp && duration_ != kInfiniteDuration) {
clamped_duration_ms = duration_.InMilliseconds();
if (duration_ > base::Seconds(1)) {
// Turns 54321 => 10000.
const uint64_t base =
std::pow(10, static_cast<uint64_t>(std::log10(*clamped_duration_ms)));
// Turns 54321 => 4321.
const uint64_t modulus = *clamped_duration_ms % base;
// Turns 54321 => 50000 and 55321 => 60000
clamped_duration_ms =
*clamped_duration_ms - modulus + (modulus < base / 2 ? 0 : base);
}
}
base::flat_set<AudioCodecProfile> aac_profiles;
for (auto& ukm_record : ukm_records_) {
ukm::builders::Media_BasicPlayback builder(source_id_);
builder.SetIsTopFrame(is_top_frame_);
builder.SetIsBackground(properties_->is_background);
builder.SetIsMuted(properties_->is_muted);
builder.SetPlayerID(player_id_);
if (clamped_duration_ms.has_value())
builder.SetDuration(*clamped_duration_ms);
bool recorded_all_metric = false;
for (auto& kv : ukm_record.aggregate_watch_time_info) {
DCHECK_GE(kv.second, base::TimeDelta());
if (kv.first == WatchTimeKey::kAudioAll ||
kv.first == WatchTimeKey::kAudioBackgroundAll ||
kv.first == WatchTimeKey::kAudioVideoAll ||
kv.first == WatchTimeKey::kAudioVideoMutedAll ||
kv.first == WatchTimeKey::kAudioVideoBackgroundAll ||
kv.first == WatchTimeKey::kVideoAll ||
kv.first == WatchTimeKey::kVideoBackgroundAll) {
// Only one of these keys should be present.
DCHECK(!recorded_all_metric);
recorded_all_metric = true;
builder.SetWatchTime(kv.second.InMilliseconds());
if (ukm_record.total_underflow_count) {
builder.SetMeanTimeBetweenRebuffers(
(kv.second / ukm_record.total_underflow_count).InMilliseconds());
}
} else if (kv.first == WatchTimeKey::kAudioAc ||
kv.first == WatchTimeKey::kAudioBackgroundAc ||
kv.first == WatchTimeKey::kAudioVideoAc ||
kv.first == WatchTimeKey::kAudioVideoMutedAc ||
kv.first == WatchTimeKey::kAudioVideoBackgroundAc ||
kv.first == WatchTimeKey::kVideoAc ||
kv.first == WatchTimeKey::kVideoBackgroundAc) {
builder.SetWatchTime_AC(kv.second.InMilliseconds());
} else if (kv.first == WatchTimeKey::kAudioBattery ||
kv.first == WatchTimeKey::kAudioBackgroundBattery ||
kv.first == WatchTimeKey::kAudioVideoBattery ||
kv.first == WatchTimeKey::kAudioVideoMutedBattery ||
kv.first == WatchTimeKey::kAudioVideoBackgroundBattery ||
kv.first == WatchTimeKey::kVideoBattery ||
kv.first == WatchTimeKey::kVideoBackgroundBattery) {
builder.SetWatchTime_Battery(kv.second.InMilliseconds());
} else if (kv.first == WatchTimeKey::kAudioNativeControlsOn ||
kv.first == WatchTimeKey::kAudioVideoNativeControlsOn ||
kv.first == WatchTimeKey::kAudioVideoMutedNativeControlsOn ||
kv.first == WatchTimeKey::kVideoNativeControlsOn) {
builder.SetWatchTime_NativeControlsOn(kv.second.InMilliseconds());
} else if (kv.first == WatchTimeKey::kAudioNativeControlsOff ||
kv.first == WatchTimeKey::kAudioVideoNativeControlsOff ||
kv.first == WatchTimeKey::kAudioVideoMutedNativeControlsOff ||
kv.first == WatchTimeKey::kVideoNativeControlsOff) {
builder.SetWatchTime_NativeControlsOff(kv.second.InMilliseconds());
} else if (kv.first == WatchTimeKey::kAudioVideoDisplayFullscreen ||
kv.first == WatchTimeKey::kAudioVideoMutedDisplayFullscreen ||
kv.first == WatchTimeKey::kVideoDisplayFullscreen) {
builder.SetWatchTime_DisplayFullscreen(kv.second.InMilliseconds());
} else if (kv.first == WatchTimeKey::kAudioVideoDisplayInline ||
kv.first == WatchTimeKey::kAudioVideoMutedDisplayInline ||
kv.first == WatchTimeKey::kVideoDisplayInline) {
builder.SetWatchTime_DisplayInline(kv.second.InMilliseconds());
} else if (kv.first == WatchTimeKey::kAudioVideoDisplayPictureInPicture ||
kv.first ==
WatchTimeKey::kAudioVideoMutedDisplayPictureInPicture ||
kv.first == WatchTimeKey::kVideoDisplayPictureInPicture) {
builder.SetWatchTime_DisplayPictureInPicture(
kv.second.InMilliseconds());
}
}
// See note in mojom::PlaybackProperties about why we have both of these.
builder.SetAudioCodec(
static_cast<int64_t>(ukm_record.secondary_properties->audio_codec));
builder.SetVideoCodec(
static_cast<int64_t>(ukm_record.secondary_properties->video_codec));
builder.SetAudioCodecProfile(static_cast<int64_t>(
ukm_record.secondary_properties->audio_codec_profile));
builder.SetVideoCodecProfile(
ukm_record.secondary_properties->video_codec_profile);
builder.SetHasAudio(properties_->has_audio);
builder.SetHasVideo(properties_->has_video);
if (ukm_record.secondary_properties->audio_codec == AudioCodec::kAAC)
aac_profiles.insert(ukm_record.secondary_properties->audio_codec_profile);
builder.SetAudioDecoderName(
static_cast<int64_t>(ukm_record.secondary_properties->audio_decoder));
builder.SetVideoDecoderName(
static_cast<int64_t>(ukm_record.secondary_properties->video_decoder));
builder.SetAudioEncryptionScheme(static_cast<int64_t>(
ukm_record.secondary_properties->audio_encryption_scheme));
builder.SetVideoEncryptionScheme(static_cast<int64_t>(
ukm_record.secondary_properties->video_encryption_scheme));
builder.SetIsEME(properties_->is_eme);
builder.SetIsMSE(properties_->is_mse);
builder.SetMediaStreamType(
static_cast<int64_t>(properties_->media_stream_type));
builder.SetLastPipelineStatus(pipeline_status_);
builder.SetRebuffersCount(ukm_record.total_underflow_count);
builder.SetCompletedRebuffersCount(
ukm_record.total_completed_underflow_count);
builder.SetCompletedRebuffersDuration(
ukm_record.total_underflow_duration.InMilliseconds());
builder.SetVideoFramesDecoded(ukm_record.total_video_frames_decoded);
builder.SetVideoFramesDropped(ukm_record.total_video_frames_dropped);
builder.SetVideoNaturalWidth(
ukm_record.secondary_properties->natural_size.width());
builder.SetVideoNaturalHeight(
ukm_record.secondary_properties->natural_size.height());
builder.SetAutoplayInitiated(autoplay_initiated_.value_or(false));
builder.Record(ukm_recorder);
}
if (ShouldRecordUma() && !aac_profiles.empty()) {
for (auto profile : aac_profiles)
base::UmaHistogramEnumeration("Media.AudioCodecProfile.AAC", profile);
}
ukm_records_.clear();
}
bool WatchTimeRecorder::ShouldRecordUma() const {
return properties_->media_stream_type == mojom::MediaStreamType::kNone;
}
WatchTimeRecorder::ExtendedMetricsKeyMap::ExtendedMetricsKeyMap(
const ExtendedMetricsKeyMap& copy)
: ExtendedMetricsKeyMap(copy.watch_time_key,
copy.mtbr_key,
copy.smooth_rate_key,
copy.discard_key) {}
WatchTimeRecorder::ExtendedMetricsKeyMap::ExtendedMetricsKeyMap(
WatchTimeKey watch_time_key,
std::string_view mtbr_key,
std::string_view smooth_rate_key,
std::string_view discard_key)
: watch_time_key(watch_time_key),
mtbr_key(mtbr_key),
smooth_rate_key(smooth_rate_key),
discard_key(discard_key) {}
} // namespace media