Chromium Code Reviews| OLD | NEW |
|---|---|
| (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.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, | |
| 90 base::SharedMemory::NULLHandle(), | |
| 91 0, | |
| 92 base::SyncSocket::kInvalidHandle); | |
| 93 } | |
| 94 | |
| 95 void PepperAudioOutputHost::SetVolume(double volume) { | |
| 96 if (audio_output_) | |
| 97 audio_output_->SetVolume(volume); | |
| 98 } | |
| 99 | |
| 100 int32_t PepperAudioOutputHost::OnOpen(ppapi::host::HostMessageContext* context, | |
| 101 const std::string& device_id, | |
| 102 PP_AudioSampleRate sample_rate, | |
| 103 uint32_t sample_frame_count) { | |
| 104 if (open_context_.is_valid()) | |
| 105 return PP_ERROR_INPROGRESS; | |
| 106 if (audio_output_) | |
| 107 return PP_ERROR_FAILED; | |
| 108 | |
| 109 GURL document_url = renderer_ppapi_host_->GetDocumentURL(pp_instance()); | |
| 110 if (!document_url.is_valid()) | |
| 111 return PP_ERROR_FAILED; | |
| 112 | |
| 113 // When it is done, we'll get called back on StreamCreated() or | |
| 114 // StreamCreationFailed(). | |
| 115 audio_output_ = PepperPlatformAudioOutput::Create( | |
| 116 renderer_ppapi_host_->GetRenderFrameForInstance(pp_instance())-> | |
| 117 GetRoutingID(), | |
| 118 device_id, | |
| 119 document_url, | |
| 120 static_cast<int>(sample_rate), | |
| 121 static_cast<int>(sample_frame_count), | |
| 122 this); | |
| 123 if (audio_output_) { | |
| 124 open_context_ = context->MakeReplyMessageContext(); | |
| 125 return PP_OK_COMPLETIONPENDING; | |
| 126 } else { | |
| 127 return PP_ERROR_FAILED; | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 int32_t PepperAudioOutputHost::OnStartOrStop( | |
| 132 ppapi::host::HostMessageContext* /* context */, | |
| 133 bool playback) { | |
| 134 if (!audio_output_) | |
| 135 return PP_ERROR_FAILED; | |
| 136 | |
| 137 PepperPluginInstanceImpl* instance = static_cast<PepperPluginInstanceImpl*>( | |
| 138 PepperPluginInstance::Get(pp_instance())); | |
| 139 | |
| 140 if (playback) { | |
| 141 // If plugin is in power saver mode, defer audio IPC communication. | |
| 142 if (instance && instance->throttler() && | |
| 143 instance->throttler()->power_saver_enabled()) { | |
| 144 instance->throttler()->NotifyAudioThrottled(); | |
| 145 playback_throttled_ = true; | |
| 146 return PP_TRUE; | |
| 147 } | |
| 148 if (instance) | |
| 149 instance->audio_controller().AddInstance(this); | |
| 150 | |
| 151 audio_output_->StartPlayback(); | |
| 152 } | |
| 153 else { | |
| 154 if (instance) | |
| 155 instance->audio_controller().RemoveInstance(this); | |
| 156 | |
| 157 audio_output_->StopPlayback(); | |
| 158 } | |
| 159 return PP_OK; | |
| 160 } | |
| 161 | |
| 162 int32_t PepperAudioOutputHost::OnClose( | |
| 163 ppapi::host::HostMessageContext* /* context */) { | |
| 164 Close(); | |
| 165 return PP_OK; | |
| 166 } | |
| 167 | |
| 168 void PepperAudioOutputHost::OnOpenComplete( | |
| 169 int32_t result, | |
| 170 base::SharedMemoryHandle shared_memory_handle, | |
| 171 size_t shared_memory_size, | |
| 172 base::SyncSocket::Handle socket_handle) { | |
| 173 // Make sure the handles are cleaned up. | |
| 174 base::SyncSocket scoped_socket(socket_handle); | |
| 175 base::SharedMemory scoped_shared_memory(shared_memory_handle, false); | |
| 176 | |
| 177 if (!open_context_.is_valid()) { | |
| 178 NOTREACHED(); | |
| 179 return; | |
| 180 } | |
| 181 | |
| 182 ppapi::proxy::SerializedHandle serialized_socket_handle( | |
| 183 ppapi::proxy::SerializedHandle::SOCKET); | |
| 184 ppapi::proxy::SerializedHandle serialized_shared_memory_handle( | |
| 185 ppapi::proxy::SerializedHandle::SHARED_MEMORY); | |
| 186 | |
| 187 if (result == PP_OK) { | |
| 188 IPC::PlatformFileForTransit temp_socket = | |
| 189 IPC::InvalidPlatformFileForTransit(); | |
| 190 base::SharedMemoryHandle temp_shmem = base::SharedMemory::NULLHandle(); | |
| 191 result = GetRemoteHandles( | |
| 192 scoped_socket, scoped_shared_memory, &temp_socket, &temp_shmem); | |
| 193 | |
| 194 serialized_socket_handle.set_socket(temp_socket); | |
| 195 serialized_shared_memory_handle.set_shmem(temp_shmem, shared_memory_size); | |
| 196 } | |
| 197 | |
| 198 // Send all the values, even on error. This simplifies some of our cleanup | |
| 199 // code since the handles will be in the other process and could be | |
| 200 // inconvenient to clean up. Our IPC code will automatically handle this for | |
| 201 // us, as long as the remote side always closes the handles it receives, even | |
| 202 // in the failure case. | |
| 203 open_context_.params.AppendHandle(serialized_socket_handle); | |
| 204 open_context_.params.AppendHandle(serialized_shared_memory_handle); | |
| 205 SendOpenReply(result); | |
| 206 } | |
| 207 | |
| 208 int32_t PepperAudioOutputHost::GetRemoteHandles( | |
| 209 const base::SyncSocket& socket, | |
| 210 const base::SharedMemory& shared_memory, | |
| 211 IPC::PlatformFileForTransit* remote_socket_handle, | |
| 212 base::SharedMemoryHandle* remote_shared_memory_handle) { | |
| 213 *remote_socket_handle = renderer_ppapi_host_->ShareHandleWithRemote( | |
| 214 ConvertSyncSocketHandle(socket), false); | |
| 215 if (*remote_socket_handle == IPC::InvalidPlatformFileForTransit()) | |
| 216 return PP_ERROR_FAILED; | |
| 217 | |
| 218 *remote_shared_memory_handle = | |
| 219 renderer_ppapi_host_->ShareSharedMemoryHandleWithRemote( | |
| 220 shared_memory.handle()); | |
| 221 if (!base::SharedMemory::IsHandleValid(*remote_shared_memory_handle)) | |
| 222 return PP_ERROR_FAILED; | |
| 223 | |
| 224 return PP_OK; | |
| 225 } | |
| 226 | |
| 227 void PepperAudioOutputHost::Close() { | |
| 228 if (!audio_output_) | |
| 229 return; | |
| 230 | |
| 231 audio_output_->ShutDown(); | |
| 232 audio_output_ = NULL; | |
| 233 | |
| 234 if (open_context_.is_valid()) | |
| 235 SendOpenReply(PP_ERROR_ABORTED); | |
| 236 } | |
| 237 | |
| 238 void PepperAudioOutputHost::SendOpenReply(int32_t result) { | |
| 239 open_context_.params.set_result(result); | |
| 240 host()->SendReply(open_context_, PpapiPluginMsg_AudioOutput_OpenReply()); | |
| 241 open_context_ = ppapi::host::ReplyMessageContext(); | |
| 242 } | |
| 243 | |
| 244 void PepperAudioOutputHost::OnThrottleStateChange() { | |
| 245 PepperPluginInstanceImpl* instance = static_cast<PepperPluginInstanceImpl*>( | |
| 246 PepperPluginInstance::Get(pp_instance())); | |
| 247 if (playback_throttled_ && instance && instance->throttler() && | |
| 248 !instance->throttler()->power_saver_enabled()) { | |
| 249 // If we have become unthrottled, and we have a pending playback, | |
| 250 // start it. | |
| 251 StartDeferredPlayback(); | |
| 252 } | |
| 253 } | |
| 254 | |
| 255 void PepperAudioOutputHost::StartDeferredPlayback() { | |
| 256 DCHECK(playback_throttled_); | |
| 257 playback_throttled_ = false; | |
| 258 | |
| 259 PepperPluginInstanceImpl* instance = static_cast<PepperPluginInstanceImpl*>( | |
| 260 PepperPluginInstance::Get(pp_instance())); | |
| 261 if (instance) | |
| 262 instance->audio_controller().AddInstance(this); | |
| 263 | |
| 264 audio_output_->StartPlayback(); | |
|
bbudge
2017/03/23 18:05:44
check for null?
Xing
2017/03/29 21:14:32
Done.
| |
| 265 } | |
| 266 } // namespace content | |
| OLD | NEW |