【问题标题】:RTCPeerConnection is not a constructor, in Firefox and SafariRTCPeerConnection 不是构造函数,在 Firefox 和 Safari 中
【发布时间】:2018-05-31 15:24:39
【问题描述】:

我正在编写一个非常简单的 WebRTC 应用程序来从 RaspberryPi Zero 摄像头流式传输实时视频。我使用Linux Project's UV4L driver 设置服务器和JavaScript 来连接和播放视频流。我的 JavaScript 代码基于UV4L's demo,它本质上使用 RTC Web 套接字方法来执行协商。

他们的代码在 Chrome 中运行良好,但在 Firefox 或 Safari 下似乎无法运行。

RTCPeerConnection = window.webkitRTCPeerConnection;
RTCSessionDescription = window.RTCSessionDescription;
RTCIceCandidate = window.RTCIceCandidate;
var ws;

function signal(url, onStream, onError, onClose, onMessage) {    
    if("WebSocket" in window) {
        var pc;
        ws = new WebSocket(url);

        ws.onopen = function () {
            var config = {"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]};
            pc = new RTCPeerConnection(config);     // <---- ERROR here.
            pc.onicecandidate = function (event) {
                // ... ICE negotiation.
            };

            if('ontrack' in pc) {
                pc.ontrack = function(event) {
                    // ... set stream object and play
                };
            } else {  // onaddstream() is deprecated
                pc.onaddstream = function (event) {
                    // ... set stream object and play
                };
            }
            // ... other event listeners.
            ws.send(...); // Signals the remote peer to initiate a call
        };
    }
}

特别是,当我尝试连接时,我收到一个错误,在 Firefox v60.0.1 中抛出以下错误(在 Safari 中非常相似):

TypeError: RTCPeerConnection 不是构造函数

根据MDN docs,Firefox 从 v22 开始支持此构造函数。可能是什么问题?

【问题讨论】:

  • 似乎是RTCPeerConnection 是不可构造的。你试过删除new吗?
  • 我刚试过,Chrome和Firefox都抱怨。错误消息说RTCPeerConnection 不能用作函数。

标签: javascript firefox websocket safari webrtc


【解决方案1】:

我的错误原来是一个愚蠢的错字。代码开头的 RTCPeerConnection 声明是错误的。应该是:

RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 2016-02-22
    • 2017-06-02
    • 1970-01-01
    相关资源
    最近更新 更多