OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 MEDIA_BLINK_WEBMEDIAPLAYER_DELEGATE_H_ | 5 #ifndef MEDIA_BLINK_WEBMEDIAPLAYER_DELEGATE_H_ |
6 #define MEDIA_BLINK_WEBMEDIAPLAYER_DELEGATE_H_ | 6 #define MEDIA_BLINK_WEBMEDIAPLAYER_DELEGATE_H_ |
7 | 7 |
8 namespace blink { | 8 namespace blink { |
9 class WebMediaPlayer; | 9 class WebMediaPlayer; |
10 } | 10 } |
11 namespace media { | 11 namespace media { |
12 | 12 |
13 enum class MediaContentType; | 13 enum class MediaContentType; |
14 | 14 |
15 // An interface to allow a WebMediaPlayer to communicate changes of state to | 15 // An interface to collect WebMediaPlayer state changes and to fan out commands |
16 // objects that need to know. | 16 // from the browser. |
17 class WebMediaPlayerDelegate { | 17 class WebMediaPlayerDelegate { |
18 public: | 18 public: |
| 19 // Note: WebMediaPlayerDelegate implementations should not call an Observer |
| 20 // method on a stack that includes a call from the player. |
| 21 // Note: It is likely that players will call WebMediaPlayerDelegate methods |
| 22 // from within Observer callbacks. |
19 class Observer { | 23 class Observer { |
20 public: | 24 public: |
21 // Called when the WebMediaPlayer enters the background or foreground | 25 // Called when the host frame is hidden (usually by tab switching). |
22 // respectively. Note: Some implementations will stop playback when hidden, | 26 // Note: OnFrameHidden() is not called when the frame is closed, even though |
23 // and thus subsequently call WebMediaPlayerDelegate::PlayerGone(). | 27 // IsFrameHidden() will start returning true. |
24 virtual void OnHidden() = 0; | 28 virtual void OnFrameHidden() = 0; |
25 virtual void OnShown() = 0; | |
26 | 29 |
27 // Requests a WebMediaPlayer instance to release all idle resources. If | 30 // Called when the host frame is closed. |
28 // |must_suspend| is true, the player must stop playback, release all idle | 31 // Note: It is possible for a closed frame to be shown again. (Android only; |
29 // resources, and finally call WebMediaPlayerDelegate::PlayerGone(). If | 32 // other platforms tear down players when the host frame is closed.) There |
30 // |must_suspend| is false, the player may ignore the request. Optionally, | 33 // is no callback for frame opening, observers are expected to wait until |
31 // it may do some or all of the same actions as when |must_suspend| is true. | 34 // OnFrameShown(). |
32 // To be clear, the player is not required to call PlayerGone() when | 35 // TODO(sandersd): Experiment to verify exactly what gets called when |
33 // |must_suspend| is false. | 36 // restoring a closed tab on Android. |
34 // Return false to reject the request and indicate that further calls to | 37 virtual void OnFrameClosed() = 0; |
35 // OnSuspendRequested() are required. Otherwise the Observer is removed | |
36 // from the idle list. | |
37 virtual bool OnSuspendRequested(bool must_suspend) = 0; | |
38 | 38 |
| 39 // Called when the host frame is shown (usually by tab switching). |
| 40 virtual void OnFrameShown() = 0; |
| 41 |
| 42 // Called when an idle player has become stale, usually interpreted to mean |
| 43 // that it is unlikely to be interacted with in the near future. |
| 44 // |
| 45 // Players should typically respond by releasing resources, for example by |
| 46 // discarding their decoders. |
| 47 virtual void OnIdleTimeout() = 0; |
| 48 |
| 49 // Called when external controls are activated. |
39 virtual void OnPlay() = 0; | 50 virtual void OnPlay() = 0; |
40 virtual void OnPause() = 0; | 51 virtual void OnPause() = 0; |
41 | 52 |
42 // Playout volume should be set to current_volume * multiplier. The range is | 53 // Called to control audio ducking. Output volume should be set to |
43 // [0, 1] and is typically 1. | 54 // |player_volume| * |multiplier|. The range of |multiplier| is [0, 1], |
| 55 // where 1 indicates normal (non-ducked) playback. |
44 virtual void OnVolumeMultiplierUpdate(double multiplier) = 0; | 56 virtual void OnVolumeMultiplierUpdate(double multiplier) = 0; |
45 }; | 57 }; |
46 | 58 |
47 WebMediaPlayerDelegate() {} | 59 // Returns true if the host frame is hidden or closed. |
| 60 virtual bool IsFrameHidden() = 0; |
48 | 61 |
49 // Subscribe or unsubscribe from observer callbacks respectively. A client | 62 // Returns true if the host frame is closed. |
50 // must use the delegate id returned by AddObserver() for all other calls. | 63 virtual bool IsFrameClosed() = 0; |
| 64 |
| 65 // Returns |true| if background video playback permission has been granted, |
| 66 // for example by a media session 'play' command. |
| 67 virtual bool IsBackgroundVideoPlaybackUnlocked() = 0; |
| 68 |
| 69 // Subscribe to observer callbacks. A player must use the returned |player_id| |
| 70 // for the rest of the calls below. |
51 virtual int AddObserver(Observer* observer) = 0; | 71 virtual int AddObserver(Observer* observer) = 0; |
52 virtual void RemoveObserver(int delegate_id) = 0; | |
53 | 72 |
54 // The specified player started playing media. | 73 // Unsubscribe from observer callbacks. |
55 virtual void DidPlay(int delegate_id, | 74 virtual void RemoveObserver(int player_id) = 0; |
| 75 |
| 76 // Notify playback started. This will request appropriate wake locks and, if |
| 77 // applicable, show a pause button in external controls. |
| 78 // |
| 79 // DidPlay() should not be called for remote playback. |
| 80 virtual void DidPlay(int player_id, |
56 bool has_video, | 81 bool has_video, |
57 bool has_audio, | 82 bool has_audio, |
58 bool is_remote, | |
59 media::MediaContentType media_content_type) = 0; | 83 media::MediaContentType media_content_type) = 0; |
60 | 84 |
61 // The specified player stopped playing media. This may be called at any time | 85 // Notify that playback is paused. This will drop wake locks and, if |
62 // with or without a DidPlay() having previously occurred. Calling this will | 86 // applicable, show a play button in external controls. |
63 // cause the delegate to be registered for idle suspension. I.e., after some | 87 // TODO(sandersd): It may be helpful to get |has_audio| and |has_video| here, |
64 // time elapses without a DidPlay(), OnSuspendRequested() will be issued. | 88 // so that we can do the right thing with media that starts paused. |
65 virtual void DidPause(int delegate_id, bool reached_end_of_stream) = 0; | 89 virtual void DidPause(int player_id) = 0; |
66 | 90 |
67 // The specified player was destroyed or suspended and will no longer accept | 91 // Notify that playback is stopped. This will drop wake locks and remove any |
68 // Observer::OnPlay() or Observer::OnPause() calls. This may be called | 92 // external controls. |
69 // multiple times in row. Note: Clients must still call RemoveObserver() to | 93 // |
70 // unsubscribe from callbacks. | 94 // Clients must still call RemoveObserver() to unsubscribe from observer |
71 virtual void PlayerGone(int delegate_id) = 0; | 95 // callbacks. |
| 96 virtual void PlayerGone(int player_id) = 0; |
72 | 97 |
73 // Returns whether the render frame is currently hidden. | 98 // Set the player's idle state. While idle, a player may recieve an |
74 virtual bool IsHidden() = 0; | 99 // OnIdleTimeout() callback. |
| 100 // TODO(sandersd): Merge this into DidPlay()/DidPause()/PlayerGone(). |
| 101 virtual void SetIdle(int player_id, bool is_idle) = 0; |
75 | 102 |
76 // Returns whether there's a video playing in background within the render | 103 // Get the player's idle state. A stale player is considered idle. |
77 // frame. | 104 // TODO(sandersd): Remove this. It is only used in tests and in one special |
78 virtual bool IsPlayingBackgroundVideo() = 0; | 105 // case in WMPI. |
| 106 virtual bool IsIdle(int player_id) = 0; |
| 107 |
| 108 // Returns a stale player to an idle state, and resumes OnIdleTimeout() calls |
| 109 // without an additional idle timeout. |
| 110 // TODO(sandersd): This exists only to support WMPI's didLoadingProgress() |
| 111 // workaround. A better option may be to take a 'minimum idle' duration in |
| 112 // SetIdle(). |
| 113 virtual void ClearStaleFlag(int player_id) = 0; |
| 114 |
| 115 // Returns |true| if the player is stale; that is that OnIdleTimeout() was |
| 116 // called and returned |true|. |
| 117 virtual bool IsStale(int player_id) = 0; |
79 | 118 |
80 protected: | 119 protected: |
81 virtual ~WebMediaPlayerDelegate() {} | 120 WebMediaPlayerDelegate() = default; |
| 121 virtual ~WebMediaPlayerDelegate() = default; |
82 }; | 122 }; |
83 | 123 |
84 } // namespace media | 124 } // namespace media |
85 | 125 |
86 #endif // MEDIA_BLINK_WEBMEDIAPLAYER_DELEGATE_H_ | 126 #endif // MEDIA_BLINK_WEBMEDIAPLAYER_DELEGATE_H_ |
OLD | NEW |