9785b106bc7a0d24d0daf7860a04614be41cd782
[platform/framework/web/crosswalk.git] / src / media / base / audio_buffer_converter.h
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef MEDIA_BASE_AUDIO_BUFFER_CONVERTER
6 #define MEDIA_BASE_AUDIO_BUFFER_CONVERTER
7
8 #include <deque>
9
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/time/time.h"
13 #include "media/audio/audio_parameters.h"
14 #include "media/base/audio_converter.h"
15 #include "media/base/audio_timestamp_helper.h"
16 #include "media/base/media_export.h"
17
18 namespace media {
19
20 class AudioBuffer;
21 class AudioBus;
22
23 // Takes AudioBuffers in any format and uses an AudioConverter to convert them
24 // to a common format (usually the hardware output format).
25 class MEDIA_EXPORT AudioBufferConverter : public AudioConverter::InputCallback {
26  public:
27   explicit AudioBufferConverter(const AudioParameters& output_params);
28   virtual ~AudioBufferConverter();
29
30   void AddInput(const scoped_refptr<AudioBuffer>& buffer);
31
32   // Is an output buffer available via GetNextBuffer()?
33   bool HasNextBuffer();
34
35   // This should only be called this is HasNextBuffer() returns true.
36   scoped_refptr<AudioBuffer> GetNextBuffer();
37
38   // Reset internal state.
39   void Reset();
40
41   // Reset internal timestamp state. Upon the next AddInput() call, our base
42   // timestamp will be set to match the input buffer.
43   void ResetTimestampState();
44
45  private:
46   // Callback to provide data to the AudioConverter
47   virtual double ProvideInput(AudioBus* audio_bus,
48                               base::TimeDelta buffer_delay) OVERRIDE;
49
50   // Reset the converter in response to a configuration change.
51   void ResetConverter(const scoped_refptr<AudioBuffer>& input_buffer);
52
53   // Perform conversion if we have enough data.
54   void ConvertIfPossible();
55
56   // Flush remaining output
57   void Flush();
58
59   // The output parameters.
60   AudioParameters output_params_;
61
62   // The current input parameters (we cache these to detect configuration
63   // changes, so we know when to reset the AudioConverter).
64   AudioParameters input_params_;
65
66   typedef std::deque<scoped_refptr<AudioBuffer> > BufferQueue;
67
68   // Queued up inputs (there will never be all that much data stored here, as
69   // soon as there's enough here to produce an output buffer we will do so).
70   BufferQueue queued_inputs_;
71
72   // Offset into the front element of |queued_inputs_|. A ProvideInput() call
73   // doesn't necessarily always consume an entire buffer.
74   int last_input_buffer_offset_;
75
76   // Buffer of output frames, to be returned by GetNextBuffer().
77   BufferQueue queued_outputs_;
78
79   // How many frames of input we have in |queued_inputs_|.
80   int input_frames_;
81
82   // Input frames in the AudioConverter's internal buffers.
83   double buffered_input_frames_;
84
85   // Ratio of sample rates, in/out.
86   double io_sample_rate_ratio_;
87
88   // Computes timestamps in terms of the output sample rate.
89   AudioTimestampHelper timestamp_helper_;
90
91   // Are we flushing everything, without regard for providing AudioConverter
92   // full AudioBuses in ProvideInput()?
93   bool is_flushing_;
94
95   // The AudioConverter which does the real work here.
96   scoped_ptr<AudioConverter> audio_converter_;
97 };
98
99 }  // namespace media
100
101 #endif  // MEDIA_BASE_AUDIO_BUFFER_CONVERTER