[ServiceWorker] Implement WebServiceWorkerContextClient::openWindow().
[chromium-blink-merge.git] / content / renderer / pepper / ppb_audio_impl.cc
blobb7fcf0d9c79156340466d3faef32852f819baecf
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/pepper_platform_audio_output.h"
9 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
10 #include "content/renderer/pepper/plugin_instance_throttler_impl.h"
11 #include "content/renderer/render_frame_impl.h"
12 #include "content/renderer/render_view_impl.h"
13 #include "media/audio/audio_output_controller.h"
14 #include "ppapi/c/pp_completion_callback.h"
15 #include "ppapi/c/ppb_audio.h"
16 #include "ppapi/c/ppb_audio_config.h"
17 #include "ppapi/shared_impl/resource_tracker.h"
18 #include "ppapi/thunk/enter.h"
19 #include "ppapi/thunk/ppb_audio_config_api.h"
20 #include "ppapi/thunk/thunk.h"
22 using ppapi::PpapiGlobals;
23 using ppapi::thunk::EnterResourceNoLock;
24 using ppapi::thunk::PPB_Audio_API;
25 using ppapi::thunk::PPB_AudioConfig_API;
26 using ppapi::TrackedCallback;
28 namespace content {
30 // PPB_Audio_Impl --------------------------------------------------------------
32 PPB_Audio_Impl::PPB_Audio_Impl(PP_Instance instance)
33 : Resource(ppapi::OBJECT_IS_IMPL, instance), audio_(NULL) {}
35 PPB_Audio_Impl::~PPB_Audio_Impl() {
36 // Calling ShutDown() makes sure StreamCreated cannot be called anymore and
37 // releases the audio data associated with the pointer. Note however, that
38 // until ShutDown returns, StreamCreated may still be called. This will be
39 // OK since we'll just immediately clean up the data it stored later in this
40 // destructor.
41 if (audio_) {
42 audio_->ShutDown();
43 audio_ = NULL;
47 PPB_Audio_API* PPB_Audio_Impl::AsPPB_Audio_API() { return this; }
49 PP_Resource PPB_Audio_Impl::GetCurrentConfig() {
50 // AddRef on behalf of caller, while keeping a ref for ourselves.
51 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(config_);
52 return config_;
55 PP_Bool PPB_Audio_Impl::StartPlayback() {
56 if (!audio_)
57 return PP_FALSE;
58 if (playing())
59 return PP_TRUE;
60 SetStartPlaybackState();
61 return PP_FromBool(audio_->StartPlayback());
64 PP_Bool PPB_Audio_Impl::StopPlayback() {
65 if (!audio_)
66 return PP_FALSE;
67 if (!playing())
68 return PP_TRUE;
69 if (!audio_->StopPlayback())
70 return PP_FALSE;
71 SetStopPlaybackState();
72 return PP_TRUE;
75 int32_t PPB_Audio_Impl::Open(PP_Resource config,
76 scoped_refptr<TrackedCallback> create_callback) {
77 // Validate the config and keep a reference to it.
78 EnterResourceNoLock<PPB_AudioConfig_API> enter(config, true);
79 if (enter.failed())
80 return PP_ERROR_FAILED;
81 config_ = config;
83 PepperPluginInstanceImpl* instance = static_cast<PepperPluginInstanceImpl*>(
84 PepperPluginInstance::Get(pp_instance()));
85 if (!instance)
86 return PP_ERROR_FAILED;
88 // Prevent any throttling since we are playing audio. This stopgap prevents
89 // video from appearing 'frozen' while the audio track plays.
90 if (instance->throttler() && instance->throttler()->power_saver_enabled()) {
91 instance->throttler()->MarkPluginEssential(
92 PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_AUDIO);
95 // When the stream is created, we'll get called back on StreamCreated().
96 DCHECK(!audio_);
97 audio_ = PepperPlatformAudioOutput::Create(
98 static_cast<int>(enter.object()->GetSampleRate()),
99 static_cast<int>(enter.object()->GetSampleFrameCount()),
100 instance->GetRenderView()->GetRoutingID(),
101 instance->render_frame()->GetRoutingID(),
102 this);
103 if (!audio_)
104 return PP_ERROR_FAILED;
106 // At this point, we are guaranteeing ownership of the completion
107 // callback. Audio promises to fire the completion callback
108 // once and only once.
109 SetCreateCallback(create_callback);
111 return PP_OK_COMPLETIONPENDING;
114 int32_t PPB_Audio_Impl::GetSyncSocket(int* sync_socket) {
115 return GetSyncSocketImpl(sync_socket);
118 int32_t PPB_Audio_Impl::GetSharedMemory(int* shm_handle, uint32_t* shm_size) {
119 return GetSharedMemoryImpl(shm_handle, shm_size);
122 void PPB_Audio_Impl::OnSetStreamInfo(
123 base::SharedMemoryHandle shared_memory_handle,
124 size_t shared_memory_size,
125 base::SyncSocket::Handle socket_handle) {
126 EnterResourceNoLock<PPB_AudioConfig_API> enter(config_, true);
127 SetStreamInfo(pp_instance(),
128 shared_memory_handle,
129 shared_memory_size,
130 socket_handle,
131 enter.object()->GetSampleRate(),
132 enter.object()->GetSampleFrameCount());
135 } // namespace content