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..6a1d6bdb14561e5623cf2a37d2441d18bb49fcf0 |
--- /dev/null |
+++ b/content/browser/media/android/media_session.cc |
@@ -0,0 +1,133 @@ |
+// 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/browser_media_player_manager.h" |
whywhat
2015/05/12 12:50:45
You don't need this include.
mlamouri (slow - plz ping)
2015/05/19 21:56:15
Done.
|
+#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::~MediaSession() { |
+ DCHECK(players_.IsEmpty()); |
+ 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) |
whywhat
2015/05/12 12:50:45
follow the style guide here that's different from
mlamouri (slow - plz ping)
2015/05/19 21:56:15
Sorry.
|
+ , has_audio_focus_(false) |
+ , audio_focus_type_(Type::Transient) { |
+} |
+ |
+bool MediaSession::RequestAudioFocus(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_ && |
whywhat
2015/05/12 12:50:45
if you had None in Type as the first member, could
mlamouri (slow - plz ping)
2015/05/19 21:56:15
I wouldn't recommend doing that. I could write a h
|
+ (audio_focus_type_ == Type::Content || audio_focus_type_ == type)) { |
+ return true; |
+ } |
+ |
+ // The session should be reset if a player is starting while all players are |
+ // suspended. |
+ if (!has_audio_focus_) |
+ players_.Clear(); |
+ |
+ players_.Add(new PlayerIdentifier(delegate, player_id)); |
+ |
+ 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; |
+ return has_audio_focus_; |
+} |
+ |
+void MediaSession::AbandonAudioFocus(MediaSessionDelegate* delegate, |
+ int player_id) { |
+ for (PlayersMap::Iterator<PlayerIdentifier> iter(&players_); !iter.IsAtEnd(); |
whywhat
2015/05/12 12:50:45
why using IDMap if you're not using its ids?
somet
mlamouri (slow - plz ping)
2015/05/19 21:56:15
Done. I'm using a hash_set now. It avoids adding d
|
+ iter.Advance()) { |
+ PlayerIdentifier* player_identifier = iter.GetCurrentValue(); |
+ if (delegate == player_identifier->delegate_ && |
+ player_id == player_identifier->id_) { |
+ players_.Remove(iter.GetCurrentKey()); |
+ } |
+ } |
+ |
+ AbandonAudioFocusIfNeeded(); |
+} |
+ |
+void MediaSession::AbandonAudioFocus(MediaSessionDelegate* delegate) { |
+ for (PlayersMap::Iterator<PlayerIdentifier> iter(&players_); !iter.IsAtEnd(); |
+ iter.Advance()) { |
+ PlayerIdentifier* player_identifier = iter.GetCurrentValue(); |
+ if (delegate == player_identifier->delegate_) |
+ players_.Remove(iter.GetCurrentKey()); |
+ } |
+ |
+ AbandonAudioFocusIfNeeded(); |
+} |
+ |
+void MediaSession::AbandonAudioFocusIfNeeded() { |
+ if (!has_audio_focus_ || !players_.IsEmpty()) |
+ 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 (PlayersMap::Iterator<PlayerIdentifier> iter(&players_); !iter.IsAtEnd(); |
+ iter.Advance()) { |
+ PlayerIdentifier* player_identifier = iter.GetCurrentValue(); |
+ player_identifier->delegate_->OnSuspend(player_identifier->id_); |
+ } |
+} |
+ |
+void MediaSession::OnResume(JNIEnv* env, jobject obj) { |
+ has_audio_focus_ = true; |
+ |
+ for (PlayersMap::Iterator<PlayerIdentifier> iter(&players_); !iter.IsAtEnd(); |
+ iter.Advance()) { |
+ PlayerIdentifier* player_identifier = iter.GetCurrentValue(); |
+ player_identifier->delegate_->OnResume(player_identifier->id_); |
+ } |
+} |
+ |
+} // namespace content |