OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/renderer_host/media/audio_mirroring_manager.h" |
| 6 |
| 7 #include "base/memory/singleton.h" |
| 8 #include "content/public/browser/browser_thread.h" |
| 9 |
| 10 namespace content { |
| 11 |
| 12 AudioMirroringManager::AudioMirroringManager() {} |
| 13 |
| 14 AudioMirroringManager::~AudioMirroringManager() { |
| 15 DCHECK(diverters_.empty()); |
| 16 DCHECK(sessions_.empty()); |
| 17 } |
| 18 |
| 19 // static |
| 20 AudioMirroringManager* AudioMirroringManager::GetInstance() { |
| 21 return Singleton<AudioMirroringManager, |
| 22 StaticMemorySingletonTraits<AudioMirroringManager> >::get(); |
| 23 } |
| 24 |
| 25 void AudioMirroringManager::AddDiverter( |
| 26 int render_process_id, int render_view_id, Diverter* diverter) { |
| 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 28 DCHECK(diverter); |
| 29 |
| 30 // DCHECK(diverter not already in diverters_ under any key) |
| 31 #ifndef NDEBUG |
| 32 for (DiverterMap::const_iterator it = diverters_.begin(); |
| 33 it != diverters_.end(); ++it) { |
| 34 DCHECK_NE(diverter, it->second); |
| 35 } |
| 36 #endif |
| 37 |
| 38 // Add the diverter to the set of active diverters. |
| 39 const Target target(render_process_id, render_view_id); |
| 40 diverters_.insert(std::make_pair(target, diverter)); |
| 41 |
| 42 // If a mirroring session is active, start diverting the audio stream |
| 43 // immediately. |
| 44 SessionMap::iterator session_it = sessions_.find(target); |
| 45 if (session_it != sessions_.end()) { |
| 46 diverter->StartDiverting( |
| 47 session_it->second->AddInput(diverter->GetAudioParameters())); |
| 48 } |
| 49 } |
| 50 |
| 51 void AudioMirroringManager::RemoveDiverter( |
| 52 int render_process_id, int render_view_id, Diverter* diverter) { |
| 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 54 |
| 55 // Stop diverting the audio stream if a mirroring session is active. |
| 56 const Target target(render_process_id, render_view_id); |
| 57 SessionMap::iterator session_it = sessions_.find(target); |
| 58 if (session_it != sessions_.end()) |
| 59 diverter->StopDiverting(); |
| 60 |
| 61 // Remove the diverter from the set of active diverters. |
| 62 for (DiverterMap::iterator it = diverters_.lower_bound(target); |
| 63 it != diverters_.end() && it->first == target; ++it) { |
| 64 if (it->second == diverter) { |
| 65 diverters_.erase(it); |
| 66 break; |
| 67 } |
| 68 } |
| 69 } |
| 70 |
| 71 void AudioMirroringManager::StartMirroring( |
| 72 int render_process_id, int render_view_id, |
| 73 MirroringDestination* destination) { |
| 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 75 DCHECK(destination); |
| 76 |
| 77 // Insert an entry into the set of active mirroring sessions. If a mirroring |
| 78 // session is already active for |render_process_id| + |render_view_id|, |
| 79 // replace the entry. |
| 80 const Target target(render_process_id, render_view_id); |
| 81 SessionMap::iterator session_it = sessions_.find(target); |
| 82 MirroringDestination* old_destination; |
| 83 if (session_it == sessions_.end()) { |
| 84 old_destination = NULL; |
| 85 sessions_.insert(std::make_pair(target, destination)); |
| 86 |
| 87 DVLOG(1) << "Start mirroring render_process_id:render_view_id=" |
| 88 << render_process_id << ':' << render_view_id |
| 89 << " --> MirroringDestination@" << destination; |
| 90 } else { |
| 91 old_destination = session_it->second; |
| 92 session_it->second = destination; |
| 93 |
| 94 DVLOG(1) << "Switch mirroring of render_process_id:render_view_id=" |
| 95 << render_process_id << ':' << render_view_id |
| 96 << " MirroringDestination@" << old_destination |
| 97 << " --> MirroringDestination@" << destination; |
| 98 } |
| 99 |
| 100 // Divert audio streams coming from |target| to |destination|. If streams |
| 101 // were already diverted to the |old_destination|, remove them. |
| 102 for (DiverterMap::iterator it = diverters_.lower_bound(target); |
| 103 it != diverters_.end() && it->first == target; ++it) { |
| 104 Diverter* const diverter = it->second; |
| 105 if (old_destination) |
| 106 diverter->StopDiverting(); |
| 107 diverter->StartDiverting( |
| 108 destination->AddInput(diverter->GetAudioParameters())); |
| 109 } |
| 110 } |
| 111 |
| 112 void AudioMirroringManager::StopMirroring( |
| 113 int render_process_id, int render_view_id, |
| 114 MirroringDestination* destination) { |
| 115 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 116 |
| 117 // Stop mirroring if there is an active session *and* the destination |
| 118 // matches. |
| 119 const Target target(render_process_id, render_view_id); |
| 120 SessionMap::iterator session_it = sessions_.find(target); |
| 121 if (session_it == sessions_.end() || destination != session_it->second) |
| 122 return; |
| 123 |
| 124 DVLOG(1) << "Stop mirroring render_process_id:render_view_id=" |
| 125 << render_process_id << ':' << render_view_id |
| 126 << " --> MirroringDestination@" << destination; |
| 127 |
| 128 // Stop diverting each audio stream in the mirroring session being stopped. |
| 129 for (DiverterMap::iterator it = diverters_.lower_bound(target); |
| 130 it != diverters_.end() && it->first == target; ++it) { |
| 131 it->second->StopDiverting(); |
| 132 } |
| 133 |
| 134 // Remove the entry from the set of active mirroring sessions. |
| 135 sessions_.erase(session_it); |
| 136 } |
| 137 |
| 138 AudioMirroringManager::Diverter::~Diverter() {} |
| 139 |
| 140 AudioMirroringManager::MirroringDestination::~MirroringDestination() {} |
| 141 |
| 142 } // namespace content |
OLD | NEW |