Skip to content

Truly independent web browser

3.5 KB
Raw Blame
<!DOCTYPE html>
<!-- Allow autoplay so the scripted play() works without a user gesture, independent of the browser's
     autoplay policy that test-web would otherwise inherit. -->
<script>internals.setAutoplayPolicy("allow-audio-and-video");</script>
<video id="video"></video>
<script src="../include.js"></script>
<script>
    asyncTest(done => {
        function fail(name, error) {
            println(`FAIL: ${name}: ${error}`);
            done();
        }

        try {
            const video = document.getElementById("video");
            const mimeType = 'video/webm;codecs="vp9,opus"';
            const mediaSource = new MediaSource();
            video.src = URL.createObjectURL(mediaSource);

            // The test WebM file has:
            //   Init segment: bytes 0-529
            //   Cluster 1 (0ms): bytes 530-946884
            //   Cluster 2 (4981ms): bytes 946885-1605920
            const CLUSTER2_START = 946885;

            mediaSource.addEventListener("sourceopen", async () => {
                try {
                    const sourceBuffer = mediaSource.addSourceBuffer(mimeType);

                    const response = await fetch("../../../Assets/test-webm.webm");
                    if (!response.ok) {
                        fail("fetch", `HTTP ${response.status}`);
                        return;
                    }
                    const data = await response.arrayBuffer();

                    // Append only the first cluster, and never signal endOfStream(), so that playback runs out of
                    // data at the end of it.
                    await new Promise((resolve, reject) => {
                        sourceBuffer.addEventListener("updateend", resolve, { once: true });
                        sourceBuffer.addEventListener("error", () => reject(new Error("error")), { once: true });
                        sourceBuffer.appendBuffer(data.slice(0, CLUSTER2_START));
                    });
                    const bufferedEnd = sourceBuffer.buffered.end(0);

                    await new Promise(resolve => {
                        video.addEventListener("seeked", resolve, { once: true });
                        video.currentTime = bufferedEnd - 0.3;
                    });

                    // play() itself fires waiting if the seek left readyState below HAVE_FUTURE_DATA, so only start
                    // listening once playback is underway.
                    await new Promise(resolve => {
                        video.addEventListener("playing", resolve, { once: true });
                        video.play();
                    });

                    video.addEventListener("waiting", () => {
                        println(`waiting fired while playing: ${!video.paused}`);
                        println(`readyState is at most HAVE_CURRENT_DATA: ${video.readyState <= HTMLMediaElement.HAVE_CURRENT_DATA}`);

                        // Parallelism in the decoder can cause it to hold some frames, so we need to fudge the math here a bit.
                        const frameDuration = 0.04;
                        const endingSlack = frameDuration * 4;

                        println(`stopped near the end of the appended data: ${Math.abs(video.currentTime - bufferedEnd) < endingSlack}`);
                        done();
                    }, { once: true });
                } catch (e) {
                    fail("sourceopen handler", e);
                }
            });
        } catch (e) {
            fail("setup", e);
        }
    });
</script>