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/proxy/audio_input_resource.h" | 5 #include "ppapi/proxy/audio_input_resource.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "ipc/ipc_platform_file.h" | 9 #include "ipc/ipc_platform_file.h" |
10 #include "media/audio/audio_parameters.h" | 10 #include "media/audio/audio_parameters.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 namespace ppapi { | 22 namespace ppapi { |
23 namespace proxy { | 23 namespace proxy { |
24 | 24 |
25 AudioInputResource::AudioInputResource( | 25 AudioInputResource::AudioInputResource( |
26 Connection connection, | 26 Connection connection, |
27 PP_Instance instance) | 27 PP_Instance instance) |
28 : PluginResource(connection, instance), | 28 : PluginResource(connection, instance), |
29 open_state_(BEFORE_OPEN), | 29 open_state_(BEFORE_OPEN), |
30 capturing_(false), | 30 capturing_(false), |
31 shared_memory_size_(0), | 31 shared_memory_size_(0), |
32 audio_input_callback_0_2_(NULL), | 32 audio_input_callback_0_3_(NULL), |
33 audio_input_callback_(NULL), | 33 audio_input_callback_(NULL), |
34 user_data_(NULL), | 34 user_data_(NULL), |
35 enumeration_helper_(this), | 35 enumeration_helper_(this), |
36 bytes_per_second_(0) { | 36 bytes_per_second_(0) { |
37 SendCreate(RENDERER, PpapiHostMsg_AudioInput_Create()); | 37 SendCreate(RENDERER, PpapiHostMsg_AudioInput_Create()); |
38 } | 38 } |
39 | 39 |
40 AudioInputResource::~AudioInputResource() { | 40 AudioInputResource::~AudioInputResource() { |
41 Close(); | 41 Close(); |
42 } | 42 } |
43 | 43 |
44 thunk::PPB_AudioInput_API* AudioInputResource::AsPPB_AudioInput_API() { | 44 thunk::PPB_AudioInput_API* AudioInputResource::AsPPB_AudioInput_API() { |
45 return this; | 45 return this; |
46 } | 46 } |
47 | 47 |
48 void AudioInputResource::OnReplyReceived( | 48 void AudioInputResource::OnReplyReceived( |
49 const ResourceMessageReplyParams& params, | 49 const ResourceMessageReplyParams& params, |
50 const IPC::Message& msg) { | 50 const IPC::Message& msg) { |
51 if (!enumeration_helper_.HandleReply(params, msg)) | 51 if (!enumeration_helper_.HandleReply(params, msg)) |
52 PluginResource::OnReplyReceived(params, msg); | 52 PluginResource::OnReplyReceived(params, msg); |
53 } | 53 } |
54 | 54 |
55 int32_t AudioInputResource::EnumerateDevices0_2( | |
56 PP_Resource* devices, | |
57 scoped_refptr<TrackedCallback> callback) { | |
58 return enumeration_helper_.EnumerateDevices0_2(devices, callback); | |
59 } | |
60 | |
61 int32_t AudioInputResource::EnumerateDevices( | 55 int32_t AudioInputResource::EnumerateDevices( |
62 const PP_ArrayOutput& output, | 56 const PP_ArrayOutput& output, |
63 scoped_refptr<TrackedCallback> callback) { | 57 scoped_refptr<TrackedCallback> callback) { |
64 return enumeration_helper_.EnumerateDevices(output, callback); | 58 return enumeration_helper_.EnumerateDevices(output, callback); |
65 } | 59 } |
66 | 60 |
67 int32_t AudioInputResource::MonitorDeviceChange( | 61 int32_t AudioInputResource::MonitorDeviceChange( |
68 PP_MonitorDeviceChangeCallback callback, | 62 PP_MonitorDeviceChangeCallback callback, |
69 void* user_data) { | 63 void* user_data) { |
70 return enumeration_helper_.MonitorDeviceChange(callback, user_data); | 64 return enumeration_helper_.MonitorDeviceChange(callback, user_data); |
71 } | 65 } |
72 | 66 |
73 int32_t AudioInputResource::Open0_2( | 67 int32_t AudioInputResource::Open0_3( |
74 PP_Resource device_ref, | 68 PP_Resource device_ref, |
75 PP_Resource config, | 69 PP_Resource config, |
76 PPB_AudioInput_Callback_0_2 audio_input_callback_0_2, | 70 PPB_AudioInput_Callback_0_3 audio_input_callback_0_3, |
77 void* user_data, | 71 void* user_data, |
78 scoped_refptr<TrackedCallback> callback) { | 72 scoped_refptr<TrackedCallback> callback) { |
79 return CommonOpen(device_ref, config, audio_input_callback_0_2, NULL, | 73 return CommonOpen(device_ref, config, audio_input_callback_0_3, NULL, |
80 user_data, callback); | 74 user_data, callback); |
81 } | 75 } |
82 | 76 |
83 int32_t AudioInputResource::Open(PP_Resource device_ref, | 77 int32_t AudioInputResource::Open(PP_Resource device_ref, |
84 PP_Resource config, | 78 PP_Resource config, |
85 PPB_AudioInput_Callback audio_input_callback, | 79 PPB_AudioInput_Callback audio_input_callback, |
86 void* user_data, | 80 void* user_data, |
87 scoped_refptr<TrackedCallback> callback) { | 81 scoped_refptr<TrackedCallback> callback) { |
88 return CommonOpen(device_ref, config, NULL, audio_input_callback, user_data, | 82 return CommonOpen(device_ref, config, NULL, audio_input_callback, user_data, |
89 callback); | 83 callback); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 if (capturing_) { | 194 if (capturing_) { |
201 // Set |capturing_| to false so that the state looks consistent to | 195 // Set |capturing_| to false so that the state looks consistent to |
202 // StartCapture(), which will reset it to true. | 196 // StartCapture(), which will reset it to true. |
203 capturing_ = false; | 197 capturing_ = false; |
204 StartCapture(); | 198 StartCapture(); |
205 } | 199 } |
206 } | 200 } |
207 | 201 |
208 void AudioInputResource::StartThread() { | 202 void AudioInputResource::StartThread() { |
209 // Don't start the thread unless all our state is set up correctly. | 203 // Don't start the thread unless all our state is set up correctly. |
210 if ((!audio_input_callback_0_2_ && !audio_input_callback_) || | 204 if ((!audio_input_callback_0_3_ && !audio_input_callback_) || |
211 !socket_.get() || !capturing_ || !shared_memory_->memory()) { | 205 !socket_.get() || !capturing_ || !shared_memory_->memory()) { |
212 return; | 206 return; |
213 } | 207 } |
214 DCHECK(!audio_input_thread_.get()); | 208 DCHECK(!audio_input_thread_.get()); |
215 audio_input_thread_.reset(new base::DelegateSimpleThread( | 209 audio_input_thread_.reset(new base::DelegateSimpleThread( |
216 this, "plugin_audio_input_thread")); | 210 this, "plugin_audio_input_thread")); |
217 audio_input_thread_->Start(); | 211 audio_input_thread_->Start(); |
218 } | 212 } |
219 | 213 |
220 void AudioInputResource::StopThread() { | 214 void AudioInputResource::StopThread() { |
(...skipping 21 matching lines...) Expand all Loading... |
242 // While closing the stream, we may receive buffers whose size is different | 236 // While closing the stream, we may receive buffers whose size is different |
243 // from |data_buffer_size|. | 237 // from |data_buffer_size|. |
244 CHECK_LE(buffer->params.size, data_buffer_size); | 238 CHECK_LE(buffer->params.size, data_buffer_size); |
245 if (buffer->params.size > 0) { | 239 if (buffer->params.size > 0) { |
246 if (audio_input_callback_) { | 240 if (audio_input_callback_) { |
247 PP_TimeDelta latency = | 241 PP_TimeDelta latency = |
248 static_cast<double>(pending_data) / bytes_per_second_; | 242 static_cast<double>(pending_data) / bytes_per_second_; |
249 audio_input_callback_(&buffer->audio[0], buffer->params.size, latency, | 243 audio_input_callback_(&buffer->audio[0], buffer->params.size, latency, |
250 user_data_); | 244 user_data_); |
251 } else { | 245 } else { |
252 audio_input_callback_0_2_(&buffer->audio[0], buffer->params.size, | 246 audio_input_callback_0_3_(&buffer->audio[0], buffer->params.size, |
253 user_data_); | 247 user_data_); |
254 } | 248 } |
255 } | 249 } |
256 } | 250 } |
257 } | 251 } |
258 | 252 |
259 int32_t AudioInputResource::CommonOpen( | 253 int32_t AudioInputResource::CommonOpen( |
260 PP_Resource device_ref, | 254 PP_Resource device_ref, |
261 PP_Resource config, | 255 PP_Resource config, |
262 PPB_AudioInput_Callback_0_2 audio_input_callback_0_2, | 256 PPB_AudioInput_Callback_0_3 audio_input_callback_0_3, |
263 PPB_AudioInput_Callback audio_input_callback, | 257 PPB_AudioInput_Callback audio_input_callback, |
264 void* user_data, | 258 void* user_data, |
265 scoped_refptr<TrackedCallback> callback) { | 259 scoped_refptr<TrackedCallback> callback) { |
266 std::string device_id; | 260 std::string device_id; |
267 // |device_id| remains empty if |device_ref| is 0, which means the default | 261 // |device_id| remains empty if |device_ref| is 0, which means the default |
268 // device. | 262 // device. |
269 if (device_ref != 0) { | 263 if (device_ref != 0) { |
270 thunk::EnterResourceNoLock<thunk::PPB_DeviceRef_API> enter_device_ref( | 264 thunk::EnterResourceNoLock<thunk::PPB_DeviceRef_API> enter_device_ref( |
271 device_ref, true); | 265 device_ref, true); |
272 if (enter_device_ref.failed()) | 266 if (enter_device_ref.failed()) |
273 return PP_ERROR_BADRESOURCE; | 267 return PP_ERROR_BADRESOURCE; |
274 device_id = enter_device_ref.object()->GetDeviceRefData().id; | 268 device_id = enter_device_ref.object()->GetDeviceRefData().id; |
275 } | 269 } |
276 | 270 |
277 if (TrackedCallback::IsPending(open_callback_)) | 271 if (TrackedCallback::IsPending(open_callback_)) |
278 return PP_ERROR_INPROGRESS; | 272 return PP_ERROR_INPROGRESS; |
279 if (open_state_ != BEFORE_OPEN) | 273 if (open_state_ != BEFORE_OPEN) |
280 return PP_ERROR_FAILED; | 274 return PP_ERROR_FAILED; |
281 | 275 |
282 if (!audio_input_callback_0_2 && !audio_input_callback) | 276 if (!audio_input_callback_0_3 && !audio_input_callback) |
283 return PP_ERROR_BADARGUMENT; | 277 return PP_ERROR_BADARGUMENT; |
284 thunk::EnterResourceNoLock<thunk::PPB_AudioConfig_API> enter_config(config, | 278 thunk::EnterResourceNoLock<thunk::PPB_AudioConfig_API> enter_config(config, |
285 true); | 279 true); |
286 if (enter_config.failed()) | 280 if (enter_config.failed()) |
287 return PP_ERROR_BADARGUMENT; | 281 return PP_ERROR_BADARGUMENT; |
288 | 282 |
289 config_ = config; | 283 config_ = config; |
290 audio_input_callback_0_2_ = audio_input_callback_0_2; | 284 audio_input_callback_0_3_ = audio_input_callback_0_3; |
291 audio_input_callback_ = audio_input_callback; | 285 audio_input_callback_ = audio_input_callback; |
292 user_data_ = user_data; | 286 user_data_ = user_data; |
293 open_callback_ = callback; | 287 open_callback_ = callback; |
294 bytes_per_second_ = kAudioInputChannels * (kBitsPerAudioInputSample / 8) * | 288 bytes_per_second_ = kAudioInputChannels * (kBitsPerAudioInputSample / 8) * |
295 enter_config.object()->GetSampleRate(); | 289 enter_config.object()->GetSampleRate(); |
296 | 290 |
297 PpapiHostMsg_AudioInput_Open msg( | 291 PpapiHostMsg_AudioInput_Open msg( |
298 device_id, enter_config.object()->GetSampleRate(), | 292 device_id, enter_config.object()->GetSampleRate(), |
299 enter_config.object()->GetSampleFrameCount()); | 293 enter_config.object()->GetSampleFrameCount()); |
300 Call<PpapiPluginMsg_AudioInput_OpenReply>( | 294 Call<PpapiPluginMsg_AudioInput_OpenReply>( |
301 RENDERER, msg, | 295 RENDERER, msg, |
302 base::Bind(&AudioInputResource::OnPluginMsgOpenReply, | 296 base::Bind(&AudioInputResource::OnPluginMsgOpenReply, |
303 base::Unretained(this))); | 297 base::Unretained(this))); |
304 return PP_OK_COMPLETIONPENDING; | 298 return PP_OK_COMPLETIONPENDING; |
305 } | 299 } |
306 } // namespace proxy | 300 } // namespace proxy |
307 } // namespace ppapi | 301 } // namespace ppapi |
OLD | NEW |