github.com/pion/webrtc/v3@v3.2.24/examples/play-from-disk/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      urls: 'stun:stun.l.google.com:19302'
     9    }]
    10  })
    11  const log = msg => {
    12    document.getElementById('div').innerHTML += msg + '<br>'
    13  }
    14  
    15  pc.ontrack = function (event) {
    16    const el = document.createElement(event.track.kind)
    17    el.srcObject = event.streams[0]
    18    el.autoplay = true
    19    el.controls = true
    20  
    21    document.getElementById('remoteVideos').appendChild(el)
    22  }
    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  // Offer to receive 1 audio, and 1 video track
    32  pc.addTransceiver('video', {
    33    direction: 'sendrecv'
    34  })
    35  pc.addTransceiver('audio', {
    36    direction: 'sendrecv'
    37  })
    38  
    39  pc.createOffer().then(d => pc.setLocalDescription(d)).catch(log)
    40  
    41  window.startSession = () => {
    42    const sd = document.getElementById('remoteSessionDescription').value
    43    if (sd === '') {
    44      return alert('Session Description must not be empty')
    45    }
    46  
    47    try {
    48      pc.setRemoteDescription(JSON.parse(atob(sd)))
    49    } catch (e) {
    50      alert(e)
    51    }
    52  }
    53  
    54  window.copySessionDescription = () => {
    55    const browserSessionDescription = document.getElementById('localSessionDescription')
    56  
    57    browserSessionDescription.focus()
    58    browserSessionDescription.select()
    59  
    60    try {
    61      const successful = document.execCommand('copy')
    62      const msg = successful ? 'successful' : 'unsuccessful'
    63      log('Copying SessionDescription was ' + msg)
    64    } catch (err) {
    65      log('Oops, unable to copy SessionDescription ' + err)
    66    }
    67  }