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..449a6d2ef8915f80cf11b39d307381abc0988697 |
| --- /dev/null |
| +++ b/content/browser/media/android/media_session_controller.cc |
| @@ -0,0 +1,112 @@ |
| +// 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/media_web_contents_observer.h" |
| +#include "content/common/media/media_player_delegate_messages.h" |
| +#include "content/public/browser/browser_thread.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), |
| + media_session_( |
| + MediaSession::Get(media_web_contents_observer_->web_contents())) {} |
| + |
| +MediaSessionController::~MediaSessionController() { |
| + if (!has_session_) |
| + return; |
| + media_session_->RemovePlayer(this, player_id_); |
| +} |
| + |
| +bool MediaSessionController::Initialize(bool has_audio, |
| + bool is_remote, |
| + base::TimeDelta duration) { |
| + // Don't generate a new id if one has already been set. |
| + if (!has_session_) { |
| + // These objects are only created on the UI thread, so this is safe. |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + static uint32_t player_id = 0; |
| + player_id_ = static_cast<int>(player_id++); |
| + } else { |
| + // |has_audio| is sticky to workaround inconsistent audio presence signals |
| + // between WebMediaPlayerAndroid and MediaPlayerManager. |
| + // TODO(dalecurtis): Delete sticky audio once we're no longer using WMPA and |
| + // the BrowserMediaPlayerManagers. Tracked by http://crbug.com/580626 |
|
mlamouri (slow - plz ping)
2016/01/26 21:02:50
Maybe this comment needs some more explanations. S
DaleCurtis
2016/01/27 02:25:13
Reworded. Let me know if it needs more work.
|
| + has_audio = has_session_; |
| + } |
| + |
| + // Don't bother with a MediaSession for remote players or without audio. If |
| + // we already have a session from a previous call, release it. |
| + if (!has_audio || is_remote) { |
| + if (has_session_) { |
| + has_session_ = false; |
| + media_session_->RemovePlayer(this, player_id_); |
| + } |
| + return true; |
| + } |
| + |
| + const MediaSession::Type media_session_type = |
| + duration == base::TimeDelta() || |
| + duration > |
| + base::TimeDelta::FromSeconds(kMinimumDurationForContentSecs) |
| + ? MediaSession::Type::Content |
| + : MediaSession::Type::Transient; |
| + |
| + if (has_session_) { |
| + // If we were previously initialized and a different session type has been |
| + // requested, release the existing session. |
| + if (media_session_type != session_type_) { |
| + has_session_ = false; |
| + media_session_->RemovePlayer(this, player_id_); |
| + } else { |
| + // We already have a session of the correct type, so do nothing more. |
|
mlamouri (slow - plz ping)
2016/01/26 21:02:50
I think this is might lead to bugs. `session_type_
DaleCurtis
2016/01/26 21:23:33
I think we want to be careful about having duplica
DaleCurtis
2016/01/26 21:35:08
Sorted out offline. MediaSession is a stack type d
DaleCurtis
2016/01/27 02:25:13
Done.
|
| + return true; |
| + } |
| + } |
| + |
| + // If a session can't be created, force a pause immediately. |
| + if (!media_session_->AddPlayer(this, player_id_, media_session_type)) { |
| + OnSuspend(player_id_); |
| + return false; |
| + } |
| + |
| + session_type_ = media_session_type; |
| + has_session_ = true; |
| + return true; |
| +} |
| + |
| +void MediaSessionController::OnSuspend(int player_id) { |
| + DCHECK_EQ(player_id_, player_id); |
| + media_web_contents_observer_->Send( |
| + new MediaPlayerDelegateMsg_Pause(id_.first->GetRoutingID(), id_.second)); |
| +} |
| + |
| +void MediaSessionController::OnResume(int player_id) { |
| + DCHECK_EQ(player_id_, player_id); |
| + media_web_contents_observer_->Send( |
| + new MediaPlayerDelegateMsg_Play(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 MediaPlayerDelegateMsg_UpdateVolumeMultiplier( |
| + id_.first->GetRoutingID(), id_.second, volume_multiplier)); |
| +} |
| + |
| +void MediaSessionController::OnPlaybackPaused() { |
| + // 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 (!media_session_->IsSuspended()) |
| + media_session_->OnPlayerPaused(this, player_id_); |
| +} |
| + |
| +} // namespace content |