This source file includes following definitions.
- Clear
- Append
- ReadFrames
- PeekFrames
- SeekFrames
- InternalRead
- UpdateCurrentTime
#include "media/base/audio_buffer_queue.h"
#include <algorithm>
#include "base/logging.h"
#include "media/base/audio_bus.h"
#include "media/base/buffers.h"
namespace media {
AudioBufferQueue::AudioBufferQueue() { Clear(); }
AudioBufferQueue::~AudioBufferQueue() {}
void AudioBufferQueue::Clear() {
buffers_.clear();
current_buffer_ = buffers_.begin();
current_buffer_offset_ = 0;
frames_ = 0;
current_time_ = kNoTimestamp();
}
void AudioBufferQueue::Append(const scoped_refptr<AudioBuffer>& buffer_in) {
if (buffers_.empty() && buffer_in->timestamp() != kNoTimestamp()) {
current_time_ = buffer_in->timestamp();
}
buffers_.push_back(buffer_in);
current_buffer_ = buffers_.begin();
frames_ += buffer_in->frame_count();
CHECK_GT(frames_, 0);
}
int AudioBufferQueue::ReadFrames(int frames,
int dest_frame_offset,
AudioBus* dest) {
DCHECK_GE(dest->frames(), frames + dest_frame_offset);
return InternalRead(frames, true, 0, dest_frame_offset, dest);
}
int AudioBufferQueue::PeekFrames(int frames,
int source_frame_offset,
int dest_frame_offset,
AudioBus* dest) {
DCHECK_GE(dest->frames(), frames);
return InternalRead(
frames, false, source_frame_offset, dest_frame_offset, dest);
}
void AudioBufferQueue::SeekFrames(int frames) {
CHECK_LE(frames, frames_);
int taken = InternalRead(frames, true, 0, 0, NULL);
DCHECK_EQ(taken, frames);
}
int AudioBufferQueue::InternalRead(int frames,
bool advance_position,
int source_frame_offset,
int dest_frame_offset,
AudioBus* dest) {
int taken = 0;
BufferQueue::iterator current_buffer = current_buffer_;
int current_buffer_offset = current_buffer_offset_;
int frames_to_skip = source_frame_offset;
while (taken < frames) {
if (current_buffer == buffers_.end())
break;
scoped_refptr<AudioBuffer> buffer = *current_buffer;
int remaining_frames_in_buffer =
buffer->frame_count() - current_buffer_offset;
if (frames_to_skip > 0) {
int skipped = std::min(remaining_frames_in_buffer, frames_to_skip);
current_buffer_offset += skipped;
frames_to_skip -= skipped;
} else {
int copied = std::min(frames - taken, remaining_frames_in_buffer);
if (dest) {
buffer->ReadFrames(
copied, current_buffer_offset, dest_frame_offset + taken, dest);
}
taken += copied;
current_buffer_offset += copied;
}
if (current_buffer_offset == buffer->frame_count()) {
if (advance_position) {
UpdateCurrentTime(current_buffer, current_buffer_offset);
}
BufferQueue::iterator next = current_buffer + 1;
if (next == buffers_.end())
break;
current_buffer = next;
current_buffer_offset = 0;
}
}
if (advance_position) {
frames_ -= taken;
DCHECK_GE(frames_, 0);
DCHECK(current_buffer_ != buffers_.end() || frames_ == 0);
UpdateCurrentTime(current_buffer, current_buffer_offset);
buffers_.erase(buffers_.begin(), current_buffer);
current_buffer_ = buffers_.begin();
current_buffer_offset_ = current_buffer_offset;
}
return taken;
}
void AudioBufferQueue::UpdateCurrentTime(BufferQueue::iterator buffer,
int offset) {
if (buffer != buffers_.end() && (*buffer)->timestamp() != kNoTimestamp()) {
double time_offset = ((*buffer)->duration().InMicroseconds() * offset) /
static_cast<double>((*buffer)->frame_count());
current_time_ =
(*buffer)->timestamp() + base::TimeDelta::FromMicroseconds(
static_cast<int64>(time_offset + 0.5));
}
}
}