現在使用webrtc,發現連接建立之后,音視頻資料無法正確收發。代碼如下:
"use strict";
// UserMedia configure
var mediaConstraints = {
audio: true, // We want an audio track
video: true
};
var offerConstraints = {
offerToReceiveAudio: 1,
offerToReceiveVideo: 1,
voiceActivityDetection: false
};
var myPeerConnection = null; // RTCPeerConnection
var localStream = null;
var webStream = null;
var testsdp = null;
var dataChannel = null;
// real send function
function send(msg) {
var msgJSON = JSON.stringify(msg);
MySend({
address: document.getElementById('name').value
}, msgJSON, (err) => {
if (err) {
console.log(err);
}
});
}
function sendToServer(msg) {
console.log("send msg");
console.log(msg);
var msgJSON = JSON.stringify(msg);
send(msgJSON);
}
function handleNewICECandidateMsg(msg) {
try {
var candidate = new RTCIceCandidate(msg.candidate);
console.log("*** Adding received ICE candidate: " + JSON.stringify(candidate));
if (!myPeerConnection) { createPeerConnection(); }
myPeerConnection.addIceCandidate(candidate)
} catch (err) {
reportError(err);
}
}
// called when receive msg
function onMessage(res) {
var msg = JSON.parse(JSON.parse(res.data));
console.log("Message received: " + msg.type);
console.log(msg);
switch (msg.type) {
case "video-offer": // Invitation and offer to chat
handleVideoOfferMsg(msg);
break;
case "video-answer": // Callee has answered our offer
handleVideoAnswerMsg(msg);
break;
case "hang-up": // The other peer has hung up the call
handleHangUpMsg(msg);
break;
case "new-ice-candidate":
handleNewICECandidateMsg(msg);
break;
default:
log_error("Unknown message received:");
console.log(msg);
}
};
// called when RTCPeerConnection.addStream was called
function handleAddStream(event) {
console.log("*** handleAddStream");
console.log(event);
webStream = event.streams[0] || event.stream;
document.getElementById("received_video").srcObject = webStream;
document.getElementById("received_video").crossOrigin = '*';
document.getElementById("hangup-button").disabled = false;
}
function handleICECandidateEvent(event) {
if (event.candidate) {
console.log("*** Outgoing ICE candidate: " + event.candidate.candidate);
sendToServer({
type: "new-ice-candidate",
candidate: event.candidate
});
}
}
function handleSignalStateChanged(event) {
console.log("***** handleSignalStateChanged *****");
console.log(event)
}
// create a RTCPeerConnection
function createPeerConnection() {
console.log("Setting up a connection...");
myPeerConnection = new RTCPeerConnection({ "rtcpMuxPolicy": "require", "bundlePolicy": "max-bundle", "iceServers": [] });
myPeerConnection.ontrack = handleAddStream;
myPeerConnection.onicecandidate = handleICECandidateEvent
myPeerConnection.onsignalingstatechange = handleSignalStateChanged;
}
// close the Video call chat
function closeVideoCall() {
var localVideo = document.getElementById("local_video");
console.log("Closing the call");
// Close the RTCPeerConnection
if (myPeerConnection) {
console.log("--> Closing the peer connection");
if (localVideo.srcObject) {
localVideo.pause();
localVideo.srcObject.getTracks().forEach(track => {
track.stop();
});
}
// Close the peer connection
myPeerConnection.close();
// dataChannel.close();
// dataChannel = null;
myPeerConnection = null;
}
document.getElementById("hangup-button").disabled = true;
}
// deal with the hangup msg
function handleHangUpMsg(msg) {
console.log("*** Received hang up notification from other peer");
closeVideoCall();
}
// click event
function hangUpCall() {
closeVideoCall();
sendToServer({
type: "hang-up"
});
}
function sendMsg() {
// dataChannel.send("hello, world");
}
// click event to start a video call chat
function invite() {
console.log("Starting to prepare an invitation");
if (myPeerConnection) {
console.log("You can't start a call because you already have one open!");
} else {
createPeerConnection();
navigator.mediaDevices.getUserMedia(mediaConstraints).then(function (stream) {
localStream = stream;
console.log('localStream');
console.log(localStream);
// Adding a local stream won't trigger the onaddstream callback
// myPeerConnection.addStream(localStream);
for (const track of localStream.getTracks()) {
myPeerConnection.addTrack(track, localStream);
}
document.getElementById('local_video').srcObject = localStream;
document.getElementById("local_video").crossOrigin = '*';
// myPeerConnection.createOffer(function (offer) {
// // offer.sdp = replaceIp(offer.sdp);
// testsdp = offer;
// myPeerConnection.setLocalDescription(offer, function () {
// sendToServer({
// type: "video-offer",
// sdp: offer
// });
// }, error => { console.log(error); })
// }, error => { console.log(error); })
return myPeerConnection.createOffer(offerConstraints);
}).then(function (offer) {
return myPeerConnection.setLocalDescription(offer);
}).then(function () {
sendToServer({
type: "video-offer",
sdp: myPeerConnection.localDescription
});
}).catch(reportError);
}
}
// deal with the coming offer
function handleVideoOfferMsg(msg) {
console.log("*** Call handleVideoOfferMsg");
if (!myPeerConnection) {
createPeerConnection();
}
var rtcs = msg.sdp;
myPeerConnection.setRemoteDescription(rtcs).then(function () {
return navigator.mediaDevices.getUserMedia(mediaConstraints);
}).then(function (stream) {
localStream = stream;
// myPeerConnection.addStream(localStream);
for (const track of localStream.getTracks()) {
myPeerConnection.addTrack(track, localStream);
}
document.getElementById('local_video').srcObject = localStream;
console.log(localStream);
document.getElementById("local_video").crossOrigin = '*';
return myPeerConnection.createAnswer(offerConstraints);
}).then(function (answer) {
return myPeerConnection.setLocalDescription(new RTCSessionDescription(answer));
}).then(function () {
console.log("send video-offer");
return;
sendToServer({
type: "video-answer",
sdp: myPeerConnection.localDescription
});
}).catch(reportError);
}
// deal with the coming answer
function handleVideoAnswerMsg(msg) {
console.log("*** handleVideoAnswerMsg");
var desc = msg.sdp;
myPeerConnection.setRemoteDescription(desc).catch(reportError);
}
// deal with the errors occured when get the UserMedia
function handleGetUserMediaError(e) {
switch (e.name) {
case "NotFoundError":
console.log("Unable to open your call because no camera and/or microphone" +
"were found.");
break;
case "SecurityError":
case "PermissionDeniedError":
break;
default:
console.log("Error opening your camera and/or microphone: " + e.message);
break;
}
closeVideoCall();
}
function reportError(errMessage) {
console.log("errMessage");
console.log(errMessage);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/165567.html
標籤:其他
