Chromium Code Reviews| Index: content/browser/renderer_host/media/audio_mirroring_manager.cc |
| diff --git a/content/browser/renderer_host/media/audio_mirroring_manager.cc b/content/browser/renderer_host/media/audio_mirroring_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..41753bd678d783c13d84fcf9364455a28562f186 |
| --- /dev/null |
| +++ b/content/browser/renderer_host/media/audio_mirroring_manager.cc |
| @@ -0,0 +1,154 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/renderer_host/media/audio_mirroring_manager.h" |
| + |
| +#include "base/memory/singleton.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace content { |
| + |
| +AudioMirroringManager::AudioMirroringManager() {} |
| + |
| +AudioMirroringManager::~AudioMirroringManager() { |
| + DCHECK(diverters_.empty()); |
| + DCHECK(sessions_.empty()); |
| +} |
| + |
| +AudioMirroringManager* AudioMirroringManager::test_instance_ = NULL; |
| + |
| +// static |
| +void AudioMirroringManager::SetInstanceForTesting( |
| + AudioMirroringManager* test_instance) { |
| + test_instance_ = test_instance; |
| +} |
| + |
| +// static |
| +AudioMirroringManager* AudioMirroringManager::GetInstance() { |
| + typedef Singleton<AudioMirroringManager, |
| + StaticMemorySingletonTraits<AudioMirroringManager> > |
| + SingletonAMM; |
| + return test_instance_ ? test_instance_ : SingletonAMM::get(); |
| +} |
| + |
| +void AudioMirroringManager::AddDiverter( |
| + int render_process_id, int render_view_id, Diverter* diverter) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + DCHECK(diverter); |
| + |
| + // DCHECK(diverter not already in diverters_ under any key) |
| +#ifndef NDEBUG |
| + for (DiverterMap::const_iterator it = diverters_.begin(); |
| + it != diverters_.end(); ++it) { |
| + DCHECK_NE(diverter, it->second); |
| + } |
| +#endif |
| + |
| + // Add the diverter to the set of active diverters. |
| + const Target target = std::make_pair(render_process_id, render_view_id); |
| + diverters_.insert(std::make_pair(target, diverter)); |
| + |
| + // If a mirroring session is active, start diverting the audio stream |
| + // immediately. |
| + SessionMap::iterator session_it = sessions_.find(target); |
| + if (session_it != sessions_.end()) { |
| + diverter->StartDiverting( |
| + session_it->second->AddInput(diverter->GetAudioParameters())); |
| + } |
| +} |
| + |
| +void AudioMirroringManager::RemoveDiverter( |
| + int render_process_id, int render_view_id, Diverter* diverter) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + // Stop diverting the audio stream if a mirroring session is active. |
| + const Target target = std::make_pair(render_process_id, render_view_id); |
| + SessionMap::iterator session_it = sessions_.find(target); |
| + if (session_it != sessions_.end()) { |
| + diverter->StopDiverting(); |
| + } |
| + |
| + // Remove the diverter from the set of active diverters. |
| + for (DiverterMap::iterator it = diverters_.lower_bound(target); |
| + it != diverters_.end() && it->first == target; ++it) { |
| + if (it->second == diverter) { |
| + diverters_.erase(it); |
| + break; |
| + } |
| + } |
| +} |
| + |
| +void AudioMirroringManager::StartMirroring( |
| + int render_process_id, int render_view_id, |
| + MirroringDestination* destination) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + DCHECK(destination); |
| + |
| + // Insert an entry into the set of active mirroring sessions. If a mirroring |
| + // session is already active for |render_process_id| + |render_view_id|, |
| + // replace the entry. |
| + const Target target = std::make_pair(render_process_id, render_view_id); |
| + SessionMap::iterator session_it = sessions_.find(target); |
| + MirroringDestination* old_destination; |
| + if (session_it == sessions_.end()) { |
| + old_destination = NULL; |
| + sessions_.insert(std::make_pair(target, destination)); |
| + |
| + DVLOG(1) << "Start mirroring render_process_id:render_view_id=" |
| + << render_process_id << ':' << render_view_id |
| + << " --> MirroringDestination@" << destination; |
| + } else { |
| + old_destination = session_it->second; |
| + session_it->second = destination; |
| + |
| + DVLOG(1) << "Switch mirroring of render_process_id:render_view_id=" |
| + << render_process_id << ':' << render_view_id |
| + << " MirroringDestination@" << old_destination |
| + << " --> MirroringDestination@" << destination; |
| + } |
| + |
| + // Divert audio streams coming from |target| to |destination|. If streams |
| + // were already diverted to the |old_destination|, remove them. |
| + for (DiverterMap::iterator it = diverters_.lower_bound(target); |
| + it != diverters_.end() && it->first == target; ++it) { |
| + Diverter* const diverter = it->second; |
| + if (old_destination) |
| + diverter->StopDiverting(); |
| + diverter->StartDiverting( |
| + destination->AddInput(diverter->GetAudioParameters())); |
| + } |
| +} |
| + |
| +void AudioMirroringManager::StopMirroring( |
| + int render_process_id, int render_view_id, |
| + MirroringDestination* destination) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + // Stop mirroring if there is an active session *and* the destination |
| + // matches. |
| + const Target target = std::make_pair(render_process_id, render_view_id); |
| + SessionMap::iterator session_it = sessions_.find(target); |
| + 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.
|
| + return; |
| + } |
| + |
| + DVLOG(1) << "Stop mirroring render_process_id:render_view_id=" |
| + << render_process_id << ':' << render_view_id |
| + << " --> MirroringDestination@" << destination; |
| + |
| + // Stop diverting each audio stream in the mirroring session being stopped. |
| + for (DiverterMap::iterator it = diverters_.lower_bound(target); |
| + it != diverters_.end() && it->first == target; ++it) { |
| + it->second->StopDiverting(); |
| + } |
| + |
| + // Remove the entry from the set of active mirroring sessions. |
| + sessions_.erase(session_it); |
| +} |
| + |
| +AudioMirroringManager::Diverter::~Diverter() {} |
| + |
| +AudioMirroringManager::MirroringDestination::~MirroringDestination() {} |
| + |
| +} // namespace content |