OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_ |
| 6 #define CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_ |
| 7 |
| 8 #include <jni.h> |
| 9 |
| 10 #include "base/android/scoped_java_ref.h" |
| 11 #include "base/id_map.h" |
| 12 #include "content/common/content_export.h" |
| 13 #include "content/public/browser/web_contents_observer.h" |
| 14 #include "content/public/browser/web_contents_user_data.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 class MediaSessionBrowserTest; |
| 19 class MediaSessionObserver; |
| 20 |
| 21 // 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 // it on demand. |
| 24 // 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 // 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 // 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 // Android system interaction occurs in the Java counterpart to this class. |
| 31 class CONTENT_EXPORT MediaSession |
| 32 : public content::WebContentsObserver, |
| 33 protected content::WebContentsUserData<MediaSession> { |
| 34 public: |
| 35 enum class Type { |
| 36 Content, |
| 37 Transient |
| 38 }; |
| 39 |
| 40 static bool RegisterMediaSession(JNIEnv* env); |
| 41 |
| 42 // Returns the MediaSession associated to this WebContents. Creates one if |
| 43 // none is currently available. |
| 44 static MediaSession* Get(WebContents* web_contents); |
| 45 |
| 46 ~MediaSession() override; |
| 47 |
| 48 // Adds the given player to the current media session. Returns whether the |
| 49 // player was successfully added. If it returns false, AddPlayer() should be |
| 50 // called again later. |
| 51 bool AddPlayer(MediaSessionObserver* observer, int player_id, Type type); |
| 52 |
| 53 // Removes the given player from the current media session. Abandons audio |
| 54 // focus if that was the last player in the session. |
| 55 void RemovePlayer(MediaSessionObserver* observer, int player_id); |
| 56 |
| 57 // Removes all the players associated with |observer|. Abandons audio focus if |
| 58 // these were the last players in the session. |
| 59 void RemovePlayers(MediaSessionObserver* observer); |
| 60 |
| 61 // Called when the Android system requests the MediaSession to be suspended. |
| 62 // Called by Java through JNI. |
| 63 void OnSuspend(JNIEnv* env, jobject obj, jboolean temporary); |
| 64 |
| 65 // Called when the Android system requests the MediaSession to be resumed. |
| 66 // Called by Java through JNI. |
| 67 void OnResume(JNIEnv* env, jobject obj); |
| 68 |
| 69 protected: |
| 70 friend class content::MediaSessionBrowserTest; |
| 71 |
| 72 // Resets the |j_media_session_| ref to prevent calling the Java backend |
| 73 // during content_browsertests. |
| 74 void ResetJavaRefForTest(); |
| 75 |
| 76 bool IsActiveForTest() const; |
| 77 Type audio_focus_type_for_test() const; |
| 78 |
| 79 void OnSuspend(bool temporary); |
| 80 void OnResume(); |
| 81 |
| 82 private: |
| 83 friend class content::WebContentsUserData<MediaSession>; |
| 84 |
| 85 enum class State { |
| 86 Active, |
| 87 TemporarilySuspended, |
| 88 Suspended, |
| 89 }; |
| 90 |
| 91 // Representation of a player for the MediaSession. |
| 92 struct PlayerIdentifier { |
| 93 PlayerIdentifier(MediaSessionObserver* observer, int player_id); |
| 94 PlayerIdentifier(const PlayerIdentifier&) = default; |
| 95 |
| 96 void operator=(const PlayerIdentifier&) = delete; |
| 97 bool operator==(const PlayerIdentifier& player_identifier) const; |
| 98 |
| 99 // Hash operator for base::hash_map<>. |
| 100 struct Hash { |
| 101 size_t operator()(const PlayerIdentifier& player_identifier) const; |
| 102 }; |
| 103 |
| 104 MediaSessionObserver* observer; |
| 105 int player_id; |
| 106 }; |
| 107 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>; |
| 108 |
| 109 explicit MediaSession(WebContents* web_contents); |
| 110 |
| 111 // Setup the JNI. |
| 112 void Initialize(); |
| 113 |
| 114 // Requests audio focus to Android using |j_media_session_|. |
| 115 // Returns whether the request was granted. If |j_media_session_| is null, it |
| 116 // will always return true. |
| 117 bool RequestSystemAudioFocus(Type type); |
| 118 |
| 119 // 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. |
| 121 void AbandonSystemAudioFocusIfNeeded(); |
| 122 |
| 123 base::android::ScopedJavaGlobalRef<jobject> j_media_session_; |
| 124 PlayersMap players_; |
| 125 |
| 126 State audio_focus_state_; |
| 127 Type audio_focus_type_; |
| 128 |
| 129 DISALLOW_COPY_AND_ASSIGN(MediaSession); |
| 130 }; |
| 131 |
| 132 } // namespace content |
| 133 |
| 134 #endif // CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_ |
OLD | NEW |