summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webrtc-encoded-transform/script-late-transform.https.html
blob: 726852bad9c2a7793d46d78f991a922d588d51f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
<script src="/resources/testharness.js"></script>
        <script src="/resources/testharnessreport.js"></script>
        <script src=/resources/testdriver.js></script>
<script src=/resources/testdriver-vendor.js></script>
<script src='../mediacapture-streams/permission-helper.js'></script>
    </head>
    <body>
        <video controls id="video" autoplay></video>
        <canvas id="canvas" width="640" height="480"></canvas>
        <script src ="routines.js"></script>
        <script>
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 getCircleImageData()
{
    return grabFrameData(450, 100, 150, 100);
}

async function checkVideoIsUpdated(test, shouldBeUpdated, count, referenceData)
{
    if (count === undefined)
        count = 0;
    else if (count >= 20)
        return Promise.reject("checkVideoIsUpdated timed out :" + shouldBeUpdated + " " + count);

    if (referenceData === undefined)
        referenceData = getCircleImageData();

    await waitFor(test, 200);
    const newData = getCircleImageData();

    if (shouldBeUpdated === (JSON.stringify(referenceData) !== JSON.stringify(newData)))
        return;

    await checkVideoIsUpdated(test, shouldBeUpdated, ++count, newData);
}

promise_test(async (test) => {
    await setMediaPermission("granted", ["camera"]);
    const localStream = await navigator.mediaDevices.getUserMedia({video: true});
    const senderTransform = new SFrameTransform({ compatibilityMode: "H264" });
    const receiverTransform = new SFrameTransform({ compatibilityMode: "H264" });
    await crypto.subtle.importKey("raw", new Uint8Array([143, 77, 43, 10, 72, 19, 37, 67, 236, 219, 24, 93, 26, 165, 91, 178]), "HKDF", false, ["deriveBits", "deriveKey"]).then(key => {
       senderTransform.setEncryptionKey(key);
       receiverTransform.setEncryptionKey(key);
    });

    let sender, receiver;
    const stream = await new Promise((resolve, reject) => {
        createConnections(test, (firstConnection) => {
            pc1 = firstConnection;
            sender = firstConnection.addTrack(localStream.getVideoTracks()[0], localStream);
            sender.transform = senderTransform;
        }, (secondConnection) => {
            pc2 = secondConnection;
            secondConnection.ontrack = (trackEvent) => {
                receiver = trackEvent.receiver;
                // we do not set the receiver transform here;
                resolve(trackEvent.streams[0]);
            };
        }, {
            observeOffer : (offer) => {
                const lines = offer.sdp.split('\r\n');
                const h264Lines = lines.filter(line => line.indexOf("a=fmtp") === 0 && line.indexOf("42e01f") !== -1);
                const baselineNumber = h264Lines[0].substring(6).split(' ')[0];
                offer.sdp = lines.filter(line => {
                    return (line.indexOf('a=fmtp') === -1 && line.indexOf('a=rtcp-fb') === -1 && line.indexOf('a=rtpmap') === -1) || line.indexOf(baselineNumber) !== -1;
                }).join('\r\n');
            }
        });
        test.step_timeout(() => reject("Test timed out"), 5000);
    });

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

    // We set the receiver transform here so that the decoder probably tried to decode sframe content.
    test.step_timeout(() => receiver.transform = receiverTransform, 50);
    await checkVideoIsUpdated(test, true);
}, "video exchange with late receiver transform");
        </script>
    </body>
</html>