| 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 // 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) { |
| 51 } |
| 52 |
| 23 AudioInput_Dev::AudioInput_Dev(const InstanceHandle& instance, | 53 AudioInput_Dev::AudioInput_Dev(const InstanceHandle& instance, |
| 24 const AudioConfig& config, | 54 const AudioConfig& config, |
| 25 PPB_AudioInput_Callback callback, | 55 PPB_AudioInput_Callback callback, |
| 26 void* user_data) | 56 void* user_data) |
| 27 : config_(config) { | 57 : config_(config), |
| 28 if (has_interface<PPB_AudioInput_Dev>()) { | 58 enum_state_(NULL), |
| 29 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev>()->Create( | 59 audio_input_callback_(callback), |
| 60 user_data_(user_data) { |
| 61 if (has_interface<PPB_AudioInput_Dev_0_2>()) { |
| 62 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev_0_2>()->Create( |
| 63 instance.pp_instance())); |
| 64 } else if (has_interface<PPB_AudioInput_Dev_0_1>()) { |
| 65 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev_0_1>()->Create( |
| 30 instance.pp_instance(), config.pp_resource(), callback, user_data)); | 66 instance.pp_instance(), config.pp_resource(), callback, user_data)); |
| 31 } | 67 } |
| 32 } | 68 } |
| 33 | 69 |
| 70 AudioInput_Dev::AudioInput_Dev(const InstanceHandle& instance) |
| 71 : enum_state_(NULL), |
| 72 audio_input_callback_(NULL), |
| 73 user_data_(NULL) { |
| 74 if (has_interface<PPB_AudioInput_Dev_0_2>()) { |
| 75 PassRefFromConstructor(get_interface<PPB_AudioInput_Dev_0_2>()->Create( |
| 76 instance.pp_instance())); |
| 77 } |
| 78 } |
| 79 |
| 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() { |
| 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 } |
| 101 |
| 34 // static | 102 // static |
| 35 bool AudioInput_Dev::IsAvailable() { | 103 bool AudioInput_Dev::IsAvailable() { |
| 36 return has_interface<PPB_AudioInput_Dev>(); | 104 return has_interface<PPB_AudioInput_Dev_0_2>() || |
| 105 has_interface<PPB_AudioInput_Dev_0_1>(); |
| 106 } |
| 107 |
| 108 int32_t AudioInput_Dev::EnumerateDevices(std::vector<DeviceRef_Dev>* devices, |
| 109 const CompletionCallback& callback) { |
| 110 if (!has_interface<PPB_AudioInput_Dev_0_2>()) |
| 111 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 112 if (!devices) |
| 113 return callback.MayForce(PP_ERROR_BADARGUMENT); |
| 114 if (!callback.pp_completion_callback().func) |
| 115 return callback.MayForce(PP_ERROR_BLOCKS_MAIN_THREAD); |
| 116 if (enum_state_) |
| 117 return callback.MayForce(PP_ERROR_INPROGRESS); |
| 118 |
| 119 // It will be deleted in OnEnumerateDevicesComplete(). |
| 120 enum_state_ = new EnumerateDevicesState(devices, callback, this); |
| 121 return get_interface<PPB_AudioInput_Dev_0_2>()->EnumerateDevices( |
| 122 pp_resource(), &enum_state_->devices_resource, |
| 123 PP_MakeCompletionCallback(&AudioInput_Dev::OnEnumerateDevicesComplete, |
| 124 enum_state_)); |
| 125 } |
| 126 |
| 127 int32_t AudioInput_Dev::Open(const DeviceRef_Dev& device_ref, |
| 128 const CompletionCallback& callback) { |
| 129 if (has_interface<PPB_AudioInput_Dev_0_2>()) { |
| 130 return get_interface<PPB_AudioInput_Dev_0_2>()->Open( |
| 131 pp_resource(), device_ref.pp_resource(), config_.pp_resource(), |
| 132 audio_input_callback_, user_data_, callback.pp_completion_callback()); |
| 133 } |
| 134 |
| 135 if (has_interface<PPB_AudioInput_Dev_0_1>()) { |
| 136 if (is_null()) |
| 137 return callback.MayForce(PP_ERROR_FAILED); |
| 138 |
| 139 // If the v0.1 interface is being used and there is a valid resource handle, |
| 140 // then the default device has been successfully opened during resource |
| 141 // creation. |
| 142 if (device_ref.is_null()) |
| 143 return callback.MayForce(PP_OK); |
| 144 |
| 145 // The v0.1 interface doesn't support devices other than the default one. |
| 146 return callback.MayForce(PP_ERROR_NOTSUPPORTED); |
| 147 } |
| 148 |
| 149 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 150 } |
| 151 |
| 152 int32_t AudioInput_Dev::Open(const DeviceRef_Dev& device_ref, |
| 153 const AudioConfig& config, |
| 154 PPB_AudioInput_Callback audio_input_callback, |
| 155 void* user_data, |
| 156 const CompletionCallback& callback) { |
| 157 if (has_interface<PPB_AudioInput_Dev_0_2>()) { |
| 158 return get_interface<PPB_AudioInput_Dev_0_2>()->Open( |
| 159 pp_resource(), device_ref.pp_resource(), config.pp_resource(), |
| 160 audio_input_callback, user_data, callback.pp_completion_callback()); |
| 161 } |
| 162 |
| 163 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 37 } | 164 } |
| 38 | 165 |
| 39 bool AudioInput_Dev::StartCapture() { | 166 bool AudioInput_Dev::StartCapture() { |
| 40 return has_interface<PPB_AudioInput_Dev>() && | 167 if (has_interface<PPB_AudioInput_Dev_0_2>()) { |
| 41 get_interface<PPB_AudioInput_Dev>()->StartCapture(pp_resource()); | 168 return PP_ToBool(get_interface<PPB_AudioInput_Dev_0_2>()->StartCapture( |
| 169 pp_resource())); |
| 170 } |
| 171 |
| 172 if (has_interface<PPB_AudioInput_Dev_0_1>()) { |
| 173 return PP_ToBool(get_interface<PPB_AudioInput_Dev_0_1>()->StartCapture( |
| 174 pp_resource())); |
| 175 } |
| 176 |
| 177 return false; |
| 42 } | 178 } |
| 43 | 179 |
| 44 bool AudioInput_Dev::StopCapture() { | 180 bool AudioInput_Dev::StopCapture() { |
| 45 return has_interface<PPB_AudioInput_Dev>() && | 181 if (has_interface<PPB_AudioInput_Dev_0_2>()) { |
| 46 get_interface<PPB_AudioInput_Dev>()->StopCapture(pp_resource()); | 182 return PP_ToBool(get_interface<PPB_AudioInput_Dev_0_2>()->StopCapture( |
| 183 pp_resource())); |
| 184 } |
| 185 |
| 186 if (has_interface<PPB_AudioInput_Dev_0_1>()) { |
| 187 return PP_ToBool(get_interface<PPB_AudioInput_Dev_0_1>()->StopCapture( |
| 188 pp_resource())); |
| 189 } |
| 190 |
| 191 return false; |
| 192 } |
| 193 |
| 194 void AudioInput_Dev::Close() { |
| 195 if (has_interface<PPB_AudioInput_Dev_0_2>()) |
| 196 get_interface<PPB_AudioInput_Dev_0_2>()->Close(pp_resource()); |
| 197 } |
| 198 |
| 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; |
| 47 } | 237 } |
| 48 | 238 |
| 49 } // namespace pp | 239 } // namespace pp |
| OLD | NEW |