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" | |
9 #include "ppapi/shared_impl/ppapi_globals.h" | 8 #include "ppapi/shared_impl/ppapi_globals.h" |
10 #include "ppapi/shared_impl/ppb_audio_config_shared.h" | 9 #include "ppapi/shared_impl/ppb_audio_config_shared.h" |
11 #include "ppapi/shared_impl/proxy_lock.h" | 10 #include "ppapi/shared_impl/proxy_lock.h" |
12 | 11 |
13 namespace ppapi { | 12 namespace ppapi { |
14 | 13 |
15 #if defined(OS_NACL) | 14 #if defined(OS_NACL) |
16 namespace { | 15 namespace { |
17 // Because this is static, the function pointers will be NULL initially. | 16 // Because this is static, the function pointers will be NULL initially. |
18 PP_ThreadFunctions thread_functions; | 17 PP_ThreadFunctions thread_functions; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 size_t shared_memory_size, | 106 size_t shared_memory_size, |
108 base::SyncSocket::Handle socket_handle, | 107 base::SyncSocket::Handle socket_handle, |
109 PP_AudioSampleRate sample_rate, | 108 PP_AudioSampleRate sample_rate, |
110 int sample_frame_count) { | 109 int sample_frame_count) { |
111 socket_.reset(new base::CancelableSyncSocket(socket_handle)); | 110 socket_.reset(new base::CancelableSyncSocket(socket_handle)); |
112 shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false)); | 111 shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false)); |
113 shared_memory_size_ = shared_memory_size; | 112 shared_memory_size_ = shared_memory_size; |
114 bytes_per_second_ = kAudioOutputChannels * (kBitsPerAudioOutputSample / 8) * | 113 bytes_per_second_ = kAudioOutputChannels * (kBitsPerAudioOutputSample / 8) * |
115 sample_rate; | 114 sample_rate; |
116 | 115 |
117 if (!shared_memory_->Map( | 116 if (!shared_memory_->Map(shared_memory_size_)) { |
118 media::TotalSharedMemorySizeInBytes(shared_memory_size_))) { | |
119 PpapiGlobals::Get()->LogWithSource( | 117 PpapiGlobals::Get()->LogWithSource( |
120 instance, | 118 instance, |
121 PP_LOGLEVEL_WARNING, | 119 PP_LOGLEVEL_WARNING, |
122 std::string(), | 120 std::string(), |
123 "Failed to map shared memory for PPB_Audio_Shared."); | 121 "Failed to map shared memory for PPB_Audio_Shared."); |
124 } else { | 122 } else { |
125 audio_bus_ = media::AudioBus::WrapMemory( | 123 audio_bus_ = media::AudioBus::WrapMemory( |
126 kAudioOutputChannels, sample_frame_count, shared_memory_->memory()); | 124 kAudioOutputChannels, sample_frame_count, shared_memory_->memory()); |
127 // Setup integer audio buffer for user audio data. | 125 // Setup integer audio buffer for user audio data. |
128 client_buffer_size_bytes_ = | 126 client_buffer_size_bytes_ = |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 } | 194 } |
197 | 195 |
198 // static | 196 // static |
199 void PPB_Audio_Shared::CallRun(void* self) { | 197 void PPB_Audio_Shared::CallRun(void* self) { |
200 PPB_Audio_Shared* audio = static_cast<PPB_Audio_Shared*>(self); | 198 PPB_Audio_Shared* audio = static_cast<PPB_Audio_Shared*>(self); |
201 audio->Run(); | 199 audio->Run(); |
202 } | 200 } |
203 #endif | 201 #endif |
204 | 202 |
205 void PPB_Audio_Shared::Run() { | 203 void PPB_Audio_Shared::Run() { |
206 int pending_data; | 204 int pending_data = 0; |
207 const int bytes_per_frame = | 205 uint32_t buffer_index = 0; |
208 sizeof(*audio_bus_->channel(0)) * audio_bus_->channels(); | |
209 | |
210 while (sizeof(pending_data) == | 206 while (sizeof(pending_data) == |
211 socket_->Receive(&pending_data, sizeof(pending_data)) && | 207 socket_->Receive(&pending_data, sizeof(pending_data)) && |
212 pending_data != media::kPauseMark) { | 208 pending_data >= 0) { |
213 PP_TimeDelta latency = | 209 PP_TimeDelta latency = |
214 static_cast<double>(pending_data) / bytes_per_second_; | 210 static_cast<double>(pending_data) / bytes_per_second_; |
215 callback_.Run(client_buffer_.get(), client_buffer_size_bytes_, latency, | 211 callback_.Run(client_buffer_.get(), client_buffer_size_bytes_, latency, |
216 user_data_); | 212 user_data_); |
217 | 213 |
218 // Deinterleave the audio data into the shared memory as float. | 214 // Deinterleave the audio data into the shared memory as float. |
219 audio_bus_->FromInterleaved( | 215 audio_bus_->FromInterleaved( |
220 client_buffer_.get(), audio_bus_->frames(), | 216 client_buffer_.get(), audio_bus_->frames(), |
221 kBitsPerAudioOutputSample / 8); | 217 kBitsPerAudioOutputSample / 8); |
222 | 218 |
223 // Let the host know we are done. | 219 ++buffer_index; |
224 // TODO(dalecurtis): Technically this is not the exact size. Due to channel | 220 size_t bytes_sent = socket_->Send(&buffer_index, sizeof(buffer_index)); |
225 // padding for alignment, there may be more data available than this. We're | 221 if (bytes_sent != sizeof(buffer_index)) |
226 // relying on AudioSyncReader::Read() to parse this with that in mind. | 222 break; |
227 // Rename these methods to Set/GetActualFrameCount(). | |
228 media::SetActualDataSizeInBytes( | |
229 shared_memory_.get(), shared_memory_size_, | |
230 audio_bus_->frames() * bytes_per_frame); | |
231 } | 223 } |
232 } | 224 } |
233 | 225 |
234 } // namespace ppapi | 226 } // namespace ppapi |
OLD | NEW |