Remove render view id from the audio input and output, part two!
[chromium-blink-merge.git] / content / renderer / pepper / ppb_audio_impl.cc
blobd5b1b753a907f8f179bcd527ac55eea01aca3d42
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),
34 audio_(NULL),
35 playback_throttled_(false) {
36 PepperPluginInstanceImpl* plugin_instance =
37 static_cast<PepperPluginInstanceImpl*>(
38 PepperPluginInstance::Get(pp_instance()));
39 if (plugin_instance && plugin_instance->throttler()) {
40 plugin_instance->throttler()->AddObserver(this);
44 PPB_Audio_Impl::~PPB_Audio_Impl() {
45 PepperPluginInstanceImpl* instance = static_cast<PepperPluginInstanceImpl*>(
46 PepperPluginInstance::Get(pp_instance()));
47 if (instance && instance->throttler()) {
48 instance->throttler()->RemoveObserver(this);
51 // Calling ShutDown() makes sure StreamCreated cannot be called anymore and
52 // releases the audio data associated with the pointer. Note however, that
53 // until ShutDown returns, StreamCreated may still be called. This will be
54 // OK since we'll just immediately clean up the data it stored later in this
55 // destructor.
56 if (audio_) {
57 audio_->ShutDown();
58 audio_ = NULL;
62 PPB_Audio_API* PPB_Audio_Impl::AsPPB_Audio_API() { return this; }
64 PP_Resource PPB_Audio_Impl::GetCurrentConfig() {
65 // AddRef on behalf of caller, while keeping a ref for ourselves.
66 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(config_);
67 return config_;
70 PP_Bool PPB_Audio_Impl::StartPlayback() {
71 if (!audio_)
72 return PP_FALSE;
73 if (playing())
74 return PP_TRUE;
76 // If plugin is in power saver mode, defer audio IPC communication.
77 PepperPluginInstanceImpl* instance = static_cast<PepperPluginInstanceImpl*>(
78 PepperPluginInstance::Get(pp_instance()));
79 if (instance && instance->throttler() &&
80 instance->throttler()->power_saver_enabled()) {
81 instance->throttler()->NotifyAudioThrottled();
82 playback_throttled_ = true;
83 return PP_TRUE;
86 SetStartPlaybackState();
87 return PP_FromBool(audio_->StartPlayback());
90 PP_Bool PPB_Audio_Impl::StopPlayback() {
91 if (!audio_)
92 return PP_FALSE;
94 if (playback_throttled_) {
95 // If a start playback request is still deferred, we must fulfill it first
96 // to shut down the audio thread correctly.
97 StartDeferredPlayback();
100 if (!playing())
101 return PP_TRUE;
102 if (!audio_->StopPlayback())
103 return PP_FALSE;
104 SetStopPlaybackState();
106 return PP_TRUE;
109 int32_t PPB_Audio_Impl::Open(PP_Resource config,
110 scoped_refptr<TrackedCallback> create_callback) {
111 // Validate the config and keep a reference to it.
112 EnterResourceNoLock<PPB_AudioConfig_API> enter(config, true);
113 if (enter.failed())
114 return PP_ERROR_FAILED;
115 config_ = config;
117 PepperPluginInstanceImpl* instance = static_cast<PepperPluginInstanceImpl*>(
118 PepperPluginInstance::Get(pp_instance()));
119 if (!instance)
120 return PP_ERROR_FAILED;
122 // When the stream is created, we'll get called back on StreamCreated().
123 DCHECK(!audio_);
124 audio_ = PepperPlatformAudioOutput::Create(
125 static_cast<int>(enter.object()->GetSampleRate()),
126 static_cast<int>(enter.object()->GetSampleFrameCount()),
127 instance->render_frame()->GetRoutingID(),
128 this);
129 if (!audio_)
130 return PP_ERROR_FAILED;
132 // At this point, we are guaranteeing ownership of the completion
133 // callback. Audio promises to fire the completion callback
134 // once and only once.
135 SetCreateCallback(create_callback);
137 return PP_OK_COMPLETIONPENDING;
140 int32_t PPB_Audio_Impl::GetSyncSocket(int* sync_socket) {
141 return GetSyncSocketImpl(sync_socket);
144 int32_t PPB_Audio_Impl::GetSharedMemory(int* shm_handle, uint32_t* shm_size) {
145 return GetSharedMemoryImpl(shm_handle, shm_size);
148 void PPB_Audio_Impl::OnSetStreamInfo(
149 base::SharedMemoryHandle shared_memory_handle,
150 size_t shared_memory_size,
151 base::SyncSocket::Handle socket_handle) {
152 EnterResourceNoLock<PPB_AudioConfig_API> enter(config_, true);
153 SetStreamInfo(pp_instance(),
154 shared_memory_handle,
155 shared_memory_size,
156 socket_handle,
157 enter.object()->GetSampleRate(),
158 enter.object()->GetSampleFrameCount());
161 void PPB_Audio_Impl::OnThrottleStateChange() {
162 PepperPluginInstanceImpl* instance = static_cast<PepperPluginInstanceImpl*>(
163 PepperPluginInstance::Get(pp_instance()));
164 if (playback_throttled_ && instance && instance->throttler() &&
165 !instance->throttler()->power_saver_enabled()) {
166 // If we have become unthrottled, and we have a pending playback, start it.
167 StartDeferredPlayback();
171 void PPB_Audio_Impl::StartDeferredPlayback() {
172 DCHECK(playback_throttled_);
173 playback_throttled_ = false;
175 SetStartPlaybackState();
176 audio_->StartPlayback();
179 } // namespace content