Chromium Code Reviews| 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 } else { | |
| 38 // |has_audio| is sticky to workaround inconsistent audio presence signals | |
| 39 // between WebMediaPlayerAndroid and MediaPlayerManager. | |
| 40 // TODO(dalecurtis): Delete sticky audio once we're no longer using WMPA and | |
| 41 // 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.
| |
| 42 has_audio = has_session_; | |
| 43 } | |
| 44 | |
| 45 // Don't bother with a MediaSession for remote players or without audio. If | |
| 46 // we already have a session from a previous call, release it. | |
| 47 if (!has_audio || is_remote) { | |
| 48 if (has_session_) { | |
| 49 has_session_ = false; | |
| 50 media_session_->RemovePlayer(this, player_id_); | |
| 51 } | |
| 52 return true; | |
| 53 } | |
| 54 | |
| 55 const MediaSession::Type media_session_type = | |
| 56 duration == base::TimeDelta() || | |
| 57 duration > | |
| 58 base::TimeDelta::FromSeconds(kMinimumDurationForContentSecs) | |
| 59 ? MediaSession::Type::Content | |
| 60 : MediaSession::Type::Transient; | |
| 61 | |
| 62 if (has_session_) { | |
| 63 // If we were previously initialized and a different session type has been | |
| 64 // requested, release the existing session. | |
| 65 if (media_session_type != session_type_) { | |
| 66 has_session_ = false; | |
| 67 media_session_->RemovePlayer(this, player_id_); | |
| 68 } else { | |
| 69 // 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.
| |
| 70 return true; | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 // If a session can't be created, force a pause immediately. | |
| 75 if (!media_session_->AddPlayer(this, player_id_, media_session_type)) { | |
| 76 OnSuspend(player_id_); | |
| 77 return false; | |
| 78 } | |
| 79 | |
| 80 session_type_ = media_session_type; | |
| 81 has_session_ = true; | |
| 82 return true; | |
| 83 } | |
| 84 | |
| 85 void MediaSessionController::OnSuspend(int player_id) { | |
| 86 DCHECK_EQ(player_id_, player_id); | |
| 87 media_web_contents_observer_->Send( | |
| 88 new MediaPlayerDelegateMsg_Pause(id_.first->GetRoutingID(), id_.second)); | |
| 89 } | |
| 90 | |
| 91 void MediaSessionController::OnResume(int player_id) { | |
| 92 DCHECK_EQ(player_id_, player_id); | |
| 93 media_web_contents_observer_->Send( | |
| 94 new MediaPlayerDelegateMsg_Play(id_.first->GetRoutingID(), id_.second)); | |
| 95 } | |
| 96 | |
| 97 void MediaSessionController::OnSetVolumeMultiplier(int player_id, | |
| 98 double volume_multiplier) { | |
| 99 DCHECK_EQ(player_id_, player_id); | |
| 100 media_web_contents_observer_->Send( | |
| 101 new MediaPlayerDelegateMsg_UpdateVolumeMultiplier( | |
| 102 id_.first->GetRoutingID(), id_.second, volume_multiplier)); | |
| 103 } | |
| 104 | |
| 105 void MediaSessionController::OnPlaybackPaused() { | |
| 106 // We check for suspension here since the renderer may issue its own pause | |
| 107 // in response to or while a pause from the browser is in flight. | |
| 108 if (!media_session_->IsSuspended()) | |
| 109 media_session_->OnPlayerPaused(this, player_id_); | |
| 110 } | |
| 111 | |
| 112 } // namespace content | |
| OLD | NEW |