Skip to content

Commit

Permalink
trickle-ice: add getUserMedia support (webrtc#1478)
Browse files Browse the repository at this point in the history
which can change the candidate gathering behaviour
  • Loading branch information
fippo authored Sep 21, 2021
1 parent 3e61e14 commit 82b6ec8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/content/peerconnection/trickle-ice/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ <h2>ICE options</h2>
</div>

</section>

<section>
<div>
<input type="checkbox" name="getUserMedia" id="getUserMedia">
<label for="getUserMedia"><span>Acquire microphone/camera permissions</span></label>
</div>

</section>

<section>

Expand Down
24 changes: 22 additions & 2 deletions src/content/peerconnection/trickle-ice/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const servers = document.querySelector('select#servers');
const urlInput = document.querySelector('input#url');
const usernameInput = document.querySelector('input#username');
const iceCandidatePoolInput = document.querySelector('input#iceCandidatePool');
const getUserMediaInput = document.querySelector('input#getUserMedia');

addButton.onclick = addServer;
gatherButton.onclick = start;
Expand All @@ -29,13 +30,14 @@ resetButton.onclick = (e) => {
setDefaultServer(serversSelect);
};

iceCandidatePoolInput.onchange = function(e) {
iceCandidatePoolInput.onchange = (e) => {
const span = e.target.parentElement.querySelector('span');
span.textContent = e.target.value;
};

let begin;
let pc;
let stream;
let candidates;

const allServersKey = 'servers';
Expand Down Expand Up @@ -115,13 +117,17 @@ function removeServer() {
writeServersToLocalStorage();
}

function start() {
async function start() {
// Clean out the table.
while (candidateTBody.firstChild) {
candidateTBody.removeChild(candidateTBody.firstChild);
}

gatherButton.disabled = true;
if (getUserMediaInput.checked) {
stream = await navigator.mediaDevices.getUserMedia({audio: true, video: true});
}
getUserMediaInput.disabled = true;

// Read the values from the input boxes.
const iceServers = [];
Expand Down Expand Up @@ -154,6 +160,9 @@ function start() {
pc.onicecandidate = iceCallback;
pc.onicegatheringstatechange = gatheringStateChange;
pc.onicecandidateerror = iceCandidateError;
if (stream) {
stream.getTracks().forEach(track => pc.addTrack(track, stream));
}
pc.createOffer(
offerOptions
).then(
Expand Down Expand Up @@ -254,7 +263,13 @@ function iceCallback(event) {
appendCell(row, getFinalResult(), 7);
pc.close();
pc = null;
pc = null;
if (stream) {
stream.getTracks().forEach(track => track.stop());
stream = null;
}
gatherButton.disabled = false;
getUserMediaInput.disabled = false;
}
candidateTBody.appendChild(row);
}
Expand All @@ -269,7 +284,12 @@ function gatheringStateChange() {
appendCell(row, getFinalResult(), 7);
pc.close();
pc = null;
if (stream) {
stream.getTracks().forEach(track => track.stop());
stream = null;
}
gatherButton.disabled = false;
getUserMediaInput.disabled = false;
candidateTBody.appendChild(row);
}

Expand Down

0 comments on commit 82b6ec8

Please sign in to comment.