<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Testing GPU process crash</title>
        <script src="../resources/testharness.js"></script>
        <script src="../resources/testharnessreport.js"></script>
    </head>
    <body>
        <video id="video1" autoplay playsInline width="320" height="240"></video>
        <video id="video2" autoplay playsInline width="320" height="240"></video>
        <script src ="routines.js"></script>
        <script>
var receivingConnection;
promise_test((test) => {
    return navigator.mediaDevices.getUserMedia({video: true}).then((localStream) => {
        return new Promise((resolve, reject) => {
            track = localStream.getVideoTracks()[0];

            createConnections((firstConnection) => {
                firstConnection.addTrack(track, localStream);
                firstConnection.getTransceivers()[0].setCodecPreferences([{mimeType: "video/VP8", clockRate: 90000}]);
            }, (secondConnection) => {
                receivingConnection = secondConnection;
                secondConnection.ontrack = (trackEvent) => {
                    remoteTrack = trackEvent.track;
                    resolve(trackEvent.streams[0]);
                };
            });
            setTimeout(() => reject("Test timed out"), 5000);
        });
    }).then((remoteStream) => {
        video1.srcObject = remoteStream;
        return video1.play();
    });
}, "Setting video exchange with VP8");

promise_test(async () => {
    video2.srcObject = await new Promise((resolve, reject) => {
        createConnections((firstConnection) => {
            firstConnection.addTrack(video1.srcObject.getVideoTracks()[0], video1.srcObject);
        }, (secondConnection) => {
            receivingConnection = secondConnection;
            secondConnection.ontrack = (trackEvent) => {
                remoteTrack = trackEvent.track;
                resolve(trackEvent.streams[0]);
            };
        });
        setTimeout(() => reject("Test timed out"), 5000);
    });
    await video2.play();
}, "Setting exchange with H264 using VP8 decoded stream as input");

async function getInboundRTPStatsNumberOfDecodedFrames(connection)
{
    var report = await connection.getStats();
    var framesDecoded;
    report.forEach((statItem) => {
        if (statItem.type === "inbound-rtp")
            framesDecoded = statItem.framesDecoded;
    });
    return framesDecoded;
}

async function testFrameDecodedIncreased(connection, count, previousFramesNumber)
{
    if (previousFramesNumber === undefined) {
        let number = await getInboundRTPStatsNumberOfDecodedFrames(connection);
        await waitFor(1000);
        return testFrameDecodedIncreased(connection, 0, number);
    }

    var number = await getInboundRTPStatsNumberOfDecodedFrames(connection);
    if (previousFramesNumber && number > previousFramesNumber)
        return;

    if (count >= 20)
        return Promise.reject("test increasing frame decoded timed out");

    await waitFor(1000);
    return testFrameDecodedIncreased(connection, ++count, previousFramesNumber);
}


promise_test(async (test) => {
    await testFrameDecodedIncreased(receivingConnection);
    if (window.testRunner)
        testRunner.terminateGPUProcess();
    await new Promise(resolve => setTimeout(resolve, 1000));
    await testFrameDecodedIncreased(receivingConnection);
}, "Frames should be coming even after gpu process crash");
        </script>
    </body>
</html>
