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_delegate.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(MediaSessionDelegate* delegate, | |
32 int player_id) | |
33 : delegate(delegate), | |
34 player_id(player_id) { | |
35 } | |
36 | |
37 bool MediaSession::PlayerIdentifier::operator==( | |
38 const PlayerIdentifier& other) const { | |
39 return this->delegate == other.delegate && 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<MediaSessionDelegate*>()( | |
45 player_identifier.delegate); | |
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(MediaSessionDelegate* delegate, | |
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(delegate, player_id)); | |
80 return true; | |
81 } | |
82 | |
83 // The session should be reset if a player is starting while all players are | |
84 // suspended. | |
85 if (!has_audio_focus_) | |
86 players_.clear(); | |
87 | |
88 JNIEnv* env = base::android::AttachCurrentThread(); | |
89 DCHECK(env); | |
90 has_audio_focus_ = Java_MediaSession_requestAudioFocus( | |
91 env, j_media_session_.obj(), type == Type::Transient); | |
92 audio_focus_type_ = type; | |
93 | |
94 if (has_audio_focus_) | |
95 players_.insert(PlayerIdentifier(delegate, player_id)); | |
96 | |
97 return has_audio_focus_; | |
98 } | |
99 | |
100 void MediaSession::RemovePlayer(MediaSessionDelegate* delegate, | |
101 int player_id) { | |
102 auto it = players_.find(PlayerIdentifier(delegate, player_id)); | |
103 if (it != players_.end()) | |
104 players_.erase(it); | |
105 | |
106 AbandonSystemAudioFocusIfNeeded(); | |
107 } | |
108 | |
109 void MediaSession::RemovePlayers(MediaSessionDelegate* delegate) { | |
110 for (auto it = players_.begin(); it != players_.end(); ) { | |
111 if (it->delegate == delegate) | |
112 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.
| |
113 else | |
114 ++it; | |
115 } | |
116 | |
117 AbandonSystemAudioFocusIfNeeded(); | |
118 } | |
119 | |
120 void MediaSession::AbandonSystemAudioFocusIfNeeded() { | |
121 if (!has_audio_focus_ || !players_.empty()) | |
122 return; | |
123 | |
124 JNIEnv* env = base::android::AttachCurrentThread(); | |
125 DCHECK(env); | |
126 Java_MediaSession_abandonAudioFocus(env, j_media_session_.obj()); | |
127 has_audio_focus_ = false; | |
128 } | |
129 | |
130 void MediaSession::OnSuspend(JNIEnv* env, jobject obj) { | |
131 has_audio_focus_ = false; | |
132 | |
133 for (const auto& it : players_) | |
134 it.delegate->OnSuspend(it.player_id); | |
135 } | |
136 | |
137 void MediaSession::OnResume(JNIEnv* env, jobject obj) { | |
138 has_audio_focus_ = true; | |
139 | |
140 for (const auto& it : players_) | |
141 it.delegate->OnResume(it.player_id); | |
142 } | |
143 | |
144 } // namespace content | |
OLD | NEW |