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_pull_fifo.h" | 5 #include "media/base/audio_pull_fifo.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "media/base/audio_bus.h" |
10 | 11 |
11 namespace media { | 12 namespace media { |
12 | 13 |
13 AudioPullFifo::AudioPullFifo(int channels, int frames, const ReadCB& read_cb) | 14 AudioPullFifo::AudioPullFifo(int channels, int frames, const ReadCB& read_cb) |
14 : read_cb_(read_cb) { | 15 : read_cb_(read_cb), |
15 fifo_.reset(new AudioFifo(channels, frames)); | 16 fifo_(AudioBus::Create(channels, frames)), |
16 bus_ = AudioBus::Create(channels, frames); | 17 fifo_index_(frames) {} |
17 } | |
18 | 18 |
19 AudioPullFifo::~AudioPullFifo() { | 19 AudioPullFifo::~AudioPullFifo() {} |
20 read_cb_.Reset(); | |
21 } | |
22 | 20 |
23 void AudioPullFifo::Consume(AudioBus* destination, int frames_to_consume) { | 21 void AudioPullFifo::Consume(AudioBus* destination, int frames_to_consume) { |
24 DCHECK(destination); | |
25 DCHECK_LE(frames_to_consume, destination->frames()); | 22 DCHECK_LE(frames_to_consume, destination->frames()); |
26 | 23 |
27 int write_pos = 0; | |
28 int remaining_frames_to_provide = frames_to_consume; | 24 int remaining_frames_to_provide = frames_to_consume; |
29 | 25 |
30 // Try to fulfill the request using what's available in the FIFO. | 26 // Try to fulfill the request using what's available in the FIFO. |
31 ReadFromFifo(destination, &remaining_frames_to_provide, &write_pos); | 27 int frames_read = ReadFromFifo(destination, remaining_frames_to_provide, 0); |
| 28 int write_pos = frames_read; |
| 29 remaining_frames_to_provide -= frames_read; |
32 | 30 |
33 // Get the remaining audio frames from the producer using the callback. | 31 // Get the remaining audio frames from the producer using the callback. |
34 while (remaining_frames_to_provide > 0) { | 32 while (remaining_frames_to_provide > 0) { |
| 33 DCHECK_EQ(fifo_index_, fifo_->frames()); |
| 34 fifo_index_ = 0; |
| 35 |
35 // Fill up the FIFO by acquiring audio data from the producer. | 36 // Fill up the FIFO by acquiring audio data from the producer. |
36 read_cb_.Run(write_pos, bus_.get()); | 37 read_cb_.Run(write_pos, fifo_.get()); |
37 fifo_->Push(bus_.get()); | |
38 | 38 |
39 // Try to fulfill the request using what's available in the FIFO. | 39 // Try to fulfill the request using what's available in the FIFO. |
40 ReadFromFifo(destination, &remaining_frames_to_provide, &write_pos); | 40 frames_read = |
| 41 ReadFromFifo(destination, remaining_frames_to_provide, write_pos); |
| 42 write_pos += frames_read; |
| 43 remaining_frames_to_provide -= frames_read; |
41 } | 44 } |
42 } | 45 } |
43 | 46 |
44 void AudioPullFifo::Clear() { | 47 void AudioPullFifo::Clear() { fifo_index_ = fifo_->frames(); } |
45 fifo_->Clear(); | |
46 } | |
47 | 48 |
48 void AudioPullFifo::ReadFromFifo(AudioBus* destination, | 49 int AudioPullFifo::ReadFromFifo(AudioBus* destination, |
49 int* frames_to_provide, | 50 int frames_to_provide, |
50 int* write_pos) { | 51 int write_pos) { |
51 DCHECK(frames_to_provide); | 52 int frames = std::min(frames_to_provide, fifo_->frames() - fifo_index_); |
52 DCHECK(write_pos); | 53 if (frames <= 0) |
53 int frames = std::min(fifo_->frames(), *frames_to_provide); | 54 return 0; |
54 fifo_->Consume(destination, *write_pos, frames); | 55 |
55 *write_pos += frames; | 56 for (int ch = 0; ch < fifo_->channels(); ++ch) { |
56 *frames_to_provide -= frames; | 57 const float* src = fifo_->channel(ch) + fifo_index_; |
| 58 float* dest = destination->channel(ch) + write_pos; |
| 59 memcpy(dest, src, frames * sizeof(*src)); |
| 60 } |
| 61 |
| 62 fifo_index_ += frames; |
| 63 return frames; |
57 } | 64 } |
58 | 65 |
59 } // namespace media | 66 } // namespace media |
OLD | NEW |