summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/media-source/mediasource-seek-beyond-duration.html
blob: 8b07c9f80172c14bcda78e3bf5ba9a7a4be3006c (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
<!DOCTYPE html>
<!-- Copyright © 2016 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
<html>
    <head>
        <title>Test MediaSource behavior when seeking beyond the duration of the clip.</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 seekToSpecifiedTimeSetEOSAndVerifyDone(test, mediaElement, mediaSource, seekToTime)
            {
                assert_less_than(mediaElement.currentTime, mediaElement.duration, 'Not at the end yet.');
                test.expectEvent(mediaElement, 'seeking', 'mediaElement seeking');
                // Seek to specified time.
                mediaElement.currentTime = seekToTime;
                if (seekToTime >= mediaSource.duration) {
                    assert_equals(mediaElement.currentTime, mediaSource.duration, 'Current time equals duration.');
                } else {
                    assert_equals(mediaElement.currentTime, seekToTime, 'Current time equals specified seek time.');
                }

                test.waitForExpectedEvents(function()
                {
                    test.expectEvent(mediaElement, 'timeupdate', 'mediaElement time updated.');
                    test.expectEvent(mediaElement, 'seeked', 'mediaElement seeked');
                    test.expectEvent(mediaElement, 'ended', 'mediaElement ended.');
                    test.expectEvent(mediaSource, 'sourceended', 'mediaSource ended.');
                    mediaSource.endOfStream();
                    assert_true(mediaElement.seeking, 'mediaElement seeking.');
                });

                test.waitForExpectedEvents(function()
                {
                    assert_equals(mediaElement.currentTime, mediaSource.duration, 'Current time equals duration.');
                    test.done();
                });
            };

            mediasource_testafterdataloaded(function(test, mediaElement, mediaSource, segmentInfo, sourceBuffer, mediaData)
            {
                mediaElement.play();
                var initSegment = MediaSourceUtil.extractSegmentData(mediaData, segmentInfo.init);

                // Append the initialization segment to trigger a transition to HAVE_METADATA.
                test.expectEvent(sourceBuffer, 'updateend', 'sourceBuffer end update.');
                test.expectEvent(mediaElement, 'loadedmetadata', 'Reached HAVE_METADATA');
                sourceBuffer.appendBuffer(initSegment);

                test.waitForExpectedEvents(function()
                {
                    // Add sufficient segments to have at least 2s of play-time.
                    var playbackData = MediaSourceUtil.getMediaDataForPlaybackTime(mediaData, segmentInfo, 2.0);
                    test.expectEvent(sourceBuffer, 'updateend', 'sourceBuffer');
                    test.expectEvent(mediaElement, 'playing', 'Playing media.');
                    sourceBuffer.appendBuffer(playbackData);
                });

                test.waitForExpectedEvents(function()
                {
                    assert_equals(mediaElement.duration, segmentInfo.duration);
                    assert_greater_than_equal(mediaElement.duration, 2.0, 'Duration is >2.0s.');

                    test.expectEvent(sourceBuffer, "updateend");
                    sourceBuffer.remove(1.5, Infinity);
                    assert_true(sourceBuffer.updating, "updating");
                });

                test.waitForExpectedEvents(function()
                {
                    assert_false(sourceBuffer.updating, "updating");
                    test.waitForCurrentTimeChange(mediaElement, function()
                    {
                        // Update duration.
                        mediaSource.duration = 1.5;
                        seekToSpecifiedTimeSetEOSAndVerifyDone(test, mediaElement, mediaSource, 1.8);
                    });
                });
            }, 'Test seeking beyond updated media duration.');

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

                // Append all media data for complete playback.
                test.expectEvent(sourceBuffer, 'updateend', 'sourceBuffer end update.');
                test.expectEvent(mediaElement, 'loadedmetadata', 'Reached HAVE_METADATA');
                test.expectEvent(mediaElement, 'playing', 'Playing media.');
                sourceBuffer.appendBuffer(mediaData);

                test.waitForExpectedEvents(function()
                {
                    test.waitForCurrentTimeChange(mediaElement, function()
                    {
                        seekToSpecifiedTimeSetEOSAndVerifyDone(test, mediaElement, mediaSource, mediaSource.duration, mediaSource.duration + 0.1);
                    });
                });

            }, 'Test seeking beyond media duration.');
        </script>
    </body>
</html>