Açıklama Yok

pcSip备份.js 10.0KB

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