| 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 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_ | 5 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_ |
| 6 #define MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_ | 6 #define MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "media/audio/audio_io.h" | 13 #include "media/audio/audio_io.h" |
| 14 #include "media/audio/audio_manager.h" | 14 #include "media/audio/audio_manager.h" |
| 15 #include "media/audio/audio_output_dispatcher.h" | 15 #include "media/audio/audio_output_dispatcher.h" |
| 16 #include "media/audio/audio_parameters.h" | 16 #include "media/audio/audio_parameters.h" |
| 17 | 17 |
| 18 namespace media { | 18 namespace media { |
| 19 | 19 |
| 20 class OnMoreDataResampler; | 20 class OnMoreDataConverter; |
| 21 | 21 |
| 22 // AudioOutputResampler is a browser-side resampling and rebuffering solution | 22 // AudioOutputResampler is a browser-side resampling and buffering solution |
| 23 // which ensures audio data is always output at given parameters. The rough | 23 // which ensures audio data is always output at given parameters. See the |
| 24 // flow is: Client -> [FIFO] -> [Resampler] -> Output Device. | 24 // AudioConverter class for details on the conversion process. |
| 25 // | |
| 26 // The FIFO and resampler are only used when necessary. To be clear: | |
| 27 // - The resampler is only used if the input and output sample rates differ. | |
| 28 // - The FIFO is only used if the input and output frame sizes differ or if | |
| 29 // the resampler is used. | |
| 30 // | 25 // |
| 31 // AOR works by intercepting the AudioSourceCallback provided to StartStream() | 26 // AOR works by intercepting the AudioSourceCallback provided to StartStream() |
| 32 // and redirecting to the appropriate resampling or FIFO callback which passes | 27 // and redirecting it through an AudioConverter instance. AudioBuffersState is |
| 33 // through to the original callback only when necessary. | 28 // adjusted for buffer delay caused by the conversion process. |
| 34 // | 29 // |
| 35 // AOR will automatically fall back from AUDIO_PCM_LOW_LATENCY to | 30 // AOR will automatically fall back from AUDIO_PCM_LOW_LATENCY to |
| 36 // AUDIO_PCM_LINEAR if the output device fails to open at the requested output | 31 // AUDIO_PCM_LINEAR if the output device fails to open at the requested output |
| 37 // parameters. | 32 // parameters. |
| 33 // |
| 38 // TODO(dalecurtis): Ideally the low latency path will be as reliable as the | 34 // TODO(dalecurtis): Ideally the low latency path will be as reliable as the |
| 39 // high latency path once we have channel mixing and support querying for the | 35 // high latency path once we have channel mixing and support querying for the |
| 40 // hardware's configured bit depth. Monitor the UMA stats for fallback and | 36 // hardware's configured bit depth. Monitor the UMA stats for fallback and |
| 41 // remove fallback support once it's stable. http://crbug.com/148418 | 37 // remove fallback support once it's stable. http://crbug.com/148418 |
| 42 // | |
| 43 // Currently channel downmixing and upmixing is not supported. | |
| 44 // TODO(dalecurtis): Add channel remixing. http://crbug.com/138762 | |
| 45 class MEDIA_EXPORT AudioOutputResampler : public AudioOutputDispatcher { | 38 class MEDIA_EXPORT AudioOutputResampler : public AudioOutputDispatcher { |
| 46 public: | 39 public: |
| 47 AudioOutputResampler(AudioManager* audio_manager, | 40 AudioOutputResampler(AudioManager* audio_manager, |
| 48 const AudioParameters& input_params, | 41 const AudioParameters& input_params, |
| 49 const AudioParameters& output_params, | 42 const AudioParameters& output_params, |
| 50 const base::TimeDelta& close_delay); | 43 const base::TimeDelta& close_delay); |
| 51 | 44 |
| 52 // AudioOutputDispatcher interface. | 45 // AudioOutputDispatcher interface. |
| 53 virtual bool OpenStream() OVERRIDE; | 46 virtual bool OpenStream() OVERRIDE; |
| 54 virtual bool StartStream(AudioOutputStream::AudioSourceCallback* callback, | 47 virtual bool StartStream(AudioOutputStream::AudioSourceCallback* callback, |
| 55 AudioOutputProxy* stream_proxy) OVERRIDE; | 48 AudioOutputProxy* stream_proxy) OVERRIDE; |
| 56 virtual void StopStream(AudioOutputProxy* stream_proxy) OVERRIDE; | 49 virtual void StopStream(AudioOutputProxy* stream_proxy) OVERRIDE; |
| 57 virtual void StreamVolumeSet(AudioOutputProxy* stream_proxy, | 50 virtual void StreamVolumeSet(AudioOutputProxy* stream_proxy, |
| 58 double volume) OVERRIDE; | 51 double volume) OVERRIDE; |
| 59 virtual void CloseStream(AudioOutputProxy* stream_proxy) OVERRIDE; | 52 virtual void CloseStream(AudioOutputProxy* stream_proxy) OVERRIDE; |
| 60 virtual void Shutdown() OVERRIDE; | 53 virtual void Shutdown() OVERRIDE; |
| 61 | 54 |
| 62 private: | 55 private: |
| 63 friend class base::RefCountedThreadSafe<AudioOutputResampler>; | 56 friend class base::RefCountedThreadSafe<AudioOutputResampler>; |
| 64 virtual ~AudioOutputResampler(); | 57 virtual ~AudioOutputResampler(); |
| 65 | 58 |
| 66 // Used to initialize the FIFO and resamplers. | 59 // Used to initialize and reinitialize |dispatcher_|. |
| 67 void Initialize(); | 60 void Initialize(); |
| 68 | 61 |
| 69 // Dispatcher to proxy all AudioOutputDispatcher calls too. | 62 // Dispatcher to proxy all AudioOutputDispatcher calls too. |
| 70 scoped_refptr<AudioOutputDispatcher> dispatcher_; | 63 scoped_refptr<AudioOutputDispatcher> dispatcher_; |
| 71 | 64 |
| 72 // Map of outstanding OnMoreDataResampler objects. A new object is created | 65 // Map of outstanding OnMoreDataConverter objects. A new object is created |
| 73 // on every StartStream() call and destroyed on CloseStream(). | 66 // on every StartStream() call and destroyed on CloseStream(). |
| 74 typedef std::map<AudioOutputProxy*, OnMoreDataResampler*> CallbackMap; | 67 typedef std::map<AudioOutputProxy*, OnMoreDataConverter*> CallbackMap; |
| 75 CallbackMap callbacks_; | 68 CallbackMap callbacks_; |
| 76 | 69 |
| 77 // Ratio of input bytes to output bytes used to correct playback delay with | |
| 78 // regard to buffering and resampling. | |
| 79 double io_ratio_; | |
| 80 | |
| 81 // Used by AudioOutputDispatcherImpl; kept so we can reinitialize on the fly. | 70 // Used by AudioOutputDispatcherImpl; kept so we can reinitialize on the fly. |
| 82 base::TimeDelta close_delay_; | 71 base::TimeDelta close_delay_; |
| 83 | 72 |
| 84 // AudioParameters used to setup the output stream. | 73 // AudioParameters used to setup the output stream. |
| 85 AudioParameters output_params_; | 74 AudioParameters output_params_; |
| 86 | 75 |
| 87 // Whether any streams have been opened through |dispatcher_|, if so we can't | 76 // Whether any streams have been opened through |dispatcher_|, if so we can't |
| 88 // fallback on future OpenStream() failures. | 77 // fallback on future OpenStream() failures. |
| 89 bool streams_opened_; | 78 bool streams_opened_; |
| 90 | 79 |
| 91 DISALLOW_COPY_AND_ASSIGN(AudioOutputResampler); | 80 DISALLOW_COPY_AND_ASSIGN(AudioOutputResampler); |
| 92 }; | 81 }; |
| 93 | 82 |
| 94 } // namespace media | 83 } // namespace media |
| 95 | 84 |
| 96 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_ | 85 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_ |
| OLD | NEW |