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 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/media/watch_time_reporter.h"
#include <numeric>
#include <vector>
#include "base/functional/bind.h"
#include "base/power_monitor/power_monitor.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "media/base/pipeline_status.h"
#include "media/base/timestamp_constants.h"
#include "media/base/watch_time_keys.h"
#include "third_party/blink/public/platform/web_media_player.h"
namespace blink {
// The minimum width and height of videos to report watch time metrics for.
constexpr gfx::Size kMinimumVideoSize = gfx::Size(200, 140);
static bool IsOnBatteryPower() {
auto* power_monitor = base::PowerMonitor::GetInstance();
if (!power_monitor->IsInitialized()) {
return false;
}
return power_monitor->GetBatteryPowerStatus() ==
base::PowerStateObserver::BatteryPowerStatus::kBatteryPower;
}
// Helper function for managing property changes. If the watch time timer is
// running it sets the pending value otherwise it sets the current value and
// then returns true if the component needs finalize.
enum class PropertyAction { kNoActionRequired, kFinalizeRequired };
template <typename T>
PropertyAction HandlePropertyChange(T new_value,
bool is_timer_running,
WatchTimeComponent<T>* component) {
if (!component)
return PropertyAction::kNoActionRequired;
if (is_timer_running)
component->SetPendingValue(new_value);
else
component->SetCurrentValue(new_value);
return component->NeedsFinalize() ? PropertyAction::kFinalizeRequired
: PropertyAction::kNoActionRequired;
}
WatchTimeReporter::WatchTimeReporter(
media::mojom::PlaybackPropertiesPtr properties,
const gfx::Size& natural_size,
GetMediaTimeCB get_media_time_cb,
GetPipelineStatsCB get_pipeline_stats_cb,
media::mojom::MediaMetricsProvider* provider,
scoped_refptr<base::SequencedTaskRunner> task_runner,
const base::TickClock* tick_clock)
: WatchTimeReporter(std::move(properties),
false /* is_background */,
false /* is_muted */,
natural_size,
std::move(get_media_time_cb),
std::move(get_pipeline_stats_cb),
provider,
task_runner,
tick_clock) {}
WatchTimeReporter::WatchTimeReporter(
media::mojom::PlaybackPropertiesPtr properties,
bool is_background,
bool is_muted,
const gfx::Size& natural_size,
GetMediaTimeCB get_media_time_cb,
GetPipelineStatsCB get_pipeline_stats_cb,
media::mojom::MediaMetricsProvider* provider,
scoped_refptr<base::SequencedTaskRunner> task_runner,
const base::TickClock* tick_clock)
: properties_(std::move(properties)),
is_background_(is_background),
is_muted_(is_muted),
get_media_time_cb_(std::move(get_media_time_cb)),
get_pipeline_stats_cb_(std::move(get_pipeline_stats_cb)),
reporting_timer_(tick_clock),
natural_size_(natural_size) {
DCHECK(get_media_time_cb_);
DCHECK(get_pipeline_stats_cb_);
DCHECK(properties_->has_audio || properties_->has_video);
DCHECK_EQ(is_background, properties_->is_background);
// The background reporter receives play/pause events instead of visibility
// changes, so it must always be visible to function correctly.
if (is_background_)
DCHECK(is_visible_);
// The muted reporter receives play/pause events instead of volume changes, so
// its volume must always be audible to function correctly.
if (is_muted_)
DCHECK_EQ(volume_, 1.0);
base::PowerMonitor::GetInstance()->AddPowerStateObserver(this);
provider->AcquireWatchTimeRecorder(properties_->Clone(),
recorder_.BindNewPipeAndPassReceiver());
reporting_timer_.SetTaskRunner(task_runner);
base_component_ = CreateBaseComponent();
power_component_ = CreatePowerComponent();
if (!is_background_) {
controls_component_ = CreateControlsComponent();
if (properties_->has_video || properties_->has_audio) {
display_type_component_ = CreateDisplayTypeComponent();
}
}
// If this is a sub-reporter we're done.
if (is_background_ || is_muted_)
return;
// Background watch time is reported by creating an background only watch time
// reporter which receives play when hidden and pause when shown. This avoids
// unnecessary complexity inside the UpdateWatchTime() for handling this case.
auto prop_copy = properties_.Clone();
prop_copy->is_background = true;
background_reporter_.reset(new WatchTimeReporter(
std::move(prop_copy), true /* is_background */, false /* is_muted */,
natural_size_, get_media_time_cb_, get_pipeline_stats_cb_, provider,
task_runner, tick_clock));
// Muted watch time is only reported for audio+video playback.
if (!properties_->has_video || !properties_->has_audio)
return;
// Similar to the above, muted watch time is reported by creating a muted only
// watch time reporter which receives play when muted and pause when audible.
prop_copy = properties_.Clone();
prop_copy->is_muted = true;
muted_reporter_.reset(new WatchTimeReporter(
std::move(prop_copy), false /* is_background */, true /* is_muted */,
natural_size_, get_media_time_cb_, get_pipeline_stats_cb_, provider,
task_runner, tick_clock));
}
WatchTimeReporter::~WatchTimeReporter() {
background_reporter_.reset();
muted_reporter_.reset();
// This is our last chance, so finalize now if there's anything remaining.
in_shutdown_ = true;
MaybeFinalizeWatchTime(FinalizeTime::IMMEDIATELY);
base::PowerMonitor::GetInstance()->RemovePowerStateObserver(this);
}
void WatchTimeReporter::OnPlaying() {
if (background_reporter_ && !is_visible_)
background_reporter_->OnPlaying();
if (muted_reporter_ && !volume_)
muted_reporter_->OnPlaying();
is_playing_ = true;
is_seeking_ = false;
MaybeStartReportingTimer(get_media_time_cb_.Run());
}
void WatchTimeReporter::OnPaused() {
if (background_reporter_)
background_reporter_->OnPaused();
if (muted_reporter_)
muted_reporter_->OnPaused();
is_playing_ = false;
MaybeFinalizeWatchTime(FinalizeTime::ON_NEXT_UPDATE);
}
void WatchTimeReporter::OnSeeking() {
if (background_reporter_)
background_reporter_->OnSeeking();
if (muted_reporter_)
muted_reporter_->OnSeeking();
// Seek is a special case that does not have hysteresis, when this is called
// the seek is imminent, so finalize the previous playback immediately.
is_seeking_ = true;
MaybeFinalizeWatchTime(FinalizeTime::IMMEDIATELY);
}
void WatchTimeReporter::OnVolumeChange(double volume) {
if (background_reporter_)
background_reporter_->OnVolumeChange(volume);
// The muted reporter should never receive volume changes.
DCHECK(!is_muted_);
const double old_volume = volume_;
volume_ = volume;
// We're only interesting in transitions in and out of the muted state.
if (!old_volume && volume) {
if (muted_reporter_)
muted_reporter_->OnPaused();
MaybeStartReportingTimer(get_media_time_cb_.Run());
} else if (old_volume && !volume_) {
if (muted_reporter_ && is_playing_)
muted_reporter_->OnPlaying();
MaybeFinalizeWatchTime(FinalizeTime::ON_NEXT_UPDATE);
}
}
void WatchTimeReporter::OnShown() {
// The background reporter should never receive visibility changes.
DCHECK(!is_background_);
if (background_reporter_)
background_reporter_->OnPaused();
if (muted_reporter_)
muted_reporter_->OnShown();
is_visible_ = true;
MaybeStartReportingTimer(get_media_time_cb_.Run());
}
void WatchTimeReporter::OnHidden() {
// The background reporter should never receive visibility changes.
DCHECK(!is_background_);
if (background_reporter_ && is_playing_)
background_reporter_->OnPlaying();
if (muted_reporter_)
muted_reporter_->OnHidden();
is_visible_ = false;
MaybeFinalizeWatchTime(FinalizeTime::ON_NEXT_UPDATE);
}
void WatchTimeReporter::OnError(media::PipelineStatus status) {
// Since playback should have stopped by this point, go ahead and send the
// error directly instead of on the next timer tick. It won't be recorded
// until finalization anyways.
recorder_->OnError(status);
if (background_reporter_)
background_reporter_->OnError(status);
if (muted_reporter_)
muted_reporter_->OnError(status);
}
void WatchTimeReporter::OnUnderflow() {
if (background_reporter_)
background_reporter_->OnUnderflow();
if (muted_reporter_)
muted_reporter_->OnUnderflow();
if (!reporting_timer_.IsRunning())
return;
if (!pending_underflow_events_.empty())
DCHECK_NE(pending_underflow_events_.back().duration, media::kNoTimestamp);
// In the event of a pending finalize, we don't want to count underflow events
// that occurred after the finalize time. Yet if the finalize is canceled we
// want to ensure they are all recorded.
pending_underflow_events_.push_back(
{false, get_media_time_cb_.Run(), media::kNoTimestamp});
}
void WatchTimeReporter::OnUnderflowComplete(base::TimeDelta elapsed) {
if (background_reporter_)
background_reporter_->OnUnderflowComplete(elapsed);
if (muted_reporter_)
muted_reporter_->OnUnderflowComplete(elapsed);
if (!reporting_timer_.IsRunning())
return;
// Drop this underflow completion if we don't have a corresponding underflow
// start event; this can happen if a finalize occurs between the underflow and
// the completion.
if (pending_underflow_events_.empty())
return;
// There should only ever be one outstanding underflow, so stick the duration
// in the last underflow event.
DCHECK_EQ(pending_underflow_events_.back().duration, media::kNoTimestamp);
pending_underflow_events_.back().duration = elapsed;
}
void WatchTimeReporter::OnNativeControlsEnabled() {
OnNativeControlsChanged(true);
}
void WatchTimeReporter::OnNativeControlsDisabled() {
OnNativeControlsChanged(false);
}
void WatchTimeReporter::OnDisplayTypeInline() {
OnDisplayTypeChanged(WebMediaPlayer::DisplayType::kInline);
}
void WatchTimeReporter::OnDisplayTypeFullscreen() {
OnDisplayTypeChanged(WebMediaPlayer::DisplayType::kFullscreen);
}
void WatchTimeReporter::OnDisplayTypeVideoPictureInPicture() {
OnDisplayTypeChanged(WebMediaPlayer::DisplayType::kVideoPictureInPicture);
}
void WatchTimeReporter::OnDisplayTypeDocumentPictureInPicture() {
OnDisplayTypeChanged(WebMediaPlayer::DisplayType::kDocumentPictureInPicture);
}
void WatchTimeReporter::UpdateSecondaryProperties(
media::mojom::SecondaryPlaybackPropertiesPtr secondary_properties) {
// Flush any unrecorded watch time before updating the secondary properties to
// ensure the UKM record is finalized with up-to-date watch time information.
if (reporting_timer_.IsRunning())
RecordWatchTime();
recorder_->UpdateSecondaryProperties(secondary_properties.Clone());
if (background_reporter_) {
background_reporter_->UpdateSecondaryProperties(
secondary_properties.Clone());
}
if (muted_reporter_)
muted_reporter_->UpdateSecondaryProperties(secondary_properties.Clone());
// A change in resolution may affect ShouldReportingTimerRun().
bool original_should_run = ShouldReportingTimerRun();
natural_size_ = secondary_properties->natural_size;
bool should_run = ShouldReportingTimerRun();
if (original_should_run != should_run) {
if (should_run) {
MaybeStartReportingTimer(get_media_time_cb_.Run());
} else {
MaybeFinalizeWatchTime(FinalizeTime::ON_NEXT_UPDATE);
}
}
}
void WatchTimeReporter::SetAutoplayInitiated(bool autoplay_initiated) {
recorder_->SetAutoplayInitiated(autoplay_initiated);
if (background_reporter_)
background_reporter_->SetAutoplayInitiated(autoplay_initiated);
if (muted_reporter_)
muted_reporter_->SetAutoplayInitiated(autoplay_initiated);
}
void WatchTimeReporter::OnDurationChanged(base::TimeDelta duration) {
recorder_->OnDurationChanged(duration);
if (background_reporter_)
background_reporter_->OnDurationChanged(duration);
if (muted_reporter_)
muted_reporter_->OnDurationChanged(duration);
}
void WatchTimeReporter::OnBatteryPowerStatusChange(
base::PowerStateObserver::BatteryPowerStatus battery_power_status) {
bool battery_power =
(battery_power_status ==
base::PowerStateObserver::BatteryPowerStatus::kBatteryPower);
if (HandlePropertyChange<bool>(battery_power, reporting_timer_.IsRunning(),
power_component_.get()) ==
PropertyAction::kFinalizeRequired) {
RestartTimerForHysteresis();
}
}
void WatchTimeReporter::OnNativeControlsChanged(bool has_native_controls) {
if (muted_reporter_)
muted_reporter_->OnNativeControlsChanged(has_native_controls);
if (HandlePropertyChange<bool>(
has_native_controls, reporting_timer_.IsRunning(),
controls_component_.get()) == PropertyAction::kFinalizeRequired) {
RestartTimerForHysteresis();
}
}
void WatchTimeReporter::OnDisplayTypeChanged(
WebMediaPlayer::DisplayType display_type) {
if (muted_reporter_)
muted_reporter_->OnDisplayTypeChanged(display_type);
if (HandlePropertyChange<WebMediaPlayer::DisplayType>(
display_type, reporting_timer_.IsRunning(),
display_type_component_.get()) == PropertyAction::kFinalizeRequired) {
RestartTimerForHysteresis();
}
}
bool WatchTimeReporter::ShouldReportWatchTime() const {
// Report listen time or watch time for videos of sufficient size.
return properties_->has_video
? (natural_size_.height() >= kMinimumVideoSize.height() &&
natural_size_.width() >= kMinimumVideoSize.width())
: properties_->has_audio;
}
bool WatchTimeReporter::ShouldReportingTimerRun() const {
// TODO(dalecurtis): We should only consider |volume_| when there is actually
// an audio track; requires updating lots of tests to fix.
return ShouldReportWatchTime() && is_playing_ && volume_ && is_visible_ &&
!in_shutdown_ && !is_seeking_ && has_valid_start_timestamp_;
}
void WatchTimeReporter::MaybeStartReportingTimer(
base::TimeDelta start_timestamp) {
DCHECK_GE(start_timestamp, base::TimeDelta());
// It's possible for |current_time| to be kInfiniteDuration here if the page
// seeks to kInfiniteDuration (2**64 - 1) when Duration() is infinite. There
// is no possible elapsed watch time when this occurs, so don't start the
// WatchTimeReporter at this time. If a later seek puts us earlier in the
// stream this method will be called again after OnSeeking().
has_valid_start_timestamp_ = start_timestamp != media::kInfiniteDuration;
// Don't start the timer if our state indicates we shouldn't; this check is
// important since the various event handlers do not have to care about the
// state of other events.
const bool should_start = ShouldReportingTimerRun();
if (reporting_timer_.IsRunning()) {
base_component_->SetPendingValue(should_start);
return;
}
base_component_->SetCurrentValue(should_start);
if (!should_start)
return;
if (properties_->has_video) {
initial_stats_ = get_pipeline_stats_cb_.Run();
last_stats_ = media::PipelineStatistics();
}
ResetUnderflowState();
base_component_->OnReportingStarted(start_timestamp);
power_component_->OnReportingStarted(start_timestamp);
if (controls_component_)
controls_component_->OnReportingStarted(start_timestamp);
if (display_type_component_)
display_type_component_->OnReportingStarted(start_timestamp);
reporting_timer_.Start(FROM_HERE, reporting_interval_, this,
&WatchTimeReporter::UpdateWatchTime);
}
void WatchTimeReporter::MaybeFinalizeWatchTime(FinalizeTime finalize_time) {
if (HandlePropertyChange<bool>(
ShouldReportingTimerRun(), reporting_timer_.IsRunning(),
base_component_.get()) == PropertyAction::kNoActionRequired) {
return;
}
if (finalize_time == FinalizeTime::IMMEDIATELY) {
UpdateWatchTime();
return;
}
// Always restart the timer when finalizing, so that we allow for the full
// length of |kReportingInterval| to elapse for hysteresis purposes.
DCHECK_EQ(finalize_time, FinalizeTime::ON_NEXT_UPDATE);
RestartTimerForHysteresis();
}
void WatchTimeReporter::RestartTimerForHysteresis() {
// Restart the reporting timer so the full hysteresis is afforded.
DCHECK(reporting_timer_.IsRunning());
reporting_timer_.Start(FROM_HERE, reporting_interval_, this,
&WatchTimeReporter::UpdateWatchTime);
}
void WatchTimeReporter::RecordWatchTime() {
// If we're finalizing, use the media time at time of finalization.
const base::TimeDelta current_timestamp =
base_component_->NeedsFinalize() ? base_component_->end_timestamp()
: get_media_time_cb_.Run();
// Pass along any underflow events which have occurred since the last report.
if (!pending_underflow_events_.empty()) {
const int last_underflow_count = total_underflow_count_;
const int last_completed_underflow_count = total_completed_underflow_count_;
for (auto& ufe : pending_underflow_events_) {
// Since the underflow occurred after finalize, ignore the event and mark
// it for deletion.
if (ufe.timestamp > current_timestamp) {
ufe.reported = true;
ufe.duration = base::TimeDelta();
continue;
}
if (!ufe.reported) {
ufe.reported = true;
++total_underflow_count_;
}
// Drop any rebuffer completions that took more than a minute. For our
// purposes these are considered as timeouts. We want a maximum since
// rebuffer duration is in real time and not media time, which means if
// the rebuffer spans a suspend/resume the time can be arbitrarily long.
constexpr base::TimeDelta kMaximumRebufferDuration = base::Minutes(1);
if (ufe.duration != media::kNoTimestamp &&
ufe.duration <= kMaximumRebufferDuration) {
++total_completed_underflow_count_;
total_underflow_duration_ += ufe.duration;
}
}
std::erase_if(pending_underflow_events_, [](const UnderflowEvent& ufe) {
return ufe.reported && ufe.duration != media::kNoTimestamp;
});
if (last_underflow_count != total_underflow_count_)
recorder_->UpdateUnderflowCount(total_underflow_count_);
if (last_completed_underflow_count != total_completed_underflow_count_) {
recorder_->UpdateUnderflowDuration(total_completed_underflow_count_,
total_underflow_duration_);
}
}
if (properties_->has_video) {
auto stats = get_pipeline_stats_cb_.Run();
DCHECK_GE(stats.video_frames_decoded, initial_stats_.video_frames_decoded);
DCHECK_GE(stats.video_frames_dropped, initial_stats_.video_frames_dropped);
// Offset the stats based on where they were when we started reporting.
stats.video_frames_decoded -= initial_stats_.video_frames_decoded;
stats.video_frames_dropped -= initial_stats_.video_frames_dropped;
// Only send updates.
if (last_stats_.video_frames_decoded != stats.video_frames_decoded ||
last_stats_.video_frames_dropped != stats.video_frames_dropped) {
recorder_->UpdateVideoDecodeStats(stats.video_frames_decoded,
stats.video_frames_dropped);
last_stats_ = stats;
}
}
// Record watch time for all components.
base_component_->RecordWatchTime(current_timestamp);
power_component_->RecordWatchTime(current_timestamp);
if (display_type_component_)
display_type_component_->RecordWatchTime(current_timestamp);
if (controls_component_)
controls_component_->RecordWatchTime(current_timestamp);
}
void WatchTimeReporter::UpdateWatchTime() {
// First record watch time.
RecordWatchTime();
// Second, process any pending finalize events.
std::vector<media::WatchTimeKey> keys_to_finalize;
if (power_component_->NeedsFinalize())
power_component_->Finalize(&keys_to_finalize);
if (display_type_component_ && display_type_component_->NeedsFinalize()) {
display_type_component_->Finalize(&keys_to_finalize);
}
if (controls_component_ && controls_component_->NeedsFinalize())
controls_component_->Finalize(&keys_to_finalize);
// Then finalize the base component.
if (!base_component_->NeedsFinalize()) {
if (!keys_to_finalize.empty())
recorder_->FinalizeWatchTime(keys_to_finalize);
return;
}
// Always send finalize, even if we don't currently have any data, it's
// harmless to send since nothing will be logged if we've already finalized.
base_component_->Finalize(&keys_to_finalize);
recorder_->FinalizeWatchTime({});
// Stop the timer if this is supposed to be our last tick.
ResetUnderflowState();
reporting_timer_.Stop();
}
void WatchTimeReporter::ResetUnderflowState() {
total_underflow_count_ = total_completed_underflow_count_ = 0;
total_underflow_duration_ = base::TimeDelta();
pending_underflow_events_.clear();
}
#define NORMAL_KEY(key) \
((properties_->has_video && properties_->has_audio) \
? (is_background_ \
? media::WatchTimeKey::kAudioVideoBackground##key \
: (is_muted_ ? media::WatchTimeKey::kAudioVideoMuted##key \
: media::WatchTimeKey::kAudioVideo##key)) \
: properties_->has_video \
? (is_background_ ? media::WatchTimeKey::kVideoBackground##key \
: media::WatchTimeKey::kVideo##key) \
: (is_background_ ? media::WatchTimeKey::kAudioBackground##key \
: media::WatchTimeKey::kAudio##key))
std::unique_ptr<WatchTimeComponent<bool>>
WatchTimeReporter::CreateBaseComponent() {
std::vector<media::WatchTimeKey> keys_to_finalize;
keys_to_finalize.emplace_back(NORMAL_KEY(All));
if (properties_->has_video && properties_->has_audio && !is_background_ &&
!is_muted_ &&
properties_->renderer_type == media::RendererType::kMediaFoundation) {
keys_to_finalize.emplace_back(
media::WatchTimeKey::kAudioVideoMediaFoundationAll);
if (properties_->is_eme) {
keys_to_finalize.emplace_back(
media::WatchTimeKey::kAudioVideoMediaFoundationEme);
}
}
if (properties_->is_mse)
keys_to_finalize.emplace_back(NORMAL_KEY(Mse));
else
keys_to_finalize.emplace_back(NORMAL_KEY(Src));
if (properties_->is_eme)
keys_to_finalize.emplace_back(NORMAL_KEY(Eme));
if (properties_->is_embedded_media_experience)
keys_to_finalize.emplace_back(NORMAL_KEY(EmbeddedExperience));
return std::make_unique<WatchTimeComponent<bool>>(
false, std::move(keys_to_finalize),
WatchTimeComponent<bool>::ValueToKeyCB(), get_media_time_cb_,
recorder_.get());
}
std::unique_ptr<WatchTimeComponent<bool>>
WatchTimeReporter::CreatePowerComponent() {
std::vector<media::WatchTimeKey> keys_to_finalize{NORMAL_KEY(Battery),
NORMAL_KEY(Ac)};
return std::make_unique<WatchTimeComponent<bool>>(
IsOnBatteryPower(), std::move(keys_to_finalize),
base::BindRepeating(&WatchTimeReporter::GetPowerKey,
base::Unretained(this)),
get_media_time_cb_, recorder_.get());
}
media::WatchTimeKey WatchTimeReporter::GetPowerKey(bool is_on_battery_power) {
return is_on_battery_power ? NORMAL_KEY(Battery) : NORMAL_KEY(Ac);
}
#undef NORMAL_KEY
#define FOREGROUND_KEY(key) \
((properties_->has_video && properties_->has_audio) \
? (is_muted_ ? media::WatchTimeKey::kAudioVideoMuted##key \
: media::WatchTimeKey::kAudioVideo##key) \
: properties_->has_audio ? media::WatchTimeKey::kAudio##key \
: media::WatchTimeKey::kVideo##key)
std::unique_ptr<WatchTimeComponent<bool>>
WatchTimeReporter::CreateControlsComponent() {
DCHECK(!is_background_);
std::vector<media::WatchTimeKey> keys_to_finalize{
FOREGROUND_KEY(NativeControlsOn), FOREGROUND_KEY(NativeControlsOff)};
return std::make_unique<WatchTimeComponent<bool>>(
false, std::move(keys_to_finalize),
base::BindRepeating(&WatchTimeReporter::GetControlsKey,
base::Unretained(this)),
get_media_time_cb_, recorder_.get());
}
media::WatchTimeKey WatchTimeReporter::GetControlsKey(
bool has_native_controls) {
return has_native_controls ? FOREGROUND_KEY(NativeControlsOn)
: FOREGROUND_KEY(NativeControlsOff);
}
#undef FOREGROUND_KEY
#define DISPLAY_TYPE_KEY(key) \
((properties_->has_audio && properties_->has_video) \
? (is_muted_ ? media::WatchTimeKey::kAudioVideoMuted##key \
: media::WatchTimeKey::kAudioVideo##key) \
: properties_->has_audio ? media::WatchTimeKey::kAudio##key \
: media::WatchTimeKey::kVideo##key)
std::unique_ptr<WatchTimeComponent<WebMediaPlayer::DisplayType>>
WatchTimeReporter::CreateDisplayTypeComponent() {
DCHECK(properties_->has_video || properties_->has_audio);
DCHECK(!is_background_);
std::vector<media::WatchTimeKey> keys_to_finalize{
DISPLAY_TYPE_KEY(DisplayInline), DISPLAY_TYPE_KEY(DisplayFullscreen),
DISPLAY_TYPE_KEY(DisplayPictureInPicture)};
if (properties_->has_audio && properties_->has_video) {
keys_to_finalize.emplace_back(
media::WatchTimeKey::kAudioVideoAutoPipMediaPlayback);
}
if (properties_->has_audio && !properties_->has_video) {
keys_to_finalize.emplace_back(
media::WatchTimeKey::kAudioAutoPipMediaPlayback);
}
return std::make_unique<WatchTimeComponent<WebMediaPlayer::DisplayType>>(
WebMediaPlayer::DisplayType::kInline, std::move(keys_to_finalize),
base::BindRepeating(&WatchTimeReporter::GetDisplayTypeKey,
base::Unretained(this)),
get_media_time_cb_, recorder_.get());
}
media::WatchTimeKey WatchTimeReporter::GetDisplayTypeKey(
WebMediaPlayer::DisplayType display_type) {
switch (display_type) {
case WebMediaPlayer::DisplayType::kInline:
return DISPLAY_TYPE_KEY(DisplayInline);
case WebMediaPlayer::DisplayType::kFullscreen:
return DISPLAY_TYPE_KEY(DisplayFullscreen);
case WebMediaPlayer::DisplayType::kVideoPictureInPicture:
case WebMediaPlayer::DisplayType::kDocumentPictureInPicture:
return DISPLAY_TYPE_KEY(DisplayPictureInPicture);
}
}
#undef DISPLAY_TYPE_KEY
} // namespace blink
|