Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / webkit / plugins / ppapi / ppb_audio_impl.cc
blobaea9005a2c0b4790df28a1da8d5e5b8686273dc7
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;
26 namespace webkit {
27 namespace ppapi {
29 // PPB_Audio_Impl --------------------------------------------------------------
31 PPB_Audio_Impl::PPB_Audio_Impl(PP_Instance instance)
32 : Resource(::ppapi::OBJECT_IS_IMPL, instance),
33 audio_(NULL),
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
42 // destructor.
43 if (audio_) {
44 audio_->ShutDown();
45 audio_ = NULL;
49 // static
50 PP_Resource PPB_Audio_Impl::Create(PP_Instance instance,
51 PP_Resource config,
52 PPB_Audio_Callback audio_callback,
53 void* user_data) {
54 scoped_refptr<PPB_Audio_Impl> audio(new PPB_Audio_Impl(instance));
55 if (!audio->Init(config, audio_callback, user_data))
56 return 0;
57 return audio->GetReference();
60 PPB_Audio_API* PPB_Audio_Impl::AsPPB_Audio_API() {
61 return this;
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);
68 if (enter.failed())
69 return false;
70 config_ = config;
72 if (!callback)
73 return false;
74 SetCallback(callback, user_data);
76 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this);
77 if (!plugin_delegate)
78 return false;
80 // When the stream is created, we'll get called back on StreamCreated().
81 CHECK(!audio_);
82 audio_ = plugin_delegate->CreateAudioOutput(
83 enter.object()->GetSampleRate(), enter.object()->GetSampleFrameCount(),
84 this);
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_);
92 return config_;
95 PP_Bool PPB_Audio_Impl::StartPlayback() {
96 if (!audio_)
97 return PP_FALSE;
98 if (playing())
99 return PP_TRUE;
100 SetStartPlaybackState();
101 return BoolToPPBool(audio_->StartPlayback());
104 PP_Bool PPB_Audio_Impl::StopPlayback() {
105 if (!audio_)
106 return PP_FALSE;
107 if (!playing())
108 return PP_TRUE;
109 if (!audio_->StopPlayback())
110 return PP_FALSE;
111 SetStopPlaybackState();
112 return PP_TRUE;
115 int32_t PPB_Audio_Impl::OpenTrusted(
116 PP_Resource config,
117 scoped_refptr<TrackedCallback> create_callback) {
118 // Validate the config and keep a reference to it.
119 EnterResourceNoLock<PPB_AudioConfig_API> enter(config, true);
120 if (enter.failed())
121 return PP_ERROR_FAILED;
122 config_ = config;
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().
129 DCHECK(!audio_);
130 audio_ = plugin_delegate->CreateAudioOutput(
131 enter.object()->GetSampleRate(), enter.object()->GetSampleFrameCount(),
132 this);
133 if (!audio_)
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_);
161 } // namespace ppapi
162 } // namespace webkit