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

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: 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/browser_media_player_manager.h"
whywhat 2015/05/12 12:50:45 You don't need this include.
mlamouri (slow - plz ping) 2015/05/19 21:56:15 Done.
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::~MediaSession() {
32 DCHECK(players_.IsEmpty());
33 DCHECK(!has_audio_focus_);
34 }
35
36 void MediaSession::Initialize() {
37 JNIEnv* env = base::android::AttachCurrentThread();
38 DCHECK(env);
39 j_media_session_.Reset(Java_MediaSession_createMediaSession(
40 env,
41 base::android::GetApplicationContext(),
42 reinterpret_cast<intptr_t>(this)));
43 }
44
45 MediaSession::MediaSession(WebContents* web_contents)
46 : WebContentsObserver(web_contents)
whywhat 2015/05/12 12:50:45 follow the style guide here that's different from
mlamouri (slow - plz ping) 2015/05/19 21:56:15 Sorry.
47 , has_audio_focus_(false)
48 , audio_focus_type_(Type::Transient) {
49 }
50
51 bool MediaSession::RequestAudioFocus(MediaSessionDelegate* delegate,
52 int player_id,
53 Type type) {
54 // If the audio focus is already granted and is of type Content, there is
55 // nothing to do. If it is granted of type Transient the requested type is
56 // also transient, there is also nothing to do. Otherwise, the session needs
57 // to request audio focus again.
58 if (has_audio_focus_ &&
whywhat 2015/05/12 12:50:45 if you had None in Type as the first member, could
mlamouri (slow - plz ping) 2015/05/19 21:56:15 I wouldn't recommend doing that. I could write a h
59 (audio_focus_type_ == Type::Content || audio_focus_type_ == type)) {
60 return true;
61 }
62
63 // The session should be reset if a player is starting while all players are
64 // suspended.
65 if (!has_audio_focus_)
66 players_.Clear();
67
68 players_.Add(new PlayerIdentifier(delegate, player_id));
69
70 JNIEnv* env = base::android::AttachCurrentThread();
71 DCHECK(env);
72 has_audio_focus_ = Java_MediaSession_requestAudioFocus(
73 env, j_media_session_.obj(), type == Type::Transient);
74 audio_focus_type_ = type;
75 return has_audio_focus_;
76 }
77
78 void MediaSession::AbandonAudioFocus(MediaSessionDelegate* delegate,
79 int player_id) {
80 for (PlayersMap::Iterator<PlayerIdentifier> iter(&players_); !iter.IsAtEnd();
whywhat 2015/05/12 12:50:45 why using IDMap if you're not using its ids? somet
mlamouri (slow - plz ping) 2015/05/19 21:56:15 Done. I'm using a hash_set now. It avoids adding d
81 iter.Advance()) {
82 PlayerIdentifier* player_identifier = iter.GetCurrentValue();
83 if (delegate == player_identifier->delegate_ &&
84 player_id == player_identifier->id_) {
85 players_.Remove(iter.GetCurrentKey());
86 }
87 }
88
89 AbandonAudioFocusIfNeeded();
90 }
91
92 void MediaSession::AbandonAudioFocus(MediaSessionDelegate* delegate) {
93 for (PlayersMap::Iterator<PlayerIdentifier> iter(&players_); !iter.IsAtEnd();
94 iter.Advance()) {
95 PlayerIdentifier* player_identifier = iter.GetCurrentValue();
96 if (delegate == player_identifier->delegate_)
97 players_.Remove(iter.GetCurrentKey());
98 }
99
100 AbandonAudioFocusIfNeeded();
101 }
102
103 void MediaSession::AbandonAudioFocusIfNeeded() {
104 if (!has_audio_focus_ || !players_.IsEmpty())
105 return;
106
107 JNIEnv* env = base::android::AttachCurrentThread();
108 DCHECK(env);
109 Java_MediaSession_abandonAudioFocus(env, j_media_session_.obj());
110 has_audio_focus_ = false;
111 }
112
113 void MediaSession::OnSuspend(JNIEnv* env, jobject obj) {
114 has_audio_focus_ = false;
115
116 for (PlayersMap::Iterator<PlayerIdentifier> iter(&players_); !iter.IsAtEnd();
117 iter.Advance()) {
118 PlayerIdentifier* player_identifier = iter.GetCurrentValue();
119 player_identifier->delegate_->OnSuspend(player_identifier->id_);
120 }
121 }
122
123 void MediaSession::OnResume(JNIEnv* env, jobject obj) {
124 has_audio_focus_ = true;
125
126 for (PlayersMap::Iterator<PlayerIdentifier> iter(&players_); !iter.IsAtEnd();
127 iter.Advance()) {
128 PlayerIdentifier* player_identifier = iter.GetCurrentValue();
129 player_identifier->delegate_->OnResume(player_identifier->id_);
130 }
131 }
132
133 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698