<!doctype html><!-- webkit-test-runner [ UseGPUProcessForDisplayCapture=true ] -->
<html>
    <head>
        <meta charset="utf-8">
        <title>getDisplayMedia and frame rate</title>
        <script src="../../resources/testharness.js"></script>
        <script src="../../resources/testharnessreport.js"></script>
        <script src="resources/getDisplayMedia-utils.js"></script>
    </head>
    <body>
        <video id=video1 autoplay playsInline width=100px></video>
        <video id=video2 autoplay playsInline width=100px></video>
        <script>
promise_test(async () => {
    if (!window.internals)
        return;

    const stream = await callGetDisplayMedia({ video: true });
    internals.observeMediaStreamTrack(stream.getVideoTracks()[0]);
    let currentCount = internals.trackVideoSampleCount;
    while (currentCount === internals.trackVideoSampleCount)
        await new Promise(resolve => setTimeout(resolve, 50));
}, "Ensure getDisplayMedia generate frames");

promise_test(async () => {
    if (!window.internals)
        return;

    const stream = await callGetDisplayMedia({ video: { frameRate : 30 } });
    assert_equals(stream.getVideoTracks()[0].getSettings().frameRate, 30, "before cloning");
    assert_equals(stream.getVideoTracks()[0].getSettings().width, 1920, "before cloning");
    assert_equals(stream.getVideoTracks()[0].getSettings().height, 1080, "before cloning");

    const stream2 = stream.clone();
    await stream2.getVideoTracks()[0].applyConstraints({ width:320, height:240, frameRate : 1 });
    assert_equals(stream2.getVideoTracks()[0].getSettings().frameRate, 1);

    video1.srcObject = stream;
    await video1.play();
    assert_equals(video1.videoWidth, 1920, "after cloning");
    assert_equals(video1.videoHeight, 1080, "after cloning");

    video2.srcObject = stream2;
    await video2.play();
    assert_equals(video2.videoWidth, 320);
    assert_equals(video2.videoHeight, 320 * 1080 / 1920);

    internals.observeMediaStreamTrack(stream.getVideoTracks()[0]);
    let currentCount = internals.trackVideoSampleCount;
    while (!currentCount) {
        await new Promise(resolve => setTimeout(resolve, 50));
        currentCount = internals.trackVideoSampleCount;
    }

    await new Promise(resolve => setTimeout(resolve, 500));
    assert_true(internals.trackVideoSampleCount - currentCount > 1);
}, "Ensure getDisplayMedia generate frames with valid frame rate and size in case of clones");
        </script>
    </body>
</html>
