| 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> |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 void PlayerGone(int delegate_id) override; | 56 void PlayerGone(int delegate_id) override; |
| 57 bool IsHidden() override; | 57 bool IsHidden() override; |
| 58 bool IsPlayingBackgroundVideo() override; | 58 bool IsPlayingBackgroundVideo() override; |
| 59 | 59 |
| 60 // content::RenderFrameObserver overrides. | 60 // content::RenderFrameObserver overrides. |
| 61 void WasHidden() override; | 61 void WasHidden() override; |
| 62 void WasShown() override; | 62 void WasShown() override; |
| 63 bool OnMessageReceived(const IPC::Message& msg) override; | 63 bool OnMessageReceived(const IPC::Message& msg) override; |
| 64 void OnDestruct() override; | 64 void OnDestruct() override; |
| 65 | 65 |
| 66 // Zeros out |idle_cleanup_interval_|, and sets |idle_timeout_| to | 66 // Zeros out |idle_cleanup_interval_|, sets |idle_timeout_| to |idle_timeout|, |
| 67 // |idle_timeout|. A zero cleanup interval will cause the idle timer to run | 67 // and |is_low_end_device_| to |is_low_end_device|. A zero cleanup interval |
| 68 // with each run of the message loop. | 68 // will cause the idle timer to run with each run of the message loop. |
| 69 void SetIdleCleanupParamsForTesting(base::TimeDelta idle_timeout, | 69 void SetIdleCleanupParamsForTesting(base::TimeDelta idle_timeout, |
| 70 base::TickClock* tick_clock); | 70 base::TickClock* tick_clock, |
| 71 bool is_low_end_device); |
| 71 bool IsIdleCleanupTimerRunningForTesting() const { | 72 bool IsIdleCleanupTimerRunningForTesting() const { |
| 72 return idle_cleanup_timer_.IsRunning(); | 73 return idle_cleanup_timer_.IsRunning(); |
| 73 } | 74 } |
| 74 | 75 |
| 75 friend class RendererWebMediaPlayerDelegateTest; | 76 friend class RendererWebMediaPlayerDelegateTest; |
| 76 | 77 |
| 77 private: | 78 private: |
| 78 void OnMediaDelegatePause(int delegate_id); | 79 void OnMediaDelegatePause(int delegate_id); |
| 79 void OnMediaDelegatePlay(int delegate_id); | 80 void OnMediaDelegatePlay(int delegate_id); |
| 80 void OnMediaDelegateSuspendAllMediaPlayers(); | 81 void OnMediaDelegateSuspendAllMediaPlayers(); |
| 81 void OnMediaDelegateVolumeMultiplierUpdate(int delegate_id, | 82 void OnMediaDelegateVolumeMultiplierUpdate(int delegate_id, |
| 82 double multiplier); | 83 double multiplier); |
| 83 | 84 |
| 84 // Adds or removes a delegate from |idle_delegate_map_|. The first insertion | 85 // Adds or removes a delegate from |idle_delegate_map_|. The first insertion |
| 85 // or last removal will start or stop |idle_cleanup_timer_| respectively. | 86 // or last removal will start or stop |idle_cleanup_timer_| respectively. |
| 86 void AddIdleDelegate(int delegate_id); | 87 void AddIdleDelegate(int delegate_id); |
| 87 void RemoveIdleDelegate(int delegate_id); | 88 void RemoveIdleDelegate(int delegate_id); |
| 88 | 89 |
| 89 // Runs periodically to suspend idle delegates in |idle_delegate_map_|. | 90 // Runs periodically to suspend idle delegates in |idle_delegate_map_| which |
| 90 void CleanupIdleDelegates(); | 91 // have been idle for longer than |timeout|. |
| 92 void CleanupIdleDelegates(base::TimeDelta timeout); |
| 91 | 93 |
| 92 // Setter for |is_playing_background_video_| that updates the metrics. | 94 // Setter for |is_playing_background_video_| that updates the metrics. |
| 93 void SetIsPlayingBackgroundVideo(bool is_playing); | 95 void SetIsPlayingBackgroundVideo(bool is_playing); |
| 94 | 96 |
| 95 bool has_played_media_ = false; | 97 bool has_played_media_ = false; |
| 96 IDMap<Observer> id_map_; | 98 IDMap<Observer> id_map_; |
| 97 | 99 |
| 98 // Tracks which delegates have entered an idle state. After some period of | 100 // Tracks which delegates have entered an idle state. After some period of |
| 99 // inactivity these players will be suspended to release unused resources. | 101 // inactivity these players will be suspended to release unused resources. |
| 100 bool idle_cleanup_running_ = false; | 102 bool idle_cleanup_running_ = false; |
| 101 std::map<int, base::TimeTicks> idle_delegate_map_; | 103 std::map<int, base::TimeTicks> idle_delegate_map_; |
| 102 base::RepeatingTimer idle_cleanup_timer_; | 104 base::Timer idle_cleanup_timer_; |
| 103 | 105 |
| 104 // Amount of time allowed to elapse after a delegate enters the paused before | 106 // Amount of time allowed to elapse after a delegate enters the paused before |
| 105 // the delegate is suspended. | 107 // the delegate is suspended. |
| 106 base::TimeDelta idle_timeout_; | 108 base::TimeDelta idle_timeout_; |
| 107 | 109 |
| 108 // The polling interval used for checking the delegates to see if any have | 110 // The polling interval used for checking the delegates to see if any have |
| 109 // exceeded |idle_timeout_| since their last pause state. | 111 // exceeded |idle_timeout_| since their last pause state. |
| 110 base::TimeDelta idle_cleanup_interval_; | 112 base::TimeDelta idle_cleanup_interval_; |
| 111 | 113 |
| 112 // Clock used for calculating when delegates have expired. May be overridden | 114 // Clock used for calculating when delegates have expired. May be overridden |
| 113 // for testing. | 115 // for testing. |
| 114 std::unique_ptr<base::DefaultTickClock> default_tick_clock_; | 116 std::unique_ptr<base::DefaultTickClock> default_tick_clock_; |
| 115 base::TickClock* tick_clock_; | 117 base::TickClock* tick_clock_; |
| 116 | 118 |
| 117 // If a video is playing in the background. Set when user resumes a video | 119 // If a video is playing in the background. Set when user resumes a video |
| 118 // allowing it to play and reset when either user pauses it or it goes | 120 // allowing it to play and reset when either user pauses it or it goes |
| 119 // foreground. | 121 // foreground. |
| 120 bool is_playing_background_video_ = false; | 122 bool is_playing_background_video_ = false; |
| 121 | 123 |
| 122 #if defined(OS_ANDROID) | 124 #if defined(OS_ANDROID) |
| 123 // Keeps track of when the background video playback started for metrics. | 125 // Keeps track of when the background video playback started for metrics. |
| 124 base::TimeTicks background_video_playing_start_time_; | 126 base::TimeTicks background_video_playing_start_time_; |
| 125 #endif // OS_ANDROID | 127 #endif // OS_ANDROID |
| 126 | 128 |
| 127 // The currently playing local videos. Used to determine whether | 129 // The currently playing local videos. Used to determine whether |
| 128 // OnMediaDelegatePlay() should allow the videos to play in the background or | 130 // OnMediaDelegatePlay() should allow the videos to play in the background or |
| 129 // not. | 131 // not. |
| 130 std::set<int> playing_videos_; | 132 std::set<int> playing_videos_; |
| 131 | 133 |
| 134 // Determined at construction time based on system information; determines |
| 135 // when the idle cleanup timer should be fired more aggressively. |
| 136 bool is_low_end_device_; |
| 137 |
| 132 DISALLOW_COPY_AND_ASSIGN(RendererWebMediaPlayerDelegate); | 138 DISALLOW_COPY_AND_ASSIGN(RendererWebMediaPlayerDelegate); |
| 133 }; | 139 }; |
| 134 | 140 |
| 135 } // namespace media | 141 } // namespace media |
| 136 | 142 |
| 137 #endif // CONTENT_RENDERER_MEDIA_RENDERER_WEBMEDIAPLAYER_DELEGATE_H_ | 143 #endif // CONTENT_RENDERER_MEDIA_RENDERER_WEBMEDIAPLAYER_DELEGATE_H_ |
| OLD | NEW |