Index: media/base/audio_rechunker.h |
diff --git a/media/base/audio_rechunker.h b/media/base/audio_rechunker.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8099e1185808e88daad4e5cad4062a5db37aef69 |
--- /dev/null |
+++ b/media/base/audio_rechunker.h |
@@ -0,0 +1,73 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef MEDIA_BASE_AUDIO_RECHUNKER_H_ |
+#define MEDIA_BASE_AUDIO_RECHUNKER_H_ |
+ |
+#include "base/callback.h" |
+#include "base/macros.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/time/time.h" |
+#include "media/base/audio_bus.h" |
+#include "media/base/media_export.h" |
+ |
+namespace media { |
+ |
+// Yet another FIFO for audio data that re-chunks audio to a desired buffer |
+// size. Unlike AudioFifo and AudioBlockFifo, this FIFO cannot overflow: The |
+// client is required to provide a callback that is called synchronously during |
+// a push whenever enough data becomes available. In addition, this FIFO |
+// shifts reference timestamps to account for any samples that were pending in |
+// internal buffers from prior pushes. This implementation eliminates redundant |
+// memory copies when the input buffer size always matches the desired buffer |
+// size. |
+class MEDIA_EXPORT AudioRechunker final { |
+ public: |
+ using RechunkedAudioCallback = |
+ base::Callback<void(const AudioBus& output, |
+ base::TimeDelta reference_timestamp)>; |
+ |
+ // Creates a new AudioRechunker that re-chunks audio into the required |
+ // |output_duration| and delivers it by running |callback|. |
+ AudioRechunker(base::TimeDelta output_duration, |
+ const RechunkedAudioCallback& callback); |
+ |
+ ~AudioRechunker(); |
+ |
+ int sample_rate() const { return sample_rate_; } |
+ |
+ // Returns the number of frames in each AudioBus delivered to the |
+ // RechunkedAudioCallback. |
+ int output_frames() const { return output_frames_; } |
+ |
+ // Must be called at least once before the first call to Push(). May be |
+ // called later just before each audio format change. Returns true if it is |
+ // possible to EXACTLY re-chunk audio to the required |output_duration| |
+ // provided to the constructor, false otherwise (e.g., 22050 Hz audio cannot |
+ // be divided evenly into 10 ms chunks). |
+ bool SetSampleRate(int sample_rate); |
+ |
+ // Pushes all audio channel data from |input_bus| through the FIFO. This will |
+ // result in zero, one, or multiple synchronous calls to the |
+ // RechunkedAudioCallback provided in the constructor. |
+ void Push(const AudioBus& input_bus, base::TimeDelta reference_timestamp); |
+ |
+ private: |
+ const base::TimeDelta output_duration_; |
+ const RechunkedAudioCallback callback_; |
+ |
+ // These are computed in each call to SetSampleRate(). |
+ int sample_rate_; |
+ int output_frames_; |
+ |
+ // Queue of frames pending for delivery. |
+ scoped_ptr<AudioBus> audio_queue_; |
+ int pending_frames_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(AudioRechunker); |
+}; |
+ |
+} // namespace media |
+ |
+#endif // MEDIA_BASE_AUDIO_RECHUNKER_H_ |