This source file includes following definitions.
- buffer_index_
- UpdatePendingBytes
- Close
- Init
- PrepareForeignSocketHandle
- PrepareForeignSocketHandle
- WaitUntilDataIsReady
#include "content/browser/renderer_host/media/audio_sync_reader.h"
#include <algorithm>
#include "base/command_line.h"
#include "base/memory/shared_memory.h"
#include "base/metrics/histogram.h"
#include "content/public/common/content_switches.h"
#include "media/audio/audio_buffers_state.h"
#include "media/audio/audio_parameters.h"
using media::AudioBus;
namespace content {
AudioSyncReader::AudioSyncReader(base::SharedMemory* shared_memory,
const media::AudioParameters& params)
: shared_memory_(shared_memory),
mute_audio_(CommandLine::ForCurrentProcess()->HasSwitch(
switches::kMuteAudio)),
packet_size_(shared_memory_->requested_size()),
renderer_callback_count_(0),
renderer_missed_callback_count_(0),
#if defined(OS_MACOSX)
maximum_wait_time_(params.GetBufferDuration() / 2),
#else
maximum_wait_time_(base::TimeDelta::FromMilliseconds(20)),
#endif
buffer_index_(0) {
DCHECK_EQ(packet_size_, AudioBus::CalculateMemorySize(params));
output_bus_ = AudioBus::WrapMemory(params, shared_memory->memory());
output_bus_->Zero();
}
AudioSyncReader::~AudioSyncReader() {
if (!renderer_callback_count_)
return;
int percentage_missed =
100.0 * renderer_missed_callback_count_ / renderer_callback_count_;
UMA_HISTOGRAM_PERCENTAGE(
"Media.AudioRendererMissedDeadline", percentage_missed);
}
void AudioSyncReader::UpdatePendingBytes(uint32 bytes) {
output_bus_->Zero();
socket_->Send(&bytes, sizeof(bytes));
++buffer_index_;
}
void AudioSyncReader::Read(const AudioBus* source, AudioBus* dest) {
++renderer_callback_count_;
if (!WaitUntilDataIsReady()) {
++renderer_missed_callback_count_;
dest->Zero();
return;
}
if (mute_audio_)
dest->Zero();
else
output_bus_->CopyTo(dest);
}
void AudioSyncReader::Close() {
socket_->Close();
}
bool AudioSyncReader::Init() {
socket_.reset(new base::CancelableSyncSocket());
foreign_socket_.reset(new base::CancelableSyncSocket());
return base::CancelableSyncSocket::CreatePair(socket_.get(),
foreign_socket_.get());
}
#if defined(OS_WIN)
bool AudioSyncReader::PrepareForeignSocketHandle(
base::ProcessHandle process_handle,
base::SyncSocket::Handle* foreign_handle) {
::DuplicateHandle(GetCurrentProcess(), foreign_socket_->handle(),
process_handle, foreign_handle,
0, FALSE, DUPLICATE_SAME_ACCESS);
return (*foreign_handle != 0);
}
#else
bool AudioSyncReader::PrepareForeignSocketHandle(
base::ProcessHandle process_handle,
base::FileDescriptor* foreign_handle) {
foreign_handle->fd = foreign_socket_->handle();
foreign_handle->auto_close = false;
return (foreign_handle->fd != -1);
}
#endif
bool AudioSyncReader::WaitUntilDataIsReady() {
base::TimeDelta timeout = maximum_wait_time_;
const base::TimeTicks start_time = base::TimeTicks::Now();
const base::TimeTicks finish_time = start_time + timeout;
size_t bytes_received = 0;
uint32 renderer_buffer_index = 0;
while (timeout.InMicroseconds() > 0) {
bytes_received = socket_->ReceiveWithTimeout(
&renderer_buffer_index, sizeof(renderer_buffer_index), timeout);
if (!bytes_received)
break;
DCHECK_EQ(bytes_received, sizeof(renderer_buffer_index));
if (renderer_buffer_index == buffer_index_)
break;
timeout = finish_time - base::TimeTicks::Now();
}
if (!bytes_received || renderer_buffer_index != buffer_index_) {
DVLOG(2) << "AudioSyncReader::WaitUntilDataIsReady() timed out.";
base::TimeDelta time_since_start = base::TimeTicks::Now() - start_time;
UMA_HISTOGRAM_CUSTOM_TIMES("Media.AudioOutputControllerDataNotReady",
time_since_start,
base::TimeDelta::FromMilliseconds(1),
base::TimeDelta::FromMilliseconds(1000),
50);
return false;
}
return true;
}
}