OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_RENDERER_MEDIA_RENDERER_WEBMEDIAPLAYER_DELEGATE_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_RENDERER_WEBMEDIAPLAYER_DELEGATE_H_ |
6 #define CONTENT_RENDERER_MEDIA_RENDERER_WEBMEDIAPLAYER_DELEGATE_H_ | 6 #define CONTENT_RENDERER_MEDIA_RENDERER_WEBMEDIAPLAYER_DELEGATE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <set> | 10 #include <set> |
11 | 11 |
12 #include "base/id_map.h" | 12 #include "base/id_map.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" |
14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/single_thread_task_runner.h" |
15 #include "base/time/default_tick_clock.h" | 17 #include "base/time/default_tick_clock.h" |
16 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
17 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
18 #include "content/public/renderer/render_frame_observer.h" | 20 #include "content/public/renderer/render_frame_observer.h" |
19 #include "media/blink/webmediaplayer_delegate.h" | 21 #include "media/blink/webmediaplayer_delegate.h" |
20 | 22 |
21 #if defined(OS_ANDROID) | 23 #if defined(OS_ANDROID) |
22 #include "base/time/time.h" | 24 #include "base/time/time.h" |
23 #endif // OS_ANDROID | 25 #endif // OS_ANDROID |
24 | 26 |
25 namespace media { | 27 namespace media { |
26 | 28 |
27 enum class MediaContentType; | 29 enum class MediaContentType; |
28 | 30 |
29 // An interface to allow a WebMediaPlayerImpl to communicate changes of state | 31 // Standard implementation of WebMediaPlayerDelegate; communicates state to |
30 // to objects that need to know. | 32 // the MediaPlayerDelegateHost. |
31 class CONTENT_EXPORT RendererWebMediaPlayerDelegate | 33 class CONTENT_EXPORT RendererWebMediaPlayerDelegate |
32 : public content::RenderFrameObserver, | 34 : public content::RenderFrameObserver, |
33 public NON_EXPORTED_BASE(WebMediaPlayerDelegate), | 35 public NON_EXPORTED_BASE(WebMediaPlayerDelegate), |
34 public NON_EXPORTED_BASE( | 36 public NON_EXPORTED_BASE( |
35 base::SupportsWeakPtr<RendererWebMediaPlayerDelegate>) { | 37 base::SupportsWeakPtr<RendererWebMediaPlayerDelegate>) { |
36 public: | 38 public: |
37 explicit RendererWebMediaPlayerDelegate(content::RenderFrame* render_frame); | 39 explicit RendererWebMediaPlayerDelegate(content::RenderFrame* render_frame); |
38 ~RendererWebMediaPlayerDelegate() override; | 40 ~RendererWebMediaPlayerDelegate() override; |
39 | 41 |
40 // Returns true if this RenderFrame has ever seen media playback before. | 42 // Returns true if this RenderFrame has ever seen media playback before. |
41 bool has_played_media() const { return has_played_media_; } | 43 bool has_played_media() const { return has_played_media_; } |
42 | 44 |
43 // WebMediaPlayerDelegate implementation. | 45 // WebMediaPlayerDelegate implementation. |
| 46 bool IsFrameHidden() override; |
| 47 bool IsFrameClosed() override; |
| 48 bool IsBackgroundVideoPlaybackUnlocked() override; |
44 int AddObserver(Observer* observer) override; | 49 int AddObserver(Observer* observer) override; |
45 void RemoveObserver(int delegate_id) override; | 50 void RemoveObserver(int player_id) override; |
46 void DidPlay(int delegate_id, | 51 void DidPlay(int player_id, |
47 bool has_video, | 52 bool has_video, |
48 bool has_audio, | 53 bool has_audio, |
49 bool is_remote, | |
50 MediaContentType media_content_type) override; | 54 MediaContentType media_content_type) override; |
51 void DidPause(int delegate_id, bool reached_end_of_stream) override; | 55 void DidPause(int player_id) override; |
52 void PlayerGone(int delegate_id) override; | 56 void PlayerGone(int player_id) override; |
53 bool IsHidden() override; | 57 void SetIdle(int player_id, bool is_idle) override; |
54 bool IsPlayingBackgroundVideo() override; | 58 bool IsIdle(int player_id) override; |
| 59 void ClearStaleFlag(int player_id) override; |
| 60 bool IsStale(int player_id) override; |
55 | 61 |
56 // content::RenderFrameObserver overrides. | 62 // content::RenderFrameObserver overrides. |
57 void WasHidden() override; | 63 void WasHidden() override; |
58 void WasShown() override; | 64 void WasShown() override; |
59 bool OnMessageReceived(const IPC::Message& msg) override; | 65 bool OnMessageReceived(const IPC::Message& msg) override; |
60 void OnDestruct() override; | 66 void OnDestruct() override; |
61 | 67 |
62 // Zeros out |idle_cleanup_interval_|, sets |idle_timeout_| to |idle_timeout|, | 68 // Zeros out |idle_cleanup_interval_|, sets |idle_timeout_| to |idle_timeout|, |
63 // and |is_low_end_device_| to |is_low_end_device|. A zero cleanup interval | 69 // and |is_low_end_device_| to |is_low_end_device|. A zero cleanup interval |
64 // will cause the idle timer to run with each run of the message loop. | 70 // will cause the idle timer to run with each run of the message loop. |
65 void SetIdleCleanupParamsForTesting(base::TimeDelta idle_timeout, | 71 void SetIdleCleanupParamsForTesting(base::TimeDelta idle_timeout, |
66 base::TickClock* tick_clock, | 72 base::TickClock* tick_clock, |
67 bool is_low_end_device); | 73 bool is_low_end_device); |
68 bool IsIdleCleanupTimerRunningForTesting() const { | 74 bool IsIdleCleanupTimerRunningForTesting() const; |
69 return idle_cleanup_timer_.IsRunning(); | 75 |
70 } | 76 // Note: Does not call OnFrameHidden()/OnFrameShown(). |
| 77 void SetFrameHiddenForTesting(bool is_hidden); |
71 | 78 |
72 friend class RendererWebMediaPlayerDelegateTest; | 79 friend class RendererWebMediaPlayerDelegateTest; |
73 | 80 |
74 private: | 81 private: |
75 void OnMediaDelegatePause(int delegate_id); | 82 void OnMediaDelegatePause(int player_id); |
76 void OnMediaDelegatePlay(int delegate_id); | 83 void OnMediaDelegatePlay(int player_id); |
77 void OnMediaDelegateSuspendAllMediaPlayers(); | 84 void OnMediaDelegateSuspendAllMediaPlayers(); |
78 void OnMediaDelegateVolumeMultiplierUpdate(int delegate_id, | 85 void OnMediaDelegateVolumeMultiplierUpdate(int player_id, double multiplier); |
79 double multiplier); | |
80 | 86 |
81 // Adds or removes a delegate from |idle_delegate_map_|. The first insertion | 87 // Schedules UpdateTask() to run soon. |
82 // or last removal will start or stop |idle_cleanup_timer_| respectively. | 88 void ScheduleUpdateTask(); |
83 void AddIdleDelegate(int delegate_id); | |
84 void RemoveIdleDelegate(int delegate_id); | |
85 | 89 |
86 // Runs periodically to suspend idle delegates in |idle_delegate_map_| which | 90 // Processes state changes, dispatches CleanupIdlePlayers(). |
| 91 void UpdateTask(); |
| 92 |
| 93 // Records UMAs about background playback. |
| 94 void RecordBackgroundVideoPlayback(); |
| 95 |
| 96 // Runs periodically to notify stale players in |idle_player_map_| which |
87 // have been idle for longer than |timeout|. | 97 // have been idle for longer than |timeout|. |
88 void CleanupIdleDelegates(base::TimeDelta timeout); | 98 void CleanUpIdlePlayers(base::TimeDelta timeout); |
89 | 99 |
90 // Setter for |is_playing_background_video_| that updates the metrics. | 100 // True if any media has ever been played in this render frame. Affects |
91 void SetIsPlayingBackgroundVideo(bool is_playing); | 101 // autoplay logic in RenderFrameImpl. |
| 102 bool has_played_media_ = false; |
92 | 103 |
93 bool has_played_media_ = false; | 104 bool background_video_allowed_ = false; |
| 105 bool is_frame_closed_ = false; |
| 106 bool is_frame_hidden_for_testing_ = false; |
| 107 |
| 108 // State related to scheduling UpdateTask(). These are cleared each time |
| 109 // UpdateTask() runs. |
| 110 bool has_played_video_ = false; |
| 111 bool pending_update_task_ = false; |
| 112 |
94 IDMap<Observer*> id_map_; | 113 IDMap<Observer*> id_map_; |
95 | 114 |
96 // Tracks which delegates have entered an idle state. After some period of | 115 // Tracks which players have entered an idle state. After some period of |
97 // inactivity these players will be suspended to release unused resources. | 116 // inactivity these players will be notified and become stale. |
98 bool idle_cleanup_running_ = false; | 117 std::map<int, base::TimeTicks> idle_player_map_; |
99 std::map<int, base::TimeTicks> idle_delegate_map_; | 118 std::set<int> stale_players_; |
100 base::Timer idle_cleanup_timer_; | 119 base::OneShotTimer idle_cleanup_timer_; |
101 | 120 |
102 // Amount of time allowed to elapse after a delegate enters the paused before | 121 // Amount of time allowed to elapse after a player becomes idle before |
103 // the delegate is suspended. | 122 // it can transition to stale. |
104 base::TimeDelta idle_timeout_; | 123 base::TimeDelta idle_timeout_; |
105 | 124 |
106 // The polling interval used for checking the delegates to see if any have | 125 // The polling interval used for checking the players to see if any have |
107 // exceeded |idle_timeout_| since their last pause state. | 126 // exceeded |idle_timeout_| since becoming idle. |
108 base::TimeDelta idle_cleanup_interval_; | 127 base::TimeDelta idle_cleanup_interval_; |
109 | 128 |
110 // Clock used for calculating when delegates have expired. May be overridden | 129 // Clock used for calculating when players have become stale. May be |
111 // for testing. | 130 // overridden for testing. |
112 std::unique_ptr<base::DefaultTickClock> default_tick_clock_; | 131 std::unique_ptr<base::DefaultTickClock> default_tick_clock_; |
113 base::TickClock* tick_clock_; | 132 base::TickClock* tick_clock_; |
114 | 133 |
115 // If a video is playing in the background. Set when user resumes a video | 134 #if defined(OS_ANDROID) |
116 // allowing it to play and reset when either user pauses it or it goes | 135 bool was_playing_background_video_ = false; |
117 // foreground. | |
118 bool is_playing_background_video_ = false; | |
119 | 136 |
120 #if defined(OS_ANDROID) | |
121 // Keeps track of when the background video playback started for metrics. | 137 // Keeps track of when the background video playback started for metrics. |
122 base::TimeTicks background_video_playing_start_time_; | 138 base::TimeTicks background_video_start_time_; |
123 #endif // OS_ANDROID | 139 #endif // OS_ANDROID |
124 | 140 |
125 // The currently playing local videos. Used to determine whether | 141 // The currently playing local videos. Used to determine whether |
126 // OnMediaDelegatePlay() should allow the videos to play in the background or | 142 // OnMediaDelegatePlay() should allow the videos to play in the background or |
127 // not. | 143 // not. |
128 std::set<int> playing_videos_; | 144 std::set<int> playing_videos_; |
129 | 145 |
130 // Determined at construction time based on system information; determines | 146 // Determined at construction time based on system information; determines |
131 // when the idle cleanup timer should be fired more aggressively. | 147 // when the idle cleanup timer should be fired more aggressively. |
132 bool is_low_end_device_; | 148 bool is_low_end_device_; |
133 | 149 |
134 DISALLOW_COPY_AND_ASSIGN(RendererWebMediaPlayerDelegate); | 150 DISALLOW_COPY_AND_ASSIGN(RendererWebMediaPlayerDelegate); |
135 }; | 151 }; |
136 | 152 |
137 } // namespace media | 153 } // namespace media |
138 | 154 |
139 #endif // CONTENT_RENDERER_MEDIA_RENDERER_WEBMEDIAPLAYER_DELEGATE_H_ | 155 #endif // CONTENT_RENDERER_MEDIA_RENDERER_WEBMEDIAPLAYER_DELEGATE_H_ |
OLD | NEW |