Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(594)

Side by Side Diff: media/base/audio_rechunker.cc

Issue 1714593003: Introduce media::AudioPushFifo and a couple of use cases (and clean-ups). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix Win compile issue. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include <algorithm>
8
9 namespace media {
10
11 AudioRechunker::AudioRechunker(base::TimeDelta output_duration,
12 const RechunkedAudioCallback& callback)
13 : output_duration_(output_duration),
14 callback_(callback),
15 sample_rate_(0),
16 output_frames_(0) {
17 DCHECK_GT(output_duration_, base::TimeDelta());
18 DCHECK(!callback_.is_null());
19 }
20
21 AudioRechunker::~AudioRechunker() {}
22
23 bool AudioRechunker::SetSampleRate(int sample_rate) {
24 DCHECK_GT(sample_rate, 0);
25
26 sample_rate_ = sample_rate;
27 audio_queue_.reset();
xjz 2016/02/20 01:53:04 Same as the other comment, I think here also assum
miu 2016/02/23 04:27:41 That's not the assumption. See my reply to your o
28 pending_frames_ = 0;
29
30 const int64_t numerator = sample_rate * output_duration_.InMicroseconds();
31 output_frames_ = numerator / base::Time::kMicrosecondsPerSecond;
32 DCHECK_GT(output_frames_, 0);
33 return ((numerator % base::Time::kMicrosecondsPerSecond) == 0);
34 }
35
36 void AudioRechunker::Push(const AudioBus& input_bus,
37 base::TimeDelta reference_timestamp) {
38 DCHECK_GT(output_frames_, 0);
39
40 // Fast path: No buffering required.
41 if ((pending_frames_ == 0) && (input_bus.frames() == output_frames_)) {
42 callback_.Run(input_bus, reference_timestamp);
43 return;
44 }
45
46 // Lazy-create the |audio_queue_| if needed.
47 if (!audio_queue_ || audio_queue_->channels() != input_bus.channels())
48 audio_queue_ = AudioBus::Create(input_bus.channels(), output_frames_);
49
50 // Adjust |reference_timestamp| to be that of the first sample in
51 // |audio_queue_| since that will be the first sample delivered via the
52 // |callback_|.
53 reference_timestamp -= base::TimeDelta::FromMicroseconds(
54 pending_frames_ * base::Time::kMicrosecondsPerSecond / sample_rate_);
55
56 // Repeatedly fill up |audio_queue_| with more sample frames from |input_bus|
o1ka 2016/02/22 13:04:47 I find the part below a bit difficult to read due
miu 2016/02/23 04:27:41 Acknowledged.
57 // and deliver batches until all sample frames in |input_bus| have been
58 // consumed.
59 int source_pos = 0;
o1ka 2016/02/22 13:04:47 Can it be named |input_bus_offset| or something li
miu 2016/02/23 04:27:41 Done. Changed to input_offset.
60 do {
61 // Attempt to fill |audio_queue_| completely.
62 const int frames_to_push =
o1ka 2016/02/22 13:04:47 |frames_to_queue|? As I see it, frames_to_push are
miu 2016/02/23 04:27:41 Done. Changed to frames_to_enqueue.
63 std::min(static_cast<int>(input_bus.frames() - source_pos),
64 output_frames_ - pending_frames_);
65 if (frames_to_push > 0) {
66 DVLOG(2) << "Enqueuing " << frames_to_push << " frames.";
67 input_bus.CopyPartialFramesTo(source_pos, frames_to_push, pending_frames_,
68 audio_queue_.get());
o1ka 2016/02/22 13:04:47 It would be also nice to somehow associate |pendin
miu 2016/02/23 04:27:41 Done.
69 pending_frames_ += frames_to_push;
70 source_pos += frames_to_push;
71 }
72
73 // If |audio_queue_| has been filled completely, deliver the re-chunked
74 // audio to the consumer.
75 if (pending_frames_ == output_frames_) {
xjz 2016/02/20 01:53:04 Does here assume that for each Push, the input fra
miu 2016/02/23 04:27:41 No, that's not the assumption at all. The number
76 DVLOG(2) << "Delivering another " << pending_frames_ << " frames.";
77 callback_.Run(*audio_queue_, reference_timestamp);
78 reference_timestamp += output_duration_;
79 pending_frames_ = 0;
80 }
o1ka 2016/02/22 13:04:47 To improve readability, it would be nice to have a
miu 2016/02/23 04:27:41 Done.
81 } while (source_pos < input_bus.frames());
82 }
83
84 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698