From: Ruben Beltran del Rio Date: Wed, 7 Oct 2020 19:40:15 +0000 (+0200) Subject: Add media and peer objects X-Git-Url: https://git.r.bdr.sh/rbdr/junction/commitdiff_plain/e2da0c51692b64318a0f1b8d888536ee632c53d6?ds=inline Add media and peer objects --- diff --git a/extension/media.js b/extension/media.js new file mode 100644 index 0000000..33c6fee --- /dev/null +++ b/extension/media.js @@ -0,0 +1,29 @@ +'use strict'; + +const internals = { + mediaStream: null +}; + +module.exports = { + async start() { + + internals.mediaStream = internals.mediaStream || await navigator.mediaDevices.getUserMedia({ + audio: true + }); + + return internals.mediaStream; + }, + + stop() { + + if (!internals.mediaStream) { + return; + } + + for (const track of internals.mediaStream.getAudioTracks()) { + track.stop(); + } + + internals.mediaStream = null; + } +}; diff --git a/extension/peers.js b/extension/peers.js new file mode 100644 index 0000000..41155d4 --- /dev/null +++ b/extension/peers.js @@ -0,0 +1,41 @@ +'use strict'; + +const internals = { + peers: {}, + + createAudioElement(source) { + + const audioElement = document.createElement('audio'); + audioElement.setAttribute('class', 'junction-call-audio'); + audioElement.src = source; + audioElement.autoplay = 'autoplay'; + document.querySelector('body').appendChild(audioElement); + + return audioElement; + } +}; + +module.exports = { + add(id, url) { + + internals.peers[id] && this.remove(id); + internals.peers[id] = internals.createAudioElement(url) + }, + + remove(id) { + + internals.peers[id] && internals.peers[id].remove(); + delete internals.peers[id]; + }, + + count() { + + return Object.keys(internals.peers).length; + }, + + reset() { + + internals.peers = {}; + document.querySelectorAll('.junction-call-audio').forEach((audioElement) => audioElement.remove()); + } +};