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

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

Issue 8909006: Fix start/stop of html5 audio stream and race condition when pausing. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/audio_renderer_impl.h" 5 #include "content/renderer/media/audio_renderer_impl.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 10
(...skipping 20 matching lines...) Expand all
31 if (sample_rate <= 48000) 31 if (sample_rate <= 48000)
32 return kNominalBufferSize; 32 return kNominalBufferSize;
33 else if (sample_rate <= 96000) 33 else if (sample_rate <= 96000)
34 return kNominalBufferSize * 2; 34 return kNominalBufferSize * 2;
35 return kNominalBufferSize * 4; 35 return kNominalBufferSize * 4;
36 } 36 }
37 37
38 AudioRendererImpl::AudioRendererImpl() 38 AudioRendererImpl::AudioRendererImpl()
39 : AudioRendererBase(), 39 : AudioRendererBase(),
40 bytes_per_second_(0), 40 bytes_per_second_(0),
41 stopped_(false) { 41 stopped_(false),
42 prepopulated_(false) {
42 // We create the AudioDevice here because it must be created in the 43 // We create the AudioDevice here because it must be created in the
43 // main thread. But we don't yet know the audio format (sample-rate, etc.) 44 // main thread. But we don't yet know the audio format (sample-rate, etc.)
44 // at this point. Later, when OnInitialize() is called, we have 45 // at this point. Later, when OnInitialize() is called, we have
45 // the audio format information and call the AudioDevice::Initialize() 46 // the audio format information and call the AudioDevice::Initialize()
46 // method to fully initialize it. 47 // method to fully initialize it.
47 audio_device_ = new AudioDevice(); 48 audio_device_ = new AudioDevice();
48 } 49 }
49 50
50 AudioRendererImpl::~AudioRendererImpl() { 51 AudioRendererImpl::~AudioRendererImpl() {
51 } 52 }
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 } 186 }
186 187
187 void AudioRendererImpl::DoSeek() { 188 void AudioRendererImpl::DoSeek() {
188 earliest_end_time_ = base::Time::Now(); 189 earliest_end_time_ = base::Time::Now();
189 190
190 // Pause and flush the stream when we seek to a new location. 191 // Pause and flush the stream when we seek to a new location.
191 DCHECK(audio_device_.get()); 192 DCHECK(audio_device_.get());
192 audio_device_->Pause(true); 193 audio_device_->Pause(true);
193 } 194 }
194 195
195 void AudioRendererImpl::Render(const std::vector<float*>& audio_data, 196 size_t AudioRendererImpl::Render(const std::vector<float*>& audio_data,
196 size_t number_of_frames, 197 size_t number_of_frames,
197 size_t audio_delay_milliseconds) { 198 size_t audio_delay_milliseconds) {
199 // Ignore first pre-populating call.
200 // It is issued when creating the stream, and there was no request for data
201 // from AudioSyncReader. We will get that first request when player issue
202 // "play" command.
203 if (!prepopulated_) {
204 prepopulated_ = true;
205 return 0;
206 }
198 if (stopped_ || GetPlaybackRate() == 0.0f) { 207 if (stopped_ || GetPlaybackRate() == 0.0f) {
199 // Output silence if stopped. 208 // Output silence if stopped.
200 for (size_t i = 0; i < audio_data.size(); ++i) 209 for (size_t i = 0; i < audio_data.size(); ++i)
201 memset(audio_data[i], 0, sizeof(float) * number_of_frames); 210 memset(audio_data[i], 0, sizeof(float) * number_of_frames);
202 return; 211 return 0;
203 } 212 }
204 213
205 // Adjust the playback delay. 214 // Adjust the playback delay.
206 base::Time current_time = base::Time::Now(); 215 base::Time current_time = base::Time::Now();
207 216
208 base::TimeDelta request_delay = 217 base::TimeDelta request_delay =
209 base::TimeDelta::FromMilliseconds(audio_delay_milliseconds); 218 base::TimeDelta::FromMilliseconds(audio_delay_milliseconds);
210 219
211 // Finally we need to adjust the delay according to playback rate. 220 // Finally we need to adjust the delay according to playback rate.
212 if (GetPlaybackRate() != 1.0f) { 221 if (GetPlaybackRate() != 1.0f) {
213 request_delay = base::TimeDelta::FromMicroseconds( 222 request_delay = base::TimeDelta::FromMicroseconds(
214 static_cast<int64>(ceil(request_delay.InMicroseconds() * 223 static_cast<int64>(ceil(request_delay.InMicroseconds() *
215 GetPlaybackRate()))); 224 GetPlaybackRate())));
216 } 225 }
217 226
218 uint32 bytes_per_frame = 227 uint32 bytes_per_frame =
219 audio_parameters_.bits_per_sample * audio_parameters_.channels / 8; 228 audio_parameters_.bits_per_sample * audio_parameters_.channels / 8;
220 229
221 const size_t buf_size = number_of_frames * bytes_per_frame; 230 const size_t buf_size = number_of_frames * bytes_per_frame;
222 scoped_array<uint8> buf(new uint8[buf_size]); 231 scoped_array<uint8> buf(new uint8[buf_size]);
223 232
224 base::Time time_now = base::Time::Now(); 233 base::Time time_now = base::Time::Now();
225 uint32 filled = FillBuffer(buf.get(), 234 uint32 filled = FillBuffer(buf.get(),
226 buf_size, 235 buf_size,
227 request_delay, 236 request_delay,
228 time_now >= earliest_end_time_); 237 time_now >= earliest_end_time_);
229 DCHECK_LE(filled, buf_size); 238 DCHECK_LE(filled, buf_size);
239 UpdateEarliestEndTime(filled, request_delay, time_now);
230 240
231 uint32 filled_frames = filled / bytes_per_frame; 241 uint32 filled_frames = filled / bytes_per_frame;
232 242
233 // Deinterleave each audio channel. 243 // Deinterleave each audio channel.
234 int channels = audio_data.size(); 244 int channels = audio_data.size();
235 for (int channel_index = 0; channel_index < channels; ++channel_index) { 245 for (int channel_index = 0; channel_index < channels; ++channel_index) {
236 media::DeinterleaveAudioChannel(buf.get(), 246 media::DeinterleaveAudioChannel(buf.get(),
237 audio_data[channel_index], 247 audio_data[channel_index],
238 channels, 248 channels,
239 channel_index, 249 channel_index,
240 bytes_per_frame / channels, 250 bytes_per_frame / channels,
241 filled_frames); 251 filled_frames);
242 252
243 // If FillBuffer() didn't give us enough data then zero out the remainder. 253 // If FillBuffer() didn't give us enough data then zero out the remainder.
244 if (filled_frames < number_of_frames) { 254 if (filled_frames < number_of_frames) {
245 int frames_to_zero = number_of_frames - filled_frames; 255 int frames_to_zero = number_of_frames - filled_frames;
246 memset(audio_data[channel_index], 0, sizeof(float) * frames_to_zero); 256 memset(audio_data[channel_index], 0, sizeof(float) * frames_to_zero);
247 } 257 }
248 } 258 }
259 return filled_frames;
249 } 260 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698