4 <script type=
"application/javascript" src=
"mediaStreamPlayback.js"></script>
8 <script type=
"application/javascript">
12 title:
"MediaStream's addTrack() and removeTrack() with getUserMedia streams Test",
16 runTest(() =
> Promise.resolve()
17 .then(() =
> getUserMedia({audio: true})).then(stream =
>
18 getUserMedia({video: true}).then(otherStream =
> {
19 info(
"Test addTrack()ing a video track to an audio-only gUM stream");
20 var track = stream.getTracks()[
0];
21 var otherTrack = otherStream.getTracks()[
0];
23 stream.addTrack(track);
24 checkMediaStreamContains(stream, [track],
"Re-added audio");
26 stream.addTrack(otherTrack);
27 checkMediaStreamContains(stream, [track, otherTrack],
"Added video");
29 var testElem = createMediaElement('video', 'testAddTrackAudioVideo');
30 var playback = new LocalMediaStreamPlayback(testElem, stream);
31 return playback.playMediaWithMediaStreamTracksStop(false);
33 .then(() =
> getUserMedia({video: true})).then(stream =
>
34 getUserMedia({video: true}).then(otherStream =
> {
35 info(
"Test addTrack()ing a video track to a video-only gUM stream");
36 var track = stream.getTracks()[
0];
37 var otherTrack = otherStream.getTracks()[
0];
39 stream.addTrack(track);
40 checkMediaStreamContains(stream, [track],
"Re-added video");
42 stream.addTrack(otherTrack);
43 checkMediaStreamContains(stream, [track, otherTrack],
"Added video");
45 var test = createMediaElement('video', 'testAddTrackDoubleVideo');
46 var playback = new LocalMediaStreamPlayback(test, stream);
47 return playback.playMediaWithMediaStreamTracksStop(false);
49 .then(() =
> getUserMedia({video: true})).then(stream =
>
50 getUserMedia({video: true}).then(otherStream =
> {
51 info(
"Test removeTrack() existing and added video tracks from a video-only gUM stream");
52 var track = stream.getTracks()[
0];
53 var otherTrack = otherStream.getTracks()[
0];
55 stream.removeTrack(otherTrack);
56 checkMediaStreamContains(stream, [track],
"Removed non-existing video");
58 stream.addTrack(otherTrack);
59 checkMediaStreamContains(stream, [track, otherTrack],
"Added video");
61 stream.removeTrack(otherTrack);
62 checkMediaStreamContains(stream, [track],
"Removed added video");
64 stream.removeTrack(otherTrack);
65 checkMediaStreamContains(stream, [track],
"Re-removed added video");
67 stream.removeTrack(track);
68 checkMediaStreamContains(stream, [],
"Removed original video");
70 var elem = createMediaElement('video', 'testRemoveAllVideo');
71 var loadeddata = false;
72 elem.onloadeddata = () =
> { loadeddata = true; elem.onloadeddata = null; };
73 elem.srcObject = stream;
75 return wait(
500).then(() =
> {
76 ok(!loadeddata,
"Stream without tracks shall not raise 'loadeddata' on media element");
78 elem.srcObject = null;
81 stream.addTrack(track);
82 checkMediaStreamContains(stream, [track],
"Re-added added-then-removed track");
83 var playback = new LocalMediaStreamPlayback(elem, stream);
84 return playback.playMediaWithMediaStreamTracksStop(false);
86 .then(() =
> otherTrack.stop());
88 .then(() =
> getUserMedia({ audio: true })).then(audioStream =
>
89 getUserMedia({ video: true }).then(videoStream =
> {
90 info(
"Test adding track and removing the original");
91 var audioTrack = audioStream.getTracks()[
0];
92 var videoTrack = videoStream.getTracks()[
0];
93 videoStream.removeTrack(videoTrack);
94 audioStream.addTrack(videoTrack);
96 checkMediaStreamContains(videoStream, [],
"1, Removed original track");
97 checkMediaStreamContains(audioStream, [audioTrack, videoTrack],
98 "2, Added external track");
100 var elem = createMediaElement('video', 'testAddRemoveOriginalTrackVideo');
101 var playback = new LocalMediaStreamPlayback(elem, audioStream);
102 return playback.playMediaWithMediaStreamTracksStop(false);
105 var ac = new AudioContext();
107 var osc1k = createOscillatorStream(ac,
1000);
108 var audioTrack1k = osc1k.getTracks()[
0];
110 var osc5k = createOscillatorStream(ac,
5000);
111 var audioTrack5k = osc5k.getTracks()[
0];
113 var osc10k = createOscillatorStream(ac,
10000);
114 var audioTrack10k = osc10k.getTracks()[
0];
117 return Promise.resolve().then(() =
> {
118 info(
"Analysing audio output with original 1k track");
119 var analyser = new AudioStreamAnalyser(ac, stream);
120 return analyser.waitForAnalysisSuccess(array =
>
121 array[analyser.binIndexForFrequency(
50)] <
50 &&
122 array[analyser.binIndexForFrequency(
1000)]
> 200 &&
123 array[analyser.binIndexForFrequency(
5000)] <
50 &&
124 array[analyser.binIndexForFrequency(
10000)] <
50);
126 info(
"Analysing audio output with removed original 1k track and added 5k track");
127 stream.removeTrack(audioTrack1k);
128 stream.addTrack(audioTrack5k);
129 var analyser = new AudioStreamAnalyser(ac, stream);
130 return analyser.waitForAnalysisSuccess(array =
>
131 array[analyser.binIndexForFrequency(
1000)] <
50 &&
132 array[analyser.binIndexForFrequency(
5000)]
> 200 &&
133 array[analyser.binIndexForFrequency(
10000)] <
50);
135 info(
"Analysing audio output with removed 5k track and added 10k track");
136 stream.removeTrack(audioTrack5k);
137 stream.addTrack(audioTrack10k);
138 var analyser = new AudioStreamAnalyser(ac, stream);
139 return analyser.waitForAnalysisSuccess(array =
>
140 array[analyser.binIndexForFrequency(
1000)] <
50 &&
141 array[analyser.binIndexForFrequency(
5000)] <
50 &&
142 array[analyser.binIndexForFrequency(
10000)]
> 200);
144 info(
"Analysing audio output with re-added 1k, 5k and added 10k tracks");
145 stream.addTrack(audioTrack1k);
146 stream.addTrack(audioTrack5k);
147 var analyser = new AudioStreamAnalyser(ac, stream);
148 return analyser.waitForAnalysisSuccess(array =
>
149 array[analyser.binIndexForFrequency(
50)] <
50 &&
150 array[analyser.binIndexForFrequency(
1000)]
> 200 &&
151 array[analyser.binIndexForFrequency(
2500)] <
50 &&
152 array[analyser.binIndexForFrequency(
5000)]
> 200 &&
153 array[analyser.binIndexForFrequency(
7500)] <
50 &&
154 array[analyser.binIndexForFrequency(
10000)]
> 200 &&
155 array[analyser.binIndexForFrequency(
11000)] <
50);