Line data Source code
1 : /* This Source Code Form is subject to the terms of the Mozilla Public
2 : * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
4 :
5 : #ifndef MediaEngineCameraVideoSource_h
6 : #define MediaEngineCameraVideoSource_h
7 :
8 : #include "MediaEngine.h"
9 :
10 : #include "nsDirectoryServiceDefs.h"
11 :
12 : // conflicts with #include of scoped_ptr.h
13 : #undef FF
14 : // Avoid warnings about redefinition of WARN_UNUSED_RESULT
15 : #include "ipc/IPCMessageUtils.h"
16 :
17 : // WebRTC includes
18 : #include "webrtc/modules/video_capture/video_capture_defines.h"
19 :
20 : namespace webrtc {
21 : using CaptureCapability = VideoCaptureCapability;
22 : }
23 :
24 : namespace mozilla {
25 :
26 : class MediaEngineCameraVideoSource : public MediaEngineVideoSource
27 : {
28 : public:
29 : // Some subclasses use an index to track multiple instances.
30 0 : explicit MediaEngineCameraVideoSource(int aIndex,
31 : const char* aMonitorName = "Camera.Monitor")
32 0 : : MediaEngineVideoSource(kReleased)
33 : , mMonitor(aMonitorName)
34 : , mWidth(0)
35 : , mHeight(0)
36 : , mInitDone(false)
37 : , mHasDirectListeners(false)
38 : , mCaptureIndex(aIndex)
39 0 : , mTrackID(0)
40 0 : {}
41 :
42 0 : explicit MediaEngineCameraVideoSource(const char* aMonitorName = "Camera.Monitor")
43 0 : : MediaEngineCameraVideoSource(0, aMonitorName) {}
44 :
45 : void GetName(nsAString& aName) const override;
46 : void GetUUID(nsACString& aUUID) const override;
47 : void SetDirectListeners(bool aHasListeners) override;
48 :
49 0 : bool IsFake() override
50 : {
51 0 : return false;
52 : }
53 :
54 0 : nsresult TakePhoto(MediaEnginePhotoCallback* aCallback) override
55 : {
56 0 : return NS_ERROR_NOT_IMPLEMENTED;
57 : }
58 :
59 : uint32_t GetBestFitnessDistance(
60 : const nsTArray<const NormalizedConstraintSet*>& aConstraintSets,
61 : const nsString& aDeviceId) const override;
62 :
63 0 : void Shutdown() override {};
64 :
65 : protected:
66 : struct CapabilityCandidate {
67 0 : explicit CapabilityCandidate(uint8_t index, uint32_t distance = 0)
68 0 : : mIndex(index), mDistance(distance) {}
69 :
70 : size_t mIndex;
71 : uint32_t mDistance;
72 : };
73 : typedef nsTArray<CapabilityCandidate> CapabilitySet;
74 :
75 0 : ~MediaEngineCameraVideoSource() {}
76 :
77 : // guts for appending data to the MSG track
78 : virtual bool AppendToTrack(SourceMediaStream* aSource,
79 : layers::Image* aImage,
80 : TrackID aID,
81 : StreamTime delta,
82 : const PrincipalHandle& aPrincipalHandle);
83 : uint32_t GetFitnessDistance(const webrtc::CaptureCapability& aCandidate,
84 : const NormalizedConstraintSet &aConstraints,
85 : const nsString& aDeviceId) const;
86 : static void TrimLessFitCandidates(CapabilitySet& set);
87 : static void LogConstraints(const NormalizedConstraintSet& aConstraints);
88 : static void LogCapability(const char* aHeader,
89 : const webrtc::CaptureCapability &aCapability,
90 : uint32_t aDistance);
91 : virtual size_t NumCapabilities() const;
92 : virtual void GetCapability(size_t aIndex, webrtc::CaptureCapability& aOut) const;
93 : virtual bool ChooseCapability(const NormalizedConstraints &aConstraints,
94 : const MediaEnginePrefs &aPrefs,
95 : const nsString& aDeviceId);
96 : void SetName(nsString aName);
97 : void SetUUID(const char* aUUID);
98 : const nsCString& GetUUID() const; // protected access
99 :
100 : // Engine variables.
101 :
102 : // mMonitor protects mImage access/changes, and transitions of mState
103 : // from kStarted to kStopped (which are combined with EndTrack() and
104 : // image changes).
105 : // mMonitor also protects mSources[] and mPrincipalHandles[] access/changes.
106 : // mSources[] and mPrincipalHandles[] are accessed from webrtc threads.
107 :
108 : // All the mMonitor accesses are from the child classes.
109 : Monitor mMonitor; // Monitor for processing Camera frames.
110 : nsTArray<RefPtr<SourceMediaStream>> mSources; // When this goes empty, we shut down HW
111 : nsTArray<PrincipalHandle> mPrincipalHandles; // Directly mapped to mSources.
112 : RefPtr<layers::Image> mImage;
113 : RefPtr<layers::ImageContainer> mImageContainer;
114 : int mWidth, mHeight; // protected with mMonitor on Gonk due to different threading
115 : // end of data protected by mMonitor
116 :
117 :
118 : bool mInitDone;
119 : bool mHasDirectListeners;
120 : int mCaptureIndex;
121 : TrackID mTrackID;
122 :
123 : webrtc::CaptureCapability mCapability;
124 :
125 : mutable nsTArray<webrtc::CaptureCapability> mHardcodedCapabilities;
126 : private:
127 : nsString mDeviceName;
128 : nsCString mUniqueId;
129 : nsString mFacingMode;
130 : };
131 :
132 :
133 : } // namespace mozilla
134 :
135 : #endif // MediaEngineCameraVideoSource_h
|