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