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