Skip to content

Commit

Permalink
Forcing to reduce bandwidth on WiFi/Cell networks.
Browse files Browse the repository at this point in the history
  • Loading branch information
muaz-khan committed Feb 17, 2016
1 parent 05326ca commit ab7615a
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 109 deletions.
65 changes: 46 additions & 19 deletions RTCMultiConnection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Last time updated: 2016-02-13 7:21:53 AM UTC
// Last time updated: 2016-02-17 4:59:46 PM UTC

// ______________________________
// RTCMultiConnection-v3.0 (Beta)
Expand Down Expand Up @@ -1443,6 +1443,38 @@
connection.isOnline = false;
});

connection.isLowBandwidth = false;
if (navigator && navigator.connection && navigator.connection.type) {
connection.isLowBandwidth = navigator.connection.type.toString().toLowerCase().search(/wifi|cell/g) !== -1;
if (connection.isLowBandwidth) {
connection.bandwidth = {
audio: 30,
video: 30,
screen: 30
};

if (connection.mediaConstraints.audio && connection.mediaConstraints.audio.optional.length) {
var newArray = [];
connection.mediaConstraints.audio.optional.forEach(function(opt) {
if (typeof opt.bandwidth === 'undefined') {
newArray.push(opt);
}
});
connection.mediaConstraints.audio.optional = newArray;
}

if (connection.mediaConstraints.video && connection.mediaConstraints.video.optional.length) {
var newArray = [];
connection.mediaConstraints.video.optional.forEach(function(opt) {
if (typeof opt.bandwidth === 'undefined') {
newArray.push(opt);
}
});
connection.mediaConstraints.video.optional = newArray;
}
}
}

connection.getExtraData = function(remoteUserId) {
if (!remoteUserId) throw 'remoteUserId is required.';
if (!connection.peers[remoteUserId]) return {};
Expand Down Expand Up @@ -2227,7 +2259,6 @@
// detect node-webkit
var isNodeWebkit = !!(window.process && (typeof window.process === 'object') && window.process.versions && window.process.versions['node-webkit']);


var chromeVersion = 50;
var matchArray = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
if (isChrome && matchArray && matchArray[2]) {
Expand Down Expand Up @@ -3140,7 +3171,6 @@
})();

// IceServersHandler.js
// note: "urls" doesn't works in old-firefox.

var iceFrame, loadedIceFrame;

Expand All @@ -3159,8 +3189,6 @@
function iFrameLoaderCallback(event) {
if (!event.data || !event.data.iceServers) return;
callback(event.data.iceServers);

// this event listener is no more needed
window.removeEventListener('message', iFrameLoaderCallback);
}

Expand All @@ -3171,7 +3199,7 @@
(document.body || document.documentElement).appendChild(iframe);
}

