subscribe.html 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>测试WebRTC拉流</title>
  8. </head>
  9. <body>
  10. <video id="video" width="640" height="480" autoplay muted controls>
  11. </video>
  12. <!-- <button id="sw" onclick="action()" type="button" style="width:100px;height:30px;display: block;">unpublish</button> -->
  13. <pre>
  14. <code id="remoteSdp">
  15. </code>
  16. </pre>
  17. </body>
  18. <script>
  19. let action = () => { console.log('action not set'); };
  20. (async () => {
  21. const pc = new RTCPeerConnection();
  22. pc.ontrack = (e) => {
  23. console.log('ontrack', e);
  24. if (e.streams.length === 0) return;
  25. document.getElementById('video').srcObject = e.streams[0];
  26. document.getElementById('video').play();
  27. };
  28. pc.oniceconnectionstatechange = () => {
  29. console.log('oniceconnectionstatechange', pc.iceConnectionState);
  30. };
  31. pc.onicecandidate = (e) => {
  32. console.log('onicecandidate', e.candidate);
  33. };
  34. pc.addTransceiver('video', { direction: 'recvonly' });
  35. pc.addTransceiver('audio', { direction: 'recvonly' });
  36. // const dc = pc.createDataChannel('sdp');
  37. const offer = await pc.createOffer();
  38. await pc.setLocalDescription(offer);
  39. const searchParams = new URLSearchParams(location.search);
  40. const streamPath = searchParams.get('streamPath') || 'live/webrtc';
  41. searchParams.delete('streamPath')
  42. const result = await fetch(
  43. `/webrtc/play/${streamPath}${location.search?`?${searchParams.toString()}`:''}`,
  44. {
  45. method: 'POST',
  46. mode: 'cors',
  47. cache: 'no-cache',
  48. credentials: 'include',
  49. redirect: 'follow',
  50. referrerPolicy: 'no-referrer',
  51. headers: { 'Content-Type': 'application/sdp' },
  52. body: offer.sdp,
  53. },
  54. );
  55. const remoteSdp = await result.text();
  56. document.getElementById('remoteSdp').innerText = remoteSdp;
  57. await pc.setRemoteDescription(
  58. new RTCSessionDescription({ type: 'answer', sdp: remoteSdp }),
  59. );
  60. // dc.onmessage = async (e) => {
  61. // await pc.setRemoteDescription(
  62. // new RTCSessionDescription({ type: 'answer', sdp: e.data }),
  63. // );
  64. // };
  65. // const publish = async () => {
  66. // videoTransceiver.direction = 'sendonly';
  67. // audioTransceiver.direction = 'sendonly';
  68. // const offer = await pc.createOffer();
  69. // await pc.setLocalDescription(offer);
  70. // dc.send('1' + offer.sdp);
  71. // action = unpublish;
  72. // document.getElementById('sw').innerText = 'unpublish';
  73. // };
  74. // const unpublish = async () => {
  75. // videoTransceiver.direction = 'inactive';
  76. // audioTransceiver.direction = 'inactive';
  77. // const offer = await pc.createOffer();
  78. // await pc.setLocalDescription(offer);
  79. // dc.send('0' + offer.sdp);
  80. // action = publish;
  81. // document.getElementById('sw').innerText = 'publish';
  82. // };
  83. // action = unpublish;
  84. })()
  85. </script>
  86. </html>