index.html 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Answer</title>
  6. <style>
  7. #v2 {
  8. width: 500px;
  9. height: 500px;
  10. border: solid 1px red;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <video id="v2" autoPlay muted></video>
  16. <script>
  17. (async () => {
  18. const video = document.getElementById('v2')
  19. const socket = new WebSocket("ws://172.16.101.112:8888");
  20. await new Promise((r) => (socket.onopen = r));
  21. console.log("open websocket");
  22. const offer = await new Promise((r) => (socket.onmessage = (ev) => r(JSON.parse(ev.data))));
  23. console.log("offer", offer.sdp);
  24. const peer = new RTCPeerConnection({
  25. iceServers: [
  26. // {
  27. // urls: [
  28. // "stun:caner.top:3478",
  29. // "stun:stun.l.google.com:19302",
  30. // "stun:stun1.l.google.com:19302",
  31. // "stun:stun2.l.google.com:19302",
  32. // "stun:stun3.l.google.com:19302",
  33. // ],
  34. // },
  35. // {
  36. // urls: "turn:caner.top:3478",
  37. // username: "admin",
  38. // credential: "123456",
  39. // },
  40. ],
  41. });
  42. peer.onicecandidate = ({ candidate }) => {
  43. if (!candidate) {
  44. const sdp = JSON.stringify(peer.localDescription);
  45. socket.send(sdp);
  46. }
  47. };
  48. peer.oniceconnectionstatechange = () => {
  49. console.log("oniceconnectionstatechange", peer.iceConnectionState);
  50. };
  51. peer.ontrack = (e) => {
  52. console.log("ontrack", e);
  53. video.srcObject = e.streams[0];
  54. };
  55. await peer.setRemoteDescription(offer);
  56. const answer = await peer.createAnswer();
  57. await peer.setLocalDescription(answer);
  58. })()
  59. </script>
  60. </body>
  61. </html>