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 #include "content/browser/media/android/media_session.h" | 5 #include "content/browser/media/android/media_session.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "content/browser/media/android/media_session_observer.h" | 8 #include "content/browser/media/android/media_session_observer.h" |
9 #include "content/public/browser/web_contents.h" | |
10 #include "content/public/browser/web_contents_delegate.h" | |
9 #include "jni/MediaSession_jni.h" | 11 #include "jni/MediaSession_jni.h" |
10 | 12 |
11 namespace content { | 13 namespace content { |
12 | 14 |
13 DEFINE_WEB_CONTENTS_USER_DATA_KEY(MediaSession); | 15 DEFINE_WEB_CONTENTS_USER_DATA_KEY(MediaSession); |
14 | 16 |
15 MediaSession::PlayerIdentifier::PlayerIdentifier(MediaSessionObserver* observer, | 17 MediaSession::PlayerIdentifier::PlayerIdentifier(MediaSessionObserver* observer, |
16 int player_id) | 18 int player_id) |
17 : observer(observer), | 19 : observer(observer), |
18 player_id(player_id) { | 20 player_id(player_id) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 | 74 |
73 if (audio_focus_state_ != State::Active) | 75 if (audio_focus_state_ != State::Active) |
74 return false; | 76 return false; |
75 | 77 |
76 // The session should be reset if a player is starting while all players are | 78 // The session should be reset if a player is starting while all players are |
77 // suspended. | 79 // suspended. |
78 if (old_audio_focus_state != State::Active) | 80 if (old_audio_focus_state != State::Active) |
79 players_.clear(); | 81 players_.clear(); |
80 | 82 |
81 players_.insert(PlayerIdentifier(observer, player_id)); | 83 players_.insert(PlayerIdentifier(observer, player_id)); |
84 UpdateWebContents(); | |
82 | 85 |
83 return true; | 86 return true; |
84 } | 87 } |
85 | 88 |
86 void MediaSession::RemovePlayer(MediaSessionObserver* observer, | 89 void MediaSession::RemovePlayer(MediaSessionObserver* observer, |
87 int player_id) { | 90 int player_id) { |
88 auto it = players_.find(PlayerIdentifier(observer, player_id)); | 91 auto it = players_.find(PlayerIdentifier(observer, player_id)); |
89 if (it != players_.end()) | 92 if (it != players_.end()) |
90 players_.erase(it); | 93 players_.erase(it); |
91 | 94 |
92 AbandonSystemAudioFocusIfNeeded(); | 95 AbandonSystemAudioFocusIfNeeded(); |
93 } | 96 } |
94 | 97 |
95 void MediaSession::RemovePlayers(MediaSessionObserver* observer) { | 98 void MediaSession::RemovePlayers(MediaSessionObserver* observer) { |
96 for (auto it = players_.begin(); it != players_.end();) { | 99 for (auto it = players_.begin(); it != players_.end();) { |
97 if (it->observer == observer) | 100 if (it->observer == observer) |
98 players_.erase(it++); | 101 players_.erase(it++); |
99 else | 102 else |
100 ++it; | 103 ++it; |
101 } | 104 } |
102 | 105 |
103 AbandonSystemAudioFocusIfNeeded(); | 106 AbandonSystemAudioFocusIfNeeded(); |
104 } | 107 } |
105 | 108 |
106 void MediaSession::OnSuspend(JNIEnv* env, jobject obj, jboolean temporary) { | 109 void MediaSession::OnSuspend(JNIEnv* env, jobject obj, jboolean temporary) { |
107 OnSuspend(temporary); | 110 OnSuspendInternal(temporary); |
111 UpdateWebContents(); | |
108 } | 112 } |
109 | 113 |
110 void MediaSession::OnResume(JNIEnv* env, jobject obj) { | 114 void MediaSession::OnResume(JNIEnv* env, jobject obj) { |
111 OnResume(); | 115 OnResumeInternal(); |
116 UpdateWebContents(); | |
117 } | |
118 | |
119 void MediaSession::Resume() { | |
120 DCHECK(IsSuspended()); | |
121 | |
122 OnResumeInternal(); | |
123 } | |
124 | |
125 void MediaSession::Suspend() { | |
126 DCHECK(!IsSuspended()); | |
127 | |
128 // Since the playback can be resumed, it's a transient suspension. | |
129 OnSuspendInternal(true); | |
130 } | |
131 | |
132 bool MediaSession::IsSuspended() const { | |
133 return audio_focus_state_ != State::Active; | |
134 } | |
135 | |
136 bool MediaSession::IsControllable() const { | |
137 return audio_focus_state_ != State::Suspended && | |
Ted C
2015/07/07 17:11:19
Why does Suspended determine whether it is control
whywhat
2015/07/07 19:19:04
So for the Content type, "Active" and "SuspendedTe
| |
138 audio_focus_type_ == Type::Content; | |
112 } | 139 } |
113 | 140 |
114 void MediaSession::ResetJavaRefForTest() { | 141 void MediaSession::ResetJavaRefForTest() { |
115 j_media_session_.Reset(); | 142 j_media_session_.Reset(); |
116 } | 143 } |
117 | 144 |
118 bool MediaSession::IsActiveForTest() const { | 145 bool MediaSession::IsActiveForTest() const { |
119 return audio_focus_state_ == State::Active; | 146 return audio_focus_state_ == State::Active; |
120 } | 147 } |
121 | 148 |
122 MediaSession::Type MediaSession::audio_focus_type_for_test() const { | 149 MediaSession::Type MediaSession::audio_focus_type_for_test() const { |
123 return audio_focus_type_; | 150 return audio_focus_type_; |
124 } | 151 } |
125 | 152 |
126 void MediaSession::OnSuspend(bool temporary) { | 153 void MediaSession::RemoveAllPlayersForTest() { |
154 players_.clear(); | |
155 AbandonSystemAudioFocusIfNeeded(); | |
156 } | |
157 | |
158 void MediaSession::OnSuspendInternal(bool temporary) { | |
127 if (temporary) | 159 if (temporary) |
128 audio_focus_state_ = State::TemporarilySuspended; | 160 audio_focus_state_ = State::TemporarilySuspended; |
129 else | 161 else |
130 audio_focus_state_ = State::Suspended; | 162 audio_focus_state_ = State::Suspended; |
131 | 163 |
132 for (const auto& it : players_) | 164 for (const auto& it : players_) |
133 it.observer->OnSuspend(it.player_id); | 165 it.observer->OnSuspend(it.player_id); |
134 } | 166 } |
135 | 167 |
136 void MediaSession::OnResume() { | 168 void MediaSession::OnResumeInternal() { |
137 audio_focus_state_ = State::Active; | 169 audio_focus_state_ = State::Active; |
138 | 170 |
139 for (const auto& it : players_) | 171 for (const auto& it : players_) |
140 it.observer->OnResume(it.player_id); | 172 it.observer->OnResume(it.player_id); |
141 } | 173 } |
142 | 174 |
143 MediaSession::MediaSession(WebContents* web_contents) | 175 MediaSession::MediaSession(WebContents* web_contents) |
144 : WebContentsObserver(web_contents), | 176 : WebContentsObserver(web_contents), |
145 audio_focus_state_(State::Suspended), | 177 audio_focus_state_(State::Suspended), |
146 audio_focus_type_(Type::Transient) { | 178 audio_focus_type_(Type::Transient) {} |
147 } | |
148 | 179 |
149 void MediaSession::Initialize() { | 180 void MediaSession::Initialize() { |
150 JNIEnv* env = base::android::AttachCurrentThread(); | 181 JNIEnv* env = base::android::AttachCurrentThread(); |
151 DCHECK(env); | 182 DCHECK(env); |
152 j_media_session_.Reset(Java_MediaSession_createMediaSession( | 183 j_media_session_.Reset(Java_MediaSession_createMediaSession( |
153 env, | 184 env, |
154 base::android::GetApplicationContext(), | 185 base::android::GetApplicationContext(), |
155 reinterpret_cast<intptr_t>(this))); | 186 reinterpret_cast<intptr_t>(this))); |
156 } | 187 } |
157 | 188 |
(...skipping 13 matching lines...) Expand all Loading... | |
171 return; | 202 return; |
172 | 203 |
173 // During tests, j_media_session_ might be null. | 204 // During tests, j_media_session_ might be null. |
174 if (!j_media_session_.is_null()) { | 205 if (!j_media_session_.is_null()) { |
175 JNIEnv* env = base::android::AttachCurrentThread(); | 206 JNIEnv* env = base::android::AttachCurrentThread(); |
176 DCHECK(env); | 207 DCHECK(env); |
177 Java_MediaSession_abandonAudioFocus(env, j_media_session_.obj()); | 208 Java_MediaSession_abandonAudioFocus(env, j_media_session_.obj()); |
178 } | 209 } |
179 | 210 |
180 audio_focus_state_ = State::Suspended; | 211 audio_focus_state_ = State::Suspended; |
212 UpdateWebContents(); | |
213 } | |
214 | |
215 void MediaSession::UpdateWebContents() { | |
216 web_contents()->OnMediaSessionStateChanged(); | |
181 } | 217 } |
182 | 218 |
183 } // namespace content | 219 } // namespace content |
OLD | NEW |