OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MEDIA_BLINK_WATCH_TIME_REPORTER_H_ |
| 6 #define MEDIA_BLINK_WATCH_TIME_REPORTER_H_ |
| 7 |
| 8 #include "base/callback.h" |
| 9 #include "base/power_monitor/power_observer.h" |
| 10 #include "base/time/time.h" |
| 11 #include "base/timer/timer.h" |
| 12 #include "media/base/timestamp_constants.h" |
| 13 #include "media/blink/media_blink_export.h" |
| 14 #include "ui/gfx/geometry/size.h" |
| 15 |
| 16 namespace media { |
| 17 |
| 18 class MEDIA_BLINK_EXPORT WatchTimeReporter : base::PowerObserver { |
| 19 public: |
| 20 using GetMediaTimeCB = base::Callback<base::TimeDelta(void)>; |
| 21 WatchTimeReporter(bool has_audio, |
| 22 bool has_video, |
| 23 bool is_mse, |
| 24 bool is_encrypted, |
| 25 const gfx::Size& initial_video_size, |
| 26 const GetMediaTimeCB& get_media_time_cb); |
| 27 ~WatchTimeReporter() override; |
| 28 |
| 29 void OnPlaying(); |
| 30 void OnPaused(); |
| 31 void OnSeeking(base::TimeDelta seek_timestamp); |
| 32 void OnVolumeChange(double volume); |
| 33 void OnShown(); |
| 34 void OnHidden(); |
| 35 |
| 36 private: |
| 37 friend class WatchTimeReporterTest; |
| 38 |
| 39 // Histogram names used for reporting. Exported for testing purposes only. |
| 40 static const char kHistogramAudioVideoAll[]; |
| 41 static const char kHistogramAudioVideoMse[]; |
| 42 static const char kHistogramAudioVideoEme[]; |
| 43 static const char kHistogramAudioVideoSrc[]; |
| 44 static const char kHistogramAudioVideoBattery[]; |
| 45 static const char kHistogramAudioVideoAc[]; |
| 46 |
| 47 // base::PowerObserver implementation. |
| 48 // |
| 49 // We only observe power source changes. We don't need to observe suspend and |
| 50 // resume events because we report watch time in terms of elapsed media time |
| 51 // and not in terms of elapsed real time. |
| 52 void OnPowerStateChange(bool on_battery_power) override; |
| 53 |
| 54 bool ShouldReportWatchTime(); |
| 55 void StartReportingTimer(base::TimeDelta start_timestamp); |
| 56 enum class FinalizeTime { IMMEDIATELY, ON_NEXT_UPDATE }; |
| 57 void FinalizeWatchTime(FinalizeTime finalize_time); |
| 58 void UpdateWatchTime(); |
| 59 |
| 60 // Initialized during construction. |
| 61 const bool has_audio_; |
| 62 const bool has_video_; |
| 63 const bool is_mse_; |
| 64 const bool is_encrypted_; |
| 65 const gfx::Size initial_video_size_; |
| 66 const GetMediaTimeCB get_media_time_cb_; |
| 67 |
| 68 // The amount of time between each UpdateWatchTime(); this is the frequency by |
| 69 // which the histograms are updated. In the event of a process crash or kill |
| 70 // this is also the most amount of watch time that we might lose. |
| 71 base::TimeDelta reporting_interval_ = base::TimeDelta::FromSeconds(5); |
| 72 |
| 73 base::RepeatingTimer reporting_timer_; |
| 74 |
| 75 // Updated by the OnXXX() methods above. |
| 76 bool is_on_battery_power_ = false; |
| 77 bool is_playing_ = false; |
| 78 bool is_visible_ = true; |
| 79 double volume_ = 1.0; |
| 80 |
| 81 // The last media timestamp seen by UpdateWatchTime(). |
| 82 base::TimeDelta last_media_timestamp_ = kNoTimestamp; |
| 83 |
| 84 // The starting and ending timestamps used for reporting watch time. |
| 85 base::TimeDelta start_timestamp_; |
| 86 base::TimeDelta end_timestamp_ = kNoTimestamp; |
| 87 |
| 88 // Similar to the above but tracks watch time relative to whether or not |
| 89 // battery or AC power is being used. |
| 90 base::TimeDelta start_timestamp_for_power_; |
| 91 base::TimeDelta end_timestamp_for_power_ = kNoTimestamp; |
| 92 |
| 93 DISALLOW_COPY_AND_ASSIGN(WatchTimeReporter); |
| 94 }; |
| 95 |
| 96 } // namespace media |
| 97 |
| 98 #endif // MEDIA_BLINK_WATCH_TIME_REPORTER_H_ |
OLD | NEW |