Downscaling to 360p for larger calls working

This commit is contained in:
Arya Vohra 2020-06-02 11:33:15 +08:00
parent f72d77494e
commit 38ead5c785
3 changed files with 55 additions and 41 deletions

2
public/css/chat.css vendored
View File

@ -145,7 +145,7 @@ a {
padding: 10px; padding: 10px;
} }
#remote-video:first-child:nth-last-child(1) { #remote-video:first-child:nth-last-child(1) {
/* -or- li:only-child { */ /* -or- li:only-child { */
max-height: 80vh; max-height: 80vh;
} }

View File

@ -204,7 +204,9 @@ var VideoChat = {
break; break;
case "disconnected": case "disconnected":
logIt("disconnected - UUID " + uuid); logIt("disconnected - UUID " + uuid);
VideoChat.remoteVideoWrapper.removeChild(document.querySelectorAll(`[uuid="${uuid}"]`)[0]); VideoChat.remoteVideoWrapper.removeChild(
document.querySelectorAll(`[uuid="${uuid}"]`)[0]
);
VideoChat.connected.delete(uuid); VideoChat.connected.delete(uuid);
VideoChat.peerConnections.delete(uuid); VideoChat.peerConnections.delete(uuid);
dataChannel.delete(uuid); dataChannel.delete(uuid);
@ -225,15 +227,6 @@ var VideoChat = {
break; break;
} }
}; };
// Downscale send resolution and bitrate if num in room > 4
if (VideoChat.peerConnections.size > 3) {
for (var pc in VideoChat.peerConnections) {
downscaleStream(pc);
}
}
callback(uuid); callback(uuid);
}; };
}, },
@ -370,6 +363,12 @@ var VideoChat = {
VideoChat.connected.set(uuid, true); VideoChat.connected.set(uuid, true);
// Hide caption status text // Hide caption status text
captionText.fadeOut(); captionText.fadeOut();
// Downscale send resolution and bitrate if num in room > 4
if (VideoChat.peerConnections.size > 3) {
VideoChat.peerConnections.forEach(function (value, key, map) {
downscaleStream(value);
});
}
// Reposition local video after a second, as there is often a delay // Reposition local video after a second, as there is often a delay
// between adding a stream and the height of the video div changing // between adding a stream and the height of the video div changing
setTimeout(() => rePositionLocalVideo(), 500); setTimeout(() => rePositionLocalVideo(), 500);
@ -508,16 +507,18 @@ function sendToAllDataChannels(message) {
// } // }
// End Fullscreen // End Fullscreen
// Downscale single stream // Downscale single stream
async function downscaleStream(pc) { async function downscaleStream(pc, applying = false) {
height = 480; height = 240;
rate = 800000; rate = 800000;
if (applying) return;
try { try {
applying = true;
do { do {
h = height; h = height;
r = rate; const sender = pc.getSenders().find(function (s) {
const [sender] = pc.getSenders(); return s.track.kind === "video";
});
const ratio = sender.track.getSettings().height / height; const ratio = sender.track.getSettings().height / height;
const params = sender.getParameters(); const params = sender.getParameters();
if (!params.encodings) params.encodings = [{}]; // Firefox workaround! if (!params.encodings) params.encodings = [{}]; // Firefox workaround!
@ -527,6 +528,8 @@ async function downscaleStream(pc) {
} while (h != height); } while (h != height);
} catch (e) { } catch (e) {
logIt(e); logIt(e);
} finally {
applying = false;
} }
} }
@ -542,7 +545,7 @@ function muteMicrophone() {
}); });
audioTrack.enabled = VideoChat.audioEnabled; audioTrack.enabled = VideoChat.audioEnabled;
}); });
// select mic button and mic button text // select mic button and mic button text
const micButtonIcon = document.getElementById("mic-icon"); const micButtonIcon = document.getElementById("mic-icon");
const micButtonText = document.getElementById("mic-text"); const micButtonText = document.getElementById("mic-text");
@ -894,15 +897,16 @@ function uuidToColor(uuid) {
// Using uuid to generate random. unique pastel color // Using uuid to generate random. unique pastel color
var hash = 0; var hash = 0;
for (var i = 0; i < uuid.length; i++) { for (var i = 0; i < uuid.length; i++) {
hash = uuid.charCodeAt(i) + ((hash << 5) - hash); hash = uuid.charCodeAt(i) + ((hash << 5) - hash);
hash = hash & hash; hash = hash & hash;
} }
var hue = Math.abs(hash % 360); var hue = Math.abs(hash % 360);
console.log(hue);
// Ensure color is not similar to other colors // Ensure color is not similar to other colors
var availColors = Array.from({length: 18}, (x,i) => i*20); var availColors = Array.from({ length: 18 }, (x, i) => i * 20);
VideoChat.peerColors.forEach(function(value, key, map) {availColors[Math.floor(value/20)] = null}); VideoChat.peerColors.forEach(function (value, key, map) {
if (availColors[Math.floor(hue/20)] == null) { availColors[Math.floor(value / 20)] = null;
});
if (availColors[Math.floor(hue / 20)] == null) {
for (var i = 0; i < availColors.length; i++) { for (var i = 0; i < availColors.length; i++) {
if (availColors[i] != null) { if (availColors[i] != null) {
hue = (hue % 20) + availColors[i]; hue = (hue % 20) + availColors[i];
@ -917,7 +921,9 @@ function uuidToColor(uuid) {
// Sets the border color of uuid's stream // Sets the border color of uuid's stream
function setStreamColor(uuid) { function setStreamColor(uuid) {
const color = uuidToColor(uuid); const color = uuidToColor(uuid);
document.querySelectorAll(`[uuid="${uuid}"]`)[0].style.border = `3px solid ${color}`; document.querySelectorAll(
`[uuid="${uuid}"]`
)[0].style.border = `3px solid ${color}`;
VideoChat.peerColors[uuid] = color; VideoChat.peerColors[uuid] = color;
} }
@ -952,18 +958,27 @@ function togglePictureInPicture() {
logIt("Error exiting pip."); logIt("Error exiting pip.");
logIt(error); logIt(error);
}); });
} else if (VideoChat.remoteVideoWrapper.lastChild.webkitPresentationMode === "inline") {
VideoChat.remoteVideoWrapper.lastChild.webkitSetPresentationMode("picture-in-picture");
} else if ( } else if (
VideoChat.remoteVideoWrapper.lastChild.webkitPresentationMode === "picture-in-picture" VideoChat.remoteVideoWrapper.lastChild.webkitPresentationMode === "inline"
) { ) {
VideoChat.remoteVideoWrapper.lastChild.webkitSetPresentationMode("inline"); VideoChat.remoteVideoWrapper.lastChild.webkitSetPresentationMode(
"picture-in-picture"
);
} else if (
VideoChat.remoteVideoWrapper.lastChild.webkitPresentationMode ===
"picture-in-picture"
) {
VideoChat.remoteVideoWrapper.lastChild.webkitSetPresentationMode(
"inline"
);
} else { } else {
VideoChat.remoteVideoWrapper.lastChild.requestPictureInPicture().catch((error) => { VideoChat.remoteVideoWrapper.lastChild
alert( .requestPictureInPicture()
"You must be connected to another person to enter picture in picture." .catch((error) => {
); alert(
}); "You must be connected to another person to enter picture in picture."
);
});
} }
} else { } else {
alert( alert(

15
public/landing.html vendored
View File

@ -67,9 +67,9 @@
class="mt-0 mb-32 reveal-from-bottom" class="mt-0 mb-32 reveal-from-bottom"
data-reveal-delay="300" data-reveal-delay="300"
> >
Simple, Secure, and Fast. Peer to peer group video Simple, Secure, and Fast. Peer to peer group video calling
calling provides quality and latency simply not provides quality and latency simply not available with
available with traditional technology. traditional technology.
</p> </p>
<div class="reveal-from-bottom" data-reveal-delay="450"> <div class="reveal-from-bottom" data-reveal-delay="450">
<a <a
@ -201,9 +201,8 @@
<div class="features-tiles-item-content"> <div class="features-tiles-item-content">
<h4 class="mt-0 mb-8">Decentralized group calls</h4> <h4 class="mt-0 mb-8">Decentralized group calls</h4>
<p class="m-0 text-sm"> <p class="m-0 text-sm">
Zipcall lets you talk to up to four friends by Zipcall lets you talk to up to four friends by directly
directly connecting to them, completely connecting to them, completely decentralized.
decentralized.
</p> </p>
</div> </div>
</div> </div>
@ -248,8 +247,8 @@
<h4 class="mt-0 mb-8">Total Privacy and Security</h4> <h4 class="mt-0 mb-8">Total Privacy and Security</h4>
<p class="m-0 text-sm"> <p class="m-0 text-sm">
Zipcall is built privacy first. Each chat is single use, Zipcall is built privacy first. Each chat is single use,
and end to end state of the art encryption means your calls and end to end state of the art encryption means your
are exactly that. Your calls. calls are exactly that. Your calls.
</p> </p>
</div> </div>
</div> </div>