<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Testing video exchange using setDirection with renegotiation from offerer to receiver</title>
        <script src="../resources/testharness.js"></script>
        <script src="../resources/testharnessreport.js"></script>
    </head>
    <body>
        <video id="video" autoplay=""></video>
        <canvas id="canvas" width="640" height="480"></canvas>
        <script src ="routines.js"></script>
        <script>
video = document.getElementById("video");
canvas = document.getElementById("canvas");

function grabFrameData(x, y, w, h)
{
    canvas.width = video.videoWidth;
    canvas.height = video.videoHeight;

    canvas.getContext('2d').drawImage(video, x, y, w, h, x, y, w, h);
    return canvas.getContext('2d').getImageData(x, y, w, h).data;
}

function testImage(testName)
{
    const data = grabFrameData(10, 325, 250, 1);

    var index = 20;
    const test1 = data[index] < 100;
    const test2 = data[index + 1] < 100;
    const test3 = data[index + 2] < 100;

    index = 80;
    const test4 = data[index] > 200;
    const test5 = data[index + 1] > 200;
    const test6 = data[index + 2] > 200;

    index += 80;
    const test7 = data[index] > 200;
    const test8 = data[index + 1] > 200;
    const test9 = data[index + 2] < 100;

    if (!test1 || !test2 || !test3 || !test4 || !test5 || !test6 || !test7 || !test8 || !test9)
        console.log(JSON.stringify(data));

    assert_true(test1, testName + '-1');
    assert_true(test2, testName + '-2');
    assert_true(test3, testName + '-3');

    assert_true(test4, testName + '-4');
    assert_true(test5, testName + '-5');
    assert_true(test6, testName + '-6');

    assert_true(test7, testName + '-7');
    assert_true(test8, testName + '-8');
    assert_true(test9, testName + '-9');
}

var pc1, pc2;

promise_test(async (t) => {
    const localStream = await navigator.mediaDevices.getUserMedia({video: true});
    const stream = await new Promise((resolve, reject) => {
        createConnections((firstConnection) => {
            pc1 = firstConnection;
            firstConnection.addTrack(localStream.getVideoTracks()[0], localStream);
        }, (secondConnection) => {
            pc2 = secondConnection;
            secondConnection.ontrack = (trackEvent) => { resolve(trackEvent.streams[0]); };
        });
        setTimeout(() => reject("Test timed out"), 5000);
    });

    video.srcObject = stream;
    await video.play();

    testImage('test1');
    video.srcObject = null;

    let promise = new Promise((resolve) => {
        pc2.getReceivers()[0].track.onmute = resolve;
    });

    pc1.getTransceivers()[0].direction = "inactive";
    await renegotiate(pc1, pc2);
    await promise;

    promise = new Promise((resolve) => {
        pc2.getReceivers()[0].track.onunmute = resolve;
    });

    pc1.getTransceivers()[0].direction = "sendrecv";
    const streamPromise = new Promise(resolve => {
        pc2.ontrack = (trackEvent) => { resolve (trackEvent.streams[0]); };
    });

    await renegotiate(pc1, pc2);
    video.srcObject = await streamPromise;
    await promise;

    test(() => {
        assert_equals(stream, video.srcObject);
    }, "The MediaStream should remain the same");
    await video.play();

    testImage('test2');
}, "Going from sendrecv to inactive and back to sendrecv");
        </script>
    </body>
</html>