if (typeof window.getExternalIceServers === 'undefined' || window.getExternalIceServers == true) {
if (typeof window.getExternalIceServers !== 'undefined' && window.getExternalIceServers == true) {
loadIceFrame(function(externalIceServers) {
if (!externalIceServers || !externalIceServers.length) return;
window.RMCExternalIceServers = externalIceServers;
Expand All @@ -3186,13 +3214,11 @@
function getIceServers(connection) {
var iceServers = [];

// Firefox <= 37 doesn't understands "urls"

iceServers.push({
urls: 'stun:stun.l.google.com:19302'
});

iceServers.push({
}, {
urls: 'stun:mmt-stun.verkstad.net'
}, {
urls: 'stun:stun.anyfirewall.com:3478'
});

Expand All @@ -3203,25 +3229,26 @@
});

iceServers.push({
urls: 'turn:turn.anyfirewall.com:443?transport=tcp',
urls: 'turn:turn.anyfirewall.com:443',
credential: 'webrtc',
username: 'webrtc'
});

// copyright of mmt-turn.verkstad: Ericsson
iceServers.push({
urls: 'turn:mmt-turn.verkstad.net',
username: 'webrtc',
credential: 'secret'
});

if (window.RMCExternalIceServers) {
iceServers = window.RMCExternalIceServers.concat(iceServers);
connection.iceServers = iceServers;
} else if (typeof window.getExternalIceServers === 'undefined' || window.getExternalIceServers == true) {
} else if (typeof window.getExternalIceServers !== 'undefined' && window.getExternalIceServers == true) {
window.iceServersLoadCallback = function() {
iceServers = window.RMCExternalIceServers.concat(iceServers);
connection.iceServers = iceServers;
};
} else {
iceServers.push({
urls: 'turn:turn.anyfirewall.com:443?transport=udp',
credential: 'webrtc',
username: 'webrtc'
});
}

return iceServers;
Expand Down
10 changes: 5 additions & 5 deletions RTCMultiConnection.min.js

Large diffs are not rendered by default.

22 changes: 9 additions & 13 deletions demos/Scalable-Broadcast.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ <h1><a href="https://github.com/muaz-khan/WebRTC-Scalable-Broadcast">WebRTC Scal
<script src="/dist/rmc3.min.js"></script>

<script src="https://cdn.webrtc-experiment.com/RecordRTC.js"></script>
<script src="https://cdn.webrtc-experiment.com/ConcatenateBlobs.js"></script>

<script>
var connection = new RTCMultiConnection(null, {
useDefaultDevices: true // if we don't need to force selection of specific devices
Expand All @@ -134,7 +132,7 @@ <h1><a href="https://github.com/muaz-khan/WebRTC-Scalable-Broadcast">WebRTC Scal
// its mandatory in v3.0
connection.enableScalableBroadcast = true;

// each broadcast should serve only 1 users
// each relaying-user should serve only 1 users
connection.maxRelayLimitPerUser = 1;

// we don't need to keep room-opened
Expand Down Expand Up @@ -278,9 +276,9 @@ <h1><a href="https://github.com/muaz-khan/WebRTC-Scalable-Broadcast">WebRTC Scal
});
}

// Firefox is UN_UN_UABLE to record remote MediaStream
// Firefox seems UN_ABLE to record remote MediaStream
// WebAudio solution merely records audio
// so recording MUST_MUST be skipped for Firefox.
// so recording is skipped for Firefox.
if(connection.DetectRTC.browser.name === 'Chrome') {
repeatedlyRecordStream(event.stream);
}
Expand Down Expand Up @@ -335,13 +333,11 @@ <h1><a href="https://github.com/muaz-khan/WebRTC-Scalable-Broadcast">WebRTC Scal
connection.isUpperUserLeft = true;

if(allRecordedBlobs.length) {
ConcatenateBlobs(allRecordedBlobs, 'video/webm', function(resultingBlob) {
if(!connection.isUpperUserLeft) return;
videoPreview.src = URL.createObjectURL(resultingBlob);
videoPreview.play();

allRecordedBlobs = [];
});
// playing lats recorded blob
var lastBlob = allRecordedBlobs[allRecordedBlobs.length - 1];
videoPreview.src = URL.createObjectURL(lastBlob);
videoPreview.play();
allRecordedBlobs = [];
}
else if(connection.currentRecorder) {
var recorder = connection.currentRecorder;
Expand Down Expand Up @@ -384,7 +380,7 @@ <h1><a href="https://github.com/muaz-khan/WebRTC-Scalable-Broadcast">WebRTC Scal
connection.currentRecorder = null;
repeatedlyRecordStream(stream);
});
}, 30 * 1000);
}, 30 * 1000); // 30-seconds
};

</script>
Expand Down
30 changes: 13 additions & 17 deletions dev/IceServersHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// IceServersHandler.js
// note: "urls" doesn't works in old-firefox.

var iceFrame, loadedIceFrame;

