Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

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

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: with tests Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
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 #include "content/browser/media/android/media_session.h"
6
7 #include "base/android/jni_android.h"
8 #include "content/browser/media/android/media_session_observer.h"
9 #include "jni/MediaSession_jni.h"
10
11 namespace content {
12
13 DEFINE_WEB_CONTENTS_USER_DATA_KEY(MediaSession);
14
15 // static
16 bool content::MediaSession::RegisterMediaSession(JNIEnv* env) {
17 return RegisterNativesImpl(env);
18 }
19
20 // static
21 MediaSession* MediaSession::Get(WebContents* web_contents) {
22 MediaSession* session = FromWebContents(web_contents);
23 if (!session) {
24 CreateForWebContents(web_contents);
25 session = FromWebContents(web_contents);
26 session->Initialize();
27 }
28 return session;
29 }
30
31 MediaSession::PlayerIdentifier::PlayerIdentifier(MediaSessionObserver* observer,
32 int player_id)
33 : observer(observer),
34 player_id(player_id) {
35 }
36
37 bool MediaSession::PlayerIdentifier::operator==(
38 const PlayerIdentifier& other) const {
39 return this->observer == other.observer && this->player_id == other.player_id;
40 }
41
42 size_t MediaSession::PlayerIdentifier::Hash::operator()(
43 const PlayerIdentifier& player_identifier) const {
44 size_t hash = BASE_HASH_NAMESPACE::hash<MediaSessionObserver*>()(
45 player_identifier.observer);
46 hash += BASE_HASH_NAMESPACE::hash<int>()(player_identifier.player_id);
47 return hash;
48 }
49
50 MediaSession::~MediaSession() {
51 DCHECK(players_.empty());
52 DCHECK(!has_audio_focus_);
53 }
54
55 void MediaSession::Initialize() {
56 JNIEnv* env = base::android::AttachCurrentThread();
57 DCHECK(env);
58 j_media_session_.Reset(Java_MediaSession_createMediaSession(
59 env,
60 base::android::GetApplicationContext(),
61 reinterpret_cast<intptr_t>(this)));
62 }
63
64 MediaSession::MediaSession(WebContents* web_contents)
65 : WebContentsObserver(web_contents),
66 has_audio_focus_(false),
67 audio_focus_type_(Type::Transient) {
68 }
69
70 bool MediaSession::AddPlayer(MediaSessionObserver* observer,
71 int player_id,
72 Type type) {
73 // If the audio focus is already granted and is of type Content, there is
74 // nothing to do. If it is granted of type Transient the requested type is
75 // also transient, there is also nothing to do. Otherwise, the session needs
76 // to request audio focus again.
77 if (has_audio_focus_ &&
78 (audio_focus_type_ == Type::Content || audio_focus_type_ == type)) {
79 players_.insert(PlayerIdentifier(observer, player_id));
80 return true;
81 }
82
83 // The session should be reset if a player is starting while all players are
qinmin 2015/05/22 16:43:23 I am not sure whether the logic is appropriate. Sa
mlamouri (slow - plz ping) 2015/05/22 19:01:58 You're right, we should make sure the audio focus
84 // suspended.
85 if (!has_audio_focus_)
86 players_.clear();
87
88 has_audio_focus_ = RequestSystemAudioFocus(type);
89 audio_focus_type_ = type;
90
91 if (has_audio_focus_)
92 players_.insert(PlayerIdentifier(observer, player_id));
93
94 return has_audio_focus_;
95 }
96
97 void MediaSession::RemovePlayer(MediaSessionObserver* observer,
98 int player_id) {
99 auto it = players_.find(PlayerIdentifier(observer, player_id));
100 if (it != players_.end())
101 players_.erase(it);
102
103 AbandonSystemAudioFocusIfNeeded();
104 }
105
106 void MediaSession::RemovePlayers(MediaSessionObserver* observer) {
107 for (auto it = players_.begin(); it != players_.end(); ) {
108 if (it->observer == observer)
109 players_.erase(it++);
110 else
111 ++it;
112 }
113
114 AbandonSystemAudioFocusIfNeeded();
115 }
116
117 bool MediaSession::RequestSystemAudioFocus(Type type) {
118 if (j_media_session_.is_null())
119 return true;
120
121 JNIEnv* env = base::android::AttachCurrentThread();
122 DCHECK(env);
123 return Java_MediaSession_requestAudioFocus(env, j_media_session_.obj(),
124 type == Type::Transient);
125 }
126
127 void MediaSession::AbandonSystemAudioFocusIfNeeded() {
128 if (!has_audio_focus_ || !players_.empty())
129 return;
130
131 if (!j_media_session_.is_null()) {
132 JNIEnv* env = base::android::AttachCurrentThread();
133 DCHECK(env);
134 Java_MediaSession_abandonAudioFocus(env, j_media_session_.obj());
135 }
136
137 has_audio_focus_ = false;
138 }
139
140 void MediaSession::OnSuspend(JNIEnv* env, jobject obj) {
141 OnSuspend();
142 }
143
144 void MediaSession::OnSuspend() {
145 has_audio_focus_ = false;
146
147 for (const auto& it : players_)
148 it.observer->OnSuspend(it.player_id);
149 }
150
151
152 void MediaSession::OnResume(JNIEnv* env, jobject obj) {
153 OnResume();
154 }
155
156 void MediaSession::OnResume() {
157 has_audio_focus_ = true;
158
159 for (const auto& it : players_)
160 it.observer->OnResume(it.player_id);
161 }
162
163 void MediaSession::ResetJavaRefForTest() {
164 j_media_session_.Reset();
165 }
166
167 MediaSession::Type MediaSession::audio_focus_type_for_test() const {
168 return audio_focus_type_;
169 }
170
171 bool MediaSession::has_audio_focus_for_test() const {
172 return has_audio_focus_;
173 }
174
175 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/media/android/media_session.h ('k') | content/browser/media/android/media_session_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698