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 |
| 6 #include "base/logging.h" |
| 7 #include "ppapi/c/dev/ppb_audio_input_dev.h" |
| 8 #include "ppapi/c/pp_completion_callback.h" |
| 9 #include "ppapi/shared_impl/resource_tracker.h" |
| 10 #include "ppapi/shared_impl/tracker_base.h" |
| 11 #include "ppapi/thunk/enter.h" |
| 12 #include "ppapi/thunk/ppb_audio_config_api.h" |
| 13 #include "ppapi/thunk/thunk.h" |
| 14 #include "webkit/plugins/ppapi/common.h" |
| 15 #include "webkit/plugins/ppapi/ppb_audio_input_impl.h" |
| 16 #include "webkit/plugins/ppapi/resource_helper.h" |
| 17 |
| 18 using ppapi::thunk::EnterResourceNoLock; |
| 19 using ppapi::thunk::PPB_AudioInput_API; |
| 20 using ppapi::thunk::PPB_AudioConfig_API; |
| 21 |
| 22 namespace webkit { |
| 23 namespace ppapi { |
| 24 |
| 25 // PPB_AudioInput_Impl --------------------------------------------------------- |
| 26 |
| 27 PPB_AudioInput_Impl::PPB_AudioInput_Impl(PP_Instance instance) |
| 28 : Resource(instance), |
| 29 audio_input_(NULL) { |
| 30 } |
| 31 |
| 32 PPB_AudioInput_Impl::~PPB_AudioInput_Impl() { |
| 33 // Calling ShutDown() makes sure StreamCreated cannot be called anymore and |
| 34 // releases the audio data associated with the pointer. Note however, that |
| 35 // until ShutDown returns, StreamCreated may still be called. This will be |
| 36 // OK since we'll just immediately clean up the data it stored later in this |
| 37 // destructor. |
| 38 if (audio_input_) { |
| 39 audio_input_->ShutDown(); |
| 40 audio_input_ = NULL; |
| 41 } |
| 42 } |
| 43 |
| 44 // static |
| 45 PP_Resource PPB_AudioInput_Impl::Create(PP_Instance instance, |
| 46 PP_Resource config, |
| 47 PPB_AudioInput_Callback audio_input_callback, |
| 48 void* user_data) { |
| 49 scoped_refptr<PPB_AudioInput_Impl> |
| 50 audio_input(new PPB_AudioInput_Impl(instance)); |
| 51 if (!audio_input->Init(config, audio_input_callback, user_data)) |
| 52 return 0; |
| 53 return audio_input->GetReference(); |
| 54 } |
| 55 |
| 56 PPB_AudioInput_API* PPB_AudioInput_Impl::AsPPB_AudioInput_API() { |
| 57 return this; |
| 58 } |
| 59 |
| 60 bool PPB_AudioInput_Impl::Init(PP_Resource config, |
| 61 PPB_AudioInput_Callback callback, |
| 62 void* user_data) { |
| 63 // Validate the config and keep a reference to it. |
| 64 EnterResourceNoLock<PPB_AudioConfig_API> enter(config, true); |
| 65 if (enter.failed()) |
| 66 return false; |
| 67 config_ = config; |
| 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( |
| 80 enter.object()->GetSampleRate(), |
| 81 enter.object()->GetSampleFrameCount(), |
| 82 this); |
| 83 return audio_input_ != NULL; |
| 84 } |
| 85 |
| 86 PP_Resource PPB_AudioInput_Impl::GetCurrentConfig() { |
| 87 // AddRef on behalf of caller, while keeping a ref for ourselves. |
| 88 ::ppapi::TrackerBase::Get()->GetResourceTracker()->AddRefResource(config_); |
| 89 return config_; |
| 90 } |
| 91 |
| 92 PP_Bool PPB_AudioInput_Impl::StartCapture() { |
| 93 if (!audio_input_) |
| 94 return PP_FALSE; |
| 95 if (capturing()) |
| 96 return PP_TRUE; |
| 97 SetStartCaptureState(); |
| 98 return BoolToPPBool(audio_input_->StartCapture()); |
| 99 } |
| 100 |
| 101 PP_Bool PPB_AudioInput_Impl::StopCapture() { |
| 102 if (!audio_input_) |
| 103 return PP_FALSE; |
| 104 if (!capturing()) |
| 105 return PP_TRUE; |
| 106 if (!audio_input_->StopCapture()) |
| 107 return PP_FALSE; |
| 108 SetStopCaptureState(); |
| 109 return PP_TRUE; |
| 110 } |
| 111 |
| 112 int32_t PPB_AudioInput_Impl::OpenTrusted(PP_Resource config, |
| 113 PP_CompletionCallback create_callback) { |
| 114 // Validate the config and keep a reference to it. |
| 115 EnterResourceNoLock<PPB_AudioConfig_API> enter(config, true); |
| 116 if (enter.failed()) |
| 117 return PP_ERROR_FAILED; |
| 118 config_ = config; |
| 119 |
| 120 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); |
| 121 if (!plugin_delegate) |
| 122 return PP_ERROR_FAILED; |
| 123 |
| 124 // When the stream is created, we'll get called back on StreamCreated(). |
| 125 DCHECK(!audio_input_); |
| 126 audio_input_ = plugin_delegate->CreateAudioInput( |
| 127 enter.object()->GetSampleRate(), |
| 128 enter.object()->GetSampleFrameCount(), |
| 129 this); |
| 130 |
| 131 if (!audio_input_) |
| 132 return PP_ERROR_FAILED; |
| 133 |
| 134 // At this point, we are guaranteeing ownership of the completion |
| 135 // callback. Audio promises to fire the completion callback |
| 136 // once and only once. |
| 137 SetCallbackInfo(true, create_callback); |
| 138 return PP_OK_COMPLETIONPENDING; |
| 139 } |
| 140 |
| 141 int32_t PPB_AudioInput_Impl::GetSyncSocket(int* sync_socket) { |
| 142 return AudioHelper::GetSyncSocket(sync_socket); |
| 143 } |
| 144 |
| 145 int32_t PPB_AudioInput_Impl::GetSharedMemory(int* shm_handle, |
| 146 uint32_t* shm_size) { |
| 147 return AudioHelper::GetSharedMemory(shm_handle, shm_size); |
| 148 } |
| 149 |
| 150 void PPB_AudioInput_Impl::OnSetStreamInfo( |
| 151 base::SharedMemoryHandle shared_memory_handle, |
| 152 size_t shared_memory_size, |
| 153 base::SyncSocket::Handle socket_handle) { |
| 154 SetStreamInfo(shared_memory_handle, shared_memory_size, socket_handle); |
| 155 } |
| 156 |
| 157 void PPB_AudioInput_Impl::StreamCreated( |
| 158 base::SharedMemoryHandle shared_memory_handle, |
| 159 size_t shared_memory_size, |
| 160 base::SyncSocket::Handle socket_handle) { |
| 161 StreamCreated_(shared_memory_handle, shared_memory_size, socket_handle); |
| 162 } |
| 163 |
| 164 } // namespace ppapi |
| 165 } // namespace webkit |
| 166 |
OLD | NEW |