audio_block_fifo.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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 12namespace 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. 19class 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. 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 private: 49 // The actual FIFO is a vector of audio buses. 50 ScopedVector<AudioBus> audio_blocks_; 51 52 // Maximum number of frames of data one block of memory can contain. 53 // This value is set by |frames| in the constructor. 54 const int block_frames_; 55 56 // Used to keep track which block of memory to be written. 57 int write_block_; 58 59 // Used to keep track which block of memory to be consumed. 60 int read_block_; 61 62 // Number of available blocks of memory to be consumed. 63 int available_blocks_; 64 65 // Current write position in the current written block. 66 int write_pos_; 67 68 DISALLOW_COPY_AND_ASSIGN(AudioBlockFifo); 69}; 70 71} // namespace media 72 73#endif // MEDIA_BASE_AUDIO_BLOCK_FIFO_H_ 74