OLD | NEW |
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 "media/audio/audio_output_resampler.h" | 5 #include "media/audio/audio_output_resampler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
| 14 #include "build/build_config.h" |
14 #include "media/audio/audio_io.h" | 15 #include "media/audio/audio_io.h" |
15 #include "media/audio/audio_output_dispatcher_impl.h" | 16 #include "media/audio/audio_output_dispatcher_impl.h" |
16 #include "media/audio/audio_output_proxy.h" | 17 #include "media/audio/audio_output_proxy.h" |
17 #include "media/audio/audio_util.h" | 18 #include "media/audio/audio_util.h" |
18 #include "media/audio/sample_rates.h" | 19 #include "media/audio/sample_rates.h" |
19 #include "media/base/audio_converter.h" | 20 #include "media/base/audio_converter.h" |
20 #include "media/base/limits.h" | 21 #include "media/base/limits.h" |
21 #include "media/base/media_switches.h" | 22 #include "media/base/media_switches.h" |
22 | 23 |
| 24 #if defined(OS_WIN) |
| 25 #include "media/audio/win/core_audio_util_win.h" |
| 26 #endif |
| 27 |
23 namespace media { | 28 namespace media { |
24 | 29 |
25 class OnMoreDataConverter | 30 class OnMoreDataConverter |
26 : public AudioOutputStream::AudioSourceCallback, | 31 : public AudioOutputStream::AudioSourceCallback, |
27 public AudioConverter::InputCallback { | 32 public AudioConverter::InputCallback { |
28 public: | 33 public: |
29 OnMoreDataConverter(const AudioParameters& input_params, | 34 OnMoreDataConverter(const AudioParameters& input_params, |
30 const AudioParameters& output_params); | 35 const AudioParameters& output_params); |
31 virtual ~OnMoreDataConverter(); | 36 virtual ~OnMoreDataConverter(); |
32 | 37 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 // Last AudioBuffersState object received via OnMoreData(), used to correct | 70 // Last AudioBuffersState object received via OnMoreData(), used to correct |
66 // playback delay by ProvideInput() and passed on to |source_callback_|. | 71 // playback delay by ProvideInput() and passed on to |source_callback_|. |
67 AudioBuffersState current_buffers_state_; | 72 AudioBuffersState current_buffers_state_; |
68 | 73 |
69 const int input_bytes_per_second_; | 74 const int input_bytes_per_second_; |
70 | 75 |
71 // Handles resampling, buffering, and channel mixing between input and output | 76 // Handles resampling, buffering, and channel mixing between input and output |
72 // parameters. | 77 // parameters. |
73 AudioConverter audio_converter_; | 78 AudioConverter audio_converter_; |
74 | 79 |
| 80 // If we're using WaveOut on Windows' we always have to wait for DataReady() |
| 81 // before calling |source_callback_|. |
| 82 bool waveout_wait_hack_; |
| 83 |
75 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); | 84 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); |
76 }; | 85 }; |
77 | 86 |
78 // Record UMA statistics for hardware output configuration. | 87 // Record UMA statistics for hardware output configuration. |
79 static void RecordStats(const AudioParameters& output_params) { | 88 static void RecordStats(const AudioParameters& output_params) { |
80 UMA_HISTOGRAM_ENUMERATION( | 89 UMA_HISTOGRAM_ENUMERATION( |
81 "Media.HardwareAudioBitsPerChannel", output_params.bits_per_sample(), | 90 "Media.HardwareAudioBitsPerChannel", output_params.bits_per_sample(), |
82 limits::kMaxBitsPerSample); | 91 limits::kMaxBitsPerSample); |
83 UMA_HISTOGRAM_ENUMERATION( | 92 UMA_HISTOGRAM_ENUMERATION( |
84 "Media.HardwareAudioChannelLayout", output_params.channel_layout(), | 93 "Media.HardwareAudioChannelLayout", output_params.channel_layout(), |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 | 280 |
272 dispatcher_->Shutdown(); | 281 dispatcher_->Shutdown(); |
273 DCHECK(callbacks_.empty()); | 282 DCHECK(callbacks_.empty()); |
274 } | 283 } |
275 | 284 |
276 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, | 285 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, |
277 const AudioParameters& output_params) | 286 const AudioParameters& output_params) |
278 : source_callback_(NULL), | 287 : source_callback_(NULL), |
279 source_bus_(NULL), | 288 source_bus_(NULL), |
280 input_bytes_per_second_(input_params.GetBytesPerSecond()), | 289 input_bytes_per_second_(input_params.GetBytesPerSecond()), |
281 audio_converter_(input_params, output_params, false) { | 290 audio_converter_(input_params, output_params, false), |
| 291 waveout_wait_hack_(false) { |
282 io_ratio_ = | 292 io_ratio_ = |
283 static_cast<double>(input_params.GetBytesPerSecond()) / | 293 static_cast<double>(input_params.GetBytesPerSecond()) / |
284 output_params.GetBytesPerSecond(); | 294 output_params.GetBytesPerSecond(); |
| 295 |
| 296 // TODO(dalecurtis): We should require all render side clients to use a |
| 297 // buffer size that's a multiple of the hardware buffer size scaled by the |
| 298 // request_sample_rate / hw_sample_rate. Doing so ensures each hardware |
| 299 // request for audio data results in only a single render side callback and |
| 300 // would allow us to remove this hack. See http://crbug.com/162207. |
| 301 #if defined(OS_WIN) |
| 302 waveout_wait_hack_ = |
| 303 output_params.format() == AudioParameters::AUDIO_PCM_LINEAR || |
| 304 !CoreAudioUtil::IsSupported(); |
| 305 #endif |
285 } | 306 } |
286 | 307 |
287 OnMoreDataConverter::~OnMoreDataConverter() {} | 308 OnMoreDataConverter::~OnMoreDataConverter() {} |
288 | 309 |
289 void OnMoreDataConverter::Start( | 310 void OnMoreDataConverter::Start( |
290 AudioOutputStream::AudioSourceCallback* callback) { | 311 AudioOutputStream::AudioSourceCallback* callback) { |
291 base::AutoLock auto_lock(source_lock_); | 312 base::AutoLock auto_lock(source_lock_); |
292 DCHECK(!source_callback_); | 313 DCHECK(!source_callback_); |
293 source_callback_ = callback; | 314 source_callback_ = callback; |
294 | 315 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 source_lock_.AssertAcquired(); | 354 source_lock_.AssertAcquired(); |
334 | 355 |
335 // Adjust playback delay to include |buffer_delay|. | 356 // Adjust playback delay to include |buffer_delay|. |
336 // TODO(dalecurtis): Stop passing bytes around, it doesn't make sense since | 357 // TODO(dalecurtis): Stop passing bytes around, it doesn't make sense since |
337 // AudioBus is just float data. Use TimeDelta instead. | 358 // AudioBus is just float data. Use TimeDelta instead. |
338 AudioBuffersState new_buffers_state; | 359 AudioBuffersState new_buffers_state; |
339 new_buffers_state.pending_bytes = | 360 new_buffers_state.pending_bytes = |
340 io_ratio_ * (current_buffers_state_.total_bytes() + | 361 io_ratio_ * (current_buffers_state_.total_bytes() + |
341 buffer_delay.InSecondsF() * input_bytes_per_second_); | 362 buffer_delay.InSecondsF() * input_bytes_per_second_); |
342 | 363 |
| 364 if (waveout_wait_hack_) |
| 365 source_callback_->WaitTillDataReady(); |
| 366 |
343 // Retrieve data from the original callback. | 367 // Retrieve data from the original callback. |
344 int frames = source_callback_->OnMoreIOData( | 368 int frames = source_callback_->OnMoreIOData( |
345 source_bus_, dest, new_buffers_state); | 369 source_bus_, dest, new_buffers_state); |
346 | 370 |
347 // |source_bus_| should only be provided once. | 371 // |source_bus_| should only be provided once. |
348 // TODO(dalecurtis, crogers): This is not a complete fix. If ProvideInput() | 372 // TODO(dalecurtis, crogers): This is not a complete fix. If ProvideInput() |
349 // is called multiple times, we need to do something more clever here. | 373 // is called multiple times, we need to do something more clever here. |
350 source_bus_ = NULL; | 374 source_bus_ = NULL; |
351 | 375 |
352 // Zero any unfilled frames if anything was filled, otherwise we'll just | 376 // Zero any unfilled frames if anything was filled, otherwise we'll just |
(...skipping 11 matching lines...) Expand all Loading... |
364 source_callback_->OnError(stream, code); | 388 source_callback_->OnError(stream, code); |
365 } | 389 } |
366 | 390 |
367 void OnMoreDataConverter::WaitTillDataReady() { | 391 void OnMoreDataConverter::WaitTillDataReady() { |
368 base::AutoLock auto_lock(source_lock_); | 392 base::AutoLock auto_lock(source_lock_); |
369 if (source_callback_) | 393 if (source_callback_) |
370 source_callback_->WaitTillDataReady(); | 394 source_callback_->WaitTillDataReady(); |
371 } | 395 } |
372 | 396 |
373 } // namespace media | 397 } // namespace media |
OLD | NEW |