| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ppapi/shared_impl/ppb_audio_shared.h" | 5 #include "ppapi/shared_impl/ppb_audio_shared.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "media/audio/shared_memory_util.h" | 8 #include "media/audio/shared_memory_util.h" |
| 9 #include "ppapi/shared_impl/ppapi_globals.h" | 9 #include "ppapi/shared_impl/ppapi_globals.h" |
| 10 | 10 |
| 11 // Hard coded values from PepperPlatformAudioOutputImpl. |
| 12 // TODO(???): PPAPI shouldn't hard code these values for all clients. |
| 13 enum { kChannels = 2, kBytesPerSample = 2 }; |
| 14 |
| 11 namespace ppapi { | 15 namespace ppapi { |
| 12 | 16 |
| 13 #if defined(OS_NACL) | 17 #if defined(OS_NACL) |
| 14 namespace { | 18 namespace { |
| 15 // Because this is static, the function pointers will be NULL initially. | 19 // Because this is static, the function pointers will be NULL initially. |
| 16 PP_ThreadFunctions thread_functions; | 20 PP_ThreadFunctions thread_functions; |
| 17 } | 21 } |
| 18 #endif // defined(OS_NACL) | 22 #endif // defined(OS_NACL) |
| 19 | 23 |
| 20 PPB_Audio_Shared::PPB_Audio_Shared() | 24 PPB_Audio_Shared::PPB_Audio_Shared() |
| 21 : playing_(false), | 25 : playing_(false), |
| 22 shared_memory_size_(0), | 26 shared_memory_size_(0), |
| 23 #if defined(OS_NACL) | 27 #if defined(OS_NACL) |
| 24 thread_id_(0), | 28 thread_id_(0), |
| 25 thread_active_(false), | 29 thread_active_(false), |
| 26 #endif | 30 #endif |
| 27 callback_(NULL), | 31 callback_(NULL), |
| 28 user_data_(NULL) { | 32 user_data_(NULL), |
| 33 audio_buffer_size_(0) { |
| 29 } | 34 } |
| 30 | 35 |
| 31 PPB_Audio_Shared::~PPB_Audio_Shared() { | 36 PPB_Audio_Shared::~PPB_Audio_Shared() { |
| 32 StopThread(); | 37 StopThread(); |
| 33 } | 38 } |
| 34 | 39 |
| 35 void PPB_Audio_Shared::SetCallback(PPB_Audio_Callback callback, | 40 void PPB_Audio_Shared::SetCallback(PPB_Audio_Callback callback, |
| 36 void* user_data) { | 41 void* user_data) { |
| 37 callback_ = callback; | 42 callback_ = callback; |
| 38 user_data_ = user_data; | 43 user_data_ = user_data; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 66 size_t shared_memory_size, | 71 size_t shared_memory_size, |
| 67 base::SyncSocket::Handle socket_handle) { | 72 base::SyncSocket::Handle socket_handle) { |
| 68 socket_.reset(new base::CancelableSyncSocket(socket_handle)); | 73 socket_.reset(new base::CancelableSyncSocket(socket_handle)); |
| 69 shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false)); | 74 shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false)); |
| 70 shared_memory_size_ = shared_memory_size; | 75 shared_memory_size_ = shared_memory_size; |
| 71 | 76 |
| 72 if (!shared_memory_->Map( | 77 if (!shared_memory_->Map( |
| 73 media::TotalSharedMemorySizeInBytes(shared_memory_size_))) { | 78 media::TotalSharedMemorySizeInBytes(shared_memory_size_))) { |
| 74 PpapiGlobals::Get()->LogWithSource(instance, PP_LOGLEVEL_WARNING, "", | 79 PpapiGlobals::Get()->LogWithSource(instance, PP_LOGLEVEL_WARNING, "", |
| 75 "Failed to map shared memory for PPB_Audio_Shared."); | 80 "Failed to map shared memory for PPB_Audio_Shared."); |
| 81 } else { |
| 82 // Deduce the frame count from the size using hard coded channel count. |
| 83 audio_bus_ = media::AudioBus::WrapMemory( |
| 84 kChannels, shared_memory_size_ / (sizeof(float) * kChannels), |
| 85 shared_memory_.get()); |
| 86 // Setup integer audio buffer for user audio data. |
| 87 audio_buffer_size_ = |
| 88 audio_bus_->frames() * audio_bus_->channels() * kBytesPerSample; |
| 89 audio_buffer_.reset(new uint8_t[audio_buffer_size_]); |
| 76 } | 90 } |
| 77 | 91 |
| 78 StartThread(); | 92 StartThread(); |
| 79 } | 93 } |
| 80 | 94 |
| 81 void PPB_Audio_Shared::StartThread() { | 95 void PPB_Audio_Shared::StartThread() { |
| 82 // Don't start the thread unless all our state is set up correctly. | 96 // Don't start the thread unless all our state is set up correctly. |
| 83 if (!playing_ || !callback_ || !socket_.get() || !shared_memory_->memory()) | 97 if (!playing_ || !callback_ || !socket_.get() || !shared_memory_->memory() || |
| 98 !audio_bus_.get() || !audio_buffer_.get()) |
| 84 return; | 99 return; |
| 85 // Clear contents of shm buffer before starting audio thread. This will | 100 // Clear contents of shm buffer before starting audio thread. This will |
| 86 // prevent a burst of static if for some reason the audio thread doesn't | 101 // prevent a burst of static if for some reason the audio thread doesn't |
| 87 // start up quickly enough. | 102 // start up quickly enough. |
| 88 memset(shared_memory_->memory(), 0, shared_memory_size_); | 103 memset(shared_memory_->memory(), 0, shared_memory_size_); |
| 104 memset(audio_buffer_.get(), 0, audio_buffer_size_); |
| 89 #if !defined(OS_NACL) | 105 #if !defined(OS_NACL) |
| 90 DCHECK(!audio_thread_.get()); | 106 DCHECK(!audio_thread_.get()); |
| 91 audio_thread_.reset(new base::DelegateSimpleThread( | 107 audio_thread_.reset(new base::DelegateSimpleThread( |
| 92 this, "plugin_audio_thread")); | 108 this, "plugin_audio_thread")); |
| 93 audio_thread_->Start(); | 109 audio_thread_->Start(); |
| 94 #else | 110 #else |
| 95 // Use NaCl's special API for IRT code that creates threads that call back | 111 // Use NaCl's special API for IRT code that creates threads that call back |
| 96 // into user code. | 112 // into user code. |
| 97 if (NULL == thread_functions.thread_create || | 113 if (NULL == thread_functions.thread_create || |
| 98 NULL == thread_functions.thread_join) | 114 NULL == thread_functions.thread_join) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 | 149 |
| 134 // static | 150 // static |
| 135 void PPB_Audio_Shared::CallRun(void* self) { | 151 void PPB_Audio_Shared::CallRun(void* self) { |
| 136 PPB_Audio_Shared* audio = static_cast<PPB_Audio_Shared*>(self); | 152 PPB_Audio_Shared* audio = static_cast<PPB_Audio_Shared*>(self); |
| 137 audio->Run(); | 153 audio->Run(); |
| 138 } | 154 } |
| 139 #endif | 155 #endif |
| 140 | 156 |
| 141 void PPB_Audio_Shared::Run() { | 157 void PPB_Audio_Shared::Run() { |
| 142 int pending_data; | 158 int pending_data; |
| 143 void* buffer = shared_memory_->memory(); | |
| 144 | 159 |
| 145 while (sizeof(pending_data) == | 160 while (sizeof(pending_data) == |
| 146 socket_->Receive(&pending_data, sizeof(pending_data)) && | 161 socket_->Receive(&pending_data, sizeof(pending_data)) && |
| 147 pending_data != media::kPauseMark) { | 162 pending_data != media::kPauseMark) { |
| 148 callback_(buffer, shared_memory_size_, user_data_); | 163 callback_(audio_buffer_.get(), audio_buffer_size_, user_data_); |
| 164 |
| 165 // Deinterleave the audio data into the shared memory as float. |
| 166 audio_bus_->FromInterleaved( |
| 167 audio_buffer_.get(), audio_bus_->frames(), kBytesPerSample); |
| 149 | 168 |
| 150 // Let the host know we are done. | 169 // Let the host know we are done. |
| 151 media::SetActualDataSizeInBytes( | 170 media::SetActualDataSizeInBytes( |
| 152 shared_memory_.get(), shared_memory_size_, shared_memory_size_); | 171 shared_memory_.get(), shared_memory_size_, shared_memory_size_); |
| 153 } | 172 } |
| 154 } | 173 } |
| 155 | 174 |
| 156 } // namespace ppapi | 175 } // namespace ppapi |
| OLD | NEW |