<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Inactive/reactivate audio transceiver</title>
    <script src="../resources/testharness.js"></script>
    <script src="../resources/testharnessreport.js"></script>
</head>
<body>
    <video id="video" controls autoplay playsinline></video>
    <script src ="routines.js"></script>
    <script>
    var pc1, pc2;
    var remoteStream, remoteTrack;
    promise_test(async (test) => {
        const localStream = await navigator.mediaDevices.getUserMedia({audio: true });

        await new Promise(resolve => {
            createConnections((firstConnection) => {
                pc1 = firstConnection;
                firstConnection.addTrack(localStream.getAudioTracks()[0], localStream);
            }, (secondConnection) => {
                pc2 = secondConnection;
                secondConnection.ontrack = (trackEvent) => {
                    remoteTrack = trackEvent.track;
                    remoteStream = trackEvent.streams[0];
                    resolve();
                };
            });
        });
        video.srcObject = remoteStream;
        await video.play();
    }, "Setup audio call");

    promise_test(async (test) => {
        pc1.getTransceivers()[0].direction = "inactive";
        let offer = await pc1.createOffer();
        await pc1.setLocalDescription(offer);

        // Let's remove ssrc lines
        let sdpLines = offer.sdp.split("\r\n");
        offer.sdp = sdpLines.filter(line => line && line.length && !line.startsWith("a=ssrc")).join("\r\n") + "\r\n";

        await pc2.setRemoteDescription(offer);
        let answer = await pc2.createAnswer();
        await pc2.setLocalDescription(answer);
        await pc1.setRemoteDescription(answer);

        assert_equals(remoteTrack.readyState, "live");
    }, "Inactivate the audio transceiver");

    promise_test(async (test) => {
        pc1.getTransceivers()[0].direction = "sendonly";
        let offer = await pc1.createOffer();
        await pc1.setLocalDescription(offer);
        await pc2.setRemoteDescription(offer);
        let answer = await pc2.createAnswer();
        await pc2.setLocalDescription(answer);
        await pc1.setRemoteDescription(answer);

        assert_equals(remoteTrack.readyState, "live");
    }, "Reactivate the audio transceiver");
    </script>
</body>
</html>
