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

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

Issue 9655018: Make AudioParameters a class instead of a struct (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix copyright years Created 8 years, 9 months 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
« no previous file with comments | « content/renderer/media/audio_device.h ('k') | content/renderer/media/audio_device_thread.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/audio_device.h" 5 #include "content/renderer/media/audio_device.h"
6 6
7 #include "base/debug/trace_event.h" 7 #include "base/debug/trace_event.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/threading/thread_restrictions.h" 9 #include "base/threading/thread_restrictions.h"
10 #include "base/time.h" 10 #include "base/time.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 AudioDevice::AudioDevice() 42 AudioDevice::AudioDevice()
43 : ScopedLoopObserver(ChildProcess::current()->io_message_loop()), 43 : ScopedLoopObserver(ChildProcess::current()->io_message_loop()),
44 callback_(NULL), 44 callback_(NULL),
45 volume_(1.0), 45 volume_(1.0),
46 stream_id_(0), 46 stream_id_(0),
47 play_on_start_(true), 47 play_on_start_(true),
48 is_started_(false) { 48 is_started_(false) {
49 filter_ = RenderThreadImpl::current()->audio_message_filter(); 49 filter_ = RenderThreadImpl::current()->audio_message_filter();
50 } 50 }
51 51
52 AudioDevice::AudioDevice(size_t buffer_size, 52 AudioDevice::AudioDevice(const AudioParameters& params,
53 int channels,
54 double sample_rate,
55 RenderCallback* callback) 53 RenderCallback* callback)
56 : ScopedLoopObserver(ChildProcess::current()->io_message_loop()), 54 : ScopedLoopObserver(ChildProcess::current()->io_message_loop()),
57 callback_(NULL), 55 audio_parameters_(params),
56 callback_(callback),
58 volume_(1.0), 57 volume_(1.0),
59 stream_id_(0), 58 stream_id_(0),
60 play_on_start_(true), 59 play_on_start_(true),
61 is_started_(false) { 60 is_started_(false) {
62 filter_ = RenderThreadImpl::current()->audio_message_filter(); 61 filter_ = RenderThreadImpl::current()->audio_message_filter();
63 Initialize(buffer_size,
64 channels,
65 sample_rate,
66 AudioParameters::AUDIO_PCM_LOW_LATENCY,
67 callback);
68 } 62 }
69 63
70 void AudioDevice::Initialize(size_t buffer_size, 64 void AudioDevice::Initialize(const AudioParameters& params,
71 int channels,
72 double sample_rate,
73 AudioParameters::Format latency_format,
74 RenderCallback* callback) { 65 RenderCallback* callback) {
75 CHECK_EQ(0, stream_id_) << 66 CHECK_EQ(0, stream_id_) <<
76 "AudioDevice::Initialize() must be called before Start()"; 67 "AudioDevice::Initialize() must be called before Start()";
77 68
78 CHECK(!callback_); // Calling Initialize() twice? 69 CHECK(!callback_); // Calling Initialize() twice?
79 70
80 audio_parameters_.format = latency_format; 71 audio_parameters_ = params;
81 audio_parameters_.channels = channels;
82 audio_parameters_.sample_rate = static_cast<int>(sample_rate);
83 audio_parameters_.bits_per_sample = 16;
84 audio_parameters_.samples_per_packet = buffer_size;
85
86 callback_ = callback; 72 callback_ = callback;
87 } 73 }
88 74
89 AudioDevice::~AudioDevice() { 75 AudioDevice::~AudioDevice() {
90 // The current design requires that the user calls Stop() before deleting 76 // The current design requires that the user calls Stop() before deleting
91 // this class. 77 // this class.
92 CHECK_EQ(0, stream_id_); 78 CHECK_EQ(0, stream_id_);
93 } 79 }
94 80
95 void AudioDevice::Start() { 81 void AudioDevice::Start() {
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 callback_->OnRenderError(); 204 callback_->OnRenderError();
219 } 205 }
220 } 206 }
221 207
222 void AudioDevice::OnStreamCreated( 208 void AudioDevice::OnStreamCreated(
223 base::SharedMemoryHandle handle, 209 base::SharedMemoryHandle handle,
224 base::SyncSocket::Handle socket_handle, 210 base::SyncSocket::Handle socket_handle,
225 uint32 length) { 211 uint32 length) {
226 DCHECK(message_loop()->BelongsToCurrentThread()); 212 DCHECK(message_loop()->BelongsToCurrentThread());
227 DCHECK_GE(length, 213 DCHECK_GE(length,
228 audio_parameters_.samples_per_packet * sizeof(int16) * 214 audio_parameters_.frames_per_buffer() * sizeof(int16) *
229 audio_parameters_.channels); 215 audio_parameters_.channels());
230 #if defined(OS_WIN) 216 #if defined(OS_WIN)
231 DCHECK(handle); 217 DCHECK(handle);
232 DCHECK(socket_handle); 218 DCHECK(socket_handle);
233 #else 219 #else
234 DCHECK_GE(handle.fd, 0); 220 DCHECK_GE(handle.fd, 0);
235 DCHECK_GE(socket_handle, 0); 221 DCHECK_GE(socket_handle, 0);
236 #endif 222 #endif
237 223
238 base::AutoLock auto_lock(audio_thread_lock_); 224 base::AutoLock auto_lock(audio_thread_lock_);
239 225
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 } 279 }
294 280
295 // Convert the number of pending bytes in the render buffer 281 // Convert the number of pending bytes in the render buffer
296 // into milliseconds. 282 // into milliseconds.
297 int audio_delay_milliseconds = pending_data / bytes_per_ms_; 283 int audio_delay_milliseconds = pending_data / bytes_per_ms_;
298 284
299 TRACE_EVENT0("audio", "AudioDevice::FireRenderCallback"); 285 TRACE_EVENT0("audio", "AudioDevice::FireRenderCallback");
300 286
301 // Update the audio-delay measurement then ask client to render audio. 287 // Update the audio-delay measurement then ask client to render audio.
302 size_t num_frames = render_callback_->Render(audio_data_, 288 size_t num_frames = render_callback_->Render(audio_data_,
303 audio_parameters_.samples_per_packet, audio_delay_milliseconds); 289 audio_parameters_.frames_per_buffer(), audio_delay_milliseconds);
304 290
305 // Interleave, scale, and clip to int16. 291 // Interleave, scale, and clip to int16.
306 // TODO(crogers): avoid converting to integer here, and pass the data 292 // TODO(crogers): avoid converting to integer here, and pass the data
307 // to the browser process as float, so we don't lose precision for 293 // to the browser process as float, so we don't lose precision for
308 // audio hardware which has better than 16bit precision. 294 // audio hardware which has better than 16bit precision.
309 int16* data = reinterpret_cast<int16*>(shared_memory_.memory()); 295 int16* data = reinterpret_cast<int16*>(shared_memory_.memory());
310 media::InterleaveFloatToInt16(audio_data_, data, 296 media::InterleaveFloatToInt16(audio_data_, data,
311 audio_parameters_.samples_per_packet); 297 audio_parameters_.frames_per_buffer());
312 298
313 // Let the host know we are done. 299 // Let the host know we are done.
314 media::SetActualDataSizeInBytes(&shared_memory_, memory_length_, 300 media::SetActualDataSizeInBytes(&shared_memory_, memory_length_,
315 num_frames * audio_parameters_.channels * sizeof(data[0])); 301 num_frames * audio_parameters_.channels() * sizeof(data[0]));
316 } 302 }
OLDNEW
« no previous file with comments | « content/renderer/media/audio_device.h ('k') | content/renderer/media/audio_device_thread.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698