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.
5 #ifndef MEDIA_BASE_AUDIO_BLOCK_FIFO_H_
6 #define MEDIA_BASE_AUDIO_BLOCK_FIFO_H_
8 #include "base/memory/scoped_vector.h"
9 #include "media/base/audio_bus.h"
10 #include "media/base/media_export.h"
14 // First-in first-out container for AudioBus elements.
15 // The FIFO is composed of blocks of AudioBus elements, it accepts interleaved
16 // data as input and will deinterleave it into the FIFO, and it only allows
17 // consuming a whole block of AudioBus element.
18 // This class is thread-unsafe.
19 class MEDIA_EXPORT AudioBlockFifo
{
21 // Creates a new AudioBlockFifo and allocates |blocks| memory, each block
22 // of memory can store |channels| of length |frames| data.
23 AudioBlockFifo(int channels
, int frames
, int blocks
);
24 virtual ~AudioBlockFifo();
26 // Pushes interleaved audio data from |source| to the FIFO.
27 // The method will deinterleave the data into a audio bus.
28 // Push() will crash if the allocated space is insufficient.
29 void Push(const void* source
, int frames
, int bytes_per_sample
);
31 // Consumes a block of audio from the FIFO. Returns an AudioBus which
32 // contains the consumed audio data to avoid copying.
33 // Consume() will crash if the FIFO does not contain a block of data.
34 const AudioBus
* Consume();
36 // Empties the FIFO without deallocating any memory.
39 // Number of available block of memory ready to be consumed in the FIFO.
40 int available_blocks() const { return available_blocks_
; }
42 // Number of available frames of data in the FIFO.
43 int GetAvailableFrames() const;
45 // Number of unfilled frames in the whole FIFO.
46 int GetUnfilledFrames() const;
48 // Dynamically increase |blocks| of memory to the FIFO.
49 void IncreaseCapacity(int blocks
);
52 // The actual FIFO is a vector of audio buses.
53 ScopedVector
<AudioBus
> audio_blocks_
;
55 // Number of channels in AudioBus.
58 // Maximum number of frames of data one block of memory can contain.
59 // This value is set by |frames| in the constructor.
60 const int block_frames_
;
62 // Used to keep track which block of memory to be written.
65 // Used to keep track which block of memory to be consumed.
68 // Number of available blocks of memory to be consumed.
69 int available_blocks_
;
71 // Current write position in the current written block.
74 DISALLOW_COPY_AND_ASSIGN(AudioBlockFifo
);
79 #endif // MEDIA_BASE_AUDIO_BLOCK_FIFO_H_