Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: content/renderer/media/webrtc_local_audio_renderer.cc

Issue 90743004: Add generic interfaces for the sinks of the media stream audio track (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixed the nits. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/renderer/media/webrtc_local_audio_renderer.h" 5 #include "content/renderer/media/webrtc_local_audio_renderer.h"
6 6
7 #include "base/debug/trace_event.h" 7 #include "base/debug/trace_event.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/message_loop/message_loop_proxy.h" 9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 DVLOG(2) << "loopback FIFO is empty"; 49 DVLOG(2) << "loopback FIFO is empty";
50 } 50 }
51 51
52 return audio_bus->frames(); 52 return audio_bus->frames();
53 } 53 }
54 54
55 void WebRtcLocalAudioRenderer::OnRenderError() { 55 void WebRtcLocalAudioRenderer::OnRenderError() {
56 NOTIMPLEMENTED(); 56 NOTIMPLEMENTED();
57 } 57 }
58 58
59 // content::WebRtcAudioCapturerSink implementation 59 // content::MediaStreamAudioSink implementation
60 int WebRtcLocalAudioRenderer::CaptureData(const std::vector<int>& channels, 60 void WebRtcLocalAudioRenderer::OnData(const int16* audio_data,
61 const int16* audio_data, 61 int sample_rate,
62 int sample_rate, 62 int number_of_channels,
63 int number_of_channels, 63 int number_of_frames) {
64 int number_of_frames,
65 int audio_delay_milliseconds,
66 int current_volume,
67 bool need_audio_processing,
68 bool key_pressed) {
69 DCHECK(capture_thread_checker_.CalledOnValidThread()); 64 DCHECK(capture_thread_checker_.CalledOnValidThread());
70 TRACE_EVENT0("audio", "WebRtcLocalAudioRenderer::CaptureData"); 65 TRACE_EVENT0("audio", "WebRtcLocalAudioRenderer::CaptureData");
71 base::AutoLock auto_lock(thread_lock_); 66 base::AutoLock auto_lock(thread_lock_);
72 if (!playing_ || !volume_ || !loopback_fifo_) 67 if (!playing_ || !volume_ || !loopback_fifo_)
73 return 0; 68 return;
74 69
75 // Push captured audio to FIFO so it can be read by a local sink. 70 // Push captured audio to FIFO so it can be read by a local sink.
76 if (loopback_fifo_->frames() + number_of_frames <= 71 if (loopback_fifo_->frames() + number_of_frames <=
77 loopback_fifo_->max_frames()) { 72 loopback_fifo_->max_frames()) {
78 scoped_ptr<media::AudioBus> audio_source = media::AudioBus::Create( 73 scoped_ptr<media::AudioBus> audio_source = media::AudioBus::Create(
79 number_of_channels, number_of_frames); 74 number_of_channels, number_of_frames);
80 audio_source->FromInterleaved(audio_data, 75 audio_source->FromInterleaved(audio_data,
81 audio_source->frames(), 76 audio_source->frames(),
82 sizeof(audio_data[0])); 77 sizeof(audio_data[0]));
83 loopback_fifo_->Push(audio_source.get()); 78 loopback_fifo_->Push(audio_source.get());
84 79
85 const base::TimeTicks now = base::TimeTicks::Now(); 80 const base::TimeTicks now = base::TimeTicks::Now();
86 total_render_time_ += now - last_render_time_; 81 total_render_time_ += now - last_render_time_;
87 last_render_time_ = now; 82 last_render_time_ = now;
88 } else { 83 } else {
89 DVLOG(1) << "FIFO is full"; 84 DVLOG(1) << "FIFO is full";
90 } 85 }
91
92 return 0;
93 } 86 }
94 87
95 void WebRtcLocalAudioRenderer::SetCaptureFormat( 88 void WebRtcLocalAudioRenderer::OnSetFormat(
96 const media::AudioParameters& params) { 89 const media::AudioParameters& params) {
97 DVLOG(1) << "WebRtcLocalAudioRenderer::SetCaptureFormat()"; 90 DVLOG(1) << "WebRtcLocalAudioRenderer::OnSetFormat()";
98 // If the source is restarted, we might have changed to another capture 91 // If the source is restarted, we might have changed to another capture
99 // thread. 92 // thread.
100 capture_thread_checker_.DetachFromThread(); 93 capture_thread_checker_.DetachFromThread();
101 DCHECK(capture_thread_checker_.CalledOnValidThread()); 94 DCHECK(capture_thread_checker_.CalledOnValidThread());
102 95
103 // Reset the |source_params_|, |sink_params_| and |loopback_fifo_| to match 96 // Reset the |source_params_|, |sink_params_| and |loopback_fifo_| to match
104 // the new format. 97 // the new format.
105 { 98 {
106 base::AutoLock auto_lock(thread_lock_); 99 base::AutoLock auto_lock(thread_lock_);
107 if (source_params_ == params) 100 if (source_params_ == params)
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 // Post a task on the main render thread to reconfigure the |sink_| with the 134 // Post a task on the main render thread to reconfigure the |sink_| with the
142 // new format. 135 // new format.
143 message_loop_->PostTask( 136 message_loop_->PostTask(
144 FROM_HERE, 137 FROM_HERE,
145 base::Bind(&WebRtcLocalAudioRenderer::ReconfigureSink, this, 138 base::Bind(&WebRtcLocalAudioRenderer::ReconfigureSink, this,
146 params)); 139 params));
147 } 140 }
148 141
149 // WebRtcLocalAudioRenderer::WebRtcLocalAudioRenderer implementation. 142 // WebRtcLocalAudioRenderer::WebRtcLocalAudioRenderer implementation.
150 WebRtcLocalAudioRenderer::WebRtcLocalAudioRenderer( 143 WebRtcLocalAudioRenderer::WebRtcLocalAudioRenderer(
151 WebRtcLocalAudioTrack* audio_track, 144 const blink::WebMediaStreamTrack& audio_track,
152 int source_render_view_id, 145 int source_render_view_id,
153 int session_id, 146 int session_id,
154 int frames_per_buffer) 147 int frames_per_buffer)
155 : audio_track_(audio_track), 148 : audio_track_(audio_track),
156 source_render_view_id_(source_render_view_id), 149 source_render_view_id_(source_render_view_id),
157 session_id_(session_id), 150 session_id_(session_id),
158 message_loop_(base::MessageLoopProxy::current()), 151 message_loop_(base::MessageLoopProxy::current()),
159 playing_(false), 152 playing_(false),
160 frames_per_buffer_(frames_per_buffer), 153 frames_per_buffer_(frames_per_buffer),
161 volume_(0.0), 154 volume_(0.0),
162 sink_started_(false) { 155 sink_started_(false) {
163 DCHECK(audio_track);
164 DVLOG(1) << "WebRtcLocalAudioRenderer::WebRtcLocalAudioRenderer()"; 156 DVLOG(1) << "WebRtcLocalAudioRenderer::WebRtcLocalAudioRenderer()";
165 } 157 }
166 158
167 WebRtcLocalAudioRenderer::~WebRtcLocalAudioRenderer() { 159 WebRtcLocalAudioRenderer::~WebRtcLocalAudioRenderer() {
168 DCHECK(message_loop_->BelongsToCurrentThread()); 160 DCHECK(message_loop_->BelongsToCurrentThread());
169 DCHECK(!sink_.get()); 161 DCHECK(!sink_.get());
170 DVLOG(1) << "WebRtcLocalAudioRenderer::~WebRtcLocalAudioRenderer()"; 162 DVLOG(1) << "WebRtcLocalAudioRenderer::~WebRtcLocalAudioRenderer()";
171 } 163 }
172 164
173 void WebRtcLocalAudioRenderer::Start() { 165 void WebRtcLocalAudioRenderer::Start() {
174 DVLOG(1) << "WebRtcLocalAudioRenderer::Start()"; 166 DVLOG(1) << "WebRtcLocalAudioRenderer::Start()";
175 DCHECK(message_loop_->BelongsToCurrentThread()); 167 DCHECK(message_loop_->BelongsToCurrentThread());
176 168
177 if (!audio_track_)
178 return; // Stop() has been called, so never start again.
179
180 // We get audio data from |audio_track_|... 169 // We get audio data from |audio_track_|...
181 audio_track_->AddSink(this); 170 MediaStreamAudioSink::AddToAudioTrack(this, audio_track_);
182 // ...and |sink_| will get audio data from us. 171 // ...and |sink_| will get audio data from us.
183 DCHECK(!sink_.get()); 172 DCHECK(!sink_.get());
184 sink_ = AudioDeviceFactory::NewOutputDevice(source_render_view_id_); 173 sink_ = AudioDeviceFactory::NewOutputDevice(source_render_view_id_);
185 174
186 base::AutoLock auto_lock(thread_lock_); 175 base::AutoLock auto_lock(thread_lock_);
187 last_render_time_ = base::TimeTicks::Now(); 176 last_render_time_ = base::TimeTicks::Now();
188 playing_ = false; 177 playing_ = false;
189 } 178 }
190 179
191 void WebRtcLocalAudioRenderer::Stop() { 180 void WebRtcLocalAudioRenderer::Stop() {
(...skipping 14 matching lines...) Expand all
206 sink_ = NULL; 195 sink_ = NULL;
207 } 196 }
208 197
209 if (!sink_started_) { 198 if (!sink_started_) {
210 UMA_HISTOGRAM_ENUMERATION("Media.LocalRendererSinkStates", 199 UMA_HISTOGRAM_ENUMERATION("Media.LocalRendererSinkStates",
211 kSinkNeverStarted, kSinkStatesMax); 200 kSinkNeverStarted, kSinkStatesMax);
212 } 201 }
213 sink_started_ = false; 202 sink_started_ = false;
214 203
215 // Ensure that the capturer stops feeding us with captured audio. 204 // Ensure that the capturer stops feeding us with captured audio.
216 // Note that, we do not stop the capturer here since it may still be used by 205 MediaStreamAudioSink::RemoveFromAudioTrack(this, audio_track_);
217 // the WebRTC ADM.
218 if (audio_track_) {
219 audio_track_->RemoveSink(this);
220 audio_track_ = NULL;
221 }
222 } 206 }
223 207
224 void WebRtcLocalAudioRenderer::Play() { 208 void WebRtcLocalAudioRenderer::Play() {
225 DVLOG(1) << "WebRtcLocalAudioRenderer::Play()"; 209 DVLOG(1) << "WebRtcLocalAudioRenderer::Play()";
226 DCHECK(message_loop_->BelongsToCurrentThread()); 210 DCHECK(message_loop_->BelongsToCurrentThread());
227 211
228 if (!sink_.get()) 212 if (!sink_.get())
229 return; 213 return;
230 214
231 { 215 {
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 // parameters. Then, invoke MaybeStartSink() to restart everything again. 304 // parameters. Then, invoke MaybeStartSink() to restart everything again.
321 if (sink_started_) { 305 if (sink_started_) {
322 sink_->Stop(); 306 sink_->Stop();
323 sink_started_ = false; 307 sink_started_ = false;
324 } 308 }
325 sink_ = AudioDeviceFactory::NewOutputDevice(source_render_view_id_); 309 sink_ = AudioDeviceFactory::NewOutputDevice(source_render_view_id_);
326 MaybeStartSink(); 310 MaybeStartSink();
327 } 311 }
328 312
329 } // namespace content 313 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/media/webrtc_local_audio_renderer.h ('k') | content/renderer/media/webrtc_local_audio_source_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698