Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

condensed code into a method, called method #27

Open
wants to merge 1 commit into
base: solution
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 35 additions & 49 deletions public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const configuration = {
iceCandidatePoolSize: 10,
};

const callerCandidatesString = 'callerCandidates';
const calleeCandidatesString = 'calleeCandidates';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more clear to remove "String" from the end of the variable declaration and make CALLER_CANDIDATES all caps to indicate it's a constant.


let peerConnection = null;
let localStream = null;
let remoteStream = null;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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]);
Expand All @@ -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});
Expand Down Expand Up @@ -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();
});
Expand Down