Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/media/android/media_web_contents_observer_android.h" | 5 #include "content/browser/media/android/media_web_contents_observer_android.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | |
| 7 #include "content/browser/media/android/browser_media_player_manager.h" | 8 #include "content/browser/media/android/browser_media_player_manager.h" |
| 8 #include "content/browser/media/android/browser_media_session_manager.h" | 9 #include "content/browser/media/android/browser_media_session_manager.h" |
| 9 #include "content/browser/media/android/media_session.h" | 10 #include "content/browser/media/android/media_session.h" |
| 10 #include "content/browser/media/android/media_session_observer.h" | 11 #include "content/browser/media/android/media_session_observer.h" |
| 11 #include "content/browser/media/cdm/browser_cdm_manager.h" | 12 #include "content/browser/media/cdm/browser_cdm_manager.h" |
| 12 #include "content/browser/web_contents/web_contents_impl.h" | 13 #include "content/browser/web_contents/web_contents_impl.h" |
| 13 #include "content/common/frame_messages.h" | 14 #include "content/common/frame_messages.h" |
| 14 #include "content/common/media/media_player_messages_android.h" | 15 #include "content/common/media/media_player_messages_android.h" |
| 15 #include "content/common/media/media_session_messages_android.h" | 16 #include "content/common/media/media_session_messages_android.h" |
| 16 #include "content/public/browser/render_frame_host.h" | 17 #include "content/public/browser/render_frame_host.h" |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 42 base::TimeDelta::FromSeconds(5); | 43 base::TimeDelta::FromSeconds(5); |
| 43 | 44 |
| 44 const MediaSession::Type media_session_type = | 45 const MediaSession::Type media_session_type = |
| 45 duration == base::TimeDelta() || duration > kMinimumDurationForContent | 46 duration == base::TimeDelta() || duration > kMinimumDurationForContent |
| 46 ? MediaSession::Type::Content | 47 ? MediaSession::Type::Content |
| 47 : MediaSession::Type::Transient; | 48 : MediaSession::Type::Transient; |
| 48 | 49 |
| 49 // If a session can't be created, force a pause immediately. | 50 // If a session can't be created, force a pause immediately. |
| 50 if (!MediaSession::Get(media_web_contents_observer_->web_contents()) | 51 if (!MediaSession::Get(media_web_contents_observer_->web_contents()) |
| 51 ->AddPlayer(this, player_id_, media_session_type)) { | 52 ->AddPlayer(this, player_id_, media_session_type)) { |
| 52 OnSuspend(player_id_); | 53 OnSuspend(player_id_); |
|
mlamouri (slow - plz ping)
2016/01/19 17:18:17
I guess that would no longer be needed?
| |
| 53 return false; | 54 return false; |
| 54 } | 55 } |
| 55 | 56 |
| 56 initialized_ = true; | 57 initialized_ = true; |
| 57 return true; | 58 return true; |
| 58 } | 59 } |
| 59 | 60 |
| 60 ~MediaSessionController() { | 61 ~MediaSessionController() { |
| 61 if (initialized_) { | 62 if (initialized_) { |
| 62 MediaSession::Get(media_web_contents_observer_->web_contents()) | 63 MediaSession::Get(media_web_contents_observer_->web_contents()) |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 192 | 193 |
| 193 if (MediaWebContentsObserver::OnMessageReceived(msg, render_frame_host)) | 194 if (MediaWebContentsObserver::OnMessageReceived(msg, render_frame_host)) |
| 194 return true; | 195 return true; |
| 195 | 196 |
| 196 if (OnMediaPlayerMessageReceived(msg, render_frame_host)) | 197 if (OnMediaPlayerMessageReceived(msg, render_frame_host)) |
| 197 return true; | 198 return true; |
| 198 | 199 |
| 199 return OnMediaPlayerSetCdmMessageReceived(msg, render_frame_host); | 200 return OnMediaPlayerSetCdmMessageReceived(msg, render_frame_host); |
| 200 } | 201 } |
| 201 | 202 |
| 203 void MediaWebContentsObserverAndroid::CheckFocus( | |
| 204 int render_frame_id, | |
| 205 const base::Closure& on_focus_cb) { | |
| 206 for (const auto& kv : media_session_map_) { | |
| 207 if (kv.first.first->GetRoutingID() == render_frame_id) { | |
| 208 on_focus_cb.Run(); | |
| 209 return; | |
| 210 } | |
| 211 } | |
| 212 | |
| 213 deferred_focus_cbs_[render_frame_id].push_back(on_focus_cb); | |
| 214 } | |
| 215 | |
| 202 void MediaWebContentsObserverAndroid::OnMediaPlayerDelegateMessageReceived( | 216 void MediaWebContentsObserverAndroid::OnMediaPlayerDelegateMessageReceived( |
| 203 const IPC::Message& msg, | 217 const IPC::Message& msg, |
| 204 RenderFrameHost* render_frame_host) { | 218 RenderFrameHost* render_frame_host) { |
| 205 // TODO(dalecurtis): These should no longer be FrameHostMsg. | 219 // TODO(dalecurtis): These should no longer be FrameHostMsg. |
| 206 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MediaWebContentsObserverAndroid, msg, | 220 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MediaWebContentsObserverAndroid, msg, |
| 207 render_frame_host) | 221 render_frame_host) |
| 208 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaDestroyedNotification, | 222 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaDestroyedNotification, |
| 209 OnMediaDestroyedNotification) | 223 OnMediaDestroyedNotification) |
| 210 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaPlayingNotification, | 224 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaPlayingNotification, |
| 211 OnMediaPlayingNotification) | 225 OnMediaPlayingNotification) |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 309 | 323 |
| 310 // TODO(xhwang): This could possibly fail. In that case we should reject the | 324 // TODO(xhwang): This could possibly fail. In that case we should reject the |
| 311 // promise. | 325 // promise. |
| 312 media_player->SetCdm(cdm); | 326 media_player->SetCdm(cdm); |
| 313 } | 327 } |
| 314 | 328 |
| 315 void MediaWebContentsObserverAndroid::OnMediaDestroyedNotification( | 329 void MediaWebContentsObserverAndroid::OnMediaDestroyedNotification( |
| 316 RenderFrameHost* render_frame_host, | 330 RenderFrameHost* render_frame_host, |
| 317 int64_t player_cookie) { | 331 int64_t player_cookie) { |
| 318 media_session_map_.erase(MediaPlayerId(render_frame_host, player_cookie)); | 332 media_session_map_.erase(MediaPlayerId(render_frame_host, player_cookie)); |
| 333 deferred_focus_cbs_.erase(render_frame_host->GetRoutingID()); | |
| 319 } | 334 } |
| 320 | 335 |
| 321 void MediaWebContentsObserverAndroid::OnMediaPlayingNotification( | 336 void MediaWebContentsObserverAndroid::OnMediaPlayingNotification( |
| 322 RenderFrameHost* render_frame_host, | 337 RenderFrameHost* render_frame_host, |
| 323 int64_t player_cookie, | 338 int64_t player_cookie, |
| 324 bool has_video, | 339 bool has_video, |
| 325 bool has_audio, | 340 bool has_audio, |
| 326 bool is_remote, | 341 bool is_remote, |
| 327 base::TimeDelta duration) { | 342 base::TimeDelta duration) { |
| 328 // TODO(dalecurtis): Remove this allowance once RequestPlay() is deleted. | 343 // TODO(dalecurtis): Remove this allowance once RequestPlay() is deleted. |
| 329 const MediaPlayerId id(render_frame_host, player_cookie); | 344 const MediaPlayerId id(render_frame_host, player_cookie); |
| 330 if (media_session_map_.find(id) != media_session_map_.end()) | 345 if (media_session_map_.find(id) != media_session_map_.end()) |
| 331 return; | 346 return; |
| 332 | 347 |
| 333 scoped_ptr<MediaSessionController> controller( | 348 scoped_ptr<MediaSessionController> controller( |
| 334 new MediaSessionController(id, this)); | 349 new MediaSessionController(id, this)); |
| 335 | 350 |
| 336 // If initialize fails, the controller should be destroyed and a new one | 351 // If initialize fails, the controller should be destroyed and a new one |
| 337 // attempted later after another playback attempt occurs. | 352 // attempted later after another playback attempt occurs. |
| 338 if (!controller->Initialize(has_audio, is_remote, duration)) | 353 if (!controller->Initialize(has_audio, is_remote, duration)) |
| 339 return; | 354 return; |
| 340 | 355 |
| 341 media_session_map_[id] = std::move(controller); | 356 media_session_map_[id] = std::move(controller); |
| 357 | |
| 358 // Notify any deferred focus callbacks. | |
| 359 auto it = deferred_focus_cbs_.find(render_frame_host->GetRoutingID()); | |
| 360 if (it != deferred_focus_cbs_.end()) { | |
| 361 for (const auto& cb : it->second) | |
| 362 cb.Run(); | |
| 363 deferred_focus_cbs_.erase(it); | |
| 364 } | |
| 342 } | 365 } |
| 343 | 366 |
| 344 void MediaWebContentsObserverAndroid::OnMediaPausedNotification( | 367 void MediaWebContentsObserverAndroid::OnMediaPausedNotification( |
| 345 RenderFrameHost* render_frame_host, | 368 RenderFrameHost* render_frame_host, |
| 346 int64_t player_cookie, | 369 int64_t player_cookie, |
| 347 bool reached_end_of_stream) { | 370 bool reached_end_of_stream) { |
| 348 // Drop the session if playback completes normally. | 371 // Drop the session if playback completes normally. |
| 349 if (reached_end_of_stream) { | 372 if (reached_end_of_stream) { |
| 350 OnMediaDestroyedNotification(render_frame_host, player_cookie); | 373 OnMediaDestroyedNotification(render_frame_host, player_cookie); |
| 351 return; | 374 return; |
| 352 } | 375 } |
| 353 | 376 |
| 354 auto it = | 377 auto it = |
| 355 media_session_map_.find(MediaPlayerId(render_frame_host, player_cookie)); | 378 media_session_map_.find(MediaPlayerId(render_frame_host, player_cookie)); |
| 356 if (it == media_session_map_.end()) | 379 if (it == media_session_map_.end()) |
| 357 return; | 380 return; |
| 358 | 381 |
| 359 it->second->PausePlayback(); | 382 it->second->PausePlayback(); |
| 360 } | 383 } |
| 361 | 384 |
| 362 } // namespace content | 385 } // namespace content |
| OLD | NEW |