<!DOCTYPE html>
<html>
<head>
    <title>Legacy EME + HLS</title>
    <script src="../../media-resources/video-test.js"></script>
    <script src="legacy-eme.js"></script>
    <script src="eme2016.js"></script>
    <script src="support.js"></script>
    <script>
    window.addEventListener('load', async event => {
        startTest().then(endTest).catch(failTest);
    }, {once:true});

    async function startTest() {
        findMediaElement();
        video.src = 'content/prog_index.m3u8';
        waitFor(video, 'error').then(failTest);
        var event = await waitFor(video, 'webkitneedkey');

        let keys = await startLegacyEME({
            video: video,
            protocolVersion: 1,
            mimeType: 'application/vnd.apple.mpegurl'
        });

        let buffer = await fetchBuffer('resources/cert.der');
        let certificate = new Uint8Array(buffer);
        let contentId = this.extractContentId(event.initData);
        let initDataArray = this.concatInitDataIdAndCertificate(event.initData, contentId, certificate);

        let keySession = keys.createSession('application/vnd.apple.mpegurl', initDataArray);
        waitFor(keySession, 'webkitkeyerror').then(event => {
            consoleWrite(`FAIL: update() failed with code: ${ event.code }, systemCode: ${ event.systemCode }`);
            endTest();
        });
        event = await waitFor(keySession, 'webkitkeymessage');

        let response = await getResponse(event);
        keySession.update(response);

        await waitFor(video, 'canplaythrough')
    }
    </script>
</head>
<body>
    <video controls width="480"></video>
</body>
</html>
