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

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

Issue 1308983005: NOT FOR LANDING Implement WebMediaSession (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add forward declaration Created 4 years, 11 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
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/context_utils.h" 7 #include "base/android/context_utils.h"
8 #include "base/android/jni_android.h" 8 #include "base/android/jni_android.h"
9 #include "content/browser/media/android/media_session_observer.h" 9 #include "content/browser/media/android/media_session_observer.h"
10 #include "content/browser/web_contents/web_contents_impl.h" 10 #include "content/browser/web_contents/web_contents_impl.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 MediaSession* MediaSession::Get(WebContents* web_contents) { 47 MediaSession* MediaSession::Get(WebContents* web_contents) {
48 MediaSession* session = FromWebContents(web_contents); 48 MediaSession* session = FromWebContents(web_contents);
49 if (!session) { 49 if (!session) {
50 CreateForWebContents(web_contents); 50 CreateForWebContents(web_contents);
51 session = FromWebContents(web_contents); 51 session = FromWebContents(web_contents);
52 session->Initialize(); 52 session->Initialize();
53 } 53 }
54 return session; 54 return session;
55 } 55 }
56 56
57 // static
58 MediaSession* MediaSession::Create(WebContents* web_contents, int session_id) {
59 MediaSession* session = new MediaSession(web_contents, session_id);
60 session->Initialize();
61 return session;
62 }
63
57 MediaSession::~MediaSession() { 64 MediaSession::~MediaSession() {
58 DCHECK(players_.empty()); 65 DCHECK(players_.empty());
59 DCHECK(audio_focus_state_ == State::INACTIVE); 66 DCHECK(audio_focus_state_ == State::INACTIVE);
60 } 67 }
61 68
69 bool MediaSession::Activate() {
70 if (audio_focus_state_ == State::ACTIVE)
71 return true;
72
73 const Type type = Type::Content;
74 if (!RequestSystemAudioFocus(type))
75 return false;
76
77 audio_focus_state_ = State::ACTIVE;
78 audio_focus_type_ = type;
79 UpdateWebContents();
80
81 return true;
82 }
83
84 void MediaSession::Deactivate() {
85 AbandonSystemAudioFocusIfNeeded();
86 DCHECK(audio_focus_state_ == State::INACTIVE);
87 }
88
62 bool MediaSession::AddPlayer(MediaSessionObserver* observer, 89 bool MediaSession::AddPlayer(MediaSessionObserver* observer,
63 int player_id, 90 int player_id,
64 Type type) { 91 Type type) {
65 // If the audio focus is already granted and is of type Content, there is 92 // If the audio focus is already granted and is of type Content, there is
66 // nothing to do. If it is granted of type Transient the requested type is 93 // nothing to do. If it is granted of type Transient the requested type is
67 // also transient, there is also nothing to do. Otherwise, the session needs 94 // also transient, there is also nothing to do. Otherwise, the session needs
68 // to request audio focus again. 95 // to request audio focus again.
69 if (audio_focus_state_ == State::ACTIVE && 96 if (audio_focus_state_ == State::ACTIVE &&
70 (audio_focus_type_ == Type::Content || audio_focus_type_ == type)) { 97 (audio_focus_type_ == Type::Content || audio_focus_type_ == type)) {
71 players_.insert(PlayerIdentifier(observer, player_id)); 98 players_.insert(PlayerIdentifier(observer, player_id));
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 return; 287 return;
261 288
262 SetAudioFocusState(State::ACTIVE); 289 SetAudioFocusState(State::ACTIVE);
263 290
264 for (const auto& it : players_) 291 for (const auto& it : players_)
265 it.observer->OnResume(it.player_id); 292 it.observer->OnResume(it.player_id);
266 293
267 UpdateWebContents(); 294 UpdateWebContents();
268 } 295 }
269 296
270 MediaSession::MediaSession(WebContents* web_contents) 297 MediaSession::MediaSession(WebContents* web_contents, int session_id_)
271 : WebContentsObserver(web_contents), 298 : WebContentsObserver(web_contents),
272 audio_focus_state_(State::INACTIVE), 299 audio_focus_state_(State::INACTIVE),
273 audio_focus_type_(Type::Transient) {} 300 audio_focus_type_(Type::Transient),
301 session_id_(session_id_) {}
274 302
275 void MediaSession::Initialize() { 303 void MediaSession::Initialize() {
276 JNIEnv* env = base::android::AttachCurrentThread(); 304 JNIEnv* env = base::android::AttachCurrentThread();
277 DCHECK(env); 305 DCHECK(env);
278 j_media_session_.Reset(Java_MediaSession_createMediaSession( 306 j_media_session_.Reset(Java_MediaSession_createMediaSession(
279 env, 307 env,
280 base::android::GetApplicationContext(), 308 base::android::GetApplicationContext(),
281 reinterpret_cast<intptr_t>(this))); 309 reinterpret_cast<intptr_t>(this)));
282 } 310 }
283 311
(...skipping 19 matching lines...) Expand all
303 JNIEnv* env = base::android::AttachCurrentThread(); 331 JNIEnv* env = base::android::AttachCurrentThread();
304 DCHECK(env); 332 DCHECK(env);
305 Java_MediaSession_abandonAudioFocus(env, j_media_session_.obj()); 333 Java_MediaSession_abandonAudioFocus(env, j_media_session_.obj());
306 } 334 }
307 335
308 SetAudioFocusState(State::INACTIVE); 336 SetAudioFocusState(State::INACTIVE);
309 UpdateWebContents(); 337 UpdateWebContents();
310 } 338 }
311 339
312 void MediaSession::UpdateWebContents() { 340 void MediaSession::UpdateWebContents() {
313 static_cast<WebContentsImpl*>(web_contents())->OnMediaSessionStateChanged(); 341 static_cast<WebContentsImpl*>(web_contents())
342 ->OnMediaSessionStateChanged(this);
314 } 343 }
315 344
316 void MediaSession::SetAudioFocusState(State audio_focus_state) { 345 void MediaSession::SetAudioFocusState(State audio_focus_state) {
317 if (audio_focus_state == audio_focus_state_) 346 if (audio_focus_state == audio_focus_state_)
318 return; 347 return;
319 348
320 audio_focus_state_ = audio_focus_state; 349 audio_focus_state_ = audio_focus_state;
321 switch (audio_focus_state_) { 350 switch (audio_focus_state_) {
322 case State::ACTIVE: 351 case State::ACTIVE:
323 uma_helper_.OnSessionActive(); 352 uma_helper_.OnSessionActive();
324 break; 353 break;
325 case State::SUSPENDED: 354 case State::SUSPENDED:
326 uma_helper_.OnSessionSuspended(); 355 uma_helper_.OnSessionSuspended();
327 break; 356 break;
328 case State::INACTIVE: 357 case State::INACTIVE:
329 uma_helper_.OnSessionInactive(); 358 uma_helper_.OnSessionInactive();
330 break; 359 break;
331 } 360 }
332 } 361 }
333 362
334 } // namespace content 363 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/media/android/media_session.h ('k') | content/browser/web_contents/web_contents_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698