简体   繁体   English

webrtc ontrack 事件处理程序未异步触发 function

[英]webrtc ontrack event handler not firing in async function

I am trying to understand webRTC flow by writing a simple local rtc communication.我试图通过编写一个简单的本地 rtc 通信来理解 webRTC 流程。 I am also trying to write the code using the async/await syntax, I tried using Promises and its working perfectly.我也在尝试使用 async/await 语法编写代码,我尝试使用 Promises 并且它工作得很好。 I have the following script tag in my index.html file我的index.html文件中有以下脚本标签

<script async src="test.js" onload="init()"> </script>

and my javascript code和我的 javascript 代码

const a = new RTCPeerConnection();
const b = new RTCPeerConnection();

async function init() {
    try {
        a.onicecandidate = e => {
            console.log("alice candidate ", e);
            if (e.candidate) {
                b.addIceCandidate(e.candidate);
            }
        }
        b.onicecandidate = e => {
            console.log("bob candidate", e);
            if (e.candidate) {
                a.addIceCandidate(e.candidate);

            }
        }
        let stream = await navigator.mediaDevices.getDisplayMedia({
            video: { width: 800, height: 600 }

        })
        document.getElementById("local").srcObject = stream;

        for (const track of stream.getTracks()) {
            a.addTrack(track, stream);
        }

        let offer = await a.createOffer();
        await a.setLocalDescription(offer);
        await b.setRemoteDescription(a.localDescription);
        let answer = await b.createAnswer();
        await b.setLocalDescription(answer);
        await a.setRemoteDescription(b.localDescription);

        //my misunderstanding is here
        b.ontrack = async e => {
            console.log("test on track");
            document.getElementById("remote").srcObject = e.streams[0];
        }

    } catch (err) {
        console.err(err);
    }
}

My problem comes when I try to put the ontrack event inside the async function.当我尝试将 ontrack 事件放在异步 function 中时,我的问题就出现了。 The ontrack event is never fired. ontrack 事件永远不会被触发。

//when I put this event outside the async function the code works but not inside
b.ontrack = async e => {
    console.log("test on track");
    document.getElementById("remote").srcObject = e.streams[0];
}

I don't know if it is bad practice but I prefer the async/await syntax rather than the .then .我不知道这是否是不好的做法,但我更喜欢 async/await 语法而不是.then However as far as I know I can not write async/await outside an async function.但是据我所知,我不能在异步 function 之外编写异步/等待。 So my question is why does then ontrack event do not fire inside the async init() function but it does work when I put it outside in the top level scope.所以我的问题是为什么 ontrack 事件不会在async init() function 内触发,但是当我将它放在顶层 scope 之外时它确实有效。

Thank you.谢谢你。

ontrack fires as part of setRemoteDescription. ontrack 作为 setRemoteDescription 的一部分触发。 However, in your async code you are only adding a ontrack handler later so it won't get triggered.但是,在您的异步代码中,您稍后只会添加一个 ontrack 处理程序,因此它不会被触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM