WebRTC1
by sj82516
HTML
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
<video id="video1" 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
var configuration = {
iceServers: [
{
'urls': 'turn:35.163.171.201:3478',
'credential': 'root',
'username': 'user'
},
]
};
var pc = new RTCPeerConnection(configuration);
remoteCandidate.addEventListener("keyup", function(){
console.log(new RTCIceCandidate(JSON.parse(remoteCandidate.value)));
pc.addIceCandidate(new RTCIceCandidate(JSON.parse(remoteCandidate.value))).catch((err)=>{
console.log("err:", err)
});
})
remoteSDP.addEventListener("keyup", function(){
console.log(JSON.parse(remoteSDP.value))
pc.setRemoteDescription(new RTCSessionDescription(JSON.parse(remoteSDP.value)))
})
pc.onicecandidate = function(evt){
if(evt.candidate){
console.log("pc show candidate:", JSON.stringify(evt.candidate));
}
}
pc.onnegotiationneeded = function () {
console.log("webrtc start")
pc.createOffer().then((d)=>localDescCreated(d));
}
pc.ontrack = function (evt) {
remote.srcObject = evt.streams[0];
};
navigator.mediaDevices.getUserMedia({
'audio': true,
'video': true
}).then(function (stream) {
pc.addStream(video1.srcObject = stream);
});
function localDescCreated(desc) {
pc.setLocalDescription(desc).then(function () {
console.log("pc show sdp:", JSON.stringify(pc.localDescription))
});
}