OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/logging.h" |
| 8 #include "ppapi/c/pp_completion_callback.h" |
| 9 #include "ppapi/c/dev/ppb_audio_input_dev.h" |
| 10 #include "ppapi/shared_impl/resource_tracker.h" |
| 11 #include "ppapi/shared_impl/tracker_base.h" |
| 12 #include "ppapi/thunk/enter.h" |
| 13 #include "ppapi/thunk/thunk.h" |
| 14 #include "webkit/plugins/ppapi/common.h" |
| 15 #include "webkit/plugins/ppapi/resource_helper.h" |
| 16 |
| 17 using ppapi::thunk::EnterResourceNoLock; |
| 18 using ppapi::thunk::PPB_AudioInput_API; |
| 19 |
| 20 namespace webkit { |
| 21 namespace ppapi { |
| 22 |
| 23 // PPB_AudioInput_Impl ---------------------------------------------------------
----- |
| 24 |
| 25 PPB_AudioInput_Impl::PPB_AudioInput_Impl(PP_Instance instance) |
| 26 : Resource(instance), |
| 27 audio_input_(NULL), |
| 28 create_callback_pending_(false), |
| 29 shared_memory_size_for_create_callback_(0) { |
| 30 create_callback_ = PP_MakeCompletionCallback(NULL, NULL); |
| 31 } |
| 32 |
| 33 PPB_AudioInput_Impl::~PPB_AudioInput_Impl() { |
| 34 // Calling ShutDown() makes sure StreamCreated cannot be called anymore and |
| 35 // releases the audio data associated with the pointer. Note however, that |
| 36 // until ShutDown returns, StreamCreated may still be called. This will be |
| 37 // OK since we'll just immediately clean up the data it stored later in this |
| 38 // destructor. |
| 39 if (audio_input_) { |
| 40 audio_input_->ShutDown(); |
| 41 audio_input_ = NULL; |
| 42 } |
| 43 |
| 44 // If the completion callback hasn't fired yet, do so here |
| 45 // with an error condition. |
| 46 if (create_callback_pending_) { |
| 47 PP_RunCompletionCallback(&create_callback_, PP_ERROR_ABORTED); |
| 48 create_callback_pending_ = false; |
| 49 } |
| 50 } |
| 51 |
| 52 // static |
| 53 PP_Resource PPB_AudioInput_Impl::Create(PP_Instance instance, |
| 54 PPB_AudioInput_Callback audio_input_call
back, |
| 55 void* user_data) { |
| 56 scoped_refptr<PPB_AudioInput_Impl> audio_input(new PPB_AudioInput_Impl(instanc
e)); |
| 57 if (!audio_input->Init(audio_input_callback, user_data)) |
| 58 return 0; |
| 59 return audio_input->GetReference(); |
| 60 } |
| 61 |
| 62 PPB_AudioInput_API* PPB_AudioInput_Impl::AsPPB_AudioInput_API() { |
| 63 return this; |
| 64 } |
| 65 |
| 66 bool PPB_AudioInput_Impl::Init(PPB_AudioInput_Callback callback, void* user_data
) { |
| 67 // Validate the config and keep a reference to it. |
| 68 |
| 69 if (!callback) |
| 70 return false; |
| 71 SetCallback(callback, user_data); |
| 72 |
| 73 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); |
| 74 if (!plugin_delegate) |
| 75 return false; |
| 76 |
| 77 // When the stream is created, we'll get called back on StreamCreated(). |
| 78 CHECK(!audio_input_); |
| 79 audio_input_ = plugin_delegate->CreateAudioInput(this); |
| 80 return audio_input_ != NULL; |
| 81 } |
| 82 |
| 83 PP_Bool PPB_AudioInput_Impl::StartCapture() { |
| 84 if (!audio_input_) |
| 85 return PP_FALSE; |
| 86 if (capturing()) |
| 87 return PP_TRUE; |
| 88 SetStartCaptureState(); |
| 89 return BoolToPPBool(audio_input_->StartCapture()); |
| 90 } |
| 91 |
| 92 PP_Bool PPB_AudioInput_Impl::StopCapture() { |
| 93 if (!audio_input_) |
| 94 return PP_FALSE; |
| 95 if (!capturing()) |
| 96 return PP_TRUE; |
| 97 if (!audio_input_->StopCapture()) |
| 98 return PP_FALSE; |
| 99 SetStopCaptureState(); |
| 100 return PP_TRUE; |
| 101 } |
| 102 |
| 103 int32_t PPB_AudioInput_Impl::OpenTrusted(PP_CompletionCallback create_callback)
{ |
| 104 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); |
| 105 if (!plugin_delegate) |
| 106 return PP_ERROR_FAILED; |
| 107 |
| 108 // When the stream is created, we'll get called back on StreamCreated(). |
| 109 DCHECK(!audio_input_); |
| 110 audio_input_ = plugin_delegate->CreateAudioInput(this); |
| 111 |
| 112 if (!audio_input_) |
| 113 return PP_ERROR_FAILED; |
| 114 |
| 115 // At this point, we are guaranteeing ownership of the completion |
| 116 // callback. Audio promises to fire the completion callback |
| 117 // once and only once. |
| 118 create_callback_ = create_callback; |
| 119 create_callback_pending_ = true; |
| 120 return PP_OK_COMPLETIONPENDING; |
| 121 } |
| 122 |
| 123 int32_t PPB_AudioInput_Impl::GetSyncSocket(int* sync_socket) { |
| 124 if (socket_for_create_callback_.get()) { |
| 125 #if defined(OS_POSIX) |
| 126 *sync_socket = socket_for_create_callback_->handle(); |
| 127 #elif defined(OS_WIN) |
| 128 *sync_socket = reinterpret_cast<int>(socket_for_create_callback_->handle()); |
| 129 #else |
| 130 #error "Platform not supported." |
| 131 #endif |
| 132 return PP_OK; |
| 133 } |
| 134 return PP_ERROR_FAILED; |
| 135 } |
| 136 |
| 137 int32_t PPB_AudioInput_Impl::GetSharedMemory(int* shm_handle, uint32_t* shm_size
) { |
| 138 if (shared_memory_for_create_callback_.get()) { |
| 139 #if defined(OS_POSIX) |
| 140 *shm_handle = shared_memory_for_create_callback_->handle().fd; |
| 141 #elif defined(OS_WIN) |
| 142 *shm_handle = reinterpret_cast<int>( |
| 143 shared_memory_for_create_callback_->handle()); |
| 144 #else |
| 145 #error "Platform not supported." |
| 146 #endif |
| 147 *shm_size = shared_memory_size_for_create_callback_; |
| 148 return PP_OK; |
| 149 } |
| 150 return PP_ERROR_FAILED; |
| 151 } |
| 152 |
| 153 void PPB_AudioInput_Impl::StreamCreated( |
| 154 base::SharedMemoryHandle shared_memory_handle, |
| 155 size_t shared_memory_size, |
| 156 base::SyncSocket::Handle socket_handle) { |
| 157 if (create_callback_pending_) { |
| 158 // Trusted side of proxy can specify a callback to recieve handles. In |
| 159 // this case we don't need to map any data or start the thread since it |
| 160 // will be handled by the proxy. |
| 161 shared_memory_for_create_callback_.reset( |
| 162 new base::SharedMemory(shared_memory_handle, false)); |
| 163 shared_memory_size_for_create_callback_ = shared_memory_size; |
| 164 socket_for_create_callback_.reset(new base::SyncSocket(socket_handle)); |
| 165 |
| 166 PP_RunCompletionCallback(&create_callback_, 0); |
| 167 create_callback_pending_ = false; |
| 168 |
| 169 // It might be nice to close the handles here to free up some system |
| 170 // resources, but we can't since there's a race condition. The handles must |
| 171 // be valid until they're sent over IPC, which is done from the I/O thread |
| 172 // which will often get done after this code executes. We could do |
| 173 // something more elaborate like an ACK from the plugin or post a task to |
| 174 // the I/O thread and back, but this extra complexity doesn't seem worth it |
| 175 // just to clean up these handles faster. |
| 176 } else { |
| 177 SetStreamInfo(shared_memory_handle, shared_memory_size, socket_handle); |
| 178 } |
| 179 } |
| 180 |
| 181 } // namespace ppapi |
| 182 } // namespace webkit |
OLD | NEW |