Chromium Code Reviews

Side by Side Diff: content/browser/media/android/media_session.h

Issue 1110833004: Move audio focus control from media/ to content/ and make it per WebContents. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review comments Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
(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/public/browser/web_contents_observer.h"
13 #include "content/public/browser/web_contents_user_data.h"
14
15 namespace content {
16
17 class MediaSessionDelegate;
18
19 // MediaSession manages the Android AudioFocus for a given WebContents. It is
20 // requesting the audio focus, pausing when requested by the system and dropping
21 // it on demand.
22 // The audio focus can be of two types: Transient or Content. A Transient audio
23 // focus will allow other players to duck instead of pausing and will be
24 // declared as temporary to the system. A Content audio focus will not be
25 // declared as temporary and will not allow other players to duck. If a given
26 // WebContents can only have one audio focus at a time, it will be Content in
27 // case of Transient and Content audio focus are both requested.
28 // It is communicating to the Android system trough MediaSession.java via JNI.
29 class MediaSession : public content::WebContentsObserver,
30 protected content::WebContentsUserData<MediaSession> {
31 public:
32 enum class Type {
33 Content,
34 Transient
35 };
36
37 static bool RegisterMediaSession(JNIEnv* env);
38
39 // Returns the MediaSession associated to this WebContents. Create one if none
40 // is currently available.
41 static MediaSession* Get(WebContents* web_contents);
42
43 ~MediaSession() override;
44
45 // Adds the given player to the current media session. Returns whether the
46 // player was successfully added. If it returns false, AddPlayer() should be
47 // called again later.
48 bool AddPlayer(MediaSessionDelegate* delegate, int player_id, Type type);
49
50 // Removes the given player from the current media session. Abandon audio
51 // focus if that was the last player in the session.
52 void RemovePlayer(MediaSessionDelegate* delegate, int player_id);
53
54 // Removes all the players associated with |delegate|. Abandon audio focus if
55 // these were the last players in the session.
56 void RemovePlayers(MediaSessionDelegate* delegate);
57
58 // Called by Java trough JNI.
59 void OnSuspend(JNIEnv* env, jobject obj);
60 void OnResume(JNIEnv* env, jobject obj);
61
62 private:
63 friend class content::WebContentsUserData<MediaSession>;
64
65 // Representation of a player for the MediaSession.
66 struct PlayerIdentifier {
67 PlayerIdentifier(MediaSessionDelegate* delegate, int player_id);
68 PlayerIdentifier(const PlayerIdentifier&) = default;
69
70 void operator=(const PlayerIdentifier&) = delete;
71 bool operator==(const PlayerIdentifier& player_identifier) const;
72
73 // Hash operator for base::hash_map<>.
74 struct Hash {
75 size_t operator()(const PlayerIdentifier& player_identifier) const;
76 };
77
78 MediaSessionDelegate* delegate;
79 int player_id;
80 };
81 using PlayersMap = base::hash_set<PlayerIdentifier, PlayerIdentifier::Hash>;
82
83 explicit MediaSession(WebContents* web_contents);
84
85 // Setup the JNI.
86 void Initialize();
87
88 // To be called after a call to AbandonAudioFocus() in order to call the Java
89 // MediaSession if the audio focus really need to be abandoned.
90 void AbandonSystemAudioFocusIfNeeded();
91
92 base::android::ScopedJavaGlobalRef<jobject> j_media_session_;
93 PlayersMap players_;
94
95 bool has_audio_focus_;
96 Type audio_focus_type_;
97
98 DISALLOW_COPY_AND_ASSIGN(MediaSession);
99 };
100
101 } // namespace content
102
103 #endif // CONTENT_BROWSER_MEDIA_ANDROID_MEDIA_SESSION_H_
OLDNEW

Powered by Google App Engine