OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "media/base/audio_block_fifo.h" | 9 #include "media/base/audio_block_fifo.h" |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... |
29 int bytes_per_sample) { | 29 int bytes_per_sample) { |
30 PushInternal(source, frames, bytes_per_sample); | 30 PushInternal(source, frames, bytes_per_sample); |
31 } | 31 } |
32 | 32 |
33 void AudioBlockFifo::PushSilence(int frames) { | 33 void AudioBlockFifo::PushSilence(int frames) { |
34 PushInternal(nullptr, frames, 0); | 34 PushInternal(nullptr, frames, 0); |
35 } | 35 } |
36 | 36 |
37 const AudioBus* AudioBlockFifo::Consume() { | 37 const AudioBus* AudioBlockFifo::Consume() { |
38 DCHECK(available_blocks_); | 38 DCHECK(available_blocks_); |
39 AudioBus* audio_bus = audio_blocks_[read_block_]; | 39 AudioBus* audio_bus = audio_blocks_[read_block_].get(); |
40 read_block_ = (read_block_ + 1) % audio_blocks_.size(); | 40 read_block_ = (read_block_ + 1) % audio_blocks_.size(); |
41 --available_blocks_; | 41 --available_blocks_; |
42 return audio_bus; | 42 return audio_bus; |
43 } | 43 } |
44 | 44 |
45 void AudioBlockFifo::Clear() { | 45 void AudioBlockFifo::Clear() { |
46 write_pos_ = 0; | 46 write_pos_ = 0; |
47 write_block_ = 0; | 47 write_block_ = 0; |
48 read_block_ = 0; | 48 read_block_ = 0; |
49 available_blocks_ = 0; | 49 available_blocks_ = 0; |
(...skipping 10 matching lines...) Expand all Loading... |
60 return unfilled_frames; | 60 return unfilled_frames; |
61 } | 61 } |
62 | 62 |
63 void AudioBlockFifo::IncreaseCapacity(int blocks) { | 63 void AudioBlockFifo::IncreaseCapacity(int blocks) { |
64 DCHECK_GT(blocks, 0); | 64 DCHECK_GT(blocks, 0); |
65 | 65 |
66 // Create |blocks| of audio buses and insert them to the containers. | 66 // Create |blocks| of audio buses and insert them to the containers. |
67 audio_blocks_.reserve(audio_blocks_.size() + blocks); | 67 audio_blocks_.reserve(audio_blocks_.size() + blocks); |
68 | 68 |
69 const int original_size = audio_blocks_.size(); | 69 const int original_size = audio_blocks_.size(); |
70 for (int i = 0; i < blocks; ++i) { | 70 for (int i = 0; i < blocks; ++i) |
71 audio_blocks_.push_back( | 71 audio_blocks_.push_back(AudioBus::Create(channels_, block_frames_)); |
72 AudioBus::Create(channels_, block_frames_).release()); | |
73 } | |
74 | 72 |
75 if (!original_size) | 73 if (!original_size) |
76 return; | 74 return; |
77 | 75 |
78 std::rotate(audio_blocks_.begin() + read_block_, | 76 std::rotate(audio_blocks_.begin() + read_block_, |
79 audio_blocks_.begin() + original_size, audio_blocks_.end()); | 77 audio_blocks_.begin() + original_size, audio_blocks_.end()); |
80 | 78 |
81 // Update the write pointer if it is on top of the new inserted blocks. | 79 // Update the write pointer if it is on top of the new inserted blocks. |
82 if (write_block_ >= read_block_) | 80 if (write_block_ >= read_block_) |
83 write_block_ += blocks; | 81 write_block_ += blocks; |
(...skipping 12 matching lines...) Expand all Loading... |
96 // we inject silence. | 94 // we inject silence. |
97 DCHECK((source && bytes_per_sample > 0) || (!source && !bytes_per_sample)); | 95 DCHECK((source && bytes_per_sample > 0) || (!source && !bytes_per_sample)); |
98 DCHECK_GT(frames, 0); | 96 DCHECK_GT(frames, 0); |
99 DCHECK_LT(available_blocks_, static_cast<int>(audio_blocks_.size())); | 97 DCHECK_LT(available_blocks_, static_cast<int>(audio_blocks_.size())); |
100 CHECK_LE(frames, GetUnfilledFrames()); | 98 CHECK_LE(frames, GetUnfilledFrames()); |
101 | 99 |
102 const uint8_t* source_ptr = static_cast<const uint8_t*>(source); | 100 const uint8_t* source_ptr = static_cast<const uint8_t*>(source); |
103 int frames_to_push = frames; | 101 int frames_to_push = frames; |
104 while (frames_to_push) { | 102 while (frames_to_push) { |
105 // Get the current write block. | 103 // Get the current write block. |
106 AudioBus* current_block = audio_blocks_[write_block_]; | 104 AudioBus* current_block = audio_blocks_[write_block_].get(); |
107 | 105 |
108 // Figure out what segment sizes we need when adding the new content to | 106 // Figure out what segment sizes we need when adding the new content to |
109 // the FIFO. | 107 // the FIFO. |
110 const int push_frames = | 108 const int push_frames = |
111 std::min(block_frames_ - write_pos_, frames_to_push); | 109 std::min(block_frames_ - write_pos_, frames_to_push); |
112 | 110 |
113 if (source) { | 111 if (source) { |
114 // Deinterleave the content to the FIFO and update the |write_pos_|. | 112 // Deinterleave the content to the FIFO and update the |write_pos_|. |
115 current_block->FromInterleavedPartial(source_ptr, write_pos_, push_frames, | 113 current_block->FromInterleavedPartial(source_ptr, write_pos_, push_frames, |
116 bytes_per_sample); | 114 bytes_per_sample); |
117 } else { | 115 } else { |
118 current_block->ZeroFramesPartial(write_pos_, push_frames); | 116 current_block->ZeroFramesPartial(write_pos_, push_frames); |
119 } | 117 } |
120 write_pos_ = (write_pos_ + push_frames) % block_frames_; | 118 write_pos_ = (write_pos_ + push_frames) % block_frames_; |
121 if (!write_pos_) { | 119 if (!write_pos_) { |
122 // The current block is completely filled, increment |write_block_| and | 120 // The current block is completely filled, increment |write_block_| and |
123 // |available_blocks_|. | 121 // |available_blocks_|. |
124 write_block_ = (write_block_ + 1) % audio_blocks_.size(); | 122 write_block_ = (write_block_ + 1) % audio_blocks_.size(); |
125 ++available_blocks_; | 123 ++available_blocks_; |
126 } | 124 } |
127 | 125 |
128 if (source_ptr) | 126 if (source_ptr) |
129 source_ptr += push_frames * bytes_per_sample * channels_; | 127 source_ptr += push_frames * bytes_per_sample * channels_; |
130 frames_to_push -= push_frames; | 128 frames_to_push -= push_frames; |
131 DCHECK_GE(frames_to_push, 0); | 129 DCHECK_GE(frames_to_push, 0); |
132 } | 130 } |
133 } | 131 } |
134 | 132 |
135 } // namespace media | 133 } // namespace media |
OLD | NEW |