| 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 #include "ppapi/shared_impl/ppb_audio_config_shared.h" |
| 10 #include "ppapi/shared_impl/proxy_lock.h" | 11 #include "ppapi/shared_impl/proxy_lock.h" |
| 11 | 12 |
| 12 // Hard coded values from PepperPlatformAudioOutputImpl. | |
| 13 // TODO(dalecurtis): PPAPI shouldn't hard code these values for all clients. | |
| 14 enum { kChannels = 2, kBytesPerSample = 2 }; | |
| 15 | |
| 16 namespace ppapi { | 13 namespace ppapi { |
| 17 | 14 |
| 18 #if defined(OS_NACL) | 15 #if defined(OS_NACL) |
| 19 namespace { | 16 namespace { |
| 20 // Because this is static, the function pointers will be NULL initially. | 17 // Because this is static, the function pointers will be NULL initially. |
| 21 PP_ThreadFunctions thread_functions; | 18 PP_ThreadFunctions thread_functions; |
| 22 } | 19 } |
| 23 #endif // defined(OS_NACL) | 20 #endif // defined(OS_NACL) |
| 24 | 21 |
| 22 AudioCallbackCombined::AudioCallbackCombined() : callback_1_0_(NULL), |
| 23 callback_(NULL) { |
| 24 } |
| 25 |
| 26 AudioCallbackCombined::AudioCallbackCombined( |
| 27 PPB_Audio_Callback_1_0 callback_1_0) |
| 28 : callback_1_0_(callback_1_0), |
| 29 callback_(NULL) { |
| 30 } |
| 31 |
| 32 AudioCallbackCombined::AudioCallbackCombined(PPB_Audio_Callback callback) |
| 33 : callback_1_0_(NULL), |
| 34 callback_(callback) { |
| 35 } |
| 36 |
| 37 AudioCallbackCombined::~AudioCallbackCombined() { |
| 38 } |
| 39 |
| 40 bool AudioCallbackCombined::IsValid() const { |
| 41 return callback_1_0_ || callback_; |
| 42 } |
| 43 |
| 44 void AudioCallbackCombined::Run(void* sample_buffer, |
| 45 uint32_t buffer_size_in_bytes, |
| 46 PP_TimeDelta latency, |
| 47 void* user_data) const { |
| 48 if (callback_) { |
| 49 callback_(sample_buffer, buffer_size_in_bytes, latency, user_data); |
| 50 } else if (callback_1_0_) { |
| 51 callback_1_0_(sample_buffer, buffer_size_in_bytes, user_data); |
| 52 } else { |
| 53 NOTREACHED(); |
| 54 } |
| 55 } |
| 56 |
| 25 PPB_Audio_Shared::PPB_Audio_Shared() | 57 PPB_Audio_Shared::PPB_Audio_Shared() |
| 26 : playing_(false), | 58 : playing_(false), |
| 27 shared_memory_size_(0), | 59 shared_memory_size_(0), |
| 28 #if defined(OS_NACL) | 60 #if defined(OS_NACL) |
| 29 thread_id_(0), | 61 thread_id_(0), |
| 30 thread_active_(false), | 62 thread_active_(false), |
| 31 #endif | 63 #endif |
| 32 callback_(NULL), | |
| 33 user_data_(NULL), | 64 user_data_(NULL), |
| 34 client_buffer_size_bytes_(0) { | 65 client_buffer_size_bytes_(0), |
| 66 bytes_per_second_(0) { |
| 35 } | 67 } |
| 36 | 68 |
| 37 PPB_Audio_Shared::~PPB_Audio_Shared() { | 69 PPB_Audio_Shared::~PPB_Audio_Shared() { |
| 38 // Shut down the socket to escape any hanging |Receive|s. | 70 // Shut down the socket to escape any hanging |Receive|s. |
| 39 if (socket_.get()) | 71 if (socket_.get()) |
| 40 socket_->Shutdown(); | 72 socket_->Shutdown(); |
| 41 StopThread(); | 73 StopThread(); |
| 42 } | 74 } |
| 43 | 75 |
| 44 void PPB_Audio_Shared::SetCallback(PPB_Audio_Callback callback, | 76 void PPB_Audio_Shared::SetCallback(const AudioCallbackCombined& callback, |
| 45 void* user_data) { | 77 void* user_data) { |
| 46 callback_ = callback; | 78 callback_ = callback; |
| 47 user_data_ = user_data; | 79 user_data_ = user_data; |
| 48 } | 80 } |
| 49 | 81 |
| 50 void PPB_Audio_Shared::SetStartPlaybackState() { | 82 void PPB_Audio_Shared::SetStartPlaybackState() { |
| 51 DCHECK(!playing_); | 83 DCHECK(!playing_); |
| 52 #if !defined(OS_NACL) | 84 #if !defined(OS_NACL) |
| 53 DCHECK(!audio_thread_.get()); | 85 DCHECK(!audio_thread_.get()); |
| 54 #else | 86 #else |
| (...skipping 12 matching lines...) Expand all Loading... |
| 67 DCHECK(playing_); | 99 DCHECK(playing_); |
| 68 StopThread(); | 100 StopThread(); |
| 69 playing_ = false; | 101 playing_ = false; |
| 70 } | 102 } |
| 71 | 103 |
| 72 void PPB_Audio_Shared::SetStreamInfo( | 104 void PPB_Audio_Shared::SetStreamInfo( |
| 73 PP_Instance instance, | 105 PP_Instance instance, |
| 74 base::SharedMemoryHandle shared_memory_handle, | 106 base::SharedMemoryHandle shared_memory_handle, |
| 75 size_t shared_memory_size, | 107 size_t shared_memory_size, |
| 76 base::SyncSocket::Handle socket_handle, | 108 base::SyncSocket::Handle socket_handle, |
| 109 PP_AudioSampleRate sample_rate, |
| 77 int sample_frame_count) { | 110 int sample_frame_count) { |
| 78 socket_.reset(new base::CancelableSyncSocket(socket_handle)); | 111 socket_.reset(new base::CancelableSyncSocket(socket_handle)); |
| 79 shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false)); | 112 shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false)); |
| 80 shared_memory_size_ = shared_memory_size; | 113 shared_memory_size_ = shared_memory_size; |
| 114 bytes_per_second_ = kAudioOutputChannels * (kBitsPerAudioOutputSample / 8) * |
| 115 sample_rate; |
| 81 | 116 |
| 82 if (!shared_memory_->Map( | 117 if (!shared_memory_->Map( |
| 83 media::TotalSharedMemorySizeInBytes(shared_memory_size_))) { | 118 media::TotalSharedMemorySizeInBytes(shared_memory_size_))) { |
| 84 PpapiGlobals::Get()->LogWithSource( | 119 PpapiGlobals::Get()->LogWithSource( |
| 85 instance, | 120 instance, |
| 86 PP_LOGLEVEL_WARNING, | 121 PP_LOGLEVEL_WARNING, |
| 87 std::string(), | 122 std::string(), |
| 88 "Failed to map shared memory for PPB_Audio_Shared."); | 123 "Failed to map shared memory for PPB_Audio_Shared."); |
| 89 } else { | 124 } else { |
| 90 audio_bus_ = media::AudioBus::WrapMemory( | 125 audio_bus_ = media::AudioBus::WrapMemory( |
| 91 kChannels, sample_frame_count, shared_memory_->memory()); | 126 kAudioOutputChannels, sample_frame_count, shared_memory_->memory()); |
| 92 // Setup integer audio buffer for user audio data. | 127 // Setup integer audio buffer for user audio data. |
| 93 client_buffer_size_bytes_ = | 128 client_buffer_size_bytes_ = |
| 94 audio_bus_->frames() * audio_bus_->channels() * kBytesPerSample; | 129 audio_bus_->frames() * audio_bus_->channels() * |
| 130 kBitsPerAudioOutputSample / 8; |
| 95 client_buffer_.reset(new uint8_t[client_buffer_size_bytes_]); | 131 client_buffer_.reset(new uint8_t[client_buffer_size_bytes_]); |
| 96 } | 132 } |
| 97 | 133 |
| 98 StartThread(); | 134 StartThread(); |
| 99 } | 135 } |
| 100 | 136 |
| 101 void PPB_Audio_Shared::StartThread() { | 137 void PPB_Audio_Shared::StartThread() { |
| 102 // Don't start the thread unless all our state is set up correctly. | 138 // Don't start the thread unless all our state is set up correctly. |
| 103 if (!playing_ || !callback_ || !socket_.get() || !shared_memory_->memory() || | 139 if (!playing_ || !callback_.IsValid() || !socket_.get() || |
| 104 !audio_bus_.get() || !client_buffer_.get()) | 140 !shared_memory_->memory() || !audio_bus_.get() || !client_buffer_.get() || |
| 141 bytes_per_second_ == 0) |
| 105 return; | 142 return; |
| 106 // Clear contents of shm buffer before starting audio thread. This will | 143 // Clear contents of shm buffer before starting audio thread. This will |
| 107 // prevent a burst of static if for some reason the audio thread doesn't | 144 // prevent a burst of static if for some reason the audio thread doesn't |
| 108 // start up quickly enough. | 145 // start up quickly enough. |
| 109 memset(shared_memory_->memory(), 0, shared_memory_size_); | 146 memset(shared_memory_->memory(), 0, shared_memory_size_); |
| 110 memset(client_buffer_.get(), 0, client_buffer_size_bytes_); | 147 memset(client_buffer_.get(), 0, client_buffer_size_bytes_); |
| 111 #if !defined(OS_NACL) | 148 #if !defined(OS_NACL) |
| 112 DCHECK(!audio_thread_.get()); | 149 DCHECK(!audio_thread_.get()); |
| 113 audio_thread_.reset(new base::DelegateSimpleThread( | 150 audio_thread_.reset(new base::DelegateSimpleThread( |
| 114 this, "plugin_audio_thread")); | 151 this, "plugin_audio_thread")); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 #endif | 203 #endif |
| 167 | 204 |
| 168 void PPB_Audio_Shared::Run() { | 205 void PPB_Audio_Shared::Run() { |
| 169 int pending_data; | 206 int pending_data; |
| 170 const int bytes_per_frame = | 207 const int bytes_per_frame = |
| 171 sizeof(*audio_bus_->channel(0)) * audio_bus_->channels(); | 208 sizeof(*audio_bus_->channel(0)) * audio_bus_->channels(); |
| 172 | 209 |
| 173 while (sizeof(pending_data) == | 210 while (sizeof(pending_data) == |
| 174 socket_->Receive(&pending_data, sizeof(pending_data)) && | 211 socket_->Receive(&pending_data, sizeof(pending_data)) && |
| 175 pending_data != media::kPauseMark) { | 212 pending_data != media::kPauseMark) { |
| 176 callback_(client_buffer_.get(), client_buffer_size_bytes_, user_data_); | 213 PP_TimeDelta latency = |
| 214 static_cast<double>(pending_data) / bytes_per_second_; |
| 215 callback_.Run(client_buffer_.get(), client_buffer_size_bytes_, latency, |
| 216 user_data_); |
| 177 | 217 |
| 178 // Deinterleave the audio data into the shared memory as float. | 218 // Deinterleave the audio data into the shared memory as float. |
| 179 audio_bus_->FromInterleaved( | 219 audio_bus_->FromInterleaved( |
| 180 client_buffer_.get(), audio_bus_->frames(), kBytesPerSample); | 220 client_buffer_.get(), audio_bus_->frames(), |
| 221 kBitsPerAudioOutputSample / 8); |
| 181 | 222 |
| 182 // Let the host know we are done. | 223 // Let the host know we are done. |
| 183 // TODO(dalecurtis): Technically this is not the exact size. Due to channel | 224 // TODO(dalecurtis): Technically this is not the exact size. Due to channel |
| 184 // padding for alignment, there may be more data available than this. We're | 225 // padding for alignment, there may be more data available than this. We're |
| 185 // relying on AudioSyncReader::Read() to parse this with that in mind. | 226 // relying on AudioSyncReader::Read() to parse this with that in mind. |
| 186 // Rename these methods to Set/GetActualFrameCount(). | 227 // Rename these methods to Set/GetActualFrameCount(). |
| 187 media::SetActualDataSizeInBytes( | 228 media::SetActualDataSizeInBytes( |
| 188 shared_memory_.get(), shared_memory_size_, | 229 shared_memory_.get(), shared_memory_size_, |
| 189 audio_bus_->frames() * bytes_per_frame); | 230 audio_bus_->frames() * bytes_per_frame); |
| 190 } | 231 } |
| 191 } | 232 } |
| 192 | 233 |
| 193 } // namespace ppapi | 234 } // namespace ppapi |
| OLD | NEW |