summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/media-source/mediasource-remove.html
blob: 64d5a31488b45c2b96b892e5599940fd236f60dc (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<!DOCTYPE html>
<!-- Copyright © 2016 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
<html>
    <head>
        <meta  charset="utf-8">
        <title>SourceBuffer.remove() test cases.</title>
        <script src="/resources/testharness.js"></script>
        <script src="/resources/testharnessreport.js"></script>
        <script src="mediasource-util.js"></script>
    </head>
    <body>
        <div id="log"></div>
        <script>
          mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);

              mediaSource.duration = 10;

              assert_throws_js(TypeError, function()
              {
                  sourceBuffer.remove(-1, 2);
              }, "remove");

              test.done();
          }, "Test remove with an negative start.");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);

              mediaSource.duration = 10;

              [ undefined, NaN, Infinity, -Infinity ].forEach(function(item)
              {
                  assert_throws_js(TypeError, function()
                  {
                      sourceBuffer.remove(item, 2);
                  }, "remove");
              });

              test.done();
          }, "Test remove with non-finite start.");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);

              mediaSource.duration = 10;

              assert_throws_js(TypeError, function()
              {
                  sourceBuffer.remove(11, 12);
              }, "remove");

              test.done();
          }, "Test remove with a start beyond the duration.");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);

              mediaSource.duration = 10;

              assert_throws_js(TypeError, function()
              {
                  sourceBuffer.remove(2, 1);
              }, "remove");

              test.done();
          }, "Test remove with a start larger than the end.");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);

              mediaSource.duration = 10;

              assert_throws_js(TypeError, function()
              {
                  sourceBuffer.remove(0, Number.NEGATIVE_INFINITY);
              }, "remove");

              test.done();
          }, "Test remove with a NEGATIVE_INFINITY end.");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);

              mediaSource.duration = 10;

              assert_throws_js(TypeError, function()
              {
                  sourceBuffer.remove(0, Number.NaN);
              }, "remove");

              test.done();
          }, "Test remove with a NaN end.");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);

              mediaSource.duration = 10;

              mediaSource.removeSourceBuffer(sourceBuffer);

              assert_throws_dom("InvalidStateError", function()
              {
                  sourceBuffer.remove(1, 2);
              }, "remove");

              test.done();
          }, "Test remove after SourceBuffer removed from mediaSource.");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);

              assert_false(sourceBuffer.updating, "updating is false");
              assert_equals(mediaSource.duration, NaN, "duration isn't set");

              assert_throws_js(TypeError, function()
              {
                  sourceBuffer.remove(0, 0);
              }, "remove");

              test.done();
          }, "Test remove with a NaN duration.");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);

              mediaSource.duration = 10;

              test.expectEvent(sourceBuffer, "updatestart");
              test.expectEvent(sourceBuffer, "update");
              test.expectEvent(sourceBuffer, "updateend");
              sourceBuffer.remove(1, 2);

              assert_true(sourceBuffer.updating, "updating");

              assert_throws_dom("InvalidStateError", function()
              {
                  sourceBuffer.remove(3, 4);
              }, "remove");

              test.waitForExpectedEvents(function()
              {
                  test.done();
              });
          }, "Test remove while update pending.");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);

              mediaSource.duration = 10;

              test.expectEvent(sourceBuffer, "updatestart");
              test.expectEvent(sourceBuffer, "update");
              test.expectEvent(sourceBuffer, "updateend");
              sourceBuffer.remove(1, 2);

              assert_true(sourceBuffer.updating, "updating");

              assert_throws_dom("InvalidStateError", function()
              {
                  sourceBuffer.abort();
              }, "abort");

              assert_true(sourceBuffer.updating, "updating");

              test.waitForExpectedEvents(function()
              {
                  test.done();
              });
          }, "Test aborting a remove operation.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              sourceBuffer.appendBuffer(mediaData);

              test.expectEvent(sourceBuffer, "updatestart");
              test.expectEvent(sourceBuffer, "update");
              test.expectEvent(sourceBuffer, "updateend");

              test.waitForExpectedEvents(function()
              {
                  assert_less_than(mediaSource.duration, 10)

                  mediaSource.duration = 10;

                  sourceBuffer.remove(mediaSource.duration, mediaSource.duration + 2);

                  assert_true(sourceBuffer.updating, "updating");
                  test.expectEvent(sourceBuffer, "updatestart");
                  test.expectEvent(sourceBuffer, "update");
                  test.expectEvent(sourceBuffer, "updateend");
              });

              test.waitForExpectedEvents(function()
              {
                  test.done();
              });

          }, "Test remove with a start at the duration.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              sourceBuffer.appendBuffer(mediaData);

              test.expectEvent(sourceBuffer, "updatestart");
              test.expectEvent(sourceBuffer, "update");
              test.expectEvent(sourceBuffer, "updateend");

              test.waitForExpectedEvents(function()
              {
                  assert_less_than(mediaSource.duration, 10)

                  mediaSource.duration = 10;

                  sourceBuffer.remove(1, 1 + 0.9 / (1000 * 1000));

                  assert_true(sourceBuffer.updating, "updating");
                  test.expectEvent(sourceBuffer, "updatestart");
                  test.expectEvent(sourceBuffer, "update");
                  test.expectEvent(sourceBuffer, "updateend");
              });

              test.waitForExpectedEvents(function()
              {
                  test.done();
              });

          }, "Test remove with a nanosecond range.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              test.expectEvent(sourceBuffer, "updatestart");
              test.expectEvent(sourceBuffer, "update");
              test.expectEvent(sourceBuffer, "updateend");
              sourceBuffer.appendBuffer(mediaData);

              test.waitForExpectedEvents(function()
              {
                  mediaSource.endOfStream();

                  assert_equals(mediaSource.readyState, "ended");

                  test.expectEvent(sourceBuffer, "updatestart");
                  test.expectEvent(sourceBuffer, "update");
                  test.expectEvent(sourceBuffer, "updateend");
                  test.expectEvent(mediaSource, "sourceopen");
                  sourceBuffer.remove(1, 2);

                  assert_true(sourceBuffer.updating, "updating");
                  assert_equals(mediaSource.readyState, "open");
              });

              test.waitForExpectedEvents(function()
              {
                  assert_false(sourceBuffer.updating, "updating");
                  test.done();
              });
          }, "Test remove transitioning readyState from 'ended' to 'open'.");

          function removeAppendedDataTests(callback, description)
          {
              mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
              {
                  test.expectEvent(sourceBuffer, "updatestart");
                  test.expectEvent(sourceBuffer, "update");
                  test.expectEvent(sourceBuffer, "updateend");
                  sourceBuffer.appendBuffer(mediaData);

                  test.waitForExpectedEvents(function()
                  {
                      mediaSource.endOfStream();
                      assert_false(sourceBuffer.updating, "updating");

                      var start = Math.max(segmentInfo.media[0].timev, segmentInfo.media[0].timea).toFixed(3);
                      var duration = mediaElement.duration.toFixed(3);
                      var subType = MediaSourceUtil.getSubType(segmentInfo.type);

                      assertBufferedEquals(sourceBuffer, "{ [" + start + ", " + duration + ") }", "Initial buffered range.");
                      callback(test, mediaSource, sourceBuffer, duration, subType, segmentInfo);
                  });
              }, description);
          };
          function removeAndCheckBufferedRanges(test, mediaSource, sourceBuffer, start, end, expected)
          {
              test.expectEvent(sourceBuffer, "updatestart");
              test.expectEvent(sourceBuffer, "update");
              test.expectEvent(sourceBuffer, "updateend");
              sourceBuffer.remove(start, end);

              test.waitForExpectedEvents(function()
              {
                  mediaSource.endOfStream();
                  assert_false(sourceBuffer.updating, "updating");

                  assertBufferedEquals(sourceBuffer, expected, "Buffered ranges after remove().");
                  test.done();
              });
          }

          removeAppendedDataTests(function(test, mediaSource, sourceBuffer, duration, subType, segmentInfo)
          {
              removeAndCheckBufferedRanges(test, mediaSource, sourceBuffer, 0, Number.POSITIVE_INFINITY, "{ }");
          }, "Test removing all appended data.");

          removeAppendedDataTests(function(test, mediaSource, sourceBuffer, duration, subType, segmentInfo)
          {
              var expectations = {
                webm: ("{ [3.315, " + duration + ") }"),
                mp4: ("{ [3.298, " + duration + ") }"),
              };

              // Note: Range doesn't start exactly at the end of the remove range because there isn't
              // a keyframe there. The resulting range starts at the first keyframe >= the end time.
              removeAndCheckBufferedRanges(test, mediaSource, sourceBuffer, 0, 3, expectations[subType]);
          }, "Test removing beginning of appended data.");

          removeAppendedDataTests(function(test, mediaSource, sourceBuffer, duration, subType, segmentInfo)
          {
              var start = Math.max(segmentInfo.media[0].timev, segmentInfo.media[0].timea).toFixed(3);
              var expectations = {
                webm: ("{ [" + start + ", 1.005) [3.315, " + duration + ") }"),
                mp4: ("{ [" + start + ", 0.997) [3.298, " + duration + ") }"),
              };

              // Note: The first resulting range ends slightly after start because the removal algorithm only removes
              // frames with a timestamp >= the start time. If a frame starts before and ends after the remove() start
              // timestamp, then it stays in the buffer.
              removeAndCheckBufferedRanges(test, mediaSource, sourceBuffer, 1, 3, expectations[subType]);
          }, "Test removing the middle of appended data.");

          removeAppendedDataTests(function(test, mediaSource, sourceBuffer, duration, subType, segmentInfo)
          {
              var start = Math.max(segmentInfo.media[0].timev, segmentInfo.media[0].timea).toFixed(3);
              var expectations = {
                webm: "{ [" + start + ", 1.013) }",
                mp4: "{ [" + start + ", 1.022) }",
              };

              removeAndCheckBufferedRanges(test, mediaSource, sourceBuffer, 1, Number.POSITIVE_INFINITY, expectations[subType]);
          }, "Test removing the end of appended data.");
        </script>
    </body>
</html>