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

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

Issue 389623002: Add a block based Audio FIFO. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 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_block_fifo.h"
6
7 #include "base/logging.h"
8 #include "media/base/audio_fifo.h"
9
10 namespace media {
11
12 AudioBlockFifo::AudioBlockFifo(int channels, int frames, int blocks)
13 : block_frames_(frames),
14 write_pos_(0) {
15 // Create |blocks| of audio buses and push them to the containers.
16 for (int i = 0; i < blocks; ++i) {
17 scoped_ptr<AudioBus> audio_bus = AudioBus::Create(channels, frames);
18 audio_blocks_.push_back(audio_bus.release());
19 empty_blocks_.push(audio_blocks_.back());
20 }
21 }
22
23 AudioBlockFifo::~AudioBlockFifo() {}
24
25 void AudioBlockFifo::Push(const void* source, int frames,
26 int bytes_per_sample) {
27 DCHECK(source);
28 DCHECK_GT(frames, 0);
29 DCHECK_GT(bytes_per_sample, 0);
30
31 // Get the current write block.
32 AudioBus* current_block = empty_blocks_.front();
33 CHECK(current_block);
34 DCHECK_EQ(current_block->frames(), block_frames_);
35
36 // Figure out if the current written block is enough to store |frames| of new
37 // content, or what segment sizes we need when adding the new content to the
38 // FIFO.
39 int append_frames = 0;
40 int remain_frames = 0;
41 AudioFifo::GetSizes(write_pos_, block_frames_, frames, &append_frames,
DaleCurtis 2014/07/11 22:33:32 You can simplify this entire method to just the fo
no longer working on chromium 2014/07/14 11:28:26 Done with using a write loop instead of recursive
42 &remain_frames);
43 CHECK_LE(write_pos_ + append_frames, block_frames_);
44
45 // Deinterleave the content to the FIFO.
46 current_block->FromInterleaved(source, append_frames, bytes_per_sample);
47
48 write_pos_ = AudioFifo::UpdatePos(write_pos_, append_frames, block_frames_);
49 if (!write_pos_) {
50 // The current block is completely filled, move it from |empety_blocks_|
51 // to |filled_blocks_|.
52 filled_blocks_.push(current_block);
53 empty_blocks_.pop();
54 }
55
56 if (remain_frames) {
57 // Recursively write the remain frames to the FIFO.
58 const uint8* remain_source = static_cast<const uint8*>(source) +
59 append_frames * bytes_per_sample * current_block->channels();
60 Push(remain_source, remain_frames, bytes_per_sample);
61 }
62 }
63
64 const AudioBus* AudioBlockFifo::Consume() {
65 DCHECK(filled_blocks());
DaleCurtis 2014/07/11 22:33:32 Likewise, this just becomes: CHECK_LE(tail, head)
no longer working on chromium 2014/07/14 11:28:26 Lets move the discussion to the relevant comment i
66 AudioBus* audio_bus = filled_blocks_.front();
67 empty_blocks_.push(audio_bus);
68 filled_blocks_.pop();
69 return audio_bus;
70 }
71
72 void AudioBlockFifo::Clear() {
73 while (!filled_blocks_.empty()) {
DaleCurtis 2014/07/11 22:33:32 And finally: write_pos_ = tail = head = 0;
74 AudioBus* audio_bus = filled_blocks_.front();
75 empty_blocks_.push(audio_bus);
76 filled_blocks_.pop();
77 write_pos_ = 0;
78 }
79 }
80
81 int AudioBlockFifo::filled_blocks() const {
82 return filled_blocks_.size();
83 }
84
85 int AudioBlockFifo::empty_frames() const {
86 DCHECK_GE(static_cast<int>(empty_blocks_.size() * block_frames_), write_pos_);
DaleCurtis 2014/07/11 22:33:32 I'd just make this frames_ - write_pos_;
no longer working on chromium 2014/07/14 11:28:27 The comment is addressed in the head unfilled_fram
87 return empty_blocks_.size() * block_frames_ - write_pos_;
88 }
89
90 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698