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 "MediaEngine.h"
11 #include "VideoFrameUtils.h"
12 #include "mozilla/media/MediaUtils.h"
13 #include "webrtc/modules/video_capture/video_capture_impl.h"
14 #include "webrtc/modules/video_capture/video_capture_defines.h"
15 #include "webrtc/modules/video_capture/video_capture_factory.h"
16 #include "webrtc/video_engine/desktop_capture_impl.h"
17 #include <memory>
18 #include <functional>
19 
20 namespace mozilla {
21 namespace camera {
22 
23 // Historically the video engine was part of webrtc
24 // it was removed (and reimplemented in Talk)
25 class VideoEngine {
26  private:
~VideoEngine()27   virtual ~VideoEngine(){};
28   // Base cache expiration period
29   // Note because cameras use HW plug event detection, this
30   // only applies to screen based modes.
31   static const int64_t kCacheExpiryPeriodMs = 2000;
32 
33  public:
VideoEngine()34   VideoEngine(){};
35   NS_INLINE_DECL_REFCOUNTING(VideoEngine)
36 
37   static already_AddRefed<VideoEngine> Create(
38       UniquePtr<const webrtc::Config>&& aConfig);
39 #if defined(ANDROID)
40   static int SetAndroidObjects(JavaVM* javaVM);
41 #endif
42   void CreateVideoCapture(int32_t& id, const char* deviceUniqueIdUTF8);
43 
44   int ReleaseVideoCapture(const int32_t id);
45 
46   // VideoEngine is responsible for any cleanup in its modules
Delete(VideoEngine * engine)47   static void Delete(VideoEngine* engine) {}
48 
49   /** Returns an existing or creates a new new DeviceInfo.
50    *   Camera info is cached to prevent repeated lengthy polling for "realness"
51    *   of the hardware devices.  Other types of capture, e.g. screen share info,
52    *   are cached for 1 second. This could be handled in a more elegant way in
53    *   the future.
54    *   @return on failure the shared_ptr will be null, otherwise it will contain
55    *   a DeviceInfo.
56    *   @see bug 1305212 https://bugzilla.mozilla.org/show_bug.cgi?id=1305212
57    */
58   std::shared_ptr<webrtc::VideoCaptureModule::DeviceInfo>
59   GetOrCreateVideoCaptureDeviceInfo();
60 
61   const UniquePtr<const webrtc::Config>& GetConfiguration();
62 
63   class CaptureEntry {
64    public:
65     CaptureEntry(int32_t aCapnum,
66                  rtc::scoped_refptr<webrtc::VideoCaptureModule> aCapture);
67     int32_t Capnum() const;
68     rtc::scoped_refptr<webrtc::VideoCaptureModule> VideoCapture();
69 
70    private:
71     int32_t mCapnum;
72     rtc::scoped_refptr<webrtc::VideoCaptureModule> mVideoCaptureModule;
73     friend class VideoEngine;
74   };
75 
76   // Returns true iff an entry for capnum exists
77   bool WithEntry(const int32_t entryCapnum,
78                  const std::function<void(CaptureEntry& entry)>&& fn);
79 
80  private:
81   explicit VideoEngine(UniquePtr<const webrtc::Config>&& aConfig);
82   int32_t mId;
83   webrtc::CaptureDeviceInfo mCaptureDevInfo;
84   std::shared_ptr<webrtc::VideoCaptureModule::DeviceInfo> mDeviceInfo;
85   UniquePtr<const webrtc::Config> mConfig;
86   std::map<int32_t, CaptureEntry> mCaps;
87   std::map<int32_t, int32_t> mIdMap;
88   // The validity period for non-camera capture device infos`
89   int64_t mExpiryTimeInMs = 0;
90   int32_t GenerateId();
91   static int32_t sId;
92 };
93 }  // namespace camera
94 }  // namespace mozilla
95 #endif
96