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

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

Issue 1159113006: [Android] A prototype of the interactive media notification. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added browser tests Created 5 years, 6 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/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
13
mlamouri (slow - plz ping) 2015/06/18 16:43:22 nit: remove empty line
mlamouri (slow - plz ping) 2015/06/22 14:34:17 double nit: you didn't do it.
whywhat 2015/06/23 19:39:10 Do you check the diff that you commented on? :)
11 namespace content { 14 namespace content {
12 15
13 DEFINE_WEB_CONTENTS_USER_DATA_KEY(MediaSession); 16 DEFINE_WEB_CONTENTS_USER_DATA_KEY(MediaSession);
14 17
15 MediaSession::PlayerIdentifier::PlayerIdentifier(MediaSessionObserver* observer, 18 MediaSession::PlayerIdentifier::PlayerIdentifier(MediaSessionObserver* observer,
16 int player_id) 19 int player_id)
17 : observer(observer), 20 : observer(observer),
18 player_id(player_id) { 21 player_id(player_id) {
19 } 22 }
20 23
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 // to request audio focus again. 64 // to request audio focus again.
62 if (audio_focus_state_ == State::Active && 65 if (audio_focus_state_ == State::Active &&
63 (audio_focus_type_ == Type::Content || audio_focus_type_ == type)) { 66 (audio_focus_type_ == Type::Content || audio_focus_type_ == type)) {
64 players_.insert(PlayerIdentifier(observer, player_id)); 67 players_.insert(PlayerIdentifier(observer, player_id));
65 return true; 68 return true;
66 } 69 }
67 70
68 State old_audio_focus_state = audio_focus_state_; 71 State old_audio_focus_state = audio_focus_state_;
69 audio_focus_state_ = RequestSystemAudioFocus(type) ? State::Active 72 audio_focus_state_ = RequestSystemAudioFocus(type) ? State::Active
70 : State::Suspended; 73 : State::Suspended;
74 Type old_audio_focus_type = audio_focus_type_;
71 audio_focus_type_ = type; 75 audio_focus_type_ = type;
72 76
73 if (audio_focus_state_ != State::Active) 77 if (audio_focus_state_ != State::Active)
74 return false; 78 return false;
75 79
76 // The session should be reset if a player is starting while all players are 80 // The session should be reset if a player is starting while all players are
77 // suspended. 81 // suspended.
78 if (old_audio_focus_state != State::Active) 82 if (old_audio_focus_state != State::Active) {
83 // Hide the controls if the type switched from Content to Transient.
84 if (audio_focus_type_ == Type::Transient)
85 HideMediaControlsIfNeeded(old_audio_focus_type);
79 players_.clear(); 86 players_.clear();
87 }
80 88
81 players_.insert(PlayerIdentifier(observer, player_id)); 89 players_.insert(PlayerIdentifier(observer, player_id));
90 ShowMediaControlsIfNeeded(audio_focus_type_);
82 91
83 return true; 92 return true;
84 } 93 }
85 94
86 void MediaSession::RemovePlayer(MediaSessionObserver* observer, 95 void MediaSession::RemovePlayer(MediaSessionObserver* observer,
87 int player_id) { 96 int player_id) {
88 auto it = players_.find(PlayerIdentifier(observer, player_id)); 97 auto it = players_.find(PlayerIdentifier(observer, player_id));
89 if (it != players_.end()) 98 if (it != players_.end())
90 players_.erase(it); 99 players_.erase(it);
91 100
92 AbandonSystemAudioFocusIfNeeded(); 101 AbandonSystemAudioFocusIfNeeded();
93 } 102 }
94 103
95 void MediaSession::RemovePlayers(MediaSessionObserver* observer) { 104 void MediaSession::RemovePlayers(MediaSessionObserver* observer) {
96 for (auto it = players_.begin(); it != players_.end();) { 105 for (auto it = players_.begin(); it != players_.end();) {
97 if (it->observer == observer) 106 if (it->observer == observer)
98 players_.erase(it++); 107 players_.erase(it++);
99 else 108 else
100 ++it; 109 ++it;
101 } 110 }
102 111
103 AbandonSystemAudioFocusIfNeeded(); 112 AbandonSystemAudioFocusIfNeeded();
104 } 113 }
105 114
106 void MediaSession::OnSuspend(JNIEnv* env, jobject obj, jboolean temporary) { 115 void MediaSession::OnSuspend(JNIEnv* env, jobject obj, jboolean temporary) {
107 OnSuspend(temporary); 116 OnSuspend(temporary);
117
118 if (temporary) {
119 // Reflect the paused state on the media controls.
120 ShowMediaControlsIfNeeded(audio_focus_type_);
121 } else {
122 HideMediaControlsIfNeeded(audio_focus_type_);
123 }
mlamouri (slow - plz ping) 2015/06/18 16:43:22 Shouldn't that be in the ::OnSuspend(bool) method?
whywhat 2015/06/19 16:00:34 OnSuspend(bool) is called from the controls so it
mlamouri (slow - plz ping) 2015/06/22 14:34:17 Hmm, I see. Could you rename OnSuspend() to OnSusp
whywhat 2015/06/23 19:39:10 Ok. On 2015/06/22 at 14:34:17, Mounir Lamouri wro
108 } 124 }
109 125
110 void MediaSession::OnResume(JNIEnv* env, jobject obj) { 126 void MediaSession::OnResume(JNIEnv* env, jobject obj) {
111 OnResume(); 127 OnResume();
128 ShowMediaControlsIfNeeded(audio_focus_type_);
mlamouri (slow - plz ping) 2015/06/18 16:43:22 ditto
whywhat 2015/06/19 16:00:34 ditto
129 }
130
131 void MediaSession::OnControlsPause() {
132 DCHECK(!IsPaused());
133
134 // Since the playback can be resumed, it's a transient suspension.
135 OnSuspend(true);
136 }
137
138 void MediaSession::OnControlsResume() {
139 DCHECK(IsPaused());
140
141 OnResume();
142 }
143
144 bool MediaSession::IsPaused() {
145 return audio_focus_state_ != State::Active;
112 } 146 }
113 147
114 void MediaSession::ResetJavaRefForTest() { 148 void MediaSession::ResetJavaRefForTest() {
115 j_media_session_.Reset(); 149 j_media_session_.Reset();
116 } 150 }
117 151
118 bool MediaSession::IsActiveForTest() const { 152 bool MediaSession::IsActiveForTest() const {
119 return audio_focus_state_ == State::Active; 153 return audio_focus_state_ == State::Active;
120 } 154 }
121 155
122 MediaSession::Type MediaSession::audio_focus_type_for_test() const { 156 MediaSession::Type MediaSession::audio_focus_type_for_test() const {
123 return audio_focus_type_; 157 return audio_focus_type_;
124 } 158 }
125 159
160 void MediaSession::RemoveAllPlayersForTest() {
161 players_.clear();
162 AbandonSystemAudioFocusIfNeeded();
163 }
164
126 void MediaSession::OnSuspend(bool temporary) { 165 void MediaSession::OnSuspend(bool temporary) {
127 if (temporary) 166 if (temporary)
128 audio_focus_state_ = State::TemporarilySuspended; 167 audio_focus_state_ = State::TemporarilySuspended;
129 else 168 else
130 audio_focus_state_ = State::Suspended; 169 audio_focus_state_ = State::Suspended;
131 170
132 for (const auto& it : players_) 171 for (const auto& it : players_)
133 it.observer->OnSuspend(it.player_id); 172 it.observer->OnSuspend(it.player_id);
134 } 173 }
135 174
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 return; 210 return;
172 211
173 // During tests, j_media_session_ might be null. 212 // During tests, j_media_session_ might be null.
174 if (!j_media_session_.is_null()) { 213 if (!j_media_session_.is_null()) {
175 JNIEnv* env = base::android::AttachCurrentThread(); 214 JNIEnv* env = base::android::AttachCurrentThread();
176 DCHECK(env); 215 DCHECK(env);
177 Java_MediaSession_abandonAudioFocus(env, j_media_session_.obj()); 216 Java_MediaSession_abandonAudioFocus(env, j_media_session_.obj());
178 } 217 }
179 218
180 audio_focus_state_ = State::Suspended; 219 audio_focus_state_ = State::Suspended;
220 HideMediaControlsIfNeeded(audio_focus_type_);
221 }
222
223 void MediaSession::ShowMediaControlsIfNeeded(Type audio_focus_type) {
224 if (audio_focus_type == Type::Content && web_contents()->GetDelegate())
225 web_contents()->GetDelegate()->ShowMediaControls(web_contents());
226 }
227
228 void MediaSession::HideMediaControlsIfNeeded(Type audio_focus_type) {
229 if (audio_focus_type == Type::Content && web_contents()->GetDelegate())
mlamouri (slow - plz ping) 2015/06/18 16:43:22 Why do we need to check the |audio_focus_type|?
whywhat 2015/06/19 16:00:34 Because we don't want to have controls for transie
230 web_contents()->GetDelegate()->HideMediaControls(web_contents());
181 } 231 }
182 232
183 } // namespace content 233 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698