github.com/pion/webrtc/v4@v4.0.1/examples/rtcp-processing/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  navigator.mediaDevices.getUserMedia({ video: true, audio: true })
    32    .then(stream => {
    33      document.getElementById('video1').srcObject = stream
    34      stream.getTracks().forEach(track => pc.addTrack(track, stream))
    35  
    36      pc.createOffer().then(d => pc.setLocalDescription(d)).catch(log)
    37    }).catch(log)
    38  
    39  window.startSession = () => {
    40    const sd = document.getElementById('remoteSessionDescription').value
    41    if (sd === '') {
    42      return alert('Session Description must not be empty')
    43    }
    44  
    45    try {
    46      pc.setRemoteDescription(JSON.parse(atob(sd)))
    47    } catch (e) {
    48      alert(e)
    49    }
    50  }
    51  
    52  window.copySessionDescription = () => {
    53    const browserSessionDescription = document.getElementById('localSessionDescription')
    54  
    55    browserSessionDescription.focus()
    56    browserSessionDescription.select()
    57  
    58    try {
    59      const successful = document.execCommand('copy')
    60      const msg = successful ? 'successful' : 'unsuccessful'
    61      log('Copying SessionDescription was ' + msg)
    62    } catch (err) {
    63      log('Oops, unable to copy SessionDescription ' + err)
    64    }
    65  }