FreeSWITCH学习笔记 第二场 第一个镜头 JsSIP初识

来源:互联网 发布:阿里云认证考试 编辑:程序博客网 时间:2024/05/20 09:48

官网地址:JsSIP;下载地址:JsSIP下载截至本博文版本为3.1.4;GIT地址:JSSIP源码;可以在官网看一下它的DEMO,可以看下官方API文档
FreeSWITCH配置可参见FreeSWITCH学习笔记 第一场 第二个镜头 视频通话配置


  • 官方Getting Started代码解析
// Create our JsSIP instance and run it/** * 创建websocket连接,连接地址最好是wss,本地测试可以使用ws,* 如果信令服务使用FreeSWITCH,那么websocket连接地址如下:* ws://FreeSWITCH所在服务器IP:5066 或 * wss://FreeSWITCH所在服务器IP:7443*/var socket = new JsSIP.WebSocketInterface('wss://sip.myhost.com');/** * User Agent配置参数,sockets表示信令服务器的连接集合,即可以* 注册多个信令服务器;uri即注册用户的SIP地址,password为连接密* 码;常用的参数还有register(true/false)表示是否直接注册;* no_answer_timeout无应答超时时间等。具体的可参考:* http://www.jssip.net/documentation/3.0.x/api/ua_configuration_parameters/*/var configuration = {  sockets  : [ socket ],  uri      : 'sip:alice@example.com',  password : 'superpassword'};//使用上述配置创建User Agentvar ua = new JsSIP.UA(configuration);/** * 连接到信令服务器,如果是之前停止则恢复到停止之前的状态,如果是刷新操作‘’* 而且configuration的register参数设置为true则直接注册到信令服务器*/ua.start();// 注册监听事件监听各个连接状态var eventHandlers = {  'progress': function(e) {    console.log('call is in progress');  },  'failed': function(e) {    console.log('call failed with cause: '+ e.data.cause);  },  'ended': function(e) {    console.log('call ended with cause: '+ e.data.cause);  },  'confirmed': function(e) {    console.log('call confirmed');  }};/*** 拨打参数配置,eventHandlers是注册事件监听的回调;mediaConstraints* 是多媒体配置,详细配置可以参考:https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia;* pcConfig可以配置ICE服务器地址,适用于需要穿透的网络环境*/var options = {  'eventHandlers'    : eventHandlers,  'mediaConstraints' : { 'audio': true, 'video': true },  'pcConfig': {    'iceServers': [      { 'urls': ['stun:a.example.com', 'stun:b.example.com'] },      { 'urls': 'turn:example.com', 'username': 'foo', 'credential': ' 1234' }    ]  }};// 拨打音视频电话给'sip:bob@example.com'var session = ua.call('sip:bob@example.com', options);

  • JsSIP多媒体设备调用

如果翻看JsSIP源码可以发现类似navigator.mediaDevices.getUserMedia(mediaConstraints).then().catch()这样的代码,所以不用怀疑JsSIP需要在支持WebRTC协议的浏览器上才能运行,所以Apple Mobile要IOS11以上才OK,但是支持也不是很好,需要适配。具体可以查看MDN web docs


  • WebRTC调用摄像头DEMO
<!DOCTYPE html><html>    <head>        <title>WebRTC</title>        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />        <meta name="Author" content="Fly" />        <meta name="description" content="WebRTC DEMO" />        <style type="text/css">        </style>    </head>    <body>        <div>            <video id="videoView"></video>        </div>    </body>    <script type="text/javascript">        var constraints = {           audio: true,           video: {             faceMode: 'user'           }        };        // API: https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia        navigator.mediaDevices.getUserMedia(constraints).then(function success(stream) {        videoView.srcObject = stream;        document.body.addEventListener('click', function() {            videoView.play();        });        // wait until the video stream is ready for IOS 11.* safari        var interval = setInterval(function(){            if(!videoView.videoWidth){                return;            }            clearInterval(interval);           }, 1000 / 50);        }).catch(function(error){            onError({                name: error.name,                message: error.message           });        });    </script></html>

本篇只是简单介绍了使用到的知识,下一篇文章将详细介绍接听处理方式