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

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: Fixed Mounir's comments 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
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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 // to request audio focus again. 63 // to request audio focus again.
62 if (audio_focus_state_ == State::Active && 64 if (audio_focus_state_ == State::Active &&
63 (audio_focus_type_ == Type::Content || audio_focus_type_ == type)) { 65 (audio_focus_type_ == Type::Content || audio_focus_type_ == type)) {
64 players_.insert(PlayerIdentifier(observer, player_id)); 66 players_.insert(PlayerIdentifier(observer, player_id));
65 return true; 67 return true;
66 } 68 }
67 69
68 State old_audio_focus_state = audio_focus_state_; 70 State old_audio_focus_state = audio_focus_state_;
69 audio_focus_state_ = RequestSystemAudioFocus(type) ? State::Active 71 audio_focus_state_ = RequestSystemAudioFocus(type) ? State::Active
70 : State::Suspended; 72 : State::Suspended;
73 Type old_audio_focus_type = audio_focus_type_;
71 audio_focus_type_ = type; 74 audio_focus_type_ = type;
72 75
73 if (audio_focus_state_ != State::Active) 76 if (audio_focus_state_ != State::Active)
74 return false; 77 return false;
75 78
76 // The session should be reset if a player is starting while all players are 79 // The session should be reset if a player is starting while all players are
77 // suspended. 80 // suspended.
78 if (old_audio_focus_state != State::Active) 81 if (old_audio_focus_state != State::Active) {
82 // Hide the controls if the type switched from Content to Transient.
83 if (audio_focus_type_ == Type::Transient &&
84 old_audio_focus_type == Type::Content)
85 UpdateMediaControls();
mlamouri (slow - plz ping) 2015/06/24 16:15:02 With the new model of calling UpdateMediaControls(
whywhat 2015/06/24 18:36:15 Indeed. Done.
79 players_.clear(); 86 players_.clear();
87 }
80 88
81 players_.insert(PlayerIdentifier(observer, player_id)); 89 players_.insert(PlayerIdentifier(observer, player_id));
90 UpdateMediaControls();
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 OnSuspendInternal(temporary);
117 UpdateMediaControls();
108 } 118 }
109 119
110 void MediaSession::OnResume(JNIEnv* env, jobject obj) { 120 void MediaSession::OnResume(JNIEnv* env, jobject obj) {
111 OnResume(); 121 OnResumeInternal();
122 UpdateMediaControls();
123 }
124
125 void MediaSession::OnControlsPause() {
126 DCHECK(!IsPaused());
127
128 // Since the playback can be resumed, it's a transient suspension.
129 OnSuspendInternal(true);
130 }
131
132 void MediaSession::OnControlsResume() {
133 DCHECK(IsPaused());
134
135 OnResumeInternal();
136 }
137
138 bool MediaSession::IsPaused() {
139 return audio_focus_state_ != State::Active;
140 }
141
142 bool MediaSession::IsControllable() {
143 return audio_focus_state_ != State::Suspended &&
144 audio_focus_type_ == Type::Content;
112 } 145 }
113 146
114 void MediaSession::ResetJavaRefForTest() { 147 void MediaSession::ResetJavaRefForTest() {
115 j_media_session_.Reset(); 148 j_media_session_.Reset();
116 } 149 }
117 150
118 bool MediaSession::IsActiveForTest() const { 151 bool MediaSession::IsActiveForTest() const {
119 return audio_focus_state_ == State::Active; 152 return audio_focus_state_ == State::Active;
120 } 153 }
121 154
122 MediaSession::Type MediaSession::audio_focus_type_for_test() const { 155 MediaSession::Type MediaSession::audio_focus_type_for_test() const {
123 return audio_focus_type_; 156 return audio_focus_type_;
124 } 157 }
125 158
126 void MediaSession::OnSuspend(bool temporary) { 159 void MediaSession::RemoveAllPlayersForTest() {
160 players_.clear();
161 AbandonSystemAudioFocusIfNeeded();
162 }
163
164 void MediaSession::OnSuspendInternal(bool temporary) {
127 if (temporary) 165 if (temporary)
128 audio_focus_state_ = State::TemporarilySuspended; 166 audio_focus_state_ = State::TemporarilySuspended;
129 else 167 else
130 audio_focus_state_ = State::Suspended; 168 audio_focus_state_ = State::Suspended;
131 169
132 for (const auto& it : players_) 170 for (const auto& it : players_)
133 it.observer->OnSuspend(it.player_id); 171 it.observer->OnSuspend(it.player_id);
134 } 172 }
135 173
136 void MediaSession::OnResume() { 174 void MediaSession::OnResumeInternal() {
137 audio_focus_state_ = State::Active; 175 audio_focus_state_ = State::Active;
138 176
139 for (const auto& it : players_) 177 for (const auto& it : players_)
140 it.observer->OnResume(it.player_id); 178 it.observer->OnResume(it.player_id);
141 } 179 }
142 180
143 MediaSession::MediaSession(WebContents* web_contents) 181 MediaSession::MediaSession(WebContents* web_contents)
144 : WebContentsObserver(web_contents), 182 : WebContentsObserver(web_contents),
145 audio_focus_state_(State::Suspended), 183 audio_focus_state_(State::Suspended),
146 audio_focus_type_(Type::Transient) { 184 audio_focus_type_(Type::Transient) {
(...skipping 24 matching lines...) Expand all
171 return; 209 return;
172 210
173 // During tests, j_media_session_ might be null. 211 // During tests, j_media_session_ might be null.
174 if (!j_media_session_.is_null()) { 212 if (!j_media_session_.is_null()) {
175 JNIEnv* env = base::android::AttachCurrentThread(); 213 JNIEnv* env = base::android::AttachCurrentThread();
176 DCHECK(env); 214 DCHECK(env);
177 Java_MediaSession_abandonAudioFocus(env, j_media_session_.obj()); 215 Java_MediaSession_abandonAudioFocus(env, j_media_session_.obj());
178 } 216 }
179 217
180 audio_focus_state_ = State::Suspended; 218 audio_focus_state_ = State::Suspended;
219 UpdateMediaControls();
220 }
221
222 void MediaSession::UpdateMediaControls() {
223 if (web_contents()->GetDelegate())
224 web_contents()->GetDelegate()->UpdateMediaControls(web_contents());
181 } 225 }
182 226
183 } // namespace content 227 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698