Chromium Code Reviews| 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 // It is communicating to the Android system trough MediaSession.java via JNI. | |
| 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. Create one if none | |
|
qinmin
2015/05/23 18:16:06
nit:s/Create/Creates/
mlamouri (slow - plz ping)
2015/05/23 20:05:27
Done.
| |
| 43 // 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. Abandon audio | |
|
qinmin
2015/05/23 18:16:06
s/Abandon/Abandons/
mlamouri (slow - plz ping)
2015/05/23 20:05:27
Done.
| |
| 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|. Abandon audio focus if | |
|
qinmin
2015/05/23 18:16:06
ditto
mlamouri (slow - plz ping)
2015/05/23 20:05:27
Done.
| |
| 58 // these were the last players in the session. | |
| 59 void RemovePlayers(MediaSessionObserver* observer); | |
| 60 | |
| 61 // Called by Java trough JNI. | |
| 62 void OnSuspend(JNIEnv* env, jobject obj); | |
| 63 void OnResume(JNIEnv* env, jobject obj); | |
| 64 | |
| 65 protected: | |
| 66 friend class content::MediaSessionBrowserTest; | |
| 67 | |
| 68 // Resets the |j_media_session_| ref to prevent calling the Java backend | |
| 69 // during content_browsertests. | |
| 70 void ResetJavaRefForTest(); | |
| 71 | |
| 72 bool has_audio_focus_for_test() const; | |
| 73 Type audio_focus_type_for_test() const; | |
| 74 | |
| 75 void OnSuspend(); | |
| 76 void OnResume(); | |
| 77 | |
| 78 private: | |
| 79 friend class content::WebContentsUserData<MediaSession>; | |
| 80 | |
| 81 // Representation of a player for the MediaSession. | |
| 82 struct PlayerIdentifier { | |
| 83 PlayerIdentifier(MediaSessionObserver* observer, int player_id); | |
| 84 PlayerIdentifier(const PlayerIdentifier&) = default; | |
| 85 | |
| 86 void operator=(const PlayerIdentifier&) = delete; | |
| 87 bool operator==(const PlayerIdentifier& player_identifier) const; | |
| 88 | |
| 89 // Hash operator for base::hash_map<>. | |
| 90 struct Hash { | |
| 91 size_t operator()(const PlayerIdentifier& player_identifier) const; | |
| 92 }; | |
| 93 | |
| 94 MediaSessionObserver* observer; | |
| 95 int player_id; | |
| 96 }; | |
| 97 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>; | |
| 98 | |
| 99 explicit MediaSession(WebContents* web_contents); | |
| 100 | |
| 101 // Setup the JNI. | |
| 102 void Initialize(); | |
| 103 | |
| 104 // Requests audio focus to Android using |j_media_session_|. | |
| 105 // Returns whether the request was granted. If |j_media_session_| is null, it | |
| 106 // will always return true. | |
| 107 bool RequestSystemAudioFocus(Type type); | |
| 108 | |
| 109 // To be called after a call to AbandonAudioFocus() in order to call the Java | |
| 110 // MediaSession if the audio focus really need to be abandoned. | |
| 111 void AbandonSystemAudioFocusIfNeeded(); | |
| 112 | |
| 113 base::android::ScopedJavaGlobalRef<jobject> j_media_session_; | |
| 114 PlayersMap players_; | |
| 115 | |
| 116 bool has_audio_focus_; | |
| 117 Type audio_focus_type_; | |
| 118 | |
| 119 DISALLOW_COPY_AND_ASSIGN(MediaSession); | |
| 120 }; | |
| 121 | |
| 122 } // namespace content | |
| 123 | |
| 124 #endif // CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_ | |
| OLD | NEW |