OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_BASE_AUDIO_FIFO_H_ | 5 #ifndef MEDIA_BASE_AUDIO_FIFO_H_ |
6 #define MEDIA_BASE_AUDIO_FIFO_H_ | 6 #define MEDIA_BASE_AUDIO_FIFO_H_ |
7 | 7 |
| 8 #include "base/atomicops.h" |
8 #include "media/base/audio_bus.h" | 9 #include "media/base/audio_bus.h" |
9 #include "media/base/media_export.h" | 10 #include "media/base/media_export.h" |
10 | 11 |
11 namespace media { | 12 namespace media { |
12 | 13 |
13 // First-in first-out container for AudioBus elements. | 14 // First-in first-out container for AudioBus elements. |
14 // The maximum number of audio frames in the FIFO is set at construction and | 15 // The maximum number of audio frames in the FIFO is set at construction and |
15 // can not be extended dynamically. The allocated memory is utilized as a | 16 // can not be extended dynamically. The allocated memory is utilized as a |
16 // ring buffer. | 17 // ring buffer. |
| 18 // This class is thread-safe in the limited sense that one thread may call |
| 19 // Push(), while a second thread calls Consume(). |
17 class MEDIA_EXPORT AudioFifo { | 20 class MEDIA_EXPORT AudioFifo { |
18 public: | 21 public: |
19 // Creates a new AudioFifo and allocates |channels| of length |frames|. | 22 // Creates a new AudioFifo and allocates |channels| of length |frames|. |
20 AudioFifo(int channels, int frames); | 23 AudioFifo(int channels, int frames); |
21 virtual ~AudioFifo(); | 24 virtual ~AudioFifo(); |
22 | 25 |
23 // Pushes all audio channel data from |source| to the FIFO. | 26 // Pushes all audio channel data from |source| to the FIFO. |
24 // Push() will crash if the allocated space is insufficient. | 27 // Push() will crash if the allocated space is insufficient. |
25 void Push(const AudioBus* source); | 28 void Push(const AudioBus* source); |
26 | 29 |
27 // Consumes |frames_to_consume| audio frames from the FIFO and copies | 30 // Consumes |frames_to_consume| audio frames from the FIFO and copies |
28 // them to |destination| starting at position |start_frame|. | 31 // them to |destination| starting at position |start_frame|. |
29 // Consume() will crash if the FIFO does not contain |frames_to_consume| | 32 // Consume() will crash if the FIFO does not contain |frames_to_consume| |
30 // frames or if there is insufficient space in |destination| to store the | 33 // frames or if there is insufficient space in |destination| to store the |
31 // frames. | 34 // frames. |
32 void Consume(AudioBus* destination, int start_frame, int frames_to_consume); | 35 void Consume(AudioBus* destination, int start_frame, int frames_to_consume); |
33 | 36 |
34 // Empties the FIFO without deallocating any memory. | 37 // Empties the FIFO without deallocating any memory. |
35 void Clear(); | 38 void Clear(); |
36 | 39 |
37 // Number of actual audio frames in the FIFO. | 40 // Number of actual audio frames in the FIFO. |
38 int frames() const { return frames_; } | 41 int frames() const; |
| 42 |
| 43 int max_frames() const { return max_frames_; } |
39 | 44 |
40 private: | 45 private: |
41 int max_frames() const { return max_frames_; } | |
42 | |
43 // The actual FIFO is an audio bus implemented as a ring buffer. | 46 // The actual FIFO is an audio bus implemented as a ring buffer. |
44 scoped_ptr<AudioBus> audio_bus_; | 47 scoped_ptr<AudioBus> audio_bus_; |
45 | 48 |
46 // Maximum number of elements the FIFO can contain. | 49 // Maximum number of elements the FIFO can contain. |
47 // This value is set by |frames| in the constructor. | 50 // This value is set by |frames| in the constructor. |
48 const int max_frames_; | 51 const int max_frames_; |
49 | 52 |
50 // Number of actual elements in the FIFO. | 53 // Number of actual elements in the FIFO. |
51 int frames_; | 54 volatile base::subtle::Atomic32 frames_pushed_; |
| 55 volatile base::subtle::Atomic32 frames_consumed_; |
52 | 56 |
53 // Current read position. | 57 // Current read position. |
54 int read_pos_; | 58 int read_pos_; |
55 | 59 |
56 // Current write position. | 60 // Current write position. |
57 int write_pos_; | 61 int write_pos_; |
58 | 62 |
59 DISALLOW_COPY_AND_ASSIGN(AudioFifo); | 63 DISALLOW_COPY_AND_ASSIGN(AudioFifo); |
60 }; | 64 }; |
61 | 65 |
62 } // namespace media | 66 } // namespace media |
63 | 67 |
64 #endif // MEDIA_BASE_AUDIO_FIFO_H_ | 68 #endif // MEDIA_BASE_AUDIO_FIFO_H_ |
OLD | NEW |