summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/media-source/mediasource-timestamp-offset.html
blob: 8381aceeb0462af18b88e72ff8acf0a5c0874995 (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
<!DOCTYPE html>
<!-- Copyright © 2016 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
<html>
    <head>
        <title>SourceBuffer.timestampOffset 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>
          function simpleTimestampOffsetTest(value, expected, description)
          {
              mediasource_test(function(test, mediaElement, mediaSource)
              {
                  var segmentInfo = MediaSourceUtil.SEGMENT_INFO;
                  var sourceBuffer = mediaSource.addSourceBuffer(segmentInfo.type);

                  assert_equals(sourceBuffer.timestampOffset, 0,
                      "Initial timestampOffset of a SourceBuffer is 0");

                  if (expected == "TypeError")  {
                      assert_throws_js(TypeError,
                          function() { sourceBuffer.timestampOffset = value; },
                          "setting timestampOffset to " + description + " throws an exception.");
                  } else {
                      sourceBuffer.timestampOffset = value;
                      assert_equals(sourceBuffer.timestampOffset, expected);
                  }

                  test.done();
              }, "Test setting SourceBuffer.timestampOffset to " + description + ".");
          }

          simpleTimestampOffsetTest(10.5, 10.5, "a positive number");
          simpleTimestampOffsetTest(-10.4, -10.4, "a negative number");
          simpleTimestampOffsetTest(0, 0, "zero");
          simpleTimestampOffsetTest(Number.POSITIVE_INFINITY, "TypeError", "positive infinity");
          simpleTimestampOffsetTest(Number.NEGATIVE_INFINITY, "TypeError", "negative infinity");
          simpleTimestampOffsetTest(Number.NaN, "TypeError", "NaN");
          simpleTimestampOffsetTest(undefined, "TypeError", "undefined");
          simpleTimestampOffsetTest(null, 0, "null");
          simpleTimestampOffsetTest(false, 0, "false");
          simpleTimestampOffsetTest(true, 1, "true");
          simpleTimestampOffsetTest("10.5", 10.5, "a number string");
          simpleTimestampOffsetTest("", 0, "an empty string");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              var initSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.init);
              var mediaSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.media[0]);

              test.expectEvent(sourceBuffer, "updateend", "initSegment append ended.");
              sourceBuffer.appendBuffer(initSegment);

              test.waitForExpectedEvents(function()
              {
                  test.expectEvent(sourceBuffer, "updateend", "mediaSegment append ended.");
                  sourceBuffer.appendBuffer(mediaSegment);
              });

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

                  assert_equals(mediaSource.readyState, "ended");

                  mediaSource.sourceBuffers[0].timestampOffset = 2;

                  assert_equals(mediaSource.readyState, "open");

                  test.expectEvent(mediaSource, "sourceopen", "mediaSource fired 'sourceopen' event.");
              });

              test.waitForExpectedEvents(function()
              {
                  test.done();
              });
          }, "Test setting timestampOffset in 'ended' state causes a transition to 'open'.");

          mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
          {
              var initSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.init);
              var mediaSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.media[0]);

              test.expectEvent(sourceBuffer, "updateend", "initSegment append ended.");
              sourceBuffer.appendBuffer(initSegment);
              assert_equals(mediaSource.sourceBuffers[0].timestampOffset, 0, "read initial value");

              test.waitForExpectedEvents(function()
              {
                  test.expectEvent(sourceBuffer, "updateend", "mediaSegment append ended.");
                  sourceBuffer.appendBuffer(mediaSegment);
                  assert_equals(mediaSource.sourceBuffers[0].timestampOffset, 0,
                      "No change to timestampoffset after segments mode init segment append");
              });

              test.waitForExpectedEvents(function()
              {
                  assert_equals(mediaSource.sourceBuffers[0].timestampOffset, 0,
                      "No change to timestampoffset after segments mode media segment append");
                  test.done();
              });
          }, "Test getting the initial value of timestampOffset.");

          mediasource_test(function(test, mediaElement, mediaSource)
          {
              var sourceBuffer = mediaSource.addSourceBuffer(MediaSourceUtil.AUDIO_VIDEO_TYPE);
              assert_true(sourceBuffer != null, "New SourceBuffer returned");

              mediaSource.removeSourceBuffer(sourceBuffer);
              assert_equals(mediaSource.sourceBuffers.length, 0, "MediaSource.sourceBuffers is empty");
              assert_equals(mediaSource.activeSourceBuffers.length, 0, "MediaSource.activesourceBuffers is empty");

              assert_throws_dom("InvalidStateError", function()
              {
                  sourceBuffer.timestampOffset = 10;
              });

              test.done();
          }, "Test setting timestampoffset after removing the sourcebuffer.");
        </script>
    </body>
</html>