google-chrome - Firefox 到 Chrome 和 Chrome 到 Firefox 的 WebRTC 视频聊天无法正常工作

标签 google-chrome firefox webrtc

我正在尝试基于 WebRTC API 实现一个非常简单的视频聊天。 不幸的是,到目前为止,我的代码只能在 Chrome 到 Chrome 以及 Firefox 到 Firefox 之间运行。

如果我尝试从 Chrome 到 Firefox 或从 Firefox 到 Chrome,我会收到以下错误输出:

Failed to set local offer sdp: Session error code: ERROR_CONTENT. Session error description: Failed to set local video description recv parameters..(anonymous function) @ helloWebRtc.js:126***

我是否可能错过了某些内容,或者我是否需要在 Chrome 或 Firefox 浏览器中添加一些标志?

你有什么想法吗?如果我能得到任何帮助来解决这个问题,我将不胜感激。

先谢谢大家了!

<小时/>

我的 helloWebRtc.js 看起来像这样:

var localVideo = document.querySelector("#localVideo");
var remoteVideo = document.querySelector("#remoteVideo");

var SIGNAL_ROOM = "signal_room";
var CHAT_ROOM = "chat_room";
var serverConfig = {
    "iceServers": [
        {
            "urls": "stun:stun.l.google.com:19302"
        }
    ]
};

var optionalConfig = {
    optional: [
        {
            RtpDataChannels: true
        },
        {
            DtlsSrtpKeyAgreement: true
        }
    ]
};

var rtcPeerConn,
    localStream;

io = io.connect();
io.emit("ready", {"chat_room": CHAT_ROOM, "signal_room":   SIGNAL_ROOM});
io.emit("signal", {
    "room": SIGNAL_ROOM,
    "type": "user_here",
    "message": "new user joined the room"
});

io.on("rtcSignaling", function(data) {
    if(!rtcPeerConn) {
        startSignaling();
    }
    if(data.type !== "user_here" && data.message) {
        var message = JSON.parse(data.message);

        if(message.description) {
            var remoteDesc = new RTCSessionDescription(message.description);
            rtcPeerConn.setRemoteDescription(remoteDesc, function() {
                // if we receive an offer we need to answer
                if(rtcPeerConn.remoteDescription.type === "offer") {
                    rtcPeerConn.createAnswer(sendLocalDescription, function(error) {
                    console.error("error on creating answer", error);
                });
            }
        }, function(error) {
            console.error("error on set remote description", error);
        });
        } else if(message.candidate) {
            var iceCandidate = new RTCIceCandidate(message.candidate);
            rtcPeerConn.addIceCandidate(iceCandidate);
        }
    }
});

function startSignaling() {
    rtcPeerConn = new RTCPeerConnection(serverConfig, optionalConfig);

    //send any ice candidate to the other peer
    rtcPeerConn.onicecandidate = function(event) {
        if(event.candidate) {
            io.emit("signal", {
                "room": SIGNAL_ROOM,
                "type": "candidate",
                "message": JSON.stringify({
                    "candidate": event.candidate
                })
            });
        }
    };


    rtcPeerConn.onnegotiationneeded = function() {
        rtcPeerConn.createOffer(sendLocalDescription, function(error) {
            console.error("error on creating offer", error);
        });
    };

    // add the other peer's stream
    rtcPeerConn.onaddstream = function(event) {
        console.info("on add stream called");
        remoteVideo.srcObject = event.stream;
    };

    // add local stream
    navigator.mediaDevices.getUserMedia({
        audio: true,
        video: true
    })
    .then(function(stream) {
        localVideo.srcObject = stream;
        localStream = stream;
        rtcPeerConn.addStream(localStream);
    })
    .catch(function(e) {
        alert('getUserMedia() error: ' + e.name);
    });
}

function sendLocalDescription(description) {
    rtcPeerConn.setLocalDescription(
        description,
        function() {
            io.emit("signal", {
                "room": SIGNAL_ROOM,
                "type": "description",
                "message": JSON.stringify({
                    "description": rtcPeerConn.localDescription
                })
            });
        },
        function(error) {
            console.error("error to set local desc", error);
        }
    );
}

我的 NodeJS 服务器(使用 express.io)如下所示:

var express = require('express.io');
var app = express();
var PORT = 8686;

app.http().io();
console.log('server started @ localhost:8686');

// declaring folders to access i.e.g html files
app.use(express.static(__dirname + '/views'));
app.use('/scripts', express.static(__dirname + '/scripts'));

// root url i.e. "localhost:8686/"
app.get('/', function(req, res) {
    res.sendFile('index.html');
});


/**
* Socket.IO Routes for signaling pruposes
*/

app.io.route('ready', function(req) {
    req.io.join(req.data.chat_room);
    req.io.join(req.data.signal_room);
    app.io.room(req.data.chat_room).broadcast('announce', {
        message: 'New client in the ' + req.data.chat_room + ' room.'
    });
});

app.io.route('send', function(req) {
    app.io.room(req.data.room).broadcast('message', {
        message: req.data.message,
        author: req.data.author
    });
});

app.io.route('signal', function(req) {
    // Note: req means just broadcasting without letting the sender also receive their own message
    if(req.data.type === "description" || req.data.type === "candidate")
        req.io.room(req.data.room).broadcast('rtcSignaling', {
            type: req.data.type,
            message: req.data.message
        });
    else
        req.io.room(req.data.room).broadcast('rtcSignaling', {
            type: req.data.type
        });
});

app.listen(PORT);

最佳答案

您可以比较chrome和firefox生成的offer SDP,可能存在一些差异,并且无法与其他浏览器互操作。

关于google-chrome - Firefox 到 Chrome 和 Chrome 到 Firefox 的 WebRTC 视频聊天无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34263160/

相关文章:

javascript - Chrome 时间轴缓冲区使用情况

javascript - Chrome 扩展 "$ is not defined"错误

javascript - Chrome 扩展 - 从弹出窗口到内容脚本的消息传递

sql - 创建一个 shell 脚本来修改和/或在 firefox 中创建书签

android - React-native 访问设备摄像头? (特别是安卓)

angularjs - 无法阻止 Protractor 显示文件下载提示

html - 列表项不与 Firefox 合作

javascript - 如何强制 Firefox(版本 6)释放内存?

audio - 使用 WebRTC 的 P2P 音频广播

android - WebRTC 安卓 : sound from phone speaker is going into conference through phone mic and causing echo