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 "content/renderer/pepper/ppb_audio_impl.h"
7 #include "base/logging.h"
8 #include "content/renderer/pepper/common.h"
9 #include "content/renderer/pepper/pepper_platform_audio_output.h"
10 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
11 #include "content/renderer/render_view_impl.h"
12 #include "media/audio/audio_output_controller.h"
13 #include "ppapi/c/pp_completion_callback.h"
14 #include "ppapi/c/ppb_audio.h"
15 #include "ppapi/c/ppb_audio_config.h"
16 #include "ppapi/shared_impl/resource_tracker.h"
17 #include "ppapi/thunk/enter.h"
18 #include "ppapi/thunk/ppb_audio_config_api.h"
19 #include "ppapi/thunk/thunk.h"
21 using ppapi::PpapiGlobals;
22 using ppapi::thunk::EnterResourceNoLock;
23 using ppapi::thunk::PPB_Audio_API;
24 using ppapi::thunk::PPB_AudioConfig_API;
25 using ppapi::TrackedCallback;
29 // PPB_Audio_Impl --------------------------------------------------------------
31 PPB_Audio_Impl::PPB_Audio_Impl(PP_Instance instance)
32 : Resource(ppapi::OBJECT_IS_IMPL, instance),
36 PPB_Audio_Impl::~PPB_Audio_Impl() {
37 // Calling ShutDown() makes sure StreamCreated cannot be called anymore and
38 // releases the audio data associated with the pointer. Note however, that
39 // until ShutDown returns, StreamCreated may still be called. This will be
40 // OK since we'll just immediately clean up the data it stored later in this
49 PP_Resource PPB_Audio_Impl::Create(
52 const ppapi::AudioCallbackCombined& 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 const ppapi::AudioCallbackCombined& callback,
67 // Validate the config and keep a reference to it.
68 EnterResourceNoLock<PPB_AudioConfig_API> enter(config, true);
73 if (!callback.IsValid())
75 SetCallback(callback, user_data);
77 PepperPluginInstance* instance = PepperPluginInstance::Get(pp_instance());
81 // When the stream is created, we'll get called back on StreamCreated().
83 audio_ = PepperPlatformAudioOutput::Create(
84 static_cast<int>(enter.object()->GetSampleRate()),
85 static_cast<int>(enter.object()->GetSampleFrameCount()),
86 instance->GetRenderView()->GetRoutingID(),
88 return audio_ != NULL;
91 PP_Resource PPB_Audio_Impl::GetCurrentConfig() {
92 // AddRef on behalf of caller, while keeping a ref for ourselves.
93 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(config_);
97 PP_Bool PPB_Audio_Impl::StartPlayback() {
102 SetStartPlaybackState();
103 return BoolToPPBool(audio_->StartPlayback());
106 PP_Bool PPB_Audio_Impl::StopPlayback() {
111 if (!audio_->StopPlayback())
113 SetStopPlaybackState();
117 int32_t PPB_Audio_Impl::Open(
119 scoped_refptr<TrackedCallback> create_callback) {
120 // Validate the config and keep a reference to it.
121 EnterResourceNoLock<PPB_AudioConfig_API> enter(config, true);
123 return PP_ERROR_FAILED;
126 PepperPluginInstance* instance = PepperPluginInstance::Get(pp_instance());
128 return PP_ERROR_FAILED;
130 // When the stream is created, we'll get called back on StreamCreated().
132 audio_ = PepperPlatformAudioOutput::Create(
133 static_cast<int>(enter.object()->GetSampleRate()),
134 static_cast<int>(enter.object()->GetSampleFrameCount()),
135 instance->GetRenderView()->GetRoutingID(),
138 return PP_ERROR_FAILED;
140 // At this point, we are guaranteeing ownership of the completion
141 // callback. Audio promises to fire the completion callback
142 // once and only once.
143 SetCreateCallback(create_callback);
145 return PP_OK_COMPLETIONPENDING;
148 int32_t PPB_Audio_Impl::GetSyncSocket(int* sync_socket) {
149 return GetSyncSocketImpl(sync_socket);
152 int32_t PPB_Audio_Impl::GetSharedMemory(int* shm_handle,
153 uint32_t* shm_size) {
154 return GetSharedMemoryImpl(shm_handle, shm_size);
157 void PPB_Audio_Impl::OnSetStreamInfo(
158 base::SharedMemoryHandle shared_memory_handle,
159 size_t shared_memory_size,
160 base::SyncSocket::Handle socket_handle) {
161 EnterResourceNoLock<PPB_AudioConfig_API> enter(config_, true);
162 SetStreamInfo(pp_instance(), shared_memory_handle, shared_memory_size,
163 socket_handle, enter.object()->GetSampleRate(),
164 enter.object()->GetSampleFrameCount());
167 } // namespace content