| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "webkit/plugins/ppapi/ppb_audio_input_impl.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/shared_memory.h" | |
| 11 #include "base/sync_socket.h" | |
| 12 #include "build/build_config.h" | |
| 13 #include "ppapi/c/pp_completion_callback.h" | |
| 14 #include "ppapi/shared_impl/ppb_device_ref_shared.h" | |
| 15 #include "ppapi/shared_impl/tracked_callback.h" | |
| 16 #include "webkit/plugins/ppapi/common.h" | |
| 17 #include "webkit/plugins/ppapi/resource_helper.h" | |
| 18 | |
| 19 using ppapi::TrackedCallback; | |
| 20 | |
| 21 namespace webkit { | |
| 22 namespace ppapi { | |
| 23 | |
| 24 // PPB_AudioInput_Impl --------------------------------------------------------- | |
| 25 | |
| 26 PPB_AudioInput_Impl::PPB_AudioInput_Impl(PP_Instance instance) | |
| 27 : ::ppapi::PPB_AudioInput_Shared(instance), | |
| 28 audio_input_(NULL) { | |
| 29 } | |
| 30 | |
| 31 PPB_AudioInput_Impl::~PPB_AudioInput_Impl() { | |
| 32 Close(); | |
| 33 } | |
| 34 | |
| 35 // static | |
| 36 PP_Resource PPB_AudioInput_Impl::Create0_1( | |
| 37 PP_Instance instance, | |
| 38 PP_Resource config, | |
| 39 PPB_AudioInput_Callback audio_input_callback, | |
| 40 void* user_data) { | |
| 41 scoped_refptr<PPB_AudioInput_Impl> | |
| 42 audio_input(new PPB_AudioInput_Impl(instance)); | |
| 43 int32_t result = audio_input->Open( | |
| 44 "", config, audio_input_callback, user_data, | |
| 45 MakeIgnoredCompletionCallback(audio_input.get())); | |
| 46 if (result != PP_OK && result != PP_OK_COMPLETIONPENDING) | |
| 47 return 0; | |
| 48 return audio_input->GetReference(); | |
| 49 } | |
| 50 | |
| 51 int32_t PPB_AudioInput_Impl::OpenTrusted( | |
| 52 const std::string& device_id, | |
| 53 PP_Resource config, | |
| 54 scoped_refptr<TrackedCallback> create_callback) { | |
| 55 return CommonOpen(device_id, config, NULL, NULL, create_callback); | |
| 56 } | |
| 57 | |
| 58 int32_t PPB_AudioInput_Impl::GetSyncSocket(int* sync_socket) { | |
| 59 if (socket_.get()) { | |
| 60 #if defined(OS_POSIX) | |
| 61 *sync_socket = socket_->handle(); | |
| 62 #elif defined(OS_WIN) | |
| 63 *sync_socket = reinterpret_cast<int>(socket_->handle()); | |
| 64 #else | |
| 65 #error "Platform not supported." | |
| 66 #endif | |
| 67 return PP_OK; | |
| 68 } | |
| 69 return PP_ERROR_FAILED; | |
| 70 } | |
| 71 | |
| 72 int32_t PPB_AudioInput_Impl::GetSharedMemory(int* shm_handle, | |
| 73 uint32_t* shm_size) { | |
| 74 if (shared_memory_.get()) { | |
| 75 #if defined(OS_POSIX) | |
| 76 *shm_handle = shared_memory_->handle().fd; | |
| 77 #elif defined(OS_WIN) | |
| 78 *shm_handle = reinterpret_cast<int>(shared_memory_->handle()); | |
| 79 #else | |
| 80 #error "Platform not supported." | |
| 81 #endif | |
| 82 *shm_size = shared_memory_size_; | |
| 83 return PP_OK; | |
| 84 } | |
| 85 return PP_ERROR_FAILED; | |
| 86 } | |
| 87 | |
| 88 const PPB_AudioInput_Impl::DeviceRefDataVector& | |
| 89 PPB_AudioInput_Impl::GetDeviceRefData() const { | |
| 90 return devices_data_; | |
| 91 } | |
| 92 | |
| 93 void PPB_AudioInput_Impl::StreamCreated( | |
| 94 base::SharedMemoryHandle shared_memory_handle, | |
| 95 size_t shared_memory_size, | |
| 96 base::SyncSocket::Handle socket) { | |
| 97 OnOpenComplete(PP_OK, shared_memory_handle, shared_memory_size, socket); | |
| 98 } | |
| 99 | |
| 100 void PPB_AudioInput_Impl::StreamCreationFailed() { | |
| 101 OnOpenComplete(PP_ERROR_FAILED, base::SharedMemory::NULLHandle(), 0, | |
| 102 base::SyncSocket::kInvalidHandle); | |
| 103 } | |
| 104 | |
| 105 int32_t PPB_AudioInput_Impl::InternalEnumerateDevices( | |
| 106 PP_Resource* devices, | |
| 107 scoped_refptr<TrackedCallback> callback) { | |
| 108 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); | |
| 109 if (!plugin_delegate) | |
| 110 return PP_ERROR_FAILED; | |
| 111 | |
| 112 devices_ = devices; | |
| 113 enumerate_devices_callback_ = callback; | |
| 114 plugin_delegate->EnumerateDevices( | |
| 115 PP_DEVICETYPE_DEV_AUDIOCAPTURE, | |
| 116 base::Bind(&PPB_AudioInput_Impl::EnumerateDevicesCallbackFunc, | |
| 117 AsWeakPtr())); | |
| 118 return PP_OK_COMPLETIONPENDING; | |
| 119 } | |
| 120 | |
| 121 int32_t PPB_AudioInput_Impl::InternalOpen( | |
| 122 const std::string& device_id, | |
| 123 PP_AudioSampleRate sample_rate, | |
| 124 uint32_t sample_frame_count, | |
| 125 scoped_refptr<TrackedCallback> callback) { | |
| 126 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); | |
| 127 if (!plugin_delegate) | |
| 128 return PP_ERROR_FAILED; | |
| 129 | |
| 130 // When the stream is created, we'll get called back on StreamCreated(). | |
| 131 DCHECK(!audio_input_); | |
| 132 audio_input_ = plugin_delegate->CreateAudioInput( | |
| 133 device_id, sample_rate, sample_frame_count, this); | |
| 134 if (audio_input_) { | |
| 135 open_callback_ = callback; | |
| 136 return PP_OK_COMPLETIONPENDING; | |
| 137 } else { | |
| 138 return PP_ERROR_FAILED; | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 PP_Bool PPB_AudioInput_Impl::InternalStartCapture() { | |
| 143 if (!audio_input_) | |
| 144 return PP_FALSE; | |
| 145 SetStartCaptureState(); | |
| 146 audio_input_->StartCapture(); | |
| 147 return PP_TRUE; | |
| 148 } | |
| 149 | |
| 150 PP_Bool PPB_AudioInput_Impl::InternalStopCapture() { | |
| 151 if (!audio_input_) | |
| 152 return PP_FALSE; | |
| 153 audio_input_->StopCapture(); | |
| 154 SetStopCaptureState(); | |
| 155 return PP_TRUE; | |
| 156 } | |
| 157 | |
| 158 void PPB_AudioInput_Impl::InternalClose() { | |
| 159 // Calling ShutDown() makes sure StreamCreated() cannot be called anymore and | |
| 160 // releases the audio data associated with the pointer. Note however, that | |
| 161 // until ShutDown() returns, StreamCreated() may still be called. This will be | |
| 162 // OK since OnOpenComplete() will clean up the handles and do nothing else in | |
| 163 // that case. | |
| 164 if (audio_input_) { | |
| 165 audio_input_->ShutDown(); | |
| 166 audio_input_ = NULL; | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 void PPB_AudioInput_Impl::EnumerateDevicesCallbackFunc( | |
| 171 int request_id, | |
| 172 bool succeeded, | |
| 173 const DeviceRefDataVector& devices) { | |
| 174 devices_data_.clear(); | |
| 175 if (succeeded) | |
| 176 devices_data_ = devices; | |
| 177 | |
| 178 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); | |
| 179 if (plugin_delegate) | |
| 180 plugin_delegate->StopEnumerateDevices(request_id); | |
| 181 | |
| 182 OnEnumerateDevicesComplete(succeeded ? PP_OK : PP_ERROR_FAILED, devices); | |
| 183 } | |
| 184 | |
| 185 } // namespace ppapi | |
| 186 } // namespace webkit | |
| OLD | NEW |