WebRTC не отправляйте видео в ответ
У меня есть вопрос, касающийся WebRTC, и я установил простую демонстрацию двух пиров, которые могут отправлять данные друг другу.
Теперь, когда клиент 2 хочет получить предложение, он отправляет видео со своей камеры.
но клиент не отсылает ссылку на скрытое видео в ответе SDP, я не знаю почему.
это мой код:
HTML часть
<textarea id="text"></textarea><br><br>
<video id="localVideo" autoplay muted style="width:10%;"></video>
<script src="/socket.io/socket.io.js"></script>
часть JavaScript
<script type="text/javascript">
//video
let localVideo;
navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia || navigator.webkitGetUserMedia;
localVideo = document.getElementById('localVideo');
remoteVideo = document.getElementById('remoteVideo');
let constraints = {video: true,audio: false,};
if(navigator.getUserMedia) {
navigator.getUserMedia(constraints, getUserMediaSuccess, getUserMediaError);
} else {alert('Your browser does not support getUserMedia API');}
function getUserMediaSuccess(stream) {
localStream = stream;
localVideo.src = window.URL.createObjectURL(stream);
}
function getUserMediaError(error) {console.log(error);}
//video
var text = document.getElementById('text')
var peerConnection;
var peerConnectionConfig = {'iceServers': [{'url': 'stun:stun.services.mozilla.com'}, {'url': 'stun:stun.l.google.com:19302'}]};
socket = io.connect('http://localhost:8080')
socket.on('ilyes',(data)=>{
console.log('i am saying : ' + data)
})
socket.on("R-ice",(data)=>{
var id = data.me
console.log('2_ice : ' + JSON.stringify(JSON.parse(data.ice).ice))
var ice = JSON.parse(data.ice).ice
if(!peerConnection)
peerConnection = new RTCPeerConnection(peerConnectionConfig);
//datachannel
sendChannel = peerConnection.createDataChannel('sendDataChannel');
console.log('dataChannel is created : ' + sendChannel.readyState)
sendChannel.onopen = function(){console.log('onopen from LOCAL is GREAT')};
sendChannel.onclose = function(){console.log('onclose from LOCAL is GREAT')
window.location.reload()
};
peerConnection.ondatachannel = receiveChannelCallback
console.log('for now everything is OK')
peerConnection.onicecandidate = function RgotIceCandidate(event){
if(event.candidate != null){
var icee = event.candidate
console.log('got ICE : '+ JSON.stringify({icee}))
socket.emit('ice',{ice : JSON.stringify({icee}),me:id})
}
};
peerConnection.addStream(localStream);
peerConnection.addIceCandidate(new RTCIceCandidate(ice));
console.log('ice added')
})
socket.on("R-sdp",(data)=>{
console.log('Rsdp sent : ' + data)
var id = data.me
console.log('le me is : ' + id)
if(!peerConnection)
peerConnection = new RTCPeerConnection(peerConnectionConfig);
var description = JSON.parse(data.sdp).description
var sdp = JSON.stringify(description)
var offer =JSON.parse(sdp).type
console.log('attendre le ansewer')
peerConnection.addStream(localStream);
peerConnection.setRemoteDescription(new RTCSessionDescription(description), function(){
console.log('offer est : ' + offer)
if (offer=="offer"){
peerConnection.createAnswer(function RgotDescription(description){
console.log('got description : '+ JSON.stringify({description}))
peerConnection.setLocalDescription(description,function(){
socket.emit('sdp',{sdp:JSON.stringify({description}),me:id})
}, function(){console.log('sdp error')})
}, createAnswerError);
console.log('ansewer sent')
}
})
})
function createAnswerError(error){
console.log('il ya une erreur dans le ansewer : ' + error)
}
function receiveChannelCallback(event) {
console.log('Reciev Data: ' + event.data);
receiveChannel = event.channel;
receiveChannel.onmessage = handleReceiveMessage;
receiveChannel.onopen = function(){console.log('onopen from REMOTE is GREAT')};
receiveChannel.onclose = function(){console.log('onclose from REMOTE is GREAT')};
}
function handleReceiveMessage(event) {
console.log('msg recieved : '+ event.data)
text.value = event.data
socket.emit('data',text.value)
}
</script>