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" |
| 8 #include "base/logging.h" |
| 9 |
| 10 namespace media { |
| 11 |
| 12 static void GetSizes( |
| 13 int pos, int max_size, int src_size, int* size, int* wrap_size) { |
| 14 if (pos + src_size > max_size) { |
| 15 // Wrapping is required => derive size of each segment. |
| 16 *size = max_size - pos; |
| 17 *wrap_size = src_size - *size; |
| 18 } else { |
| 19 // Wrapping is not required. |
| 20 *size = src_size; |
| 21 *wrap_size = 0; |
| 22 } |
| 23 } |
| 24 |
| 25 static int UpdatePos(int pos, int step, int max_size) { |
| 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) { |
| 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 const int dest_size = destination->frames(); |
| 80 if (dest_size > frames_in_fifo_) { |
| 81 DLOG(ERROR) << "FIFO underrun."; |
| 82 return false; |
| 83 } |
| 84 |
| 85 // Figure out if wrapping is needed and if so what segment sizes we need |
| 86 // when removing audio bus content from the FIFO. |
| 87 int remove_size = 0; |
| 88 int wrap_size = 0; |
| 89 GetSizes(read_pos_, max_frames(), dest_size, &remove_size, &wrap_size); |
| 90 |
| 91 // For all channels, remove the requested amount of data from the FIFO |
| 92 // and copy the content to the destination. Wrap around if needed. |
| 93 for (int ch = 0; ch < destination->channels(); ++ch) { |
| 94 float* dest = destination->channel(ch); |
| 95 const float* src = audio_bus_->channel(ch); |
| 96 |
| 97 // Append part of (or the complete) source to the destination. |
| 98 memcpy(&dest[0], &src[read_pos_], remove_size * sizeof(src[0])); |
| 99 if (wrap_size > 0) { |
| 100 // Wrapping is needed: copy remaining part to the destination. |
| 101 memcpy(&dest[wrap_size], &src[0], wrap_size * sizeof(src[0])); |
| 102 } |
| 103 } |
| 104 |
| 105 frames_in_fifo_ -= dest_size; |
| 106 read_pos_ = UpdatePos(read_pos_, dest_size, max_frames()); |
| 107 return true; |
| 108 } |
| 109 |
| 110 void AudioFifo::Clear() { |
| 111 frames_in_fifo_ = 0; |
| 112 read_pos_ = 0; |
| 113 write_pos_ = 0; |
| 114 } |
| 115 |
| 116 } // namespace media |
OLD | NEW |