Chromium Code Reviews| Index: content/browser/media/android/media_session_controller.cc |
| diff --git a/content/browser/media/android/media_session_controller.cc b/content/browser/media/android/media_session_controller.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a5469227560ead53a66465a2b486210584126fd7 |
| --- /dev/null |
| +++ b/content/browser/media/android/media_session_controller.cc |
| @@ -0,0 +1,86 @@ |
| +// Copyright 2016 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_controller.h" |
| + |
| +#include "content/browser/media/android/media_session.h" |
| +#include "content/common/frame_messages.h" |
| +#include "content/public/browser/render_frame_host.h" |
| + |
| +namespace content { |
| + |
| +MediaSessionController::MediaSessionController( |
| + const WebContentsObserver::MediaPlayerId& id, |
| + MediaWebContentsObserver* media_web_contents_observer) |
| + : id_(id), media_web_contents_observer_(media_web_contents_observer) {} |
| + |
| +// Clients must call this after construction and destroy the controller if it |
| +// returns false. |
| +bool MediaSessionController::Initialize(bool has_audio, |
| + bool is_remote, |
| + base::TimeDelta duration) { |
| + // These objects are only created on the UI thread, so this is safe. |
|
mlamouri (slow - plz ping)
2016/01/22 16:58:31
`DCHECK_CURRENTLY_ON(BrowserThread::UI);` or it's
DaleCurtis
2016/01/23 02:10:59
Done.
|
| + static uint32_t player_id = 0; |
| + player_id_ = static_cast<int>(player_id++); |
| + has_audio_ = has_audio; |
| + |
| + // Don't bother with a MediaSession for remote players or without audio. |
| + if (!has_audio || is_remote) |
| + return true; |
| + |
| + const MediaSession::Type media_session_type = |
| + duration == base::TimeDelta() || |
| + duration > |
| + base::TimeDelta::FromSeconds(kMinimumDurationForContentSecs) |
| + ? MediaSession::Type::Content |
| + : MediaSession::Type::Transient; |
| + |
| + // If a session can't be created, force a pause immediately. |
| + if (!MediaSession::Get(media_web_contents_observer_->web_contents()) |
| + ->AddPlayer(this, player_id_, media_session_type)) { |
| + OnSuspend(player_id_); |
| + return false; |
| + } |
| + |
| + initialized_ = true; |
| + return true; |
| +} |
| + |
| +MediaSessionController::~MediaSessionController() { |
|
mlamouri (slow - plz ping)
2016/01/22 16:58:31
style: keep implementations in the same order as t
DaleCurtis
2016/01/23 02:10:59
Done.
|
| + if (initialized_) { |
|
mlamouri (slow - plz ping)
2016/01/22 16:58:31
nit:
```
if (!initialized_)
return;
// do stuff
DaleCurtis
2016/01/23 02:10:59
Done.
|
| + MediaSession::Get(media_web_contents_observer_->web_contents()) |
| + ->RemovePlayer(this, player_id_); |
| + } |
| +} |
| + |
| +void MediaSessionController::OnSuspend(int player_id) { |
| + DCHECK_EQ(player_id_, player_id); |
| + media_web_contents_observer_->Send( |
| + new FrameMsg_MediaDelegatePause(id_.first->GetRoutingID(), id_.second)); |
| +} |
| + |
| +void MediaSessionController::OnResume(int player_id) { |
| + DCHECK_EQ(player_id_, player_id); |
| + media_web_contents_observer_->Send( |
| + new FrameMsg_MediaDelegatePlay(id_.first->GetRoutingID(), id_.second)); |
| +} |
| + |
| +void MediaSessionController::OnSetVolumeMultiplier(int player_id, |
| + double volume_multiplier) { |
| + DCHECK_EQ(player_id_, player_id); |
| + media_web_contents_observer_->Send( |
| + new FrameMsg_MediaDelegateVolumeMultiplierUpdate( |
| + id_.first->GetRoutingID(), id_.second, volume_multiplier)); |
| +} |
| + |
| +void MediaSessionController::PausePlayback() { |
| + MediaSession* session = |
| + MediaSession::Get(media_web_contents_observer_->web_contents()); |
| + // We check for suspension here since the renderer may issue its own pause |
| + // in response to or while a pause from the browser is in flight. |
| + if (!session->IsSuspended()) |
| + session->OnPlayerPaused(this, player_id_); |
| +} |
| + |
| +} // namespace content |