OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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_rechunker.h" |
| 6 |
| 7 namespace media { |
| 8 |
| 9 AudioRechunker::AudioRechunker(base::TimeDelta output_duration, |
| 10 const RechunkedAudioCallback& callback) |
| 11 : output_duration_(output_duration), callback_(callback), |
| 12 sample_rate_(0), output_frames_(0) { |
| 13 DCHECK_GT(output_duration_, base::TimeDelta()); |
| 14 DCHECK(!callback_.is_null()); |
| 15 } |
| 16 |
| 17 AudioRechunker::~AudioRechunker() {} |
| 18 |
| 19 bool AudioRechunker::SetSampleRate(int sample_rate) { |
| 20 DCHECK_GT(sample_rate, 0); |
| 21 |
| 22 sample_rate_ = sample_rate; |
| 23 audio_queue_.reset(); |
| 24 pending_frames_= 0; |
| 25 |
| 26 const int64_t numerator = sample_rate * output_duration_.InMicroseconds(); |
| 27 output_frames_ = numerator / base::Time::kMicrosecondsPerSecond; |
| 28 DCHECK_GT(output_frames_, 0); |
| 29 return ((numerator % base::Time::kMicrosecondsPerSecond) == 0); |
| 30 } |
| 31 |
| 32 void AudioRechunker::Push(const AudioBus& input_bus, |
| 33 base::TimeDelta reference_timestamp) { |
| 34 DCHECK_GT(output_frames_, 0); |
| 35 |
| 36 // Fast path: No buffering required. |
| 37 if ((pending_frames_ == 0) && (input_bus.frames() == output_frames_)) { |
| 38 callback_.Run(input_bus, reference_timestamp); |
| 39 return; |
| 40 } |
| 41 |
| 42 // Lazy-create the |audio_queue_| if needed. |
| 43 if (!audio_queue_ || audio_queue_->channels() != input_bus.channels()) |
| 44 audio_queue_ = AudioBus::Create(input_bus.channels(), output_frames_); |
| 45 |
| 46 // Adjust |reference_timestamp| to be that of the first sample in |
| 47 // |audio_queue_| since that will be the first sample delivered via the |
| 48 // |callback_|. |
| 49 reference_timestamp -= base::TimeDelta::FromMicroseconds( |
| 50 pending_frames_ * base::Time::kMicrosecondsPerSecond / sample_rate_); |
| 51 |
| 52 // Repeatedly fill up |audio_queue_| with more sample frames from |input_bus| |
| 53 // and deliver batches until all sample frames in |input_bus| have been |
| 54 // consumed. |
| 55 int source_pos = 0; |
| 56 do { |
| 57 // Attempt to fill |audio_queue_| completely. |
| 58 const int frames_to_push = |
| 59 std::min(static_cast<int>(input_bus.frames() - source_pos), |
| 60 output_frames_ - pending_frames_); |
| 61 if (frames_to_push > 0) { |
| 62 DVLOG(2) << "Enqueuing " << frames_to_push << " frames."; |
| 63 input_bus.CopyPartialFramesTo(source_pos, frames_to_push, pending_frames_, |
| 64 audio_queue_.get()); |
| 65 pending_frames_ += frames_to_push; |
| 66 source_pos += frames_to_push; |
| 67 } |
| 68 |
| 69 // If |audio_queue_| has been filled completely, deliver the re-chunked |
| 70 // audio to the consumer. |
| 71 if (pending_frames_ == output_frames_) { |
| 72 DVLOG(2) << "Delivering another " << pending_frames_ << " frames."; |
| 73 callback_.Run(*audio_queue_, reference_timestamp); |
| 74 reference_timestamp += output_duration_; |
| 75 pending_frames_ = 0; |
| 76 } |
| 77 } while (source_pos < input_bus.frames()); |
| 78 } |
| 79 |
| 80 } // namespace media |
OLD | NEW |