我为一个学生项目开发一个小的webRTC示例。目的是,一个客户端(学生)通过peerConnection将他的视频和音频流发送给第二个客户端(导师)。沟通工作和导师得到SDP_OFFER和所有的东西。它也是remoteVideo src的blob。但视频输出为黑色。 在导师方面,我得到了以下错误:
未捕获错误:SYNTAX_ERR:DOM例外12
doAnswer processSignalingMessage
ws.onmessage
我希望有人有个主意。 谢谢!
有我的代码:
<!DOCTYPE html>
<html>
<head>
<title>PeerConnection with an websocket</title>
</head>
<script>
//variables
var pc;
var ws;
var name;
// Local stream generation
function gotStream(s) {
var url = webkitURL.createObjectURL(s);
document.getElementById("localView").src = url;
localStream = s;
console.log("gotStream" + s)
console.log("Started showing loval stream. url = " + url);
}
function gotStreamFailed(error) {
console.log("Failed to get access to local media. Error code was "
+ error.code + ".");
}
function getUserMedia() {
try {
navigator.webkitGetUserMedia({audio:true, video:true},
gotStream, gotStreamFailed);
console.log("Requested access to local media");
} catch (e) {
console.log(e, "getUserMedia error");
}
}
//peerConnection generation
function genreatePeerConnection(){
try{
pc = new webkitPeerConnection00("STUN stun.l.google.com:19302", onIceCandidate);
console.log("new peerConnection!");
pc.onaddstream = onAddStream;
pc.onremovestream = onRemoveStream;
}catch (e){
console.log(e, "generatePeerConnection error");
}
}
function onAddStream(e) {
var stream = e.stream;
var url = webkitURL.createObjectURL(stream);
try{
document.getElementById("remoteView").src = url;
console.log("Started showing remote stream. url = " + url);
}catch (e){
console.log(e, "no Video error...");
}
}
function onRemoveStream(e) {
document.getElementById("remoteView").src = "";
trace("Stopped showing remote stream");
}
function onIceCandidate(candidate, moreToFollow) {
console.log("candidate: " + candidate);
if (candidate) {
sendMessage({type: 'candidate',
label: candidate.label, candidate: candidate.toSdp()});
}
}
function startCall() {
console.log("adding stream" + localStream);
pc.addStream(localStream);
doCall();
}
function doCall() {
console.log("Send offer to peer");
var offer = pc.createOffer({audio:true, video:true});
pc.setLocalDescription(pc.SDP_OFFER, offer);
sendMessage({type: 'offer', sdp: offer.toSdp()});
pc.startIce();
}
function doAnswer() {
console.log("Send answer to peer");
var offer = pc.remoteDescription;
var answer = pc.createAnswer(offer.toSdp(),{audio:true, video:true});
pc.setLocalDescription(pc.SDP_ANSWER, answer);
sendMessage({type: 'answer', sdp: answer.toSdp()});
pc.startIce();
}
function processSignalingMessage(message) {
var msg = JSON.parse(message.data);
if (msg.type === 'offer') {
console.log("Test 1 - offer" + msg.sdp);
pc.setRemoteDescription(pc.SDP_OFFER, new SessionDescription(msg.sdp));
doAnswer();
} else if (msg.type === 'answer') {
pc.setRemoteDescription(pc.SDP_ANSWER, new SessionDescription(msg.sdp));
} else if (msg.type === 'candidate') {
console.log("Candidate...");
var candidate = new IceCandidate(msg.label, msg.candidate);
pc.processIceMessage(candidate);
} else if (msg.type === 'bye') {
onRemoteHangup();
}
}
//Websocket connection
function openWs(){
genreatePeerConnection();
ws = new WebSocket("ws://10.68.0.88:10081");
ws.onopen = function() {
console.log("Openned websocket connection");
}
ws.onmessage = function (evt){
console.log("you got mail: " + evt);
processSignalingMessage(evt);
}
}
//sendMessage to WS
function sendMessage(msg){
var stringMsg = JSON.stringify(msg);
ws.send(stringMsg);
}
function sendName(){
name = document.getElementById("name").value;
ws.send("name: " + name);
}
</script>
<body>
name<input id="name"/><button id="sendName" onclick="sendName();">send </button>
<video id="localView" width="352" height="288" autoplay></video>
<video id="remoteView" width="352" height="288" autoplay></video>
<button id="call" onclick="startCall();">Call</button>
<button id="connect" onclick="openWs();">Connection</button>
<button id="getMedia" onclick="getUserMedia();">Cam</button>
</body>
</html>
答案 0 :(得分:0)
仔细查看以下示例:http://www.html5rocks.com/en/tutorials/webrtc/basics/
我认为你可能有一些缺失,比如停止被调用者在processSignalingMessage()中发送商品,并在startCall()中的pc.addStream之前放置createPeerConnection()。祝你好运!
答案 1 :(得分:0)
非常感谢你的榜样!我不知道为什么我这么盲目但是通过这个例子我发现了我的错误。 我忘记了doCall()函数中的后续行:
pc.setLocalDescription(pc.SDP_OFFER, offer);
我不知道怎么会错过这个但也许我看不到森林里的树木了。 现在它有效! 谢谢:)