This source file includes following definitions.
- AddDiverter
- RemoveDiverter
- StartMirroring
- StopMirroring
#include "content/browser/media/capture/audio_mirroring_manager.h"
#include "content/public/browser/browser_thread.h"
namespace content {
namespace {
class ReentrancyGuard {
public:
#ifdef NDEBUG
ReentrancyGuard() {}
~ReentrancyGuard() {}
#else
ReentrancyGuard() {
DCHECK(!inside_a_method_);
inside_a_method_ = true;
}
~ReentrancyGuard() {
inside_a_method_ = false;
}
static bool inside_a_method_;
#endif
};
#ifndef NDEBUG
bool ReentrancyGuard::inside_a_method_ = false;
#endif
}
AudioMirroringManager::AudioMirroringManager() {}
AudioMirroringManager::~AudioMirroringManager() {
DCHECK(diverters_.empty());
DCHECK(sessions_.empty());
}
void AudioMirroringManager::AddDiverter(
int render_process_id, int render_view_id, Diverter* diverter) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
ReentrancyGuard guard;
DCHECK(diverter);
#ifndef NDEBUG
for (DiverterMap::const_iterator it = diverters_.begin();
it != diverters_.end(); ++it) {
DCHECK_NE(diverter, it->second);
}
#endif
const Target target(render_process_id, render_view_id);
diverters_.insert(std::make_pair(target, diverter));
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));
ReentrancyGuard guard;
const Target target(render_process_id, render_view_id);
SessionMap::iterator session_it = sessions_.find(target);
if (session_it != sessions_.end())
diverter->StopDiverting();
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));
ReentrancyGuard guard;
DCHECK(destination);
const Target target(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;
}
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));
ReentrancyGuard guard;
const Target target(render_process_id, render_view_id);
SessionMap::iterator session_it = sessions_.find(target);
if (session_it == sessions_.end() || destination != session_it->second)
return;
DVLOG(1) << "Stop mirroring render_process_id:render_view_id="
<< render_process_id << ':' << render_view_id
<< " --> MirroringDestination@" << destination;
for (DiverterMap::iterator it = diverters_.lower_bound(target);
it != diverters_.end() && it->first == target; ++it) {
it->second->StopDiverting();
}
sessions_.erase(session_it);
}
}