| 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 "media/audio/audio_io.h" | 14 #include "media/audio/audio_io.h" |
| 15 #include "media/audio/audio_output_dispatcher_impl.h" | 15 #include "media/audio/audio_output_dispatcher_impl.h" |
| 16 #include "media/audio/audio_output_proxy.h" | 16 #include "media/audio/audio_output_proxy.h" |
| 17 #include "media/audio/audio_util.h" | 17 #include "media/audio/audio_util.h" |
| 18 #include "media/audio/sample_rates.h" | 18 #include "media/audio/sample_rates.h" |
| 19 #include "media/base/audio_pull_fifo.h" | 19 #include "media/base/audio_pull_fifo.h" |
| 20 #include "media/base/channel_mixer.h" | 20 #include "media/base/channel_mixer.h" |
| 21 #include "media/base/limits.h" | 21 #include "media/base/limits.h" |
| 22 #include "media/base/media_switches.h" | 22 #include "media/base/media_switches.h" |
| 23 #include "media/base/multi_channel_resampler.h" | 23 #include "media/base/multi_channel_resampler.h" |
| 24 | 24 |
| 25 namespace media { | 25 namespace media { |
| 26 | 26 |
| 27 class OnMoreDataResampler : public AudioOutputStream::AudioSourceCallback { | |
| 28 public: | |
| 29 OnMoreDataResampler(double io_ratio, | |
| 30 const AudioParameters& input_params, | |
| 31 const AudioParameters& output_params); | |
| 32 virtual ~OnMoreDataResampler(); | |
| 33 | |
| 34 // AudioSourceCallback interface. | |
| 35 virtual int OnMoreData(AudioBus* dest, | |
| 36 AudioBuffersState buffers_state) OVERRIDE; | |
| 37 virtual int OnMoreIOData(AudioBus* source, | |
| 38 AudioBus* dest, | |
| 39 AudioBuffersState buffers_state) OVERRIDE; | |
| 40 virtual void OnError(AudioOutputStream* stream, int code) OVERRIDE; | |
| 41 virtual void WaitTillDataReady() OVERRIDE; | |
| 42 | |
| 43 // Sets |source_callback_|. If this is not a new object, then Stop() must be | |
| 44 // called before Start(). | |
| 45 void Start(AudioOutputStream::AudioSourceCallback* callback); | |
| 46 | |
| 47 // Clears |source_callback_| and flushes the resampler. | |
| 48 void Stop(); | |
| 49 | |
| 50 private: | |
| 51 // Called by MultiChannelResampler when more data is necessary. | |
| 52 void ProvideInput(AudioBus* audio_bus); | |
| 53 | |
| 54 // Called by AudioPullFifo when more data is necessary. Requires | |
| 55 // |source_lock_| to have been acquired. | |
| 56 void SourceCallback_Locked(AudioBus* audio_bus); | |
| 57 | |
| 58 // Passes through |source| to the |source_callback_| OnMoreIOData() call. | |
| 59 void SourceIOCallback_Locked(AudioBus* source, AudioBus* dest); | |
| 60 | |
| 61 // Ratio of input bytes to output bytes used to correct playback delay with | |
| 62 // regard to buffering and resampling. | |
| 63 double io_ratio_; | |
| 64 | |
| 65 // Source callback and associated lock. | |
| 66 base::Lock source_lock_; | |
| 67 AudioOutputStream::AudioSourceCallback* source_callback_; | |
| 68 | |
| 69 // Last AudioBuffersState object received via OnMoreData(), used to correct | |
| 70 // playback delay by ProvideInput() and passed on to |source_callback_|. | |
| 71 AudioBuffersState current_buffers_state_; | |
| 72 | |
| 73 // Total number of bytes (in terms of output parameters) stored in resampler | |
| 74 // or FIFO buffers which have not been sent to the audio device. | |
| 75 int outstanding_audio_bytes_; | |
| 76 | |
| 77 // Used to buffer data between the client and the output device in cases where | |
| 78 // the client buffer size is not the same as the output device buffer size. | |
| 79 // Bound to SourceCallback_Locked() so must only be used when |source_lock_| | |
| 80 // has already been acquired. | |
| 81 scoped_ptr<AudioPullFifo> audio_fifo_; | |
| 82 | |
| 83 // Handles resampling. | |
| 84 scoped_ptr<MultiChannelResampler> resampler_; | |
| 85 | |
| 86 // Handles channel transforms. |unmixed_audio_| is a temporary destination | |
| 87 // for audio data before it goes into the channel mixer. | |
| 88 scoped_ptr<ChannelMixer> channel_mixer_; | |
| 89 scoped_ptr<AudioBus> unmixed_audio_; | |
| 90 | |
| 91 int output_bytes_per_frame_; | |
| 92 int input_bytes_per_frame_; | |
| 93 | |
| 94 // Since resampling is expensive, figure out if we should downmix channels | |
| 95 // before resampling. | |
| 96 bool downmix_early_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(OnMoreDataResampler); | |
| 99 }; | |
| 100 | |
| 101 // Record UMA statistics for hardware output configuration. | 27 // Record UMA statistics for hardware output configuration. |
| 102 static void RecordStats(const AudioParameters& output_params) { | 28 static void RecordStats(const AudioParameters& output_params) { |
| 103 UMA_HISTOGRAM_ENUMERATION( | 29 UMA_HISTOGRAM_ENUMERATION( |
| 104 "Media.HardwareAudioBitsPerChannel", output_params.bits_per_sample(), | 30 "Media.HardwareAudioBitsPerChannel", output_params.bits_per_sample(), |
| 105 limits::kMaxBitsPerSample); | 31 limits::kMaxBitsPerSample); |
| 106 UMA_HISTOGRAM_ENUMERATION( | 32 UMA_HISTOGRAM_ENUMERATION( |
| 107 "Media.HardwareAudioChannelLayout", output_params.channel_layout(), | 33 "Media.HardwareAudioChannelLayout", output_params.channel_layout(), |
| 108 CHANNEL_LAYOUT_MAX); | 34 CHANNEL_LAYOUT_MAX); |
| 109 UMA_HISTOGRAM_ENUMERATION( | 35 UMA_HISTOGRAM_ENUMERATION( |
| 110 "Media.HardwareAudioChannelCount", output_params.channels(), | 36 "Media.HardwareAudioChannelCount", output_params.channels(), |
| (...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 source_callback_->OnError(stream, code); | 435 source_callback_->OnError(stream, code); |
| 510 } | 436 } |
| 511 | 437 |
| 512 void OnMoreDataResampler::WaitTillDataReady() { | 438 void OnMoreDataResampler::WaitTillDataReady() { |
| 513 base::AutoLock auto_lock(source_lock_); | 439 base::AutoLock auto_lock(source_lock_); |
| 514 if (source_callback_ && !outstanding_audio_bytes_) | 440 if (source_callback_ && !outstanding_audio_bytes_) |
| 515 source_callback_->WaitTillDataReady(); | 441 source_callback_->WaitTillDataReady(); |
| 516 } | 442 } |
| 517 | 443 |
| 518 } // namespace media | 444 } // namespace media |
| OLD | NEW |