video_capture_factory.h (3984B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set sw=2 ts=8 et ft=cpp : */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef MOZILLA_VIDEO_CAPTURE_FACTORY_H_ 8 #define MOZILLA_VIDEO_CAPTURE_FACTORY_H_ 9 10 #include "modules/video_capture/video_capture.h" 11 #include "modules/video_capture/video_capture_factory.h" 12 #include "modules/video_capture/video_capture_options.h" 13 #include "mozilla/MozPromise.h" 14 15 namespace mozilla::camera { 16 enum class CaptureDeviceType; 17 } 18 19 namespace webrtc { 20 class DesktopCaptureImpl; 21 } 22 23 namespace mozilla { 24 /** 25 * NOTE: This class must be accessed only on a single SerialEventTarget 26 */ 27 class VideoCaptureFactory : webrtc::VideoCaptureOptions::Callback { 28 public: 29 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(VideoCaptureFactory); 30 31 enum CameraAvailability { Unknown, Available, NotAvailable }; 32 33 VideoCaptureFactory(); 34 35 std::shared_ptr<webrtc::VideoCaptureModule::DeviceInfo> CreateDeviceInfo( 36 int32_t aId, mozilla::camera::CaptureDeviceType aType); 37 38 struct CreateVideoCaptureResult { 39 webrtc::scoped_refptr<webrtc::VideoCaptureModule> mCapturer; 40 // Pointer to the DesktopCaptureImpl instance if mCapturer is of this type. 41 // nullptr otherwise. 42 webrtc::DesktopCaptureImpl* mDesktopImpl = nullptr; 43 }; 44 45 CreateVideoCaptureResult CreateVideoCapture( 46 int32_t aModuleId, const char* aUniqueId, 47 mozilla::camera::CaptureDeviceType aType); 48 49 using CameraBackendInitPromise = MozPromise<nsresult, nsresult, false>; 50 /** 51 * Request to initialize webrtc::VideoCaptureOptions 52 * 53 * Resolves with NS_OK when VideoCaptureOptions has been properly initialized 54 * or rejects with one of the possible errors. Since this is only now 55 * supported by PipeWire, all the errors are PipeWire specific: 56 * 1) NS_ERROR_NOT_AVAILABLE - PipeWire libraries are not available on 57 * the system 58 * 2) NS_ERROR_DOM_MEDIA_NOT_ALLOWED_ERR - camera access has been rejected 59 * 3) NS_ERROR_FAILURE - generic error, usually a PipeWire failure 60 */ 61 RefPtr<CameraBackendInitPromise> InitCameraBackend(); 62 63 /** 64 * Updates information about camera availability 65 */ 66 using UpdateCameraAvailabilityPromise = 67 MozPromise<CameraAvailability, nsresult, true>; 68 RefPtr<UpdateCameraAvailabilityPromise> UpdateCameraAvailability(); 69 70 /** 71 * Invalidate any runtime state tied to backend creation. 72 */ 73 void Invalidate(); 74 75 private: 76 ~VideoCaptureFactory() = default; 77 // aka OnCameraBackendInitialized 78 // this method override has to follow webrtc::VideoCaptureOptions::Callback 79 void OnInitialized(webrtc::VideoCaptureOptions::Status status) override; 80 81 /** 82 * Resolves with true or false depending on whether there is a camera device 83 * advertised by the xdg-desktop-portal (Camera portal). Rejects with one 84 * of the following errors: 85 * 1) NS_ERROR_NOT_IMPLEMENTED - support for the Camera portal is not 86 * implemented or enabled 87 * 2) NS_ERROR_NO_INTERFACE - the camera portal is not available 88 * 3) NS_ERROR_UNEXPECTED - the camera portal returned wrong value 89 */ 90 using HasCameraDevicePromise = MozPromise<CameraAvailability, nsresult, true>; 91 RefPtr<HasCameraDevicePromise> HasCameraDevice(); 92 93 // Whether we have created fake camera device info. If true, 94 // CreateVideoCapture needs to create a fake capturer. 95 Maybe<bool> mUseFakeCamera; 96 std::atomic<bool> mCameraBackendInitialized = false; 97 CameraAvailability mCameraAvailability = Unknown; 98 #if (defined(WEBRTC_LINUX) || defined(WEBRTC_BSD)) && !defined(WEBRTC_ANDROID) 99 std::unique_ptr<webrtc::VideoCaptureOptions> mVideoCaptureOptions; 100 #endif 101 MozPromiseHolder<CameraBackendInitPromise> mPromiseHolder; 102 RefPtr<CameraBackendInitPromise> mPromise; 103 }; 104 105 } // namespace mozilla 106 107 #endif // MOZILLA_VIDEO_CAPTURE_FACTORY_H_