• 欢迎光临~

调用第三方接口实现呼叫台功能 -WebRTC

开发技术 开发技术 2022-11-30 次浏览

调用第三方接口实现呼叫台功能WebRTC

WebRTC学习笔记——建立连接 - 简书 (jianshu.com)

(4条消息) webRTC(八):查看offer/answer 的 SDP_晓果博客的博客-CSDN博客

(4条消息) WebRTC源码研究(32)获取offer answer创建的SDP_极客雨露的博客-CSDN博客

(4条消息) webrtc 的 CreateOffer 过程分析_zhuiyuanqingya的博客-CSDN博客

WebRTC是一个开源的项目,可以提供浏览器,手机应用之间实时通信能力。

  • 主要JavaScript API

    • MediaStream 音视频流对象
    • RTCPeerConnection 端对端音视频连接对象
    • RTCDataChannel 端对端数据通道对象

调用第三方接口实现呼叫台功能 -WebRTC

 

 

 

1、由于浏览器API有相应的前缀,需要有两个兼容函数来首先处理一下:(这步作用不清楚照抄下来了)

调用第三方接口实现呼叫台功能 -WebRTC调用第三方接口实现呼叫台功能 -WebRTC
function hasUserMedia() {
    navigator.getUserMedia = navigator.getUserMedia ||
        navigator.webkitGetUserMedia ||
        navigator.mozGetUserMedia ||
        navigator.msGetUserMedia;
    return !!navigator.getUserMedia;
}

function hasRTCPeerConnection() {
    window.RTCPeerConnection = window.RTCPeerConnection ||
        window.webkitRTCPeerConnection ||
        window.mozRTCPeerConnection ||
        window.msRTCPeerConnection;
    return !!window.RTCPeerConnection;
}
View Code

2、可以通过配置自己的STUN服务器地址,或者不写配置使用浏览器默认的STUN服务器地址,来创建两个RTCPeerConnection对象来模拟连接

  局域网可以不配置STUN服务器地址

  不给config,局域网内两个peer是可以找到对方的         配置个内网stun也没用     如果有网络穿透的需求,那需要自己搭建一个stun服务器

调用第三方接口实现呼叫台功能 -WebRTC

//var configuration = {
        //    //iceServers: [{ url: "stun:127.0.0.1:9876" }]
        //};
//,otherConnection是被调用方,被调用方是接收数据的
youConnection = new RTCPeerConnection(); otherConnection = new RTCPeerConnection();

调用第三方接口实现呼叫台功能 -WebRTC

3.通信双方交换ICE候选路径,也就是通过ICE获取到自己的IP和端口号后,再互相交换此信息(暂时也不清楚什么作用)

    youConnection.onicecandidate = function (event) {
        if (event.candidate) {
            console.log(event.candidate);
            otherConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
        }
    };

    otherConnection.onicecandidate = function (event) {
        if (event.candidate) {
            console.log(event.candidate);
            youConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
        }
    };

4.通信双方通过交换offer和answer来互换SDP信息  ---创建媒体协商

    var offerOptions={
        offerToRecceiveAudio: 1,
        offerToReceiveVideo: 1
    };

    youConnection.createOffer(offerOptions)
        .then(function(offer){
            console.log(offer);
            youConnection.setLocalDescription(offer);
            otherConnection.setRemoteDescription(offer);

            otherConnection.createAnswer(offerOptions)
                .then(function(answer){
                    console.log(answer);
                    otherConnection.setLocalDescription(answer);
                    youConnection.setRemoteDescription(answer);
                });
    });

 

程序员灯塔
转载请注明原文链接:调用第三方接口实现呼叫台功能 -WebRTC
喜欢 (0)
违法和不良信息举报电话:022-22558618 举报邮箱:dljd@tidljd.com