我写了一个以教程为模型的代码 - > https://bitbucket.org/webrtc/codelab。不幸的是,它只能工作一半,甚至一半。现在,打开第一个选项卡并允许操作相机后,控制台将抛出:
This appears to be Chrome adapter.js:79
Create or join to room myroomname CallToUser.js:50
>>> Room myroomname has 0 client(s) CallToUser.js:74
>>> Request to create or join room myroomname CallToUser.js:74
Created room myroomname CallToUser.js:55
Adding local stream CallToUser.js:34
Client sending a message: got user media CallToUser.js:84
>>> Got message: got user media
一旦打开,其他标签就会抛出:
This appears to be Chrome adapter.js:79
Create or join to room myroomname CallToUser.js:50
>>> Room myroomname has 1 client(s) CallToUser.js:74
>>> Request to create or join room myroomname CallToUser.js:74
User joined to room myroomname CallToUser.js:69
Adding local stream CallToUser.js:34
Client sending a message: got user media CallToUser.js:84
>>> Got message: got user media CallToUser.js:74
Client received a message: [object Object] CallToUser.js:94
Uncaught TypeError: Cannot call method 'setRemoteDescription' of undefined CallToUser.js:102
Client received a message: [object Object] CallToUser.js:94
Client received a message: [object Object] CallToUser.js:94
Client received a message: [object Object] CallToUser.js:94
Client received a message: [object Object] CallToUser.js:94
Client received a message: [object Object] CallToUser.js:94
Client received a message: [object Object] CallToUser.js:94
Client received a message: [object Object] CallToUser.js:94
Client received a message: [object Object] CallToUser.js:94
Client received a message: [object Object] CallToUser.js:94
Client received a message: [object Object] CallToUser.js:94
Client received a message: [object Object] CallToUser.js:94
Client received a message: [object Object] CallToUser.js:94
Client received a message: [object Object] CallToUser.js:94
Client received a message: [object Object] CallToUser.js:94
Client received a message: [object Object] CallToUser.js:94
Client received a message: [object Object] CallToUser.js:94
与第一个控制台添加到代码相反:
Another peer made request to join myroomname CallToUser.js:64
Client received a message: got user media CallToUser.js:94
Start call CallToUser.js:179
Client sending a message:
RTCSessionDescription {sdp: "v=0
↵o=- 2709183428323234981 2 IN IP4 127.0.0.1
↵s…8046 label:2f2f7c33-be63-4cff-8341-eabf2a30a775
↵", type: "offer"}
CallToUser.js:84
>>> Got message:
Object {sdp: "v=0
↵o=- 2709183428323234981 2 IN IP4 127.0.0.1
↵s…8046 label:2f2f7c33-be63-4cff-8341-eabf2a30a775
↵", type: "offer"}
CallToUser.js:74
Client sending a message: Object {type: "candidate", label: 0, id: "audio", candidate: "a=candidate:2999745851 1 udp 2113937151 192.168.56.1 60969 typ host generation 0
↵"} CallToUser.js:84
Client sending a message: Object {type: "candidate", label: 0, id: "audio", candidate: "a=candidate:2999745851 2 udp 2113937151 192.168.56.1 60969 typ host generation 0
↵"} CallToUser.js:84
Client sending a message: Object {type: "candidate", label: 0, id: "audio", candidate: "a=candidate:3013953624 1 udp 2113937151 192.168.1.100 60970 typ host generation 0
↵"} CallToUser.js:84
Client sending a message: Object {type: "candidate", label: 0, id: "audio", candidate: "a=candidate:3013953624 2 udp 2113937151 192.168.1.100 60970 typ host generation 0
↵"} CallToUser.js:84
Client sending a message: Object {type: "candidate", label: 1, id: "video", candidate: "a=candidate:2999745851 1 udp 2113937151 192.168.56.1 60969 typ host generation 0
↵"} CallToUser.js:84
Client sending a message: Object {type: "candidate", label: 1, id: "video", candidate: "a=candidate:2999745851 2 udp 2113937151 192.168.56.1 60969 typ host generation 0
↵"} CallToUser.js:84
Client sending a message: Object {type: "candidate", label: 1, id: "video", candidate: "a=candidate:3013953624 1 udp 2113937151 192.168.1.100 60970 typ host generation 0
↵"} CallToUser.js:84
Client sending a message: Object {type: "candidate", label: 1, id: "video", candidate: "a=candidate:3013953624 2 udp 2113937151 192.168.1.100 60970 typ host generation 0
↵"} CallToUser.js:84
>>> Got message: Object {type: "candidate", label: 0, id: "audio", candidate: "a=candidate:2999745851 1 udp 2113937151 192.168.56.1 60969 typ host generation 0
↵"} CallToUser.js:74
>>> Got message: Object {type: "candidate", label: 0, id: "audio", candidate: "a=candidate:2999745851 2 udp 2113937151 192.168.56.1 60969 typ host generation 0
↵"} CallToUser.js:74
>>> Got message: Object {type: "candidate", label: 0, id: "audio", candidate: "a=candidate:3013953624 1 udp 2113937151 192.168.1.100 60970 typ host generation 0
↵"} CallToUser.js:74
>>> Got message: Object {type: "candidate", label: 0, id: "audio", candidate: "a=candidate:3013953624 2 udp 2113937151 192.168.1.100 60970 typ host generation 0
↵"} CallToUser.js:74
>>> Got message: Object {type: "candidate", label: 1, id: "video", candidate: "a=candidate:2999745851 1 udp 2113937151 192.168.56.1 60969 typ host generation 0
↵"} CallToUser.js:74
>>> Got message: Object {type: "candidate", label: 1, id: "video", candidate: "a=candidate:2999745851 2 udp 2113937151 192.168.56.1 60969 typ host generation 0
↵"} CallToUser.js:74
>>> Got message: Object {type: "candidate", label: 1, id: "video", candidate: "a=candidate:3013953624 1 udp 2113937151 192.168.1.100 60970 typ host generation 0
↵"} CallToUser.js:74
>>> Got message: Object {type: "candidate", label: 1, id: "video", candidate: "a=candidate:3013953624 2 udp 2113937151 192.168.1.100 60970 typ host generation 0
↵"} CallToUser.js:74
Client sending a message: Object {type: "candidate", label: 0, id: "audio", candidate: "a=candidate:4233069003 1 tcp 1509957375 192.168.56.1 0 typ host generation 0
↵"} CallToUser.js:84
Client sending a message: Object {type: "candidate", label: 0, id: "audio", candidate: "a=candidate:4233069003 2 tcp 1509957375 192.168.56.1 0 typ host generation 0
↵"} CallToUser.js:84
Client sending a message: Object {type: "candidate", label: 1, id: "video", candidate: "a=candidate:4233069003 1 tcp 1509957375 192.168.56.1 0 typ host generation 0
↵"} CallToUser.js:84
Client sending a message: Object {type: "candidate", label: 1, id: "video", candidate: "a=candidate:4233069003 2 tcp 1509957375 192.168.56.1 0 typ host generation 0
↵"} CallToUser.js:84
>>> Got message: Object {type: "candidate", label: 0, id: "audio", candidate: "a=candidate:4233069003 1 tcp 1509957375 192.168.56.1 0 typ host generation 0
↵"} CallToUser.js:74
>>> Got message: Object {type: "candidate", label: 0, id: "audio", candidate: "a=candidate:4233069003 2 tcp 1509957375 192.168.56.1 0 typ host generation 0
↵"} CallToUser.js:74
>>> Got message: Object {type: "candidate", label: 1, id: "video", candidate: "a=candidate:4233069003 1 tcp 1509957375 192.168.56.1 0 typ host generation 0
↵"} CallToUser.js:74
Client sending a message: Object {type: "candidate", label: 0, id: "audio", candidate: "a=candidate:4247172264 1 tcp 1509957375 192.168.1.100 0 typ host generation 0
↵"} CallToUser.js:84
Client sending a message: Object {type: "candidate", label: 0, id: "audio", candidate: "a=candidate:4247172264 2 tcp 1509957375 192.168.1.100 0 typ host generation 0
↵"} CallToUser.js:84
Client sending a message: Object {type: "candidate", label: 1, id: "video", candidate: "a=candidate:4247172264 1 tcp 1509957375 192.168.1.100 0 typ host generation 0
↵"} CallToUser.js:84
Client sending a message: Object {type: "candidate", label: 1, id: "video", candidate: "a=candidate:4247172264 2 tcp 1509957375 192.168.1.100 0 typ host generation 0
↵"} CallToUser.js:84
>>> Got message: Object {type: "candidate", label: 1, id: "video", candidate: "a=candidate:4233069003 2 tcp 1509957375 192.168.56.1 0 typ host generation 0
↵"} CallToUser.js:74
>>> Got message: Object {type: "candidate", label: 0, id: "audio", candidate: "a=candidate:4247172264 1 tcp 1509957375 192.168.1.100 0 typ host generation 0
↵"} CallToUser.js:74
>>> Got message: Object {type: "candidate", label: 0, id: "audio", candidate: "a=candidate:4247172264 2 tcp 1509957375 192.168.1.100 0 typ host generation 0
↵"} CallToUser.js:74
>>> Got message: Object {type: "candidate", label: 1, id: "video", candidate: "a=candidate:4247172264 1 tcp 1509957375 192.168.1.100 0 typ host generation 0
↵"} CallToUser.js:74
>>> Got message: Object {type: "candidate", label: 1, id: "video", candidate: "a=candidate:4247172264 2 tcp 1509957375 192.168.1.100 0 typ host generation 0
↵"} CallToUser.js:74
End of candidates
对于任何标签页未添加到视频来电者的第二部分。我怀疑这是因为这个:
Uncaught TypeError: Cannot call method 'setRemoteDescription' of undefined
但是,我不知道如何处理这个错误。这是代码CallToUser.js和index.html
的剪辑CallToUser.js:
//'use strict';
var CallToUser = function() {
var isChannelReady;
var isInitiator;
var isStarted = false;
var servers = null;
var localStream;
var localPeerConnection;
var turnReady;
var remoteStream;
var pcConfig = {
'iceServers': [{
'url': 'stun:stun.l.google.com:19302'
}]
};
var pcConstraints = {
'optional': [{
'DtlsSrtpKeyAgreement': true
}]
};
var sdpConstraints = {
'mandatory': {
'OfferToReceiveAudio': true,
'OfferToReceiveVideo': true
}
};
var room;
var socket = io.connect();
var handleUserMedia = function(stream) {
console.log('Adding local stream');
document.getElementById('localVideo').src = window.URL.createObjectURL(stream);
localStream = stream;
sendMessage('got user media');
if(isInitiator) {
maybeStart();
}
};
var handleUserMediaError = function(error) {
console.log('getMediaError', error);
};
var createRoom = function(room) {
room = room;
if(room !== '') {
console.log('Create or join to room', room);
socket.emit('create or join', room);
}
socket.on('created', function(room) {
console.log('Created room ' + room);
isInitiator = true;
});
socket.on('full', function(room) {
console.log('Room' + room + ' is full');
});
socket.on('join', function(room) {
console.log('Another peer made request to join ' + room);
isChannelReady = true;
});
socket.on('joined', function(room) {
console.log('User joined to room ' + room);
isChannelReady = true;
});
socket.on('log', function(array) {
console.log.apply(console, array);
});
};
var connect = function() {
var constraints = {video: true};
getUserMedia(constraints, handleUserMedia, handleUserMediaError);
};
var sendMessage = function(message) {
console.log('Client sending a message: ', message);
socket.emit('message', message);
};
window.onbeforeunload = function(e){
sendMessage('bye');
};
var startCall = function() {
socket.on('message', function(message) {
console.log('Client received a message: ' + message);
if(message === 'got user media') {
maybeStart();
} else if(message.type === 'offer') {
if(!isInitiator && !isStarted) {
maybeStart();
}
localPeerConnection.setRemoteDescription(new RTCSessionDescription(message));
doAnswer();
} else if(message.type === 'candidate' && isStarted) {
var candidate = new RTCIceCandidate({
sdpMLineIndex: message.label,
candidate: message.candidate
});
localPeerConnection.addIceCandidate(candidate);
} else if(message === 'bye' && isStarted) {
handleRemoteEndCall();
}
});
if(location.hostname != 'localhost') {
requestTurn('https://computeengineondemand.appspot.com/turn?username=41784574&key=4080218913');
}
};
var maybeStart = function() {
if(!isStarted && typeof localStream != 'undefined' && isChannelReady) {
createPeerConnection();
localPeerConnection.addStream(localStream);
isStarted = true;
if(isInitiator) {
doCall();
}
}
};
var createPeerConnection = function() {
try {
localPeerConnection = new RTCPeerConnection(servers);
localPeerConnection.onicecandidate = handleIceCandidate;
localPeerConnection.onaddstream = handleRemoteStreamAdded;
localPeerConnection.onremovestream = handleRemoteStreamRemoved;
} catch(e) {
console.log('exception ' + e.message);
return;
}
};
var handleIceCandidate = function(event) {
if(event.candidate) {
sendMessage({
type: 'candidate',
label: event.candidate.sdpMLineIndex,
id: event.candidate.sdpMid,
candidate: event.candidate.candidate
});
} else {
console.log('End of candidates');
}
};
var handleRemoteStreamAdded = function(event) {
console.log('Remote stream added');
document.getElementById('remoteVideo').src = window.URL.createObjectURL(event.stream);
remoteStream = event.stream;
};
var handleRemoteStreamRemoved = function(event) {
console.log('Delete');
};
var handleCreateOfferError = function(event) {
console.log('createOffer() error: ' + e);
}
var setLocalAndSendMessage = function(sessionDescription) {
sessionDescription.sdp = preferOpus(sessionDescription.sdp);
localPeerConnection.setLocalDescription(sessionDescription);
sendMessage(sessionDescription);
};
var doCall = function() {
console.log('Start call');
localPeerConnection.createOffer(setLocalAndSendMessage, handleCreateOfferError);
};
var doAnswer = function() {
console.log('Sending answer');
localPeerConnection.createAnswer(setLocalAndSendMessage, null, sdpContraints);
};
var endCall = function() {
console.log('Hanging up');
isStarted = false;
localPeerConnection.close();
localPeerConnection = null;
sendMessage('bye');
};
var handleRemoteEndCall = function() {
};
var requestTurn = function(turnUrl) {
var turnExists = false;
for(var i in pcConfig.iceServers) {
if(pcConfig.iceServers[i].url.substr(0, 5) === 'turn:') {
turnExists = true;
turnReady = true;
break;
}
}
if(!turnExists) {
console.log('Getting TURN server from ', turnUrl);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.readyState === 4 && xhr.status === 200) {
var turnServer = JSON.parse(xhr.responseText);
console.log('Got TURN server: ', turnServer);
pc_config.iceServers.push({
'url': 'turn:' + turnServer.username + '@' + turnServer.turn,
'credential': turnServer.password
});
turnReady = true;
}
};
xhr.open('GET', turnUrl, true);
xhr.send();
}
};
var preferOpus = function(sdp) {
var sdpLines = sdp.split('\r\n');
var mLineIndex;
for(var i = 0; i < sdpLines.length; i++) {
if(sdpLines[i].search('m=audio') !== -1) {
mLineIndex = i;
break;
}
}
if(mLineIndex === null) {
return sdp;
}
for(i = 0; i < sdpLines.length; i++) {
if(sdpLines[i].search('opus/48000') !== -1) {
var opusPayload = extractSdp(sdpLines[i], /:(\d+) opus\/48000/i);
if(opusPayload) {
sdpLines[mLineIndex] = setDefaultCodec(sdpLines[mLineIndex], opusPayload);
}
break;
}
}
sdpLines = removeCN(sdpLines, mLineIndex);
sdp = sdpLines.join('\r\n');
return sdp;
};
var extractSdp = function(sdpLine, pattern) {
var result = sdpLine.match(pattern);
return result && result.length === 2 ? result[1] : null;
};
var setDefaultCodec = function(mLine, payload) {
var elements = mLine.split(' ');
var newLine = [];
var index = 0;
for(var i = 0; i < elements.length; i++) {
if(index === 3) {
newLine[index++] = payload;
}
if(elements[i] !== payload) {
newLine[index++] = elements[i];
}
}
return newLine.join(' ');
};
var removeCN = function(sdpLines, mLineIndex) {
var mLineElements = sdpLines[mLineIndex].split(' ');
for(var i = sdpLines.length - 1; i >= 0; i--) {
var payload = extractSdp(sdpLines[i], /a=rtpmap:(\d+) CN\/\d+/i);
if(payload) {
var cnPos = mLineElements.indexOf(payload);
if(cnPos !== -1) {
mLineElements.splice(cnPos, 1);
}
sdpLines.splice(i, 1);
}
}
sdpLines[mLineIndex] = mLineElements.join(' ');
return sdpLines;
};
return {
connect: connect,
startCall: startCall,
endCall: endCall,
createRoom: createRoom,
};
};
的index.html:
// some code
<script>
var calling = new CallToUser();
calling.createRoom('myroomname');
calling.connect();
calling.startCall();
</script>
有人能帮忙吗?
答案 0 :(得分:1)
更改您的流程传入消息
socket.on('message', function(message) {
console.log('Client received a message: ' + message);
if(message === 'got user media') {
maybeStart();
} else if(message.type === 'offer') {
if(!isInitiator && !isStarted) {
maybeStart();
}
localPeerConnection.setRemoteDescription(new RTCSessionDescription(message));
doAnswer();
}else if(message.type=="answer" && isStarted)
{
localPeerConnection.setRemoteDescription(new RTCSessionDescription(message));
}
else if(message.type === 'candidate' && isStarted) {
var candidate = new RTCIceCandidate({
sdpMLineIndex: message.label,
candidate: message.candidate
});
localPeerConnection.addIceCandidate(candidate);
} else if(message === 'bye' && isStarted) {
handleRemoteEndCall();
}
});
因为当其他对等接受用会话描述提供其创建答案时,您接受该答案并设置为本地对等连接