audio_block_fifo.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file.
4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#ifndef MEDIA_BASE_AUDIO_BLOCK_FIFO_H_
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#define MEDIA_BASE_AUDIO_BLOCK_FIFO_H_
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/memory/scoped_vector.h"
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "media/base/audio_bus.h"
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "media/base/media_export.h"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace media {
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// First-in first-out container for AudioBus elements.
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// The FIFO is composed of blocks of AudioBus elements, it accepts interleaved
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// data as input and will deinterleave it into the FIFO, and it only allows
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// consuming a whole block of AudioBus element.
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// This class is thread-unsafe.
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass MEDIA_EXPORT AudioBlockFifo {
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch public:
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Creates a new AudioBlockFifo and allocates |blocks| memory, each block
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // of memory can store |channels| of length |frames| data.
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  AudioBlockFifo(int channels, int frames, int blocks);
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual ~AudioBlockFifo();
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Pushes interleaved audio data from |source| to the FIFO.
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // The method will deinterleave the data into a audio bus.
28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Push() will crash if the allocated space is insufficient.
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void Push(const void* source, int frames, int bytes_per_sample);
30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Consumes a block of audio from the FIFO.  Returns an AudioBus which
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // contains the consumed audio data to avoid copying.
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Consume() will crash if the FIFO does not contain a block of data.
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const AudioBus* Consume();
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Empties the FIFO without deallocating any memory.
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void Clear();
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Number of available block of memory ready to be consumed in the FIFO.
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int available_blocks() const { return available_blocks_; }
41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Number of available frames of data in the FIFO.
435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  int GetAvailableFrames() const;
445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Number of unfilled frames in the whole FIFO.
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int GetUnfilledFrames() const;
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Dynamically increase |blocks| of memory to the FIFO.
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void IncreaseCapacity(int blocks);
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch private:
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // The actual FIFO is a vector of audio buses.
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ScopedVector<AudioBus> audio_blocks_;
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Number of channels in AudioBus.
561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  const int channels_;
571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Maximum number of frames of data one block of memory can contain.
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // This value is set by |frames| in the constructor.
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const int block_frames_;
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Used to keep track which block of memory to be written.
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int write_block_;
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Used to keep track which block of memory to be consumed.
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int read_block_;
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Number of available blocks of memory to be consumed.
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int available_blocks_;
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Current write position in the current written block.
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int write_pos_;
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DISALLOW_COPY_AND_ASSIGN(AudioBlockFifo);
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch};
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace media
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif  // MEDIA_BASE_AUDIO_BLOCK_FIFO_H_
80