').html(
$('').html(
$('').text(graph.id)
)
).append(
$('').css('padding-right','2em').css('text-align','right').html(
$('').addClass('value')
).append(
$('').data('opts',graph).text('X').addClass('close').click(function(){
var graph = $(this).data('opts');
if (confirm('Are you sure you want to remove '+graph.id+'?')) {
graph.elements.cont.remove();
var $opt = $('.graph_ids option:contains('+graph.id+')');
var $select = $opt.parent();
$opt.remove();
UI.plot.del(graph.id);
delete graphs[graph.id];
$select.trigger('change');
UI.plot.go(graphs);
}
})
)
)
);
graph.elements.legend.html($list);
function updateLegendValues(x) {
var $spans = graph.elements.legend.find('.value');
var n = 1;
if (typeof x == 'undefined') {
$spans.eq(0).html('Latest:');
}
else {
var axis = graph.plot.getXAxes()[0];
x = Math.min(axis.max,x);
x = Math.max(axis.min,x);
$spans.eq(0).html(UI.format.time(x/1e3));
}
for (var i in graph.datasets) {
var label = ' ';
if (graph.datasets[i].display) {
var tickformatter = UI.plot.yaxes[graph.datasets[i].yaxistype].tickFormatter;
var data = graph.datasets[i].data;
if (!x) {
label = tickformatter(graph.datasets[i].data[graph.datasets[i].data.length-1][1]);
}
else {
for (var j in data) {
if (data[j][0] == x) {
label = tickformatter(data[j][1]);
break;
}
if (data[j][0] > x) {
if (j != 0) {
var p1 = data[j];
var p2 = data[j-1];
var y = p1[1] + (x - p1[0]) * (p2[1] - p1[1]) / (p2[0] - p1[0]);
label = tickformatter(y);
}
break;
}
}
}
}
$spans.eq(n).html(label);
n++;
}
}
var plotdata = graph.plot.getOptions();
for (var i in graph.datasets) {
var $checkbox = $(' ').attr('type','checkbox').data('index',i).data('graph',graph).click(function(){
var graph = $(this).data('graph');
if ($(this).is(':checked')) {
graph.datasets[$(this).data('index')].display = true;
}
else {
graph.datasets[$(this).data('index')].display = false;
}
var obj = {};
obj[graph.id] = graph;
UI.plot.go(obj);
});
if (graph.datasets[i].display) {
$checkbox.attr('checked','checked');
}
$list.append(
$('').html(
$('').html(
$('').html(
$checkbox
).append(
$('').addClass('series-color').css('background-color',graph.datasets[i].color)
).append(
graph.datasets[i].label
)
)
).append(
$('
').css('padding-right','2em').css('text-align','right').html(
$('').addClass('value')
).append(
$('').text('X').addClass('close').data('index',i).data('graph',graph).click(function(){
var i = $(this).data('index');
var graph = $(this).data('graph');
if (confirm('Are you sure you want to remove '+graph.datasets[i].label+' from '+graph.id+'?')) {
graph.datasets.splice(i,1);
if (graph.datasets.length == 0) {
graph.elements.cont.remove();
var $opt = $('.graph_ids option:contains('+graph.id+')');
var $select = $opt.parent();
$opt.remove();
$select.trigger('change');
UI.plot.del(graph.id);
delete graphs[graph.id];
UI.plot.go(graphs);
}
else {
UI.plot.save(graph);
var obj = {};
obj[graph.id] = graph;
UI.plot.go(obj);
}
}
})
)
)
);
}
updateLegendValues();
//and the tooltip
var lastval = false;
graph.elements.plot.on('plothover',function(e,pos,item){
if (pos.x != lastval) {
updateLegendValues(pos.x);
lastval = pos.x;
}
if (item) {
var $t = $('').append(
$('').text(item.series.label).prepend(
$('').addClass('series-color').css('background-color',item.series.color)
)
).append(
$('
').addClass('nolay').html(
$('').html(
$('').text('Time:')
).append(
$(' ').html(UI.format.dateTime(item.datapoint[0]/1e3,'long'))
)
).append(
$(' ').html(
$('').text('Value:')
).append(
$(' ').html(item.series.yaxis.tickFormatter(item.datapoint[1],item.series.yaxis))
)
)
);
UI.tooltip.show(pos,$t.children());
}
else {
UI.tooltip.hide();
}
}).on('mouseout',function(){
updateLegendValues();
});
break;
case 'coords':
break;
}
}
},reqobj)
},
save: function(opts){
var graph = {
id: opts.id,
xaxis: opts.xaxis,
datasets: []
};
for (var i in opts.datasets) {
graph.datasets.push({
origin: opts.datasets[i].origin,
datatype: opts.datasets[i].datatype
});
}
var graphs = mist.stored.get().graphs || {};
graphs[graph.id] = graph;
mist.stored.set('graphs',graphs);
},
del: function(graphid){
var graphs = mist.stored.get().graphs || {};
delete graphs[graphid];
mist.stored.set('graphs',graphs);
},
datatype: {
getOptions: function (opts) {
var general = $.extend(true,{},UI.plot.datatype.templates.general);
var specialized = $.extend(true,{},UI.plot.datatype.templates[opts.datatype]);
opts = $.extend(true,specialized,opts);
opts = $.extend(true,general,opts);
//append the origin to the label
switch (opts.origin[0]) {
case 'total':
switch (opts.datatype) {
case 'cpuload':
case 'memload':
break;
default:
opts.label += ' (total)';
}
break;
case 'stream':
case 'protocol':
opts.label += ' ('+opts.origin[1]+')';
break;
}
//slightly randomize the color
var color = [];
var variation = 50;
for (var i in opts.basecolor) {
var c = opts.basecolor[i];
c += variation * (0.5 - Math.random());
c = Math.round(c);
c = Math.min(255,Math.max(0,c));
color.push(c);
}
opts.color = 'rgb('+color.join(',')+')';
return opts;
},
templates: {
general: {
display: true,
datatype: 'general',
label: '',
yaxistype: 'amount',
data: [],
lines: { show: true },
points: { show: false },
getdata: function() {
var streamindex = (this.origin[0] == 'stream' ? this.origin[1] : 'all_streams');
var protocolindex = (this.origin[0] == 'protocol' ? this.origin[1] : 'all_protocols');
var thedata = mist.data.totals[streamindex][protocolindex][this.datatype]
this.data = thedata;
return thedata;
}
},
cpuload: {
label: 'CPU use',
yaxistype: 'percentage',
basecolor: [237,194,64],
cores: 1,
getdata: function(dataobj){
//remove any data older than 10 minutes
var removebefore = false;
for (var i in this.data) {
if (this.data[i][0] < (mist.data.config.time-600)*1000) {
removebefore = i;
}
}
if (removebefore !== false) {
this.data.splice(0,Number(removebefore)+1);
}
this.data.push([mist.data.config.time*1000,mist.data.capabilities.cpu_use/10]);
return this.data;
}
},
memload: {
label: 'Memory load',
yaxistype: 'percentage',
basecolor: [175,216,248],
getdata: function(){
//remove any data older than 10 minutes
var removebefore = false;
for (var i in this.data) {
if (this.data[i][0] < (mist.data.config.time-600)*1000) {
removebefore = i;
}
}
if (removebefore !== false) {
this.data.splice(0,Number(removebefore)+1);
}
this.data.push([mist.data.config.time*1000,mist.data.capabilities.load.memory]);
return this.data;
}
},
clients: {
label: 'Connections',
basecolor: [203,75,75]
},
upbps: {
label: 'Bandwidth up',
yaxistype: 'bytespersec',
basecolor: [77,167,77]
},
downbps: {
label: 'Bandwidth down',
yaxistype: 'bytespersec',
basecolor: [148,64,237]
}
}
},
yaxes: {
percentage: {
name: 'percentage',
color: 'black',
tickColor: 0,
tickDecimals: 0,
tickFormatter: function(val,axis){
return UI.format.addUnit(UI.format.number(val),'%');
},
tickLength: 0,
min: 0,
max: 100
},
amount: {
name: 'amount',
color: 'black',
tickColor: 0,
tickDecimals: 0,
tickFormatter: function(val,axis){
return UI.format.number(val);
},
tickLength: 0,
min: 0
},
bytespersec: {
name: 'bytespersec',
color: 'black',
tickColor: 0,
tickDecimals: 1,
tickFormatter: function(val,axis){
return UI.format.bytes(val,true);
},
tickLength: 0,
ticks: function(axis,a,b,c,d){
//taken from flot source code (function setupTickGeneration),
//modified to think in multiples of 1024 by Carina van der Meer for DDVTECH
// heuristic based on the model a*sqrt(x) fitted to
// some data points that seemed reasonable
var noTicks = 0.3 * Math.sqrt($('.graph').first().height());
var delta = (axis.max - axis.min) / noTicks,
exponent = Math.floor(Math.log(Math.abs(delta)) / Math.log(1024)),
correcteddelta = delta / Math.pow(1024,exponent),
dec = -Math.floor(Math.log(correcteddelta) / Math.LN10),
maxDec = axis.tickDecimals;
if (maxDec != null && dec > maxDec) {
dec = maxDec;
}
var magn = Math.pow(10, -dec),
norm = correcteddelta / magn, // norm is between 1.0 and 10.0
size;
if (norm < 1.5) {
size = 1;
} else if (norm < 3) {
size = 2;
// special case for 2.5, requires an extra decimal
if (norm > 2.25 && (maxDec == null || dec + 1 <= maxDec)) {
size = 2.5;
++dec;
}
} else if (norm < 7.5) {
size = 5;
} else {
size = 10;
}
size *= magn;
size = size * Math.pow(1024,exponent);
if (axis.minTickSize != null && size < axis.minTickSize) {
size = axis.minTickSize;
}
axis.delta = delta;
axis.tickDecimals = Math.max(0, maxDec != null ? maxDec : dec);
axis.tickSize = size;
var ticks = [],
start = axis.tickSize * Math.floor(axis.min / axis.tickSize),
i = 0,
v = Number.NaN,
prev;
do {
prev = v;
v = start + i * axis.tickSize;
ticks.push(v);
++i;
} while (v < axis.max && v != prev);
return ticks;
},
min: 0
}
},
xaxes: {
time: {
name: 'time',
mode: 'time',
timezone: 'browser',
ticks: 5
}
}
},
draggable: function(ele){
ele.attr('draggable',true);
ele.on('dragstart',function(e){
$(this).css('opacity',0.4).data('dragstart',{
click: {
x: e.originalEvent.pageX,
y: e.originalEvent.pageY
},
ele: {
x: this.offsetLeft,
y: this.offsetTop
}
});
}).on('dragend',function(e){
var old = $(this).data('dragstart');
var x = old.ele.x - old.click.x + e.originalEvent.pageX;
var y = old.ele.y - old.click.y + e.originalEvent.pageY;
$(this).css({
'opacity': 1,
'top': y,
'left': x,
'right' : 'auto',
'bottom' : 'auto'
});
});
ele.parent().on('dragleave',function(){
//end the drag ?
});
},
format: {
time: function(secs,type){
var d = new Date(secs * 1000);
var str = [];
str.push(('0'+d.getHours()).slice(-2));
str.push(('0'+d.getMinutes()).slice(-2));
if (type != 'short') { str.push(('0'+d.getSeconds()).slice(-2)); }
return str.join(':');
},
date: function(secs,type) {
var d = new Date(secs * 1000);
var days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var str = [];
if (type == 'long') { str.push(days[d.getDay()]); }
str.push(('0'+d.getDate()).slice(-2));
str.push(months[d.getMonth()]);
if (type != 'short') { str.push(d.getFullYear()); }
return str.join(' ');
},
dateTime: function(secs,type) {
return UI.format.date(secs,type)+', '+UI.format.time(secs,type);
},
duration: function(seconds) {
//get the amounts
var multiplications = [1e-3, 1e3, 60, 60, 24, 7,1e9];
var units = ['ms','sec','min','hr','day','week'];
var amounts = {};
var left = seconds;
for (var i in units) {
left /= multiplications[i];
var amount = Math.round(left % multiplications[Number(i)+1]);
amounts[units[i]] = amount;
left -= amount;
}
//format it
var unit; //if all amounts are 0, format as 00:00:00
for (var i = units.length-1; i >= 0; i--) {
var amount = amounts[units[i]];
if (amounts[units[i]] > 0) {
unit = units[i];
break;
}
}
var $s = $('');
switch (unit) {
case 'week':
$s.append(UI.format.addUnit(amounts.week,'wks, ')).append(UI.format.addUnit(amounts.day,'days'));
break;
case 'day':
$s.append(UI.format.addUnit(amounts.day,'days, ')).append(UI.format.addUnit(amounts.hr,'hrs'));
break;
default:
$s.append(
[
('0'+amounts.hr).slice(-2),
('0'+amounts.min).slice(-2),
('0'+amounts.sec).slice(-2)+(amounts.ms ? '.'+amounts.ms : '')
].join(':')
);
break;
}
return $s[0].innerHTML;
},
number: function(num) {
if ((isNaN(Number(num))) || (num == 0)) { return num; }
//rounding
var sig = 3;
var mult = Math.pow(10,sig - Math.floor(Math.log(num)/Math.LN10) - 1);
num = Math.round(num * mult) / mult;
//thousand seperation
if (num > 1e4) {
var seperator = ' ';
number = num.toString().split('.');
var regex = /(\d+)(\d{3})/;
while (regex.test(number[0])) {
number[0] = number[0].replace(regex,'$1'+seperator+'$2');
}
num = number.join('.');
}
return num;
},
status: function(item) {
var $s = $('');
if (typeof item.online == 'undefined') {
$s.text('Unknown, checking..');
if (typeof item.error != 'undefined') {
$s.text(item.error);
}
return $s;
}
switch (item.online) {
case -1: $s.text('Enabling'); break;
case 0: $s.text('Unavailable').addClass('red'); break;
case 1: $s.text('Active').addClass('green'); break;
case 2: $s.text('Standby').addClass('orange'); break;
default: $s.text(item.online);
}
if ('error' in item) {
$s.text(item.error);
}
return $s;
},
capital: function(string) {
return string.charAt(0).toUpperCase() + string.substring(1);
},
addUnit: function(number,unit){
var $s = $('').html(number);
$s.append(
$('').addClass('unit').html(unit)
);
return $s[0].innerHTML;
},
bytes: function(val,persec){
var suffix = ['bytes','KiB','MiB','GiB','TiB','PiB'];
if (val == 0) {
unit = suffix[0];
}
else {
var exponent = Math.floor(Math.log(Math.abs(val)) / Math.log(1024));
if (exponent < 0) {
unit = suffix[0];
}
else {
val = val / Math.pow(1024,exponent);
unit = suffix[exponent];
}
}
return UI.format.addUnit(UI.format.number(val),unit+(persec ? '/s' : ''));
}
},
navto: function(tab,other){
var prevhash = location.hash;
var hash = prevhash.split('@');
hash[0] = [mist.user.name,mist.user.host].join('&');
hash[1] = [tab,other].join('&');
if (typeof screenlog != 'undefined') { screenlog.navto(hash[1]); } //allow logging if screenlog is active
location.hash = hash.join('@');
if (location.hash == prevhash) {
//manually trigger hashchange even though hash hasn't changed
$(window).trigger('hashchange');
}
},
showTab: function(tab,other) {
var $main = UI.elements.main;
if ((mist.user.loggedin) && (!('ui_settings' in mist.data))) {
$main.html('Loading..');
mist.send(function(){
UI.showTab(tab,other);
},{ui_settings: true});
return;
}
var $currbut = UI.elements.menu.removeClass('hide').find('.plain:contains("'+tab+'")').closest('.button');
if ($currbut.length > 0) {
//only remove previous button highlight if the current tab is found in the menu
UI.elements.menu.find('.button.active').removeClass('active');
$currbut.addClass('active');
}
//unload any video's that might still be playing
if (typeof mistvideo != 'undefined') {
for (var s in mistvideo) {
if ('embedded' in mistvideo[s]) {
for (var i in mistvideo[s].embedded) {
try {
mistvideo[s].embedded[i].player.unload();
delete mistvideo[s].embedded[i];
}
catch (e) {}
}
}
}
}
UI.interval.clear();
$main.html(
$('').text(tab)
);
switch (tab) {
case 'Login':
if (mist.user.loggedin) {
//we're already logged in what are we doing here
UI.navto('Overview');
return;
}
UI.elements.menu.addClass('hide');
UI.elements.connection.status.text('Disconnected').removeClass('green').addClass('red');
$main.append(UI.buildUI([
{
type: 'help',
help: 'Please provide your account details. You were asked to set these when MistController was started for the first time. If you did not yet set any account details, log in with your desired credentials to create a new account.'
},{
label: 'Host',
help: 'Url location of the MistServer API. Generally located at http://MistServerIP:4242/api',
'default': 'http://localhost:4242/api',
pointer: {
main: mist.user,
index: 'host'
}
},{
label: 'Username',
help: 'Please enter your username here.',
validate: ['required'],
pointer: {
main: mist.user,
index: 'name'
}
},{
label: 'Password',
type: 'password',
help: 'Please enter your password here.',
validate: ['required'],
pointer: {
main: mist.user,
index: 'rawpassword'
}
},{
type: 'buttons',
buttons: [{
label: 'Login',
type: 'save',
'function': function(){
mist.user.password = MD5(mist.user.rawpassword);
delete mist.user.rawpassword;
mist.send(function(){
UI.navto('Overview');
});
}
}]
}
]));
break;
case 'Create a new account':
UI.elements.menu.addClass('hide');
$main.append(
$(' ').text('No account has been created yet in the MistServer at ').append(
$('').text(mist.user.host)
).append('.')
);
$main.append(UI.buildUI([
{
type: 'buttons',
buttons: [{
label: 'Select other host',
type: 'cancel',
css: {'float': 'left'},
'function': function(){
UI.navto('Login');
}
}]
},{
type: 'custom',
custom: $(' ')
},{
label: 'Desired username',
type: 'str',
validate: ['required'],
help: 'Enter your desired username. In the future, you will need this to access the Management Interface.',
pointer: {
main: mist.user,
index: 'name'
}
},{
label: 'Desired password',
type: 'password',
validate: ['required',function(val,me){
$('.match_password').not($(me)).trigger('change');
return false;
}],
help: 'Enter your desired password. In the future, you will need this to access the Management Interface.',
pointer: {
main: mist.user,
index: 'rawpassword'
},
classes: ['match_password']
},{
label: 'Repeat password',
type: 'password',
validate: ['required',function(val,me){
if (val != $('.match_password').not($(me)).val()) {
return {
msg:'The fields "Desired password" and "Repeat password" do not match.',
classes: ['red']
}
}
return false;
}],
help: 'Repeat your desired password.',
classes: ['match_password']
},{
type: 'buttons',
buttons: [{
type: 'save',
label: 'Create new account',
'function': function(){
mist.send(function(){
UI.navto('Account created');
},{
authorize: {
new_username: mist.user.name,
new_password: mist.user.rawpassword
}
});
mist.user.password = MD5(mist.user.rawpassword);
delete mist.user.rawpassword;
}
}]
}]));
break;
case 'Account created':
UI.elements.menu.addClass('hide');;
$main.append(
$('
').text('Your account has been created succesfully.')
).append(UI.buildUI([
{
type: 'text',
text: 'Would you like to enable all (currently) available protocols with their default settings?'
},{
type: 'buttons',
buttons: [{
label: 'Enable protocols',
type: 'save',
'function': function(){
if (mist.data.config.protocols) {
$main.append('Unable to enable all protocols as protocol settings already exist. ');
return;
}
$main.append('Retrieving available protocols.. ');
mist.send(function(d){
var protocols = [];
for (var i in d.capabilities.connectors) {
var connector = d.capabilities.connectors[i];
if (connector.required) {
$main.append('Could not enable protocol "'+i+'" because it has required settings. ');
continue;
}
protocols.push(
{connector: i}
);
$main.append('Enabled protocol "'+i+'". ');
}
$main.append('Saving protocol settings.. ')
mist.send(function(d){
$main.append('Protocols enabled. Redirecting..');
setTimeout(function(){
UI.navto('Overview');
},5000);
},{config:{protocols:protocols}});
},{capabilities:true});
}
},{
label: 'Skip',
type: 'cancel',
'function': function(){
UI.navto('Overview');
}
}]
}
]));
break;
case 'Overview':
var $versioncheck = $('').text('Loading..');
var $streamsactive = $('');
var $errors = $('').addClass('logs');
var $viewers = $('');
var $servertime = $('');
var $protocols_on = $('');
var $protocols_off = $('');
$main.append(UI.buildUI([
{
type: 'help',
help: 'You can find most basic information about your MistServer here. You can also set the debug level and force a save to the config.json file that MistServer uses to save your settings. '
},{
type: 'span',
label: 'Version',
pointer: {
main: mist.data.config,
index: 'version'
}
},{
type: 'span',
label: 'Version check',
value: $versioncheck,
LTSonly: true
},{
type: 'span',
label: 'Server time',
value: $servertime
},{
type: 'span',
label: 'Licensed to',
'default': 'unknown',
pointer: {
main: mist.data.config.license,
index: 'user'
},
LTSonly: true
},{
type: 'span',
label: 'Configured streams',
value: (mist.data.streams ? Object.keys(mist.data.streams).length : 0)
},{
type: 'span',
label: 'Active streams',
value: $streamsactive
},{
type: 'span',
label: 'Current connections',
value: $viewers
},{
type: 'span',
label: 'Enabled protocols',
value: $protocols_on
},{
type: 'span',
label: 'Disabled protocols',
value: $protocols_off
},{
type: 'span',
label: 'Recent problems',
value: $errors
},$(' '),{
type: 'str',
label: 'Human readable name',
pointer: {
main: mist.data.config,
index: 'name'
},
help: 'You can name your MistServer here for personal use. You\'ll still need to set host name within your network yourself.'
},{
type: 'debug',
label: 'Debug level',
pointer: {
main: mist.data.config,
index: 'debug'
},
help: 'You can set the amount of debug information MistServer saves in the log. A full reboot of MistServer is required before some components of MistServer can post debug information.'
},{
type: 'checkbox',
label: 'Force configurations save',
pointer: {
main: mist.data,
index: 'save'
},
help: 'Tick the box in order to force an immediate save to the config.json MistServer uses to save your settings. Saving will otherwise happen upon closing MistServer. Don\'t forget to press save after ticking the box.'
},{
type: 'buttons',
buttons: [{
type: 'save',
label: 'Save',
'function': function(){
var send = {config: mist.data.config};
if (mist.data.save) {
send.save = mist.data.save;
}
mist.send(function(){
UI.navto('Overview');
},send)
}
}]
}
]));
if (mist.data.LTS) {
function update_update(info) {
if (!('uptodate' in info)) {
$versioncheck.text('Unknown');
return;
}
else if (info.error) {
$versioncheck.addClass('red').text(info.error);
return;
}
else if (info.uptodate) {
$versioncheck.text('Your version is up to date.').addClass('green');
return;
}
else {
$versioncheck.addClass('red').text('Version outdated!').append(
$('').text('Update').css({'font-size':'1em','margin-left':'1em'}).click(function(){
if (confirm('Are you sure you want to execute a rolling update?')) {
$versioncheck.addClass('orange').removeClass('red').text('Rolling update command sent..');
mist.stored.del('update');
mist.send(function(d){
UI.navto('Overview');
},{autoupdate: true});
}
})
);
}
}
if ((!mist.stored.get().update) || ((new Date()).getTime()-mist.stored.get().update.lastchecked > 3600e3)) {
var update = {};
update.lastchecked = (new Date()).getTime();
mist.send(function(d){
mist.stored.set('update',update);
update_update(d.update);
},{checkupdate: true});
}
else {
mist.send(function(d){
update_update(d.update);
},{update: true});
}
}
else {
$versioncheck.text('');
}
function updateViewers() {
var request = {
totals:{
fields: ['clients'],
start: -10
},
active_streams: true
};
if (!('cabailities' in mist.data)) {
request.capabilities = true;
}
mist.send(function(d){
enterStats()
},request);
}
function enterStats() {
if ('active_streams' in mist.data) {
var active = (mist.data.active_streams ? mist.data.active_streams.length : 0)
}
else {
var active = '?';
}
$streamsactive.text(active);
if (('totals' in mist.data) && ('all_streams' in mist.data.totals)) {
var clients = mist.data.totals.all_streams.all_protocols.clients;
clients = (clients.length ? UI.format.number(clients[clients.length-1][1]) : 0);
}
else {
clients = 'Loading..';
}
$viewers.text(clients);
$servertime.text(UI.format.dateTime(mist.data.config.time,'long'));
$errors.html('');
var n = 0;
for (var i in mist.data.log) {
var l = mist.data.log[i];
if (['FAIL','ERROR'].indexOf(l[1]) > -1) {
n++;
var $content = $('').addClass('content').addClass('red');
var split = l[2].split('|');
for (var i in split) {
$content.append(
$('').text(split[i])
);
}
$errors.append(
$('').append(
$('
').append(UI.format.time(l[0]))
).append(
$content
)
);
if (n == 5) { break; }
}
}
if (n == 0) {
$errors.html('None.');
}
var protocols = {
on: [],
off: []
};
for (var i in mist.data.config.protocols) {
var p = mist.data.config.protocols[i];
if (protocols.on.indexOf(p.connector) > -1) { continue; }
protocols.on.push(p.connector);
}
$protocols_on.text((protocols.on.length ? protocols.on.join(', ') : 'None.'));
if ('capabilities' in mist.data) {
for (var i in mist.data.capabilities.connectors) {
if (protocols.on.indexOf(i) == -1) {
protocols.off.push(i);
}
}
$protocols_off.text((protocols.off.length ? protocols.off.join(', ') : 'None.'));
}
else {
$protocols_off.text('Loading..')
}
}
updateViewers();
enterStats();
UI.interval.set(updateViewers,30e3);
break;
case 'Protocols':
if (typeof mist.data.capabilities == 'undefined') {
mist.send(function(d){
UI.navto(tab);
},{capabilities: true});
$main.append('Loading..');
return;
}
var $tbody = $('');
$main.append(
UI.buildUI([{
type: 'help',
help: 'You can find an overview of all the protocols and their relevant information here. You can add, edit or delete protocols.'
}])
).append(
$('').text('Delete all protocols').click(function(){
if (confirm('Are you sure you want to delete all currently configured protocols?')) {
mist.data.config.protocols = [];
mist.send(function(d){
UI.navto('Protocols');
},{config: mist.data.config});
}
})
).append(
$('').text('Enable default protocols').click(function(){
var toenable = Object.keys(mist.data.capabilities.connectors);
for (var i in mist.data.config.protocols) {
var p = mist.data.config.protocols[i];
var index = toenable.indexOf(p.connector)
if (index > -1) {
toenable.splice(index,1);
}
}
var dontskip = [];
for (var i in toenable) {
if ((!('required' in mist.data.capabilities.connectors[toenable[i]])) || (Object.keys(mist.data.capabilities.connectors[toenable[i]].required).length == 0)) {
dontskip.push(toenable[i]);
}
}
var msg = 'Click OK to enable disabled protocols with their default settings:'+"\n ";
if (dontskip.length) {
msg += dontskip.join(', ');
}
else {
msg += 'None.';
}
if (dontskip.length != toenable.length) {
var skip = toenable.filter(function(ele){
return dontskip.indexOf(ele) < 0;
});
msg += "\n\n"+'The following protocols can only be set manually:'+"\n "+skip.join(', ');
}
if (confirm(msg) && dontskip.length) {
for (var i in dontskip) {
mist.data.config.protocols.push({connector: dontskip[i]});
}
mist.send(function(d){
UI.navto('Protocols');
},{config: mist.data.config});
}
})
).append(' ').append(
$('').text('New protocol').click(function(){
UI.navto('Edit Protocol');
}).css('clear','both')
).append(
$('').html(
$('').html(
$('').html(
$('').text('Protocol')
).append(
$(' ').text('Status')
).append(
$(' ').text('Settings')
).append(
$(' ')
)
)
).append(
$tbody
)
);
function updateProtocols() {
function displaySettings(protocol){
var capabilities = mist.data.capabilities.connectors[protocol.connector];
if (!capabilities) {
return '';
}
var str = [];
var types = ['required','optional']
for (var j in types) {
for (var i in capabilities[types[j]]) {
if ((protocol[i]) && (protocol[i] != '')) {
str.push(i+': '+protocol[i]);
}
else if (capabilities[types[j]][i]['default']) {
str.push(i+': '+capabilities[types[j]][i]['default']);
}
}
}
return $('').addClass('description').text(str.join(', '));
}
$tbody.html('');
for (var i in mist.data.config.protocols) {
var protocol = mist.data.config.protocols[i];
$tbody.append(
$('').data('index',i).append(
$('').text(protocol.connector)
).append(
$(' ').html(UI.format.status(protocol))
).append(
$(' ').html(displaySettings(protocol))
).append(
$(' ').css('text-align','right').html(
$('').text('Edit').click(function(){
UI.navto('Edit Protocol',$(this).closest('tr').data('index'));
})
).append(
$('').text('Delete').click(function(){
var index = $(this).closest('tr').data('index');
if (confirm('Are you sure you want to delete the protocol "'+mist.data.config.protocols[index].connector+'"?')) {
mist.data.config.protocols.splice(index,1);
mist.send(function(d){
UI.navto('Protocols');
},{config: mist.data.config});
}
})
)
)
);
}
}
updateProtocols();
UI.interval.set(function(){
mist.send(function(){
updateProtocols();
});
},30e3);
break;
case 'Edit Protocol':
if (typeof mist.data.capabilities == 'undefined') {
mist.send(function(d){
UI.navto(tab,other);
},{capabilities: true});
$main.append('Loading..');
return;
}
var editing = false;
if ((other != '') && (other >= 0)) { editing = true; }
var current = {};
for (var i in mist.data.config.protocols) {
current[mist.data.config.protocols[i].connector] = 1;
}
function buildProtocolSettings(kind) {
var input = mist.data.capabilities.connectors[kind];
var build = mist.convertBuildOptions(input,saveas);
build.push({
type: 'hidden',
pointer: {
main: saveas,
index: 'connector'
},
value: kind
});
build.push({
type: 'buttons',
buttons: [
{
type: 'save',
label: 'Save',
'function': function(){
if (editing) {
mist.data.config.protocols[other] = saveas;
}
else {
if (!mist.data.config.protocols) {
mist.data.config.protocols = [];
}
mist.data.config.protocols.push(saveas);
}
mist.send(function(d){
UI.navto('Protocols');
},{config: mist.data.config});
}
},{
type: 'cancel',
label: 'Cancel',
'function': function(){
UI.navto('Protocols');
}
}
]
});
if (('deps' in input) && (input.deps != '')) {
$t = $('').text('Dependencies:');
$ul = $('');
$t.append($ul);
if (typeof input.deps == 'string') { input.deps = input.deps.split(', '); }
for (var i in input.deps) {
var $li = $('').text(input.deps[i]+' ');
$ul.append($li);
if ((typeof current[input.deps[i]] != 'undefined') || (typeof current[input.deps[i]+'.exe'] != 'undefined')) {
//also check for the windows executable
$li.append(
$('').addClass('green').text('(Configured)')
);
}
else {
$li.append(
$('').addClass('red').text('(Not yet configured)')
);
}
}
build.unshift({
type: 'text',
text: $t[0].innerHTML
});
}
return UI.buildUI(build);
}
var current = {};
for (var i in mist.data.config.protocols) {
current[mist.data.config.protocols[i].connector] = 1;
}
if (!editing) {
//new
$main.html(
$('').text('New Protocol')
);
var saveas = {};
var select = [['','']];
for (var i in mist.data.capabilities.connectors) {
select.push([i,i]);
}
var $cont = $('');
$main.append(UI.buildUI([{
label: 'Protocol',
type: 'select',
select: select,
'function': function(){
if ($(this).getval() == '') { return; }
$cont.html(buildProtocolSettings($(this).getval()));
}
}])).append(
$cont
);
}
else {
//editing
var protocol = mist.data.config.protocols[other];
var saveas = protocol;
$main.find('h2').append(' "'+protocol.connector+'"');
$main.append(buildProtocolSettings(protocol.connector));
}
break;
case 'Streams':
if (!('capabilities' in mist.data)) {
$main.html('Loading..');
mist.send(function(){
UI.navto(tab);
},{capabilities: true});
return;
}
var $switchmode = $('');
var $loading = $('').text('Loading..');
$main.append(
UI.buildUI([{
type: 'help',
help: 'Here you can create, edit or delete new and existing streams. Go to stream preview or embed a video player on your website.'
},
$('').css({
width: '45.25em',
display: 'flex',
'justify-content':'flex-end'
}).append(
$switchmode
).append(
$('
').text('Create a new stream').click(function(){
UI.navto('Edit');
})
)
])
).append($loading);
if (other == '') {
var s = mist.stored.get();
if ('viewmode' in s) {
other = s.viewmode;
}
}
$switchmode.text('Switch to '+(other == 'thumbnails' ? 'list' : 'thumbnail')+' view').click(function(){
mist.stored.set('viewmode',(other == 'thumbnails' ? 'list' : 'thumbnails'));
UI.navto('Streams',(other == 'thumbnails' ? 'list' : 'thumbnails'));
});
var allstreams = $.extend(true,{},mist.data.streams);
function createWcStreamObject(streamname,parent) {
var wcstream = $.extend({},parent);
delete wcstream.meta;
delete wcstream.error;
wcstream.online = 2; //should either be available (2) or active (1)
wcstream.name = streamname;
wcstream.ischild = true;
return wcstream;
}
function createPage(type,streams,folders) {
$loading.remove();
switch (type) {
case 'thumbnails':
var $shortcuts = $('').addClass('preview_icons');
function selectastream(select,folders) {
folders = folders || [];
var saveas = {};
select.sort();
select.unshift('');
$loading.remove();
$main.append(
$('
').text(tab)
).append(UI.buildUI([
{
label: 'Filter the streams',
type: 'datalist',
datalist: select,
pointer: {
main: saveas,
index: 'stream'
},
help: 'If you type something here, the box below will only show streams with names that contain your text.',
'function': function(){
var val = $(this).val();
$shortcuts.children().each(function(){
$(this).hide();
if ($(this).attr('data-stream').indexOf(val) > -1) {
$(this).show();
}
});
}
}
]));
select.shift();
$main.append(
$('').addClass('description').text('Choose a stream below.')
).append($shortcuts);
//if there is a JPG output, add actual thumnails \o/
var thumbnails = false;
///\todo activate this code when the backend is ready
/*
if (UI.findOutput('JPG')) {
var jpgport = false;
//find the http port and make sure JPG is enabled
for (var i in mist.data.config.protocols) {
var protocol = mist.data.config.protocols[i];
if ((protocol.connector == 'HTTP') || (protocol.connector == 'HTTP.exe')) {
jpgport = (protocol.port ? ':'+protocol.port : ':8080');
}
if ((protocol.connector == 'JPG') || (protocol.connector == 'JPG.exe')) {
thumbnails = true;
}
}
if ((thumbnails) && (jpgport)) {
//now we get to use it as a magical function wheee!
jpgport = parseURL(mist.user.host).host+jpgport;
thumbnails = function(streamname) {
return 'http://'+jpgport+'/'+encodeURIComponent(streamname)+'.jpg';
}
}
}
*/
for (var i in select) {
var streamname = select[i];
var source = '';
var $delete = $('').text('Delete').click(function(){
var streamname = $(this).closest('div').attr('data-stream');
if (confirm('Are you sure you want to delete the stream "'+streamname+'"?')) {
delete mist.data.streams[streamname];
var send = {};
if (mist.data.LTS) {
send.deletestream = [streamname];
}
else {
send.streams = mist.data.streams;
}
mist.send(function(d){
UI.navto('Streams');
},send);
}
});
var $edit = $('').text('Settings').click(function(){
UI.navto('Edit',$(this).closest('div').attr('data-stream'));
});
var $preview = $('').text('Preview').click(function(){
UI.navto('Preview',$(this).closest('div').attr('data-stream'));
});
var $embed = $('').text('Embed').click(function(){
UI.navto('Embed',$(this).closest('div').attr('data-stream'));
});
var $image = $('').addClass('image');
if ((thumbnails) && (folders.indexOf(streamname) == -1)) {
//there is a JPG output and this isn't a folder
$image.append(
$(' ').attr('src',thumbnails(streamname)).error(function(){
$(this).hide();
})
);
}
//its a wildcard stream
if (streamname.indexOf('+') > -1) {
var streambits = streamname.split('+');
source = mist.data.streams[streambits[0]].source+streambits[1];
$delete = '';
$edit = '';
$image.addClass('wildcard');
}
else {
source = mist.data.streams[streamname].source;
//its a folder stream
if (folders.indexOf(streamname) > -1) {
$preview = '';
$embed = '';
$image.addClass('folder');
}
}
$shortcuts.append(
$('').append(
$('
').addClass('streamname').text(streamname)
).append(
$image
).append(
$('').addClass('description').text(source)
).append(
$('').addClass('button_container').append(
$edit
).append(
$delete
).append(
$preview
).append(
$embed
)
).attr('title',streamname).attr('data-stream',streamname)
);
}
}
selectastream(streams,folders);
break;
case 'list':
default:
var $tbody = $('').append($('').append('').attr('colspan',6).text('Loading..'));
var $table = $('').html(
$('').html(
$('').html(
$('').text('Stream name').attr('data-sort-type','string').addClass('sorting-asc')
).append(
$(' ').text('Source').attr('data-sort-type','string')
).append(
$(' ').text('Status').attr('data-sort-type','int')
).append(
$(' ').css('text-align','right').text('Connections').attr('data-sort-type','int')
).append(
$(' ')
).append(
$(' ')
)
)
).append($tbody);
$main.append($table);
$table.stupidtable();
function buildStreamTable() {
var i = 0;
$tbody.html('');
/*if (mist.data.LTS) {
//insert active wildcard streams (should overwrite active folder wildcard streams)
for (var i in mist.data.active_streams) {
var streamsplit = mist.data.active_streams[i].split('+');
if (streamsplit.length < 2) { continue; }
if (streamsplit[0] in mist.data.streams) {
var wcstream = createWcStreamObject(mist.data.active_streams[i],mist.data.streams[streamsplit[0]]);
wcstream.online = 1; //it's in active_streams, so it's active. Go figure.
allstreams[mist.data.active_streams[i]] = wcstream;
}
}
}
var streams = Object.keys(allstreams);*/
streams.sort();
for (var s in streams) {
var streamname = streams[s];
var stream;
if (streamname in mist.data.streams) { stream = mist.data.streams[streamname]; }
else { stream = allstreams[streamname]; }
var $viewers = $(' ').css('text-align','right').html($('').addClass('description').text('Loading..'));
var v = 0;
if ((typeof mist.data.totals != 'undefined') && (typeof mist.data.totals[streamname] != 'undefined')) {
var data = mist.data.totals[streamname].all_protocols.clients;
var v = 0;
//get the average value
if (data.length) {
for (var i in data) {
v += data[i][1];
}
v = Math.round(v / data.length);
}
}
$viewers.html(UI.format.number(v));
if ((v == 0) && (stream.online == 1)) {
stream.online = 2;
}
var $buttons = $(' ').css('text-align','right').css('white-space','nowrap');
if ((!('ischild' in stream)) || (!stream.ischild)) {
$buttons.html(
$('').text('Settings').click(function(){
UI.navto('Edit',$(this).closest('tr').data('index'));
})
).append(
$('').text('Delete').click(function(){
var index = $(this).closest('tr').data('index');
if (confirm('Are you sure you want to delete the stream "'+index+'"?')) {
delete mist.data.streams[index];
var send = {};
if (mist.data.LTS) {
send.deletestream = [index];
}
else {
send.streams = mist.data.streams;
}
mist.send(function(d){
UI.navto('Streams');
},send);
}
})
);
}
var $streamnamelabel = $('').text(streamname);
if (stream.ischild) {
$streamnamelabel.css('padding-left','1em');
}
var $online = UI.format.status(stream);
var $preview = $('').text('Preview').click(function(){
UI.navto('Preview',$(this).closest('tr').data('index'));
});
var $embed = $('').text('Embed').click(function(){
UI.navto('Embed',$(this).closest('tr').data('index'));
});
if ('filesfound' in allstreams[streamname]) {
$online.html('');
$preview = '';
$viewers.html('');
$embed = '';
}
$tbody.append(
$('').data('index',streamname).html(
$('').html($streamnamelabel).attr('title',streamname).addClass('overflow_ellipsis')
).append(
$(' ').text(stream.source).attr('title',stream.source).addClass('description').addClass('overflow_ellipsis').css('max-width','20em')
).append(
$(' ').data('sort-value',stream.online).html($online)
).append(
$viewers
).append(
$(' ').css('white-space','nowrap').html(
$preview
).append(
$embed
)
).append(
$buttons
)
);
i++;
}
}
function updateStreams() {
var totals = [];
for (var i in mist.data.active_streams) {
totals.push({
streams: [mist.data.active_streams[i]],
fields: ['clients'],
start: -2
});
}
mist.send(function(){
$.extend(true,allstreams,mist.data.streams);
buildStreamTable();
},{
totals: totals,
active_streams: true
});
}
if (mist.data.LTS) {
//insert folder streams
var browserequests = 0;
var browsecomplete = 0;
for (var s in mist.data.streams) {
var inputs_f = mist.data.capabilities.inputs.Folder || mist.data.capabilities.inputs['Folder.exe'];
if (!inputs_f) { break; }
if (mist.inputMatch(inputs_f.source_match,mist.data.streams[s].source)) {
//this is a folder stream
allstreams[s].source += '*';
allstreams[s].filesfound = null;
mist.send(function(d,opts){
var s = opts.stream;
for (var i in d.browse.files) {
for (var j in mist.data.capabilities.inputs) {
if ((j.indexOf('Buffer') >= 0) || (j.indexOf('Buffer.exe') >= 0) || (j.indexOf('Folder') >= 0) || (j.indexOf('Folder.exe') >= 0)) { continue; }
if (mist.inputMatch(mist.data.capabilities.inputs[j].source_match,'/'+d.browse.files[i])) {
var streamname = s+'+'+d.browse.files[i];
allstreams[streamname] = createWcStreamObject(streamname,mist.data.streams[s]);
allstreams[streamname].source = mist.data.streams[s].source+d.browse.files[i];
}
}
}
if (('files' in d.browse) && (d.browse.files.length)) {
allstreams[s].filesfound = true;
}
else {
mist.data.streams[s].filesfound = false;
}
browsecomplete++;
if (browserequests == browsecomplete) {
mist.send(function(){
updateStreams();
},{active_streams: true});
UI.interval.set(function(){
updateStreams();
},5e3);
}
},{browse:mist.data.streams[s].source},{stream: s});
browserequests++;
}
}
if (browserequests == 0) {
mist.send(function(){
updateStreams();
},{active_streams: true});
UI.interval.set(function(){
updateStreams();
},5e3);
}
}
else {
mist.send(function(){
updateStreams();
},{active_streams: true});
UI.interval.set(function(){
updateStreams();
},5e3);
}
break;
}
}
if (mist.data.LTS) {
//browse into folder streams
var browserequests = 0;
var browsecomplete = 0;
var select = {};
var folders = [];
for (var s in mist.data.streams) {
var inputs_f = mist.data.capabilities.inputs.Folder || mist.data.capabilities.inputs['Folder.exe'];
if (mist.inputMatch(inputs_f.source_match,mist.data.streams[s].source)) {
//this is a folder stream
folders.push(s);
mist.send(function(d,opts){
var s = opts.stream;
for (var i in d.browse.files) {
for (var j in mist.data.capabilities.inputs) {
if ((j.indexOf('Buffer') >= 0) || (j.indexOf('Folder') >= 0)) { continue; }
if (mist.inputMatch(mist.data.capabilities.inputs[j].source_match,'/'+d.browse.files[i])) {
select[s+'+'+d.browse.files[i]] = true;
}
}
}
browsecomplete++;
if (browserequests == browsecomplete) {
mist.send(function(){
for (var i in mist.data.active_streams) {
var split = mist.data.active_streams[i].split('+');
if ((split.length > 1) && (split[0] in mist.data.streams)) {
select[mist.data.active_streams[i]] = true;
allstreams[mist.data.active_streams[i]] = createWcStreamObject(mist.data.active_streams[i],mist.data.streams[split[0]]);
}
}
select = Object.keys(select);
select = select.concat(Object.keys(mist.data.streams));
select.sort();
createPage(other,select,folders);
},{active_streams: true});
}
},{browse:mist.data.streams[s].source},{stream: s});
browserequests++;
}
}
if (browserequests == 0) {
mist.send(function(){
//var select = [];
for (var i in mist.data.active_streams) {
var split = mist.data.active_streams[i].split('+');
if ((split.length > 1) && (split[0] in mist.data.streams)) {
select[mist.data.active_streams[i]] = true;
allstreams[mist.data.active_streams[i]] = createWcStreamObject(mist.data.active_streams[i],mist.data.streams[split[0]]);
}
}
select = Object.keys(select);
if (mist.data.streams) { select = select.concat(Object.keys(mist.data.streams)); }
select.sort();
createPage(other,select);
},{active_streams: true});
}
}
else {
createPage(other,Object.keys(mist.data.streams));
}
break;
case 'Edit':
if (typeof mist.data.capabilities == 'undefined') {
mist.send(function(d){
UI.navto(tab,other);
},{capabilities: true});
$main.append('Loading..');
return;
}
var editing = false;
if (other != '') { editing = true; }
if (!editing) {
//new
$main.html(
$('').text('New Stream')
);
var saveas = {};
}
else {
//editing
var streamname = other;
var saveas = mist.data.streams[streamname];
$main.find('h2').append(' "'+streamname+'"');
}
var filetypes = [];
for (var i in mist.data.capabilities.inputs) {
filetypes.push(mist.data.capabilities.inputs[i].source_match);
}
var $inputoptions = $(' ');
function save(tab) {
if (!mist.data.streams) {
mist.data.streams = {};
}
mist.data.streams[saveas.name] = saveas;
if (other != saveas.name) {
delete mist.data.streams[other];
}
var send = {};
if (mist.data.LTS) {
send.addstream = {};
send.addstream[saveas.name] = saveas;
if (other != saveas.name) {
send.deletestream = [other];
}
}
else {
send.streams = mist.data.streams;
}
if ((saveas.stop_sessions) && (other != '')) {
send.stop_sessions = other;
delete saveas.stop_sessions;
}
mist.send(function(){
delete mist.data.streams[saveas.name].online;
delete mist.data.streams[saveas.name].error;
UI.navto(tab,(tab == 'Preview' ? saveas.name : ''));
},send);
}
var $style = $('