summaryrefslogtreecommitdiffstats
path: root/chromium/ppapi/shared_impl/ppb_audio_shared.cc
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/ppapi/shared_impl/ppb_audio_shared.cc')
-rw-r--r--chromium/ppapi/shared_impl/ppb_audio_shared.cc150
1 files changed, 77 insertions, 73 deletions
diff --git a/chromium/ppapi/shared_impl/ppb_audio_shared.cc b/chromium/ppapi/shared_impl/ppb_audio_shared.cc
index 1b12d5c5efc..99af9d7e7e2 100644
--- a/chromium/ppapi/shared_impl/ppb_audio_shared.cc
+++ b/chromium/ppapi/shared_impl/ppb_audio_shared.cc
@@ -4,37 +4,32 @@
#include "ppapi/shared_impl/ppb_audio_shared.h"
+#include "base/debug/trace_event.h"
#include "base/logging.h"
+#include "ppapi/nacl_irt/public/irt_ppapi.h"
#include "ppapi/shared_impl/ppapi_globals.h"
#include "ppapi/shared_impl/ppb_audio_config_shared.h"
#include "ppapi/shared_impl/proxy_lock.h"
namespace ppapi {
-#if defined(OS_NACL)
namespace {
+bool g_nacl_mode = false;
// Because this is static, the function pointers will be NULL initially.
-PP_ThreadFunctions thread_functions;
+PP_ThreadFunctions g_thread_functions;
}
-#endif // defined(OS_NACL)
-AudioCallbackCombined::AudioCallbackCombined() : callback_1_0_(NULL),
- callback_(NULL) {
-}
+AudioCallbackCombined::AudioCallbackCombined()
+ : callback_1_0_(NULL), callback_(NULL) {}
AudioCallbackCombined::AudioCallbackCombined(
PPB_Audio_Callback_1_0 callback_1_0)
- : callback_1_0_(callback_1_0),
- callback_(NULL) {
-}
+ : callback_1_0_(callback_1_0), callback_(NULL) {}
AudioCallbackCombined::AudioCallbackCombined(PPB_Audio_Callback callback)
- : callback_1_0_(NULL),
- callback_(callback) {
-}
+ : callback_1_0_(NULL), callback_(callback) {}
-AudioCallbackCombined::~AudioCallbackCombined() {
-}
+AudioCallbackCombined::~AudioCallbackCombined() {}
bool AudioCallbackCombined::IsValid() const {
return callback_1_0_ || callback_;
@@ -56,10 +51,7 @@ void AudioCallbackCombined::Run(void* sample_buffer,
PPB_Audio_Shared::PPB_Audio_Shared()
: playing_(false),
shared_memory_size_(0),
-#if defined(OS_NACL)
- thread_id_(0),
- thread_active_(false),
-#endif
+ nacl_thread_active_(false),
user_data_(NULL),
client_buffer_size_bytes_(0),
bytes_per_second_(0),
@@ -81,11 +73,8 @@ void PPB_Audio_Shared::SetCallback(const AudioCallbackCombined& callback,
void PPB_Audio_Shared::SetStartPlaybackState() {
DCHECK(!playing_);
-#if !defined(OS_NACL)
DCHECK(!audio_thread_.get());
-#else
- DCHECK(!thread_active_);
-#endif
+ DCHECK(!nacl_thread_active_);
// If the socket doesn't exist, that means that the plugin has started before
// the browser has had a chance to create all the shared memory info and
// notify us. This is a common case. In this case, we just set the playing_
@@ -111,8 +100,8 @@ void PPB_Audio_Shared::SetStreamInfo(
socket_.reset(new base::CancelableSyncSocket(socket_handle));
shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false));
shared_memory_size_ = shared_memory_size;
- bytes_per_second_ = kAudioOutputChannels * (kBitsPerAudioOutputSample / 8) *
- sample_rate;
+ bytes_per_second_ =
+ kAudioOutputChannels * (kBitsPerAudioOutputSample / 8) * sample_rate;
buffer_index_ = 0;
if (!shared_memory_->Map(shared_memory_size_)) {
@@ -125,9 +114,8 @@ void PPB_Audio_Shared::SetStreamInfo(
audio_bus_ = media::AudioBus::WrapMemory(
kAudioOutputChannels, sample_frame_count, shared_memory_->memory());
// Setup integer audio buffer for user audio data.
- client_buffer_size_bytes_ =
- audio_bus_->frames() * audio_bus_->channels() *
- kBitsPerAudioOutputSample / 8;
+ client_buffer_size_bytes_ = audio_bus_->frames() * audio_bus_->channels() *
+ kBitsPerAudioOutputSample / 8;
client_buffer_.reset(new uint8_t[client_buffer_size_bytes_]);
}
@@ -145,54 +133,68 @@ void PPB_Audio_Shared::StartThread() {
// start up quickly enough.
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
- // Use NaCl's special API for IRT code that creates threads that call back
- // into user code.
- 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
+ if (g_nacl_mode) {
+ // Use NaCl's special API for IRT code that creates threads that call back
+ // into user code.
+ if (!IsThreadFunctionReady())
+ return;
+
+ DCHECK(!nacl_thread_active_);
+ int result =
+ g_thread_functions.thread_create(&nacl_thread_id_, CallRun, this);
+ DCHECK_EQ(0, result);
+ nacl_thread_active_ = true;
+ } else {
+ DCHECK(!audio_thread_.get());
+ audio_thread_.reset(
+ new base::DelegateSimpleThread(this, "plugin_audio_thread"));
+ audio_thread_->Start();
+ }
}
void PPB_Audio_Shared::StopThread() {
-#if !defined(OS_NACL)
- if (audio_thread_.get()) {
- // In general, the audio thread should not do Pepper calls, but it might
- // anyway (for example, our Audio test does CallOnMainThread). If it did
- // a pepper call which acquires the lock (most of them do), and we try to
- // shut down the thread and Join it while holding the lock, we would
- // deadlock. So we give up the lock here so that the thread at least _can_
- // make Pepper calls without causing deadlock.
- CallWhileUnlocked(base::Bind(&base::DelegateSimpleThread::Join,
- base::Unretained(audio_thread_.get())));
- audio_thread_.reset();
- }
-#else
- if (thread_active_) {
- // See comment above about why we unlock here.
- int result = CallWhileUnlocked(thread_functions.thread_join, thread_id_);
- DCHECK_EQ(0, result);
- thread_active_ = false;
+ // In general, the audio thread should not do Pepper calls, but it might
+ // anyway (for example, our Audio test does CallOnMainThread). If it did a
+ // pepper call which acquires the lock (most of them do), and we try to shut
+ // down the thread and Join it while holding the lock, we would deadlock. So
+ // we give up the lock here so that the thread at least _can_ make Pepper
+ // calls without causing deadlock.
+ if (g_nacl_mode) {
+ if (nacl_thread_active_) {
+ int result =
+ CallWhileUnlocked(g_thread_functions.thread_join, nacl_thread_id_);
+ DCHECK_EQ(0, result);
+ nacl_thread_active_ = false;
+ }
+ } else {
+ if (audio_thread_.get()) {
+ CallWhileUnlocked(base::Bind(&base::DelegateSimpleThread::Join,
+ base::Unretained(audio_thread_.get())));
+ audio_thread_.reset();
+ }
}
-#endif
}
-#if defined(OS_NACL)
+// static
+bool PPB_Audio_Shared::IsThreadFunctionReady() {
+ if (!g_nacl_mode)
+ return true;
+
+ return (g_thread_functions.thread_create != NULL &&
+ g_thread_functions.thread_join != NULL);
+}
+
+// static
+void PPB_Audio_Shared::SetNaClMode() {
+ g_nacl_mode = true;
+}
+
// static
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;
+ DCHECK(g_nacl_mode);
+ g_thread_functions = *functions;
}
// static
@@ -200,7 +202,6 @@ 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 = 0;
@@ -212,15 +213,18 @@ void PPB_Audio_Shared::Run() {
if (pending_data < 0)
break;
- PP_TimeDelta latency =
- static_cast<double>(pending_data) / bytes_per_second_;
- callback_.Run(client_buffer_.get(), client_buffer_size_bytes_, latency,
- user_data_);
+ {
+ TRACE_EVENT0("audio", "PPB_Audio_Shared::FireRenderCallback");
+ PP_TimeDelta latency =
+ static_cast<double>(pending_data) / bytes_per_second_;
+ callback_.Run(
+ client_buffer_.get(), client_buffer_size_bytes_, latency, user_data_);
+ }
// Deinterleave the audio data into the shared memory as floats.
- audio_bus_->FromInterleaved(
- client_buffer_.get(), audio_bus_->frames(),
- kBitsPerAudioOutputSample / 8);
+ audio_bus_->FromInterleaved(client_buffer_.get(),
+ audio_bus_->frames(),
+ kBitsPerAudioOutputSample / 8);
// Let the other end know which buffer we just filled. The buffer index is
// used to ensure the other end is getting the buffer it expects. For more