0
我想在呼叫者和被叫之間建立一個簡單的p2p視頻聊天。Angular p2p視頻聊天 - 遠程流是黑色視頻
這是代碼:
var OnBroadcast
, i
, isCaller = true
//just for testing pourpose
, URLparams = $location.search()
, iceServers = {
'iceServers':[{
'url':'stun:stun.l.google.com:19302'
}]
}
, connOpt = {
'optional':[{
'DtlsSrtpKeyAgreement': true
}]
}
, sdpConstraints = {
'mandatory': {
'OfferToReceiveAudio': true,
'OfferToReceiveVideo': true
}
}
, localVideo = $window.document.getElementById('localVideo')
, remoteVideo = $window.document.getElementById('remoteVideo')
, peerConnection = new RTCPeerConnection(iceServers, connOpt);
if (URLparams && URLparams.stranger) {
isCaller = false;
}
peerConnection.onaddstream = function (stream) {
if (!isCaller) {
$log.info('Caller Stream is', stream);
peerConnection.addStream(stream.stream);
remoteVideo.src = $window.URL.createObjectURL(stream.stream);
}
};
peerConnection.onicecandidate = function (ices) {
if (isCaller) {
ws.broadcast({
'scope': 'callerICES',
'message': ices
});
} else {
ws.broadcast({
'scope': 'calleeICES',
'message': ices
});
}
};
navigator.getUserMedia({
'audio': true,
'video': true
}, function (stream) {
localVideo.src = $window.URL.createObjectURL(stream);
if (isCaller) {
/* VIDEO CHAT P2P----------
* create CALLER Peer
* CALLER addStream to peer
* create CALLER Offer and CALLER setLocalDescription
* send CALLER Offer to CALLEE and set CALLEE remoteDescription
* create Answer from CALLEE and CALLEE setLocalDescription
* send Answer to CALLER and set CALLER setRemoteDescription
* CALLER icecandidate and send it to CALLEE and CALLEE addIceCandidate
* CALLEE icecandidate and send it to CALLER and CALLER addIceCandidate
* CALLEE addStream
*/
peerConnection.addStream(stream);
peerConnection.createOffer(function (offer) {
peerConnection.setLocalDescription(offer, function() {
ws.broadcast({
'scope': 'callerOFFER',
'message': offer
});
});
}, function (err) {
$log.error('Unable to create Offer from Caller', err);
});
}
}, function (err) {
$log.error('Unable to getUserMedia', err);
});
OnBroadcast = $rootScope.$on('comunicator:toAll', function (eventInfo, message) {
if (message.what.scope === 'callerOFFER') {
if (!isCaller) {
peerConnection.setRemoteDescription(new RTCSessionDescription(message.what.message), function() {
peerConnection.createAnswer(function (answer) {
$log.info('Setup localDesc Callee');
peerConnection.setLocalDescription(new RTCSessionDescription(answer), function() {
ws.broadcast({
'scope':'calleeANSWER',
'message': answer
});
}, function (err) {
$log.info('Unable to set localDesc for Callee', err);
},
sdpConstraints);
}, function (err) {
$log.error('Unable to create Answer from Callee', err);
});
});
}
}
if (message.what.scope === 'calleeANSWER') {
if (isCaller) {
peerConnection.setRemoteDescription(new RTCSessionDescription(message.what.message), function() {
$log.info('Setup remoteDesc Callee');
});
}
}
if (message.what.scope === 'callerICES') {
if (!isCaller) {
for (i = 0; i < message.what.length; i += 1) {
peerConnection.addIceCandidate(new RTCIceCandidate(message.what[i]));
}
$log.info('Setup CALLEE ices', message.what);
}
}
if (message.what.scope === 'calleeICES') {
if (isCaller) {
for (i = 0; i < message.what.length; i += 1) {
peerConnection.addIceCandidate(new RTCIceCandidate(message.what[i]));
}
$log.info('Setup CALLER ices', message.what);
}
}
});
一切似乎工作,但是當我將遠程視頻<video id="remoteVideo"></video>
我看到一個黑色的視頻而已,我在同一個網址和同一Wi-Fi測試此:
來電者:本地主機:8000
被叫:本地主機:8000陌生人=真
有人能解釋我這是問題嗎?
感謝男人的真棒答案,現在我改變了代碼,因爲你所描述的,它看起來像事情正在發生或可能不是,我的難題是調試,因爲我仍然看到黑色視頻,但音頻SEEMS工作(音頻流,而不是完全黑色的視頻流)......你建議做什麼?我真的想知道發生了什麼:( – yeyhhhhhhh
,但本地流有音頻和視頻? – mido
也,請檢查這個http://stackoverflow.com/questions/17391750/remote-videostream-not-working-with的答案-webrtc – mido