main.html 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>libdatachannel media example</title>
  6. </head>
  7. <body>
  8. <p>Please enter the offer provided to you by the application: </p>
  9. <textarea cols="50" rows="50"></textarea>
  10. <button>Submit</button>
  11. <script>
  12. document.querySelector('button').addEventListener('click', async () => {
  13. let offer = JSON.parse(document.querySelector('textarea').value);
  14. rtc = new RTCPeerConnection({
  15. // Recommended for libdatachannel
  16. bundlePolicy: "max-bundle",
  17. });
  18. rtc.onicegatheringstatechange = (state) => {
  19. if (rtc.iceGatheringState === 'complete') {
  20. // We only want to provide an answer once all of our candidates have been added to the SDP.
  21. let answer = rtc.localDescription;
  22. document.querySelector('textarea').value = JSON.stringify({"type": answer.type, sdp: answer.sdp});
  23. document.querySelector('p').value = 'Please paste the answer in the application.';
  24. alert('Please paste the answer in the application.');
  25. }
  26. }
  27. await rtc.setRemoteDescription(offer);
  28. let media = await navigator.mediaDevices.getUserMedia({
  29. video: {
  30. width: 1280,
  31. height: 720
  32. }
  33. });
  34. media.getTracks().forEach(track => rtc.addTrack(track, media));
  35. let answer = await rtc.createAnswer();
  36. await rtc.setLocalDescription(answer);
  37. })
  38. </script>
  39. </body>
  40. </html>