github.com/pion/webrtc/v4@v4.0.1/examples/rtp-forwarder/jsfiddle/demo.js (about)

     1  /* eslint-env browser */
     2  
     3  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     4  // SPDX-License-Identifier: MIT
     5  
     6  const pc = new RTCPeerConnection({
     7    iceServers: [
     8      {
     9        urls: 'stun:stun.l.google.com:19302'
    10      }
    11    ]
    12  })
    13  const log = msg => {
    14    document.getElementById('logs').innerHTML += msg + '<br>'
    15  }
    16  
    17  navigator.mediaDevices.getUserMedia({ video: true, audio: true })
    18    .then(stream => {
    19      stream.getTracks().forEach(track => pc.addTrack(track, stream))
    20      document.getElementById('video1').srcObject = stream
    21      pc.createOffer().then(d => pc.setLocalDescription(d)).catch(log)
    22    }).catch(log)
    23  
    24  pc.oniceconnectionstatechange = e => log(pc.iceConnectionState)
    25  pc.onicecandidate = event => {
    26    if (event.candidate === null) {
    27      document.getElementById('localSessionDescription').value = btoa(JSON.stringify(pc.localDescription))
    28    }
    29  }
    30  
    31  window.startSession = () => {
    32    const sd = document.getElementById('remoteSessionDescription').value
    33    if (sd === '') {
    34      return alert('Session Description must not be empty')
    35    }
    36  
    37    try {
    38      pc.setRemoteDescription(JSON.parse(atob(sd)))
    39    } catch (e) {
    40      alert(e)
    41    }
    42  }
    43  
    44  window.copySDP = () => {
    45    const browserSDP = document.getElementById('localSessionDescription')
    46  
    47    browserSDP.focus()
    48    browserSDP.select()
    49  
    50    try {
    51      const successful = document.execCommand('copy')
    52      const msg = successful ? 'successful' : 'unsuccessful'
    53      log('Copying SDP was ' + msg)
    54    } catch (err) {
    55      log('Unable to copy SDP ' + err)
    56    }
    57  }