OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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_RECHUNKER_H_ | |
6 #define MEDIA_BASE_AUDIO_RECHUNKER_H_ | |
7 | |
8 #include "base/callback.h" | |
9 #include "base/macros.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/time/time.h" | |
12 #include "media/base/audio_bus.h" | |
13 #include "media/base/media_export.h" | |
14 | |
15 namespace media { | |
16 | |
17 // Yet another FIFO for audio data that re-chunks audio to a desired buffer | |
18 // size. Unlike AudioFifo and AudioBlockFifo, this FIFO cannot overflow: The | |
19 // client is required to provide a callback that is called synchronously during | |
20 // a push whenever enough data becomes available. In addition, this FIFO | |
21 // shifts reference timestamps to account for any samples that were pending in | |
22 // internal buffers from prior pushes. This implementation eliminates redundant | |
23 // memory copies when the input buffer size always matches the desired buffer | |
24 // size. | |
25 class MEDIA_EXPORT AudioRechunker final { | |
26 public: | |
27 using RechunkedAudioCallback = | |
o1ka
2016/02/22 13:04:47
I think that the fact that |output| is of output_f
| |
28 base::Callback<void(const AudioBus& output, | |
29 base::TimeDelta reference_timestamp)>; | |
30 | |
31 // Creates a new AudioRechunker that re-chunks audio into the required | |
32 // |output_duration| and delivers it by running |callback|. | |
33 AudioRechunker(base::TimeDelta output_duration, | |
34 const RechunkedAudioCallback& callback); | |
35 | |
36 ~AudioRechunker(); | |
37 | |
38 int sample_rate() const { return sample_rate_; } | |
39 | |
40 // Returns the number of frames in each AudioBus delivered to the | |
41 // RechunkedAudioCallback. | |
42 int output_frames() const { return output_frames_; } | |
43 | |
44 // Must be called at least once before the first call to Push(). May be | |
45 // called later just before each audio format change. Returns true if it is | |
46 // possible to EXACTLY re-chunk audio to the required |output_duration| | |
47 // provided to the constructor, false otherwise (e.g., 22050 Hz audio cannot | |
48 // be divided evenly into 10 ms chunks). | |
49 bool SetSampleRate(int sample_rate); | |
o1ka
2016/02/22 13:04:47
What are the consequences of |false| return value
| |
50 | |
51 // Pushes all audio channel data from |input_bus| through the FIFO. This will | |
52 // result in zero, one, or multiple synchronous calls to the | |
53 // RechunkedAudioCallback provided in the constructor. | |
54 void Push(const AudioBus& input_bus, base::TimeDelta reference_timestamp); | |
55 | |
56 private: | |
57 const base::TimeDelta output_duration_; | |
58 const RechunkedAudioCallback callback_; | |
59 | |
60 // These are computed in each call to SetSampleRate(). | |
61 int sample_rate_; | |
62 int output_frames_; | |
63 | |
64 // Queue of frames pending for delivery. | |
65 scoped_ptr<AudioBus> audio_queue_; | |
66 int pending_frames_; | |
67 | |
68 DISALLOW_COPY_AND_ASSIGN(AudioRechunker); | |
69 }; | |
70 | |
71 } // namespace media | |
72 | |
73 #endif // MEDIA_BASE_AUDIO_RECHUNKER_H_ | |
OLD | NEW |