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

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: review comments 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..1a9d54aec417bf96b600ffcb55f96a42ccf5b512
--- /dev/null
+++ b/content/browser/media/android/media_session.cc
@@ -0,0 +1,144 @@
+// 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_delegate.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(MediaSessionDelegate* delegate,
+ int player_id)
+ : delegate(delegate),
+ player_id(player_id) {
+}
+
+bool MediaSession::PlayerIdentifier::operator==(
+ const PlayerIdentifier& other) const {
+ return this->delegate == other.delegate && this->player_id == other.player_id;
+}
+
+size_t MediaSession::PlayerIdentifier::Hash::operator()(
+ const PlayerIdentifier& player_identifier) const {
+ size_t hash = BASE_HASH_NAMESPACE::hash<MediaSessionDelegate*>()(
+ player_identifier.delegate);
+ 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(MediaSessionDelegate* delegate,
+ 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(delegate, player_id));
+ return true;
+ }
+
+ // The session should be reset if a player is starting while all players are
+ // suspended.
+ if (!has_audio_focus_)
+ players_.clear();
+
+ JNIEnv* env = base::android::AttachCurrentThread();
+ DCHECK(env);
+ has_audio_focus_ = Java_MediaSession_requestAudioFocus(
+ env, j_media_session_.obj(), type == Type::Transient);
+ audio_focus_type_ = type;
+
+ if (has_audio_focus_)
+ players_.insert(PlayerIdentifier(delegate, player_id));
+
+ return has_audio_focus_;
+}
+
+void MediaSession::RemovePlayer(MediaSessionDelegate* delegate,
+ int player_id) {
+ auto it = players_.find(PlayerIdentifier(delegate, player_id));
+ if (it != players_.end())
+ players_.erase(it);
+
+ AbandonSystemAudioFocusIfNeeded();
+}
+
+void MediaSession::RemovePlayers(MediaSessionDelegate* delegate) {
+ for (auto it = players_.begin(); it != players_.end(); ) {
+ if (it->delegate == delegate)
+ players_.erase(it++);
qinmin 2015/05/20 21:35:25 break?
mlamouri (slow - plz ping) 2015/05/22 15:58:52 There might be more than one player.
+ else
+ ++it;
+ }
+
+ AbandonSystemAudioFocusIfNeeded();
+}
+
+void MediaSession::AbandonSystemAudioFocusIfNeeded() {
+ if (!has_audio_focus_ || !players_.empty())
+ return;
+
+ 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) {
+ has_audio_focus_ = false;
+
+ for (const auto& it : players_)
+ it.delegate->OnSuspend(it.player_id);
+}
+
+void MediaSession::OnResume(JNIEnv* env, jobject obj) {
+ has_audio_focus_ = true;
+
+ for (const auto& it : players_)
+ it.delegate->OnResume(it.player_id);
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698