Aucune description

pcSip备份.js 9.2KB

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