Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [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 | |
Florent Castelli | 8764dd45 | 2023-02-09 13:43:30 | [diff] [blame] | 5 | #include "content/browser/video_capture_service_impl.h" |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 6 | |
| 7 | #include "base/no_destructor.h" |
Gabriel Charette | 83d937e | 2020-02-29 01:17:56 | [diff] [blame] | 8 | #include "base/task/thread_pool.h" |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 9 | #include "base/threading/sequence_local_storage_slot.h" |
| 10 | #include "base/time/time.h" |
| 11 | #include "build/build_config.h" |
| 12 | #include "content/public/browser/browser_task_traits.h" |
| 13 | #include "content/public/browser/browser_thread.h" |
Will Harris | cd57b83 | 2023-01-05 20:03:10 | [diff] [blame] | 14 | #include "content/public/browser/child_process_host.h" |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 15 | #include "content/public/browser/service_process_host.h" |
Alex Gough | 6a170e6d | 2024-03-07 00:07:22 | [diff] [blame] | 16 | #include "content/public/browser/service_process_host_passkeys.h" |
Florent Castelli | 8764dd45 | 2023-02-09 13:43:30 | [diff] [blame] | 17 | #include "content/public/browser/video_capture_service.h" |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 18 | #include "content/public/common/content_features.h" |
| 19 | #include "content/public/common/content_switches.h" |
| 20 | #include "mojo/public/cpp/bindings/receiver_set.h" |
| 21 | #include "mojo/public/cpp/bindings/remote.h" |
| 22 | #include "mojo/public/cpp/bindings/self_owned_receiver.h" |
Alex Gough | 0177d79 | 2021-08-12 19:39:49 | [diff] [blame] | 23 | #include "services/video_capture/public/mojom/video_capture_service.mojom.h" |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 24 | #include "services/video_capture/video_capture_service_impl.h" |
| 25 | |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 26 | #if BUILDFLAG(IS_WIN) |
Gabriel Charette | 83d937e | 2020-02-29 01:17:56 | [diff] [blame] | 27 | #define CREATE_IN_PROCESS_TASK_RUNNER base::ThreadPool::CreateCOMSTATaskRunner |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 28 | #else |
Gabriel Charette | 83d937e | 2020-02-29 01:17:56 | [diff] [blame] | 29 | #define CREATE_IN_PROCESS_TASK_RUNNER \ |
| 30 | base::ThreadPool::CreateSingleThreadTaskRunner |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 31 | #endif |
| 32 | |
| 33 | namespace content { |
| 34 | |
| 35 | namespace { |
Alex Gough | 6a170e6d | 2024-03-07 00:07:22 | [diff] [blame] | 36 | |
Florent Castelli | 8764dd45 | 2023-02-09 13:43:30 | [diff] [blame] | 37 | std::atomic<bool> g_use_safe_mode(false); |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 38 | |
Alex Gough | 6a170e6d | 2024-03-07 00:07:22 | [diff] [blame] | 39 | } // namespace |
| 40 | |
| 41 | // Helper class to allow access to class-based passkeys. |
| 42 | class VideoCaptureServiceLauncher { |
| 43 | public: |
| 44 | static void Launch( |
| 45 | mojo::PendingReceiver<video_capture::mojom::VideoCaptureService> |
| 46 | receiver) { |
| 47 | ServiceProcessHost::Options options; |
| 48 | options.WithDisplayName("Video Capture"); |
| 49 | // TODO(crbug.com/328099369) Remove once gpu client is provided directly. |
| 50 | options.WithGpuClient(ServiceProcessHostGpuClient::GetPassKey()); |
| 51 | #if BUILDFLAG(IS_MAC) |
| 52 | // On Mac, the service requires a CFRunLoop which is provided by a |
| 53 | // UI message loop. See https://crbug.com/834581. |
| 54 | options.WithExtraCommandLineSwitches({switches::kMessageLoopTypeUi}); |
| 55 | if (g_use_safe_mode) { |
| 56 | // When safe-mode is enabled, we keep the original entitlements and the |
| 57 | // hardened runtime to only load safe DAL plugins and reduce crash risk |
| 58 | // from third-party DAL plugins. |
| 59 | // As this is not possible to do with unsigned developer builds, we use |
| 60 | // an undocumented environment variable that macOS CMIO module checks to |
| 61 | // prevent loading any plugins. |
| 62 | setenv("CMIO_DAL_Ignore_Standard_PlugIns", "", 1); |
| 63 | } else { |
| 64 | // On Mac, the service also needs to have a different set of |
| 65 | // entitlements, the reason being that some virtual cameras DAL plugins |
| 66 | // are not signed or are signed by a different Team ID. Hence, |
| 67 | // library validation has to be disabled (see |
| 68 | // http://crbug.com/990381#c21). |
| 69 | options.WithChildFlags(ChildProcessHost::CHILD_PLUGIN); |
| 70 | } |
| 71 | #endif |
Michael Olbrich | edcd0133 | 2024-05-28 17:23:04 | [diff] [blame] | 72 | #if defined(WEBRTC_USE_PIPEWIRE) |
| 73 | // The PipeWire camera implementation in webrtc uses gdbus for portal |
| 74 | // handling, so the glib message loop must be used. |
| 75 | options.WithExtraCommandLineSwitches({switches::kMessageLoopTypeUi}); |
| 76 | #endif |
Alex Gough | 6a170e6d | 2024-03-07 00:07:22 | [diff] [blame] | 77 | |
| 78 | ServiceProcessHost::Launch(std::move(receiver), options.Pass()); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | namespace { |
| 83 | |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 84 | video_capture::mojom::VideoCaptureService* g_service_override = nullptr; |
| 85 | |
| 86 | void BindInProcessInstance( |
| 87 | mojo::PendingReceiver<video_capture::mojom::VideoCaptureService> receiver) { |
| 88 | static base::NoDestructor<video_capture::VideoCaptureServiceImpl> service( |
Palak Agarwal | f0416e2b | 2023-11-24 21:33:54 | [diff] [blame] | 89 | std::move(receiver), GetUIThreadTaskRunner({}), |
| 90 | /*create_system_monitor=*/false); |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | mojo::Remote<video_capture::mojom::VideoCaptureService>& GetUIThreadRemote() { |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 94 | // NOTE: This use of sequence-local storage is only to ensure that the Remote |
| 95 | // only lives as long as the UI-thread sequence, since the UI-thread sequence |
| 96 | // may be torn down and reinitialized e.g. between unit tests. |
Avi Drissman | ded7717 | 2021-07-02 18:23:00 | [diff] [blame] | 97 | static base::SequenceLocalStorageSlot< |
| 98 | mojo::Remote<video_capture::mojom::VideoCaptureService>> |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 99 | remote_slot; |
Avi Drissman | ded7717 | 2021-07-02 18:23:00 | [diff] [blame] | 100 | return remote_slot.GetOrCreateValue(); |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // This is a custom traits type we use in conjunction with mojo::ReceiverSetBase |
| 104 | // so that all dispatched messages can be forwarded to the currently bound UI |
| 105 | // thread Remote. |
| 106 | struct ForwardingImplRefTraits { |
| 107 | using PointerType = void*; |
| 108 | static bool IsNull(PointerType) { return false; } |
| 109 | static video_capture::mojom::VideoCaptureService* GetRawPointer(PointerType) { |
| 110 | return &GetVideoCaptureService(); |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | // If |GetVideoCaptureService()| is called from off the UI thread, return a |
| 115 | // sequence-local Remote. Its corresponding receiver will be bound in this set, |
| 116 | // forwarding to the current UI-thread Remote. |
| 117 | void BindProxyRemoteOnUIThread( |
| 118 | mojo::PendingReceiver<video_capture::mojom::VideoCaptureService> receiver) { |
| 119 | static base::NoDestructor<mojo::ReceiverSetBase< |
| 120 | mojo::Receiver<video_capture::mojom::VideoCaptureService, |
| 121 | ForwardingImplRefTraits>, |
| 122 | void>> |
| 123 | receivers; |
| 124 | receivers->Add(nullptr, std::move(receiver)); |
| 125 | } |
| 126 | |
| 127 | } // namespace |
| 128 | |
Florent Castelli | 8764dd45 | 2023-02-09 13:43:30 | [diff] [blame] | 129 | void EnableVideoCaptureServiceSafeMode() { |
| 130 | LOG(WARNING) << "Enabling safe mode VideoCaptureService"; |
| 131 | g_use_safe_mode = true; |
| 132 | } |
| 133 | |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 134 | video_capture::mojom::VideoCaptureService& GetVideoCaptureService() { |
| 135 | if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
Avi Drissman | ded7717 | 2021-07-02 18:23:00 | [diff] [blame] | 136 | static base::SequenceLocalStorageSlot< |
| 137 | mojo::Remote<video_capture::mojom::VideoCaptureService>> |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 138 | storage; |
Avi Drissman | ded7717 | 2021-07-02 18:23:00 | [diff] [blame] | 139 | auto& remote = storage.GetOrCreateValue(); |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 140 | if (!remote.is_bound()) { |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 141 | GetUIThreadTaskRunner({})->PostTask( |
| 142 | FROM_HERE, base::BindOnce(&BindProxyRemoteOnUIThread, |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 143 | remote.BindNewPipeAndPassReceiver())); |
| 144 | } |
| 145 | return *remote.get(); |
| 146 | } |
| 147 | |
Florent Castelli | 8764dd45 | 2023-02-09 13:43:30 | [diff] [blame] | 148 | if (g_service_override) { |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 149 | return *g_service_override; |
Florent Castelli | 8764dd45 | 2023-02-09 13:43:30 | [diff] [blame] | 150 | } |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 151 | |
| 152 | auto& remote = GetUIThreadRemote(); |
| 153 | if (!remote.is_bound()) { |
| 154 | auto receiver = remote.BindNewPipeAndPassReceiver(); |
| 155 | if (features::IsVideoCaptureServiceEnabledForBrowserProcess()) { |
| 156 | auto dedicated_task_runner = CREATE_IN_PROCESS_TASK_RUNNER( |
Gabriel Charette | 83d937e | 2020-02-29 01:17:56 | [diff] [blame] | 157 | {base::MayBlock(), base::WithBaseSyncPrimitives(), |
| 158 | base::TaskPriority::BEST_EFFORT}, |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 159 | base::SingleThreadTaskRunnerThreadMode::DEDICATED); |
| 160 | dedicated_task_runner->PostTask( |
| 161 | FROM_HERE, |
| 162 | base::BindOnce(&BindInProcessInstance, std::move(receiver))); |
| 163 | } else { |
Alex Gough | 6a170e6d | 2024-03-07 00:07:22 | [diff] [blame] | 164 | // Launch in a utility service. |
| 165 | VideoCaptureServiceLauncher::Launch(std::move(receiver)); |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 166 | #if !BUILDFLAG(IS_ANDROID) |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 167 | // On Android, we do not use automatic service shutdown, because when |
| 168 | // shutting down the service, we lose caching of the supported formats, |
| 169 | // and re-querying these can take several seconds on certain Android |
| 170 | // devices. |
| 171 | remote.set_idle_handler( |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 172 | base::Seconds(5), |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 173 | base::BindRepeating( |
| 174 | [](mojo::Remote<video_capture::mojom::VideoCaptureService>* |
Ilya Nikolaevskiy | 3cd4d18 | 2022-11-29 13:49:03 | [diff] [blame] | 175 | remote) { remote->reset(); }, |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 176 | &remote)); |
Xiaohan Wang | 1ecfd00 | 2022-01-19 22:33:10 | [diff] [blame] | 177 | #endif // !BUILDFLAG(IS_ANDROID) |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 178 | |
| 179 | // Make sure the Remote is also reset in case of e.g. service crash so we |
| 180 | // can restart it as needed. |
| 181 | remote.reset_on_disconnect(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return *remote.get(); |
| 186 | } |
| 187 | |
Florent Castelli | 8764dd45 | 2023-02-09 13:43:30 | [diff] [blame] | 188 | void OverrideVideoCaptureServiceForTesting( // IN-TEST |
Ken Rockot | 2f8c1dd2 | 2019-07-23 21:56:02 | [diff] [blame] | 189 | video_capture::mojom::VideoCaptureService* service) { |
| 190 | g_service_override = service; |
| 191 | } |
| 192 | |
| 193 | } // namespace content |