OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/media/android/media_session.h" |
| 6 |
| 7 #include "base/android/jni_android.h" |
| 8 #include "content/browser/media/android/media_session_observer.h" |
| 9 #include "jni/MediaSession_jni.h" |
| 10 |
| 11 namespace content { |
| 12 |
| 13 DEFINE_WEB_CONTENTS_USER_DATA_KEY(MediaSession); |
| 14 |
| 15 // static |
| 16 bool content::MediaSession::RegisterMediaSession(JNIEnv* env) { |
| 17 return RegisterNativesImpl(env); |
| 18 } |
| 19 |
| 20 // static |
| 21 MediaSession* MediaSession::Get(WebContents* web_contents) { |
| 22 MediaSession* session = FromWebContents(web_contents); |
| 23 if (!session) { |
| 24 CreateForWebContents(web_contents); |
| 25 session = FromWebContents(web_contents); |
| 26 session->Initialize(); |
| 27 } |
| 28 return session; |
| 29 } |
| 30 |
| 31 MediaSession::PlayerIdentifier::PlayerIdentifier(MediaSessionObserver* observer, |
| 32 int player_id) |
| 33 : observer(observer), |
| 34 player_id(player_id) { |
| 35 } |
| 36 |
| 37 bool MediaSession::PlayerIdentifier::operator==( |
| 38 const PlayerIdentifier& other) const { |
| 39 return this->observer == other.observer && this->player_id == other.player_id; |
| 40 } |
| 41 |
| 42 size_t MediaSession::PlayerIdentifier::Hash::operator()( |
| 43 const PlayerIdentifier& player_identifier) const { |
| 44 size_t hash = BASE_HASH_NAMESPACE::hash<MediaSessionObserver*>()( |
| 45 player_identifier.observer); |
| 46 hash += BASE_HASH_NAMESPACE::hash<int>()(player_identifier.player_id); |
| 47 return hash; |
| 48 } |
| 49 |
| 50 MediaSession::~MediaSession() { |
| 51 DCHECK(players_.empty()); |
| 52 DCHECK(!has_audio_focus_); |
| 53 } |
| 54 |
| 55 void MediaSession::Initialize() { |
| 56 JNIEnv* env = base::android::AttachCurrentThread(); |
| 57 DCHECK(env); |
| 58 j_media_session_.Reset(Java_MediaSession_createMediaSession( |
| 59 env, |
| 60 base::android::GetApplicationContext(), |
| 61 reinterpret_cast<intptr_t>(this))); |
| 62 } |
| 63 |
| 64 MediaSession::MediaSession(WebContents* web_contents) |
| 65 : WebContentsObserver(web_contents), |
| 66 has_audio_focus_(false), |
| 67 audio_focus_type_(Type::Transient) { |
| 68 } |
| 69 |
| 70 bool MediaSession::AddPlayer(MediaSessionObserver* observer, |
| 71 int player_id, |
| 72 Type type) { |
| 73 // If the audio focus is already granted and is of type Content, there is |
| 74 // nothing to do. If it is granted of type Transient the requested type is |
| 75 // also transient, there is also nothing to do. Otherwise, the session needs |
| 76 // to request audio focus again. |
| 77 if (has_audio_focus_ && |
| 78 (audio_focus_type_ == Type::Content || audio_focus_type_ == type)) { |
| 79 players_.insert(PlayerIdentifier(observer, player_id)); |
| 80 return true; |
| 81 } |
| 82 |
| 83 bool had_audio_focus = has_audio_focus_; |
| 84 has_audio_focus_ = RequestSystemAudioFocus(type); |
| 85 audio_focus_type_ = type; |
| 86 |
| 87 if (!has_audio_focus_) |
| 88 return false; |
| 89 |
| 90 // The session should be reset if a player is starting while all players are |
| 91 // suspended. |
| 92 if (!had_audio_focus) |
| 93 players_.clear(); |
| 94 |
| 95 players_.insert(PlayerIdentifier(observer, player_id)); |
| 96 |
| 97 return true; |
| 98 } |
| 99 |
| 100 void MediaSession::RemovePlayer(MediaSessionObserver* observer, |
| 101 int player_id) { |
| 102 auto it = players_.find(PlayerIdentifier(observer, player_id)); |
| 103 if (it != players_.end()) |
| 104 players_.erase(it); |
| 105 |
| 106 AbandonSystemAudioFocusIfNeeded(); |
| 107 } |
| 108 |
| 109 void MediaSession::RemovePlayers(MediaSessionObserver* observer) { |
| 110 for (auto it = players_.begin(); it != players_.end(); ) { |
| 111 if (it->observer == observer) |
| 112 players_.erase(it++); |
| 113 else |
| 114 ++it; |
| 115 } |
| 116 |
| 117 AbandonSystemAudioFocusIfNeeded(); |
| 118 } |
| 119 |
| 120 bool MediaSession::RequestSystemAudioFocus(Type type) { |
| 121 if (j_media_session_.is_null()) |
| 122 return true; |
| 123 |
| 124 JNIEnv* env = base::android::AttachCurrentThread(); |
| 125 DCHECK(env); |
| 126 return Java_MediaSession_requestAudioFocus(env, j_media_session_.obj(), |
| 127 type == Type::Transient); |
| 128 } |
| 129 |
| 130 void MediaSession::AbandonSystemAudioFocusIfNeeded() { |
| 131 if (!has_audio_focus_ || !players_.empty()) |
| 132 return; |
| 133 |
| 134 if (!j_media_session_.is_null()) { |
| 135 JNIEnv* env = base::android::AttachCurrentThread(); |
| 136 DCHECK(env); |
| 137 Java_MediaSession_abandonAudioFocus(env, j_media_session_.obj()); |
| 138 } |
| 139 |
| 140 has_audio_focus_ = false; |
| 141 } |
| 142 |
| 143 void MediaSession::OnSuspend(JNIEnv* env, jobject obj) { |
| 144 OnSuspend(); |
| 145 } |
| 146 |
| 147 void MediaSession::OnSuspend() { |
| 148 has_audio_focus_ = false; |
| 149 |
| 150 for (const auto& it : players_) |
| 151 it.observer->OnSuspend(it.player_id); |
| 152 } |
| 153 |
| 154 |
| 155 void MediaSession::OnResume(JNIEnv* env, jobject obj) { |
| 156 OnResume(); |
| 157 } |
| 158 |
| 159 void MediaSession::OnResume() { |
| 160 has_audio_focus_ = true; |
| 161 |
| 162 for (const auto& it : players_) |
| 163 it.observer->OnResume(it.player_id); |
| 164 } |
| 165 |
| 166 void MediaSession::ResetJavaRefForTest() { |
| 167 j_media_session_.Reset(); |
| 168 } |
| 169 |
| 170 MediaSession::Type MediaSession::audio_focus_type_for_test() const { |
| 171 return audio_focus_type_; |
| 172 } |
| 173 |
| 174 bool MediaSession::has_audio_focus_for_test() const { |
| 175 return has_audio_focus_; |
| 176 } |
| 177 |
| 178 } // namespace content |
OLD | NEW |