| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ppapi/shared_impl/ppb_audio_input_shared.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "media/audio/audio_parameters.h" | |
| 9 #include "ppapi/c/pp_errors.h" | |
| 10 #include "ppapi/shared_impl/ppapi_globals.h" | |
| 11 #include "ppapi/shared_impl/ppb_device_ref_shared.h" | |
| 12 #include "ppapi/shared_impl/resource_tracker.h" | |
| 13 #include "ppapi/thunk/enter.h" | |
| 14 #include "ppapi/thunk/ppb_audio_config_api.h" | |
| 15 | |
| 16 namespace ppapi { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 void IgnoredCompletionCallback(void* user_data, int32_t result) { | |
| 21 // Do nothing. | |
| 22 } | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 26 PPB_AudioInput_Shared::PPB_AudioInput_Shared(const HostResource& audio_input) | |
| 27 : Resource(OBJECT_IS_PROXY, audio_input), | |
| 28 open_state_(BEFORE_OPEN), | |
| 29 capturing_(false), | |
| 30 shared_memory_size_(0), | |
| 31 audio_input_callback_(NULL), | |
| 32 user_data_(NULL), | |
| 33 devices_(NULL), | |
| 34 resource_object_type_(OBJECT_IS_PROXY) { | |
| 35 } | |
| 36 | |
| 37 PPB_AudioInput_Shared::PPB_AudioInput_Shared(PP_Instance instance) | |
| 38 : Resource(OBJECT_IS_IMPL, instance), | |
| 39 open_state_(BEFORE_OPEN), | |
| 40 capturing_(false), | |
| 41 shared_memory_size_(0), | |
| 42 audio_input_callback_(NULL), | |
| 43 user_data_(NULL), | |
| 44 devices_(NULL), | |
| 45 resource_object_type_(OBJECT_IS_IMPL) { | |
| 46 } | |
| 47 | |
| 48 PPB_AudioInput_Shared::~PPB_AudioInput_Shared() { | |
| 49 DCHECK(open_state_ == CLOSED); | |
| 50 } | |
| 51 | |
| 52 thunk::PPB_AudioInput_API* PPB_AudioInput_Shared::AsPPB_AudioInput_API() { | |
| 53 return this; | |
| 54 } | |
| 55 | |
| 56 int32_t PPB_AudioInput_Shared::EnumerateDevices( | |
| 57 PP_Resource* devices, | |
| 58 scoped_refptr<TrackedCallback> callback) { | |
| 59 if (TrackedCallback::IsPending(enumerate_devices_callback_)) | |
| 60 return PP_ERROR_INPROGRESS; | |
| 61 | |
| 62 return InternalEnumerateDevices(devices, callback); | |
| 63 } | |
| 64 | |
| 65 int32_t PPB_AudioInput_Shared::Open( | |
| 66 const std::string& device_id, | |
| 67 PP_Resource config, | |
| 68 PPB_AudioInput_Callback audio_input_callback, | |
| 69 void* user_data, | |
| 70 scoped_refptr<TrackedCallback> callback) { | |
| 71 if (!audio_input_callback) | |
| 72 return PP_ERROR_BADARGUMENT; | |
| 73 | |
| 74 return CommonOpen(device_id, config, audio_input_callback, user_data, | |
| 75 callback); | |
| 76 } | |
| 77 | |
| 78 PP_Resource PPB_AudioInput_Shared::GetCurrentConfig() { | |
| 79 // AddRef for the caller. | |
| 80 if (config_.get()) | |
| 81 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(config_); | |
| 82 return config_; | |
| 83 } | |
| 84 | |
| 85 PP_Bool PPB_AudioInput_Shared::StartCapture() { | |
| 86 if (open_state_ == CLOSED || (open_state_ == BEFORE_OPEN && | |
| 87 !TrackedCallback::IsPending(open_callback_))) { | |
| 88 return PP_FALSE; | |
| 89 } | |
| 90 if (capturing_) | |
| 91 return PP_TRUE; | |
| 92 | |
| 93 // If the audio input device hasn't been opened, set |capturing_| to true and | |
| 94 // return directly. Capturing will be started once the open operation is | |
| 95 // completed. | |
| 96 if (open_state_ == BEFORE_OPEN) { | |
| 97 capturing_ = true; | |
| 98 return PP_TRUE; | |
| 99 } | |
| 100 | |
| 101 return InternalStartCapture(); | |
| 102 } | |
| 103 | |
| 104 PP_Bool PPB_AudioInput_Shared::StopCapture() { | |
| 105 if (open_state_ == CLOSED) | |
| 106 return PP_FALSE; | |
| 107 if (!capturing_) | |
| 108 return PP_TRUE; | |
| 109 | |
| 110 // If the audio input device hasn't been opened, set |capturing_| to false and | |
| 111 // return directly. | |
| 112 if (open_state_ == BEFORE_OPEN) { | |
| 113 capturing_ = false; | |
| 114 return PP_TRUE; | |
| 115 } | |
| 116 | |
| 117 return InternalStopCapture(); | |
| 118 } | |
| 119 | |
| 120 void PPB_AudioInput_Shared::Close() { | |
| 121 if (open_state_ == CLOSED) | |
| 122 return; | |
| 123 | |
| 124 open_state_ = CLOSED; | |
| 125 InternalClose(); | |
| 126 StopThread(); | |
| 127 | |
| 128 if (TrackedCallback::IsPending(open_callback_)) | |
| 129 open_callback_->PostAbort(); | |
| 130 } | |
| 131 | |
| 132 void PPB_AudioInput_Shared::OnEnumerateDevicesComplete( | |
| 133 int32_t result, | |
| 134 const std::vector<DeviceRefData>& devices) { | |
| 135 DCHECK(TrackedCallback::IsPending(enumerate_devices_callback_)); | |
| 136 | |
| 137 if (result == PP_OK && devices_) { | |
| 138 *devices_ = PPB_DeviceRef_Shared::CreateResourceArray( | |
| 139 resource_object_type_, pp_instance(), devices); | |
| 140 } | |
| 141 devices_ = NULL; | |
| 142 | |
| 143 enumerate_devices_callback_->Run(result); | |
| 144 } | |
| 145 | |
| 146 void PPB_AudioInput_Shared::OnOpenComplete( | |
| 147 int32_t result, | |
| 148 base::SharedMemoryHandle shared_memory_handle, | |
| 149 size_t shared_memory_size, | |
| 150 base::SyncSocket::Handle socket_handle) { | |
| 151 if (open_state_ == BEFORE_OPEN && result == PP_OK) { | |
| 152 open_state_ = OPENED; | |
| 153 SetStreamInfo(shared_memory_handle, shared_memory_size, socket_handle); | |
| 154 } else { | |
| 155 // Clean up the handles. | |
| 156 base::SyncSocket temp_socket(socket_handle); | |
| 157 base::SharedMemory temp_mem(shared_memory_handle, false); | |
| 158 capturing_ = false; | |
| 159 } | |
| 160 | |
| 161 // The callback may have been aborted by Close(). | |
| 162 if (TrackedCallback::IsPending(open_callback_)) | |
| 163 open_callback_->Run(result); | |
| 164 } | |
| 165 | |
| 166 // static | |
| 167 scoped_refptr<TrackedCallback> | |
| 168 PPB_AudioInput_Shared::MakeIgnoredCompletionCallback( | |
| 169 Resource* resource) { | |
| 170 return new TrackedCallback(resource, | |
| 171 PP_MakeCompletionCallback(&IgnoredCompletionCallback, NULL)); | |
| 172 } | |
| 173 | |
| 174 void PPB_AudioInput_Shared::SetStartCaptureState() { | |
| 175 DCHECK(!capturing_); | |
| 176 DCHECK(!audio_input_thread_.get()); | |
| 177 | |
| 178 capturing_ = true; | |
| 179 StartThread(); | |
| 180 } | |
| 181 | |
| 182 void PPB_AudioInput_Shared::SetStopCaptureState() { | |
| 183 DCHECK(capturing_); | |
| 184 StopThread(); | |
| 185 capturing_ = false; | |
| 186 } | |
| 187 | |
| 188 void PPB_AudioInput_Shared::SetStreamInfo( | |
| 189 base::SharedMemoryHandle shared_memory_handle, | |
| 190 size_t shared_memory_size, | |
| 191 base::SyncSocket::Handle socket_handle) { | |
| 192 socket_.reset(new base::CancelableSyncSocket(socket_handle)); | |
| 193 shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false)); | |
| 194 shared_memory_size_ = shared_memory_size; | |
| 195 | |
| 196 if (!shared_memory_->Map(shared_memory_size_)) { | |
| 197 PpapiGlobals::Get()->LogWithSource(pp_instance(), PP_LOGLEVEL_WARNING, "", | |
| 198 "Failed to map shared memory for PPB_AudioInput_Shared."); | |
| 199 } | |
| 200 | |
| 201 // There is a pending capture request before SetStreamInfo(). | |
| 202 if (capturing_) { | |
| 203 // Set |capturing_| to false so that the state looks consistent to | |
| 204 // InternalStartCapture(). It will be reset to true by | |
| 205 // SetStartCaptureState(). | |
| 206 capturing_ = false; | |
| 207 InternalStartCapture(); | |
| 208 } | |
| 209 } | |
| 210 | |
| 211 void PPB_AudioInput_Shared::StartThread() { | |
| 212 // Don't start the thread unless all our state is set up correctly. | |
| 213 if (!audio_input_callback_ || !socket_.get() || !capturing_ || | |
| 214 !shared_memory_->memory()) { | |
| 215 return; | |
| 216 } | |
| 217 DCHECK(!audio_input_thread_.get()); | |
| 218 audio_input_thread_.reset(new base::DelegateSimpleThread( | |
| 219 this, "plugin_audio_input_thread")); | |
| 220 audio_input_thread_->Start(); | |
| 221 } | |
| 222 | |
| 223 void PPB_AudioInput_Shared::StopThread() { | |
| 224 // Shut down the socket to escape any hanging |Receive|s. | |
| 225 if (socket_.get()) | |
| 226 socket_->Shutdown(); | |
| 227 if (audio_input_thread_.get()) { | |
| 228 audio_input_thread_->Join(); | |
| 229 audio_input_thread_.reset(); | |
| 230 } | |
| 231 } | |
| 232 | |
| 233 void PPB_AudioInput_Shared::Run() { | |
| 234 // The shared memory represents AudioInputBufferParameters and the actual data | |
| 235 // buffer. | |
| 236 media::AudioInputBuffer* buffer = | |
| 237 static_cast<media::AudioInputBuffer*>(shared_memory_->memory()); | |
| 238 uint32_t data_buffer_size = | |
| 239 shared_memory_size_ - sizeof(media::AudioInputBufferParameters); | |
| 240 int pending_data; | |
| 241 | |
| 242 while (sizeof(pending_data) == socket_->Receive(&pending_data, | |
| 243 sizeof(pending_data)) && | |
| 244 pending_data >= 0) { | |
| 245 // While closing the stream, we may receive buffers whose size is different | |
| 246 // from |data_buffer_size|. | |
| 247 CHECK_LE(buffer->params.size, data_buffer_size); | |
| 248 if (buffer->params.size > 0) | |
| 249 audio_input_callback_(&buffer->audio[0], buffer->params.size, user_data_); | |
| 250 } | |
| 251 } | |
| 252 | |
| 253 int32_t PPB_AudioInput_Shared::CommonOpen( | |
| 254 const std::string& device_id, | |
| 255 PP_Resource config, | |
| 256 PPB_AudioInput_Callback audio_input_callback, | |
| 257 void* user_data, | |
| 258 scoped_refptr<TrackedCallback> callback) { | |
| 259 if (open_state_ != BEFORE_OPEN) | |
| 260 return PP_ERROR_FAILED; | |
| 261 | |
| 262 thunk::EnterResourceNoLock<thunk::PPB_AudioConfig_API> enter_config(config, | |
| 263 true); | |
| 264 if (enter_config.failed()) | |
| 265 return PP_ERROR_BADARGUMENT; | |
| 266 | |
| 267 if (TrackedCallback::IsPending(open_callback_)) | |
| 268 return PP_ERROR_INPROGRESS; | |
| 269 | |
| 270 config_ = config; | |
| 271 audio_input_callback_ = audio_input_callback; | |
| 272 user_data_ = user_data; | |
| 273 | |
| 274 return InternalOpen(device_id, enter_config.object()->GetSampleRate(), | |
| 275 enter_config.object()->GetSampleFrameCount(), callback); | |
| 276 } | |
| 277 | |
| 278 } // namespace ppapi | |
| OLD | NEW |