summaryrefslogtreecommitdiffstats
path: root/dom/media/mediasource/test/test_TimestampOffset_mp4.html
blob: 2a80e3e5cc2650c5dadc21bd0b71933fc01b5c85 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>MSE: basic functionality</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="mediasource.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">

SimpleTest.waitForExplicitFinish();

function range(start, end) {
  var rv = [];
  for (var i = start; i < end; ++i) {
    rv.push(i);
  }
  return rv;
}

var eps = 0.01;
runWithMSE(function(ms, el) {

  once(ms, 'sourceopen').then(function() {
    ok(true, "Receive a sourceopen event");
    var audiosb = ms.addSourceBuffer("audio/mp4");
    var videosb = ms.addSourceBuffer("video/mp4");
    // We divide the video into 3 chunks:
    // chunk 0: segments 1-4
    // chunk 1: segments 5-8
    // chunk 2: segments 9-13
    // We then fill the timeline so that it seamlessly plays the chunks in order 0, 2, 1.
    var vchunks = [ {start: 0, end: 3.2033}, { start: 3.2033, end: 6.4066}, { start: 6.4066, end: 10.01} ];
    var firstvoffset = vchunks[2].end - vchunks[2].start; // Duration of chunk 2
    var secondvoffset = -(vchunks[1].end - vchunks[1].start); // -(Duration of chunk 1)

    fetchAndLoad(videosb, 'bipbop/bipbop_video', ['init'], '.mp4')
    .then(fetchAndLoad.bind(null, videosb, 'bipbop/bipbop_video', range(1, 5), '.m4s'))
    .then(function() {
      is(videosb.buffered.length, 1, "No discontinuity");
      isfuzzy(videosb.buffered.start(0), vchunks[0].start, eps, "Chunk start");
      isfuzzy(videosb.buffered.end(0), vchunks[0].end, eps, "Chunk end");
      videosb.timestampOffset = firstvoffset;
      return fetchAndLoad(videosb, 'bipbop/bipbop_video', range(5, 9), '.m4s');
    })
    .then(function(data) {
      is(videosb.buffered.length, 2, "One discontinuity");
      isfuzzy(videosb.buffered.start(0), vchunks[0].start, eps, "First Chunk start");
      isfuzzy(videosb.buffered.end(0), vchunks[0].end, eps, "First chunk end");
      isfuzzy(videosb.buffered.start(1),  vchunks[1].start + firstvoffset, eps, "Second chunk start");
      isfuzzy(videosb.buffered.end(1), vchunks[1].end + firstvoffset, eps, "Second chunk end");
      videosb.timestampOffset = secondvoffset;
      return fetchAndLoad(videosb, 'bipbop/bipbop_video', range(9, 14), '.m4s');
    })
    .then(function() {
      is(videosb.buffered.length, 1, "No discontinuity (end)");
      isfuzzy(videosb.buffered.start(0), vchunks[0].start, eps, "Chunk start");
      isfuzzy(videosb.buffered.end(0), vchunks[2].end, eps, "Chunk end");
      audiosb.timestampOffset = 3;
    }).then(fetchAndLoad.bind(null, audiosb, 'bipbop/bipbop_audio', ['init'], '.mp4'))
    .then(fetchAndLoad.bind(null, audiosb, 'bipbop/bipbop_audio', range(1, 12), '.m4s'))
    .then(function() {
      is(audiosb.buffered.length, 1, "No audio discontinuity");
      isfuzzy(audiosb.buffered.start(0), 3, eps, "Audio starts at 3");

      // Trim the rest of the audio.
      audiosb.remove(videosb.buffered.end(0), Infinity);
      videosb.remove(videosb.buffered.end(0), Infinity);
      return Promise.all([audiosb.updating ? once(audiosb, 'updateend') : Promise.resolve(),
                          videosb.updating ? once(videosb, 'updateend') : Promise.resolve()]);
    }).then(function() {
      info("waiting for play to complete");
      el.play();
      el.currentTime = el.buffered.start(0);
      ms.endOfStream();
      Promise.all([once(el, 'ended'), once(el, 'seeked')]).then(SimpleTest.finish.bind(SimpleTest));
    });
  });
});

</script>
</pre>
</body>
</html>