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.
5 #include "webkit/plugins/ppapi/ppb_audio_impl.h"
7 #include "base/logging.h"
8 #include "media/audio/audio_output_controller.h"
9 #include "ppapi/c/pp_completion_callback.h"
10 #include "ppapi/c/ppb_audio.h"
11 #include "ppapi/c/ppb_audio_config.h"
12 #include "ppapi/c/trusted/ppb_audio_trusted.h"
13 #include "ppapi/shared_impl/resource_tracker.h"
14 #include "ppapi/thunk/enter.h"
15 #include "ppapi/thunk/ppb_audio_config_api.h"
16 #include "ppapi/thunk/thunk.h"
17 #include "webkit/plugins/ppapi/common.h"
18 #include "webkit/plugins/ppapi/resource_helper.h"
20 using ppapi::PpapiGlobals
;
21 using ppapi::thunk::EnterResourceNoLock
;
22 using ppapi::thunk::PPB_Audio_API
;
23 using ppapi::thunk::PPB_AudioConfig_API
;
24 using ppapi::TrackedCallback
;
29 // PPB_Audio_Impl --------------------------------------------------------------
31 PPB_Audio_Impl::PPB_Audio_Impl(PP_Instance instance
)
32 : Resource(::ppapi::OBJECT_IS_IMPL
, instance
),
34 sample_frame_count_(0) {
37 PPB_Audio_Impl::~PPB_Audio_Impl() {
38 // Calling ShutDown() makes sure StreamCreated cannot be called anymore and
39 // releases the audio data associated with the pointer. Note however, that
40 // until ShutDown returns, StreamCreated may still be called. This will be
41 // OK since we'll just immediately clean up the data it stored later in this
50 PP_Resource
PPB_Audio_Impl::Create(PP_Instance instance
,
52 PPB_Audio_Callback audio_callback
,
54 scoped_refptr
<PPB_Audio_Impl
> audio(new PPB_Audio_Impl(instance
));
55 if (!audio
->Init(config
, audio_callback
, user_data
))
57 return audio
->GetReference();
60 PPB_Audio_API
* PPB_Audio_Impl::AsPPB_Audio_API() {
64 bool PPB_Audio_Impl::Init(PP_Resource config
,
65 PPB_Audio_Callback callback
, void* user_data
) {
66 // Validate the config and keep a reference to it.
67 EnterResourceNoLock
<PPB_AudioConfig_API
> enter(config
, true);
74 SetCallback(callback
, user_data
);
76 PluginDelegate
* plugin_delegate
= ResourceHelper::GetPluginDelegate(this);
80 // When the stream is created, we'll get called back on StreamCreated().
82 audio_
= plugin_delegate
->CreateAudioOutput(
83 enter
.object()->GetSampleRate(), enter
.object()->GetSampleFrameCount(),
85 sample_frame_count_
= enter
.object()->GetSampleFrameCount();
86 return audio_
!= NULL
;
89 PP_Resource
PPB_Audio_Impl::GetCurrentConfig() {
90 // AddRef on behalf of caller, while keeping a ref for ourselves.
91 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(config_
);
95 PP_Bool
PPB_Audio_Impl::StartPlayback() {
100 SetStartPlaybackState();
101 return BoolToPPBool(audio_
->StartPlayback());
104 PP_Bool
PPB_Audio_Impl::StopPlayback() {
109 if (!audio_
->StopPlayback())
111 SetStopPlaybackState();
115 int32_t PPB_Audio_Impl::OpenTrusted(
117 scoped_refptr
<TrackedCallback
> create_callback
) {
118 // Validate the config and keep a reference to it.
119 EnterResourceNoLock
<PPB_AudioConfig_API
> enter(config
, true);
121 return PP_ERROR_FAILED
;
124 PluginDelegate
* plugin_delegate
= ResourceHelper::GetPluginDelegate(this);
125 if (!plugin_delegate
)
126 return PP_ERROR_FAILED
;
128 // When the stream is created, we'll get called back on StreamCreated().
130 audio_
= plugin_delegate
->CreateAudioOutput(
131 enter
.object()->GetSampleRate(), enter
.object()->GetSampleFrameCount(),
134 return PP_ERROR_FAILED
;
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 SetCreateCallback(create_callback
);
141 return PP_OK_COMPLETIONPENDING
;
144 int32_t PPB_Audio_Impl::GetSyncSocket(int* sync_socket
) {
145 return GetSyncSocketImpl(sync_socket
);
148 int32_t PPB_Audio_Impl::GetSharedMemory(int* shm_handle
,
149 uint32_t* shm_size
) {
150 return GetSharedMemoryImpl(shm_handle
, shm_size
);
153 void PPB_Audio_Impl::OnSetStreamInfo(
154 base::SharedMemoryHandle shared_memory_handle
,
155 size_t shared_memory_size
,
156 base::SyncSocket::Handle socket_handle
) {
157 SetStreamInfo(pp_instance(), shared_memory_handle
, shared_memory_size
,
158 socket_handle
, sample_frame_count_
);
162 } // namespace webkit