| 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" |
| 11 #include "ppapi/cpp/logging.h" |
| 14 #include "ppapi/cpp/module.h" | 12 #include "ppapi/cpp/module.h" |
| 15 #include "ppapi/cpp/module_impl.h" | 13 #include "ppapi/cpp/module_impl.h" |
| 16 | 14 |
| 17 namespace pp { | 15 namespace pp { |
| 18 | 16 |
| 19 namespace { | 17 namespace { |
| 20 | 18 |
| 21 template <> const char* interface_name<PPB_AudioInput_Dev_0_2>() { | 19 template <> const char* interface_name<PPB_AudioInput_Dev_0_2>() { |
| 22 return PPB_AUDIO_INPUT_DEV_INTERFACE_0_2; | 20 return PPB_AUDIO_INPUT_DEV_INTERFACE_0_2; |
| 23 } | 21 } |
| 24 | 22 |
| 25 template <> const char* interface_name<PPB_AudioInput_Dev_0_1>() { | 23 template <> const char* interface_name<PPB_AudioInput_Dev_0_1>() { |
| 26 return PPB_AUDIO_INPUT_DEV_INTERFACE_0_1; | 24 return PPB_AUDIO_INPUT_DEV_INTERFACE_0_1; |
| 27 } | 25 } |
| 28 | 26 |
| 29 } // namespace | 27 } // namespace |
| 30 | 28 |
| 31 struct AudioInput_Dev::EnumerateDevicesState { | 29 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) { | 30 user_data_(NULL) { |
| 51 } | 31 } |
| 52 | 32 |
| 53 AudioInput_Dev::AudioInput_Dev(const InstanceHandle& instance, | 33 AudioInput_Dev::AudioInput_Dev(const InstanceHandle& instance, |
| 54 const AudioConfig& config, | 34 const AudioConfig& config, |
| 55 PPB_AudioInput_Callback callback, | 35 PPB_AudioInput_Callback callback, |
| 56 void* user_data) | 36 void* user_data) |
| 57 : config_(config), | 37 : config_(config), |
| 58 enum_state_(NULL), | |
| 59 audio_input_callback_(callback), | 38 audio_input_callback_(callback), |
| 60 user_data_(user_data) { | 39 user_data_(user_data) { |
| 61 if (has_interface<PPB_AudioInput_Dev_0_2>()) { | 40 if (has_interface<PPB_AudioInput_Dev_0_2>()) { |
| 62 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev_0_2>()->Create( | 41 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev_0_2>()->Create( |
| 63 instance.pp_instance())); | 42 instance.pp_instance())); |
| 64 } else if (has_interface<PPB_AudioInput_Dev_0_1>()) { | 43 } else if (has_interface<PPB_AudioInput_Dev_0_1>()) { |
| 65 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev_0_1>()->Create( | 44 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev_0_1>()->Create( |
| 66 instance.pp_instance(), config.pp_resource(), callback, user_data)); | 45 instance.pp_instance(), config.pp_resource(), callback, user_data)); |
| 67 } | 46 } |
| 68 } | 47 } |
| 69 | 48 |
| 70 AudioInput_Dev::AudioInput_Dev(const InstanceHandle& instance) | 49 AudioInput_Dev::AudioInput_Dev(const InstanceHandle& instance) |
| 71 : enum_state_(NULL), | 50 : audio_input_callback_(NULL), |
| 72 audio_input_callback_(NULL), | |
| 73 user_data_(NULL) { | 51 user_data_(NULL) { |
| 74 if (has_interface<PPB_AudioInput_Dev_0_2>()) { | 52 if (has_interface<PPB_AudioInput_Dev_0_2>()) { |
| 75 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev_0_2>()->Create( | 53 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev_0_2>()->Create( |
| 76 instance.pp_instance())); | 54 instance.pp_instance())); |
| 77 } | 55 } |
| 78 } | 56 } |
| 79 | 57 |
| 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() { | 58 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 } | 59 } |
| 101 | 60 |
| 102 // static | 61 // static |
| 103 bool AudioInput_Dev::IsAvailable() { | 62 bool AudioInput_Dev::IsAvailable() { |
| 104 return has_interface<PPB_AudioInput_Dev_0_2>() || | 63 return has_interface<PPB_AudioInput_Dev_0_2>() || |
| 105 has_interface<PPB_AudioInput_Dev_0_1>(); | 64 has_interface<PPB_AudioInput_Dev_0_1>(); |
| 106 } | 65 } |
| 107 | 66 |
| 108 int32_t AudioInput_Dev::EnumerateDevices(std::vector<DeviceRef_Dev>* devices, | 67 int32_t AudioInput_Dev::EnumerateDevices( |
| 109 const CompletionCallback& callback) { | 68 const CompletionCallbackWithOutput<std::vector<DeviceRef_Dev> >& callback) { |
| 110 if (!has_interface<PPB_AudioInput_Dev_0_2>()) | 69 if (!has_interface<PPB_AudioInput_Dev_0_2>()) |
| 111 return callback.MayForce(PP_ERROR_NOINTERFACE); | 70 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 112 if (!devices) | |
| 113 return callback.MayForce(PP_ERROR_BADARGUMENT); | |
| 114 if (!callback.pp_completion_callback().func) | 71 if (!callback.pp_completion_callback().func) |
| 115 return callback.MayForce(PP_ERROR_BLOCKS_MAIN_THREAD); | 72 return callback.MayForce(PP_ERROR_BLOCKS_MAIN_THREAD); |
| 116 if (enum_state_) | |
| 117 return callback.MayForce(PP_ERROR_INPROGRESS); | |
| 118 | 73 |
| 119 // It will be deleted in OnEnumerateDevicesComplete(). | 74 // CallbackConverter is responsible to delete it. |
| 120 enum_state_ = new EnumerateDevicesState(devices, callback, this); | 75 EnumerateCallbackData0_2* data = new EnumerateCallbackData0_2; |
| 76 data->devices = 0; |
| 77 data->output = callback.output(); |
| 78 data->original_callback = callback.pp_completion_callback(); |
| 79 |
| 121 return get_interface<PPB_AudioInput_Dev_0_2>()->EnumerateDevices( | 80 return get_interface<PPB_AudioInput_Dev_0_2>()->EnumerateDevices( |
| 122 pp_resource(), &enum_state_->devices_resource, | 81 pp_resource(), &data->devices, |
| 123 PP_MakeCompletionCallback(&AudioInput_Dev::OnEnumerateDevicesComplete, | 82 PP_MakeCompletionCallback(&CallbackConverter, data)); |
| 124 enum_state_)); | |
| 125 } | 83 } |
| 126 | 84 |
| 127 int32_t AudioInput_Dev::Open(const DeviceRef_Dev& device_ref, | 85 int32_t AudioInput_Dev::Open(const DeviceRef_Dev& device_ref, |
| 128 const CompletionCallback& callback) { | 86 const CompletionCallback& callback) { |
| 129 if (has_interface<PPB_AudioInput_Dev_0_2>()) { | 87 if (has_interface<PPB_AudioInput_Dev_0_2>()) { |
| 130 return get_interface<PPB_AudioInput_Dev_0_2>()->Open( | 88 return get_interface<PPB_AudioInput_Dev_0_2>()->Open( |
| 131 pp_resource(), device_ref.pp_resource(), config_.pp_resource(), | 89 pp_resource(), device_ref.pp_resource(), config_.pp_resource(), |
| 132 audio_input_callback_, user_data_, callback.pp_completion_callback()); | 90 audio_input_callback_, user_data_, callback.pp_completion_callback()); |
| 133 } | 91 } |
| 134 | 92 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 } | 147 } |
| 190 | 148 |
| 191 return false; | 149 return false; |
| 192 } | 150 } |
| 193 | 151 |
| 194 void AudioInput_Dev::Close() { | 152 void AudioInput_Dev::Close() { |
| 195 if (has_interface<PPB_AudioInput_Dev_0_2>()) | 153 if (has_interface<PPB_AudioInput_Dev_0_2>()) |
| 196 get_interface<PPB_AudioInput_Dev_0_2>()->Close(pp_resource()); | 154 get_interface<PPB_AudioInput_Dev_0_2>()->Close(pp_resource()); |
| 197 } | 155 } |
| 198 | 156 |
| 199 void AudioInput_Dev::AbortEnumerateDevices() { | 157 // static |
| 200 if (enum_state_) { | 158 void AudioInput_Dev::CallbackConverter(void* user_data, int32_t result) { |
| 201 enum_state_->devices = NULL; | 159 EnumerateCallbackData0_2* data = |
| 202 Module::Get()->core()->CallOnMainThread(0, enum_state_->callback, | 160 static_cast<EnumerateCallbackData0_2*>(user_data); |
| 203 PP_ERROR_ABORTED); | |
| 204 enum_state_->audio_input = NULL; | |
| 205 enum_state_ = NULL; | |
| 206 } | |
| 207 } | |
| 208 | 161 |
| 209 // static | 162 // data->devices should remain 0 if the call to EnumerateDevices failed. |
| 210 void AudioInput_Dev::OnEnumerateDevicesComplete(void* user_data, | 163 ResourceArray_Dev resources(PASS_REF, data->devices); |
| 211 int32_t result) { | 164 PP_DCHECK(resources.is_null() || result == PP_OK); |
| 212 EnumerateDevicesState* enum_state = | |
| 213 static_cast<EnumerateDevicesState*>(user_data); | |
| 214 | 165 |
| 215 bool need_to_callback = !!enum_state->audio_input; | 166 // Need to issue the "GetDataBuffer" even for error cases and when the number |
| 216 | 167 // of items is 0. |
| 217 if (result == PP_OK) { | 168 PP_Resource* output_buf = static_cast<PP_Resource*>( |
| 218 // It will take care of releasing the reference. | 169 data->output.GetDataBuffer( |
| 219 ResourceArray_Dev resources(pp::PASS_REF, | 170 data->output.user_data, resources.is_null() ? 0 : resources.size(), |
| 220 enum_state->devices_resource); | 171 sizeof(PP_Resource))); |
| 221 | 172 if (output_buf) { |
| 222 if (need_to_callback) { | 173 for (uint32_t index = 0; index < resources.size(); ++index) { |
| 223 enum_state->devices->clear(); | 174 output_buf[index] = resources[index]; |
| 224 for (uint32_t index = 0; index < resources.size(); ++index) { | 175 Module::Get()->core()->AddRefResource(output_buf[index]); |
| 225 DeviceRef_Dev device(resources[index]); | |
| 226 enum_state->devices->push_back(device); | |
| 227 } | |
| 228 } | 176 } |
| 229 } | 177 } |
| 230 | 178 |
| 231 if (need_to_callback) { | 179 PP_RunCompletionCallback(&data->original_callback, result); |
| 232 enum_state->audio_input->enum_state_ = NULL; | 180 delete data; |
| 233 enum_state->callback.Run(result); | |
| 234 } | |
| 235 | |
| 236 delete enum_state; | |
| 237 } | 181 } |
| 238 | 182 |
| 239 } // namespace pp | 183 } // namespace pp |
| OLD | NEW |