Нет описания

newSip.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. var yourVideoView = document.getElementById('yourVideo'); //对方视频信息
  2. var myVideoView = document.getElementById('myVideo'); //对方视频信息
  3. var localStream,videoInputDevices=[],currentVideoIndex = 0
  4. $("#startBtn").attr("disabled",false) //注册
  5. $("#logoutBtn").attr("disabled",true) //注销
  6. $("#videoCall").attr("disabled",true) //视频呼叫
  7. $("#answerCall").attr("disabled",true) //接听
  8. $("#hangupCall").attr("disabled",true) //挂断
  9. $(function() {
  10. mediumInfo()
  11. })
  12. // 初始化
  13. $("#startBtn").click(function(){
  14. testStart()
  15. localMediaStream()
  16. //loginTrue()
  17. })
  18. // 注销
  19. $("#logoutBtn").click(function(){
  20. // logoutTrue()
  21. unregisterSip()
  22. })
  23. // 视频呼叫
  24. $("#videoCall").click(function(){
  25. videoCall()
  26. // videoCallTrue()
  27. })
  28. // 接听
  29. $("#answerCall").click(function(){
  30. console.log(localStream)
  31. sipIncomingRTCSession(incomingSession)
  32. // answerCallTrue()
  33. })
  34. // 挂断
  35. $("#hangupCall").click(function(){
  36. hangupCall()
  37. // userAgent.terminateSessions();
  38. // hangupCallTrue()
  39. })
  40. function logout(){
  41. //注销
  42. }
  43. // 打电话
  44. function sipCallRTCSession(e,state){
  45. console.log(e.session)
  46. console.log(state) // 1是呼出,2是呼入
  47. e.session.on("confirmed", function(data){
  48. console.log("confirmed")
  49. console.log(data)
  50. if(e.session.connection.getReceivers){
  51. console.log(e.session.connection.getReceivers())
  52. remoteStream = new MediaStream();
  53. e.session.connection.getReceivers().forEach(element => {
  54. // track可能一个音轨或者视频轨迹
  55. remoteStream.addTrack(element.track)
  56. })
  57. // console.log(remoteStream) confirmed
  58. yourVideoView.srcObject = remoteStream
  59. yourVideoView.onloadedmetadata = function() {
  60. yourVideoView.play();
  61. yourVideoView.muted = true;
  62. inCallTrue()
  63. state===1?console.log("呼叫成功"):console.log("接听成功")
  64. }
  65. }
  66. })
  67. }
  68. // 来电
  69. function sipIncomingRTCSession(incomingSession){
  70. incomingSession.answer({
  71. 'mediaConstraints': {
  72. 'audio': true,
  73. 'video': true
  74. },
  75. 'mediaStream': localStream
  76. });
  77. incomingSession = null
  78. }
  79. //获取摄像头,音频设备信息
  80. function mediumInfo() {
  81. navigator.mediaDevices.enumerateDevices()
  82. .then(gotDevices).catch(handleError);
  83. function gotDevices(deviceInfos) {
  84. deviceInfos.forEach(function(n) {
  85. if (n.kind === 'videoinput') {
  86. videoInputDevices.push(n);
  87. }
  88. })
  89. }
  90. function handleError(error) {
  91. //alert(JSON.stringify(error));
  92. }
  93. }
  94. //获取本地媒体流
  95. function localMediaStream() {
  96. console.log(videoInputDevices)
  97. // if(!videoInputDevices[0].deviceId){
  98. // alert('获取摄像头失败,请重试');
  99. // return
  100. // }
  101. navigator.mediaDevices.getUserMedia({
  102. video: {
  103. deviceId: videoInputDevices[currentVideoIndex].deviceId
  104. },
  105. audio: true
  106. }).then((stream)=>{
  107. localStream = stream;
  108. console.log(localStream)
  109. myVideoView.srcObject = localStream
  110. myVideoView.onloadedmetadata = function() {
  111. myVideoView.play();
  112. myVideoView.muted = true
  113. }
  114. }).catch((res)=>{
  115. alert('getUserMedia() error: ' + res.name);
  116. })
  117. }
  118. // 注册成功
  119. function loginTrue(){
  120. console.log("初始化成功");
  121. $("#startBtn").attr("disabled",true) //注册
  122. $("#logoutBtn").attr("disabled",false) //注销
  123. $("#videoCall").attr("disabled",false) //视频呼叫
  124. $("#answerCall").attr("disabled",true) //接听
  125. $("#hangupCall").attr("disabled",true) //挂断
  126. }
  127. // 注销成功
  128. function logoutTrue(){
  129. console.log("注销成功");
  130. $("#startBtn").attr("disabled",false) //注册
  131. $("#logoutBtn").attr("disabled",true) //注销
  132. $("#videoCall").attr("disabled",true) //视频呼叫
  133. $("#answerCall").attr("disabled",true) //接听
  134. $("#hangupCall").attr("disabled",true) //挂断
  135. }
  136. // 来电振铃
  137. function incomingTrue(){
  138. console.log("接听成功");
  139. $("#startBtn").attr("disabled",true) //注册
  140. $("#logoutBtn").attr("disabled",true) //注销
  141. $("#videoCall").attr("disabled",true) //视频呼叫
  142. $("#answerCall").attr("disabled",false) //接听
  143. $("#hangupCall").attr("disabled",false) //挂断
  144. }
  145. // 通话中
  146. function inCallTrue(){
  147. $("#startBtn").attr("disabled",true) //注册
  148. $("#logoutBtn").attr("disabled",true) //注销
  149. $("#videoCall").attr("disabled",true) //视频呼叫
  150. $("#answerCall").attr("disabled",true) //接听
  151. $("#hangupCall").attr("disabled",false) //挂断
  152. }
  153. // 挂断成功
  154. function hangupCallTrue(){
  155. console.log("挂断成功");
  156. $("#startBtn").attr("disabled",true) //注册
  157. $("#logoutBtn").attr("disabled",false) //注销
  158. $("#videoCall").attr("disabled",false) //视频呼叫
  159. $("#answerCall").attr("disabled",true) //接听
  160. $("#hangupCall").attr("disabled",true) //挂断
  161. }