tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

VideoEngine.h (4173B)


      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_VideoEngine_h
      8 #define mozilla_VideoEngine_h
      9 
     10 #include <functional>
     11 #include <map>
     12 #include <memory>
     13 
     14 #include "MediaEventSource.h"
     15 #include "mozilla/DefineEnum.h"
     16 #include "video_engine/video_capture_factory.h"
     17 
     18 namespace webrtc {
     19 class DesktopCaptureImpl;
     20 }
     21 
     22 namespace mozilla::camera {
     23 
     24 MOZ_DEFINE_ENUM_CLASS_WITH_TOSTRING(CaptureDeviceType,
     25                                    (Camera, Screen, Window, Browser));
     26 
     27 // Historically the video engine was part of webrtc
     28 // it was removed (and reimplemented in Talk)
     29 class VideoEngine : public webrtc::VideoInputFeedBack {
     30 private:
     31  virtual ~VideoEngine();
     32 
     33  // Base cache expiration period
     34  // Note because cameras use HW plug event detection, this
     35  // only applies to screen based modes.
     36  static const int64_t kCacheExpiryPeriodMs = 2000;
     37 
     38 public:
     39  NS_INLINE_DECL_REFCOUNTING(VideoEngine)
     40 
     41  static already_AddRefed<VideoEngine> Create(
     42      const CaptureDeviceType& aCaptureDeviceType,
     43      RefPtr<VideoCaptureFactory> aVideoCaptureFactory);
     44 #if defined(ANDROID)
     45  static int SetAndroidObjects();
     46 #endif
     47  int32_t GenerateId();
     48  /** Returns a non-negative capture identifier or -1 on failure.
     49   */
     50  int32_t CreateVideoCapture(const char* aDeviceUniqueIdUTF8,
     51                             uint64_t aWindowID);
     52 
     53  int ReleaseVideoCapture(const int32_t aId);
     54 
     55  // VideoEngine is responsible for any cleanup in its modules
     56  static void Delete(VideoEngine* aEngine) {}
     57 
     58  /** Returns an existing or creates a new new DeviceInfo.
     59   *   Camera info is cached to prevent repeated lengthy polling for "realness"
     60   *   of the hardware devices.  Other types of capture, e.g. screen share info,
     61   *   are cached for 1 second. This could be handled in a more elegant way in
     62   *   the future.
     63   *   @return on failure the shared_ptr will be null, otherwise it will contain
     64   *   a DeviceInfo.
     65   *   @see bug 1305212 https://bugzilla.mozilla.org/show_bug.cgi?id=1305212
     66   */
     67  std::shared_ptr<webrtc::VideoCaptureModule::DeviceInfo>
     68  GetOrCreateVideoCaptureDeviceInfo();
     69 
     70  /**
     71   * Destroys existing DeviceInfo.
     72   *  The DeviceInfo will be recreated the next time it is needed.
     73   */
     74  void ClearVideoCaptureDeviceInfo();
     75 
     76  class CaptureEntry {
     77   public:
     78    CaptureEntry(int32_t aCapnum,
     79                 webrtc::scoped_refptr<webrtc::VideoCaptureModule> aCapture,
     80                 webrtc::DesktopCaptureImpl* aDesktopImpl);
     81    int32_t Capnum() const;
     82    webrtc::scoped_refptr<webrtc::VideoCaptureModule> VideoCapture();
     83    mozilla::MediaEventSource<void>* CaptureEndedEvent();
     84 
     85   private:
     86    int32_t mCapnum;
     87    webrtc::scoped_refptr<webrtc::VideoCaptureModule> mVideoCaptureModule;
     88    webrtc::DesktopCaptureImpl* mDesktopImpl = nullptr;
     89    friend class VideoEngine;
     90  };
     91 
     92  struct CaptureHandle {
     93    int32_t mCaptureEntryNum{};
     94    uint64_t mWindowID{};
     95  };
     96 
     97  // Returns true iff an entry for capnum exists
     98  bool WithEntry(const int32_t entryCapnum,
     99                 const std::function<void(CaptureEntry& entry)>&& fn);
    100 
    101  bool IsWindowCapturing(uint64_t aWindowID, const nsCString& aUniqueIdUTF8);
    102 
    103  void OnDeviceChange() override;
    104 
    105  MediaEventSource<void>& DeviceChangeEvent() { return mDeviceChangeEvent; }
    106 
    107 private:
    108  VideoEngine(const CaptureDeviceType& aCaptureDeviceType,
    109              RefPtr<VideoCaptureFactory> aVideoCaptureFactory);
    110  int32_t mId;
    111  const CaptureDeviceType mCaptureDevType;
    112  const RefPtr<VideoCaptureFactory> mVideoCaptureFactory;
    113  std::shared_ptr<webrtc::VideoCaptureModule::DeviceInfo> mDeviceInfo;
    114  std::map<int32_t, CaptureEntry> mSharedCapturers;
    115  std::map<int32_t, CaptureHandle> mIdToCapturerMap;
    116  MediaEventProducer<void> mDeviceChangeEvent;
    117  // The validity period for non-camera capture device infos`
    118  webrtc::Timestamp mExpiryTime = webrtc::Timestamp::Micros(0);
    119 };
    120 }  // namespace mozilla::camera
    121 #endif