Skip to content

Truly independent web browser

2.9 KB
Raw Blame
<!DOCTYPE html>
<video id="video"></video>
<script src="../include.js"></script>
<script>
    asyncTest(done => {
        function fail(name, error) {
            println(`FAIL: ${name}: ${error}`);
            done();
        }

        const video = document.getElementById("video");
        const mediaSource = new MediaSource();

        function trySetDuration(newDuration) {
            try {
                mediaSource.duration = newDuration;
            } catch (e) {
                println(`set duration to ${newDuration}: threw ${e.name}, duration is ${mediaSource.duration}`);
                return;
            }
            println(`set duration to ${newDuration}: duration is ${mediaSource.duration}`);
        }

        try {
            const mimeType = 'video/webm;codecs="vp9"';
            video.src = URL.createObjectURL(mediaSource);

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

                    // The last frame of this file starts at 1.983 and ends at 1.999666666, so a duration
                    // below its start must be refused, and one within it is raised to its end.
                    const response = await fetch("../../../Assets/vp9-frame-rate-change.webm");
                    if (!response.ok) {
                        fail("fetch media", `HTTP ${response.status}`);
                        return;
                    }
                    const data = new Uint8Array(await response.arrayBuffer());

                    await new Promise((resolve, reject) => {
                        sourceBuffer.addEventListener("updateend", resolve, { once: true });
                        sourceBuffer.addEventListener("error", () =>
                            reject(new Error("error event on SourceBuffer")), { once: true });
                        sourceBuffer.appendBuffer(data);
                    });

                    const buffered = sourceBuffer.buffered;
                    println(`buffered: [${buffered.start(0)}-${buffered.end(0)}]`);
                    println(`duration after append: ${mediaSource.duration}`);

                    // Truncating below the buffered frames is refused.
                    trySetDuration(1);

                    // A duration inside the last frame is raised to the end of the buffered ranges.
                    trySetDuration(1.99);

                    // Extending the duration past the buffered ranges is allowed.
                    trySetDuration(2.5);

                    // Truncating back to the buffered end is allowed.
                    trySetDuration(1.999666666);

                    done();
                } catch (e) {
                    fail("sourceopen handler", e);
                }
            });
        } catch (e) {
            fail("setup", e);
        }
    });
</script>