Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 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" | 7 #include "ppapi/c/dev/ppb_audio_input_dev.h" |
| 8 #include "ppapi/c/pp_bool.h" | |
| 8 #include "ppapi/c/pp_errors.h" | 9 #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/instance_handle.h" | 13 #include "ppapi/cpp/instance_handle.h" |
| 10 #include "ppapi/cpp/module.h" | 14 #include "ppapi/cpp/module.h" |
| 11 #include "ppapi/cpp/module_impl.h" | 15 #include "ppapi/cpp/module_impl.h" |
| 12 | 16 |
| 13 namespace pp { | 17 namespace pp { |
| 14 | 18 |
| 15 namespace { | 19 namespace { |
| 16 | 20 |
| 17 template <> const char* interface_name<PPB_AudioInput_Dev>() { | 21 template <> const char* interface_name<PPB_AudioInput_Dev_0_2>() { |
| 18 return PPB_AUDIO_INPUT_DEV_INTERFACE; | 22 return PPB_AUDIO_INPUT_DEV_INTERFACE_0_2; |
| 23 } | |
| 24 | |
| 25 template <> const char* interface_name<PPB_AudioInput_Dev_0_1>() { | |
| 26 return PPB_AUDIO_INPUT_DEV_INTERFACE_0_1; | |
| 19 } | 27 } |
| 20 | 28 |
| 21 } // namespace | 29 } // namespace |
| 22 | 30 |
| 31 struct AudioInput_Dev::EnumerateDevicesState { | |
| 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 std::vector<DeviceRef_Dev>* devices; | |
|
viettrungluu
2012/03/06 19:17:28
Probably add a comment that it's owned by the "use
yzshen1
2012/03/07 00:30:36
Done.
| |
| 43 CompletionCallback callback; | |
| 44 AudioInput_Dev* audio_input; | |
| 45 }; | |
| 46 | |
| 47 AudioInput_Dev::AudioInput_Dev() : enum_state_(NULL), | |
| 48 audio_input_callback_(NULL), | |
| 49 user_data_(NULL) { | |
| 50 } | |
| 51 | |
| 23 AudioInput_Dev::AudioInput_Dev(const InstanceHandle& instance, | 52 AudioInput_Dev::AudioInput_Dev(const InstanceHandle& instance, |
| 24 const AudioConfig& config, | 53 const AudioConfig& config, |
| 25 PPB_AudioInput_Callback callback, | 54 PPB_AudioInput_Callback callback, |
| 26 void* user_data) | 55 void* user_data) |
| 27 : config_(config) { | 56 : config_(config), |
| 28 if (has_interface<PPB_AudioInput_Dev>()) { | 57 enum_state_(NULL), |
| 29 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev>()->Create( | 58 audio_input_callback_(callback), |
| 59 user_data_(user_data) { | |
| 60 if (has_interface<PPB_AudioInput_Dev_0_2>()) { | |
| 61 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev_0_2>()->Create( | |
| 62 instance.pp_instance())); | |
| 63 } else if (has_interface<PPB_AudioInput_Dev_0_1>()) { | |
| 64 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev_0_1>()->Create( | |
| 30 instance.pp_instance(), config.pp_resource(), callback, user_data)); | 65 instance.pp_instance(), config.pp_resource(), callback, user_data)); |
| 31 } | 66 } |
| 32 } | 67 } |
| 33 | 68 |
| 69 AudioInput_Dev::AudioInput_Dev(const InstanceHandle& instance) | |
| 70 : enum_state_(NULL), | |
| 71 audio_input_callback_(NULL), | |
| 72 user_data_(NULL) { | |
| 73 if (has_interface<PPB_AudioInput_Dev_0_2>()) { | |
| 74 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev_0_2>()->Create( | |
| 75 instance.pp_instance())); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 AudioInput_Dev::AudioInput_Dev(const AudioInput_Dev& other) | |
| 80 : Resource(other), | |
| 81 config_(other.config_), | |
| 82 enum_state_(NULL), | |
| 83 audio_input_callback_(other.audio_input_callback_), | |
| 84 user_data_(other.user_data_) { | |
| 85 } | |
| 86 | |
| 87 AudioInput_Dev::~AudioInput_Dev() { | |
| 88 AbortEnumerateDevices(); | |
| 89 } | |
| 90 | |
| 91 AudioInput_Dev& AudioInput_Dev::operator=(const AudioInput_Dev& other) { | |
| 92 AbortEnumerateDevices(); | |
| 93 | |
| 94 Resource::operator=(other); | |
| 95 config_ = other.config_; | |
| 96 audio_input_callback_ = other.audio_input_callback_; | |
| 97 user_data_ = other.user_data_; | |
| 98 return *this; | |
| 99 } | |
| 100 | |
| 34 // static | 101 // static |
| 35 bool AudioInput_Dev::IsAvailable() { | 102 bool AudioInput_Dev::IsAvailable() { |
| 36 return has_interface<PPB_AudioInput_Dev>(); | 103 return has_interface<PPB_AudioInput_Dev_0_2>() || |
| 104 has_interface<PPB_AudioInput_Dev_0_1>(); | |
| 105 } | |
| 106 | |
| 107 int32_t AudioInput_Dev::EnumerateDevices(std::vector<DeviceRef_Dev>* devices, | |
| 108 const CompletionCallback& callback) { | |
| 109 if (!has_interface<PPB_AudioInput_Dev_0_2>()) | |
| 110 return callback.MayForce(PP_ERROR_NOINTERFACE); | |
| 111 if (!devices) | |
| 112 return callback.MayForce(PP_ERROR_BADARGUMENT); | |
| 113 if (!callback.pp_completion_callback().func) | |
| 114 return callback.MayForce(PP_ERROR_BLOCKS_MAIN_THREAD); | |
| 115 if (enum_state_) | |
| 116 return callback.MayForce(PP_ERROR_INPROGRESS); | |
| 117 | |
| 118 // It will be deleted in OnEnumerateDevicesComplete(). | |
| 119 enum_state_ = new EnumerateDevicesState(devices, callback, this); | |
| 120 return get_interface<PPB_AudioInput_Dev_0_2>()->EnumerateDevices( | |
| 121 pp_resource(), &enum_state_->devices_resource, | |
| 122 PP_MakeCompletionCallback(&AudioInput_Dev::OnEnumerateDevicesComplete, | |
| 123 enum_state_)); | |
| 124 } | |
| 125 | |
| 126 int32_t AudioInput_Dev::Open(const DeviceRef_Dev& device_ref, | |
| 127 const CompletionCallback& callback) { | |
| 128 if (has_interface<PPB_AudioInput_Dev_0_2>()) { | |
| 129 return get_interface<PPB_AudioInput_Dev_0_2>()->Open( | |
| 130 pp_resource(), device_ref.pp_resource(), config_.pp_resource(), | |
| 131 audio_input_callback_, user_data_, callback.pp_completion_callback()); | |
| 132 } | |
| 133 | |
| 134 if (has_interface<PPB_AudioInput_Dev_0_1>()) { | |
| 135 if (is_null()) | |
| 136 return callback.MayForce(PP_ERROR_FAILED); | |
| 137 | |
| 138 // If the v0.1 interface is being used and there is a valid resource handle, | |
| 139 // then the default device has been successfully opened during resource | |
| 140 // creation. | |
| 141 if (device_ref.is_null()) | |
| 142 return callback.MayForce(PP_OK); | |
| 143 | |
| 144 // The v0.1 interface doesn't support devices other than the default one. | |
| 145 return callback.MayForce(PP_ERROR_NOTSUPPORTED); | |
| 146 } | |
| 147 | |
| 148 return callback.MayForce(PP_ERROR_NOINTERFACE); | |
| 149 } | |
| 150 | |
| 151 int32_t AudioInput_Dev::Open(const DeviceRef_Dev& device_ref, | |
| 152 const AudioConfig& config, | |
| 153 PPB_AudioInput_Callback audio_input_callback, | |
| 154 void* user_data, | |
| 155 const CompletionCallback& callback) { | |
| 156 if (has_interface<PPB_AudioInput_Dev_0_2>()) { | |
| 157 return get_interface<PPB_AudioInput_Dev_0_2>()->Open( | |
| 158 pp_resource(), device_ref.pp_resource(), config.pp_resource(), | |
| 159 audio_input_callback, user_data, callback.pp_completion_callback()); | |
| 160 } | |
| 161 | |
| 162 return callback.MayForce(PP_ERROR_NOINTERFACE); | |
| 37 } | 163 } |
| 38 | 164 |
| 39 bool AudioInput_Dev::StartCapture() { | 165 bool AudioInput_Dev::StartCapture() { |
| 40 return has_interface<PPB_AudioInput_Dev>() && | 166 if (has_interface<PPB_AudioInput_Dev_0_2>()) { |
| 41 get_interface<PPB_AudioInput_Dev>()->StartCapture(pp_resource()); | 167 return PP_ToBool(get_interface<PPB_AudioInput_Dev_0_2>()->StartCapture( |
| 168 pp_resource())); | |
| 169 } | |
| 170 | |
| 171 if (has_interface<PPB_AudioInput_Dev_0_1>()) { | |
| 172 return PP_ToBool(get_interface<PPB_AudioInput_Dev_0_1>()->StartCapture( | |
| 173 pp_resource())); | |
| 174 } | |
| 175 | |
| 176 return false; | |
| 42 } | 177 } |
| 43 | 178 |
| 44 bool AudioInput_Dev::StopCapture() { | 179 bool AudioInput_Dev::StopCapture() { |
| 45 return has_interface<PPB_AudioInput_Dev>() && | 180 if (has_interface<PPB_AudioInput_Dev_0_2>()) { |
| 46 get_interface<PPB_AudioInput_Dev>()->StopCapture(pp_resource()); | 181 return PP_ToBool(get_interface<PPB_AudioInput_Dev_0_2>()->StopCapture( |
| 182 pp_resource())); | |
| 183 } | |
| 184 | |
| 185 if (has_interface<PPB_AudioInput_Dev_0_1>()) { | |
| 186 return PP_ToBool(get_interface<PPB_AudioInput_Dev_0_1>()->StopCapture( | |
| 187 pp_resource())); | |
| 188 } | |
| 189 | |
| 190 return false; | |
| 191 } | |
| 192 | |
| 193 void AudioInput_Dev::Close() { | |
| 194 if (has_interface<PPB_AudioInput_Dev_0_2>()) | |
| 195 get_interface<PPB_AudioInput_Dev_0_2>()->Close(pp_resource()); | |
| 196 } | |
| 197 | |
| 198 void AudioInput_Dev::AbortEnumerateDevices() { | |
| 199 if (enum_state_) { | |
| 200 enum_state_->devices = NULL; | |
| 201 Module::Get()->core()->CallOnMainThread(0, enum_state_->callback, | |
| 202 PP_ERROR_ABORTED); | |
| 203 enum_state_->audio_input = NULL; | |
| 204 enum_state_ = NULL; | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 // static | |
| 209 void AudioInput_Dev::OnEnumerateDevicesComplete(void* user_data, | |
| 210 int32_t result) { | |
| 211 EnumerateDevicesState* enum_state = | |
| 212 static_cast<EnumerateDevicesState*>(user_data); | |
| 213 | |
| 214 bool need_to_callback = !!enum_state->audio_input; | |
| 215 | |
| 216 if (result == PP_OK) { | |
| 217 // It will take care of releasing the reference. | |
| 218 ResourceArray_Dev resources(pp::PASS_REF, | |
| 219 enum_state->devices_resource); | |
| 220 | |
| 221 if (need_to_callback) { | |
| 222 enum_state->devices->clear(); | |
| 223 for (uint32_t index = 0; index < resources.size(); ++index) { | |
|
viettrungluu
2012/03/06 19:17:28
size_t, not uint32_t, I guess.
yzshen1
2012/03/07 00:30:36
Because we usually use uint32_t in PPAPI for index
| |
| 224 DeviceRef_Dev device(resources[index]); | |
| 225 enum_state->devices->push_back(device); | |
| 226 } | |
| 227 } | |
| 228 } | |
| 229 | |
| 230 if (need_to_callback) { | |
| 231 enum_state->audio_input->enum_state_ = NULL; | |
| 232 enum_state->callback.Run(result); | |
| 233 } | |
| 234 | |
| 235 delete enum_state; | |
| 47 } | 236 } |
| 48 | 237 |
| 49 } // namespace pp | 238 } // namespace pp |
| OLD | NEW |