This source file includes following definitions.
- webaudio_source_
- Capture
- OnSetFormat
- SetAudioProcessor
- AddSink
- RemoveSink
- AddSink
- RemoveSink
- Start
- Stop
#include "content/renderer/media/webrtc_local_audio_track.h"
#include "content/public/renderer/media_stream_audio_sink.h"
#include "content/renderer/media/media_stream_audio_level_calculator.h"
#include "content/renderer/media/media_stream_audio_processor.h"
#include "content/renderer/media/media_stream_audio_sink_owner.h"
#include "content/renderer/media/media_stream_audio_track_sink.h"
#include "content/renderer/media/peer_connection_audio_sink_owner.h"
#include "content/renderer/media/webaudio_capturer_source.h"
#include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
#include "content/renderer/media/webrtc_audio_capturer.h"
namespace content {
WebRtcLocalAudioTrack::WebRtcLocalAudioTrack(
WebRtcLocalAudioTrackAdapter* adapter,
const scoped_refptr<WebRtcAudioCapturer>& capturer,
WebAudioCapturerSource* webaudio_source)
: MediaStreamTrack(adapter, true),
adapter_(adapter),
capturer_(capturer),
webaudio_source_(webaudio_source) {
DCHECK(capturer.get() || webaudio_source);
adapter_->Initialize(this);
DVLOG(1) << "WebRtcLocalAudioTrack::WebRtcLocalAudioTrack()";
}
WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack() {
DCHECK(main_render_thread_checker_.CalledOnValidThread());
DVLOG(1) << "WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack()";
Stop();
}
void WebRtcLocalAudioTrack::Capture(const int16* audio_data,
base::TimeDelta delay,
int volume,
bool key_pressed,
bool need_audio_processing) {
DCHECK(capture_thread_checker_.CalledOnValidThread());
int signal_level = level_calculator_->Calculate(
audio_data, audio_parameters_.channels(),
audio_parameters_.frames_per_buffer());
adapter_->SetSignalLevel(signal_level);
scoped_refptr<WebRtcAudioCapturer> capturer;
SinkList::ItemList sinks;
SinkList::ItemList sinks_to_notify_format;
{
base::AutoLock auto_lock(lock_);
capturer = capturer_;
sinks = sinks_.Items();
sinks_.RetrieveAndClearTags(&sinks_to_notify_format);
}
for (SinkList::ItemList::const_iterator it = sinks_to_notify_format.begin();
it != sinks_to_notify_format.end(); ++it) {
(*it)->OnSetFormat(audio_parameters_);
}
std::vector<int> voe_channels = adapter_->VoeChannels();
for (SinkList::ItemList::const_iterator it = sinks.begin();
it != sinks.end();
++it) {
int new_volume = (*it)->OnData(audio_data,
audio_parameters_.sample_rate(),
audio_parameters_.channels(),
audio_parameters_.frames_per_buffer(),
voe_channels,
delay.InMilliseconds(),
volume,
need_audio_processing,
key_pressed);
if (new_volume != 0 && capturer.get() && !webaudio_source_) {
capturer->SetVolume(new_volume);
}
}
}
void WebRtcLocalAudioTrack::OnSetFormat(
const media::AudioParameters& params) {
DVLOG(1) << "WebRtcLocalAudioTrack::OnSetFormat()";
capture_thread_checker_.DetachFromThread();
DCHECK(capture_thread_checker_.CalledOnValidThread());
audio_parameters_ = params;
level_calculator_.reset(new MediaStreamAudioLevelCalculator());
base::AutoLock auto_lock(lock_);
sinks_.TagAll();
}
void WebRtcLocalAudioTrack::SetAudioProcessor(
const scoped_refptr<MediaStreamAudioProcessor>& processor) {
adapter_->SetAudioProcessor(processor->has_audio_processing() ?
processor : NULL);
}
void WebRtcLocalAudioTrack::AddSink(MediaStreamAudioSink* sink) {
DCHECK(main_render_thread_checker_.CalledOnValidThread());
DVLOG(1) << "WebRtcLocalAudioTrack::AddSink()";
base::AutoLock auto_lock(lock_);
DCHECK(!sinks_.Contains(
MediaStreamAudioTrackSink::WrapsMediaStreamSink(sink)));
scoped_refptr<MediaStreamAudioTrackSink> sink_owner(
new MediaStreamAudioSinkOwner(sink));
sinks_.AddAndTag(sink_owner);
}
void WebRtcLocalAudioTrack::RemoveSink(MediaStreamAudioSink* sink) {
DCHECK(main_render_thread_checker_.CalledOnValidThread());
DVLOG(1) << "WebRtcLocalAudioTrack::RemoveSink()";
base::AutoLock auto_lock(lock_);
scoped_refptr<MediaStreamAudioTrackSink> removed_item = sinks_.Remove(
MediaStreamAudioTrackSink::WrapsMediaStreamSink(sink));
if (removed_item.get())
removed_item->Reset();
}
void WebRtcLocalAudioTrack::AddSink(PeerConnectionAudioSink* sink) {
DCHECK(main_render_thread_checker_.CalledOnValidThread());
DVLOG(1) << "WebRtcLocalAudioTrack::AddSink()";
base::AutoLock auto_lock(lock_);
DCHECK(!sinks_.Contains(
MediaStreamAudioTrackSink::WrapsPeerConnectionSink(sink)));
scoped_refptr<MediaStreamAudioTrackSink> sink_owner(
new PeerConnectionAudioSinkOwner(sink));
sinks_.AddAndTag(sink_owner);
}
void WebRtcLocalAudioTrack::RemoveSink(PeerConnectionAudioSink* sink) {
DCHECK(main_render_thread_checker_.CalledOnValidThread());
DVLOG(1) << "WebRtcLocalAudioTrack::RemoveSink()";
base::AutoLock auto_lock(lock_);
scoped_refptr<MediaStreamAudioTrackSink> removed_item = sinks_.Remove(
MediaStreamAudioTrackSink::WrapsPeerConnectionSink(sink));
if (removed_item.get())
removed_item->Reset();
}
void WebRtcLocalAudioTrack::Start() {
DCHECK(main_render_thread_checker_.CalledOnValidThread());
DVLOG(1) << "WebRtcLocalAudioTrack::Start()";
if (webaudio_source_.get()) {
webaudio_source_->Start(this, capturer_.get());
} else if (capturer_.get()) {
capturer_->AddTrack(this);
}
SinkList::ItemList sinks;
{
base::AutoLock auto_lock(lock_);
sinks = sinks_.Items();
}
for (SinkList::ItemList::const_iterator it = sinks.begin();
it != sinks.end();
++it) {
(*it)->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateLive);
}
}
void WebRtcLocalAudioTrack::Stop() {
DCHECK(main_render_thread_checker_.CalledOnValidThread());
DVLOG(1) << "WebRtcLocalAudioTrack::Stop()";
if (!capturer_.get() && !webaudio_source_.get())
return;
if (webaudio_source_.get()) {
webaudio_source_->Stop();
} else {
capturer_->RemoveTrack(this);
}
SinkList::ItemList sinks;
{
base::AutoLock auto_lock(lock_);
sinks = sinks_.Items();
sinks_.Clear();
webaudio_source_ = NULL;
capturer_ = NULL;
}
for (SinkList::ItemList::const_iterator it = sinks.begin();
it != sinks.end();
++it){
(*it)->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateEnded);
(*it)->Reset();
}
}
}