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/message_loop.h" |
12 #include "base/time.h" | 13 #include "base/time.h" |
13 #include "media/audio/audio_io.h" | 14 #include "media/audio/audio_io.h" |
14 #include "media/audio/audio_manager.h" | 15 #include "media/audio/audio_manager.h" |
15 #include "media/audio/audio_output_dispatcher.h" | 16 #include "media/audio/audio_output_dispatcher.h" |
16 #include "media/audio/audio_parameters.h" | 17 #include "media/audio/audio_parameters.h" |
| 18 #include "media/base/audio_pull_fifo.h" |
| 19 #include "media/base/channel_mixer.h" |
| 20 #include "media/base/multi_channel_resampler.h" |
17 | 21 |
18 namespace media { | 22 namespace media { |
19 | 23 |
20 class OnMoreDataResampler; | 24 class OnMoreDataResampler : public AudioOutputStream::AudioSourceCallback { |
| 25 public: |
| 26 OnMoreDataResampler(double io_ratio, |
| 27 const AudioParameters& input_params, |
| 28 const AudioParameters& output_params); |
| 29 virtual ~OnMoreDataResampler(); |
| 30 |
| 31 // AudioSourceCallback interface. |
| 32 virtual int OnMoreData(AudioBus* dest, |
| 33 AudioBuffersState buffers_state) OVERRIDE; |
| 34 virtual int OnMoreIOData(AudioBus* source, |
| 35 AudioBus* dest, |
| 36 AudioBuffersState buffers_state) OVERRIDE; |
| 37 virtual void OnError(AudioOutputStream* stream, int code) OVERRIDE; |
| 38 virtual void WaitTillDataReady() OVERRIDE; |
| 39 |
| 40 // Sets |source_callback_|. If this is not a new object, then Stop() must be |
| 41 // called before Start(). |
| 42 void Start(AudioOutputStream::AudioSourceCallback* callback); |
| 43 |
| 44 // Clears |source_callback_| and flushes the resampler. |
| 45 void Stop(); |
| 46 |
| 47 private: |
| 48 // Called by MultiChannelResampler when more data is necessary. |
| 49 void ProvideInput(AudioBus* audio_bus); |
| 50 |
| 51 // Called by AudioPullFifo when more data is necessary. Requires |
| 52 // |source_lock_| to have been acquired. |
| 53 void SourceCallback_Locked(AudioBus* audio_bus); |
| 54 |
| 55 // Passes through |source| to the |source_callback_| OnMoreIOData() call. |
| 56 void SourceIOCallback_Locked(AudioBus* source, AudioBus* dest); |
| 57 |
| 58 // Ratio of input bytes to output bytes used to correct playback delay with |
| 59 // regard to buffering and resampling. |
| 60 double io_ratio_; |
| 61 |
| 62 // Source callback and associated lock. |
| 63 base::Lock source_lock_; |
| 64 AudioOutputStream::AudioSourceCallback* source_callback_; |
| 65 |
| 66 // Last AudioBuffersState object received via OnMoreData(), used to correct |
| 67 // playback delay by ProvideInput() and passed on to |source_callback_|. |
| 68 AudioBuffersState current_buffers_state_; |
| 69 |
| 70 // Total number of bytes (in terms of output parameters) stored in resampler |
| 71 // or FIFO buffers which have not been sent to the audio device. |
| 72 int outstanding_audio_bytes_; |
| 73 |
| 74 // Used to buffer data between the client and the output device in cases where |
| 75 // the client buffer size is not the same as the output device buffer size. |
| 76 // Bound to SourceCallback_Locked() so must only be used when |source_lock_| |
| 77 // has already been acquired. |
| 78 scoped_ptr<AudioPullFifo> audio_fifo_; |
| 79 |
| 80 // Handles resampling. |
| 81 scoped_ptr<MultiChannelResampler> resampler_; |
| 82 |
| 83 // Handles channel transforms. |unmixed_audio_| is a temporary destination |
| 84 // for audio data before it goes into the channel mixer. |
| 85 scoped_ptr<ChannelMixer> channel_mixer_; |
| 86 scoped_ptr<AudioBus> unmixed_audio_; |
| 87 |
| 88 int output_bytes_per_frame_; |
| 89 int input_bytes_per_frame_; |
| 90 |
| 91 // Since resampling is expensive, figure out if we should downmix channels |
| 92 // before resampling. |
| 93 bool downmix_early_; |
| 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(OnMoreDataResampler); |
| 96 }; |
21 | 97 |
22 // AudioOutputResampler is a browser-side resampling and rebuffering solution | 98 // AudioOutputResampler is a browser-side resampling and rebuffering solution |
23 // which ensures audio data is always output at given parameters. The rough | 99 // which ensures audio data is always output at given parameters. The rough |
24 // flow is: Client -> [FIFO] -> [Resampler] -> Output Device. | 100 // flow is: Client -> [FIFO] -> [Resampler] -> Output Device. |
25 // | 101 // |
26 // The FIFO and resampler are only used when necessary. To be clear: | 102 // 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. | 103 // - 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 | 104 // - The FIFO is only used if the input and output frame sizes differ or if |
29 // the resampler is used. | 105 // the resampler is used. |
30 // | 106 // |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 // Whether any streams have been opened through |dispatcher_|, if so we can't | 163 // Whether any streams have been opened through |dispatcher_|, if so we can't |
88 // fallback on future OpenStream() failures. | 164 // fallback on future OpenStream() failures. |
89 bool streams_opened_; | 165 bool streams_opened_; |
90 | 166 |
91 DISALLOW_COPY_AND_ASSIGN(AudioOutputResampler); | 167 DISALLOW_COPY_AND_ASSIGN(AudioOutputResampler); |
92 }; | 168 }; |
93 | 169 |
94 } // namespace media | 170 } // namespace media |
95 | 171 |
96 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_ | 172 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_ |
OLD | NEW |