123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113 |
- scApp.controller("CSCMain", ["$scope", "$interval", "$location", "MSCApi", "$sc_utility", "$sc_server", '$sc_nav', function($scope, $interval, $location, MSCApi, $sc_utility, $sc_server, $sc_nav){
- $scope.logs = [];
- // remove expired alert.
- $interval(function(){
- for (var i = 0; i < $scope.logs.length; i++) {
- var log = $scope.logs[i];
- if (log.create + 10000 < new Date().getTime()) {
- $scope.logs.splice(i, 1);
- break;
- }
- }
- }, 3000);
- // handler system log event, from $sc_utility service.
- $scope.$on("$sc_utility_log", function(event, level, msg){
- var log = {
- level:level, msg:msg, create:new Date().getTime()
- };
- // only show 3 msgs.
- while ($scope.logs.length > 2) {
- $scope.logs.splice(0, 1);
- }
- $scope.logs.push(log);
- });
- // handle system error event, from $sc_utility service.
- $scope.$on("$sc_utility_http_error", function(event, status, response){
- var code = response.code;
- if (status !== 200) {
- if (!status && !response) {
- response = "无法访问服务器";
- } else {
- response = "HTTP/" + status + ", " + response;
- }
- } else {
- var map = {
- 1061: "RAW API被禁用",
- 1062: "服务器不允许这个操作",
- 1063: "RawApi参数不符合要求"
- };
- if (map[response.code]) {
- response = "code=" + response.code + ", " + map[response.code];
- } else {
- resonse = "code=" + response.code + ", 系统错误";
- }
- }
- if (code === 1061) {
- $sc_utility.log("trace", response);
- return;
- }
- $sc_utility.log("warn", response);
- });
- $scope.gogogo = function (path) {
- $location.path(path);
- };
- $scope.goto = function (path) {
- var absUrl = $sc_server.buildNavUrl();
- var url = absUrl.replace($location.$$path, path);
- var win = window.open('', '_self');
- win.location = url; // For safari security reason, we should set the location instead.
- };
- $scope.redirect = function (from, to) {
- var absUrl = $sc_server.buildNavUrl();
- var url = absUrl.replace(from, to);
- var win = window.open('', '_self');
- win.location = url; // For safari security reason, we should set the location instead.
- };
- // init the server and port for api.
- $sc_server.init($location, MSCApi);
- //$sc_utility.log("trace", "set baseurl to " + $sc_server.baseurl());
- }]);
- scApp.controller("CSCConnect", ["$scope", "$location", "MSCApi", "$sc_utility", "$sc_nav", "$sc_server", function($scope, $location, MSCApi, $sc_utility, $sc_nav, $sc_server){
- $sc_nav.in_control();
- $scope.server = {
- schema: $sc_server.schema,
- ip: $sc_server.host,
- port: $sc_server.port
- };
- $scope.connect = function(){
- $sc_server.schema = $scope.server.schema;
- $sc_server.host = $scope.server.ip;
- $sc_server.port = $scope.server.port;
- MSCApi.versions_get(function(data){
- $sc_utility.log("trace", "连接到SRS" + $scope.server.ip + "成功, SRS/" + data.data.version);
- $scope.goto('/summaries');
- });
- };
- $sc_utility.refresh.stop();
- }]);
- scApp.controller("CSCSummary", ["$scope", "MSCApi", "$sc_utility", "$sc_nav", function($scope, MSCApi, $sc_utility, $sc_nav){
- $sc_nav.in_summary();
- $scope.pre_kbps = null;
- $sc_utility.refresh.refresh_change(function(){
- MSCApi.summaries_get(function(data){
- var kbps = {
- initialized: false,
- in: { srs: 0, sys: 0, inner: 0 },
- out: { srs: 0, sys: 0, inner: 0 }
- };
- if ($scope.pre_kbps) {
- kbps.initialized = true;
- var diff = data.data.system.net_sample_time - $scope.pre_kbps.system.net_sample_time;
- if (diff > 0) {
- kbps.in.sys = (data.data.system.net_recv_bytes - $scope.pre_kbps.system.net_recv_bytes) * 8 / diff;
- kbps.in.inner = (data.data.system.net_recvi_bytes - $scope.pre_kbps.system.net_recvi_bytes) * 8 / diff;
- kbps.out.sys = (data.data.system.net_send_bytes - $scope.pre_kbps.system.net_send_bytes) * 8 / diff;
- kbps.out.inner = (data.data.system.net_sendi_bytes - $scope.pre_kbps.system.net_sendi_bytes) * 8 / diff;
- }
- diff = data.data.system.srs_sample_time - $scope.pre_kbps.system.srs_sample_time;
- if (diff > 0) {
- kbps.in.srs = (data.data.system.srs_recv_bytes - $scope.pre_kbps.system.srs_recv_bytes) * 8 / diff;
- kbps.out.srs = (data.data.system.srs_send_bytes - $scope.pre_kbps.system.srs_send_bytes) * 8 / diff;
- }
- diff = data.data.now_ms - $scope.pre_kbps.now_ms;
- if (!$scope.kbps.initialized || diff >= 20 * 1000) {
- $scope.pre_kbps = data.data;
- $scope.kbps = kbps;
- }
- }
- if (!$scope.kbps) {
- $scope.pre_kbps = data.data;
- $scope.kbps = kbps;
- }
- $scope.global = data.data;
- $scope.server = data.data.self;
- $scope.system = data.data.system;
- $sc_utility.refresh.request();
- });
- }, 3000);
- $sc_utility.log("trace", "Retrieve summary from SRS.");
- $sc_utility.refresh.request(0);
- }]);
- scApp.controller("CSCVhosts", ["$scope", "MSCApi", "$sc_nav", "$sc_utility", function($scope, MSCApi, $sc_nav, $sc_utility){
- $sc_nav.in_vhosts();
- $sc_utility.refresh.refresh_change(function(){
- MSCApi.vhosts_get(function(data){
- $scope.vhosts = data.vhosts;
- $sc_utility.refresh.request();
- });
- }, 3000);
- $sc_utility.log("trace", "Retrieve vhosts from SRS");
- $sc_utility.refresh.request(0);
- }]);
- scApp.controller("CSCVhost", ["$scope", "$routeParams", "MSCApi", "$sc_nav", "$sc_utility", function($scope, $routeParams, MSCApi, $sc_nav, $sc_utility){
- $sc_nav.in_vhosts();
- $sc_utility.refresh.stop();
- MSCApi.vhosts_get2($routeParams.id, function(data){
- $scope.vhost = data.vhost;
- });
- $sc_utility.log("trace", "Retrieve vhost info from SRS");
- }]);
- scApp.controller("CSCStreams", ["$scope", "$location", "MSCApi", "$sc_nav", "$sc_utility", function($scope, $location, MSCApi, $sc_nav, $sc_utility){
- $sc_nav.in_streams();
- $scope.kickoff = function(stream) {
- MSCApi.clients_delete(stream.publish.cid, function(){
- $sc_utility.log("warn", "Kickoff stream ok.");
- });
- };
- $scope.dvr = function(stream){
- $location.path($sc_utility.dvr_id(stream));
- };
- $scope.support_raw_api = false;
- MSCApi.configs_raw(function(data) {
- $scope.support_raw_api = $sc_utility.raw_api_enabled(data);
- });
- MSCApi.vhosts_get(function(data){
- var vhosts = data.vhosts;
- $sc_utility.refresh.refresh_change(function(){
- MSCApi.streams_get(function(data){
- for (var k in data.streams) {
- var stream = data.streams[k];
- stream.owner = system_array_get(vhosts, function(vhost) {return vhost.id === stream.vhost; });
- }
- $scope.streams = data.streams;
- $sc_utility.refresh.request();
- });
- }, 3000);
- $sc_utility.log("trace", "Retrieve streams from SRS");
- $sc_utility.refresh.request(0);
- });
- $sc_utility.log("trace", "Retrieve vhost info from SRS");
- }]);
- scApp.controller("CSCStream", ["$scope", '$location', "$routeParams", "MSCApi", "$sc_nav", "$sc_utility", function($scope, $location, $routeParams, MSCApi, $sc_nav, $sc_utility){
- $sc_nav.in_streams();
- $scope.kickoff = function(stream) {
- MSCApi.clients_delete(stream.publish.cid, function(){
- $sc_utility.log("warn", "Kickoff stream ok.");
- });
- };
- $sc_utility.refresh.stop();
- $scope.dvr = function(stream){
- $location.path($sc_utility.dvr_id(stream));
- };
- $scope.support_raw_api = false;
- MSCApi.configs_raw(function(data) {
- $scope.support_raw_api = $sc_utility.raw_api_enabled(data);
- });
- MSCApi.streams_get2($routeParams.id, function(data){
- var stream = data.stream;
- if (!$scope.owner) {
- MSCApi.vhosts_get2(stream.vhost, function(data) {
- var vhost = data.vhost;
- stream.owner = $scope.owner = vhost;
- $scope.stream = stream;
- });
- $sc_utility.log("trace", "Retrieve vhost info from SRS");
- } else {
- stream.owner = $scope.owner;
- $scope.stream = stream;
- }
- });
- $sc_utility.log("trace", "Retrieve stream info from SRS");
- }]);
- scApp.controller("CSCClients", ["$scope", "MSCApi", "$sc_nav", "$sc_utility", function($scope, MSCApi, $sc_nav, $sc_utility){
- $sc_nav.in_clients();
- $scope.kickoff = function(client) {
- MSCApi.clients_delete(client.id, function(){
- $sc_utility.log("warn", "Kickoff client ok.");
- });
- };
- $sc_utility.refresh.refresh_change(function(){
- MSCApi.clients_get(function(data){
- $scope.clients = data.clients;
- $sc_utility.refresh.request();
- });
- }, 3000);
- $sc_utility.log("trace", "Retrieve clients from SRS");
- $sc_utility.refresh.request(0);
- }]);
- scApp.controller("CSCClient", ["$scope", "$routeParams", "MSCApi", "$sc_nav", "$sc_utility", function($scope, $routeParams, MSCApi, $sc_nav, $sc_utility){
- $sc_nav.in_clients();
- $scope.kickoff = function(client) {
- MSCApi.clients_delete(client.id, function(){
- $sc_utility.log("warn", "Kickoff client ok.");
- });
- };
- $sc_utility.refresh.stop();
- MSCApi.clients_get2($routeParams.id, function(data){
- $scope.client = data.client;
- });
- $sc_utility.log("trace", "Retrieve client info from SRS");
- }]);
- scApp.controller("CSCConfigs", ["$scope", "$location", "MSCApi", "$sc_nav", "$sc_utility", "$sc_server", function($scope, $location, MSCApi, $sc_nav, $sc_utility, $sc_server){
- $sc_nav.in_configs();
- $sc_utility.refresh.stop();
- MSCApi.configs_raw(function(data){
- $scope.http_api = data.http_api;
- });
- $sc_utility.log("trace", "Retrieve config info from SRS");
- }]);
- scApp.factory("MSCApi", ["$http", "$sc_server", function($http, $sc_server){
- return {
- versions_get: function(success) {
- var url = $sc_server.jsonp("/api/v1/versions");
- $http.jsonp(url).success(success);
- },
- summaries_get: function(success) {
- var url = $sc_server.jsonp("/api/v1/summaries");
- $http.jsonp(url).success(success);
- },
- vhosts_get: function(success) {
- var url = $sc_server.jsonp("/api/v1/vhosts/");
- $http.jsonp(url).success(success);
- },
- vhosts_get2: function(id, success) {
- var url = $sc_server.jsonp("/api/v1/vhosts/" + id);
- $http.jsonp(url).success(success);
- },
- streams_get: function(success) {
- var url = $sc_server.jsonp("/api/v1/streams/");
- $http.jsonp(url).success(success);
- },
- streams_get2: function(id, success) {
- var url = $sc_server.jsonp("/api/v1/streams/" + id);
- $http.jsonp(url).success(success);
- },
- clients_get: function(success) {
- var url = $sc_server.jsonp("/api/v1/clients/");
- $http.jsonp(url).success(success);
- },
- clients_get2: function(id, success) {
- var url = $sc_server.jsonp("/api/v1/clients/" + id);
- $http.jsonp(url).success(success);
- },
- clients_delete: function(id, success) {
- var url = $sc_server.jsonp_delete("/api/v1/clients/" + id);
- $http.jsonp(url).success(success);
- },
- configs_raw: function(success, error) {
- var url = $sc_server.jsonp_query("/api/v1/raw", "rpc=raw");
- var obj = $http.jsonp(url).success(success);
- if (error) {
- obj.error(error);
- }
- },
- configs_get: function(success) {
- var url = $sc_server.jsonp_query("/api/v1/raw", "rpc=query&scope=global");
- $http.jsonp(url).success(success);
- },
- };
- }]);
- scApp.filter("sc_filter_log_level", function(){
- return function(v) {
- return (v === "warn" || v === "error")? "alert-warn":"alert-success";
- };
- });
- scApp.filter("sc_filter_nav_active", ["$sc_nav", function($sc_nav){
- return function(v){
- return $sc_nav.is_selected(v)? "active":"";
- };
- }]);
- scApp.filter("sc_unsafe", ['$sce', function($sce){
- return function(v) {
- return $sce.trustAsHtml(v);
- };
- }]);
- scApp.filter("sc_filter_filesize_k", function(){
- return function(v){
- // PB
- if (v > 1024 * 1024 * 1024 * 1024) {
- return Number(v / 1024.0 / 1024 / 1024 / 1024).toFixed(2) + "PB";
- }
- // TB
- if (v > 1024 * 1024 * 1024) {
- return Number(v / 1024.0 / 1024 / 1024).toFixed(2) + "TB";
- }
- // GB
- if (v > 1024 * 1024) {
- return Number(v / 1024.0 / 1024).toFixed(2) + "GB";
- }
- // MB
- if (v > 1024) {
- return Number(v / 1024.0).toFixed(2) + "MB";
- }
- return Number(v).toFixed(2) + "KB";
- };
- });
- scApp.filter("sc_filter_filesize_k2", function(){
- return function(v){
- // PB
- if (v > 1024 * 1024 * 1024 * 1024) {
- return Number(v / 1024.0 / 1024 / 1024 / 1024).toFixed(0) + "PB";
- }
- // TB
- if (v > 1024 * 1024 * 1024) {
- return Number(v / 1024.0 / 1024 / 1024).toFixed(0) + "TB";
- }
- // GB
- if (v > 1024 * 1024) {
- return Number(v / 1024.0 / 1024).toFixed(0) + "GB";
- }
- // MB
- if (v > 1024) {
- return Number(v / 1024.0).toFixed(0) + "MB";
- }
- return Number(v).toFixed(0) + "KB";
- };
- });
- scApp.filter("sc_filter_filerate_k", function(){
- return function(v){
- // PB
- if (v > 1024 * 1024 * 1024 * 1024) {
- return Number(v / 1024.0 / 1024 / 1024 / 1024).toFixed(2) + "PBps";
- }
- // TB
- if (v > 1024 * 1024 * 1024) {
- return Number(v / 1024.0 / 1024 / 1024).toFixed(2) + "TBps";
- }
- // GB
- if (v > 1024 * 1024) {
- return Number(v / 1024.0 / 1024).toFixed(2) + "GBps";
- }
- // MB
- if (v > 1024) {
- return Number(v / 1024.0).toFixed(2) + "MBps";
- }
- return Number(v).toFixed(2) + "KBps";
- };
- });
- scApp.filter("sc_filter_filerate_k2", function(){
- return function(v){
- // PB
- if (v > 1024 * 1024 * 1024 * 1024) {
- return Number(v / 1024.0 / 1024 / 1024 / 1024).toFixed(0) + "PBps";
- }
- // TB
- if (v > 1024 * 1024 * 1024) {
- return Number(v / 1024.0 / 1024 / 1024).toFixed(0) + "TBps";
- }
- // GB
- if (v > 1024 * 1024) {
- return Number(v / 1024.0 / 1024).toFixed(0) + "GBps";
- }
- // MB
- if (v > 1024) {
- return Number(v / 1024.0).toFixed(0) + "MBps";
- }
- return Number(v).toFixed(0) + "KBps";
- };
- });
- scApp.filter("sc_filter_bitrate_k", function(){
- return function(v){
- // PB
- if (v > 1000 * 1000 * 1000 * 1000) {
- return Number(v / 1000.0 / 1000 / 1000 / 1000).toFixed(2) + "Pbps";
- }
- // TB
- if (v > 1000 * 1000 * 1000) {
- return Number(v / 1000.0 / 1000 / 1000).toFixed(2) + "Tbps";
- }
- // GB
- if (v > 1000 * 1000) {
- return Number(v / 1000.0 / 1000).toFixed(2) + "Gbps";
- }
- // MB
- if (v > 1000) {
- return Number(v / 1000.0).toFixed(2) + "Mbps";
- }
- return Number(v).toFixed(2) + "Kbps";
- };
- });
- scApp.filter("sc_filter_bitrate_k2", function(){
- return function(v){
- // PB
- if (v > 1000 * 1000 * 1000 * 1000) {
- return Number(v / 1000.0 / 1000 / 1000 / 1000).toFixed(0) + "Pbps";
- }
- // TB
- if (v > 1000 * 1000 * 1000) {
- return Number(v / 1000.0 / 1000 / 1000).toFixed(0) + "Tbps";
- }
- // GB
- if (v > 1000 * 1000) {
- return Number(v / 1000.0 / 1000).toFixed(0) + "Gbps";
- }
- // MB
- if (v > 1000) {
- return Number(v / 1000.0).toFixed(0) + "Mbps";
- }
- return Number(v).toFixed(0) + "Kbps";
- };
- });
- scApp.filter("sc_filter_percent", function(){
- return function(v){
- return Number(v).toFixed(2) + "%";
- };
- });
- scApp.filter("sc_filter_percent2", function(){
- return function(v){
- return Number(v).toFixed(0) + "%";
- };
- });
- scApp.filter("sc_filter_percentf", function(){
- return function(v){
- return Number(v * 100).toFixed(2) + "%";
- };
- });
- scApp.filter("sc_filter_percentf2", function(){
- return function(v){
- return Number(v * 100).toFixed(0) + "%";
- };
- });
- scApp.filter("sc_filter_video", function(){
- return function(v){
- // set default value for SRS2.
- v.width = v.width? v.width : 0;
- v.height = v.height? v.height : 0;
- return v? v.codec + "/" + v.profile + "/" + v.level + "/" + v.width + "x" + v.height : "无视频";
- };
- });
- scApp.filter("sc_filter_audio", function(){
- return function(v){
- return v? v.codec + "/" + v.sample_rate + "/" + (v.channel === 2? "Stereo":"Mono") + "/" + v.profile : "无音频";
- };
- });
- scApp.filter("sc_filter_number", function(){
- return function(v){
- return Number(v).toFixed(2);
- };
- });
- scApp.filter("sc_filter_less", function(){
- return function(v) {
- return v? (v.length > 15? v.slice(0, 15) + "...":v):v;
- };
- });
- scApp.filter('sc_filter_style_error', function(){
- return function(v){
- return v? 'alert-danger':'';
- };
- });
- scApp.filter('sc_filter_preview_url', ['$sc_server', function($sc_server){
- return function(v){
- var page = $sc_server.schema + `://${$sc_server.host}:${$sc_server.http}/players/srs_player.html`;
- var http = $sc_server.http[$sc_server.http.length - 1];
- var query = "vhost=" + v.owner.name + "&app=" + v.app + "&stream=" + v.name + ".flv";
- query += "&server=" + $sc_server.host +"&port=" + http + "&autostart=true&schema=" + $sc_server.schema;
- return v? page+"?" + query:"javascript:void(0)";
- };
- }]);
- scApp.filter('sc_filter_streamURL', function(){
- return function(v){
- if (!v || !v.url) return '';
- const pos = v.url.lastIndexOf('/');
- const stream = pos < 0 ? '' : v.url.substr(pos);
- // Use name or extract from url.
- let streamName = v.name ? v.name : stream;
- if (streamName && streamName.indexOf('/') !== 0) streamName = `/${streamName}`;
- const pos2 = v.tcUrl.indexOf('?');
- const tcUrl = pos2 < 0 ? v.tcUrl : v.tcUrl.substr(0, pos2);
- let params = pos2 < 0 ? '' : v.tcUrl.substr(pos2);
- if (params === '?vhost=__defaultVhost__' || params === '?domain=__defaultVhost__') params = '';
- return `${tcUrl}${streamName}${params}`;
- };
- });
- // the sc nav is the nevigator
- scApp.provider("$sc_nav", function(){
- this.$get = function(){
- return {
- selected: null,
- in_control: function(){
- this.selected = "/console";
- },
- in_summary: function(){
- this.selected = "/summaries";
- },
- in_vhosts: function(){
- this.selected = "/vhosts";
- },
- in_streams: function(){
- this.selected = "/streams";
- },
- in_clients: function(){
- this.selected = "/clients";
- },
- in_configs: function(){
- this.selected = "/configs";
- },
- go_summary: function($location){
- $location.path("/summaries");
- },
- is_selected: function(v){
- return v === this.selected;
- }
- };
- };
- });
- // the sc server is the server we connected to.
- scApp.provider("$sc_server", [function(){
- this.$get = function(){
- var self = {
- schema: "http",
- host: null,
- port: 1985,
- rtmp: [1935],
- http: [8080],
- baseurl: function(){
- return self.schema + "://" + self.host + (self.port === 80? "": ":" + self.port);
- },
- sstq: function () {
- const s = localStorage.getItem('SRS_TERRAFORM_TOKEN');
- const obj = s ? JSON.parse(s) : {};
- return obj.token ? `&token=${obj.token}` : '';
- },
- jsonp: function(url){
- return self.baseurl() + url + "?callback=JSON_CALLBACK" + self.sstq();
- },
- jsonp_delete: function(url) {
- return self.jsonp(url) + "&method=DELETE" + self.sstq();
- },
- jsonp_query: function(url, query){
- return self.baseurl() + url + "?callback=JSON_CALLBACK&" + query + self.sstq();
- },
- buildNavUrl: function () {
- var $location = self.$location;
- var url = $location.absUrl();
- if (url.indexOf('?') > 0) {
- url = url.slice(0, url.indexOf('?'));
- }
- url += '?';
- var query = {};
- for (var key in $location.search()) {
- query[key] = $location.search()[key];
- }
- query['schema'] = self.schema;
- query['host'] = self.host;
- query['port'] = self.port;
- var queries = [];
- for (var key in query) {
- var value = query[key];
- if (!Array.isArray(value)) {
- value = [value];
- }
- for (var i in value) {
- queries.push(key + '=' + value[i]);
- }
- }
- url += queries.join('&');
- return url;
- },
- init: function($location, MSCApi) {
- self.$location = $location;
- // Set the right schema for proxy.
- if ($location.search().schema) {
- self.schema = $location.search().schema;
- } else {
- self.schema = $location.protocol();
- }
- // query string then url.
- if ($location.search().host) {
- self.host = $location.search().host;
- } else {
- self.host = $location.host();
- }
- if ($location.search().port) {
- self.port = $location.search().port;
- } else {
- self.port = $location.port();
- }
- if ($location.search().http) {
- self.http = [$location.search().http];
- }
- }
- };
- return self;
- };
- }]);
- // the sc utility is a set of helper utilities.
- scApp.provider("$sc_utility", function(){
- this.$get = ["$rootScope", function($rootScope){
- return {
- log: function(level, msg) {
- $rootScope.$broadcast("$sc_utility_log", level, msg);
- },
- http_error: function(status, response) {
- $rootScope.$broadcast("$sc_utility_http_error", status, response);
- },
- find_siblings: function(elem, className) {
- if (elem.hasClass(className)) {
- return elem;
- }
- if (!elem[0].nextSibling) {
- return null;
- }
- var sibling = angular.element(elem[0].nextSibling);
- return this.find_siblings(sibling, className);
- },
- array_actual_equals: function(a, b) {
- // all elements of a in b.
- for (var i = 0; i < a.length; i++) {
- if (!system_array_contains(b, a[i])) {
- return false;
- }
- }
- // all elements of b in a.
- for (i = 0; i < b.length; i++) {
- if (!system_array_contains(a, b[i])) {
- return false;
- }
- }
- return true;
- },
- object2arr: function(obj, each) {
- var arr = [];
- for (var k in obj) {
- var v = obj[k];
- if (each) {
- each(v);
- }
- arr.push(v);
- }
- return arr;
- },
- /**
- * transform the api data to angularjs perfer, for instance:
- data.global.listen = ["1935, "1936"];
- data.global.http_api.listen = "1985";
- * parsed to:
- global.listen = {
- key: 'listen',
- value: ["1935", "1936"],
- error: false
- }
- global.http_api.listen = {
- key: 'http_api.listen',
- value: "1985",
- error: false
- }
- * where the error is used for commit error.
- */
- object2complex: function(complex, obj, prefix) {
- for (var k in obj) {
- var v = obj[k];
- //console.log("k=" + key + ", v=" + typeof v + ", " + v);
- var key = prefix? prefix + "." + k : k;
- if (key === "vhosts") {
- // use name as vhost id.
- complex[k] = this.object2arr(v, function(e) { e.vid = e.name; });
- continue;
- }
- if (typeof v === "object" && v.constructor !== Array) {
- var cv = {};
- complex[k] = cv;
- this.object2complex(cv, v, key);
- continue;
- }
- // convert number to str for select to
- // choose the right default one.
- if (key === "pithy_print_ms") {
- v = String(v);
- }
- complex[k] = {
- key: system_string_trim(key, 'global.'),
- value: v,
- error: false
- };
- }
- return complex;
- },
- copy_object: function(dst, src) {
- for (var k in src) {
- dst[k] = src[k];
- }
- },
- dvr_id: function(stream) {
- var url = '/dvr/' + stream.owner.name
- + '/' + stream.id
- + '/' + escape(stream.app.replace('/', '___'))
- + '/' + escape(stream.name.replace('/', '___'));
- return url;
- },
- raw_api_enabled: function(data) {
- return data.http_api && data.http_api.enabled && data.http_api.raw_api && data.http_api.raw_api.enabled;
- },
- refresh: async_refresh2
- };
- }];
- });
- // sc-collapse: scCollapse
- /**
- * Usage:
- <div class="accordion">
- <div class="accordion-group">
- <div class="accordion-heading" sc-collapse="in">
- <a class="accordion-toggle" href="javascript:void(0)">
- HTTP RAW API
- </a>
- </div>
- <div class="accordion-body collapse">
- <div class="accordion-inner">
- 该服务器不支持HTTP RAW API,或者配置中禁用了该功能。
- </div>
- </div>
- </div>
- </div>
- */
- scApp.directive('scCollapse', ["$sc_utility", function($sc_utility){
- return {
- restrict: 'A',
- scope: true,
- controller: ['$scope', function($scope) {
- }],
- compile: function(elem, attrs) {
- return function(scope, elem, attrs){
- if (attrs.scCollapse === "in") {
- var obj = $sc_utility.find_siblings(elem, 'accordion-body');
- obj.addClass('in');
- }
- elem.on('click', function(){
- var obj = $sc_utility.find_siblings(elem, 'accordion-body');
- obj.toggleClass('in');
- });
- };
- }
- };
- }]);
- // sc-pretty: scPretty
- /**
- * Usage:
- <tr sc-pretty scp-key="http_api.enabled" scp-value="http_api.enabled" scp-bool="true"
- scp-desc="是否开启HTTP API,开启后就可以访问SRS提供的API管理服务器。默认: {{false| sc_filter_enabled}}">
- </tr>
- */
- scApp.directive("scPretty", [function(){
- return {
- restrict: 'A',
- scope: {
- key: '@scpKey',
- value: '=scpValue',
- desc: '@scpDesc',
- bool: '@scpBool'
- },
- template: ''
- + '<td>{{key}}</td>'
- + '<td>'
- + '<span class="{{value == undefined? \'label\':\'\'}}">'
- + '<span ng-show="bool && value != undefined">{{value| sc_filter_enabled}}</span>'
- + '<span ng-show="!bool || value === undefined">{{value| sc_filter_obj}}</span>'
- + '</span>'
- + '</td>'
- + '<td>{{desc}}</td>'
- + '<td>只读</td>'
- };
- }]);
- // sc-pretty2: scPretty2
- /**
- * Usage:
- <tr sc-pretty2 scp-data="global.daemon" scp-bool="true"
- scp-desc="是否以后台启动SRS。默认: {{true| sc_filter_yesno}}">
- </tr>
- */
- scApp.directive("scPretty2", [function(){
- return {
- restrict: 'A',
- scope: {
- data: '=scpData',
- desc: '@scpDesc',
- bool: '@scpBool',
- link: '@scpLink'
- },
- controller: ['$scope', function($scope){
- }],
- template: ''
- + '<td>{{key}}</td>'
- + '<td>'
- + '<span ng-if="link">'
- + '<a href="{{link}}">{{data.value}}</a>'
- + '</span>'
- + '<span class="{{data.value == undefined? \'label\':\'\'}}" ng-if="!link">'
- + '<span ng-show="bool && data.value != undefined">{{data.value| sc_filter_enabled}}</span>'
- + '<span ng-show="!bool || data.value == undefined">{{data.value| sc_filter_obj}}</span>'
- + '</span>'
- + '</td>'
- + '<td>{{desc}}</td>'
- + '<td>Readonly</td>',
- link: function(scope, elem, attrs){
- scope.key = system_string_trim(attrs.scpData, "global.");
- }
- };
- }]);
- // sc-directive: scDirective
- /**
- * Usage:
- <tr sc-directive scd-data="obj"
- scd-desc="侦听的端口" scd-default="1935" scd-span="span3"
- scd-array="true" scd-bool="true" scd-select="1935,1936,1937"
- scd-submit="submit(obj)">
- </tr>
- * where obj is:
- {
- key: "listen",
- value: ["1935", "1936"],
- error: false
- }
- */
- scApp.directive("scDirective", ["$sc_utility", function($sc_utility){
- return {
- restrict: 'A',
- scope: {
- data: '=scdData',
- desc: '@scdDesc',
- submit: '&scdSubmit',
- span: '@scdSpan',
- default: '@scdDefault',
- array: '@scdArray',
- bool: '@scdBool',
- select: '@scdSelect'
- },
- controller: ['$scope', function($scope) {
- // whether current directive is editing.
- $scope.editing = false;
- // previous old value, for cancel and array value.
- $scope.old_data = {
- init: false,
- value: undefined,
- reset: function(){
- this.init = false;
- this.value = undefined;
- }
- };
- // split select to array.
- if (typeof $scope.select === "string" && $scope.select && !$scope.selects) {
- $scope.selects = $scope.select.split(",");
- }
- $scope.edit = function() {
- $scope.editing = true;
- };
- $scope.commit = function() {
- // for array, string to array.
- if ($scope.array === "true" && typeof $scope.data.value === "string") {
- $scope.data.value = $scope.data.value.split(",");
- }
- if ($scope.old_data.init && !$scope.submit()) {
- return;
- }
- $scope.editing = false;
- $scope.old_data.reset();
- };
- $scope.load_default = function(){
- if ($scope.default !== undefined) {
- if ($scope.bool === "true") {
- $scope.data.value = $scope.default === "true";
- } else if ($scope.array === "true") {
- $scope.data.value = $scope.default.split(",");
- } else {
- $scope.data.value = $scope.default;
- }
- }
- };
- $scope.cancel = function() {
- if ($scope.old_data.init) {
- $scope.data.value = $scope.old_data.value;
- }
- // for array, always restore it when cancel.
- if ($scope.array === "true") {
- $scope.data.value = $scope.old_data.value;
- }
- $scope.editing = false;
- $scope.old_data.reset();
- };
- $scope.$watch("editing", function(nv, ov){
- // init, ignore.
- if (!nv && !nv) {
- return;
- }
- // when server not set this option, the whole data is undefined.
- if (!$scope.data) {
- $scope.data = {
- key: $scope.key,
- value: undefined,
- error: false
- };
- }
- // save the old value.
- if (!$scope.old_data.init) {
- $scope.old_data.value = $scope.data.value;
- $scope.old_data.init = true;
- }
- // start editing.
- if (nv && !ov) {
- // for array, array to string.
- if ($scope.array === "true") {
- $scope.data.value = $scope.data.value.join(",");
- }
- }
- });
- }],
- template: scDirectiveTemplate,
- link: function(scope, elem, attrs){
- scope.key = system_string_trim(attrs.scdData, "global.");
- }
- };
- }]);
- // config the http interceptor.
- scApp.config(['$httpProvider', function($httpProvider){
- $httpProvider.interceptors.push('MHttpInterceptor');
- }]);
- // the http interceptor.
- scApp.factory('MHttpInterceptor', ["$q", "$sc_utility", function($q, $sc_utility){
- // register the interceptor as a service
- // @see: https://code.angularjs.org/1.2.0-rc.3/docs/api/ng.$http
- // @remark: the function($q) should never add other params.
- return {
- 'request': function(config) {
- return config || $q.when(config);
- },
- 'requestError': function(rejection) {
- return $q.reject(rejection);
- },
- 'response': function(response) {
- if (response.data.code && response.data.code !== 0) {
- $sc_utility.http_error(response.status, response.data);
- // the $q.reject, will cause the error function of controller.
- // @see: https://code.angularjs.org/1.2.0-rc.3/docs/api/ng.$q
- return $q.reject(response);
- }
- return response || $q.when(response);
- },
- 'responseError': function(rejection) {
- $sc_utility.http_error(rejection.status, rejection.data);
- return $q.reject(rejection);
- }
- };
- }]);
|