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_H_
6 #define MEDIA_BASE_AUDIO_BUFFER_CONVERTER_H_
7 
8 #include <memory>
9 
10 #include "base/containers/circular_deque.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/time/time.h"
13 #include "media/base/audio_buffer.h"
14 #include "media/base/audio_converter.h"
15 #include "media/base/audio_parameters.h"
16 #include "media/base/audio_timestamp_helper.h"
17 #include "media/base/media_export.h"
18 
19 namespace media {
20 
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   ~AudioBufferConverter() override;
29 
30   void AddInput(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 
input_buffer_size_for_testing()45   int input_buffer_size_for_testing() const {
46     return input_params_.frames_per_buffer();
47   }
input_frames_left_for_testing()48   int input_frames_left_for_testing() const {
49     return input_frames_;
50   }
51 
52  private:
53   // Callback to provide data to the AudioConverter
54   double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override;
55 
56   // Reset the converter in response to a configuration change.
57   void ResetConverter(const AudioBuffer& input_buffer);
58 
59   // Perform conversion if we have enough data.
60   void ConvertIfPossible();
61 
62   // Flush remaining output
63   void Flush();
64 
65   // The output parameters.
66   AudioParameters output_params_;
67 
68   // The current input parameters (we cache these to detect configuration
69   // changes, so we know when to reset the AudioConverter).
70   AudioParameters input_params_;
71 
72   using BufferQueue = base::circular_deque<scoped_refptr<AudioBuffer>>;
73 
74   // Queued up inputs (there will never be all that much data stored here, as
75   // soon as there's enough here to produce an output buffer we will do so).
76   BufferQueue queued_inputs_;
77 
78   // Offset into the front element of |queued_inputs_|. A ProvideInput() call
79   // doesn't necessarily always consume an entire buffer.
80   int last_input_buffer_offset_;
81 
82   // Buffer of output frames, to be returned by GetNextBuffer().
83   BufferQueue queued_outputs_;
84 
85   // How many frames of input we have in |queued_inputs_|.
86   int input_frames_;
87 
88   // Input frames in the AudioConverter's internal buffers.
89   double buffered_input_frames_;
90 
91   // Ratio of sample rates, in/out.
92   double io_sample_rate_ratio_;
93 
94   // Computes timestamps in terms of the output sample rate.
95   AudioTimestampHelper timestamp_helper_;
96 
97   // Are we flushing everything, without regard for providing AudioConverter
98   // full AudioBuses in ProvideInput()?
99   bool is_flushing_;
100 
101   // Pool to avoid thrashing memory when allocating AudioBuffers.
102   scoped_refptr<AudioBufferMemoryPool> pool_;
103 
104   // The AudioConverter which does the real work here.
105   std::unique_ptr<AudioConverter> audio_converter_;
106 };
107 
108 }  // namespace media
109 
110 #endif  // MEDIA_BASE_AUDIO_BUFFER_CONVERTER_H_
111