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

Unified 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: with tests 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/media/android/media_session.cc
diff --git a/content/browser/media/android/media_session.cc b/content/browser/media/android/media_session.cc
new file mode 100644
index 0000000000000000000000000000000000000000..03b4749b7a12ecbd812c45753d4c310a10497dcc
--- /dev/null
+++ b/content/browser/media/android/media_session.cc
@@ -0,0 +1,175 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/media/android/media_session.h"
+
+#include "base/android/jni_android.h"
+#include "content/browser/media/android/media_session_observer.h"
+#include "jni/MediaSession_jni.h"
+
+namespace content {
+
+DEFINE_WEB_CONTENTS_USER_DATA_KEY(MediaSession);
+
+// static
+bool content::MediaSession::RegisterMediaSession(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+// static
+MediaSession* MediaSession::Get(WebContents* web_contents) {
+ MediaSession* session = FromWebContents(web_contents);
+ if (!session) {
+ CreateForWebContents(web_contents);
+ session = FromWebContents(web_contents);
+ session->Initialize();
+ }
+ return session;
+}
+
+MediaSession::PlayerIdentifier::PlayerIdentifier(MediaSessionObserver* observer,
+ int player_id)
+ : observer(observer),
+ player_id(player_id) {
+}
+
+bool MediaSession::PlayerIdentifier::operator==(
+ const PlayerIdentifier& other) const {
+ return this->observer == other.observer && this->player_id == other.player_id;
+}
+
+size_t MediaSession::PlayerIdentifier::Hash::operator()(
+ const PlayerIdentifier& player_identifier) const {
+ size_t hash = BASE_HASH_NAMESPACE::hash<MediaSessionObserver*>()(
+ player_identifier.observer);
+ hash += BASE_HASH_NAMESPACE::hash<int>()(player_identifier.player_id);
+ return hash;
+}
+
+MediaSession::~MediaSession() {
+ DCHECK(players_.empty());
+ DCHECK(!has_audio_focus_);
+}
+
+void MediaSession::Initialize() {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ DCHECK(env);
+ j_media_session_.Reset(Java_MediaSession_createMediaSession(
+ env,
+ base::android::GetApplicationContext(),
+ reinterpret_cast<intptr_t>(this)));
+}
+
+MediaSession::MediaSession(WebContents* web_contents)
+ : WebContentsObserver(web_contents),
+ has_audio_focus_(false),
+ audio_focus_type_(Type::Transient) {
+}
+
+bool MediaSession::AddPlayer(MediaSessionObserver* observer,
+ int player_id,
+ Type type) {
+ // If the audio focus is already granted and is of type Content, there is
+ // nothing to do. If it is granted of type Transient the requested type is
+ // also transient, there is also nothing to do. Otherwise, the session needs
+ // to request audio focus again.
+ if (has_audio_focus_ &&
+ (audio_focus_type_ == Type::Content || audio_focus_type_ == type)) {
+ players_.insert(PlayerIdentifier(observer, player_id));
+ return true;
+ }
+
+ // The session should be reset if a player is starting while all players are
qinmin 2015/05/22 16:43:23 I am not sure whether the logic is appropriate. Sa
mlamouri (slow - plz ping) 2015/05/22 19:01:58 You're right, we should make sure the audio focus
+ // suspended.
+ if (!has_audio_focus_)
+ players_.clear();
+
+ has_audio_focus_ = RequestSystemAudioFocus(type);
+ audio_focus_type_ = type;
+
+ if (has_audio_focus_)
+ players_.insert(PlayerIdentifier(observer, player_id));
+
+ return has_audio_focus_;
+}
+
+void MediaSession::RemovePlayer(MediaSessionObserver* observer,
+ int player_id) {
+ auto it = players_.find(PlayerIdentifier(observer, player_id));
+ if (it != players_.end())
+ players_.erase(it);
+
+ AbandonSystemAudioFocusIfNeeded();
+}
+
+void MediaSession::RemovePlayers(MediaSessionObserver* observer) {
+ for (auto it = players_.begin(); it != players_.end(); ) {
+ if (it->observer == observer)
+ players_.erase(it++);
+ else
+ ++it;
+ }
+
+ AbandonSystemAudioFocusIfNeeded();
+}
+
+bool MediaSession::RequestSystemAudioFocus(Type type) {
+ if (j_media_session_.is_null())
+ return true;
+
+ JNIEnv* env = base::android::AttachCurrentThread();
+ DCHECK(env);
+ return Java_MediaSession_requestAudioFocus(env, j_media_session_.obj(),
+ type == Type::Transient);
+}
+
+void MediaSession::AbandonSystemAudioFocusIfNeeded() {
+ if (!has_audio_focus_ || !players_.empty())
+ return;
+
+ if (!j_media_session_.is_null()) {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ DCHECK(env);
+ Java_MediaSession_abandonAudioFocus(env, j_media_session_.obj());
+ }
+
+ has_audio_focus_ = false;
+}
+
+void MediaSession::OnSuspend(JNIEnv* env, jobject obj) {
+ OnSuspend();
+}
+
+void MediaSession::OnSuspend() {
+ has_audio_focus_ = false;
+
+ for (const auto& it : players_)
+ it.observer->OnSuspend(it.player_id);
+}
+
+
+void MediaSession::OnResume(JNIEnv* env, jobject obj) {
+ OnResume();
+}
+
+void MediaSession::OnResume() {
+ has_audio_focus_ = true;
+
+ for (const auto& it : players_)
+ it.observer->OnResume(it.player_id);
+}
+
+void MediaSession::ResetJavaRefForTest() {
+ j_media_session_.Reset();
+}
+
+MediaSession::Type MediaSession::audio_focus_type_for_test() const {
+ return audio_focus_type_;
+}
+
+bool MediaSession::has_audio_focus_for_test() const {
+ return has_audio_focus_;
+}
+
+} // namespace content
« 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