Expand All @@ -18,8 +17,6 @@ function loadIceFrame(callback, skip) {
function iFrameLoaderCallback(event) {
if (!event.data || !event.data.iceServers) return;
callback(event.data.iceServers);

// this event listener is no more needed
window.removeEventListener('message', iFrameLoaderCallback);
}

Expand All @@ -30,7 +27,7 @@ function loadIceFrame(callback, skip) {
(document.body || document.documentElement).appendChild(iframe);
}

if (typeof window.getExternalIceServers === 'undefined' || window.getExternalIceServers == true) {
if (typeof window.getExternalIceServers !== 'undefined' && window.getExternalIceServers == true) {
loadIceFrame(function(externalIceServers) {
if (!externalIceServers || !externalIceServers.length) return;
window.RMCExternalIceServers = externalIceServers;
Expand All @@ -45,13 +42,11 @@ var IceServersHandler = (function() {
function getIceServers(connection) {
var iceServers = [];

// Firefox <= 37 doesn't understands "urls"

iceServers.push({
urls: 'stun:stun.l.google.com:19302'
});

iceServers.push({
}, {
urls: 'stun:mmt-stun.verkstad.net'
}, {
urls: 'stun:stun.anyfirewall.com:3478'
});

Expand All @@ -62,25 +57,26 @@ var IceServersHandler = (function() {
});

iceServers.push({
urls: 'turn:turn.anyfirewall.com:443?transport=tcp',
urls: 'turn:turn.anyfirewall.com:443',
credential: 'webrtc',
username: 'webrtc'
});

// copyright of mmt-turn.verkstad: Ericsson
iceServers.push({
urls: 'turn:mmt-turn.verkstad.net',
username: 'webrtc',
credential: 'secret'
});

if (window.RMCExternalIceServers) {
iceServers = window.RMCExternalIceServers.concat(iceServers);
connection.iceServers = iceServers;
} else if (typeof window.getExternalIceServers === 'undefined' || window.getExternalIceServers == true) {
} else if (typeof window.getExternalIceServers !== 'undefined' && window.getExternalIceServers == true) {
window.iceServersLoadCallback = function() {
iceServers = window.RMCExternalIceServers.concat(iceServers);
connection.iceServers = iceServers;
};
} else {
iceServers.push({
urls: 'turn:turn.anyfirewall.com:443?transport=udp',
credential: 'webrtc',
username: 'webrtc'
});
}

return iceServers;
Expand Down
32 changes: 32 additions & 0 deletions dev/RTCMultiConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,38 @@ function RTCMultiConnection(roomid, forceOptions) {
connection.isOnline = false;
});

connection.isLowBandwidth = false;
if (navigator && navigator.connection && navigator.connection.type) {
connection.isLowBandwidth = navigator.connection.type.toString().toLowerCase().search(/wifi|cell/g) !== -1;
if (connection.isLowBandwidth) {
connection.bandwidth = {
audio: 30,
video: 30,
screen: 30
};

if (connection.mediaConstraints.audio && connection.mediaConstraints.audio.optional.length) {
var newArray = [];
connection.mediaConstraints.audio.optional.forEach(function(opt) {
if (typeof opt.bandwidth === 'undefined') {
newArray.push(opt);
}
});
connection.mediaConstraints.audio.optional = newArray;
}

if (connection.mediaConstraints.video && connection.mediaConstraints.video.optional.length) {
var newArray = [];
connection.mediaConstraints.video.optional.forEach(function(opt) {
if (typeof opt.bandwidth === 'undefined') {
newArray.push(opt);
}
});
connection.mediaConstraints.video.optional = newArray;
}
}
}

connection.getExtraData = function(remoteUserId) {
if (!remoteUserId) throw 'remoteUserId is required.';
if (!connection.peers[remoteUserId]) return {};
Expand Down
1 change: 0 additions & 1 deletion dev/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var isMobileDevice = !!navigator.userAgent.match(/Android|iPhone|iPad|iPod|Black
// detect node-webkit
var isNodeWebkit = !!(window.process && (typeof window.process === 'object') && window.process.versions && window.process.versions['node-webkit']);


var chromeVersion = 50;
var matchArray = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
if (isChrome && matchArray && matchArray[2]) {
Expand Down
2 changes: 1 addition & 1 deletion dist/rmc3.fbr.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit ab7615a

Please sign in to comment.