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