From 247da28ed9a856b4ca9d91422a086683f188f6b8 Mon Sep 17 00:00:00 2001 From: Amy Gleixner Date: Thu, 17 Dec 2020 21:48:13 -0800 Subject: [PATCH] condensing IceCandidate collection demo mentions a function that does this, yet the solution has the code duplicated in both the createRoom and joinRoomById methods. I consolidated those into one method, then had each aforementioned method call the new one with the corresponding strings. Also made those strings constants, since they're being used in three separate places. --- public/app.js | 84 +++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 49 deletions(-) diff --git a/public/app.js b/public/app.js index 4e3b4d6..121238e 100644 --- a/public/app.js +++ b/public/app.js @@ -12,6 +12,9 @@ const configuration = { iceCandidatePoolSize: 10, }; +const callerCandidatesString = 'callerCandidates'; +const calleeCandidatesString = 'calleeCandidates'; + let peerConnection = null; let localStream = null; let remoteStream = null; @@ -41,18 +44,8 @@ async function createRoom() { peerConnection.addTrack(track, localStream); }); - // Code for collecting ICE candidates below - const callerCandidatesCollection = roomRef.collection('callerCandidates'); - - peerConnection.addEventListener('icecandidate', event => { - if (!event.candidate) { - console.log('Got final candidate!'); - return; - } - console.log('Got candidate: ', event.candidate); - callerCandidatesCollection.add(event.candidate.toJSON()); - }); - // Code for collecting ICE candidates above + // Uncomment to collect ICE candidates below + await collectIceCandidates(roomRef, peerConnection, callerCandidatesString, calleeCandidatesString); // Code for creating a room below const offer = await peerConnection.createOffer(); @@ -90,18 +83,6 @@ async function createRoom() { } }); // Listening for remote session description above - - // Listen for remote ICE candidates below - roomRef.collection('calleeCandidates').onSnapshot(snapshot => { - snapshot.docChanges().forEach(async change => { - if (change.type === 'added') { - let data = change.doc.data(); - console.log(`Got new remote ICE candidate: ${JSON.stringify(data)}`); - await peerConnection.addIceCandidate(new RTCIceCandidate(data)); - } - }); - }); - // Listen for remote ICE candidates above } function joinRoom() { @@ -133,17 +114,8 @@ async function joinRoomById(roomId) { peerConnection.addTrack(track, localStream); }); - // Code for collecting ICE candidates below - const calleeCandidatesCollection = roomRef.collection('calleeCandidates'); - peerConnection.addEventListener('icecandidate', event => { - if (!event.candidate) { - console.log('Got final candidate!'); - return; - } - console.log('Got candidate: ', event.candidate); - calleeCandidatesCollection.add(event.candidate.toJSON()); - }); - // Code for collecting ICE candidates above + // Uncomment to collect ICE candidates below + await collectIceCandidates(roomRef, peerConnection, calleeCandidatesString, callerCandidatesString); peerConnection.addEventListener('track', event => { console.log('Got remote track:', event.streams[0]); @@ -169,21 +141,35 @@ async function joinRoomById(roomId) { }; await roomRef.update(roomWithAnswer); // Code for creating SDP answer above - - // Listening for remote ICE candidates below - roomRef.collection('callerCandidates').onSnapshot(snapshot => { - snapshot.docChanges().forEach(async change => { - if (change.type === 'added') { - let data = change.doc.data(); - console.log(`Got new remote ICE candidate: ${JSON.stringify(data)}`); - await peerConnection.addIceCandidate(new RTCIceCandidate(data)); - } - }); - }); - // Listening for remote ICE candidates above } } +// collect ICE Candidates function below + async function collectIceCandidates(roomRef, peerConnection, + localName, remoteName) { + const candidatesCollection = roomRef.collection(localName); + + peerConnection.addEventListener('icecandidate', event => { + if (!event.candidate) { + console.log('Got final candidate!'); + return; + } + console.log('Got candidate: ', event.candidate); + candidatesCollection.add(event.candidate.toJSON()); + }); + + roomRef.collection(remoteName).onSnapshot(snapshot => { + snapshot.docChanges().forEach(async change => { + if (change.type === "added") { + let data = change.doc.data(); + console.log('Got new remote ICE candidate: ${JSON.stringify(data)}'); + await peerConnection.addIceCandidate(new RTCIceCandidate(data)); + } + }); + }) +}; +// collect ICE Candidates function above + async function openUserMedia(e) { const stream = await navigator.mediaDevices.getUserMedia( {video: true, audio: true}); @@ -225,11 +211,11 @@ async function hangUp(e) { if (roomId) { const db = firebase.firestore(); const roomRef = db.collection('rooms').doc(roomId); - const calleeCandidates = await roomRef.collection('calleeCandidates').get(); + const calleeCandidates = await roomRef.collection(calleeCandidatesString).get(); calleeCandidates.forEach(async candidate => { await candidate.ref.delete(); }); - const callerCandidates = await roomRef.collection('callerCandidates').get(); + const callerCandidates = await roomRef.collection(callerCandidatesString).get(); callerCandidates.forEach(async candidate => { await candidate.ref.delete(); });