github.com/pion/webrtc/v3@v3.2.24/examples/reflect/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      pc.createOffer().then(d => pc.setLocalDescription(d)).catch(log)
    21    }).catch(log)
    22  
    23  pc.oniceconnectionstatechange = e => log(pc.iceConnectionState)
    24  pc.onicecandidate = event => {
    25    if (event.candidate === null) {
    26      document.getElementById('localSessionDescription').value = btoa(JSON.stringify(pc.localDescription))
    27    }
    28  }
    29  pc.ontrack = function (event) {
    30    const el = document.createElement(event.track.kind)
    31    el.srcObject = event.streams[0]
    32    el.autoplay = true
    33    el.controls = true
    34  
    35    document.getElementById('remoteVideos').appendChild(el)
    36  }
    37  
    38  window.startSession = () => {
    39    const sd = document.getElementById('remoteSessionDescription').value
    40    if (sd === '') {
    41      return alert('Session Description must not be empty')
    42    }
    43  
    44    try {
    45      pc.setRemoteDescription(JSON.parse(atob(sd)))
    46    } catch (e) {
    47      alert(e)
    48    }
    49  }
    50  
    51  window.copySDP = () => {
    52    const browserSDP = document.getElementById('localSessionDescription')
    53  
    54    browserSDP.focus()
    55    browserSDP.select()
    56  
    57    try {
    58      const successful = document.execCommand('copy')
    59      const msg = successful ? 'successful' : 'unsuccessful'
    60      log('Copying SDP was ' + msg)
    61    } catch (err) {
    62      log('Unable to copy SDP ' + err)
    63    }
    64  }