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