Source code

Revision control

Other Tools

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 MEDIAENGINE_REMOTE_VIDEO_SOURCE_H_
8
#define MEDIAENGINE_REMOTE_VIDEO_SOURCE_H_
9
10
#include "prcvar.h"
11
#include "prthread.h"
12
#include "nsIThread.h"
13
#include "nsIRunnable.h"
14
15
#include "mozilla/Mutex.h"
16
#include "nsCOMPtr.h"
17
#include "nsThreadUtils.h"
18
#include "DOMMediaStream.h"
19
#include "nsDirectoryServiceDefs.h"
20
#include "nsComponentManagerUtils.h"
21
22
// Avoid warnings about redefinition of WARN_UNUSED_RESULT
23
#include "ipc/IPCMessageUtils.h"
24
#include "VideoUtils.h"
25
#include "MediaEngineSource.h"
26
#include "VideoSegment.h"
27
#include "AudioSegment.h"
28
#include "MediaTrackGraph.h"
29
30
#include "MediaEngineWrapper.h"
31
#include "mozilla/dom/MediaStreamTrackBinding.h"
32
33
// Camera Access via IPC
34
#include "CamerasChild.h"
35
36
#include "NullTransport.h"
37
38
// WebRTC includes
39
#include "webrtc/common_video/include/i420_buffer_pool.h"
40
#include "webrtc/modules/video_capture/video_capture_defines.h"
41
42
namespace webrtc {
43
using CaptureCapability = VideoCaptureCapability;
44
}
45
46
namespace mozilla {
47
48
// Fitness distance is defined in
50
51
// The main difference of feasibility and fitness distance is that if the
52
// constraint is required ('max', or 'exact'), and the settings dictionary's
53
// value for the constraint does not satisfy the constraint, the fitness
54
// distance is positive infinity. Given a continuous space of settings
55
// dictionaries comprising all discrete combinations of dimension and frame-rate
56
// related properties, the feasibility distance is still in keeping with the
57
// constraints algorithm.
58
enum DistanceCalculation { kFitness, kFeasibility };
59
60
/**
61
* The WebRTC implementation of the MediaEngine interface.
62
*/
63
class MediaEngineRemoteVideoSource : public MediaEngineSource,
64
public camera::FrameRelay {
65
~MediaEngineRemoteVideoSource();
66
67
struct CapabilityCandidate {
68
explicit CapabilityCandidate(webrtc::CaptureCapability aCapability,
69
uint32_t aDistance = 0)
70
: mCapability(aCapability), mDistance(aDistance) {}
71
72
const webrtc::CaptureCapability mCapability;
73
uint32_t mDistance;
74
};
75
76
class CapabilityComparator {
77
public:
78
bool Equals(const CapabilityCandidate& aCandidate,
79
const webrtc::CaptureCapability& aCapability) const {
80
return aCandidate.mCapability == aCapability;
81
}
82
};
83
84
bool ChooseCapability(const NormalizedConstraints& aConstraints,
85
const MediaEnginePrefs& aPrefs,
86
webrtc::CaptureCapability& aCapability,
87
const DistanceCalculation aCalculate);
88
89
uint32_t GetDistance(const webrtc::CaptureCapability& aCandidate,
90
const NormalizedConstraintSet& aConstraints,
91
const DistanceCalculation aCalculate) const;
92
93
uint32_t GetFitnessDistance(
94
const webrtc::CaptureCapability& aCandidate,
95
const NormalizedConstraintSet& aConstraints) const;
96
97
uint32_t GetFeasibilityDistance(
98
const webrtc::CaptureCapability& aCandidate,
99
const NormalizedConstraintSet& aConstraints) const;
100
101
static void TrimLessFitCandidates(nsTArray<CapabilityCandidate>& aSet);
102
103
public:
104
MediaEngineRemoteVideoSource(int aIndex, camera::CaptureEngine aCapEngine,
105
bool aScary);
106
107
// ExternalRenderer
108
int DeliverFrame(uint8_t* aBuffer,
109
const camera::VideoFrameProperties& aProps) override;
110
111
// MediaEngineSource
112
dom::MediaSourceEnum GetMediaSource() const override;
113
nsresult Allocate(const dom::MediaTrackConstraints& aConstraints,
114
const MediaEnginePrefs& aPrefs,
115
const ipc::PrincipalInfo& aPrincipalInfo,
116
const char** aOutBadConstraint) override;
117
nsresult Deallocate() override;
118
void SetTrack(const RefPtr<SourceMediaTrack>& aTrack,
119
const PrincipalHandle& aPrincipal) override;
120
nsresult Start() override;
121
nsresult Reconfigure(const dom::MediaTrackConstraints& aConstraints,
122
const MediaEnginePrefs& aPrefs,
123
const char** aOutBadConstraint) override;
124
nsresult FocusOnSelectedSource() override;
125
nsresult Stop() override;
126
127
uint32_t GetBestFitnessDistance(
128
const nsTArray<const NormalizedConstraintSet*>& aConstraintSets)
129
const override;
130
void GetSettings(dom::MediaTrackSettings& aOutSettings) const override;
131
132
void Refresh(int aIndex);
133
134
void Shutdown() override;
135
136
nsString GetName() const override;
137
void SetName(nsString aName);
138
139
nsCString GetUUID() const override;
140
void SetUUID(const char* aUUID);
141
142
nsString GetGroupId() const override;
143
void SetGroupId(nsString aGroupId);
144
145
bool GetScary() const override { return mScary; }
146
147
RefPtr<GenericNonExclusivePromise> GetFirstFramePromise() const override {
148
return mFirstFramePromise;
149
}
150
151
private:
152
// Initialize the needed Video engine interfaces.
153
void Init();
154
155
/**
156
* Returns the number of capabilities for the underlying device.
157
*
158
* Guaranteed to return at least one capability.
159
*/
160
size_t NumCapabilities() const;
161
162
/**
163
* Returns the capability with index `aIndex` for our assigned device.
164
*
165
* It is an error to call this with `aIndex >= NumCapabilities()`.
166
*
167
* The lifetime of the returned capability is the same as for this source.
168
*/
169
webrtc::CaptureCapability& GetCapability(size_t aIndex) const;
170
171
int mCaptureIndex;
172
const camera::CaptureEngine mCapEngine; // source of media (cam, screen etc)
173
const bool mScary;
174
175
// mMutex protects certain members on 3 threads:
176
// MediaManager, Cameras IPC and MediaTrackGraph.
177
Mutex mMutex;
178
179
// Current state of this source.
180
// Set under mMutex on the owning thread. Accessed under one of the two.
181
MediaEngineSourceState mState = kReleased;
182
183
// The source track that we feed video data to.
184
// Set under mMutex on the owning thread. Accessed under one of the two.
185
RefPtr<SourceMediaTrack> mTrack;
186
187
// The PrincipalHandle that gets attached to the frames we feed to mTrack.
188
// Set under mMutex on the owning thread. Accessed under one of the two.
189
PrincipalHandle mPrincipal = PRINCIPAL_HANDLE_NONE;
190
191
// Set in Start() and Deallocate() on the owning thread.
192
// Accessed in DeliverFrame() on the camera IPC thread, guaranteed to happen
193
// after Start() and before the end of Stop().
194
RefPtr<layers::ImageContainer> mImageContainer;
195
196
// A buffer pool used to manage the temporary buffer used when rescaling
197
// incoming images. Cameras IPC thread only.
198
webrtc::I420BufferPool mRescalingBufferPool;
199
200
// The intrinsic size of the latest captured image, so we can feed black
201
// images of the same size while stopped.
202
// Set under mMutex on the Cameras IPC thread. Accessed under one of the two.
203
gfx::IntSize mImageSize = gfx::IntSize(0, 0);
204
205
struct AtomicBool {
206
Atomic<bool> mValue;
207
};
208
209
// True when resolution settings have been updated from a real frame's
210
// resolution. Threadsafe.
211
// TODO: This can be removed in bug 1453269.
212
const RefPtr<media::Refcountable<AtomicBool>> mSettingsUpdatedByFrame;
213
214
// The current settings of this source.
215
// Note that these may be different from the settings of the underlying device
216
// since we scale frames to avoid fingerprinting.
217
// Members are main thread only.
218
const RefPtr<media::Refcountable<dom::MediaTrackSettings>> mSettings;
219
MozPromiseHolder<GenericNonExclusivePromise> mFirstFramePromiseHolder;
220
RefPtr<GenericNonExclusivePromise> mFirstFramePromise;
221
222
// The capability currently chosen by constraints of the user of this source.
223
// Set under mMutex on the owning thread. Accessed under one of the two.
224
webrtc::CaptureCapability mCapability;
225
226
/**
227
* Capabilities that we choose between when applying constraints.
228
*
229
* This allows for memoization of capabilities as they're requested from the
230
* parent process.
231
*
232
* This is mutable so that the const methods NumCapabilities() and
233
* GetCapability() can reset it. Owning thread only.
234
*/
235
mutable nsTArray<UniquePtr<webrtc::CaptureCapability>> mCapabilities;
236
237
/**
238
* True if mCapabilities only contains hardcoded capabilities. This can happen
239
* if the underlying device is not reporting any capabilities. These can be
240
* affected by constraints, so they're evaluated in ChooseCapability() rather
241
* than GetCapability().
242
*
243
* This is mutable so that the const methods NumCapabilities() and
244
* GetCapability() can reset it. Owning thread only.
245
*/
246
mutable bool mCapabilitiesAreHardcoded = false;
247
248
nsString mDeviceName;
249
nsCString mUniqueId;
250
Maybe<nsString> mFacingMode;
251
252
// Whether init has successfully completed.
253
// Set in Init(), reset in Shutdown().
254
// Owning thread only.
255
bool mInitDone = false;
256
};
257
258
} // namespace mozilla
259
260
#endif /* MEDIAENGINE_REMOTE_VIDEO_SOURCE_H_ */