1 // Copyright (c) 2012 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_FIFO_H_
6 #define MEDIA_BASE_AUDIO_FIFO_H_
8 #include "base/atomicops.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 maximum number of audio frames in the FIFO is set at construction and
16 // can not be extended dynamically. The allocated memory is utilized as a
18 // This class is thread-safe in the limited sense that one thread may call
19 // Push(), while a second thread calls Consume().
20 class MEDIA_EXPORT AudioFifo
{
22 // Creates a new AudioFifo and allocates |channels| of length |frames|.
23 AudioFifo(int channels
, int frames
);
26 // Pushes all audio channel data from |source| to the FIFO.
27 // Push() will crash if the allocated space is insufficient.
28 void Push(const AudioBus
* source
);
30 // Consumes |frames_to_consume| audio frames from the FIFO and copies
31 // them to |destination| starting at position |start_frame|.
32 // Consume() will crash if the FIFO does not contain |frames_to_consume|
33 // frames or if there is insufficient space in |destination| to store the
35 void Consume(AudioBus
* destination
, int start_frame
, int frames_to_consume
);
37 // Empties the FIFO without deallocating any memory.
40 // Number of actual audio frames in the FIFO.
43 int max_frames() const { return max_frames_
; }
46 // The actual FIFO is an audio bus implemented as a ring buffer.
47 scoped_ptr
<AudioBus
> audio_bus_
;
49 // Maximum number of elements the FIFO can contain.
50 // This value is set by |frames| in the constructor.
51 const int max_frames_
;
53 // Number of actual elements in the FIFO.
54 volatile base::subtle::Atomic32 frames_pushed_
;
55 volatile base::subtle::Atomic32 frames_consumed_
;
57 // Current read position.
60 // Current write position.
63 DISALLOW_COPY_AND_ASSIGN(AudioFifo
);
68 #endif // MEDIA_BASE_AUDIO_FIFO_H_