暂无描述

pcSip备份.js 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. var outgoingSession = null,
  2. incomingSession = null,
  3. currentSession = null,
  4. nativeStream = null,
  5. callVideoState = null,
  6. callVideoFail = null,
  7. inComing = true,
  8. sip_uri_, sip_password_, ws_uri_, localStream = null,
  9. userAgent = null,
  10. currentVideoIndex = 0,
  11. timeOut = null;
  12. var getWidthScale = document.documentElement.clientWidth; //监测浏览器的宽度
  13. var myVideoView = document.getElementById('myVideo'); //我的本地视频
  14. var videoView_1 = document.getElementById('videoView'); //对方的视频信息
  15. $(function() {
  16. $(".videoList").css({
  17. "height": getWidthScale * 0.9 / 1.6 + "px",
  18. })
  19. $(".videoMy").css({
  20. "height": getWidthScale * 0.9 / 1.6 + "px",
  21. })
  22. })
  23. function switchScreen() {
  24. console.log("switchScreen")
  25. $(".titText").hide();
  26. $(".callVideo").hide();
  27. $(".videoMy").hide()
  28. $(".videoList").css({
  29. "height": document.documentElement.clientWidth * 0.9 / 1.6 + "px",
  30. // "width":"100%",
  31. // "margin-left": "0%"
  32. })
  33. }
  34. function resScreen() {
  35. console.log("resScreen")
  36. $(".titText").show();
  37. $(".callVideo").show();
  38. $(".videoList").css({
  39. "height": document.documentElement.clientWidth * 0.9 / 1.6 + "px",
  40. // "width":"80%",
  41. // "margin-left": "10%"
  42. })
  43. }
  44. //开启本地摄像头
  45. function captureLocalMediaVideo() {
  46. navigator.getUserMedia({
  47. video: true,
  48. audio: false
  49. }, function(stream) {
  50. nativeStream = stream;
  51. myVideoView.srcObject = stream;
  52. }, function(e) {
  53. if(e.name != "NotReadableError") {
  54. alert('getUserMedia() error: ' + e.name);
  55. }
  56. });
  57. }
  58. //获取本地媒体流
  59. function localMediaStream() {
  60. navigator.getUserMedia({
  61. video: true,
  62. audio: true
  63. }, function(stream) {
  64. localStream = stream;
  65. if(callVideoState) {
  66. sipCallVideo();
  67. }
  68. }, function(e) {
  69. if(e.name != "NotReadableError") {
  70. alert('getUserMedia() error: ' + e.name);
  71. }
  72. });
  73. }
  74. // 关闭摄像头
  75. function closeMediaVideo() {
  76. //if(nativeStream){
  77. // nativeStream.getTracks().forEach(function(track) {
  78. // track.stop();
  79. // });
  80. //}
  81. if(localStream) {
  82. localStream.getTracks().forEach(function(track) {
  83. track.stop();
  84. });
  85. }
  86. }
  87. function testStart() {
  88. sip_uri_ = "sip:" + selectExten + "@"+huayi.config.sip_ip; // 12345sp.zwfw.anyang.gov.cn
  89. // sip_password_ = huayi.config.sip_password_; //zhumadian12345800100
  90. // sip_password_ = 1012; //zhumadian12345800100
  91. sip_password_ = "123456"; //zhumadian12345800100
  92. ws_uri_ = huayi.config.ws_uri_
  93. console.info("get input info: sip_uri = ", sip_uri_, " sip_password = ", sip_password_, " ws_uri = ", ws_uri_);
  94. var socket = new JsSIP.WebSocketInterface(ws_uri_);
  95. var configuration = {
  96. sockets: [socket],
  97. outbound_proxy_set: ws_uri_,
  98. uri: sip_uri_, //与用户代理关联的SIP URI(字符串)。这是您的提供商提供给您的SIP地址
  99. password: sip_password_, //SIP身份验证密码
  100. contact_uri: 'sip:' + selectExten + huayi.config.contact_uri,
  101. register: true, //指示启动时JsSIP用户代理是否应自动注册
  102. session_timers: false, //启用会话计时器(根据RFC 4028)
  103. };
  104. userAgent = new JsSIP.UA(configuration);
  105. // JsSIP.debug.enable('JsSIP:*');
  106. JsSIP.debug.disable('JsSIP:*');
  107. //成功注册成功,data:Response JsSIP.IncomingResponse收到的SIP 2XX响应的实例
  108. userAgent.on('registered', function(data) {
  109. console.info("registered: ", data.response.status_code, ",", data.response.reason_phrase);
  110. $(".titText").show();
  111. $(".titText").html(selectExten + "注册成功");
  112. captureLocalMediaVideo()
  113. });
  114. //由于注册失败而被解雇,data:Response JsSIP.IncomingResponse接收到的SIP否定响应的实例,如果失败是由这样的响应的接收产生的,否则为空
  115. userAgent.on('registrationFailed', function(data) {
  116. console.log("registrationFailed, ", data);
  117. //console.warn("registrationFailed, ", data.response.status_code, ",", data.response.reason_phrase, " cause - ", data.cause);
  118. });
  119. //1.在注册到期之前发射几秒钟。如果应用程序没有为这个事件设置任何监听器,JsSIP将像往常一样重新注册。
  120. userAgent.on('registrationExpiring', function() {
  121. //==console.warn("registrationExpiring");
  122. });
  123. //为传入或传出会话/呼叫激发。data:
  124. userAgent.on('newRTCSession', function(data) {
  125. //console.info('onNewRTCSession: ', data);
  126. console.info('onNewRTCSession: ', data);
  127. var originator = data.originator;
  128. var session = data.session;
  129. var request = data.request;
  130. if(data.session._direction == "outgoing") {
  131. sipCallRTCSession(data, 1)
  132. outgoingSession = data.session;
  133. outgoingSession.on('connecting', function(data) {
  134. currentSession = outgoingSession;
  135. outgoingSession = null;
  136. });
  137. }
  138. if(data.originator == "remote") {
  139. incomingSession = data.session
  140. sipCallRTCSession(data, 2)
  141. console.info("incomingSession, answer the call");
  142. incomingSession = data.session;
  143. $(".videoBtn").show();
  144. $(".videoCall").show();
  145. $(".vidDrop").hide();
  146. $(".videoList").hide();
  147. $(".videoMy").hide();
  148. $('.maxOpen').trigger("click");
  149. if(!nativeStream) {
  150. captureLocalMediaVideo()
  151. }
  152. timeOut = setInterval(function() {
  153. if(incomingSession.isEnded()) {
  154. $(".videoBtn").hide();
  155. $(".videoCall").hide();
  156. clearTimeout(timeOut)
  157. }
  158. }, 2000)
  159. localMediaStream()
  160. // sipIncomingRTCSession(incomingSession) confirmed
  161. }
  162. //接受呼叫时激发
  163. data.session.on('accepted', function(data) {
  164. console.info("3");
  165. if(data.originator == 'remote' && currentSession == null) {
  166. currentSession = incomingSession;
  167. incomingSession = null;
  168. }
  169. });
  170. //确认呼叫后激发
  171. data.session.on('confirmed', function(data) {
  172. console.info("4");
  173. $(".callStyle").text("连接中");
  174. if(data.originator == 'remote' && currentSession == null) {
  175. currentSession = incomingSession;
  176. incomingSession = null;
  177. }
  178. });
  179. //在将远程SDP传递到RTC引擎之前以及在发送本地SDP之前激发。此事件提供了修改传入和传出SDP的机制。
  180. data.session.on('sdp', function(data) {
  181. console.info("5");
  182. });
  183. //接收或生成对邀请请求的1XX SIP类响应(>100)时激发。该事件在SDP处理之前触发(如果存在),以便在需要时对其进行微调,甚至通过删除数据对象中响应参数的主体来删除它
  184. data.session.on('progress', function(data) {
  185. console.info("6");
  186. if(data.originator == 'remote') {}
  187. });
  188. });
  189. //为传入或传出消息请求激发。data:
  190. userAgent.on('newMessage', function(data) {
  191. console.info("8");
  192. if(data.originator == 'local') {} else {}
  193. });
  194. //连接到信令服务器,并恢复以前的状态,如果以前停止。重新开始时,如果UA配置中的参数设置为register:true,则向SIP域注册。
  195. userAgent.start();
  196. setInterval(function() {
  197. userAgent.register();
  198. }, 30 * 1000)
  199. }
  200. var eventHandlers = {
  201. 'progress': function(e) {
  202. console.log('call is in progress');
  203. },
  204. 'failed': function(e) {
  205. console.log('call failed: ', e);
  206. if(callVideoFail) {
  207. setTimeout(function() {
  208. videoCall()
  209. }, 2000)
  210. }
  211. },
  212. 'ended': function(e) {
  213. console.log('call ended : ', e);
  214. dropCall();
  215. },
  216. 'confirmed': function(e) {
  217. console.log('call confirmed');
  218. }
  219. };
  220. // 打电话
  221. function sipCallRTCSession(e, state) {
  222. console.log(e.session)
  223. console.log(state) // 1是呼出,2是呼入
  224. e.session.on("confirmed", function(data) {
  225. console.log("confirmed")
  226. console.log(data)
  227. callVideoFail = false;
  228. if(e.session.connection.getReceivers) {
  229. console.log(e.session.connection.getReceivers())
  230. remoteStream = new MediaStream();
  231. e.session.connection.getReceivers().forEach(element => {
  232. // track可能一个音轨或者视频轨迹
  233. remoteStream.addTrack(element.track)
  234. })
  235. console.log(remoteStream)
  236. if(inComing) {
  237. inComing = false;
  238. videoView_1.srcObject = remoteStream
  239. videoView_1.onloadedmetadata = function() {
  240. videoView_1.play();
  241. videoView_1.muted = true;
  242. state === 1 ? console.log("呼叫成功") : console.log("接听成功")
  243. $(".callStyle").text("通话中");
  244. $(".videoCall").show();
  245. $(".videoMy").show();
  246. $(".videoBtn").hide();
  247. $(".videoList").show();
  248. $(".vidDrop").show();
  249. }
  250. }
  251. }
  252. })
  253. }
  254. //视频呼叫
  255. function audioCall(callPhone) {
  256. if($("#numberCall").val() < 1005 || $("#numberCall").val() > 2080) {
  257. alert("账号不正确,请重新输入")
  258. } else {
  259. inComing = true
  260. callVideoState = true;
  261. localMediaStream();
  262. if(!nativeStream) {
  263. captureLocalMediaVideo()
  264. }
  265. // $(".videoCall").show();
  266. $(".callStyle").text("连接中");
  267. }
  268. }
  269. function sipCallVideo() {
  270. callVideoFail = true;
  271. if(localStream) {
  272. var sip_phone_number_ = $("#numberCall").val().toString();
  273. var options = {
  274. 'eventHandlers': eventHandlers,
  275. 'mediaConstraints': {
  276. 'audio': true,
  277. 'video': false
  278. },
  279. 'mediaStream': localStream
  280. };
  281. callVideoState = false;
  282. outgoingSession = userAgent.call(sip_phone_number_, options);
  283. }
  284. }
  285. //接听
  286. function answerCall() {
  287. $(".callStyle").text("连接中")
  288. $(".videoCall").hide();
  289. clearTimeout(timeOut)
  290. inComing = true;
  291. if(incomingSession) {
  292. incomingSession.answer({
  293. 'mediaConstraints': {
  294. 'audio': true,
  295. 'video': true
  296. },
  297. 'mediaStream': localStream
  298. });
  299. incomingSession = null;
  300. }
  301. }
  302. //注销
  303. function unReg() {
  304. console.log('注销----------->');
  305. userAgent.unregister(true);
  306. }
  307. //挂断
  308. function dropCall() {
  309. userAgent.terminateSessions();
  310. $(".callStyle").text("")
  311. resScreen()
  312. closeMediaVideo();
  313. $(".videoCall").hide();
  314. }