@historydev
Острая аллергия на анимешников

Почему remoteDescription null?

Здравствуйте. Пытаюсь разобраться с webRTC, по гайду от гугла. У меня получился следующий код, однако я получаю ошибку. Подскажите пожалуйста что я сделал не так?

index.js:29 Uncaught (in promise) DOMException: Failed to execute 'addIceCandidate' on 'RTCPeerConnection': The remote description was null


import { io } from "https://cdn.socket.io/4.3.2/socket.io.esm.min.js";

const socket = io('http://localhost:4000');

const pc = () => {
	const configuration = {'iceServers': [{'urls': 'stun:stun.l.google.com:19302'}]}
	return new RTCPeerConnection(configuration);
}

const peerConnection = pc();

const remoteVideo = document.querySelector('#remoteVideo');
const localVideo = document.querySelector('#localVideo');
await navigator.mediaDevices.getUserMedia({video: true, audio: true}).then(stream => {
	localVideo.srcObject = stream;
	stream.getTracks().forEach(track => {
		peerConnection.addTrack(track, stream);
	});
});

socket.on('answer', async answer => {
	console.log(peerConnection.signalingState, peerConnection.connectionState)
	const remoteDesc = new RTCSessionDescription(answer);
	await peerConnection.setRemoteDescription(remoteDesc);
});

socket.on('iceCandidate', async iceCandidate => {
	await peerConnection.addIceCandidate(iceCandidate);
});

peerConnection.addEventListener('icecandidate', event => {
	console.log(event);
	if (event.candidate) {
		socket.emit('message', {type: 'iceCandidate', message: event.candidate});
	}
});

peerConnection.addEventListener('connectionstatechange', event => {

	console.log('PeerState: ', peerConnection.connectionState)
	if (peerConnection.connectionState === 'connected') {
		console.log('Peer connected')
	}
});

peerConnection.addEventListener('track', async (event) => {
	console.log(event.streams);
	const [remoteStream] = event.streams;
	remoteVideo.srcObject = remoteStream;
});

document.querySelector('#listen').onclick = () => {
	socket.on('offer', async offer => {
		await peerConnection.setRemoteDescription(new RTCSessionDescription(offer));
		const answer = await peerConnection.createAnswer();
		await peerConnection.setLocalDescription(answer);
		socket.emit('message', {type: 'answer', message: answer});
	});
}


document.querySelector('#makeCall').onclick = async() => {
	const offer = await peerConnection.createOffer();
	await peerConnection.setLocalDescription(offer);
	socket.emit('message', {type: 'offer', message: offer});
}


P.S: Заменил offer на answer в listen, ошибка ушла, состояние подключён, на окне где нажимаешь listen ничего не происходит, на окне где нажимаешь makeCall начинает грузиться видео но так и не загружается:

document.querySelector('#listen').onclick = () => {
	socket.on('answer', async answer => {
		console.log(peerConnection.signalingState, peerConnection.connectionState)
		const remoteDesc = new RTCSessionDescription(answer);
		await peerConnection.setRemoteDescription(remoteDesc);
	});
}


Спасибо!
  • Вопрос задан
  • 159 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы