Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: content/renderer/pepper/pepper_audio_output_host.cc

Issue 2755613002: Support audio output device enumeration and selection in PPAPI (Closed)
Patch Set: Fix format, Rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2017 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.
4
5 #include "content/renderer/pepper/pepper_audio_output_host.h"
6
7 #include "base/logging.h"
8 #include "build/build_config.h"
9 #include "content/common/pepper_file_util.h"
10 #include "content/renderer/pepper/pepper_audio_controller.h"
11 #include "content/renderer/pepper/pepper_media_device_manager.h"
12 #include "content/renderer/pepper/pepper_platform_audio_output_dev.h"
13 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
14 #include "content/renderer/pepper/plugin_instance_throttler_impl.h"
15 #include "content/renderer/pepper/renderer_ppapi_host_impl.h"
16 #include "content/renderer/render_frame_impl.h"
17 #include "ipc/ipc_message.h"
18 #include "ppapi/c/pp_errors.h"
19 #include "ppapi/host/dispatch_host_message.h"
20 #include "ppapi/host/ppapi_host.h"
21 #include "ppapi/proxy/ppapi_messages.h"
22 #include "ppapi/proxy/serialized_structs.h"
23
24 namespace content {
25
26 namespace {
27
28 base::PlatformFile ConvertSyncSocketHandle(const base::SyncSocket& socket) {
29 return socket.handle();
30 }
31
32 } // namespace
33
34 PepperAudioOutputHost::PepperAudioOutputHost(RendererPpapiHostImpl* host,
35 PP_Instance instance,
36 PP_Resource resource)
37 : ResourceHost(host->GetPpapiHost(), instance, resource),
38 renderer_ppapi_host_(host),
39 audio_output_(NULL),
40 playback_throttled_(false),
41 enumeration_helper_(this,
42 PepperMediaDeviceManager::GetForRenderFrame(
43 host->GetRenderFrameForInstance(pp_instance())),
44 PP_DEVICETYPE_DEV_AUDIOOUTPUT,
45 host->GetDocumentURL(instance)) {
46 PepperPluginInstanceImpl* plugin_instance =
47 static_cast<PepperPluginInstanceImpl*>(
48 PepperPluginInstance::Get(pp_instance()));
49 if (plugin_instance && plugin_instance->throttler())
50 plugin_instance->throttler()->AddObserver(this);
51 }
52
53 PepperAudioOutputHost::~PepperAudioOutputHost() {
54 PepperPluginInstanceImpl* instance = static_cast<PepperPluginInstanceImpl*>(
55 PepperPluginInstance::Get(pp_instance()));
56 if (instance) {
57 if (instance->throttler()) {
58 instance->throttler()->RemoveObserver(this);
59 }
60 instance->audio_controller().RemoveInstance(this);
61 }
62 Close();
63 }
64
65 int32_t PepperAudioOutputHost::OnResourceMessageReceived(
66 const IPC::Message& msg,
67 ppapi::host::HostMessageContext* context) {
68 int32_t result = PP_ERROR_FAILED;
69 if (enumeration_helper_.HandleResourceMessage(msg, context, &result))
70 return result;
71
72 PPAPI_BEGIN_MESSAGE_MAP(PepperAudioOutputHost, msg)
73 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_AudioOutput_Open, OnOpen)
74 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_AudioOutput_StartOrStop,
75 OnStartOrStop)
76 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_AudioOutput_Close, OnClose)
77 PPAPI_END_MESSAGE_MAP()
78 return PP_ERROR_FAILED;
79 }
80
81 void PepperAudioOutputHost::StreamCreated(
82 base::SharedMemoryHandle shared_memory_handle,
83 size_t shared_memory_size,
84 base::SyncSocket::Handle socket) {
85 OnOpenComplete(PP_OK, shared_memory_handle, shared_memory_size, socket);
86 }
87
88 void PepperAudioOutputHost::StreamCreationFailed() {
89 OnOpenComplete(PP_ERROR_FAILED, base::SharedMemory::NULLHandle(), 0,
90 base::SyncSocket::kInvalidHandle);
91 }
92
93 void PepperAudioOutputHost::SetVolume(double volume) {
94 if (audio_output_)
95 audio_output_->SetVolume(volume);
96 }
97
98 int32_t PepperAudioOutputHost::OnOpen(ppapi::host::HostMessageContext* context,
99 const std::string& device_id,
100 PP_AudioSampleRate sample_rate,
101 uint32_t sample_frame_count) {
102 if (open_context_.is_valid())
103 return PP_ERROR_INPROGRESS;
104
105 if (audio_output_)
106 return PP_ERROR_FAILED;
107
108 GURL document_url = renderer_ppapi_host_->GetDocumentURL(pp_instance());
109 if (!document_url.is_valid())
110 return PP_ERROR_FAILED;
111
112 // When it is done, we'll get called back on StreamCreated() or
113 // StreamCreationFailed().
114 audio_output_ = PepperPlatformAudioOutputDev::Create(
115 renderer_ppapi_host_->GetRenderFrameForInstance(pp_instance())
116 ->GetRoutingID(),
117 device_id, document_url, static_cast<int>(sample_rate),
118 static_cast<int>(sample_frame_count), this);
119 if (audio_output_) {
120 open_context_ = context->MakeReplyMessageContext();
121 return PP_OK_COMPLETIONPENDING;
122 } else {
123 return PP_ERROR_FAILED;
124 }
125 }
126
127 int32_t PepperAudioOutputHost::OnStartOrStop(
128 ppapi::host::HostMessageContext* /* context */,
129 bool playback) {
130 if (!audio_output_)
131 return PP_ERROR_FAILED;
132
133 PepperPluginInstanceImpl* instance = static_cast<PepperPluginInstanceImpl*>(
134 PepperPluginInstance::Get(pp_instance()));
135
136 if (playback) {
137 // If plugin is in power saver mode, defer audio IPC communication.
138 if (instance && instance->throttler() &&
139 instance->throttler()->power_saver_enabled()) {
140 instance->throttler()->NotifyAudioThrottled();
141 playback_throttled_ = true;
142 return PP_TRUE;
143 }
144 if (instance)
145 instance->audio_controller().AddInstance(this);
146
147 audio_output_->StartPlayback();
148 } else {
149 if (instance)
150 instance->audio_controller().RemoveInstance(this);
151
152 audio_output_->StopPlayback();
153 }
154 return PP_OK;
155 }
156
157 int32_t PepperAudioOutputHost::OnClose(
158 ppapi::host::HostMessageContext* /* context */) {
159 Close();
160 return PP_OK;
161 }
162
163 void PepperAudioOutputHost::OnOpenComplete(
164 int32_t result,
165 base::SharedMemoryHandle shared_memory_handle,
166 size_t shared_memory_size,
167 base::SyncSocket::Handle socket_handle) {
168 // Make sure the handles are cleaned up.
169 base::SyncSocket scoped_socket(socket_handle);
170 base::SharedMemory scoped_shared_memory(shared_memory_handle, false);
171
172 if (!open_context_.is_valid()) {
173 NOTREACHED();
174 return;
175 }
176
177 ppapi::proxy::SerializedHandle serialized_socket_handle(
178 ppapi::proxy::SerializedHandle::SOCKET);
179 ppapi::proxy::SerializedHandle serialized_shared_memory_handle(
180 ppapi::proxy::SerializedHandle::SHARED_MEMORY);
181
182 if (result == PP_OK) {
183 IPC::PlatformFileForTransit temp_socket =
184 IPC::InvalidPlatformFileForTransit();
185 base::SharedMemoryHandle temp_shmem = base::SharedMemory::NULLHandle();
186 result = GetRemoteHandles(scoped_socket, scoped_shared_memory, &temp_socket,
187 &temp_shmem);
188
189 serialized_socket_handle.set_socket(temp_socket);
190 serialized_shared_memory_handle.set_shmem(temp_shmem, shared_memory_size);
191 }
192
193 // Send all the values, even on error. This simplifies some of our cleanup
194 // code since the handles will be in the other process and could be
195 // inconvenient to clean up. Our IPC code will automatically handle this for
196 // us, as long as the remote side always closes the handles it receives, even
197 // in the failure case.
198 open_context_.params.AppendHandle(serialized_socket_handle);
199 open_context_.params.AppendHandle(serialized_shared_memory_handle);
200 SendOpenReply(result);
201 }
202
203 int32_t PepperAudioOutputHost::GetRemoteHandles(
204 const base::SyncSocket& socket,
205 const base::SharedMemory& shared_memory,
206 IPC::PlatformFileForTransit* remote_socket_handle,
207 base::SharedMemoryHandle* remote_shared_memory_handle) {
208 *remote_socket_handle = renderer_ppapi_host_->ShareHandleWithRemote(
209 ConvertSyncSocketHandle(socket), false);
210 if (*remote_socket_handle == IPC::InvalidPlatformFileForTransit())
211 return PP_ERROR_FAILED;
212
213 *remote_shared_memory_handle =
214 renderer_ppapi_host_->ShareSharedMemoryHandleWithRemote(
215 shared_memory.handle());
216 if (!base::SharedMemory::IsHandleValid(*remote_shared_memory_handle))
217 return PP_ERROR_FAILED;
218
219 return PP_OK;
220 }
221
222 void PepperAudioOutputHost::Close() {
223 if (!audio_output_)
224 return;
225
226 audio_output_->ShutDown();
227 audio_output_ = NULL;
228
229 if (open_context_.is_valid())
230 SendOpenReply(PP_ERROR_ABORTED);
231 }
232
233 void PepperAudioOutputHost::SendOpenReply(int32_t result) {
234 open_context_.params.set_result(result);
235 host()->SendReply(open_context_, PpapiPluginMsg_AudioOutput_OpenReply());
236 open_context_ = ppapi::host::ReplyMessageContext();
237 }
238
239 void PepperAudioOutputHost::OnThrottleStateChange() {
240 PepperPluginInstanceImpl* instance = static_cast<PepperPluginInstanceImpl*>(
241 PepperPluginInstance::Get(pp_instance()));
242 if (playback_throttled_ && instance && instance->throttler() &&
243 !instance->throttler()->power_saver_enabled()) {
244 // If we have become unthrottled, and we have a pending playback,
245 // start it.
246 StartDeferredPlayback();
247 }
248 }
249
250 void PepperAudioOutputHost::StartDeferredPlayback() {
251 if (!audio_output_)
252 return;
253
254 DCHECK(playback_throttled_);
255 playback_throttled_ = false;
256
257 PepperPluginInstanceImpl* instance = static_cast<PepperPluginInstanceImpl*>(
258 PepperPluginInstance::Get(pp_instance()));
259 if (instance)
260 instance->audio_controller().AddInstance(this);
261
262 audio_output_->StartPlayback();
263 }
264
265 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/pepper/pepper_audio_output_host.h ('k') | content/renderer/pepper/pepper_media_device_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698