无冰块候选者 无法连接对等网络rtc

问题描述 投票:0回答:1

我试着实现了以下代码中的webrtc Webrtc.org对等连接指南. 虽然没有产生有效的冰候选人。我还注意到在SDP中,即使我在不同的设备上运行应用程序,本地主机的IP(127.0.0.1)总是被使用。在Firefox中,我确实得到了一个icecandidate事件。虽然event.candidate == null。

我有什么地方做错了吗?

下面是代码。

let socket = io()
let localStream = null;
const configuration = {
    'iceServers': [{
        'urls': 'stun:stun.l.google.com:19302'
    }]
}
let peerConnection = new RTCPeerConnection(configuration)

// init video media
function start() {
    navigator.mediaDevices.getUserMedia({
        audio: true,
        video: true
    }).then(stream => {
        localVideo.srcObject = stream
        localStream = stream
    })
    for (let track of localStream.getTracks()) {
        peerConnection.addTrack(track, localStream)
    }
}

// starts the call
async function call() {
    const offer = await peerConnection.createOffer()
    await peerConnection.setLocalDescription(offer)
    socket.emit('offer', offer)
}

peerConnection.onicecandidate = event => {
    if (event.candidate) {
        socket.emit('newIceCandidate', event.candidate)
    }
}


// Listen for remote ICE candidates and add them to the local RTCPeerConnection

socket.on('newIceCandidate', async candidate => {
    try {
        await peerConnection.addIceCandidate(candidate)
    } catch (e) {
        console.log(e)
    }
})

// Is never called
peerConnection.addEventListener('connectionstatechange', event => {
    if (peerConnection.connectionState === 'connected') {
        console.log('CONNECTED PEER')
    }
})


// user 1 receiving data from other
socket.on('answer', async remoteDescription => {
    const remoteDesc = new RTCSessionDescription(remoteDescription)
    await peerConnection.setRemoteDescription(remoteDesc)
})

// user 2 getting init message
socket.on('offer', async offer => {
    peerConnection.setRemoteDescription(new RTCSessionDescription(offer))
    const answer = await peerConnection.createAnswer()
    await peerConnection.setLocalDescription(answer)
    socket.emit('answer', answer)
})

/////////////// Less relevant /////////////////


peerConnection.oniceconnectionstatechange = state => {
    console.log('ice state change!')
}

peerConnection.ontrack = ({
    streams: [stream]
}) => remoteVideo.srcObject = stream


function stop() {

}

start()

Console outputPeer connection contents

javascript webrtc rtcpeerconnection
1个回答
1
投票

在webrtc协商之前,你必须将你的轨迹添加到peerconnection对象中。

但在你的代码中,你没有。

getUserMedia函数是Promise。

所以像这样尝试

// init video media
function start() {
    navigator.mediaDevices.getUserMedia({
        audio: true,
        video: true
    }).then(stream => {
        localVideo.srcObject = stream
        localStream = stream

        for (let track of localStream.getTracks()) {
            peerConnection.addTrack(track, localStream)
        }
    })
}
© www.soinside.com 2019 - 2024. All rights reserved.