demo.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* eslint-env browser */
  2. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  3. // SPDX-License-Identifier: MIT
  4. // Create peer conn
  5. const pc = new RTCPeerConnection({
  6. iceServers: [{
  7. urls: 'stun:stun.l.google.com:19302'
  8. }]
  9. })
  10. pc.oniceconnectionstatechange = (e) => {
  11. console.log('connection state change', pc.iceConnectionState)
  12. }
  13. pc.onicecandidate = (event) => {
  14. if (event.candidate === null) {
  15. document.getElementById('localSessionDescription').value = btoa(
  16. JSON.stringify(pc.localDescription)
  17. )
  18. }
  19. }
  20. pc.onnegotiationneeded = (e) =>
  21. pc
  22. .createOffer()
  23. .then((d) => pc.setLocalDescription(d))
  24. .catch(console.error)
  25. pc.ontrack = (event) => {
  26. console.log('Got track event', event)
  27. const video = document.createElement('video')
  28. video.srcObject = event.streams[0]
  29. video.autoplay = true
  30. video.width = '500'
  31. const label = document.createElement('div')
  32. label.textContent = event.streams[0].id
  33. document.getElementById('serverVideos').appendChild(label)
  34. document.getElementById('serverVideos').appendChild(video)
  35. }
  36. navigator.mediaDevices
  37. .getUserMedia({
  38. video: {
  39. width: {
  40. ideal: 4096
  41. },
  42. height: {
  43. ideal: 2160
  44. },
  45. frameRate: {
  46. ideal: 60,
  47. min: 10
  48. }
  49. },
  50. audio: false
  51. })
  52. .then((stream) => {
  53. document.getElementById('browserVideo').srcObject = stream
  54. pc.addTransceiver(stream.getVideoTracks()[0], {
  55. direction: 'sendonly',
  56. streams: [stream],
  57. sendEncodings: [
  58. // for firefox order matters... first high resolution, then scaled resolutions...
  59. {
  60. rid: 'f'
  61. },
  62. {
  63. rid: 'h',
  64. scaleResolutionDownBy: 2.0
  65. },
  66. {
  67. rid: 'q',
  68. scaleResolutionDownBy: 4.0
  69. }
  70. ]
  71. })
  72. pc.addTransceiver('video')
  73. pc.addTransceiver('video')
  74. pc.addTransceiver('video')
  75. })
  76. window.startSession = () => {
  77. const sd = document.getElementById('remoteSessionDescription').value
  78. if (sd === '') {
  79. return alert('Session Description must not be empty')
  80. }
  81. try {
  82. console.log('answer', JSON.parse(atob(sd)))
  83. pc.setRemoteDescription(JSON.parse(atob(sd)))
  84. } catch (e) {
  85. alert(e)
  86. }
  87. }
  88. window.copySDP = () => {
  89. const browserSDP = document.getElementById('localSessionDescription')
  90. browserSDP.focus()
  91. browserSDP.select()
  92. try {
  93. const successful = document.execCommand('copy')
  94. const msg = successful ? 'successful' : 'unsuccessful'
  95. console.log('Copying SDP was ' + msg)
  96. } catch (err) {
  97. console.log('Unable to copy SDP ' + err)
  98. }
  99. }