#include "ppapi/shared_impl/ppb_audio_shared.h"
#include "base/logging.h"
#include "media/audio/shared_memory_util.h"
#include "ppapi/shared_impl/ppapi_globals.h"
enum { kChannels = 2, kBytesPerSample = 2 };
namespace ppapi {
#if defined(OS_NACL)
namespace {
PP_ThreadFunctions thread_functions;
}
#endif
PPB_Audio_Shared::PPB_Audio_Shared()
: playing_(false),
shared_memory_size_(0),
#if defined(OS_NACL)
thread_id_(0),
thread_active_(false),
#endif
callback_(NULL),
user_data_(NULL),
client_buffer_size_bytes_(0) {
}
PPB_Audio_Shared::~PPB_Audio_Shared() {
if (socket_.get())
socket_->Shutdown();
StopThread();
}
void PPB_Audio_Shared::SetCallback(PPB_Audio_Callback callback,
void* user_data) {
callback_ = callback;
user_data_ = user_data;
}
void PPB_Audio_Shared::SetStartPlaybackState() {
DCHECK(!playing_);
#if !defined(OS_NACL)
DCHECK(!audio_thread_.get());
#else
DCHECK(!thread_active_);
#endif
playing_ = true;
StartThread();
}
void PPB_Audio_Shared::SetStopPlaybackState() {
DCHECK(playing_);
StopThread();
playing_ = false;
}
void PPB_Audio_Shared::SetStreamInfo(
PP_Instance instance,
base::SharedMemoryHandle shared_memory_handle,
size_t shared_memory_size,
base::SyncSocket::Handle socket_handle,
int sample_frame_count) {
socket_.reset(new base::CancelableSyncSocket(socket_handle));
shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false));
shared_memory_size_ = shared_memory_size;
if (!shared_memory_->Map(
media::TotalSharedMemorySizeInBytes(shared_memory_size_))) {
PpapiGlobals::Get()->LogWithSource(instance, PP_LOGLEVEL_WARNING, "",
"Failed to map shared memory for PPB_Audio_Shared.");
} else {
audio_bus_ = media::AudioBus::WrapMemory(
kChannels, sample_frame_count, shared_memory_->memory());
client_buffer_size_bytes_ =
audio_bus_->frames() * audio_bus_->channels() * kBytesPerSample;
client_buffer_.reset(new uint8_t[client_buffer_size_bytes_]);
}
StartThread();
}
void PPB_Audio_Shared::StartThread() {
if (!playing_ || !callback_ || !socket_.get() || !shared_memory_->memory() ||
!audio_bus_.get() || !client_buffer_.get())
return;
memset(shared_memory_->memory(), 0, shared_memory_size_);
memset(client_buffer_.get(), 0, client_buffer_size_bytes_);
#if !defined(OS_NACL)
DCHECK(!audio_thread_.get());
audio_thread_.reset(new base::DelegateSimpleThread(
this, "plugin_audio_thread"));
audio_thread_->Start();
#else
if (NULL == thread_functions.thread_create ||
NULL == thread_functions.thread_join)
return;
int result = thread_functions.thread_create(&thread_id_, CallRun, this);
DCHECK_EQ(result, 0);
thread_active_ = true;
#endif
}
void PPB_Audio_Shared::StopThread() {
#if !defined(OS_NACL)
if (audio_thread_.get()) {
audio_thread_->Join();
audio_thread_.reset();
}
#else
if (thread_active_) {
int result = thread_functions.thread_join(thread_id_);
DCHECK_EQ(0, result);
thread_active_ = false;
}
#endif
}
#if defined(OS_NACL)
void PPB_Audio_Shared::SetThreadFunctions(
const struct PP_ThreadFunctions* functions) {
DCHECK(thread_functions.thread_create == NULL);
DCHECK(thread_functions.thread_join == NULL);
thread_functions = *functions;
}
void PPB_Audio_Shared::CallRun(void* self) {
PPB_Audio_Shared* audio = static_cast<PPB_Audio_Shared*>(self);
audio->Run();
}
#endif
void PPB_Audio_Shared::Run() {
int pending_data;
const int bytes_per_frame =
sizeof(*audio_bus_->channel(0)) * audio_bus_->channels();
while (sizeof(pending_data) ==
socket_->Receive(&pending_data, sizeof(pending_data)) &&
pending_data != media::kPauseMark) {
callback_(client_buffer_.get(), client_buffer_size_bytes_, user_data_);
audio_bus_->FromInterleaved(
client_buffer_.get(), audio_bus_->frames(), kBytesPerSample);
media::SetActualDataSizeInBytes(
shared_memory_.get(), shared_memory_size_,
audio_bus_->frames() * bytes_per_frame);
}
}
}