You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
8.7KB

  1. <html>
  2. <meta charset="utf-8">
  3. <head>
  4. <title>ZLM RTC demo</title>
  5. <script src="./ZLMRTCClient.js"></script>
  6. </head>
  7. <body>
  8. <div style="text-align: center;">
  9. <div>
  10. <video id='video' controls autoplay style="text-align:left;">
  11. Your browser is too old which doesn't support HTML5 video.
  12. </video>
  13. <video id='selfVideo' controls autoplay style="text-align:right;">
  14. Your browser is too old which doesn't support HTML5 video.
  15. </video>
  16. </div>
  17. <div>
  18. <p>
  19. <label for="streamUrl">url:</label>
  20. <input type="text" style="co" id='streamUrl' value="http://192.168.1.101/index/api/webrtc?app=live&stream=xiong&type=play">
  21. </p>
  22. <p>
  23. <label for="simulcast">simulcast:</label>
  24. <input type="checkbox" id='simulcast'>
  25. </p>
  26. <p>
  27. <label for="useCamera">useCamera:</label>
  28. <input type="checkbox" id='useCamera' checked="checked">
  29. </p>
  30. <p>
  31. <label for="audioEnable">audioEnable:</label>
  32. <input type="checkbox" id='audioEnable' checked="checked">
  33. </p>
  34. <p>
  35. <label for="videoEnable">videoEnable:</label>
  36. <input type="checkbox" id='videoEnable' checked="checked">
  37. </p>
  38. <p>
  39. <label for="method">method(play or push or echo):</label>
  40. <input type="radio" name="method" value="echo" >echo
  41. <input type="radio" name="method" value="push" >push
  42. <input type="radio" name="method" value="play" checked = true>play
  43. </p>
  44. <p>
  45. <label for="resolution">resolution:</label>
  46. <select id="resolution">
  47. </select>
  48. </p>
  49. <p>
  50. <label for="datachannel">datachannel:</label>
  51. <input id='datachannel' name="datachannel" type="checkbox" value="0">
  52. </p>
  53. <button onclick="start()">开始(start)</button>
  54. <button onclick="stop()">停止(stop)</button>
  55. <p>
  56. <label for="msgsend">msgsend:</label>
  57. <input type="text" id='msgsend' value="hello word !">
  58. </p>
  59. <p>
  60. <label for="msgrecv">msgrecv:</label>
  61. <input type="text" id='msgrecv' disabled>
  62. </p>
  63. <button onclick="send()">发送(send by datachannel)</button>
  64. <button onclick="close()">关闭(close datachannel)</button>
  65. </div>
  66. </div>
  67. <script>
  68. var player = null
  69. var recvOnly = true
  70. var resArr = []
  71. var ishttps = 'https:' == document.location.protocol ? true : false
  72. var isLocal = "file:" == document.location.protocol ? true : false
  73. var url = document.location.protocol+"//"+window.location.host+"/index/api/webrtc?app=live&stream=test&type=play"
  74. if(!ishttps && !isLocal){
  75. alert('本demo需要在https的网站访问 ,如果你要推流的话(this demo must access in site of https if you want push stream)')
  76. }
  77. if(isLocal){
  78. url = "http://127.0.0.1"+"/index/api/webrtc?app=live&stream=test&type=play"
  79. }
  80. document.getElementById('streamUrl').value = url
  81. document.getElementsByName("method").forEach((el,idx)=>{
  82. el.onclick=function(e){
  83. let url = new URL(document.getElementById('streamUrl').value);
  84. url.searchParams.set("type",el.value)
  85. document.getElementById('streamUrl').value = url.toString()
  86. if(el.value == "play"){
  87. recvOnly = true;
  88. }else if(el.value == "echo"){
  89. recvOnly = false;
  90. }else{
  91. recvOnly = false;
  92. }
  93. };
  94. });
  95. ZLMRTCClient.GetAllScanResolution().forEach((r,i)=>{
  96. opt = document.createElement('option');
  97. opt.text = r.label +"("+r.width+"x"+r.height+")";
  98. opt.value = r;
  99. document.getElementById("resolution").add(opt,null)
  100. //console.log(opt.text.match(/\d+/g))
  101. })
  102. function start_play(){
  103. let elr = document.getElementById("resolution");
  104. let res = elr.options[elr.selectedIndex].text.match(/\d+/g);
  105. let h = parseInt(res.pop());
  106. let w = parseInt(res.pop());
  107. player = new ZLMRTCClient.Endpoint(
  108. {
  109. element: document.getElementById('video'),// video 标签
  110. debug: true,// 是否打印日志
  111. zlmsdpUrl:document.getElementById('streamUrl').value,//流地址
  112. simulcast:document.getElementById('simulcast').checked,
  113. useCamera:document.getElementById('useCamera').checked,
  114. audioEnable:document.getElementById('audioEnable').checked,
  115. videoEnable:document.getElementById('videoEnable').checked,
  116. recvOnly:recvOnly,
  117. resolution:{w:w,h:h},
  118. usedatachannel:document.getElementById('datachannel').checked,
  119. }
  120. );
  121. player.on(ZLMRTCClient.Events.WEBRTC_ICE_CANDIDATE_ERROR,function(e)
  122. {// ICE 协商出错
  123. console.log('ICE 协商出错')
  124. });
  125. player.on(ZLMRTCClient.Events.WEBRTC_ON_REMOTE_STREAMS,function(e)
  126. {//获取到了远端流,可以播放
  127. console.log('播放成功',e.streams)
  128. });
  129. player.on(ZLMRTCClient.Events.WEBRTC_OFFER_ANWSER_EXCHANGE_FAILED,function(e)
  130. {// offer anwser 交换失败
  131. console.log('offer anwser 交换失败',e)
  132. stop();
  133. });
  134. player.on(ZLMRTCClient.Events.WEBRTC_ON_LOCAL_STREAM,function(s)
  135. {// 获取到了本地流
  136. document.getElementById('selfVideo').srcObject=s;
  137. document.getElementById('selfVideo').muted = true;
  138. //console.log('offer anwser 交换失败',e)
  139. });
  140. player.on(ZLMRTCClient.Events.CAPTURE_STREAM_FAILED,function(s)
  141. {// 获取本地流失败
  142. console.log('获取本地流失败')
  143. });
  144. player.on(ZLMRTCClient.Events.WEBRTC_ON_CONNECTION_STATE_CHANGE,function(state)
  145. {// RTC 状态变化 ,详情参考 https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/connectionState
  146. console.log('当前状态==>',state)
  147. });
  148. player.on(ZLMRTCClient.Events.WEBRTC_ON_DATA_CHANNEL_OPEN,function(event)
  149. {
  150. console.log('rtc datachannel 打开 :',event)
  151. });
  152. player.on(ZLMRTCClient.Events.WEBRTC_ON_DATA_CHANNEL_MSG,function(event)
  153. {
  154. console.log('rtc datachannel 消息 :',event.data)
  155. document.getElementById('msgrecv').value = event.data
  156. });
  157. player.on(ZLMRTCClient.Events.WEBRTC_ON_DATA_CHANNEL_ERR,function(event)
  158. {
  159. console.log('rtc datachannel 错误 :',event)
  160. });
  161. player.on(ZLMRTCClient.Events.WEBRTC_ON_DATA_CHANNEL_CLOSE,function(event)
  162. {
  163. console.log('rtc datachannel 关闭 :',event)
  164. });
  165. }
  166. function start()
  167. {
  168. stop();
  169. let elr = document.getElementById("resolution");
  170. let res = elr.options[elr.selectedIndex].text.match(/\d+/g);
  171. let h = parseInt(res.pop());
  172. let w = parseInt(res.pop());
  173. if(document.getElementById('useCamera').checked && !recvOnly)
  174. {
  175. ZLMRTCClient.isSupportResolution(w,h).then(e=>{
  176. start_play()
  177. }).catch(e=>{
  178. alert("not support resolution")
  179. });
  180. }else{
  181. start_play()
  182. }
  183. }
  184. function stop()
  185. {
  186. if(player)
  187. {
  188. player.close();
  189. player = null;
  190. var remote = document.getElementById('video');
  191. if(remote)
  192. {
  193. remote.srcObject = null;
  194. remote.load();
  195. }
  196. var local = document.getElementById('selfVideo');
  197. local.srcObject = null;
  198. local.load();
  199. }
  200. }
  201. function send(){
  202. if(player){
  203. //send msg refernece https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/send
  204. player.sendMsg(document.getElementById('msgsend').value)
  205. }
  206. }
  207. function close(){
  208. if(player){
  209. player.closeDataChannel()
  210. }
  211. }
  212. </script>
  213. </body>
  214. <script>
  215. </script>
  216. </html>