summaryrefslogtreecommitdiffstats
path: root/dom/media/DOMMediaStream.h
blob: b0a9f895bbaacbd0bb2502c49787651060e0cd30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef NSDOMMEDIASTREAM_H_
#define NSDOMMEDIASTREAM_H_

#include "ImageContainer.h"

#include "nsCycleCollectionParticipant.h"
#include "nsWrapperCache.h"
#include "nsIPrincipal.h"
#include "MediaTrackConstraints.h"
#include "mozilla/DOMEventTargetHelper.h"
#include "mozilla/RelativeTimeline.h"
#include "mozilla/WeakPtr.h"

namespace mozilla {

class AbstractThread;
class DOMMediaStream;

enum class BlockingMode;

namespace dom {
class HTMLCanvasElement;
class MediaStreamTrack;
class MediaStreamTrackSource;
class AudioStreamTrack;
class VideoStreamTrack;
}  // namespace dom

namespace layers {
class ImageContainer;
class OverlayImage;
}  // namespace layers

#define NS_DOMMEDIASTREAM_IID                        \
  {                                                  \
    0x8cb65468, 0x66c0, 0x444e, {                    \
      0x89, 0x9f, 0x89, 0x1d, 0x9e, 0xd2, 0xbe, 0x7c \
    }                                                \
  }

/**
 * DOMMediaStream is the implementation of the js-exposed MediaStream interface.
 *
 * This is a thin main-thread class grouping MediaStreamTracks together.
 */
class DOMMediaStream : public DOMEventTargetHelper,
                       public RelativeTimeline,
                       public SupportsWeakPtr {
  typedef dom::MediaStreamTrack MediaStreamTrack;
  typedef dom::AudioStreamTrack AudioStreamTrack;
  typedef dom::VideoStreamTrack VideoStreamTrack;
  typedef dom::MediaStreamTrackSource MediaStreamTrackSource;

 public:
  typedef dom::MediaTrackConstraints MediaTrackConstraints;

  class TrackListener {
   public:
    virtual ~TrackListener() = default;

    /**
     * Called when the DOMMediaStream has a live track added, either by
     * script (addTrack()) or the source creating one.
     */
    virtual void NotifyTrackAdded(const RefPtr<MediaStreamTrack>& aTrack){};

    /**
     * Called when the DOMMediaStream removes a live track from playback, either
     * by script (removeTrack(), track.stop()) or the source ending it.
     */
    virtual void NotifyTrackRemoved(const RefPtr<MediaStreamTrack>& aTrack){};

    /**
     * Called when the DOMMediaStream has become active.
     */
    virtual void NotifyActive(){};

    /**
     * Called when the DOMMediaStream has become inactive.
     */
    virtual void NotifyInactive(){};

    /**
     * Called when the DOMMediaStream has become audible.
     */
    virtual void NotifyAudible(){};

    /**
     * Called when the DOMMediaStream has become inaudible.
     */
    virtual void NotifyInaudible(){};
  };

  explicit DOMMediaStream(nsPIDOMWindowInner* aWindow);

  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DOMMediaStream, DOMEventTargetHelper)
  NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOMMEDIASTREAM_IID)

  virtual JSObject* WrapObject(JSContext* aCx,
                               JS::Handle<JSObject*> aGivenProto) override;

  // WebIDL

  static already_AddRefed<DOMMediaStream> Constructor(
      const dom::GlobalObject& aGlobal, ErrorResult& aRv);

  static already_AddRefed<DOMMediaStream> Constructor(
      const dom::GlobalObject& aGlobal, const DOMMediaStream& aStream,
      ErrorResult& aRv);

  static already_AddRefed<DOMMediaStream> Constructor(
      const dom::GlobalObject& aGlobal,
      const dom::Sequence<OwningNonNull<MediaStreamTrack>>& aTracks,
      ErrorResult& aRv);

  static already_AddRefed<dom::Promise> CountUnderlyingStreams(
      const dom::GlobalObject& aGlobal, ErrorResult& aRv);

  void GetId(nsAString& aID) const;

  void GetAudioTracks(nsTArray<RefPtr<AudioStreamTrack>>& aTracks) const;
  void GetAudioTracks(nsTArray<RefPtr<MediaStreamTrack>>& aTracks) const;
  void GetVideoTracks(nsTArray<RefPtr<VideoStreamTrack>>& aTracks) const;
  void GetVideoTracks(nsTArray<RefPtr<MediaStreamTrack>>& aTracks) const;
  void GetTracks(nsTArray<RefPtr<MediaStreamTrack>>& aTracks) const;
  MediaStreamTrack* GetTrackById(const nsAString& aId) const;
  void AddTrack(MediaStreamTrack& aTrack);
  void RemoveTrack(MediaStreamTrack& aTrack);
  already_AddRefed<DOMMediaStream> Clone();

  bool Active() const;

  IMPL_EVENT_HANDLER(addtrack)
  IMPL_EVENT_HANDLER(removetrack)

  // NON-WebIDL

  // Returns true if this stream contains a live audio track.
  bool Audible() const;

  /**
   * Returns true if this DOMMediaStream has aTrack in mTracks.
   */
  bool HasTrack(const MediaStreamTrack& aTrack) const;

  /**
   * Returns a principal indicating who may access this stream. The stream
   * contents can only be accessed by principals subsuming this principal.
   */
  already_AddRefed<nsIPrincipal> GetPrincipal();

  // Webrtc allows the remote side to name a stream whatever it wants, and we
  // need to surface this to content.
  void AssignId(const nsAString& aID) { mID = aID; }

  /**
   * Adds a MediaStreamTrack to mTracks and raises "addtrack".
   *
   * Note that "addtrack" is raised synchronously and only has an effect if
   * this MediaStream is already exposed to script. For spec compliance this is
   * to be called from an async task.
   */
  void AddTrackInternal(MediaStreamTrack* aTrack);

  /**
   * Removes a MediaStreamTrack from mTracks and fires "removetrack" if it
   * was removed.
   *
   * Note that "removetrack" is raised synchronously and only has an effect if
   * this MediaStream is already exposed to script. For spec compliance this is
   * to be called from an async task.
   */
  void RemoveTrackInternal(MediaStreamTrack* aTrack);

  /**
   * Add an nsISupports object that this stream will keep alive as long as
   * the stream itself is alive.
   */
  void AddConsumerToKeepAlive(nsISupports* aConsumer) {
    mConsumersToKeepAlive.AppendElement(aConsumer);
  }

  // Registers a track listener to this MediaStream, for listening to changes
  // to our track set. The caller must call UnregisterTrackListener before
  // being destroyed, so we don't hold on to a dead pointer. Main thread only.
  void RegisterTrackListener(TrackListener* aListener);

  // Unregisters a track listener from this MediaStream. The caller must call
  // UnregisterTrackListener before being destroyed, so we don't hold on to
  // a dead pointer. Main thread only.
  void UnregisterTrackListener(TrackListener* aListener);

 protected:
  virtual ~DOMMediaStream();

  void Destroy();

  // Dispatches NotifyActive() to all registered track listeners.
  void NotifyActive();

  // Dispatches NotifyInactive() to all registered track listeners.
  void NotifyInactive();

  // Dispatches NotifyAudible() to all registered track listeners.
  void NotifyAudible();

  // Dispatches NotifyInaudible() to all registered track listeners.
  void NotifyInaudible();

  // Dispatches NotifyTrackAdded() to all registered track listeners.
  void NotifyTrackAdded(const RefPtr<MediaStreamTrack>& aTrack);

  // Dispatches NotifyTrackRemoved() to all registered track listeners.
  void NotifyTrackRemoved(const RefPtr<MediaStreamTrack>& aTrack);

  // Dispatches "addtrack" or "removetrack".
  nsresult DispatchTrackEvent(const nsAString& aName,
                              const RefPtr<MediaStreamTrack>& aTrack);

  // MediaStreamTracks contained by this DOMMediaStream.
  nsTArray<RefPtr<MediaStreamTrack>> mTracks;

  // Listener tracking when live MediaStreamTracks in mTracks end.
  class PlaybackTrackListener;
  RefPtr<PlaybackTrackListener> mPlaybackTrackListener;

  nsString mID;

  // Keep these alive while the stream is alive.
  nsTArray<nsCOMPtr<nsISupports>> mConsumersToKeepAlive;

  // The track listeners subscribe to changes in this stream's track set.
  nsTArray<TrackListener*> mTrackListeners;

  // True if this stream has live tracks.
  bool mActive = false;

  // True if this stream has live audio tracks.
  bool mAudible = false;
};

NS_DEFINE_STATIC_IID_ACCESSOR(DOMMediaStream, NS_DOMMEDIASTREAM_IID)

}  // namespace mozilla

#endif /* NSDOMMEDIASTREAM_H_ */