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/session/media_session_controllers_manager.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "content/browser/media/session/media_session.h" |
| 9 #include "content/browser/media/session/media_session_controller.h" |
| 10 #include "content/browser/media/session/media_session_observer.h" |
| 11 #include "media/base/media_switches.h" |
| 12 |
| 13 namespace content { |
| 14 |
| 15 namespace { |
| 16 |
| 17 bool IsDefaultMediaSessionEnabled() { |
| 18 #if defined(OS_ANDROID) |
| 19 return true; |
| 20 #else |
| 21 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 22 switches::kEnableDefaultMediaSession); |
| 23 #endif |
| 24 } |
| 25 |
| 26 } // anonymous namespace |
| 27 |
| 28 MediaSessionControllersManager::MediaSessionControllersManager( |
| 29 MediaWebContentsObserver* media_web_contents_observer) |
| 30 : media_web_contents_observer_(media_web_contents_observer) { |
| 31 } |
| 32 |
| 33 MediaSessionControllersManager::~MediaSessionControllersManager() = default; |
| 34 |
| 35 void MediaSessionControllersManager::Clear(RenderFrameHost* render_frame_host) { |
| 36 if (!IsDefaultMediaSessionEnabled()) |
| 37 return; |
| 38 |
| 39 for (auto it = controllers_map_.begin(); it != controllers_map_.end();) { |
| 40 if (it->first.first == render_frame_host) |
| 41 it = controllers_map_.erase(it); |
| 42 else |
| 43 ++it; |
| 44 } |
| 45 } |
| 46 |
| 47 bool MediaSessionControllersManager::RequestPlay(const MediaPlayerId& id, |
| 48 bool has_audio, bool is_remote, base::TimeDelta duration) { |
| 49 if (!IsDefaultMediaSessionEnabled()) |
| 50 return true; |
| 51 |
| 52 // Since we don't remove session instances on pause, there may be an existing |
| 53 // instance for this playback attempt. |
| 54 // |
| 55 // In this case, try to reinitialize it with the new settings. If they are |
| 56 // the same, this is a no-op. If the reinitialize fails, destroy the |
| 57 // controller. A later playback attempt will create a new controller. |
| 58 auto it = controllers_map_.find(id); |
| 59 if (it != controllers_map_.end()) { |
| 60 if (!it->second->Initialize(has_audio, is_remote, duration)) |
| 61 controllers_map_.erase(it); |
| 62 return false; |
| 63 } |
| 64 |
| 65 scoped_ptr<MediaSessionController> controller( |
| 66 new MediaSessionController(id, media_web_contents_observer_)); |
| 67 |
| 68 if (!controller->Initialize(has_audio, is_remote, duration)) |
| 69 return false; |
| 70 |
| 71 controllers_map_[id] = std::move(controller); |
| 72 return true; |
| 73 } |
| 74 |
| 75 void MediaSessionControllersManager::OnPause(const MediaPlayerId& id) { |
| 76 if (!IsDefaultMediaSessionEnabled()) |
| 77 return; |
| 78 |
| 79 auto it = controllers_map_.find(id); |
| 80 if (it == controllers_map_.end()) |
| 81 return; |
| 82 |
| 83 it->second->OnPlaybackPaused(); |
| 84 } |
| 85 |
| 86 void MediaSessionControllersManager::OnEnd(const MediaPlayerId& id) { |
| 87 if (!IsDefaultMediaSessionEnabled()) |
| 88 return; |
| 89 controllers_map_.erase(id); |
| 90 } |
| 91 |
| 92 } // namespace content |
OLD | NEW |