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