| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/media/android/media_session_controller.h" |
| 6 |
| 7 #include "content/browser/media/media_web_contents_observer.h" |
| 8 #include "content/common/media/media_player_delegate_messages.h" |
| 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/render_frame_host.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 MediaSessionController::MediaSessionController( |
| 15 const WebContentsObserver::MediaPlayerId& id, |
| 16 MediaWebContentsObserver* media_web_contents_observer) |
| 17 : id_(id), |
| 18 media_web_contents_observer_(media_web_contents_observer), |
| 19 media_session_( |
| 20 MediaSession::Get(media_web_contents_observer_->web_contents())) {} |
| 21 |
| 22 MediaSessionController::~MediaSessionController() { |
| 23 if (!has_session_) |
| 24 return; |
| 25 media_session_->RemovePlayer(this, player_id_); |
| 26 } |
| 27 |
| 28 bool MediaSessionController::Initialize(bool has_audio, |
| 29 bool is_remote, |
| 30 base::TimeDelta duration) { |
| 31 // Don't generate a new id if one has already been set. |
| 32 if (!has_session_) { |
| 33 // These objects are only created on the UI thread, so this is safe. |
| 34 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 35 static uint32_t player_id = 0; |
| 36 player_id_ = static_cast<int>(player_id++); |
| 37 } |
| 38 |
| 39 // Don't bother with a MediaSession for remote players or without audio. If |
| 40 // we already have a session from a previous call, release it. |
| 41 if (!has_audio || is_remote) { |
| 42 if (has_session_) { |
| 43 has_session_ = false; |
| 44 media_session_->RemovePlayer(this, player_id_); |
| 45 } |
| 46 return true; |
| 47 } |
| 48 |
| 49 const MediaSession::Type media_session_type = |
| 50 duration == base::TimeDelta() || |
| 51 duration > |
| 52 base::TimeDelta::FromSeconds(kMinimumDurationForContentSecs) |
| 53 ? MediaSession::Type::Content |
| 54 : MediaSession::Type::Transient; |
| 55 |
| 56 // If we were previously initialized and a different session type has been |
| 57 // requested, release the existing session. |
| 58 if (has_session_ && media_session_type != session_type_) { |
| 59 has_session_ = false; |
| 60 media_session_->RemovePlayer(this, player_id_); |
| 61 } |
| 62 |
| 63 // If a session can't be created, force a pause immediately. |
| 64 if (!media_session_->AddPlayer(this, player_id_, media_session_type)) { |
| 65 OnSuspend(player_id_); |
| 66 return false; |
| 67 } |
| 68 |
| 69 has_session_ = true; |
| 70 return true; |
| 71 } |
| 72 |
| 73 void MediaSessionController::OnSuspend(int player_id) { |
| 74 DCHECK_EQ(player_id_, player_id); |
| 75 media_web_contents_observer_->Send( |
| 76 new MediaPlayerDelegateMsg_Pause(id_.first->GetRoutingID(), id_.second)); |
| 77 } |
| 78 |
| 79 void MediaSessionController::OnResume(int player_id) { |
| 80 DCHECK_EQ(player_id_, player_id); |
| 81 media_web_contents_observer_->Send( |
| 82 new MediaPlayerDelegateMsg_Play(id_.first->GetRoutingID(), id_.second)); |
| 83 } |
| 84 |
| 85 void MediaSessionController::OnSetVolumeMultiplier(int player_id, |
| 86 double volume_multiplier) { |
| 87 DCHECK_EQ(player_id_, player_id); |
| 88 media_web_contents_observer_->Send( |
| 89 new MediaPlayerDelegateMsg_UpdateVolumeMultiplier( |
| 90 id_.first->GetRoutingID(), id_.second, volume_multiplier)); |
| 91 } |
| 92 |
| 93 void MediaSessionController::OnPlaybackPaused() { |
| 94 // We check for suspension here since the renderer may issue its own pause |
| 95 // in response to or while a pause from the browser is in flight. |
| 96 if (!media_session_->IsSuspended()) |
| 97 media_session_->OnPlayerPaused(this, player_id_); |
| 98 } |
| 99 |
| 100 } // namespace content |
| OLD | NEW |