<!DOCTYPE html>
<html>
<head>
    <title>fps-mse-unmuxed-key-renewal</title>
    <script src=../../../media-resources/video-test.js></script>
    <script src=support.js></script>
    <script src="eme2016.js"></script>
    <script>
    window.addEventListener('load', async event => {
        startTest().then(endTest).catch(failTest);
    });

    async function startTest() {
        let video = document.querySelector('video');
        let keys = await startEME({video: video, capabilities: [{
            initDataTypes: ['sinf'],
            audioCapabilities: [{ contentType: 'audio/mp4', robustness: '' }],
            videoCapabilities: [{ contentType: 'video/mp4', robustness: '' }],
            distinctiveIdentifier: 'not-allowed',
            persistentState: 'not-allowed',
            sessionTypes: ['temporary'],
        }]});

        let mediaSource = new MediaSource;
        video.srcObject = mediaSource;
        consoleWrite('Created mediaSource');
        await waitFor(mediaSource, 'sourceopen');

        consoleWrite('-');
        consoleWrite('Appending Encrypted Video Buffer 1');

        let {sourceBuffer: sourceBuffer1, session: session1} =
            await createBufferAppendAndWaitForEncrypted(video, mediaSource, 'video/mp4', 'content/elementary-stream-video-header-keyid-2.m4v');

        consoleWrite('-');
        consoleWrite('Appending Encrypted Video Payload');

        await fetchAndAppend(sourceBuffer1, 'content/elementary-stream-video-payload.m4v');

        consoleWrite('-');
        consoleWrite('Trigger License Renewal');

        await runAndWaitForLicenseRequest(session1, () => {
            session1.update(stringToUInt8Array("renew"))
        });
    }
    </script>
</head>
<body>
    <video controls width="480"></video>
</body>
</html>
