main.html 1.6 KB

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