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_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_ | 5 #ifndef CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_ |
6 #define CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_ | 6 #define CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_ |
7 | 7 |
8 #include <jni.h> | 8 #include <jni.h> |
9 | 9 |
10 #include "base/android/scoped_java_ref.h" | 10 #include "base/android/scoped_java_ref.h" |
11 #include "base/id_map.h" | 11 #include "base/id_map.h" |
12 #include "content/common/content_export.h" | 12 #include "content/common/content_export.h" |
13 #include "content/public/browser/web_contents_observer.h" | 13 #include "content/public/browser/web_contents_observer.h" |
14 #include "content/public/browser/web_contents_user_data.h" | 14 #include "content/public/browser/web_contents_user_data.h" |
15 | 15 |
| 16 class MediaSessionBrowserTest; |
| 17 |
16 namespace content { | 18 namespace content { |
17 | 19 |
18 class MediaSessionBrowserTest; | |
19 class MediaSessionObserver; | 20 class MediaSessionObserver; |
20 | 21 |
21 // MediaSession manages the Android AudioFocus for a given WebContents. It is | 22 // MediaSession manages the Android AudioFocus for a given WebContents. It is |
22 // requesting the audio focus, pausing when requested by the system and dropping | 23 // requesting the audio focus, pausing when requested by the system and dropping |
23 // it on demand. | 24 // it on demand. |
24 // The audio focus can be of two types: Transient or Content. A Transient audio | 25 // The audio focus can be of two types: Transient or Content. A Transient audio |
25 // focus will allow other players to duck instead of pausing and will be | 26 // focus will allow other players to duck instead of pausing and will be |
26 // declared as temporary to the system. A Content audio focus will not be | 27 // declared as temporary to the system. A Content audio focus will not be |
27 // declared as temporary and will not allow other players to duck. If a given | 28 // declared as temporary and will not allow other players to duck. If a given |
28 // WebContents can only have one audio focus at a time, it will be Content in | 29 // WebContents can only have one audio focus at a time, it will be Content in |
29 // case of Transient and Content audio focus are both requested. | 30 // case of Transient and Content audio focus are both requested. |
30 // Android system interaction occurs in the Java counterpart to this class. | 31 // Android system interaction occurs in the Java counterpart to this class. |
31 class CONTENT_EXPORT MediaSession | 32 class CONTENT_EXPORT MediaSession |
32 : public content::WebContentsObserver, | 33 : public WebContentsObserver, |
33 protected content::WebContentsUserData<MediaSession> { | 34 protected WebContentsUserData<MediaSession> { |
34 public: | 35 public: |
35 enum class Type { | 36 enum class Type { |
36 Content, | 37 Content, |
37 Transient | 38 Transient |
38 }; | 39 }; |
39 | 40 |
40 static bool RegisterMediaSession(JNIEnv* env); | 41 static bool RegisterMediaSession(JNIEnv* env); |
41 | 42 |
42 // Returns the MediaSession associated to this WebContents. Creates one if | 43 // Returns the MediaSession associated to this WebContents. Creates one if |
43 // none is currently available. | 44 // none is currently available. |
(...skipping 15 matching lines...) Expand all Loading... |
59 void RemovePlayers(MediaSessionObserver* observer); | 60 void RemovePlayers(MediaSessionObserver* observer); |
60 | 61 |
61 // Called when the Android system requests the MediaSession to be suspended. | 62 // Called when the Android system requests the MediaSession to be suspended. |
62 // Called by Java through JNI. | 63 // Called by Java through JNI. |
63 void OnSuspend(JNIEnv* env, jobject obj, jboolean temporary); | 64 void OnSuspend(JNIEnv* env, jobject obj, jboolean temporary); |
64 | 65 |
65 // Called when the Android system requests the MediaSession to be resumed. | 66 // Called when the Android system requests the MediaSession to be resumed. |
66 // Called by Java through JNI. | 67 // Called by Java through JNI. |
67 void OnResume(JNIEnv* env, jobject obj); | 68 void OnResume(JNIEnv* env, jobject obj); |
68 | 69 |
69 protected: | 70 // Called when the user requests resuming the session. No-op if the session is |
70 friend class content::MediaSessionBrowserTest; | 71 // not controllable. |
| 72 void Resume(); |
| 73 |
| 74 // Called when the user requests suspending the session. No-op if the session |
| 75 // is not controllable. |
| 76 void Suspend(); |
| 77 |
| 78 // Returns if the session can be controlled by Resume() and Suspend calls |
| 79 // above. |
| 80 bool IsControllable() const; |
| 81 |
| 82 // Returns if the session is currently suspended. |
| 83 bool IsSuspended() const; |
| 84 |
| 85 private: |
| 86 friend class content::WebContentsUserData<MediaSession>; |
| 87 friend class ::MediaSessionBrowserTest; |
71 | 88 |
72 // Resets the |j_media_session_| ref to prevent calling the Java backend | 89 // Resets the |j_media_session_| ref to prevent calling the Java backend |
73 // during content_browsertests. | 90 // during content_browsertests. |
74 void ResetJavaRefForTest(); | 91 void ResetJavaRefForTest(); |
75 | |
76 bool IsActiveForTest() const; | 92 bool IsActiveForTest() const; |
77 Type audio_focus_type_for_test() const; | 93 Type audio_focus_type_for_test() const; |
| 94 void RemoveAllPlayersForTest(); |
78 | 95 |
79 void OnSuspend(bool temporary); | 96 void OnSuspendInternal(bool temporary); |
80 void OnResume(); | 97 void OnResumeInternal(); |
81 | |
82 private: | |
83 friend class content::WebContentsUserData<MediaSession>; | |
84 | 98 |
85 enum class State { | 99 enum class State { |
86 Active, | 100 Active, |
87 TemporarilySuspended, | 101 TemporarilySuspended, |
88 Suspended, | 102 Suspended, |
89 }; | 103 }; |
90 | 104 |
91 // Representation of a player for the MediaSession. | 105 // Representation of a player for the MediaSession. |
92 struct PlayerIdentifier { | 106 struct PlayerIdentifier { |
93 PlayerIdentifier(MediaSessionObserver* observer, int player_id); | 107 PlayerIdentifier(MediaSessionObserver* observer, int player_id); |
(...skipping 19 matching lines...) Expand all Loading... |
113 | 127 |
114 // Requests audio focus to Android using |j_media_session_|. | 128 // Requests audio focus to Android using |j_media_session_|. |
115 // Returns whether the request was granted. If |j_media_session_| is null, it | 129 // Returns whether the request was granted. If |j_media_session_| is null, it |
116 // will always return true. | 130 // will always return true. |
117 bool RequestSystemAudioFocus(Type type); | 131 bool RequestSystemAudioFocus(Type type); |
118 | 132 |
119 // To be called after a call to AbandonAudioFocus() in order to call the Java | 133 // To be called after a call to AbandonAudioFocus() in order to call the Java |
120 // MediaSession if the audio focus really need to be abandoned. | 134 // MediaSession if the audio focus really need to be abandoned. |
121 void AbandonSystemAudioFocusIfNeeded(); | 135 void AbandonSystemAudioFocusIfNeeded(); |
122 | 136 |
| 137 // Notifies WebContents about the state change of the media session. |
| 138 void UpdateWebContents(); |
| 139 |
123 base::android::ScopedJavaGlobalRef<jobject> j_media_session_; | 140 base::android::ScopedJavaGlobalRef<jobject> j_media_session_; |
124 PlayersMap players_; | 141 PlayersMap players_; |
125 | 142 |
126 State audio_focus_state_; | 143 State audio_focus_state_; |
127 Type audio_focus_type_; | 144 Type audio_focus_type_; |
128 | 145 |
129 DISALLOW_COPY_AND_ASSIGN(MediaSession); | 146 DISALLOW_COPY_AND_ASSIGN(MediaSession); |
130 }; | 147 }; |
131 | 148 |
132 } // namespace content | 149 } // namespace content |
133 | 150 |
134 #endif // CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_ | 151 #endif // CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_ |
OLD | NEW |