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