demo.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. {
  8. urls: 'stun:stun.l.google.com:19302'
  9. }
  10. ]
  11. })
  12. pc.oniceconnectionstatechange = e => {
  13. console.debug('connection state change', pc.iceConnectionState)
  14. }
  15. pc.onicecandidate = event => {
  16. if (event.candidate === null) {
  17. document.getElementById('localSessionDescription').value = btoa(JSON.stringify(pc.localDescription))
  18. }
  19. }
  20. pc.onnegotiationneeded = e =>
  21. pc.createOffer().then(d => pc.setLocalDescription(d)).catch(console.error)
  22. pc.ontrack = event => {
  23. console.log('Got track event', event)
  24. document.getElementById('serverVideo').srcObject = new MediaStream([event.track])
  25. }
  26. const canvases = [
  27. document.getElementById('canvasOne'),
  28. document.getElementById('canvasTwo'),
  29. document.getElementById('canvasThree')
  30. ]
  31. // Firefox requires getContext to be invoked on an HTML Canvas Element
  32. // prior to captureStream
  33. const canvasContexts = canvases.map(c => c.getContext('2d'))
  34. // Capture canvas streams and add to peer conn
  35. const streams = canvases.map(c => c.captureStream())
  36. streams.forEach(stream => stream.getVideoTracks().forEach(track => pc.addTrack(track, stream)))
  37. // Start circles
  38. requestAnimationFrame(() => drawCircle(canvasContexts[0], '#006699', 0))
  39. requestAnimationFrame(() => drawCircle(canvasContexts[1], '#cf635f', 0))
  40. requestAnimationFrame(() => drawCircle(canvasContexts[2], '#46c240', 0))
  41. function drawCircle (ctx, color, angle) {
  42. // Background
  43. ctx.clearRect(0, 0, 200, 200)
  44. ctx.fillStyle = '#eeeeee'
  45. ctx.fillRect(0, 0, 200, 200)
  46. // Draw and fill in circle
  47. ctx.beginPath()
  48. const radius = 25 + 50 * Math.abs(Math.cos(angle))
  49. ctx.arc(100, 100, radius, 0, Math.PI * 2, false)
  50. ctx.closePath()
  51. ctx.fillStyle = color
  52. ctx.fill()
  53. // Call again
  54. requestAnimationFrame(() => drawCircle(ctx, color, angle + (Math.PI / 64)))
  55. }
  56. window.startSession = () => {
  57. const sd = document.getElementById('remoteSessionDescription').value
  58. if (sd === '') {
  59. return alert('Session Description must not be empty')
  60. }
  61. try {
  62. pc.setRemoteDescription(JSON.parse(atob(sd)))
  63. } catch (e) {
  64. alert(e)
  65. }
  66. }
  67. window.copySDP = () => {
  68. const browserSDP = document.getElementById('localSessionDescription')
  69. browserSDP.focus()
  70. browserSDP.select()
  71. try {
  72. const successful = document.execCommand('copy')
  73. const msg = successful ? 'successful' : 'unsuccessful'
  74. console.log('Copying SDP was ' + msg)
  75. } catch (err) {
  76. console.log('Unable to copy SDP ' + err)
  77. }
  78. }