summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/tests/mochitests/test_getUserMedia_mediaStreamConstructors.html
blob: 4ea6e3f444de2f8415e82ffb10731b2a0c95254f (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
<!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: "MediaStream constructors with getUserMedia streams Test",
    bug: "1070216"
  });

  var audioContext = new AudioContext();
  var videoElement;

  runTest(() => Promise.resolve()
    .then(() => videoElement = createMediaElement('video', 'constructorsTest'))
    .then(() => getUserMedia({video: true})).then(gUMStream => {
      info("Test default constructor with video");
      ok(gUMStream.active, "gUMStream with one track should be active");
      var track = gUMStream.getTracks()[0];

      var stream = new MediaStream();
      ok(!stream.active, "New MediaStream should be inactive");
      checkMediaStreamContains(stream, [], "Default constructed stream");

      stream.addTrack(track);
      ok(stream.active, "MediaStream should be active after adding a track");
      checkMediaStreamContains(stream, [track], "Added video track");

      var playback = new MediaStreamPlayback(videoElement, stream);
      return playback.playMedia(false).then(() => {
        ok(!gUMStream.active, "gUMStream should be inactive after stopping");
        ok(!stream.active, "stream with stopped tracks should be inactive");
      });
    })
    .then(() => getUserMedia({video: true})).then(gUMStream => {
      info("Test copy constructor with gUM stream");
      ok(gUMStream.active, "gUMStream with one track should be active");
      var track = gUMStream.getTracks()[0];

      var stream = new MediaStream(gUMStream);
      ok(stream.active, "List constructed MediaStream should be active");
      checkMediaStreamContains(stream, [track], "Copy constructed video track");

      var playback = new MediaStreamPlayback(videoElement, stream);
      return playback.playMedia(false).then(() => {
        ok(!gUMStream.active, "gUMStream should be inactive after stopping");
        ok(!stream.active, "stream with stopped tracks should be inactive");
      });
    })
    .then(() => getUserMedia({video: true})).then(gUMStream => {
      info("Test list constructor with empty list");
      ok(gUMStream.active, "gUMStream with one track should be active");
      var track = gUMStream.getTracks()[0];

      var stream = new MediaStream([]);
      ok(!stream.active, "Empty-list constructed MediaStream should be inactive");
      checkMediaStreamContains(stream, [], "Empty-list constructed stream");

      stream.addTrack(track);
      ok(stream.active, "MediaStream should be active after adding a track");
      checkMediaStreamContains(stream, [track], "Added video track");

      var playback = new MediaStreamPlayback(videoElement, stream);
      return playback.playMedia(false).then(() => {
        ok(!gUMStream.active, "gUMStream should be inactive after stopping");
        ok(!stream.active, "stream with stopped tracks should be inactive");
      });
    })
    .then(() => getUserMedia({audio: true, video: true})).then(gUMStream => {
      info("Test list constructor with a gUM audio/video stream");
      ok(gUMStream.active, "gUMStream with two tracks should be active");
      var audioTrack = gUMStream.getAudioTracks()[0];
      var videoTrack = gUMStream.getVideoTracks()[0];

      var stream = new MediaStream([audioTrack, videoTrack]);
      ok(stream.active, "List constructed MediaStream should be active");
      checkMediaStreamContains(stream, [audioTrack, videoTrack],
                               "List constructed audio and video tracks");

      var playback = new MediaStreamPlayback(videoElement, stream);
      return playback.playMedia(false).then(() => {
        ok(!gUMStream.active, "gUMStream should be inactive after stopping");
        ok(!stream.active, "stream with stopped tracks should be inactive");
      });
    })
    .then(() => getUserMedia({video: true})).then(gUMStream => {
      info("Test list constructor with gUM-video and WebAudio tracks");
      ok(gUMStream.active, "gUMStream with one track should be active");
      var audioStream = createOscillatorStream(audioContext, 2000);
      ok(audioStream.active, "WebAudio stream should be active");

      var audioTrack = audioStream.getTracks()[0];
      var videoTrack = gUMStream.getTracks()[0];

      var stream = new MediaStream([audioTrack, videoTrack]);
      ok(stream.active, "List constructed MediaStream should be active");
      checkMediaStreamContains(stream, [audioTrack, videoTrack],
                               "List constructed WebAudio and gUM-video tracks");

      var playback = new MediaStreamPlayback(videoElement, stream);
      return playback.playMedia(false).then(() => {
        gUMStream.getTracks().forEach(t => t.stop());
        ok(!gUMStream.active, "gUMStream should be inactive after stopping");
        ok(!stream.active, "stream with stopped tracks should be inactive");
      });
    })
    .then(() => {
      var osc1k = createOscillatorStream(audioContext, 1000);
      var audioTrack1k = osc1k.getTracks()[0];

      var osc5k = createOscillatorStream(audioContext, 5000);
      var audioTrack5k = osc5k.getTracks()[0];

      var osc10k = createOscillatorStream(audioContext, 10000);
      var audioTrack10k = osc10k.getTracks()[0];

      return Promise.resolve().then(() => {
        info("Analysing audio output with empty default constructed stream");
        var stream = new MediaStream();
        var analyser = new AudioStreamAnalyser(audioContext, stream);
        return analyser.waitForAnalysisSuccess(array =>
          array[analyser.binIndexForFrequency(1000)]  < 50 &&
          array[analyser.binIndexForFrequency(5000)]  < 50 &&
          array[analyser.binIndexForFrequency(10000)] < 50)
          .then(() => analyser.disconnect());
      }).then(() => {
        info("Analysing audio output with copy constructed 5k stream");
        var stream = new MediaStream(osc5k);
        is(stream.active, osc5k.active,
           "Copy constructed MediaStream should preserve active state");
        var analyser = new AudioStreamAnalyser(audioContext, stream);
        return analyser.waitForAnalysisSuccess(array =>
          array[analyser.binIndexForFrequency(1000)]  < 50 &&
          array[analyser.binIndexForFrequency(5000)]  > 200 &&
          array[analyser.binIndexForFrequency(10000)] < 50)
          .then(() => analyser.disconnect());
      }).then(() => {
        info("Analysing audio output with empty-list constructed stream");
        var stream = new MediaStream([]);
        var analyser = new AudioStreamAnalyser(audioContext, stream);
        return analyser.waitForAnalysisSuccess(array =>
          array[analyser.binIndexForFrequency(1000)]  < 50 &&
          array[analyser.binIndexForFrequency(5000)]  < 50 &&
          array[analyser.binIndexForFrequency(10000)] < 50)
          .then(() => analyser.disconnect());
      }).then(() => {
        info("Analysing audio output with list constructed 1k, 5k and 10k tracks");
        var stream = new MediaStream([audioTrack1k, audioTrack5k, audioTrack10k]);
        ok(stream.active,
           "List constructed MediaStream from WebAudio should be active");
        var analyser = new AudioStreamAnalyser(audioContext, stream);
        return analyser.waitForAnalysisSuccess(array =>
          array[analyser.binIndexForFrequency(50)]    < 50 &&
          array[analyser.binIndexForFrequency(1000)]  > 200 &&
          array[analyser.binIndexForFrequency(2500)]  < 50 &&
          array[analyser.binIndexForFrequency(5000)]  > 200 &&
          array[analyser.binIndexForFrequency(7500)]  < 50 &&
          array[analyser.binIndexForFrequency(10000)] > 200 &&
          array[analyser.binIndexForFrequency(11000)] < 50)
          .then(() => analyser.disconnect());
      });
    }));
</script>
</pre>
</body>
</html>