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 MOZILLA_TRACKUNIONSTREAM_H_
7 #define MOZILLA_TRACKUNIONSTREAM_H_
8 
9 #include "MediaStreamGraph.h"
10 #include "nsAutoPtr.h"
11 #include <algorithm>
12 
13 namespace mozilla {
14 
15 /**
16  * See MediaStreamGraph::CreateTrackUnionStream.
17  */
18 class TrackUnionStream : public ProcessedMediaStream {
19 public:
20   explicit TrackUnionStream();
21 
AsTrackUnionStream()22   virtual TrackUnionStream* AsTrackUnionStream() override { return this; }
23   friend class DOMMediaStream;
24 
25   void RemoveInput(MediaInputPort* aPort) override;
26   void ProcessInput(GraphTime aFrom, GraphTime aTo, uint32_t aFlags) override;
27 
28   void SetTrackEnabledImpl(TrackID aTrackID, DisabledTrackMode aMode) override;
29 
30   MediaStream* GetInputStreamFor(TrackID aTrackID) override;
31   TrackID GetInputTrackIDFor(TrackID aTrackID) override;
32 
33   friend class MediaStreamGraphImpl;
34 
35 protected:
36   // Only non-ended tracks are allowed to persist in this map.
37   struct TrackMapEntry {
38     // mEndOfConsumedInputTicks is the end of the input ticks that we've consumed.
39     // 0 if we haven't consumed any yet.
40     StreamTime mEndOfConsumedInputTicks;
41     // mEndOfLastInputIntervalInInputStream is the timestamp for the end of the
42     // previous interval which was unblocked for both the input and output
43     // stream, in the input stream's timeline, or -1 if there wasn't one.
44     StreamTime mEndOfLastInputIntervalInInputStream;
45     // mEndOfLastInputIntervalInOutputStream is the timestamp for the end of the
46     // previous interval which was unblocked for both the input and output
47     // stream, in the output stream's timeline, or -1 if there wasn't one.
48     StreamTime mEndOfLastInputIntervalInOutputStream;
49     MediaInputPort* mInputPort;
50     // We keep track IDs instead of track pointers because
51     // tracks can be removed without us being notified (e.g.
52     // when a finished track is forgotten.) When we need a Track*,
53     // we call StreamTracks::FindTrack, which will return null if
54     // the track has been deleted.
55     TrackID mInputTrackID;
56     TrackID mOutputTrackID;
57     nsAutoPtr<MediaSegment> mSegment;
58     // These are direct track listeners that have been added to this
59     // TrackUnionStream-track and forwarded to the input track. We will update
60     // these when this track's disabled status changes.
61     nsTArray<RefPtr<DirectMediaStreamTrackListener>> mOwnedDirectListeners;
62   };
63 
64   // Add the track to this stream, retaining its TrackID if it has never
65   // been previously used in this stream, allocating a new TrackID otherwise.
66   uint32_t AddTrack(MediaInputPort* aPort, StreamTracks::Track* aTrack,
67                     GraphTime aFrom);
68   void EndTrack(uint32_t aIndex);
69   void CopyTrackData(StreamTracks::Track* aInputTrack,
70                      uint32_t aMapIndex, GraphTime aFrom, GraphTime aTo,
71                      bool* aOutputTrackFinished);
72 
73   void AddDirectTrackListenerImpl(already_AddRefed<DirectMediaStreamTrackListener> aListener,
74                                   TrackID aTrackID) override;
75   void RemoveDirectTrackListenerImpl(DirectMediaStreamTrackListener* aListener,
76                                      TrackID aTrackID) override;
77 
78   nsTArray<TrackMapEntry> mTrackMap;
79 
80   // The next available TrackID, starting at 1 and progressing upwards.
81   // All TrackIDs in [1, mNextAvailableTrackID) have implicitly been used.
82   TrackID mNextAvailableTrackID;
83 
84   // Sorted array of used TrackIDs that require manual tracking.
85   nsTArray<TrackID> mUsedTracks;
86 
87   // Direct track listeners that have not been forwarded to their input stream
88   // yet. We'll forward these as their inputs become available.
89   nsTArray<TrackBound<DirectMediaStreamTrackListener>> mPendingDirectTrackListeners;
90 };
91 
92 } // namespace mozilla
93 
94 #endif /* MOZILLA_MEDIASTREAMGRAPH_H_ */
95