Skip to content

Instantly share code, notes, and snippets.

@rajdeeprath
Last active August 28, 2018 13:05
Show Gist options
  • Save rajdeeprath/d0ec688db7f078865a25d616ebed62c1 to your computer and use it in GitHub Desktop.
Save rajdeeprath/d0ec688db7f078865a25d616ebed62c1 to your computer and use it in GitHub Desktop.
Edge Detect
// Defining/accessing testbed configuration.
(function (window, adapter) {
if (typeof adapter !== 'undefined') {
console.log('Browser: ' + JSON.stringify(adapter.browserDetails, null, 2));
}
// http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
function getParameterByName(name, url) { // eslint-disable-line no-unused-vars
if (!url) {
url = window.location.href;
}
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var protocol = window.location.protocol;
var port = window.location.port;
protocol = protocol.substring(0, protocol.lastIndexOf(':'));
var isMoz = !!navigator.mozGetUserMedia;
var isEdge = (adapter && adapter.browserDetails.browser.toLowerCase() === 'edge') || (window.navigator.userAgent.toLowerCase().indexOf("edge") > -1);
var isiPod = !!navigator.platform && /iPod/.test(navigator.platform);
var config = sessionStorage.getItem('r5proTestBed');
var json;
var serverSettings = {
"protocol": protocol,
"httpport": port,
"hlsport": 5080,
"hlssport": 443,
"wsport": 8081,
"wssport": 8083,
"rtmpport": 1935,
"rtmpsport": 1936
};
function assignStorage () {
json = {
"host": "localhost",
"port": 8554, // rtsp
"stream1": "stream1",
"stream2": "stream2",
"app": "live",
"proxy": "streammanager",
"isEdge": isEdge,
"streamMode": "live",
"cameraWidth": 854,
"cameraHeight": 480,
"embedWidth": "100%",
"embedHeight": 480,
"buffer": 0.5,
"bandwidth": {
"audio": 50,
"video": 256
},
"useAudio": true,
"useVideo": true,
"mediaConstraints": {
"audio": isiPod ? false : true,
"video": (isMoz || isEdge) ? true : {
"width": {
"min": 320,
"max": 640
},
"height": {
"min": 240,
"max": 480
},
"frameRate": {
"min": 8,
"max": 24
}
}
},
"publisherFailoverOrder": "rtc,rtmp",
"subscriberFailoverOrder": "rtc,rtmp,hls",
"iceServers": [
{
"urls": "stun:stun2.l.google.com:19302"
}
],
"googleIce": [
{
"urls": "stun:stun2.l.google.com:19302"
}
],
"mozIce": [
{
"urls": "stun:stun.services.mozilla.com:3478"
}
],
"iceTransport": "udp",
"verboseLogging": true,
"streamManagerAPI": "3.0",
"streamManagerAccessToken": "xyz123"
};
/**
if (isMoz) {
json.iceServers = json.mozIce;
}
*/
sessionStorage.setItem('r5proTestBed', JSON.stringify(json));
}
function defineIceServers () {
var param = getParameterByName('ice');
if (param) {
if (param === 'moz') {
json.iceServers = json.mozIce;
}
else {
json.iceServers = json.googleIce;
}
console.log('ICE server provided in query param: ' + JSON.stringify(json.iceServers, null, 2));
}
}
if (config) {
try {
json = JSON.parse(config);
}
catch (e) {
assignStorage();
}
finally {
defineIceServers();
sessionStorage.setItem('r5proTestBed', JSON.stringify(json));
}
}
else {
assignStorage();
defineIceServers();
sessionStorage.setItem('r5proTestBed', JSON.stringify(json));
}
sessionStorage.setItem('r5proServerSettings', JSON.stringify(serverSettings));
return json;
})(this, window.adapter);
(function(window, document, red5prosdk) {
'use strict';
var serverSettings = (function() {
var settings = sessionStorage.getItem('r5proServerSettings');
try {
return JSON.parse(settings);
}
catch (e) {
console.error('Could not read server settings from sessionstorage: ' + e.message);
}
return {};
})();
var configuration = (function () {
var conf = sessionStorage.getItem('r5proTestBed');
try {
return JSON.parse(conf);
}
catch (e) {
console.error('Could not read testbed configuration from sessionstorage: ' + e.message);
}
return {}
})();
red5prosdk.setLogLevel(configuration.verboseLogging ? red5prosdk.LOG_LEVELS.TRACE : red5prosdk.LOG_LEVELS.WARN);
var targetPublisher;
var updateStatusFromEvent = window.red5proHandlePublisherEvent; // defined in src/template/partial/status-field-publisher.hbs
var streamTitle = document.getElementById('stream-title');
var statisticsField = document.getElementById('statistics-field');
var protocol = serverSettings.protocol;
var isSecure = protocol == 'https';
function getSocketLocationFromProtocol () {
return !isSecure
? {protocol: 'ws', port: serverSettings.wsport}
: {protocol: 'wss', port: serverSettings.wssport};
}
function onBitrateUpdate (bitrate, packetsSent) {
statisticsField.innerText = 'Bitrate: ' + Math.floor(bitrate) + '. Packets Sent: ' + packetsSent + '.';
}
function onPublisherEvent (event) {
console.log('[Red5ProPublisher] ' + event.type + '.');
updateStatusFromEvent(event);
}
function onPublishFail (message) {
console.error('[Red5ProPublisher] Publish Error :: ' + message);
}
function onPublishSuccess (publisher) {
console.log('[Red5ProPublisher] Publish Complete.');
try {
window.trackBitrate(publisher.getPeerConnection(), onBitrateUpdate);
}
catch (e) {
// no tracking for you!
}
}
function onUnpublishFail (message) {
console.error('[Red5ProPublisher] Unpublish Error :: ' + message);
}
function onUnpublishSuccess () {
console.log('[Red5ProPublisher] Unpublish Complete.');
}
function getUserMediaConfiguration () {
return {
mediaConstraints: {
audio: configuration.useAudio ? configuration.mediaConstraints.audio : false,
video: configuration.useVideo ? configuration.mediaConstraints.video : false
}
};
}
function getRTMPMediaConfiguration () {
return {
mediaConstraints: {
audio: configuration.useAudio ? configuration.mediaConstraints.audio : false,
video: configuration.useVideo ? {
width: configuration.cameraWidth,
height: configuration.cameraHeight
} : false
}
}
}
function unpublish () {
return new Promise(function (resolve, reject) {
var publisher = targetPublisher;
publisher.unpublish()
.then(function () {
onUnpublishSuccess();
resolve();
})
.catch(function (error) {
var jsonError = typeof error === 'string' ? error : JSON.stringify(error, 2, null);
onUnpublishFail('Unmount Error ' + jsonError);
reject(error);
});
});
}
var config = Object.assign({},
configuration,
getUserMediaConfiguration());
var rtcConfig = Object.assign({}, config, {
protocol: getSocketLocationFromProtocol().protocol,
port: getSocketLocationFromProtocol().port,
streamName: config.stream1,
});
var rtmpConfig = Object.assign({}, config, {
protocol: 'rtmp',
port: serverSettings.rtmpport,
streamName: config.stream1,
backgroundColor: '#000000',
swf: '../../lib/red5pro/red5pro-publisher.swf',
swfobjectURL: '../../lib/swfobject/swfobject.js',
productInstallURL: '../../lib/swfobject/playerProductInstall.swf'
}, getRTMPMediaConfiguration());
var publishOrder = config.publisherFailoverOrder
.split(',')
.map(function (item) {
return item.trim()
});
if (window.query('view')) {
publishOrder = [window.query('view')];
}
var publisher = new red5prosdk.Red5ProPublisher()
var order = "undefined;"
if(configuration.isEdge === true)
{
alert("This is edge")
console.log("Do additional flash detection");
//https://helpx.adobe.com/flash-player/kb/flash-player-issues-windows-10-edge.html
}
else
{
order = {
rtc: rtcConfig,
rtmp: rtmpConfig
};
}
publisher.setPublishOrder(publishOrder)
.init(order)
.then(function (publisherImpl) {
streamTitle.innerText = configuration.stream1;
targetPublisher = publisherImpl;
targetPublisher.on('*', onPublisherEvent);
return targetPublisher.publish();
})
.then(function () {
onPublishSuccess(targetPublisher);
})
.catch(function (error) {
var jsonError = typeof error === 'string' ? error : JSON.stringify(error, null, 2);
console.error('[Red5ProPublisher] :: Error in publishing - ' + jsonError);
onPublishFail(jsonError);
});
window.addEventListener('beforeunload', function() {
function clearRefs () {
if (targetPublisher) {
targetPublisher.off('*', onPublisherEvent);
}
targetPublisher = undefined;
}
unpublish().then(clearRefs).catch(clearRefs);
window.untrackBitrate();
});
})(this, document, window.red5prosdk);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment