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 #include "media/base/audio_fifo.h" | 5 #include "media/base/audio_fifo.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
9 namespace media { | 9 namespace media { |
10 | 10 |
(...skipping 21 matching lines...) Expand all Loading... |
32 | 32 |
33 // Updates the read/write position with |step| modulo the maximum number of | 33 // Updates the read/write position with |step| modulo the maximum number of |
34 // elements in the FIFO to ensure that the position counters wraps around at | 34 // elements in the FIFO to ensure that the position counters wraps around at |
35 // the endpoint. | 35 // the endpoint. |
36 static int UpdatePos(int pos, int step, int max_size) { | 36 static int UpdatePos(int pos, int step, int max_size) { |
37 return ((pos + step) % max_size); | 37 return ((pos + step) % max_size); |
38 } | 38 } |
39 | 39 |
40 AudioFifo::AudioFifo(int channels, int frames) | 40 AudioFifo::AudioFifo(int channels, int frames) |
41 : audio_bus_(AudioBus::Create(channels, frames)), | 41 : audio_bus_(AudioBus::Create(channels, frames)), |
42 max_frames_in_fifo_(frames), | 42 max_frames_(frames), |
43 frames_in_fifo_(0), | 43 frames_(0), |
44 read_pos_(0), | 44 read_pos_(0), |
45 write_pos_(0) {} | 45 write_pos_(0) {} |
46 | 46 |
47 AudioFifo::~AudioFifo() {} | 47 AudioFifo::~AudioFifo() {} |
48 | 48 |
49 bool AudioFifo::Push(const AudioBus* source) { | 49 void AudioFifo::Push(const AudioBus* source) { |
50 DCHECK(source); | 50 DCHECK(source); |
51 DCHECK_EQ(source->channels(), audio_bus_->channels()); | 51 DCHECK_EQ(source->channels(), audio_bus_->channels()); |
52 | 52 |
53 // Ensure that there is space for the new data in the FIFO. | 53 // Ensure that there is space for the new data in the FIFO. |
54 const int source_size = source->frames(); | 54 const int source_size = source->frames(); |
55 if (frames_in_fifo_ + source_size > max_frames()) { | 55 CHECK_LE(source_size + frames_, max_frames_); |
56 DLOG(ERROR) << "FIFO overflow."; | |
57 return false; | |
58 } | |
59 | 56 |
60 // Figure out if wrapping is needed and if so what segment sizes we need | 57 // Figure out if wrapping is needed and if so what segment sizes we need |
61 // when adding the new audio bus content to the FIFO. | 58 // when adding the new audio bus content to the FIFO. |
62 int append_size = 0; | 59 int append_size = 0; |
63 int wrap_size = 0; | 60 int wrap_size = 0; |
64 GetSizes(write_pos_, max_frames(), source_size, &append_size, &wrap_size); | 61 GetSizes(write_pos_, max_frames(), source_size, &append_size, &wrap_size); |
65 | 62 |
66 // Copy all channels from the source to the FIFO. Wrap around if needed. | 63 // Copy all channels from the source to the FIFO. Wrap around if needed. |
67 for (int ch = 0; ch < source->channels(); ++ch) { | 64 for (int ch = 0; ch < source->channels(); ++ch) { |
68 float* dest = audio_bus_->channel(ch); | 65 float* dest = audio_bus_->channel(ch); |
69 const float* src = source->channel(ch); | 66 const float* src = source->channel(ch); |
70 | 67 |
71 // Append part of (or the complete) source to the FIFO. | 68 // Append part of (or the complete) source to the FIFO. |
72 memcpy(&dest[write_pos_], &src[0], append_size * sizeof(src[0])); | 69 memcpy(&dest[write_pos_], &src[0], append_size * sizeof(src[0])); |
73 if (wrap_size > 0) { | 70 if (wrap_size > 0) { |
74 // Wrapping is needed: copy remaining part from the source to the FIFO. | 71 // Wrapping is needed: copy remaining part from the source to the FIFO. |
75 memcpy(&dest[0], &src[append_size], wrap_size * sizeof(src[0])); | 72 memcpy(&dest[0], &src[append_size], wrap_size * sizeof(src[0])); |
76 } | 73 } |
77 } | 74 } |
78 | 75 |
79 frames_in_fifo_ += source_size; | 76 frames_ += source_size; |
80 DCHECK_LE(frames_in_fifo_, max_frames()); | 77 DCHECK_LE(frames_, max_frames()); |
81 write_pos_ = UpdatePos(write_pos_, source_size, max_frames()); | 78 write_pos_ = UpdatePos(write_pos_, source_size, max_frames()); |
82 return true; | |
83 } | 79 } |
84 | 80 |
85 bool AudioFifo::Consume(AudioBus* destination, int frames_to_consume) { | 81 |
| 82 void AudioFifo::Consume(AudioBus* destination, |
| 83 int start_frame, |
| 84 int frames_to_consume) { |
86 DCHECK(destination); | 85 DCHECK(destination); |
87 DCHECK_EQ(destination->channels(), audio_bus_->channels()); | 86 DCHECK_EQ(destination->channels(), audio_bus_->channels()); |
88 | 87 |
89 // It is not possible to ask for more data than what is available in the FIFO. | 88 // It is not possible to ask for more data than what is available in the FIFO. |
90 if (frames_to_consume > frames_in_fifo_) { | 89 CHECK_LE(frames_to_consume, frames_); |
91 DLOG(ERROR) << "FIFO underrun."; | |
92 return false; | |
93 } | |
94 | 90 |
95 // A copy from the FIFO to |destination| will only be performed if the | 91 // A copy from the FIFO to |destination| will only be performed if the |
96 // allocated memory in |destination| is sufficient. | 92 // allocated memory in |destination| is sufficient. |
97 if (frames_to_consume > destination->frames()) { | 93 CHECK_LE(frames_to_consume + start_frame, destination->frames()); |
98 DLOG(ERROR) << "Insufficient space in destination."; | |
99 return false; | |
100 } | |
101 | 94 |
102 // Figure out if wrapping is needed and if so what segment sizes we need | 95 // Figure out if wrapping is needed and if so what segment sizes we need |
103 // when removing audio bus content from the FIFO. | 96 // when removing audio bus content from the FIFO. |
104 int consume_size = 0; | 97 int consume_size = 0; |
105 int wrap_size = 0; | 98 int wrap_size = 0; |
106 GetSizes(read_pos_, max_frames(), frames_to_consume, | 99 GetSizes(read_pos_, max_frames(), frames_to_consume, |
107 &consume_size, &wrap_size); | 100 &consume_size, &wrap_size); |
108 | 101 |
109 // For all channels, remove the requested amount of data from the FIFO | 102 // For all channels, remove the requested amount of data from the FIFO |
110 // and copy the content to the destination. Wrap around if needed. | 103 // and copy the content to the destination. Wrap around if needed. |
111 for (int ch = 0; ch < destination->channels(); ++ch) { | 104 for (int ch = 0; ch < destination->channels(); ++ch) { |
112 float* dest = destination->channel(ch); | 105 float* dest = destination->channel(ch); |
113 const float* src = audio_bus_->channel(ch); | 106 const float* src = audio_bus_->channel(ch); |
114 | 107 |
115 // Copy a selected part of the FIFO to the destination. | 108 // Copy a selected part of the FIFO to the destination. |
116 memcpy(&dest[0], &src[read_pos_], consume_size * sizeof(src[0])); | 109 memcpy(&dest[start_frame], &src[read_pos_], consume_size * sizeof(src[0])); |
117 if (wrap_size > 0) { | 110 if (wrap_size > 0) { |
118 // Wrapping is needed: copy remaining part to the destination. | 111 // Wrapping is needed: copy remaining part to the destination. |
119 memcpy(&dest[consume_size], &src[0], wrap_size * sizeof(src[0])); | 112 memcpy(&dest[consume_size + start_frame], &src[0], |
| 113 wrap_size * sizeof(src[0])); |
120 } | 114 } |
121 } | 115 } |
122 | 116 |
123 frames_in_fifo_ -= frames_to_consume; | 117 frames_ -= frames_to_consume; |
124 read_pos_ = UpdatePos(read_pos_, frames_to_consume, max_frames()); | 118 read_pos_ = UpdatePos(read_pos_, frames_to_consume, max_frames()); |
125 return true; | |
126 } | 119 } |
127 | 120 |
128 void AudioFifo::Clear() { | 121 void AudioFifo::Clear() { |
129 frames_in_fifo_ = 0; | 122 frames_ = 0; |
130 read_pos_ = 0; | 123 read_pos_ = 0; |
131 write_pos_ = 0; | 124 write_pos_ = 0; |
132 } | 125 } |
133 | 126 |
134 } // namespace media | 127 } // namespace media |
OLD | NEW |