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/android/media_session.h" |
| 8 #include "content/browser/media/media_web_contents_observer.h" |
| 9 #include "content/common/media/media_player_delegate_messages.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/render_frame_host.h" |
| 12 |
| 13 namespace content { |
| 14 |
| 15 MediaSessionController::MediaSessionController( |
| 16 const WebContentsObserver::MediaPlayerId& id, |
| 17 MediaWebContentsObserver* media_web_contents_observer) |
| 18 : id_(id), |
| 19 media_web_contents_observer_(media_web_contents_observer), |
| 20 media_session_( |
| 21 MediaSession::Get(media_web_contents_observer_->web_contents())) {} |
| 22 |
| 23 MediaSessionController::~MediaSessionController() { |
| 24 if (!has_session_) |
| 25 return; |
| 26 media_session_->RemovePlayer(this, player_id_); |
| 27 } |
| 28 |
| 29 bool MediaSessionController::Initialize(bool has_audio, |
| 30 bool is_remote, |
| 31 base::TimeDelta duration) { |
| 32 // Don't generate a new id if one has already been set. |
| 33 if (!has_session_) { |
| 34 // These objects are only created on the UI thread, so this is safe. |
| 35 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 36 static uint32_t player_id = 0; |
| 37 player_id_ = static_cast<int>(player_id++); |
| 38 } else { |
| 39 // WebMediaPlayerAndroid does not have an accurate sense of audio presence, |
| 40 // only the MediaPlayerManager does, so WMPA never reports audio unless it's |
| 41 // sure (no video stream). This leads to issues when Initialize() is called |
| 42 // by WMPA (reporting no audio and subsequently releasing the session) after |
| 43 // the manager accurately reported audio. |
| 44 // |
| 45 // To workaround this, |has_audio| is sticky; I.e., once a session has been |
| 46 // created with audio all future sessions will also have audio. |
| 47 // |
| 48 // TODO(dalecurtis): Delete sticky audio once we're no longer using WMPA and |
| 49 // the BrowserMediaPlayerManagers. Tracked by http://crbug.com/580626 |
| 50 has_audio = true; |
| 51 } |
| 52 |
| 53 // Don't bother with a MediaSession for remote players or without audio. If |
| 54 // we already have a session from a previous call, release it. |
| 55 if (!has_audio || is_remote) { |
| 56 if (has_session_) { |
| 57 has_session_ = false; |
| 58 media_session_->RemovePlayer(this, player_id_); |
| 59 } |
| 60 return true; |
| 61 } |
| 62 |
| 63 const MediaSession::Type media_session_type = |
| 64 duration == base::TimeDelta() || |
| 65 duration > |
| 66 base::TimeDelta::FromSeconds(kMinimumDurationForContentSecs) |
| 67 ? MediaSession::Type::Content |
| 68 : MediaSession::Type::Transient; |
| 69 |
| 70 // If a session can't be created, force a pause immediately. Attempt to add a |
| 71 // session even if we already have one. MediaSession expects AddPlayer() to |
| 72 // be called after OnPlaybackPaused() to reactivate the session. |
| 73 if (!media_session_->AddPlayer(this, player_id_, media_session_type)) { |
| 74 OnSuspend(player_id_); |
| 75 return false; |
| 76 } |
| 77 |
| 78 has_session_ = true; |
| 79 return true; |
| 80 } |
| 81 |
| 82 void MediaSessionController::OnSuspend(int player_id) { |
| 83 DCHECK_EQ(player_id_, player_id); |
| 84 media_web_contents_observer_->Send( |
| 85 new MediaPlayerDelegateMsg_Pause(id_.first->GetRoutingID(), id_.second)); |
| 86 } |
| 87 |
| 88 void MediaSessionController::OnResume(int player_id) { |
| 89 DCHECK_EQ(player_id_, player_id); |
| 90 media_web_contents_observer_->Send( |
| 91 new MediaPlayerDelegateMsg_Play(id_.first->GetRoutingID(), id_.second)); |
| 92 } |
| 93 |
| 94 void MediaSessionController::OnSetVolumeMultiplier(int player_id, |
| 95 double volume_multiplier) { |
| 96 DCHECK_EQ(player_id_, player_id); |
| 97 media_web_contents_observer_->Send( |
| 98 new MediaPlayerDelegateMsg_UpdateVolumeMultiplier( |
| 99 id_.first->GetRoutingID(), id_.second, volume_multiplier)); |
| 100 } |
| 101 |
| 102 void MediaSessionController::OnPlaybackPaused() { |
| 103 // We check for suspension here since the renderer may issue its own pause |
| 104 // in response to or while a pause from the browser is in flight. |
| 105 if (!media_session_->IsSuspended()) |
| 106 media_session_->OnPlayerPaused(this, player_id_); |
| 107 } |
| 108 |
| 109 } // namespace content |
OLD | NEW |