Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(466)

Side by Side Diff: content/renderer/media/renderer_webmediaplayer_delegate.h

Issue 2490783002: Refactor WebMediaPlayerDelegate interface. (Closed)
Patch Set: Clarify comments and names. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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,
52 bool has_audio,
DaleCurtis 2017/01/10 21:28:23 Why did you reorder these? The order matches that
sandersd (OOO until July 31) 2017/01/11 01:22:50 Done.
47 bool has_video, 53 bool has_video,
48 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;
DaleCurtis 2017/01/10 21:28:23 Do we need all these ForTesting() methods if the t
sandersd (OOO until July 31) 2017/01/11 01:22:50 I suppose not; at the very least they can be made
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
90 // Setter for |is_playing_background_video_| that updates the metrics.
91 void SetIsPlayingBackgroundVideo(bool is_playing);
92 99
93 bool has_played_media_ = false; 100 bool has_played_media_ = false;
101 bool background_video_allowed_ = false;
102 bool is_frame_closed_ = false;
103 bool is_frame_hidden_for_testing_ = false;
104
105 // State related to scheduling UpdateTask().
106 bool did_play_video_ = false;
DaleCurtis 2017/01/10 21:28:23 has_played_video_ for consistency?
sandersd (OOO until July 31) 2017/01/11 01:22:50 Done.
107 bool pending_update_task_ = false;
108
94 IDMap<Observer*> id_map_; 109 IDMap<Observer*> id_map_;
95 110
96 // Tracks which delegates have entered an idle state. After some period of 111 // Tracks which players have entered an idle state. After some period of
97 // inactivity these players will be suspended to release unused resources. 112 // inactivity these players will be notified and become stale.
98 bool idle_cleanup_running_ = false; 113 std::map<int, base::TimeTicks> idle_player_map_;
99 std::map<int, base::TimeTicks> idle_delegate_map_; 114 std::set<int> stale_players_;
100 base::Timer idle_cleanup_timer_; 115 base::OneShotTimer idle_cleanup_timer_;
101 116
102 // Amount of time allowed to elapse after a delegate enters the paused before 117 // Amount of time allowed to elapse after a player becomes idle before
103 // the delegate is suspended. 118 // it can transition to stale.
104 base::TimeDelta idle_timeout_; 119 base::TimeDelta idle_timeout_;
105 120
106 // The polling interval used for checking the delegates to see if any have 121 // The polling interval used for checking the players to see if any have
107 // exceeded |idle_timeout_| since their last pause state. 122 // exceeded |idle_timeout_| since becoming idle.
108 base::TimeDelta idle_cleanup_interval_; 123 base::TimeDelta idle_cleanup_interval_;
109 124
110 // Clock used for calculating when delegates have expired. May be overridden 125 // Clock used for calculating when players have become stale. May be
111 // for testing. 126 // overridden for testing.
112 std::unique_ptr<base::DefaultTickClock> default_tick_clock_; 127 std::unique_ptr<base::DefaultTickClock> default_tick_clock_;
113 base::TickClock* tick_clock_; 128 base::TickClock* tick_clock_;
114 129
115 // If a video is playing in the background. Set when user resumes a video 130 #if defined(OS_ANDROID)
116 // allowing it to play and reset when either user pauses it or it goes 131 bool was_playing_background_video_ = false;
117 // foreground.
118 bool is_playing_background_video_ = false;
119 132
120 #if defined(OS_ANDROID)
121 // Keeps track of when the background video playback started for metrics. 133 // Keeps track of when the background video playback started for metrics.
122 base::TimeTicks background_video_playing_start_time_; 134 base::TimeTicks background_video_start_time_;
123 #endif // OS_ANDROID 135 #endif // OS_ANDROID
124 136
125 // The currently playing local videos. Used to determine whether 137 // The currently playing local videos. Used to determine whether
126 // OnMediaDelegatePlay() should allow the videos to play in the background or 138 // OnMediaDelegatePlay() should allow the videos to play in the background or
127 // not. 139 // not.
128 std::set<int> playing_videos_; 140 std::set<int> playing_videos_;
129 141
130 // Determined at construction time based on system information; determines 142 // Determined at construction time based on system information; determines
131 // when the idle cleanup timer should be fired more aggressively. 143 // when the idle cleanup timer should be fired more aggressively.
132 bool is_low_end_device_; 144 bool is_low_end_device_;
133 145
134 DISALLOW_COPY_AND_ASSIGN(RendererWebMediaPlayerDelegate); 146 DISALLOW_COPY_AND_ASSIGN(RendererWebMediaPlayerDelegate);
135 }; 147 };
136 148
137 } // namespace media 149 } // namespace media
138 150
139 #endif // CONTENT_RENDERER_MEDIA_RENDERER_WEBMEDIAPLAYER_DELEGATE_H_ 151 #endif // CONTENT_RENDERER_MEDIA_RENDERER_WEBMEDIAPLAYER_DELEGATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698