Bug 1772053 - Enable dynamic code disable mitigations only on Windows 10 1703+ r...
[gecko.git] / dom / media / DOMMediaStream.h
blobb0a9f895bbaacbd0bb2502c49787651060e0cd30
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef NSDOMMEDIASTREAM_H_
7 #define NSDOMMEDIASTREAM_H_
9 #include "ImageContainer.h"
11 #include "nsCycleCollectionParticipant.h"
12 #include "nsWrapperCache.h"
13 #include "nsIPrincipal.h"
14 #include "MediaTrackConstraints.h"
15 #include "mozilla/DOMEventTargetHelper.h"
16 #include "mozilla/RelativeTimeline.h"
17 #include "mozilla/WeakPtr.h"
19 namespace mozilla {
21 class AbstractThread;
22 class DOMMediaStream;
24 enum class BlockingMode;
26 namespace dom {
27 class HTMLCanvasElement;
28 class MediaStreamTrack;
29 class MediaStreamTrackSource;
30 class AudioStreamTrack;
31 class VideoStreamTrack;
32 } // namespace dom
34 namespace layers {
35 class ImageContainer;
36 class OverlayImage;
37 } // namespace layers
39 #define NS_DOMMEDIASTREAM_IID \
40 { \
41 0x8cb65468, 0x66c0, 0x444e, { \
42 0x89, 0x9f, 0x89, 0x1d, 0x9e, 0xd2, 0xbe, 0x7c \
43 } \
46 /**
47 * DOMMediaStream is the implementation of the js-exposed MediaStream interface.
49 * This is a thin main-thread class grouping MediaStreamTracks together.
51 class DOMMediaStream : public DOMEventTargetHelper,
52 public RelativeTimeline,
53 public SupportsWeakPtr {
54 typedef dom::MediaStreamTrack MediaStreamTrack;
55 typedef dom::AudioStreamTrack AudioStreamTrack;
56 typedef dom::VideoStreamTrack VideoStreamTrack;
57 typedef dom::MediaStreamTrackSource MediaStreamTrackSource;
59 public:
60 typedef dom::MediaTrackConstraints MediaTrackConstraints;
62 class TrackListener {
63 public:
64 virtual ~TrackListener() = default;
66 /**
67 * Called when the DOMMediaStream has a live track added, either by
68 * script (addTrack()) or the source creating one.
70 virtual void NotifyTrackAdded(const RefPtr<MediaStreamTrack>& aTrack){};
72 /**
73 * Called when the DOMMediaStream removes a live track from playback, either
74 * by script (removeTrack(), track.stop()) or the source ending it.
76 virtual void NotifyTrackRemoved(const RefPtr<MediaStreamTrack>& aTrack){};
78 /**
79 * Called when the DOMMediaStream has become active.
81 virtual void NotifyActive(){};
83 /**
84 * Called when the DOMMediaStream has become inactive.
86 virtual void NotifyInactive(){};
88 /**
89 * Called when the DOMMediaStream has become audible.
91 virtual void NotifyAudible(){};
93 /**
94 * Called when the DOMMediaStream has become inaudible.
96 virtual void NotifyInaudible(){};
99 explicit DOMMediaStream(nsPIDOMWindowInner* aWindow);
101 NS_DECL_ISUPPORTS_INHERITED
102 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DOMMediaStream, DOMEventTargetHelper)
103 NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOMMEDIASTREAM_IID)
105 virtual JSObject* WrapObject(JSContext* aCx,
106 JS::Handle<JSObject*> aGivenProto) override;
108 // WebIDL
110 static already_AddRefed<DOMMediaStream> Constructor(
111 const dom::GlobalObject& aGlobal, ErrorResult& aRv);
113 static already_AddRefed<DOMMediaStream> Constructor(
114 const dom::GlobalObject& aGlobal, const DOMMediaStream& aStream,
115 ErrorResult& aRv);
117 static already_AddRefed<DOMMediaStream> Constructor(
118 const dom::GlobalObject& aGlobal,
119 const dom::Sequence<OwningNonNull<MediaStreamTrack>>& aTracks,
120 ErrorResult& aRv);
122 static already_AddRefed<dom::Promise> CountUnderlyingStreams(
123 const dom::GlobalObject& aGlobal, ErrorResult& aRv);
125 void GetId(nsAString& aID) const;
127 void GetAudioTracks(nsTArray<RefPtr<AudioStreamTrack>>& aTracks) const;
128 void GetAudioTracks(nsTArray<RefPtr<MediaStreamTrack>>& aTracks) const;
129 void GetVideoTracks(nsTArray<RefPtr<VideoStreamTrack>>& aTracks) const;
130 void GetVideoTracks(nsTArray<RefPtr<MediaStreamTrack>>& aTracks) const;
131 void GetTracks(nsTArray<RefPtr<MediaStreamTrack>>& aTracks) const;
132 MediaStreamTrack* GetTrackById(const nsAString& aId) const;
133 void AddTrack(MediaStreamTrack& aTrack);
134 void RemoveTrack(MediaStreamTrack& aTrack);
135 already_AddRefed<DOMMediaStream> Clone();
137 bool Active() const;
139 IMPL_EVENT_HANDLER(addtrack)
140 IMPL_EVENT_HANDLER(removetrack)
142 // NON-WebIDL
144 // Returns true if this stream contains a live audio track.
145 bool Audible() const;
148 * Returns true if this DOMMediaStream has aTrack in mTracks.
150 bool HasTrack(const MediaStreamTrack& aTrack) const;
153 * Returns a principal indicating who may access this stream. The stream
154 * contents can only be accessed by principals subsuming this principal.
156 already_AddRefed<nsIPrincipal> GetPrincipal();
158 // Webrtc allows the remote side to name a stream whatever it wants, and we
159 // need to surface this to content.
160 void AssignId(const nsAString& aID) { mID = aID; }
163 * Adds a MediaStreamTrack to mTracks and raises "addtrack".
165 * Note that "addtrack" is raised synchronously and only has an effect if
166 * this MediaStream is already exposed to script. For spec compliance this is
167 * to be called from an async task.
169 void AddTrackInternal(MediaStreamTrack* aTrack);
172 * Removes a MediaStreamTrack from mTracks and fires "removetrack" if it
173 * was removed.
175 * Note that "removetrack" is raised synchronously and only has an effect if
176 * this MediaStream is already exposed to script. For spec compliance this is
177 * to be called from an async task.
179 void RemoveTrackInternal(MediaStreamTrack* aTrack);
182 * Add an nsISupports object that this stream will keep alive as long as
183 * the stream itself is alive.
185 void AddConsumerToKeepAlive(nsISupports* aConsumer) {
186 mConsumersToKeepAlive.AppendElement(aConsumer);
189 // Registers a track listener to this MediaStream, for listening to changes
190 // to our track set. The caller must call UnregisterTrackListener before
191 // being destroyed, so we don't hold on to a dead pointer. Main thread only.
192 void RegisterTrackListener(TrackListener* aListener);
194 // Unregisters a track listener from this MediaStream. The caller must call
195 // UnregisterTrackListener before being destroyed, so we don't hold on to
196 // a dead pointer. Main thread only.
197 void UnregisterTrackListener(TrackListener* aListener);
199 protected:
200 virtual ~DOMMediaStream();
202 void Destroy();
204 // Dispatches NotifyActive() to all registered track listeners.
205 void NotifyActive();
207 // Dispatches NotifyInactive() to all registered track listeners.
208 void NotifyInactive();
210 // Dispatches NotifyAudible() to all registered track listeners.
211 void NotifyAudible();
213 // Dispatches NotifyInaudible() to all registered track listeners.
214 void NotifyInaudible();
216 // Dispatches NotifyTrackAdded() to all registered track listeners.
217 void NotifyTrackAdded(const RefPtr<MediaStreamTrack>& aTrack);
219 // Dispatches NotifyTrackRemoved() to all registered track listeners.
220 void NotifyTrackRemoved(const RefPtr<MediaStreamTrack>& aTrack);
222 // Dispatches "addtrack" or "removetrack".
223 nsresult DispatchTrackEvent(const nsAString& aName,
224 const RefPtr<MediaStreamTrack>& aTrack);
226 // MediaStreamTracks contained by this DOMMediaStream.
227 nsTArray<RefPtr<MediaStreamTrack>> mTracks;
229 // Listener tracking when live MediaStreamTracks in mTracks end.
230 class PlaybackTrackListener;
231 RefPtr<PlaybackTrackListener> mPlaybackTrackListener;
233 nsString mID;
235 // Keep these alive while the stream is alive.
236 nsTArray<nsCOMPtr<nsISupports>> mConsumersToKeepAlive;
238 // The track listeners subscribe to changes in this stream's track set.
239 nsTArray<TrackListener*> mTrackListeners;
241 // True if this stream has live tracks.
242 bool mActive = false;
244 // True if this stream has live audio tracks.
245 bool mAudible = false;
248 NS_DEFINE_STATIC_IID_ACCESSOR(DOMMediaStream, NS_DOMMEDIASTREAM_IID)
250 } // namespace mozilla
252 #endif /* NSDOMMEDIASTREAM_H_ */