summaryrefslogtreecommitdiff
path: root/dom/media/tests/mochitest/test_getUserMedia_mediaStreamTrackClone.html
blob: e5e07644271406d2acd3a4cc90e3247eecde4ff5 (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
<!DOCTYPE HTML>
<html>
<head>
  <script type="application/javascript" src="mediaStreamPlayback.js"></script>
</head>
<body>
<pre id="test">
<script type="application/javascript">
  "use strict";

  createHTML({
    title: "MediaStreamTrack.clone()",
    bug: "1208371"
  });

  var testSingleTrackClonePlayback = constraints =>
      getUserMedia(constraints).then(stream => {
    info("Test clone()ing an " + constraints + " gUM track");
    var track = stream.getTracks()[0];
    var clone = track.clone();

    checkMediaStreamTrackCloneAgainstOriginal(clone, track);

    info("Stopping original track");
    track.stop();

    info("Creating new stream for clone");
    var cloneStream = new MediaStream([clone]);
    checkMediaStreamContains(cloneStream, [clone]);

    info("Testing playback of track clone");
    var test = createMediaElement('video', 'testClonePlayback');
    var playback = new MediaStreamPlayback(test, cloneStream);
    return playback.playMedia(false);
  });

  runTest(() => Promise.resolve()
    .then(() => testSingleTrackClonePlayback({audio: true}))
    .then(() => testSingleTrackClonePlayback({video: true}))
    .then(() => getUserMedia({video: true})).then(stream => {
      info("Test cloning a track into inception");
      var track = stream.getTracks()[0];
      var clone = track;
      var clones = Array(10).fill().map(() => clone = clone.clone());
      var inceptionClone = clones.pop();
      checkMediaStreamTrackCloneAgainstOriginal(inceptionClone, track);

      var cloneStream = new MediaStream();
      cloneStream.addTrack(inceptionClone);

      // cloneStream is now essentially the same as stream.clone();
      checkMediaStreamCloneAgainstOriginal(cloneStream, stream);

      var test = createMediaElement('video', 'testClonePlayback');
      var playback = new MediaStreamPlayback(test, cloneStream);
      return playback.playMedia(false).then(() => {
          info("Testing that clones of ended tracks are ended");
          cloneStream.clone().getTracks().forEach(t =>
            is(t.readyState, "ended", "Track " + t.id + " should be ended"));
        })
        .then(() => {
          clones.forEach(t => t.stop());
          track.stop();
        });
    })
    .then(() => getUserMedia({audio: true, video: true})).then(stream => {
      info("Test adding many track clones to the original stream");

      const LOOPS = 3;
      for (var i = 0; i < LOOPS; i++) {
        stream.getTracks().forEach(t => stream.addTrack(t.clone()));
      }
      is(stream.getVideoTracks().length, Math.pow(2, LOOPS),
         "The original track should contain the original video track and all the video clones");
      stream.getTracks().forEach(t1 => is(stream.getTracks()
                                                .filter(t2 => t1.id == t2.id)
                                                .length,
                                          1, "Each track should be unique"));

      var test = createMediaElement('video', 'testClonePlayback');
      var playback = new MediaStreamPlayback(test, stream);
      return playback.playMedia(false);
    })
    .then(() => {
      info("Testing audio content routing with MediaStreamTrack.clone()");
      var ac = new AudioContext();

      var osc1kOriginal = createOscillatorStream(ac, 1000);
      var audioTrack1kOriginal = osc1kOriginal.getTracks()[0];
      var audioTrack1kClone = audioTrack1kOriginal.clone();

      var osc5kOriginal = createOscillatorStream(ac, 5000);
      var audioTrack5kOriginal = osc5kOriginal.getTracks()[0];
      var audioTrack5kClone = audioTrack5kOriginal.clone();

      return Promise.resolve().then(() => {
        info("Analysing audio output enabled and disabled tracks that don't affect each other");
        audioTrack1kOriginal.enabled = true;
        audioTrack5kOriginal.enabled = false;

        audioTrack1kClone.enabled = false;
        audioTrack5kClone.enabled = true;

        var analyser =
          new AudioStreamAnalyser(ac, new MediaStream([audioTrack1kOriginal,
                                                      audioTrack5kOriginal]));
        return analyser.waitForAnalysisSuccess(array =>
                 array[analyser.binIndexForFrequency(50)] < 50 &&
                 array[analyser.binIndexForFrequency(1000)] > 200 &&
                 array[analyser.binIndexForFrequency(3000)] < 50 &&
                 array[analyser.binIndexForFrequency(5000)] < 50)
          .then(() => analyser.disconnect())
          .then(() => {
            var cloneAnalyser =
              new AudioStreamAnalyser(ac, new MediaStream([audioTrack1kClone,
                                                           audioTrack5kClone]));
            return cloneAnalyser.waitForAnalysisSuccess(array =>
                     array[cloneAnalyser.binIndexForFrequency(1000)] < 50 &&
                     array[cloneAnalyser.binIndexForFrequency(3000)] < 50 &&
                     array[cloneAnalyser.binIndexForFrequency(5000)] > 200 &&
                     array[cloneAnalyser.binIndexForFrequency(10000)] < 50)
              .then(() => cloneAnalyser.disconnect());
          })
          // Restore original tracks
          .then(() => [audioTrack1kOriginal,
                       audioTrack5kOriginal,
                       audioTrack1kClone,
                       audioTrack5kClone].forEach(t => t.enabled = true));
      }).then(() => {
        info("Analysing audio output of 1k original and 5k clone.");
        var stream = new MediaStream();
        stream.addTrack(audioTrack1kOriginal);
        stream.addTrack(audioTrack5kClone);

        var analyser = new AudioStreamAnalyser(ac, stream);
        return analyser.waitForAnalysisSuccess(array =>
                 array[analyser.binIndexForFrequency(50)] < 50 &&
                 array[analyser.binIndexForFrequency(1000)] > 200 &&
                 array[analyser.binIndexForFrequency(3000)] < 50 &&
                 array[analyser.binIndexForFrequency(5000)] > 200 &&
                 array[analyser.binIndexForFrequency(10000)] < 50)
          .then(() => {
            info("Waiting for tracks to stop");
            stream.getTracks().forEach(t => t.stop());
            return analyser.waitForAnalysisSuccess(array =>
                     array[analyser.binIndexForFrequency(50)] < 50 &&
                     array[analyser.binIndexForFrequency(1000)] < 50 &&
                     array[analyser.binIndexForFrequency(3000)] < 50 &&
                     array[analyser.binIndexForFrequency(5000)] < 50 &&
                     array[analyser.binIndexForFrequency(10000)] < 50);
          }).then(() => analyser.disconnect());
      }).then(() => {
        info("Analysing audio output of clones of clones (1kx2 + 5kx4)");
        var stream = new MediaStream([audioTrack1kClone.clone(),
                                      audioTrack5kOriginal.clone().clone().clone().clone()]);

        var analyser = new AudioStreamAnalyser(ac, stream);
        return analyser.waitForAnalysisSuccess(array =>
                 array[analyser.binIndexForFrequency(50)] < 50 &&
                 array[analyser.binIndexForFrequency(1000)] > 200 &&
                 array[analyser.binIndexForFrequency(3000)] < 50 &&
                 array[analyser.binIndexForFrequency(5000)] > 200 &&
                 array[analyser.binIndexForFrequency(10000)] < 50)
          .then(() => analyser.disconnect());
      });
    }));
</script>
</pre>
</body>
</html>