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