/src/mozilla-central/dom/media/webrtc/MediaEngineDefault.h
Line | Count | Source (jump to first uncovered line) |
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 MEDIAENGINEDEFAULT_H_ |
6 | | #define MEDIAENGINEDEFAULT_H_ |
7 | | |
8 | | #include "nsINamed.h" |
9 | | #include "nsITimer.h" |
10 | | |
11 | | #include "nsAutoPtr.h" |
12 | | #include "nsCOMPtr.h" |
13 | | #include "DOMMediaStream.h" |
14 | | #include "nsComponentManagerUtils.h" |
15 | | #include "mozilla/Mutex.h" |
16 | | |
17 | | #include "VideoUtils.h" |
18 | | #include "MediaEngine.h" |
19 | | #include "MediaEnginePrefs.h" |
20 | | #include "VideoSegment.h" |
21 | | #include "AudioSegment.h" |
22 | | #include "StreamTracks.h" |
23 | | #include "MediaEngineSource.h" |
24 | | #include "MediaStreamGraph.h" |
25 | | #include "SineWaveGenerator.h" |
26 | | |
27 | | namespace mozilla { |
28 | | |
29 | | namespace layers { |
30 | | class ImageContainer; |
31 | | } // namespace layers |
32 | | |
33 | | class MediaEngineDefault; |
34 | | |
35 | | /** |
36 | | * The default implementation of the MediaEngine interface. |
37 | | */ |
38 | | class MediaEngineDefaultVideoSource : public MediaEngineSource |
39 | | { |
40 | | public: |
41 | | MediaEngineDefaultVideoSource(); |
42 | | |
43 | | nsString GetName() const override; |
44 | | nsCString GetUUID() const override; |
45 | | |
46 | | nsresult Allocate(const dom::MediaTrackConstraints &aConstraints, |
47 | | const MediaEnginePrefs &aPrefs, |
48 | | const nsString& aDeviceId, |
49 | | const ipc::PrincipalInfo& aPrincipalInfo, |
50 | | AllocationHandle** aOutHandle, |
51 | | const char** aOutBadConstraint) override; |
52 | | nsresult SetTrack(const RefPtr<const AllocationHandle>& aHandle, |
53 | | const RefPtr<SourceMediaStream>& aStream, |
54 | | TrackID aTrackID, |
55 | | const PrincipalHandle& aPrincipal) override; |
56 | | nsresult Start(const RefPtr<const AllocationHandle>& aHandle) override; |
57 | | nsresult Reconfigure(const RefPtr<AllocationHandle>& aHandle, |
58 | | const dom::MediaTrackConstraints& aConstraints, |
59 | | const MediaEnginePrefs& aPrefs, |
60 | | const nsString& aDeviceId, |
61 | | const char** aOutBadConstraint) override; |
62 | | nsresult Stop(const RefPtr<const AllocationHandle>& aHandle) override; |
63 | | nsresult Deallocate(const RefPtr<const AllocationHandle>& aHandle) override; |
64 | | void Pull(const RefPtr<const AllocationHandle>& aHandle, |
65 | | const RefPtr<SourceMediaStream>& aStream, |
66 | | TrackID aTrackID, |
67 | | StreamTime aDesiredTime, |
68 | | const PrincipalHandle& aPrincipalHandle) override; |
69 | | |
70 | | uint32_t GetBestFitnessDistance( |
71 | | const nsTArray<const NormalizedConstraintSet*>& aConstraintSets, |
72 | | const nsString& aDeviceId) const override; |
73 | | |
74 | | bool IsFake() const override |
75 | 0 | { |
76 | 0 | return true; |
77 | 0 | } |
78 | | |
79 | | dom::MediaSourceEnum GetMediaSource() const override |
80 | 0 | { |
81 | 0 | return dom::MediaSourceEnum::Camera; |
82 | 0 | } |
83 | | |
84 | | protected: |
85 | | ~MediaEngineDefaultVideoSource(); |
86 | | |
87 | | /** |
88 | | * Called by mTimer when it's time to generate a new frame. |
89 | | */ |
90 | | void GenerateFrame(); |
91 | | |
92 | | nsCOMPtr<nsITimer> mTimer; |
93 | | |
94 | | RefPtr<layers::ImageContainer> mImageContainer; |
95 | | |
96 | | // mMutex protects mState, mImage, mStream, mTrackID |
97 | | Mutex mMutex; |
98 | | |
99 | | // Current state of this source. |
100 | | // Set under mMutex on the owning thread. Accessed under one of the two. |
101 | | MediaEngineSourceState mState = kReleased; |
102 | | RefPtr<layers::Image> mImage; |
103 | | RefPtr<SourceMediaStream> mStream; |
104 | | TrackID mTrackID = TRACK_NONE; |
105 | | |
106 | | MediaEnginePrefs mOpts; |
107 | | int mCb = 16; |
108 | | int mCr = 16; |
109 | | }; |
110 | | |
111 | | class SineWaveGenerator; |
112 | | |
113 | | class MediaEngineDefaultAudioSource : public MediaEngineSource |
114 | | { |
115 | | public: |
116 | | MediaEngineDefaultAudioSource(); |
117 | | |
118 | | nsString GetName() const override; |
119 | | nsCString GetUUID() const override; |
120 | | |
121 | | nsresult Allocate(const dom::MediaTrackConstraints &aConstraints, |
122 | | const MediaEnginePrefs &aPrefs, |
123 | | const nsString& aDeviceId, |
124 | | const ipc::PrincipalInfo& aPrincipalInfo, |
125 | | AllocationHandle** aOutHandle, |
126 | | const char** aOutBadConstraint) override; |
127 | | nsresult SetTrack(const RefPtr<const AllocationHandle>& aHandle, |
128 | | const RefPtr<SourceMediaStream>& aStream, |
129 | | TrackID aTrackID, |
130 | | const PrincipalHandle& aPrincipal) override; |
131 | | nsresult Start(const RefPtr<const AllocationHandle>& aHandle) override; |
132 | | nsresult Reconfigure(const RefPtr<AllocationHandle>& aHandle, |
133 | | const dom::MediaTrackConstraints& aConstraints, |
134 | | const MediaEnginePrefs& aPrefs, |
135 | | const nsString& aDeviceId, |
136 | | const char** aOutBadConstraint) override; |
137 | | nsresult Stop(const RefPtr<const AllocationHandle>& aHandle) override; |
138 | | nsresult Deallocate(const RefPtr<const AllocationHandle>& aHandle) override; |
139 | | void inline AppendToSegment(AudioSegment& aSegment, |
140 | | TrackTicks aSamples, |
141 | | const PrincipalHandle& aPrincipalHandle); |
142 | | void Pull(const RefPtr<const AllocationHandle>& aHandle, |
143 | | const RefPtr<SourceMediaStream>& aStream, |
144 | | TrackID aTrackID, |
145 | | StreamTime aDesiredTime, |
146 | | const PrincipalHandle& aPrincipalHandle) override; |
147 | | |
148 | | bool IsFake() const override |
149 | 0 | { |
150 | 0 | return true; |
151 | 0 | } |
152 | | |
153 | | dom::MediaSourceEnum GetMediaSource() const override |
154 | 0 | { |
155 | 0 | return dom::MediaSourceEnum::Microphone; |
156 | 0 | } |
157 | | |
158 | | uint32_t GetBestFitnessDistance( |
159 | | const nsTArray<const NormalizedConstraintSet*>& aConstraintSets, |
160 | | const nsString& aDeviceId) const override; |
161 | | |
162 | | bool IsAvailable() const; |
163 | | |
164 | | protected: |
165 | | ~MediaEngineDefaultAudioSource(); |
166 | | |
167 | | // mMutex protects mState, mStream, mTrackID |
168 | | Mutex mMutex; |
169 | | |
170 | | // Current state of this source. |
171 | | // Set under mMutex on the owning thread. Accessed under one of the two. |
172 | | MediaEngineSourceState mState = kReleased; |
173 | | RefPtr<SourceMediaStream> mStream; |
174 | | TrackID mTrackID = TRACK_NONE; |
175 | | |
176 | | // Accessed in Pull (from MSG thread) |
177 | | TrackTicks mLastNotify = 0; |
178 | | uint32_t mFreq = 1000; // ditto |
179 | | |
180 | | // Created on Start, then accessed from Pull (MSG thread) |
181 | | nsAutoPtr<SineWaveGenerator> mSineGenerator; |
182 | | }; |
183 | | |
184 | | |
185 | | class MediaEngineDefault : public MediaEngine |
186 | | { |
187 | | public: |
188 | | MediaEngineDefault() = default; |
189 | | |
190 | | void EnumerateDevices(uint64_t aWindowId, |
191 | | dom::MediaSourceEnum, |
192 | | MediaSinkEnum, |
193 | | nsTArray<RefPtr<MediaDevice>>*) override; |
194 | | void Shutdown() override; |
195 | | void ReleaseResourcesForWindow(uint64_t aWindowId) override; |
196 | | |
197 | | private: |
198 | 0 | ~MediaEngineDefault() = default; |
199 | | |
200 | | // WindowID -> Array of devices. |
201 | | nsClassHashtable<nsUint64HashKey, |
202 | | nsTArray<RefPtr<MediaEngineSource>>> mVSources; |
203 | | nsClassHashtable<nsUint64HashKey, |
204 | | nsTArray<RefPtr<MediaEngineDefaultAudioSource>>> mASources; |
205 | | }; |
206 | | |
207 | | } // namespace mozilla |
208 | | |
209 | | #endif /* NSMEDIAENGINEDEFAULT_H_ */ |