Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: webkit/plugins/ppapi/ppb_audio_input_impl.cc

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

Powered by Google App Engine
This is Rietveld 408576698