Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / media / webrtc / webrtc_local_audio_track_adapter.cc
blob14c487859479ebedafebe1d9962a48bd05bafd0c
1 // Copyright 2014 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.
5 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
7 #include "base/logging.h"
8 #include "content/renderer/media/media_stream_audio_processor.h"
9 #include "content/renderer/media/webrtc/peer_connection_dependency_factory.h"
10 #include "content/renderer/media/webrtc/webrtc_audio_sink_adapter.h"
11 #include "content/renderer/media/webrtc_local_audio_track.h"
12 #include "content/renderer/render_thread_impl.h"
13 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
15 namespace content {
17 static const char kAudioTrackKind[] = "audio";
19 scoped_refptr<WebRtcLocalAudioTrackAdapter>
20 WebRtcLocalAudioTrackAdapter::Create(
21 const std::string& label,
22 webrtc::AudioSourceInterface* track_source) {
23 // TODO(tommi): Change this so that the signaling thread is one of the
24 // parameters to this method.
25 scoped_refptr<base::MessageLoopProxy> signaling_thread;
26 RenderThreadImpl* current = RenderThreadImpl::current();
27 if (current) {
28 PeerConnectionDependencyFactory* pc_factory =
29 current->GetPeerConnectionDependencyFactory();
30 signaling_thread = pc_factory->GetWebRtcSignalingThread();
33 LOG_IF(ERROR, !signaling_thread.get()) << "No signaling thread!";
35 rtc::RefCountedObject<WebRtcLocalAudioTrackAdapter>* adapter =
36 new rtc::RefCountedObject<WebRtcLocalAudioTrackAdapter>(
37 label, track_source, signaling_thread);
38 return adapter;
41 WebRtcLocalAudioTrackAdapter::WebRtcLocalAudioTrackAdapter(
42 const std::string& label,
43 webrtc::AudioSourceInterface* track_source,
44 const scoped_refptr<base::SingleThreadTaskRunner>& signaling_thread)
45 : webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>(label),
46 owner_(NULL),
47 track_source_(track_source),
48 signaling_thread_(signaling_thread),
49 signal_level_(0) {
50 signaling_thread_checker_.DetachFromThread();
51 capture_thread_.DetachFromThread();
54 WebRtcLocalAudioTrackAdapter::~WebRtcLocalAudioTrackAdapter() {
57 void WebRtcLocalAudioTrackAdapter::Initialize(WebRtcLocalAudioTrack* owner) {
58 DCHECK(!owner_);
59 DCHECK(owner);
60 owner_ = owner;
63 void WebRtcLocalAudioTrackAdapter::SetAudioProcessor(
64 const scoped_refptr<MediaStreamAudioProcessor>& processor) {
65 // SetAudioProcessor will be called when a new capture thread has been
66 // initialized, so we need to detach from any current capture thread we're
67 // checking and attach to the current one.
68 capture_thread_.DetachFromThread();
69 DCHECK(capture_thread_.CalledOnValidThread());
70 base::AutoLock auto_lock(lock_);
71 audio_processor_ = processor;
74 std::string WebRtcLocalAudioTrackAdapter::kind() const {
75 return kAudioTrackKind;
78 bool WebRtcLocalAudioTrackAdapter::set_enabled(bool enable) {
79 // If we're not called on the signaling thread, we need to post a task to
80 // change the state on the correct thread.
81 if (signaling_thread_.get() && !signaling_thread_->BelongsToCurrentThread()) {
82 signaling_thread_->PostTask(FROM_HERE,
83 base::Bind(
84 base::IgnoreResult(&WebRtcLocalAudioTrackAdapter::set_enabled),
85 this, enable));
86 return true;
89 return webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>::
90 set_enabled(enable);
93 void WebRtcLocalAudioTrackAdapter::AddSink(
94 webrtc::AudioTrackSinkInterface* sink) {
95 DCHECK(signaling_thread_checker_.CalledOnValidThread());
96 DCHECK(sink);
97 #ifndef NDEBUG
98 // Verify that |sink| has not been added.
99 for (ScopedVector<WebRtcAudioSinkAdapter>::const_iterator it =
100 sink_adapters_.begin();
101 it != sink_adapters_.end(); ++it) {
102 DCHECK(!(*it)->IsEqual(sink));
104 #endif
106 scoped_ptr<WebRtcAudioSinkAdapter> adapter(
107 new WebRtcAudioSinkAdapter(sink));
108 owner_->AddSink(adapter.get());
109 sink_adapters_.push_back(adapter.release());
112 void WebRtcLocalAudioTrackAdapter::RemoveSink(
113 webrtc::AudioTrackSinkInterface* sink) {
114 DCHECK(signaling_thread_checker_.CalledOnValidThread());
115 DCHECK(sink);
116 for (ScopedVector<WebRtcAudioSinkAdapter>::iterator it =
117 sink_adapters_.begin();
118 it != sink_adapters_.end(); ++it) {
119 if ((*it)->IsEqual(sink)) {
120 owner_->RemoveSink(*it);
121 sink_adapters_.erase(it);
122 return;
127 bool WebRtcLocalAudioTrackAdapter::GetSignalLevel(int* level) {
128 DCHECK(signaling_thread_checker_.CalledOnValidThread());
130 base::AutoLock auto_lock(lock_);
131 *level = signal_level_;
132 return true;
135 rtc::scoped_refptr<webrtc::AudioProcessorInterface>
136 WebRtcLocalAudioTrackAdapter::GetAudioProcessor() {
137 DCHECK(signaling_thread_checker_.CalledOnValidThread());
138 base::AutoLock auto_lock(lock_);
139 return audio_processor_.get();
142 std::vector<int> WebRtcLocalAudioTrackAdapter::VoeChannels() const {
143 DCHECK(capture_thread_.CalledOnValidThread());
144 base::AutoLock auto_lock(lock_);
145 return voe_channels_;
148 void WebRtcLocalAudioTrackAdapter::SetSignalLevel(int signal_level) {
149 DCHECK(capture_thread_.CalledOnValidThread());
150 base::AutoLock auto_lock(lock_);
151 signal_level_ = signal_level;
154 void WebRtcLocalAudioTrackAdapter::AddChannel(int channel_id) {
155 DCHECK(signaling_thread_checker_.CalledOnValidThread());
156 DVLOG(1) << "WebRtcLocalAudioTrack::AddChannel(channel_id="
157 << channel_id << ")";
158 base::AutoLock auto_lock(lock_);
159 if (std::find(voe_channels_.begin(), voe_channels_.end(), channel_id) !=
160 voe_channels_.end()) {
161 // We need to handle the case when the same channel is connected to the
162 // track more than once.
163 return;
166 voe_channels_.push_back(channel_id);
169 void WebRtcLocalAudioTrackAdapter::RemoveChannel(int channel_id) {
170 DCHECK(signaling_thread_checker_.CalledOnValidThread());
171 DVLOG(1) << "WebRtcLocalAudioTrack::RemoveChannel(channel_id="
172 << channel_id << ")";
173 base::AutoLock auto_lock(lock_);
174 std::vector<int>::iterator iter =
175 std::find(voe_channels_.begin(), voe_channels_.end(), channel_id);
176 DCHECK(iter != voe_channels_.end());
177 voe_channels_.erase(iter);
180 webrtc::AudioSourceInterface* WebRtcLocalAudioTrackAdapter::GetSource() const {
181 DCHECK(signaling_thread_checker_.CalledOnValidThread());
182 return track_source_;
185 cricket::AudioRenderer* WebRtcLocalAudioTrackAdapter::GetRenderer() {
186 return NULL;
189 } // namespace content