github.com/pion/webrtc/v3@v3.2.24/examples/broadcast/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 log = msg => { 7 document.getElementById('logs').innerHTML += msg + '<br>' 8 } 9 10 window.createSession = isPublisher => { 11 const pc = new RTCPeerConnection({ 12 iceServers: [ 13 { 14 urls: 'stun:stun.l.google.com:19302' 15 } 16 ] 17 }) 18 pc.oniceconnectionstatechange = e => log(pc.iceConnectionState) 19 pc.onicecandidate = event => { 20 if (event.candidate === null) { 21 document.getElementById('localSessionDescription').value = btoa(JSON.stringify(pc.localDescription)) 22 } 23 } 24 25 if (isPublisher) { 26 navigator.mediaDevices.getUserMedia({ video: true, audio: false }) 27 .then(stream => { 28 stream.getTracks().forEach(track => pc.addTrack(track, stream)) 29 document.getElementById('video1').srcObject = stream 30 pc.createOffer() 31 .then(d => pc.setLocalDescription(d)) 32 .catch(log) 33 }).catch(log) 34 } else { 35 pc.addTransceiver('video') 36 pc.createOffer() 37 .then(d => pc.setLocalDescription(d)) 38 .catch(log) 39 40 pc.ontrack = function (event) { 41 const el = document.getElementById('video1') 42 el.srcObject = event.streams[0] 43 el.autoplay = true 44 el.controls = true 45 } 46 } 47 48 window.startSession = () => { 49 const sd = document.getElementById('remoteSessionDescription').value 50 if (sd === '') { 51 return alert('Session Description must not be empty') 52 } 53 54 try { 55 pc.setRemoteDescription(JSON.parse(atob(sd))) 56 } catch (e) { 57 alert(e) 58 } 59 } 60 61 window.copySDP = () => { 62 const browserSDP = document.getElementById('localSessionDescription') 63 64 browserSDP.focus() 65 browserSDP.select() 66 67 try { 68 const successful = document.execCommand('copy') 69 const msg = successful ? 'successful' : 'unsuccessful' 70 log('Copying SDP was ' + msg) 71 } catch (err) { 72 log('Unable to copy SDP ' + err) 73 } 74 } 75 76 const btns = document.getElementsByClassName('createSessionButton') 77 for (let i = 0; i < btns.length; i++) { 78 btns[i].style = 'display: none' 79 } 80 81 document.getElementById('signalingContainer').style = 'display: block' 82 }