[email protected] | 00d320a | 2012-02-14 00:27:04 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 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 "ppapi/proxy/ppb_audio_proxy.h" |
| 6 | |
[email protected] | 79d23c7 | 2011-08-08 19:40:40 | [diff] [blame] | 7 | #include "base/compiler_specific.h" |
[email protected] | ac9ba8fe | 2010-12-30 18:08:36 | [diff] [blame] | 8 | #include "base/threading/simple_thread.h" |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 9 | #include "build/build_config.h" |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 10 | #include "ppapi/c/pp_errors.h" |
[email protected] | b9a5984 | 2011-01-15 01:04:00 | [diff] [blame] | 11 | #include "ppapi/c/ppb_audio.h" |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 12 | #include "ppapi/c/ppb_audio_config.h" |
| 13 | #include "ppapi/c/ppb_var.h" |
[email protected] | cd910b9 | 2011-06-01 07:19:31 | [diff] [blame] | 14 | #include "ppapi/proxy/enter_proxy.h" |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 15 | #include "ppapi/proxy/plugin_dispatcher.h" |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 16 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 17 | #include "ppapi/shared_impl/api_id.h" |
[email protected] | 5a77eca | 2011-11-23 23:46:46 | [diff] [blame] | 18 | #include "ppapi/shared_impl/platform_file.h" |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 19 | #include "ppapi/shared_impl/ppapi_globals.h" |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 20 | #include "ppapi/shared_impl/ppb_audio_shared.h" |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 21 | #include "ppapi/shared_impl/resource.h" |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 22 | #include "ppapi/thunk/enter.h" |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 23 | #include "ppapi/thunk/ppb_audio_config_api.h" |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 24 | #include "ppapi/thunk/resource_creation_api.h" |
| 25 | #include "ppapi/thunk/thunk.h" |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 26 | |
[email protected] | 5a77eca | 2011-11-23 23:46:46 | [diff] [blame] | 27 | using ppapi::IntToPlatformFile; |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 28 | using ppapi::proxy::SerializedHandle; |
[email protected] | 79d23c7 | 2011-08-08 19:40:40 | [diff] [blame] | 29 | using ppapi::thunk::EnterResourceNoLock; |
| 30 | using ppapi::thunk::PPB_Audio_API; |
| 31 | using ppapi::thunk::PPB_AudioConfig_API; |
[email protected] | cd910b9 | 2011-06-01 07:19:31 | [diff] [blame] | 32 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 33 | namespace ppapi { |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 34 | namespace proxy { |
| 35 | |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 36 | class Audio : public Resource, public PPB_Audio_Shared { |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 37 | public: |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 38 | Audio(const HostResource& audio_id, |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 39 | PP_Resource config_id, |
[email protected] | 6b151126 | 2013-09-06 21:46:34 | [diff] [blame] | 40 | const AudioCallbackCombined& callback, |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 41 | void* user_data); |
Peter Boström | 3d5b3cb | 2021-09-23 21:35:45 | [diff] [blame] | 42 | |
| 43 | Audio(const Audio&) = delete; |
| 44 | Audio& operator=(const Audio&) = delete; |
| 45 | |
jvoung | bd2e01dd | 2015-01-21 20:35:39 | [diff] [blame] | 46 | ~Audio() override; |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 47 | |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 48 | // Resource overrides. |
jvoung | bd2e01dd | 2015-01-21 20:35:39 | [diff] [blame] | 49 | PPB_Audio_API* AsPPB_Audio_API() override; |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 50 | |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 51 | // PPB_Audio_API implementation. |
jvoung | bd2e01dd | 2015-01-21 20:35:39 | [diff] [blame] | 52 | PP_Resource GetCurrentConfig() override; |
| 53 | PP_Bool StartPlayback() override; |
| 54 | PP_Bool StopPlayback() override; |
| 55 | int32_t Open(PP_Resource config_id, |
| 56 | scoped_refptr<TrackedCallback> create_callback) override; |
| 57 | int32_t GetSyncSocket(int* sync_socket) override; |
Alexandr Ilin | 20f2841c | 2018-06-01 11:56:18 | [diff] [blame] | 58 | int32_t GetSharedMemory(base::UnsafeSharedMemoryRegion** shm) override; |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 59 | |
| 60 | private: |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 61 | // Owning reference to the current config object. This isn't actually used, |
| 62 | // we just dish it out as requested by the plugin. |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 63 | PP_Resource config_; |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 64 | }; |
| 65 | |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 66 | Audio::Audio(const HostResource& audio_id, |
| 67 | PP_Resource config_id, |
[email protected] | 6b151126 | 2013-09-06 21:46:34 | [diff] [blame] | 68 | const AudioCallbackCombined& callback, |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 69 | void* user_data) |
[email protected] | 00d320a | 2012-02-14 00:27:04 | [diff] [blame] | 70 | : Resource(OBJECT_IS_PROXY, audio_id), |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 71 | config_(config_id) { |
| 72 | SetCallback(callback, user_data); |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 73 | PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(config_); |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | Audio::~Audio() { |
Xiaohan Wang | 0fd6e56a | 2022-01-13 20:26:11 | [diff] [blame] | 77 | #if BUILDFLAG(IS_NACL) |
[email protected] | 4310fd8 | 2012-09-20 23:38:29 | [diff] [blame] | 78 | // Invoke StopPlayback() to ensure audio back-end has a chance to send the |
| 79 | // escape value over the sync socket, which will terminate the client side |
| 80 | // audio callback loop. This is required for NaCl Plugins that can't escape |
| 81 | // by shutting down the sync_socket. |
| 82 | StopPlayback(); |
| 83 | #endif |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 84 | PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(config_); |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | cd910b9 | 2011-06-01 07:19:31 | [diff] [blame] | 87 | PPB_Audio_API* Audio::AsPPB_Audio_API() { |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 88 | return this; |
| 89 | } |
| 90 | |
| 91 | PP_Resource Audio::GetCurrentConfig() { |
| 92 | // AddRef for the caller. |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 93 | PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(config_); |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 94 | return config_; |
| 95 | } |
| 96 | |
| 97 | PP_Bool Audio::StartPlayback() { |
| 98 | if (playing()) |
| 99 | return PP_TRUE; |
[email protected] | a4a01e4 | 2014-04-21 10:36:21 | [diff] [blame] | 100 | if (!PPB_Audio_Shared::IsThreadFunctionReady()) |
| 101 | return PP_FALSE; |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 102 | SetStartPlaybackState(); |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 103 | PluginDispatcher::GetForResource(this)->Send( |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 104 | new PpapiHostMsg_PPBAudio_StartOrStop( |
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 105 | API_ID_PPB_AUDIO, host_resource(), true)); |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 106 | return PP_TRUE; |
| 107 | } |
| 108 | |
| 109 | PP_Bool Audio::StopPlayback() { |
| 110 | if (!playing()) |
| 111 | return PP_TRUE; |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 112 | PluginDispatcher::GetForResource(this)->Send( |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 113 | new PpapiHostMsg_PPBAudio_StartOrStop( |
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 114 | API_ID_PPB_AUDIO, host_resource(), false)); |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 115 | SetStopPlaybackState(); |
| 116 | return PP_TRUE; |
| 117 | } |
| 118 | |
[email protected] | 9590f860 | 2013-04-23 18:55:21 | [diff] [blame] | 119 | int32_t Audio::Open(PP_Resource config_id, |
| 120 | scoped_refptr<TrackedCallback> create_callback) { |
[email protected] | 79d23c7 | 2011-08-08 19:40:40 | [diff] [blame] | 121 | return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface. |
| 122 | } |
| 123 | |
| 124 | int32_t Audio::GetSyncSocket(int* sync_socket) { |
| 125 | return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface. |
| 126 | } |
| 127 | |
Alexandr Ilin | 20f2841c | 2018-06-01 11:56:18 | [diff] [blame] | 128 | int32_t Audio::GetSharedMemory(base::UnsafeSharedMemoryRegion** shm) { |
[email protected] | 79d23c7 | 2011-08-08 19:40:40 | [diff] [blame] | 129 | return PP_ERROR_NOTSUPPORTED; // Don't proxy the trusted interface. |
| 130 | } |
| 131 | |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 132 | PPB_Audio_Proxy::PPB_Audio_Proxy(Dispatcher* dispatcher) |
| 133 | : InterfaceProxy(dispatcher), |
[email protected] | a2f53dc | 2013-04-30 01:06:35 | [diff] [blame] | 134 | callback_factory_(this) { |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | PPB_Audio_Proxy::~PPB_Audio_Proxy() { |
| 138 | } |
| 139 | |
[email protected] | 465faa2 | 2011-02-08 16:31:46 | [diff] [blame] | 140 | // static |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 141 | PP_Resource PPB_Audio_Proxy::CreateProxyResource( |
| 142 | PP_Instance instance_id, |
| 143 | PP_Resource config_id, |
[email protected] | 6b151126 | 2013-09-06 21:46:34 | [diff] [blame] | 144 | const AudioCallbackCombined& audio_callback, |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 145 | void* user_data) { |
| 146 | PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance_id); |
| 147 | if (!dispatcher) |
| 148 | return 0; |
| 149 | |
[email protected] | 79d23c7 | 2011-08-08 19:40:40 | [diff] [blame] | 150 | EnterResourceNoLock<PPB_AudioConfig_API> config(config_id, true); |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 151 | if (config.failed()) |
| 152 | return 0; |
| 153 | |
[email protected] | 6b151126 | 2013-09-06 21:46:34 | [diff] [blame] | 154 | if (!audio_callback.IsValid()) |
[email protected] | c6b2b7f | 2011-12-01 02:38:25 | [diff] [blame] | 155 | return 0; |
| 156 | |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 157 | HostResource result; |
| 158 | dispatcher->Send(new PpapiHostMsg_PPBAudio_Create( |
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 159 | API_ID_PPB_AUDIO, instance_id, |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 160 | config.object()->GetSampleRate(), config.object()->GetSampleFrameCount(), |
| 161 | &result)); |
| 162 | if (result.is_null()) |
| 163 | return 0; |
| 164 | |
[email protected] | bab65ef | 2011-08-20 04:53:22 | [diff] [blame] | 165 | return (new Audio(result, config_id, |
| 166 | audio_callback, user_data))->GetReference(); |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 169 | bool PPB_Audio_Proxy::OnMessageReceived(const IPC::Message& msg) { |
| 170 | bool handled = true; |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 171 | IPC_BEGIN_MESSAGE_MAP(PPB_Audio_Proxy, msg) |
[email protected] | 771fc8d | 2012-05-19 01:39:05 | [diff] [blame] | 172 | // Don't build host side into NaCl IRT. |
Xiaohan Wang | 0fd6e56a | 2022-01-13 20:26:11 | [diff] [blame] | 173 | #if !BUILDFLAG(IS_NACL) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 174 | IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudio_Create, OnMsgCreate) |
| 175 | IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBAudio_StartOrStop, |
| 176 | OnMsgStartOrStop) |
[email protected] | 771fc8d | 2012-05-19 01:39:05 | [diff] [blame] | 177 | #endif |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 178 | IPC_MESSAGE_HANDLER(PpapiMsg_PPBAudio_NotifyAudioStreamCreated, |
| 179 | OnMsgNotifyAudioStreamCreated) |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 180 | IPC_MESSAGE_UNHANDLED(handled = false) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 181 | IPC_END_MESSAGE_MAP() |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 182 | return handled; |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 183 | } |
| 184 | |
Xiaohan Wang | 0fd6e56a | 2022-01-13 20:26:11 | [diff] [blame] | 185 | #if !BUILDFLAG(IS_NACL) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 186 | void PPB_Audio_Proxy::OnMsgCreate(PP_Instance instance_id, |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 187 | int32_t sample_rate, |
| 188 | uint32_t sample_frame_count, |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 189 | HostResource* result) { |
[email protected] | 4f200612 | 2012-04-30 05:13:17 | [diff] [blame] | 190 | thunk::EnterResourceCreation resource_creation(instance_id); |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 191 | if (resource_creation.failed()) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 192 | return; |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 193 | |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 194 | // Make the resource and get the API pointer to its trusted interface. |
| 195 | result->SetHostResource( |
| 196 | instance_id, |
| 197 | resource_creation.functions()->CreateAudioTrusted(instance_id)); |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 198 | if (result->is_null()) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 199 | return; |
[email protected] | 79d23c7 | 2011-08-08 19:40:40 | [diff] [blame] | 200 | |
| 201 | // At this point, we've set the result resource, and this is a sync request. |
| 202 | // Anything below this point must issue the AudioChannelConnected callback |
| 203 | // to the browser. Since that's an async message, it will be issued back to |
| 204 | // the plugin after the Create function returns (which is good because it |
| 205 | // would be weird to get a connected message with a failure code for a |
| 206 | // resource you haven't finished creating yet). |
| 207 | // |
| 208 | // The ...ForceCallback class will help ensure the callback is always called. |
| 209 | // All error cases must call SetResult on this class. |
| 210 | EnterHostFromHostResourceForceCallback<PPB_Audio_API> enter( |
| 211 | *result, callback_factory_, |
| 212 | &PPB_Audio_Proxy::AudioChannelConnected, *result); |
| 213 | if (enter.failed()) |
| 214 | return; // When enter fails, it will internally schedule the callback. |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 215 | |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 216 | // Make an audio config object. |
| 217 | PP_Resource audio_config_res = |
| 218 | resource_creation.functions()->CreateAudioConfig( |
| 219 | instance_id, static_cast<PP_AudioSampleRate>(sample_rate), |
| 220 | sample_frame_count); |
[email protected] | 79d23c7 | 2011-08-08 19:40:40 | [diff] [blame] | 221 | if (!audio_config_res) { |
| 222 | enter.SetResult(PP_ERROR_FAILED); |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 223 | return; |
[email protected] | 79d23c7 | 2011-08-08 19:40:40 | [diff] [blame] | 224 | } |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 225 | |
| 226 | // Initiate opening the audio object. |
[email protected] | 9590f860 | 2013-04-23 18:55:21 | [diff] [blame] | 227 | enter.SetResult(enter.object()->Open(audio_config_res, |
| 228 | enter.callback())); |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 229 | |
| 230 | // Clean up the temporary audio config resource we made. |
| 231 | const PPB_Core* core = static_cast<const PPB_Core*>( |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 232 | dispatcher()->local_get_interface()(PPB_CORE_INTERFACE)); |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 233 | core->ReleaseResource(audio_config_res); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 234 | } |
| 235 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 236 | void PPB_Audio_Proxy::OnMsgStartOrStop(const HostResource& audio_id, |
| 237 | bool play) { |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 238 | EnterHostFromHostResource<PPB_Audio_API> enter(audio_id); |
| 239 | if (enter.failed()) |
| 240 | return; |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 241 | if (play) |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 242 | enter.object()->StartPlayback(); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 243 | else |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 244 | enter.object()->StopPlayback(); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 245 | } |
| 246 | |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 247 | void PPB_Audio_Proxy::AudioChannelConnected( |
| 248 | int32_t result, |
| 249 | const HostResource& resource) { |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 250 | IPC::PlatformFileForTransit socket_handle = |
| 251 | IPC::InvalidPlatformFileForTransit(); |
Alexandr Ilin | 20f2841c | 2018-06-01 11:56:18 | [diff] [blame] | 252 | base::UnsafeSharedMemoryRegion shared_memory_region; |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 253 | |
| 254 | int32_t result_code = result; |
| 255 | if (result_code == PP_OK) { |
| 256 | result_code = GetAudioConnectedHandles(resource, &socket_handle, |
Alexandr Ilin | 20f2841c | 2018-06-01 11:56:18 | [diff] [blame] | 257 | &shared_memory_region); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // Send all the values, even on error. This simplifies some of our cleanup |
| 261 | // code since the handles will be in the other process and could be |
| 262 | // inconvenient to clean up. Our IPC code will automatically handle this for |
| 263 | // us, as long as the remote side always closes the handles it receives |
| 264 | // (in OnMsgNotifyAudioStreamCreated), even in the failure case. |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 265 | SerializedHandle fd_wrapper(SerializedHandle::SOCKET, socket_handle); |
Alexandr Ilin | 20f2841c | 2018-06-01 11:56:18 | [diff] [blame] | 266 | SerializedHandle handle_wrapper( |
| 267 | base::UnsafeSharedMemoryRegion::TakeHandleForSerialization( |
| 268 | std::move(shared_memory_region))); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 269 | dispatcher()->Send(new PpapiMsg_PPBAudio_NotifyAudioStreamCreated( |
Alexandr Ilin | 20f2841c | 2018-06-01 11:56:18 | [diff] [blame] | 270 | API_ID_PPB_AUDIO, resource, result_code, std::move(fd_wrapper), |
| 271 | std::move(handle_wrapper))); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | int32_t PPB_Audio_Proxy::GetAudioConnectedHandles( |
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 275 | const HostResource& resource, |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 276 | IPC::PlatformFileForTransit* foreign_socket_handle, |
Alexandr Ilin | 20f2841c | 2018-06-01 11:56:18 | [diff] [blame] | 277 | base::UnsafeSharedMemoryRegion* foreign_shared_memory_region) { |
[email protected] | 79d23c7 | 2011-08-08 19:40:40 | [diff] [blame] | 278 | // Get the audio interface which will give us the handles. |
[email protected] | 5c96602 | 2011-09-13 18:09:37 | [diff] [blame] | 279 | EnterHostFromHostResource<PPB_Audio_API> enter(resource); |
[email protected] | 79d23c7 | 2011-08-08 19:40:40 | [diff] [blame] | 280 | if (enter.failed()) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 281 | return PP_ERROR_NOINTERFACE; |
| 282 | |
| 283 | // Get the socket handle for signaling. |
| 284 | int32_t socket_handle; |
[email protected] | 79d23c7 | 2011-08-08 19:40:40 | [diff] [blame] | 285 | int32_t result = enter.object()->GetSyncSocket(&socket_handle); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 286 | if (result != PP_OK) |
| 287 | return result; |
| 288 | |
[email protected] | db0f57f5 | 2011-02-25 03:54:54 | [diff] [blame] | 289 | // socket_handle doesn't belong to us: don't close it. |
| 290 | *foreign_socket_handle = dispatcher()->ShareHandleWithRemote( |
| 291 | IntToPlatformFile(socket_handle), false); |
| 292 | if (*foreign_socket_handle == IPC::InvalidPlatformFileForTransit()) |
| 293 | return PP_ERROR_FAILED; |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 294 | |
| 295 | // Get the shared memory for the buffer. |
Alexandr Ilin | 20f2841c | 2018-06-01 11:56:18 | [diff] [blame] | 296 | base::UnsafeSharedMemoryRegion* shared_memory_region; |
| 297 | result = enter.object()->GetSharedMemory(&shared_memory_region); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 298 | if (result != PP_OK) |
| 299 | return result; |
| 300 | |
Alexandr Ilin | 20f2841c | 2018-06-01 11:56:18 | [diff] [blame] | 301 | // shared_memory_region doesn't belong to us: don't close it. |
| 302 | *foreign_shared_memory_region = |
| 303 | dispatcher()->ShareUnsafeSharedMemoryRegionWithRemote( |
| 304 | *shared_memory_region); |
| 305 | if (!foreign_shared_memory_region->IsValid()) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 306 | return PP_ERROR_FAILED; |
| 307 | |
| 308 | return PP_OK; |
| 309 | } |
Xiaohan Wang | 0fd6e56a | 2022-01-13 20:26:11 | [diff] [blame] | 310 | #endif // !BUILDFLAG(IS_NACL) |
[email protected] | 771fc8d | 2012-05-19 01:39:05 | [diff] [blame] | 311 | |
| 312 | // Processed in the plugin (message from host). |
| 313 | void PPB_Audio_Proxy::OnMsgNotifyAudioStreamCreated( |
| 314 | const HostResource& audio_id, |
| 315 | int32_t result_code, |
[email protected] | 8510d28 | 2012-08-30 19:47:38 | [diff] [blame] | 316 | SerializedHandle socket_handle, |
| 317 | SerializedHandle handle) { |
[email protected] | 246fc49 | 2012-08-27 20:28:18 | [diff] [blame] | 318 | CHECK(socket_handle.is_socket()); |
Alexandr Ilin | 20f2841c | 2018-06-01 11:56:18 | [diff] [blame] | 319 | CHECK(handle.is_shmem_region()); |
[email protected] | 771fc8d | 2012-05-19 01:39:05 | [diff] [blame] | 320 | EnterPluginFromHostResource<PPB_Audio_API> enter(audio_id); |
| 321 | if (enter.failed() || result_code != PP_OK) { |
| 322 | // The caller may still have given us these handles in the failure case. |
Alexandr Ilin | 20f2841c | 2018-06-01 11:56:18 | [diff] [blame] | 323 | // The easiest way to clean socket handle up is to just put them in the |
| 324 | // SyncSocket object and then close it. The shared memory region will be |
| 325 | // cleaned up automatically. This failure case is not performance critical. |
[email protected] | 771fc8d | 2012-05-19 01:39:05 | [diff] [blame] | 326 | base::SyncSocket temp_socket( |
[email protected] | 246fc49 | 2012-08-27 20:28:18 | [diff] [blame] | 327 | IPC::PlatformFileForTransitToPlatformFile(socket_handle.descriptor())); |
[email protected] | 771fc8d | 2012-05-19 01:39:05 | [diff] [blame] | 328 | } else { |
[email protected] | dfc48e4c | 2012-09-05 13:39:21 | [diff] [blame] | 329 | EnterResourceNoLock<PPB_AudioConfig_API> config( |
| 330 | static_cast<Audio*>(enter.object())->GetCurrentConfig(), true); |
Alexandr Ilin | 20f2841c | 2018-06-01 11:56:18 | [diff] [blame] | 331 | static_cast<Audio*>(enter.object()) |
| 332 | ->SetStreamInfo(enter.resource()->pp_instance(), |
| 333 | base::UnsafeSharedMemoryRegion::Deserialize( |
| 334 | handle.TakeSharedMemoryRegion()), |
Robert Sesek | 8dd0645 | 2020-02-19 01:10:49 | [diff] [blame] | 335 | base::SyncSocket::ScopedHandle( |
| 336 | IPC::PlatformFileForTransitToPlatformFile( |
| 337 | socket_handle.descriptor())), |
Alexandr Ilin | 20f2841c | 2018-06-01 11:56:18 | [diff] [blame] | 338 | config.object()->GetSampleRate(), |
| 339 | config.object()->GetSampleFrameCount()); |
[email protected] | 771fc8d | 2012-05-19 01:39:05 | [diff] [blame] | 340 | } |
| 341 | } |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 342 | |
| 343 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 344 | } // namespace ppapi |