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/cpp/dev/audio_input_dev.h" | 5 #include "ppapi/cpp/dev/audio_input_dev.h" |
6 | 6 |
7 #include "ppapi/c/dev/ppb_audio_input_dev.h" | |
8 #include "ppapi/c/pp_bool.h" | 7 #include "ppapi/c/pp_bool.h" |
9 #include "ppapi/c/pp_errors.h" | 8 #include "ppapi/c/pp_errors.h" |
10 #include "ppapi/cpp/completion_callback.h" | |
11 #include "ppapi/cpp/dev/device_ref_dev.h" | |
12 #include "ppapi/cpp/dev/resource_array_dev.h" | 9 #include "ppapi/cpp/dev/resource_array_dev.h" |
13 #include "ppapi/cpp/instance_handle.h" | 10 #include "ppapi/cpp/instance_handle.h" |
14 #include "ppapi/cpp/module.h" | |
15 #include "ppapi/cpp/module_impl.h" | 11 #include "ppapi/cpp/module_impl.h" |
16 | 12 |
17 namespace pp { | 13 namespace pp { |
18 | 14 |
19 namespace { | 15 namespace { |
20 | 16 |
21 template <> const char* interface_name<PPB_AudioInput_Dev_0_2>() { | 17 template <> const char* interface_name<PPB_AudioInput_Dev_0_2>() { |
22 return PPB_AUDIO_INPUT_DEV_INTERFACE_0_2; | 18 return PPB_AUDIO_INPUT_DEV_INTERFACE_0_2; |
23 } | 19 } |
24 | 20 |
25 template <> const char* interface_name<PPB_AudioInput_Dev_0_1>() { | 21 template <> const char* interface_name<PPB_AudioInput_Dev_0_1>() { |
26 return PPB_AUDIO_INPUT_DEV_INTERFACE_0_1; | 22 return PPB_AUDIO_INPUT_DEV_INTERFACE_0_1; |
27 } | 23 } |
28 | 24 |
29 } // namespace | 25 } // namespace |
30 | 26 |
31 struct AudioInput_Dev::EnumerateDevicesState { | 27 AudioInput_Dev::AudioInput_Dev() : audio_input_callback_(NULL), |
32 EnumerateDevicesState(std::vector<DeviceRef_Dev>* in_devices, | |
33 const CompletionCallback& in_callback, | |
34 AudioInput_Dev* in_audio_input) | |
35 : devices_resource(0), | |
36 devices(in_devices), | |
37 callback(in_callback), | |
38 audio_input(in_audio_input) { | |
39 } | |
40 | |
41 PP_Resource devices_resource; | |
42 // This is owned by the user of AudioInput_Dev::EnumerateDevices(). | |
43 std::vector<DeviceRef_Dev>* devices; | |
44 CompletionCallback callback; | |
45 AudioInput_Dev* audio_input; | |
46 }; | |
47 | |
48 AudioInput_Dev::AudioInput_Dev() : enum_state_(NULL), | |
49 audio_input_callback_(NULL), | |
50 user_data_(NULL) { | 28 user_data_(NULL) { |
51 } | 29 } |
52 | 30 |
53 AudioInput_Dev::AudioInput_Dev(const InstanceHandle& instance, | 31 AudioInput_Dev::AudioInput_Dev(const InstanceHandle& instance, |
54 const AudioConfig& config, | 32 const AudioConfig& config, |
55 PPB_AudioInput_Callback callback, | 33 PPB_AudioInput_Callback callback, |
56 void* user_data) | 34 void* user_data) |
57 : config_(config), | 35 : config_(config), |
58 enum_state_(NULL), | |
59 audio_input_callback_(callback), | 36 audio_input_callback_(callback), |
60 user_data_(user_data) { | 37 user_data_(user_data) { |
61 if (has_interface<PPB_AudioInput_Dev_0_2>()) { | 38 if (has_interface<PPB_AudioInput_Dev_0_2>()) { |
62 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev_0_2>()->Create( | 39 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev_0_2>()->Create( |
63 instance.pp_instance())); | 40 instance.pp_instance())); |
64 } else if (has_interface<PPB_AudioInput_Dev_0_1>()) { | 41 } else if (has_interface<PPB_AudioInput_Dev_0_1>()) { |
65 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev_0_1>()->Create( | 42 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev_0_1>()->Create( |
66 instance.pp_instance(), config.pp_resource(), callback, user_data)); | 43 instance.pp_instance(), config.pp_resource(), callback, user_data)); |
67 } | 44 } |
68 } | 45 } |
69 | 46 |
70 AudioInput_Dev::AudioInput_Dev(const InstanceHandle& instance) | 47 AudioInput_Dev::AudioInput_Dev(const InstanceHandle& instance) |
71 : enum_state_(NULL), | 48 : audio_input_callback_(NULL), |
72 audio_input_callback_(NULL), | |
73 user_data_(NULL) { | 49 user_data_(NULL) { |
74 if (has_interface<PPB_AudioInput_Dev_0_2>()) { | 50 if (has_interface<PPB_AudioInput_Dev_0_2>()) { |
75 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev_0_2>()->Create( | 51 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev_0_2>()->Create( |
76 instance.pp_instance())); | 52 instance.pp_instance())); |
77 } | 53 } |
78 } | 54 } |
79 | 55 |
80 AudioInput_Dev::AudioInput_Dev(const AudioInput_Dev& other) | |
81 : Resource(other), | |
82 config_(other.config_), | |
83 enum_state_(NULL), | |
84 audio_input_callback_(other.audio_input_callback_), | |
85 user_data_(other.user_data_) { | |
86 } | |
87 | |
88 AudioInput_Dev::~AudioInput_Dev() { | 56 AudioInput_Dev::~AudioInput_Dev() { |
89 AbortEnumerateDevices(); | |
90 } | |
91 | |
92 AudioInput_Dev& AudioInput_Dev::operator=(const AudioInput_Dev& other) { | |
93 AbortEnumerateDevices(); | |
94 | |
95 Resource::operator=(other); | |
96 config_ = other.config_; | |
97 audio_input_callback_ = other.audio_input_callback_; | |
98 user_data_ = other.user_data_; | |
99 return *this; | |
100 } | 57 } |
101 | 58 |
102 // static | 59 // static |
103 bool AudioInput_Dev::IsAvailable() { | 60 bool AudioInput_Dev::IsAvailable() { |
104 return has_interface<PPB_AudioInput_Dev_0_2>() || | 61 return has_interface<PPB_AudioInput_Dev_0_2>() || |
105 has_interface<PPB_AudioInput_Dev_0_1>(); | 62 has_interface<PPB_AudioInput_Dev_0_1>(); |
106 } | 63 } |
107 | 64 |
108 int32_t AudioInput_Dev::EnumerateDevices(std::vector<DeviceRef_Dev>* devices, | 65 int32_t AudioInput_Dev::EnumerateDevices( |
109 const CompletionCallback& callback) { | 66 const CompletionCallbackWithOutput<std::vector<DeviceRef_Dev> >& callback) { |
110 if (!has_interface<PPB_AudioInput_Dev_0_2>()) | 67 if (!has_interface<PPB_AudioInput_Dev_0_2>()) |
111 return callback.MayForce(PP_ERROR_NOINTERFACE); | 68 return callback.MayForce(PP_ERROR_NOINTERFACE); |
112 if (!devices) | |
113 return callback.MayForce(PP_ERROR_BADARGUMENT); | |
114 if (!callback.pp_completion_callback().func) | 69 if (!callback.pp_completion_callback().func) |
115 return callback.MayForce(PP_ERROR_BLOCKS_MAIN_THREAD); | 70 return callback.MayForce(PP_ERROR_BLOCKS_MAIN_THREAD); |
116 if (enum_state_) | |
117 return callback.MayForce(PP_ERROR_INPROGRESS); | |
118 | 71 |
119 // It will be deleted in OnEnumerateDevicesComplete(). | 72 // ArrayOutputCallbackConverter is responsible to delete it. |
120 enum_state_ = new EnumerateDevicesState(devices, callback, this); | 73 ResourceArray_Dev::ArrayOutputCallbackData* data = |
| 74 new ResourceArray_Dev::ArrayOutputCallbackData( |
| 75 callback.output(), callback.pp_completion_callback()); |
121 return get_interface<PPB_AudioInput_Dev_0_2>()->EnumerateDevices( | 76 return get_interface<PPB_AudioInput_Dev_0_2>()->EnumerateDevices( |
122 pp_resource(), &enum_state_->devices_resource, | 77 pp_resource(), &data->resource_array_output, |
123 PP_MakeCompletionCallback(&AudioInput_Dev::OnEnumerateDevicesComplete, | 78 PP_MakeCompletionCallback( |
124 enum_state_)); | 79 &ResourceArray_Dev::ArrayOutputCallbackConverter, data)); |
125 } | 80 } |
126 | 81 |
127 int32_t AudioInput_Dev::Open(const DeviceRef_Dev& device_ref, | 82 int32_t AudioInput_Dev::Open(const DeviceRef_Dev& device_ref, |
128 const CompletionCallback& callback) { | 83 const CompletionCallback& callback) { |
129 if (has_interface<PPB_AudioInput_Dev_0_2>()) { | 84 if (has_interface<PPB_AudioInput_Dev_0_2>()) { |
130 return get_interface<PPB_AudioInput_Dev_0_2>()->Open( | 85 return get_interface<PPB_AudioInput_Dev_0_2>()->Open( |
131 pp_resource(), device_ref.pp_resource(), config_.pp_resource(), | 86 pp_resource(), device_ref.pp_resource(), config_.pp_resource(), |
132 audio_input_callback_, user_data_, callback.pp_completion_callback()); | 87 audio_input_callback_, user_data_, callback.pp_completion_callback()); |
133 } | 88 } |
134 | 89 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 } | 144 } |
190 | 145 |
191 return false; | 146 return false; |
192 } | 147 } |
193 | 148 |
194 void AudioInput_Dev::Close() { | 149 void AudioInput_Dev::Close() { |
195 if (has_interface<PPB_AudioInput_Dev_0_2>()) | 150 if (has_interface<PPB_AudioInput_Dev_0_2>()) |
196 get_interface<PPB_AudioInput_Dev_0_2>()->Close(pp_resource()); | 151 get_interface<PPB_AudioInput_Dev_0_2>()->Close(pp_resource()); |
197 } | 152 } |
198 | 153 |
199 void AudioInput_Dev::AbortEnumerateDevices() { | |
200 if (enum_state_) { | |
201 enum_state_->devices = NULL; | |
202 Module::Get()->core()->CallOnMainThread(0, enum_state_->callback, | |
203 PP_ERROR_ABORTED); | |
204 enum_state_->audio_input = NULL; | |
205 enum_state_ = NULL; | |
206 } | |
207 } | |
208 | |
209 // static | |
210 void AudioInput_Dev::OnEnumerateDevicesComplete(void* user_data, | |
211 int32_t result) { | |
212 EnumerateDevicesState* enum_state = | |
213 static_cast<EnumerateDevicesState*>(user_data); | |
214 | |
215 bool need_to_callback = !!enum_state->audio_input; | |
216 | |
217 if (result == PP_OK) { | |
218 // It will take care of releasing the reference. | |
219 ResourceArray_Dev resources(pp::PASS_REF, | |
220 enum_state->devices_resource); | |
221 | |
222 if (need_to_callback) { | |
223 enum_state->devices->clear(); | |
224 for (uint32_t index = 0; index < resources.size(); ++index) { | |
225 DeviceRef_Dev device(resources[index]); | |
226 enum_state->devices->push_back(device); | |
227 } | |
228 } | |
229 } | |
230 | |
231 if (need_to_callback) { | |
232 enum_state->audio_input->enum_state_ = NULL; | |
233 enum_state->callback.Run(result); | |
234 } | |
235 | |
236 delete enum_state; | |
237 } | |
238 | |
239 } // namespace pp | 154 } // namespace pp |
OLD | NEW |