Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / webrtc_local_audio_track.cc
1 // Copyright (c) 2012 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.
4
5 #include "content/renderer/media/webrtc_local_audio_track.h"
6
7 #include "content/public/renderer/media_stream_audio_sink.h"
8 #include "content/renderer/media/media_stream_audio_level_calculator.h"
9 #include "content/renderer/media/media_stream_audio_processor.h"
10 #include "content/renderer/media/media_stream_audio_sink_owner.h"
11 #include "content/renderer/media/media_stream_audio_track_sink.h"
12 #include "content/renderer/media/peer_connection_audio_sink_owner.h"
13 #include "content/renderer/media/webaudio_capturer_source.h"
14 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
15 #include "content/renderer/media/webrtc_audio_capturer.h"
16
17 namespace content {
18
19 WebRtcLocalAudioTrack::WebRtcLocalAudioTrack(
20     WebRtcLocalAudioTrackAdapter* adapter,
21     const scoped_refptr<WebRtcAudioCapturer>& capturer,
22     WebAudioCapturerSource* webaudio_source)
23     : MediaStreamTrack(true),
24       adapter_(adapter),
25       capturer_(capturer),
26       webaudio_source_(webaudio_source) {
27   DCHECK(capturer.get() || webaudio_source);
28   signal_thread_checker_.DetachFromThread();
29
30   adapter_->Initialize(this);
31
32   DVLOG(1) << "WebRtcLocalAudioTrack::WebRtcLocalAudioTrack()";
33 }
34
35 WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack() {
36   DCHECK(main_render_thread_checker_.CalledOnValidThread());
37   DVLOG(1) << "WebRtcLocalAudioTrack::~WebRtcLocalAudioTrack()";
38   // Users might not call Stop() on the track.
39   Stop();
40 }
41
42 void WebRtcLocalAudioTrack::Capture(const int16* audio_data,
43                                     base::TimeDelta delay,
44                                     int volume,
45                                     bool key_pressed,
46                                     bool need_audio_processing,
47                                     bool force_report_nonzero_energy) {
48   DCHECK(capture_thread_checker_.CalledOnValidThread());
49
50   // Calculate the signal level regardless if the track is disabled or enabled.
51   int signal_level = level_calculator_->Calculate(
52       audio_data, audio_parameters_.channels(),
53       audio_parameters_.frames_per_buffer(), force_report_nonzero_energy);
54   adapter_->SetSignalLevel(signal_level);
55
56   scoped_refptr<WebRtcAudioCapturer> capturer;
57   SinkList::ItemList sinks;
58   SinkList::ItemList sinks_to_notify_format;
59   {
60     base::AutoLock auto_lock(lock_);
61     capturer = capturer_;
62     sinks = sinks_.Items();
63     sinks_.RetrieveAndClearTags(&sinks_to_notify_format);
64   }
65
66   // Notify the tracks on when the format changes. This will do nothing if
67   // |sinks_to_notify_format| is empty.
68   for (SinkList::ItemList::const_iterator it = sinks_to_notify_format.begin();
69        it != sinks_to_notify_format.end(); ++it) {
70     (*it)->OnSetFormat(audio_parameters_);
71   }
72
73   // Feed the data to the sinks.
74   // TODO(jiayl): we should not pass the real audio data down if the track is
75   // disabled. This is currently done so to feed input to WebRTC typing
76   // detection and should be changed when audio processing is moved from
77   // WebRTC to the track.
78   std::vector<int> voe_channels = adapter_->VoeChannels();
79   for (SinkList::ItemList::const_iterator it = sinks.begin();
80        it != sinks.end();
81        ++it) {
82     int new_volume = (*it)->OnData(audio_data,
83                                    audio_parameters_.sample_rate(),
84                                    audio_parameters_.channels(),
85                                    audio_parameters_.frames_per_buffer(),
86                                    voe_channels,
87                                    delay.InMilliseconds(),
88                                    volume,
89                                    need_audio_processing,
90                                    key_pressed);
91     if (new_volume != 0 && capturer.get() && !webaudio_source_.get()) {
92       // Feed the new volume to WebRtc while changing the volume on the
93       // browser.
94       capturer->SetVolume(new_volume);
95     }
96   }
97 }
98
99 void WebRtcLocalAudioTrack::OnSetFormat(
100     const media::AudioParameters& params) {
101   DVLOG(1) << "WebRtcLocalAudioTrack::OnSetFormat()";
102   // If the source is restarted, we might have changed to another capture
103   // thread.
104   capture_thread_checker_.DetachFromThread();
105   DCHECK(capture_thread_checker_.CalledOnValidThread());
106
107   audio_parameters_ = params;
108   level_calculator_.reset(new MediaStreamAudioLevelCalculator());
109
110   base::AutoLock auto_lock(lock_);
111   // Remember to notify all sinks of the new format.
112   sinks_.TagAll();
113 }
114
115 void WebRtcLocalAudioTrack::SetAudioProcessor(
116     const scoped_refptr<MediaStreamAudioProcessor>& processor) {
117   // if the |processor| does not have audio processing, which can happen if
118   // kDisableAudioTrackProcessing is set set or all the constraints in
119   // the |processor| are turned off. In such case, we pass NULL to the
120   // adapter to indicate that no stats can be gotten from the processor.
121   adapter_->SetAudioProcessor(processor->has_audio_processing() ?
122       processor : NULL);
123 }
124
125 void WebRtcLocalAudioTrack::AddSink(MediaStreamAudioSink* sink) {
126   // This method is called from webrtc, on the signaling thread, when the local
127   // description is set and from the main thread from WebMediaPlayerMS::load
128   // (via WebRtcLocalAudioRenderer::Start).
129   DCHECK(main_render_thread_checker_.CalledOnValidThread() ||
130          signal_thread_checker_.CalledOnValidThread());
131   DVLOG(1) << "WebRtcLocalAudioTrack::AddSink()";
132   base::AutoLock auto_lock(lock_);
133
134   // Verify that |sink| is not already added to the list.
135   DCHECK(!sinks_.Contains(
136       MediaStreamAudioTrackSink::WrapsMediaStreamSink(sink)));
137
138   // Create (and add to the list) a new MediaStreamAudioTrackSink
139   // which owns the |sink| and delagates all calls to the
140   // MediaStreamAudioSink interface. It will be tagged in the list, so
141   // we remember to call OnSetFormat() on the new sink.
142   scoped_refptr<MediaStreamAudioTrackSink> sink_owner(
143       new MediaStreamAudioSinkOwner(sink));
144   sinks_.AddAndTag(sink_owner.get());
145 }
146
147 void WebRtcLocalAudioTrack::RemoveSink(MediaStreamAudioSink* sink) {
148   // See AddSink for additional context.  When local audio is stopped from
149   // webrtc, we'll be called here on the signaling thread.
150   DCHECK(main_render_thread_checker_.CalledOnValidThread() ||
151          signal_thread_checker_.CalledOnValidThread());
152   DVLOG(1) << "WebRtcLocalAudioTrack::RemoveSink()";
153
154   scoped_refptr<MediaStreamAudioTrackSink> removed_item;
155   {
156     base::AutoLock auto_lock(lock_);
157     removed_item = sinks_.Remove(
158         MediaStreamAudioTrackSink::WrapsMediaStreamSink(sink));
159   }
160
161   // Clear the delegate to ensure that no more capture callbacks will
162   // be sent to this sink. Also avoids a possible crash which can happen
163   // if this method is called while capturing is active.
164   if (removed_item.get())
165     removed_item->Reset();
166 }
167
168 void WebRtcLocalAudioTrack::AddSink(PeerConnectionAudioSink* sink) {
169   DCHECK(main_render_thread_checker_.CalledOnValidThread());
170   DVLOG(1) << "WebRtcLocalAudioTrack::AddSink()";
171   base::AutoLock auto_lock(lock_);
172
173   // Verify that |sink| is not already added to the list.
174   DCHECK(!sinks_.Contains(
175       MediaStreamAudioTrackSink::WrapsPeerConnectionSink(sink)));
176
177   // Create (and add to the list) a new MediaStreamAudioTrackSink
178   // which owns the |sink| and delagates all calls to the
179   // MediaStreamAudioSink interface. It will be tagged in the list, so
180   // we remember to call OnSetFormat() on the new sink.
181   scoped_refptr<MediaStreamAudioTrackSink> sink_owner(
182       new PeerConnectionAudioSinkOwner(sink));
183   sinks_.AddAndTag(sink_owner.get());
184 }
185
186 void WebRtcLocalAudioTrack::RemoveSink(PeerConnectionAudioSink* sink) {
187   DCHECK(main_render_thread_checker_.CalledOnValidThread());
188   DVLOG(1) << "WebRtcLocalAudioTrack::RemoveSink()";
189
190   base::AutoLock auto_lock(lock_);
191
192   scoped_refptr<MediaStreamAudioTrackSink> removed_item = sinks_.Remove(
193       MediaStreamAudioTrackSink::WrapsPeerConnectionSink(sink));
194   // Clear the delegate to ensure that no more capture callbacks will
195   // be sent to this sink. Also avoids a possible crash which can happen
196   // if this method is called while capturing is active.
197   if (removed_item.get())
198     removed_item->Reset();
199 }
200
201 void WebRtcLocalAudioTrack::Start() {
202   DCHECK(main_render_thread_checker_.CalledOnValidThread());
203   DVLOG(1) << "WebRtcLocalAudioTrack::Start()";
204   if (webaudio_source_.get()) {
205     // If the track is hooking up with WebAudio, do NOT add the track to the
206     // capturer as its sink otherwise two streams in different clock will be
207     // pushed through the same track.
208     webaudio_source_->Start(this, capturer_.get());
209   } else if (capturer_.get()) {
210     capturer_->AddTrack(this);
211   }
212
213   SinkList::ItemList sinks;
214   {
215     base::AutoLock auto_lock(lock_);
216     sinks = sinks_.Items();
217   }
218   for (SinkList::ItemList::const_iterator it = sinks.begin();
219        it != sinks.end();
220        ++it) {
221     (*it)->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateLive);
222   }
223 }
224
225 void WebRtcLocalAudioTrack::SetEnabled(bool enabled) {
226   DCHECK(thread_checker_.CalledOnValidThread());
227   if (adapter_.get())
228     adapter_->set_enabled(enabled);
229 }
230
231 void WebRtcLocalAudioTrack::Stop() {
232   DCHECK(main_render_thread_checker_.CalledOnValidThread());
233   DVLOG(1) << "WebRtcLocalAudioTrack::Stop()";
234   if (!capturer_.get() && !webaudio_source_.get())
235     return;
236
237   if (webaudio_source_.get()) {
238     // Called Stop() on the |webaudio_source_| explicitly so that
239     // |webaudio_source_| won't push more data to the track anymore.
240     // Also note that the track is not registered as a sink to the |capturer_|
241     // in such case and no need to call RemoveTrack().
242     webaudio_source_->Stop();
243   } else {
244     // It is necessary to call RemoveTrack on the |capturer_| to avoid getting
245     // audio callback after Stop().
246     capturer_->RemoveTrack(this);
247   }
248
249   // Protect the pointers using the lock when accessing |sinks_| and
250   // setting the |capturer_| to NULL.
251   SinkList::ItemList sinks;
252   {
253     base::AutoLock auto_lock(lock_);
254     sinks = sinks_.Items();
255     sinks_.Clear();
256     webaudio_source_ = NULL;
257     capturer_ = NULL;
258   }
259
260   for (SinkList::ItemList::const_iterator it = sinks.begin();
261        it != sinks.end();
262        ++it){
263     (*it)->OnReadyStateChanged(blink::WebMediaStreamSource::ReadyStateEnded);
264     (*it)->Reset();
265   }
266 }
267
268 webrtc::AudioTrackInterface* WebRtcLocalAudioTrack::GetAudioAdapter() {
269   DCHECK(thread_checker_.CalledOnValidThread());
270   return adapter_.get();
271 }
272
273 }  // namespace content