• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_BLOCK_FIFO_H_
6 #define MEDIA_BASE_AUDIO_BLOCK_FIFO_H_
7 
8 #include "base/memory/scoped_vector.h"
9 #include "media/base/audio_bus.h"
10 #include "media/base/media_export.h"
11 
12 namespace media {
13 
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 {
20  public:
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();
25 
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);
30 
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();
35 
36   // Empties the FIFO without deallocating any memory.
37   void Clear();
38 
39   // Number of available block of memory ready to be consumed in the FIFO.
available_blocks()40   int available_blocks() const { return available_blocks_; }
41 
42   // Number of available frames of data in the FIFO.
43   int GetAvailableFrames() const;
44 
45   // Number of unfilled frames in the whole FIFO.
46   int GetUnfilledFrames() const;
47 
48   // Dynamically increase |blocks| of memory to the FIFO.
49   void IncreaseCapacity(int blocks);
50 
51  private:
52   // The actual FIFO is a vector of audio buses.
53   ScopedVector<AudioBus> audio_blocks_;
54 
55   // Number of channels in AudioBus.
56   const int channels_;
57 
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_;
61 
62   // Used to keep track which block of memory to be written.
63   int write_block_;
64 
65   // Used to keep track which block of memory to be consumed.
66   int read_block_;
67 
68   // Number of available blocks of memory to be consumed.
69   int available_blocks_;
70 
71   // Current write position in the current written block.
72   int write_pos_;
73 
74   DISALLOW_COPY_AND_ASSIGN(AudioBlockFifo);
75 };
76 
77 }  // namespace media
78 
79 #endif  // MEDIA_BASE_AUDIO_BLOCK_FIFO_H_
80