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

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: Moved OnMediaSessionStateChanged to WebContentsImpl Created 5 years, 5 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/browser/web_contents/web_contents_impl.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/public/browser/web_contents_delegate.h"
9 #include "jni/MediaSession_jni.h" 12 #include "jni/MediaSession_jni.h"
10 13
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) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
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)
79 players_.clear(); 82 players_.clear();
80 83
81 players_.insert(PlayerIdentifier(observer, player_id)); 84 players_.insert(PlayerIdentifier(observer, player_id));
85 UpdateWebContents();
82 86
83 return true; 87 return true;
84 } 88 }
85 89
86 void MediaSession::RemovePlayer(MediaSessionObserver* observer, 90 void MediaSession::RemovePlayer(MediaSessionObserver* observer,
87 int player_id) { 91 int player_id) {
88 auto it = players_.find(PlayerIdentifier(observer, player_id)); 92 auto it = players_.find(PlayerIdentifier(observer, player_id));
89 if (it != players_.end()) 93 if (it != players_.end())
90 players_.erase(it); 94 players_.erase(it);
91 95
92 AbandonSystemAudioFocusIfNeeded(); 96 AbandonSystemAudioFocusIfNeeded();
93 } 97 }
94 98
95 void MediaSession::RemovePlayers(MediaSessionObserver* observer) { 99 void MediaSession::RemovePlayers(MediaSessionObserver* observer) {
96 for (auto it = players_.begin(); it != players_.end();) { 100 for (auto it = players_.begin(); it != players_.end();) {
97 if (it->observer == observer) 101 if (it->observer == observer)
98 players_.erase(it++); 102 players_.erase(it++);
99 else 103 else
100 ++it; 104 ++it;
101 } 105 }
102 106
103 AbandonSystemAudioFocusIfNeeded(); 107 AbandonSystemAudioFocusIfNeeded();
104 } 108 }
105 109
106 void MediaSession::OnSuspend(JNIEnv* env, jobject obj, jboolean temporary) { 110 void MediaSession::OnSuspend(JNIEnv* env, jobject obj, jboolean temporary) {
107 OnSuspend(temporary); 111 OnSuspendInternal(temporary);
112 UpdateWebContents();
108 } 113 }
109 114
110 void MediaSession::OnResume(JNIEnv* env, jobject obj) { 115 void MediaSession::OnResume(JNIEnv* env, jobject obj) {
111 OnResume(); 116 OnResumeInternal();
117 UpdateWebContents();
118 }
119
120 void MediaSession::Resume() {
121 DCHECK(IsSuspended());
122
123 OnResumeInternal();
124 }
125
126 void MediaSession::Suspend() {
127 DCHECK(!IsSuspended());
128
129 // Since the playback can be resumed, it's a transient suspension.
130 OnSuspendInternal(true);
131 }
132
133 bool MediaSession::IsSuspended() const {
134 return audio_focus_state_ != State::Active;
135 }
136
137 bool MediaSession::IsControllable() const {
138 // Only content type media session can be controllable unless it's stopped.
139 return audio_focus_state_ != State::Suspended &&
140 audio_focus_type_ == Type::Content;
112 } 141 }
113 142
114 void MediaSession::ResetJavaRefForTest() { 143 void MediaSession::ResetJavaRefForTest() {
115 j_media_session_.Reset(); 144 j_media_session_.Reset();
116 } 145 }
117 146
118 bool MediaSession::IsActiveForTest() const { 147 bool MediaSession::IsActiveForTest() const {
119 return audio_focus_state_ == State::Active; 148 return audio_focus_state_ == State::Active;
120 } 149 }
121 150
122 MediaSession::Type MediaSession::audio_focus_type_for_test() const { 151 MediaSession::Type MediaSession::audio_focus_type_for_test() const {
123 return audio_focus_type_; 152 return audio_focus_type_;
124 } 153 }
125 154
126 void MediaSession::OnSuspend(bool temporary) { 155 void MediaSession::RemoveAllPlayersForTest() {
156 players_.clear();
157 AbandonSystemAudioFocusIfNeeded();
158 }
159
160 void MediaSession::OnSuspendInternal(bool temporary) {
127 if (temporary) 161 if (temporary)
128 audio_focus_state_ = State::TemporarilySuspended; 162 audio_focus_state_ = State::TemporarilySuspended;
129 else 163 else
130 audio_focus_state_ = State::Suspended; 164 audio_focus_state_ = State::Suspended;
131 165
132 for (const auto& it : players_) 166 for (const auto& it : players_)
133 it.observer->OnSuspend(it.player_id); 167 it.observer->OnSuspend(it.player_id);
134 } 168 }
135 169
136 void MediaSession::OnResume() { 170 void MediaSession::OnResumeInternal() {
137 audio_focus_state_ = State::Active; 171 audio_focus_state_ = State::Active;
138 172
139 for (const auto& it : players_) 173 for (const auto& it : players_)
140 it.observer->OnResume(it.player_id); 174 it.observer->OnResume(it.player_id);
141 } 175 }
142 176
143 MediaSession::MediaSession(WebContents* web_contents) 177 MediaSession::MediaSession(WebContents* web_contents)
144 : WebContentsObserver(web_contents), 178 : WebContentsObserver(web_contents),
145 audio_focus_state_(State::Suspended), 179 audio_focus_state_(State::Suspended),
146 audio_focus_type_(Type::Transient) { 180 audio_focus_type_(Type::Transient) {}
147 }
148 181
149 void MediaSession::Initialize() { 182 void MediaSession::Initialize() {
150 JNIEnv* env = base::android::AttachCurrentThread(); 183 JNIEnv* env = base::android::AttachCurrentThread();
151 DCHECK(env); 184 DCHECK(env);
152 j_media_session_.Reset(Java_MediaSession_createMediaSession( 185 j_media_session_.Reset(Java_MediaSession_createMediaSession(
153 env, 186 env,
154 base::android::GetApplicationContext(), 187 base::android::GetApplicationContext(),
155 reinterpret_cast<intptr_t>(this))); 188 reinterpret_cast<intptr_t>(this)));
156 } 189 }
157 190
(...skipping 13 matching lines...) Expand all
171 return; 204 return;
172 205
173 // During tests, j_media_session_ might be null. 206 // During tests, j_media_session_ might be null.
174 if (!j_media_session_.is_null()) { 207 if (!j_media_session_.is_null()) {
175 JNIEnv* env = base::android::AttachCurrentThread(); 208 JNIEnv* env = base::android::AttachCurrentThread();
176 DCHECK(env); 209 DCHECK(env);
177 Java_MediaSession_abandonAudioFocus(env, j_media_session_.obj()); 210 Java_MediaSession_abandonAudioFocus(env, j_media_session_.obj());
178 } 211 }
179 212
180 audio_focus_state_ = State::Suspended; 213 audio_focus_state_ = State::Suspended;
214 UpdateWebContents();
215 }
216
217 void MediaSession::UpdateWebContents() {
218 static_cast<WebContentsImpl*>(web_contents())->OnMediaSessionStateChanged();
181 } 219 }
182 220
183 } // namespace content 221 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/media/android/media_session.h ('k') | content/browser/media/android/media_session_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698