Index: media/blink/watch_time_reporter.h |
diff --git a/media/blink/watch_time_reporter.h b/media/blink/watch_time_reporter.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5b1e436c0174f328430931e666066687172f8ef0 |
--- /dev/null |
+++ b/media/blink/watch_time_reporter.h |
@@ -0,0 +1,98 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef MEDIA_BLINK_WATCH_TIME_REPORTER_H_ |
+#define MEDIA_BLINK_WATCH_TIME_REPORTER_H_ |
+ |
+#include "base/callback.h" |
+#include "base/power_monitor/power_observer.h" |
+#include "base/time/time.h" |
+#include "base/timer/timer.h" |
+#include "media/base/timestamp_constants.h" |
+#include "media/blink/media_blink_export.h" |
+#include "ui/gfx/geometry/size.h" |
+ |
+namespace media { |
+ |
+class MEDIA_BLINK_EXPORT WatchTimeReporter : base::PowerObserver { |
+ public: |
+ using GetMediaTimeCB = base::Callback<base::TimeDelta(void)>; |
+ WatchTimeReporter(bool has_audio, |
+ bool has_video, |
+ bool is_mse, |
+ bool is_encrypted, |
+ const gfx::Size& initial_video_size, |
+ const GetMediaTimeCB& get_media_time_cb); |
+ ~WatchTimeReporter() override; |
+ |
+ void OnPlaying(); |
+ void OnPaused(); |
+ void OnSeeking(base::TimeDelta seek_timestamp); |
+ void OnVolumeChange(double volume); |
+ void OnShown(); |
+ void OnHidden(); |
+ |
+ private: |
+ friend class WatchTimeReporterTest; |
+ |
+ // Histogram names used for reporting. Exported for testing purposes only. |
+ static const char kHistogramAudioVideoAll[]; |
+ static const char kHistogramAudioVideoMse[]; |
+ static const char kHistogramAudioVideoEme[]; |
+ static const char kHistogramAudioVideoSrc[]; |
+ static const char kHistogramAudioVideoBattery[]; |
+ static const char kHistogramAudioVideoAc[]; |
+ |
+ // base::PowerObserver implementation. |
+ // |
+ // We only observe power source changes. We don't need to observe suspend and |
+ // resume events because we report watch time in terms of elapsed media time |
+ // and not in terms of elapsed real time. |
+ void OnPowerStateChange(bool on_battery_power) override; |
+ |
+ bool ShouldReportWatchTime(); |
+ void StartReportingTimer(base::TimeDelta start_timestamp); |
+ enum class FinalizeTime { IMMEDIATELY, ON_NEXT_UPDATE }; |
+ void FinalizeWatchTime(FinalizeTime finalize_time); |
+ void UpdateWatchTime(); |
+ |
+ // Initialized during construction. |
+ const bool has_audio_; |
+ const bool has_video_; |
+ const bool is_mse_; |
+ const bool is_encrypted_; |
+ const gfx::Size initial_video_size_; |
+ const GetMediaTimeCB get_media_time_cb_; |
+ |
+ // The amount of time between each UpdateWatchTime(); this is the frequency by |
+ // which the histograms are updated. In the event of a process crash or kill |
+ // this is also the most amount of watch time that we might lose. |
+ base::TimeDelta reporting_interval_ = base::TimeDelta::FromSeconds(5); |
+ |
+ base::RepeatingTimer reporting_timer_; |
+ |
+ // Updated by the OnXXX() methods above. |
+ bool is_on_battery_power_ = false; |
+ bool is_playing_ = false; |
+ bool is_visible_ = true; |
+ double volume_ = 1.0; |
+ |
+ // The last media timestamp seen by UpdateWatchTime(). |
+ base::TimeDelta last_media_timestamp_ = kNoTimestamp; |
+ |
+ // The starting and ending timestamps used for reporting watch time. |
+ base::TimeDelta start_timestamp_; |
+ base::TimeDelta end_timestamp_ = kNoTimestamp; |
+ |
+ // Similar to the above but tracks watch time relative to whether or not |
+ // battery or AC power is being used. |
+ base::TimeDelta start_timestamp_for_power_; |
+ base::TimeDelta end_timestamp_for_power_ = kNoTimestamp; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(WatchTimeReporter); |
+}; |
+ |
+} // namespace media |
+ |
+#endif // MEDIA_BLINK_WATCH_TIME_REPORTER_H_ |