DOMMediaStream.h (8089B)
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/. */ 5 6 #ifndef NSDOMMEDIASTREAM_H_ 7 #define NSDOMMEDIASTREAM_H_ 8 9 #include "ImageContainer.h" 10 #include "MediaTrackConstraints.h" 11 #include "mozilla/DOMEventTargetHelper.h" 12 #include "mozilla/RelativeTimeline.h" 13 #include "mozilla/WeakPtr.h" 14 #include "nsCycleCollectionParticipant.h" 15 #include "nsIPrincipal.h" 16 #include "nsWrapperCache.h" 17 18 namespace mozilla { 19 20 class AbstractThread; 21 class DOMMediaStream; 22 23 enum class BlockingMode; 24 25 namespace dom { 26 class HTMLCanvasElement; 27 class MediaStreamTrack; 28 class MediaStreamTrackSource; 29 class AudioStreamTrack; 30 class VideoStreamTrack; 31 } // namespace dom 32 33 namespace layers { 34 class ImageContainer; 35 class OverlayImage; 36 } // namespace layers 37 38 #define NS_DOMMEDIASTREAM_IID \ 39 {0x8cb65468, 0x66c0, 0x444e, {0x89, 0x9f, 0x89, 0x1d, 0x9e, 0xd2, 0xbe, 0x7c}} 40 41 /** 42 * DOMMediaStream is the implementation of the js-exposed MediaStream interface. 43 * 44 * This is a thin main-thread class grouping MediaStreamTracks together. 45 */ 46 class DOMMediaStream : public DOMEventTargetHelper, 47 public RelativeTimeline, 48 public SupportsWeakPtr { 49 typedef dom::MediaStreamTrack MediaStreamTrack; 50 typedef dom::AudioStreamTrack AudioStreamTrack; 51 typedef dom::VideoStreamTrack VideoStreamTrack; 52 typedef dom::MediaStreamTrackSource MediaStreamTrackSource; 53 54 public: 55 typedef dom::MediaTrackConstraints MediaTrackConstraints; 56 57 class TrackListener : public nsISupports { 58 public: 59 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 60 NS_DECL_CYCLE_COLLECTION_CLASS(TrackListener) 61 62 /** 63 * Called when the DOMMediaStream has a live track added, either by 64 * script (addTrack()) or the source creating one. 65 */ 66 virtual void NotifyTrackAdded(const RefPtr<MediaStreamTrack>& aTrack) {}; 67 68 /** 69 * Called when the DOMMediaStream removes a live track from playback, either 70 * by script (removeTrack(), track.stop()) or the source ending it. 71 */ 72 virtual void NotifyTrackRemoved(const RefPtr<MediaStreamTrack>& aTrack) {}; 73 74 /** 75 * Called when the DOMMediaStream has become active. 76 */ 77 virtual void NotifyActive() {}; 78 79 /** 80 * Called when the DOMMediaStream has become inactive. 81 */ 82 virtual void NotifyInactive() {}; 83 84 /** 85 * Called when the DOMMediaStream has become audible. 86 */ 87 virtual void NotifyAudible() {}; 88 89 /** 90 * Called when the DOMMediaStream has become inaudible. 91 */ 92 virtual void NotifyInaudible() {}; 93 94 protected: 95 virtual ~TrackListener() = default; 96 }; 97 98 explicit DOMMediaStream(nsPIDOMWindowInner* aWindow); 99 100 NS_DECL_ISUPPORTS_INHERITED 101 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DOMMediaStream, DOMEventTargetHelper) 102 NS_INLINE_DECL_STATIC_IID(NS_DOMMEDIASTREAM_IID) 103 104 virtual JSObject* WrapObject(JSContext* aCx, 105 JS::Handle<JSObject*> aGivenProto) override; 106 107 // WebIDL 108 109 static already_AddRefed<DOMMediaStream> Constructor( 110 const dom::GlobalObject& aGlobal, ErrorResult& aRv); 111 112 static already_AddRefed<DOMMediaStream> Constructor( 113 const dom::GlobalObject& aGlobal, const DOMMediaStream& aStream, 114 ErrorResult& aRv); 115 116 static already_AddRefed<DOMMediaStream> Constructor( 117 const dom::GlobalObject& aGlobal, 118 const dom::Sequence<OwningNonNull<MediaStreamTrack>>& aTracks, 119 ErrorResult& aRv); 120 121 static already_AddRefed<dom::Promise> CountUnderlyingStreams( 122 const dom::GlobalObject& aGlobal, ErrorResult& aRv); 123 124 void GetId(nsAString& aID) const; 125 126 void GetAudioTracks(nsTArray<RefPtr<AudioStreamTrack>>& aTracks) const; 127 void GetAudioTracks(nsTArray<RefPtr<MediaStreamTrack>>& aTracks) const; 128 void GetVideoTracks(nsTArray<RefPtr<VideoStreamTrack>>& aTracks) const; 129 void GetVideoTracks(nsTArray<RefPtr<MediaStreamTrack>>& aTracks) const; 130 void GetTracks(nsTArray<RefPtr<MediaStreamTrack>>& aTracks) const; 131 MediaStreamTrack* GetTrackById(const nsAString& aId) const; 132 void AddTrack(MediaStreamTrack& aTrack); 133 void RemoveTrack(MediaStreamTrack& aTrack); 134 already_AddRefed<DOMMediaStream> Clone(); 135 136 bool Active() const; 137 138 IMPL_EVENT_HANDLER(addtrack) 139 IMPL_EVENT_HANDLER(removetrack) 140 141 // NON-WebIDL 142 143 // Returns true if this stream contains a live audio track. 144 bool Audible() const; 145 146 /** 147 * Returns true if this DOMMediaStream has aTrack in mTracks. 148 */ 149 bool HasTrack(const MediaStreamTrack& aTrack) const; 150 151 /** 152 * Returns a principal indicating who may access this stream. The stream 153 * contents can only be accessed by principals subsuming this principal. 154 */ 155 already_AddRefed<nsIPrincipal> GetPrincipal(); 156 157 // Webrtc allows the remote side to name a stream whatever it wants, and we 158 // need to surface this to content. 159 void AssignId(const nsAString& aID) { mID = aID; } 160 161 /** 162 * Adds a MediaStreamTrack to mTracks and raises "addtrack". 163 * 164 * Note that "addtrack" is raised synchronously and only has an effect if 165 * this MediaStream is already exposed to script. For spec compliance this is 166 * to be called from an async task. 167 */ 168 void AddTrackInternal(MediaStreamTrack* aTrack); 169 170 /** 171 * Removes a MediaStreamTrack from mTracks and fires "removetrack" if it 172 * was removed. 173 * 174 * Note that "removetrack" is raised synchronously and only has an effect if 175 * this MediaStream is already exposed to script. For spec compliance this is 176 * to be called from an async task. 177 */ 178 void RemoveTrackInternal(MediaStreamTrack* aTrack); 179 180 /** 181 * Add an nsISupports object that this stream will keep alive as long as 182 * the stream itself is alive. 183 */ 184 void AddConsumerToKeepAlive(nsISupports* aConsumer) { 185 mConsumersToKeepAlive.AppendElement(aConsumer); 186 } 187 188 // Registers a track listener to this MediaStream, for listening to changes 189 // to our track set. The caller must call UnregisterTrackListener before 190 // being destroyed, so we don't hold on to a dead pointer. Main thread only. 191 void RegisterTrackListener(TrackListener* aListener); 192 193 // Unregisters a track listener from this MediaStream. The caller must call 194 // UnregisterTrackListener before being destroyed, so we don't hold on to 195 // a dead pointer. Main thread only. 196 void UnregisterTrackListener(TrackListener* aListener); 197 198 protected: 199 virtual ~DOMMediaStream(); 200 201 void Destroy(); 202 203 // Dispatches NotifyActive() to all registered track listeners. 204 void NotifyActive(); 205 206 // Dispatches NotifyInactive() to all registered track listeners. 207 void NotifyInactive(); 208 209 // Dispatches NotifyAudible() to all registered track listeners. 210 void NotifyAudible(); 211 212 // Dispatches NotifyInaudible() to all registered track listeners. 213 void NotifyInaudible(); 214 215 // Dispatches NotifyTrackAdded() to all registered track listeners. 216 void NotifyTrackAdded(const RefPtr<MediaStreamTrack>& aTrack); 217 218 // Dispatches NotifyTrackRemoved() to all registered track listeners. 219 void NotifyTrackRemoved(const RefPtr<MediaStreamTrack>& aTrack); 220 221 // Dispatches "addtrack" or "removetrack". 222 nsresult DispatchTrackEvent(const nsAString& aName, 223 const RefPtr<MediaStreamTrack>& aTrack); 224 225 // MediaStreamTracks contained by this DOMMediaStream. 226 nsTArray<RefPtr<MediaStreamTrack>> mTracks; 227 228 // Listener tracking when live MediaStreamTracks in mTracks end. 229 class PlaybackTrackListener; 230 RefPtr<PlaybackTrackListener> mPlaybackTrackListener; 231 232 nsString mID; 233 234 // Keep these alive while the stream is alive. 235 nsTArray<nsCOMPtr<nsISupports>> mConsumersToKeepAlive; 236 237 // The track listeners subscribe to changes in this stream's track set. 238 nsTArray<RefPtr<TrackListener>> mTrackListeners; 239 240 // True if this stream has live tracks. 241 bool mActive = false; 242 243 // True if this stream has live audio tracks. 244 bool mAudible = false; 245 }; 246 247 } // namespace mozilla 248 249 #endif /* NSDOMMEDIASTREAM_H_ */