OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "media/base/audio_fifo.h" | |
6 | |
7 #include "media/audio/audio_parameters.h" | |
DaleCurtis
2012/09/06 11:14:13
alphabetical order (should be below base include)
henrika (OOO until Aug 14)
2012/09/06 11:52:46
Fixed. Don't need this one at all actually.
scherkus (not reviewing)
2012/09/06 13:53:57
a->z ordering
henrika (OOO until Aug 14)
2012/09/07 14:10:37
Done.
| |
8 #include "base/logging.h" | |
9 | |
10 namespace media { | |
11 | |
12 static void GetSizes( | |
DaleCurtis
2012/09/06 11:14:13
Docs.
henrika (OOO until Aug 14)
2012/09/06 11:52:46
Done.
scherkus (not reviewing)
2012/09/06 13:53:57
docs please
henrika (OOO until Aug 14)
2012/09/07 14:10:37
Done.
| |
13 int pos, int max_size, int in_size, int* size, int* wrap_size) { | |
14 if (pos + in_size > max_size) { | |
15 // Wrapping is required => derive size of each segment. | |
16 *size = max_size - pos; | |
17 *wrap_size = in_size - *size; | |
18 } else { | |
19 // Wrapping is not required. | |
20 *size = in_size; | |
21 *wrap_size = 0; | |
22 } | |
23 } | |
24 | |
25 static int UpdatePos(int pos, int step, int max_size) { | |
DaleCurtis
2012/09/06 11:14:13
Docs.
henrika (OOO until Aug 14)
2012/09/06 11:52:46
Done.
scherkus (not reviewing)
2012/09/06 13:53:57
docs
henrika (OOO until Aug 14)
2012/09/07 14:10:37
Done.
| |
26 return ((pos + step) % max_size); | |
27 } | |
28 | |
29 AudioFifo::AudioFifo(int channels, int frames) | |
30 : audio_bus_(AudioBus::Create(channels, frames)), | |
31 max_frames_in_fifo_(frames), | |
32 frames_in_fifo_(0), | |
33 read_pos_(0), | |
34 write_pos_(0) {} | |
35 | |
36 AudioFifo::~AudioFifo() {} | |
37 | |
38 bool AudioFifo::Push(const AudioBus* source) { | |
39 DCHECK(source); | |
40 DCHECK_EQ(source->channels(), audio_bus_->channels()); | |
41 | |
42 // Ensure that there is space for the new data in the FIFO. | |
43 const int source_size = source->frames(); | |
44 if (frames_in_fifo_ + source_size > max_frames()) { | |
45 DLOG(ERROR) << "FIFO overflow."; | |
46 return false; | |
47 } | |
48 | |
49 // Figure out if wrapping is needed and if so what segment sizes we need | |
50 // when adding the new audio bus content to the FIFO. | |
51 int append_size = 0; | |
52 int wrap_size = 0; | |
53 GetSizes(write_pos_, max_frames(), source_size, &append_size, &wrap_size); | |
54 | |
55 // Copy all channels from the source to the FIFO. Wrap around if needed. | |
56 for (int ch = 0; ch < source->channels(); ++ch) { | |
57 float* dest = audio_bus_->channel(ch); | |
58 const float* src = source->channel(ch); | |
59 | |
60 // Append part of (or the complete) source to the FIFO. | |
61 memcpy(&dest[write_pos_], &src[0], append_size * sizeof(src[0])); | |
62 if (wrap_size > 0) { | |
63 // Wrapping is needed: copy remaining part from the source to the FIFO. | |
64 memcpy(&dest[0], &src[append_size], wrap_size * sizeof(src[0])); | |
65 } | |
66 } | |
67 | |
68 frames_in_fifo_ += source_size; | |
69 DCHECK_LE(frames_in_fifo_, max_frames()); | |
70 write_pos_ = UpdatePos(write_pos_, source_size, max_frames()); | |
71 return true; | |
72 } | |
73 | |
74 bool AudioFifo::Consume(AudioBus* destination, int frames_to_consume) { | |
75 DCHECK(destination); | |
76 DCHECK_EQ(destination->channels(), audio_bus_->channels()); | |
77 | |
78 // It is not possible to ask for more data than what is available in the FIFO. | |
79 if (frames_to_consume > frames_in_fifo_) { | |
80 DLOG(ERROR) << "FIFO underrun."; | |
scherkus (not reviewing)
2012/09/06 13:53:57
this is something that I feel we may want to bump
henrika (OOO until Aug 14)
2012/09/07 14:10:37
Got it. Will modify.
| |
81 return false; | |
82 } | |
83 | |
84 // A copy from the FIFO to |destination| will only be performed if the | |
85 // allocated memory in |destination| is sufficient. | |
86 if (frames_to_consume > destination->frames()) { | |
87 DLOG(ERROR) << "Insufficient space in destination."; | |
scherkus (not reviewing)
2012/09/06 13:53:57
ditto
henrika (OOO until Aug 14)
2012/09/07 14:10:37
see above
| |
88 return false; | |
89 } | |
90 | |
91 // Figure out if wrapping is needed and if so what segment sizes we need | |
92 // when removing audio bus content from the FIFO. | |
93 int consume_size = 0; | |
94 int wrap_size = 0; | |
95 GetSizes(read_pos_, max_frames(), frames_to_consume, | |
96 &consume_size, &wrap_size); | |
97 | |
98 // For all channels, remove the requested amount of data from the FIFO | |
99 // and copy the content to the destination. Wrap around if needed. | |
100 for (int ch = 0; ch < destination->channels(); ++ch) { | |
101 float* dest = destination->channel(ch); | |
102 const float* src = audio_bus_->channel(ch); | |
103 | |
104 // Copy a selected part of the FIFO to the destination. | |
105 memcpy(&dest[0], &src[read_pos_], consume_size * sizeof(src[0])); | |
106 if (wrap_size > 0) { | |
107 // Wrapping is needed: copy remaining part to the destination. | |
108 memcpy(&dest[consume_size], &src[0], wrap_size * sizeof(src[0])); | |
109 } | |
110 } | |
111 | |
112 frames_in_fifo_ -= frames_to_consume; | |
113 read_pos_ = UpdatePos(read_pos_, frames_to_consume, max_frames()); | |
114 return true; | |
115 } | |
116 | |
117 void AudioFifo::Clear() { | |
118 frames_in_fifo_ = 0; | |
119 read_pos_ = 0; | |
120 write_pos_ = 0; | |
121 } | |
122 | |
123 } // namespace media | |
OLD | NEW |