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

Side by Side Diff: media/base/audio_push_fifo.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 unittest compile breakage caused by recent method rename. 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
« no previous file with comments | « media/base/audio_push_fifo.h ('k') | media/base/audio_push_fifo_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_push_fifo.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10
11 namespace media {
12
13 AudioPushFifo::AudioPushFifo(const OutputCallback& callback)
14 : callback_(callback), frames_per_buffer_(0) {
15 DCHECK(!callback_.is_null());
16 }
17
18 AudioPushFifo::~AudioPushFifo() {}
19
20 void AudioPushFifo::Reset(int frames_per_buffer) {
21 DCHECK_GT(frames_per_buffer, 0);
22
23 audio_queue_.reset();
24 queued_frames_ = 0;
25
26 frames_per_buffer_ = frames_per_buffer;
27 }
28
29 void AudioPushFifo::Push(const AudioBus& input_bus) {
30 DCHECK_GT(frames_per_buffer_, 0);
31
32 // Fast path: No buffering required.
33 if ((queued_frames_ == 0) && (input_bus.frames() == frames_per_buffer_)) {
34 callback_.Run(input_bus, 0);
35 return;
36 }
37
38 // Lazy-create the |audio_queue_| if needed.
39 if (!audio_queue_ || audio_queue_->channels() != input_bus.channels())
40 audio_queue_ = AudioBus::Create(input_bus.channels(), frames_per_buffer_);
41
42 // Start with a frame offset that refers to the position of the first sample
43 // in |audio_queue_| relative to the first sample in |input_bus|.
44 int frame_delay = -queued_frames_;
45
46 // Repeatedly fill up |audio_queue_| with more sample frames from |input_bus|
47 // and deliver batches until all sample frames in |input_bus| have been
48 // consumed.
49 int input_offset = 0;
50 do {
51 // Attempt to fill |audio_queue_| completely.
52 const int frames_to_enqueue =
53 std::min(static_cast<int>(input_bus.frames() - input_offset),
54 frames_per_buffer_ - queued_frames_);
55 if (frames_to_enqueue > 0) {
56 DVLOG(2) << "Enqueuing " << frames_to_enqueue << " frames.";
57 input_bus.CopyPartialFramesTo(input_offset, frames_to_enqueue,
58 queued_frames_, audio_queue_.get());
59 queued_frames_ += frames_to_enqueue;
60 input_offset += frames_to_enqueue;
61 }
62
63 // If |audio_queue_| has been filled completely, deliver the re-buffered
64 // audio to the consumer.
65 if (queued_frames_ == frames_per_buffer_) {
66 DVLOG(2) << "Delivering another " << queued_frames_ << " frames.";
67 callback_.Run(*audio_queue_, frame_delay);
68 frame_delay += frames_per_buffer_;
69 queued_frames_ = 0;
70 } else {
71 // Not enough frames queued-up yet to deliver more frames.
72 }
73 } while (input_offset < input_bus.frames());
74 }
75
76 void AudioPushFifo::Flush() {
77 if (queued_frames_ == 0)
78 return;
79
80 audio_queue_->ZeroFramesPartial(queued_frames_,
81 audio_queue_->frames() - queued_frames_);
82 callback_.Run(*audio_queue_, -queued_frames_);
83 queued_frames_ = 0;
84 }
85
86 } // namespace media
OLDNEW
« no previous file with comments | « media/base/audio_push_fifo.h ('k') | media/base/audio_push_fifo_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698