WebRTC2

by sj82516

HTML

<video id="video2" width="320" height="240" autoplay muted></video>
<br/>
<input id="remoteCandidate" type="text"/>
<br/>
<input id="remoteSDP" type="text"/>
<br/>
<video id="remote" width="320" height="240" autoplay muted></video>

JavaScript

remoteCandidate.addEventListener("keyup", function(){
	console.log(JSON.parse(remoteCandidate.value));
  pc.addIceCandidate(new RTCIceCandidate(JSON.parse(remoteCandidate.value)));
})

remoteSDP.addEventListener("keyup", function(){
	console.log(JSON.parse(remoteSDP.value))
	pc.setRemoteDescription(new RTCSessionDescription(JSON.parse(remoteSDP.value)));
  pc.createAnswer().then(d=>localDescCreated(d));
})

var configuration = {
  'iceServers': [{
    'url': 'stun:stun.example.org'
  }]
};
var pc;
pc = new RTCPeerConnection(configuration);
pc.onicecandidate = function(evt){
	if(evt.candidate){
  	console.log("pc show candidate:", JSON.stringify(evt.candidate));
  }
}
pc.onnegotiationneeded = function () {
  pc.createOffer().then((d)=>localDescCreated(d));
}

pc.onaddstream = function (evt) {
  remote.src = URL.createObjectURL(evt.stream);
};

navigator.mediaDevices.getUserMedia({
  'audio': true,
  'video': true
}).then(function (stream) {
  video2.src = URL.createObjectURL(stream);
  pc.addStream(stream);
});

function localDescCreated(desc) {
  pc.setLocalDescription(desc).then(function () {
    console.log("pc show sdp:", JSON.stringify(pc.localDescription))
  });
}