2
0

srs.console.js 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. scApp.controller("CSCMain", ["$scope", "$interval", "$location", "MSCApi", "$sc_utility", "$sc_server", '$sc_nav', function($scope, $interval, $location, MSCApi, $sc_utility, $sc_server, $sc_nav){
  2. $scope.logs = [];
  3. // remove expired alert.
  4. $interval(function(){
  5. for (var i = 0; i < $scope.logs.length; i++) {
  6. var log = $scope.logs[i];
  7. if (log.create + 10000 < new Date().getTime()) {
  8. $scope.logs.splice(i, 1);
  9. break;
  10. }
  11. }
  12. }, 3000);
  13. // handler system log event, from $sc_utility service.
  14. $scope.$on("$sc_utility_log", function(event, level, msg){
  15. var log = {
  16. level:level, msg:msg, create:new Date().getTime()
  17. };
  18. // only show 3 msgs.
  19. while ($scope.logs.length > 2) {
  20. $scope.logs.splice(0, 1);
  21. }
  22. $scope.logs.push(log);
  23. });
  24. // handle system error event, from $sc_utility service.
  25. $scope.$on("$sc_utility_http_error", function(event, status, response){
  26. var code = response.code;
  27. if (status !== 200) {
  28. if (!status && !response) {
  29. response = "无法访问服务器";
  30. } else {
  31. response = "HTTP/" + status + ", " + response;
  32. }
  33. } else {
  34. var map = {
  35. 1061: "RAW API被禁用",
  36. 1062: "服务器不允许这个操作",
  37. 1063: "RawApi参数不符合要求"
  38. };
  39. if (map[response.code]) {
  40. response = "code=" + response.code + ", " + map[response.code];
  41. } else {
  42. resonse = "code=" + response.code + ", 系统错误";
  43. }
  44. }
  45. if (code === 1061) {
  46. $sc_utility.log("trace", response);
  47. return;
  48. }
  49. $sc_utility.log("warn", response);
  50. });
  51. $scope.gogogo = function (path) {
  52. $location.path(path);
  53. };
  54. $scope.goto = function (path) {
  55. var absUrl = $sc_server.buildNavUrl();
  56. var url = absUrl.replace($location.$$path, path);
  57. var win = window.open('', '_self');
  58. win.location = url; // For safari security reason, we should set the location instead.
  59. };
  60. $scope.redirect = function (from, to) {
  61. var absUrl = $sc_server.buildNavUrl();
  62. var url = absUrl.replace(from, to);
  63. var win = window.open('', '_self');
  64. win.location = url; // For safari security reason, we should set the location instead.
  65. };
  66. // init the server and port for api.
  67. $sc_server.init($location, MSCApi);
  68. //$sc_utility.log("trace", "set baseurl to " + $sc_server.baseurl());
  69. }]);
  70. scApp.controller("CSCConnect", ["$scope", "$location", "MSCApi", "$sc_utility", "$sc_nav", "$sc_server", function($scope, $location, MSCApi, $sc_utility, $sc_nav, $sc_server){
  71. $sc_nav.in_control();
  72. $scope.server = {
  73. schema: $sc_server.schema,
  74. ip: $sc_server.host,
  75. port: $sc_server.port
  76. };
  77. $scope.connect = function(){
  78. $sc_server.schema = $scope.server.schema;
  79. $sc_server.host = $scope.server.ip;
  80. $sc_server.port = $scope.server.port;
  81. MSCApi.versions_get(function(data){
  82. $sc_utility.log("trace", "连接到SRS" + $scope.server.ip + "成功, SRS/" + data.data.version);
  83. $scope.goto('/summaries');
  84. });
  85. };
  86. $sc_utility.refresh.stop();
  87. }]);
  88. scApp.controller("CSCSummary", ["$scope", "MSCApi", "$sc_utility", "$sc_nav", function($scope, MSCApi, $sc_utility, $sc_nav){
  89. $sc_nav.in_summary();
  90. $scope.pre_kbps = null;
  91. $sc_utility.refresh.refresh_change(function(){
  92. MSCApi.summaries_get(function(data){
  93. var kbps = {
  94. initialized: false,
  95. in: { srs: 0, sys: 0, inner: 0 },
  96. out: { srs: 0, sys: 0, inner: 0 }
  97. };
  98. if ($scope.pre_kbps) {
  99. kbps.initialized = true;
  100. var diff = data.data.system.net_sample_time - $scope.pre_kbps.system.net_sample_time;
  101. if (diff > 0) {
  102. kbps.in.sys = (data.data.system.net_recv_bytes - $scope.pre_kbps.system.net_recv_bytes) * 8 / diff;
  103. kbps.in.inner = (data.data.system.net_recvi_bytes - $scope.pre_kbps.system.net_recvi_bytes) * 8 / diff;
  104. kbps.out.sys = (data.data.system.net_send_bytes - $scope.pre_kbps.system.net_send_bytes) * 8 / diff;
  105. kbps.out.inner = (data.data.system.net_sendi_bytes - $scope.pre_kbps.system.net_sendi_bytes) * 8 / diff;
  106. }
  107. diff = data.data.system.srs_sample_time - $scope.pre_kbps.system.srs_sample_time;
  108. if (diff > 0) {
  109. kbps.in.srs = (data.data.system.srs_recv_bytes - $scope.pre_kbps.system.srs_recv_bytes) * 8 / diff;
  110. kbps.out.srs = (data.data.system.srs_send_bytes - $scope.pre_kbps.system.srs_send_bytes) * 8 / diff;
  111. }
  112. diff = data.data.now_ms - $scope.pre_kbps.now_ms;
  113. if (!$scope.kbps.initialized || diff >= 20 * 1000) {
  114. $scope.pre_kbps = data.data;
  115. $scope.kbps = kbps;
  116. }
  117. }
  118. if (!$scope.kbps) {
  119. $scope.pre_kbps = data.data;
  120. $scope.kbps = kbps;
  121. }
  122. $scope.global = data.data;
  123. $scope.server = data.data.self;
  124. $scope.system = data.data.system;
  125. $sc_utility.refresh.request();
  126. });
  127. }, 3000);
  128. $sc_utility.log("trace", "Retrieve summary from SRS.");
  129. $sc_utility.refresh.request(0);
  130. }]);
  131. scApp.controller("CSCVhosts", ["$scope", "MSCApi", "$sc_nav", "$sc_utility", function($scope, MSCApi, $sc_nav, $sc_utility){
  132. $sc_nav.in_vhosts();
  133. $sc_utility.refresh.refresh_change(function(){
  134. MSCApi.vhosts_get(function(data){
  135. $scope.vhosts = data.vhosts;
  136. $sc_utility.refresh.request();
  137. });
  138. }, 3000);
  139. $sc_utility.log("trace", "Retrieve vhosts from SRS");
  140. $sc_utility.refresh.request(0);
  141. }]);
  142. scApp.controller("CSCVhost", ["$scope", "$routeParams", "MSCApi", "$sc_nav", "$sc_utility", function($scope, $routeParams, MSCApi, $sc_nav, $sc_utility){
  143. $sc_nav.in_vhosts();
  144. $sc_utility.refresh.stop();
  145. MSCApi.vhosts_get2($routeParams.id, function(data){
  146. $scope.vhost = data.vhost;
  147. });
  148. $sc_utility.log("trace", "Retrieve vhost info from SRS");
  149. }]);
  150. scApp.controller("CSCStreams", ["$scope", "$location", "MSCApi", "$sc_nav", "$sc_utility", function($scope, $location, MSCApi, $sc_nav, $sc_utility){
  151. $sc_nav.in_streams();
  152. $scope.kickoff = function(stream) {
  153. MSCApi.clients_delete(stream.publish.cid, function(){
  154. $sc_utility.log("warn", "Kickoff stream ok.");
  155. });
  156. };
  157. $scope.dvr = function(stream){
  158. $location.path($sc_utility.dvr_id(stream));
  159. };
  160. $scope.support_raw_api = false;
  161. MSCApi.configs_raw(function(data) {
  162. $scope.support_raw_api = $sc_utility.raw_api_enabled(data);
  163. });
  164. MSCApi.vhosts_get(function(data){
  165. var vhosts = data.vhosts;
  166. $sc_utility.refresh.refresh_change(function(){
  167. MSCApi.streams_get(function(data){
  168. for (var k in data.streams) {
  169. var stream = data.streams[k];
  170. stream.owner = system_array_get(vhosts, function(vhost) {return vhost.id === stream.vhost; });
  171. }
  172. $scope.streams = data.streams;
  173. $sc_utility.refresh.request();
  174. });
  175. }, 3000);
  176. $sc_utility.log("trace", "Retrieve streams from SRS");
  177. $sc_utility.refresh.request(0);
  178. });
  179. $sc_utility.log("trace", "Retrieve vhost info from SRS");
  180. }]);
  181. scApp.controller("CSCStream", ["$scope", '$location', "$routeParams", "MSCApi", "$sc_nav", "$sc_utility", function($scope, $location, $routeParams, MSCApi, $sc_nav, $sc_utility){
  182. $sc_nav.in_streams();
  183. $scope.kickoff = function(stream) {
  184. MSCApi.clients_delete(stream.publish.cid, function(){
  185. $sc_utility.log("warn", "Kickoff stream ok.");
  186. });
  187. };
  188. $sc_utility.refresh.stop();
  189. $scope.dvr = function(stream){
  190. $location.path($sc_utility.dvr_id(stream));
  191. };
  192. $scope.support_raw_api = false;
  193. MSCApi.configs_raw(function(data) {
  194. $scope.support_raw_api = $sc_utility.raw_api_enabled(data);
  195. });
  196. MSCApi.streams_get2($routeParams.id, function(data){
  197. var stream = data.stream;
  198. if (!$scope.owner) {
  199. MSCApi.vhosts_get2(stream.vhost, function(data) {
  200. var vhost = data.vhost;
  201. stream.owner = $scope.owner = vhost;
  202. $scope.stream = stream;
  203. });
  204. $sc_utility.log("trace", "Retrieve vhost info from SRS");
  205. } else {
  206. stream.owner = $scope.owner;
  207. $scope.stream = stream;
  208. }
  209. });
  210. $sc_utility.log("trace", "Retrieve stream info from SRS");
  211. }]);
  212. scApp.controller("CSCClients", ["$scope", "MSCApi", "$sc_nav", "$sc_utility", function($scope, MSCApi, $sc_nav, $sc_utility){
  213. $sc_nav.in_clients();
  214. $scope.kickoff = function(client) {
  215. MSCApi.clients_delete(client.id, function(){
  216. $sc_utility.log("warn", "Kickoff client ok.");
  217. });
  218. };
  219. $sc_utility.refresh.refresh_change(function(){
  220. MSCApi.clients_get(function(data){
  221. $scope.clients = data.clients;
  222. $sc_utility.refresh.request();
  223. });
  224. }, 3000);
  225. $sc_utility.log("trace", "Retrieve clients from SRS");
  226. $sc_utility.refresh.request(0);
  227. }]);
  228. scApp.controller("CSCClient", ["$scope", "$routeParams", "MSCApi", "$sc_nav", "$sc_utility", function($scope, $routeParams, MSCApi, $sc_nav, $sc_utility){
  229. $sc_nav.in_clients();
  230. $scope.kickoff = function(client) {
  231. MSCApi.clients_delete(client.id, function(){
  232. $sc_utility.log("warn", "Kickoff client ok.");
  233. });
  234. };
  235. $sc_utility.refresh.stop();
  236. MSCApi.clients_get2($routeParams.id, function(data){
  237. $scope.client = data.client;
  238. });
  239. $sc_utility.log("trace", "Retrieve client info from SRS");
  240. }]);
  241. scApp.controller("CSCConfigs", ["$scope", "$location", "MSCApi", "$sc_nav", "$sc_utility", "$sc_server", function($scope, $location, MSCApi, $sc_nav, $sc_utility, $sc_server){
  242. $sc_nav.in_configs();
  243. $sc_utility.refresh.stop();
  244. MSCApi.configs_raw(function(data){
  245. $scope.http_api = data.http_api;
  246. });
  247. $sc_utility.log("trace", "Retrieve config info from SRS");
  248. }]);
  249. scApp.factory("MSCApi", ["$http", "$sc_server", function($http, $sc_server){
  250. return {
  251. versions_get: function(success) {
  252. var url = $sc_server.jsonp("/api/v1/versions");
  253. $http.jsonp(url).success(success);
  254. },
  255. summaries_get: function(success) {
  256. var url = $sc_server.jsonp("/api/v1/summaries");
  257. $http.jsonp(url).success(success);
  258. },
  259. vhosts_get: function(success) {
  260. var url = $sc_server.jsonp("/api/v1/vhosts/");
  261. $http.jsonp(url).success(success);
  262. },
  263. vhosts_get2: function(id, success) {
  264. var url = $sc_server.jsonp("/api/v1/vhosts/" + id);
  265. $http.jsonp(url).success(success);
  266. },
  267. streams_get: function(success) {
  268. var url = $sc_server.jsonp("/api/v1/streams/");
  269. $http.jsonp(url).success(success);
  270. },
  271. streams_get2: function(id, success) {
  272. var url = $sc_server.jsonp("/api/v1/streams/" + id);
  273. $http.jsonp(url).success(success);
  274. },
  275. clients_get: function(success) {
  276. var url = $sc_server.jsonp("/api/v1/clients/");
  277. $http.jsonp(url).success(success);
  278. },
  279. clients_get2: function(id, success) {
  280. var url = $sc_server.jsonp("/api/v1/clients/" + id);
  281. $http.jsonp(url).success(success);
  282. },
  283. clients_delete: function(id, success) {
  284. var url = $sc_server.jsonp_delete("/api/v1/clients/" + id);
  285. $http.jsonp(url).success(success);
  286. },
  287. configs_raw: function(success, error) {
  288. var url = $sc_server.jsonp_query("/api/v1/raw", "rpc=raw");
  289. var obj = $http.jsonp(url).success(success);
  290. if (error) {
  291. obj.error(error);
  292. }
  293. },
  294. configs_get: function(success) {
  295. var url = $sc_server.jsonp_query("/api/v1/raw", "rpc=query&scope=global");
  296. $http.jsonp(url).success(success);
  297. },
  298. };
  299. }]);
  300. scApp.filter("sc_filter_log_level", function(){
  301. return function(v) {
  302. return (v === "warn" || v === "error")? "alert-warn":"alert-success";
  303. };
  304. });
  305. scApp.filter("sc_filter_nav_active", ["$sc_nav", function($sc_nav){
  306. return function(v){
  307. return $sc_nav.is_selected(v)? "active":"";
  308. };
  309. }]);
  310. scApp.filter("sc_unsafe", ['$sce', function($sce){
  311. return function(v) {
  312. return $sce.trustAsHtml(v);
  313. };
  314. }]);
  315. scApp.filter("sc_filter_filesize_k", function(){
  316. return function(v){
  317. // PB
  318. if (v > 1024 * 1024 * 1024 * 1024) {
  319. return Number(v / 1024.0 / 1024 / 1024 / 1024).toFixed(2) + "PB";
  320. }
  321. // TB
  322. if (v > 1024 * 1024 * 1024) {
  323. return Number(v / 1024.0 / 1024 / 1024).toFixed(2) + "TB";
  324. }
  325. // GB
  326. if (v > 1024 * 1024) {
  327. return Number(v / 1024.0 / 1024).toFixed(2) + "GB";
  328. }
  329. // MB
  330. if (v > 1024) {
  331. return Number(v / 1024.0).toFixed(2) + "MB";
  332. }
  333. return Number(v).toFixed(2) + "KB";
  334. };
  335. });
  336. scApp.filter("sc_filter_filesize_k2", function(){
  337. return function(v){
  338. // PB
  339. if (v > 1024 * 1024 * 1024 * 1024) {
  340. return Number(v / 1024.0 / 1024 / 1024 / 1024).toFixed(0) + "PB";
  341. }
  342. // TB
  343. if (v > 1024 * 1024 * 1024) {
  344. return Number(v / 1024.0 / 1024 / 1024).toFixed(0) + "TB";
  345. }
  346. // GB
  347. if (v > 1024 * 1024) {
  348. return Number(v / 1024.0 / 1024).toFixed(0) + "GB";
  349. }
  350. // MB
  351. if (v > 1024) {
  352. return Number(v / 1024.0).toFixed(0) + "MB";
  353. }
  354. return Number(v).toFixed(0) + "KB";
  355. };
  356. });
  357. scApp.filter("sc_filter_filerate_k", function(){
  358. return function(v){
  359. // PB
  360. if (v > 1024 * 1024 * 1024 * 1024) {
  361. return Number(v / 1024.0 / 1024 / 1024 / 1024).toFixed(2) + "PBps";
  362. }
  363. // TB
  364. if (v > 1024 * 1024 * 1024) {
  365. return Number(v / 1024.0 / 1024 / 1024).toFixed(2) + "TBps";
  366. }
  367. // GB
  368. if (v > 1024 * 1024) {
  369. return Number(v / 1024.0 / 1024).toFixed(2) + "GBps";
  370. }
  371. // MB
  372. if (v > 1024) {
  373. return Number(v / 1024.0).toFixed(2) + "MBps";
  374. }
  375. return Number(v).toFixed(2) + "KBps";
  376. };
  377. });
  378. scApp.filter("sc_filter_filerate_k2", function(){
  379. return function(v){
  380. // PB
  381. if (v > 1024 * 1024 * 1024 * 1024) {
  382. return Number(v / 1024.0 / 1024 / 1024 / 1024).toFixed(0) + "PBps";
  383. }
  384. // TB
  385. if (v > 1024 * 1024 * 1024) {
  386. return Number(v / 1024.0 / 1024 / 1024).toFixed(0) + "TBps";
  387. }
  388. // GB
  389. if (v > 1024 * 1024) {
  390. return Number(v / 1024.0 / 1024).toFixed(0) + "GBps";
  391. }
  392. // MB
  393. if (v > 1024) {
  394. return Number(v / 1024.0).toFixed(0) + "MBps";
  395. }
  396. return Number(v).toFixed(0) + "KBps";
  397. };
  398. });
  399. scApp.filter("sc_filter_bitrate_k", function(){
  400. return function(v){
  401. // PB
  402. if (v > 1000 * 1000 * 1000 * 1000) {
  403. return Number(v / 1000.0 / 1000 / 1000 / 1000).toFixed(2) + "Pbps";
  404. }
  405. // TB
  406. if (v > 1000 * 1000 * 1000) {
  407. return Number(v / 1000.0 / 1000 / 1000).toFixed(2) + "Tbps";
  408. }
  409. // GB
  410. if (v > 1000 * 1000) {
  411. return Number(v / 1000.0 / 1000).toFixed(2) + "Gbps";
  412. }
  413. // MB
  414. if (v > 1000) {
  415. return Number(v / 1000.0).toFixed(2) + "Mbps";
  416. }
  417. return Number(v).toFixed(2) + "Kbps";
  418. };
  419. });
  420. scApp.filter("sc_filter_bitrate_k2", function(){
  421. return function(v){
  422. // PB
  423. if (v > 1000 * 1000 * 1000 * 1000) {
  424. return Number(v / 1000.0 / 1000 / 1000 / 1000).toFixed(0) + "Pbps";
  425. }
  426. // TB
  427. if (v > 1000 * 1000 * 1000) {
  428. return Number(v / 1000.0 / 1000 / 1000).toFixed(0) + "Tbps";
  429. }
  430. // GB
  431. if (v > 1000 * 1000) {
  432. return Number(v / 1000.0 / 1000).toFixed(0) + "Gbps";
  433. }
  434. // MB
  435. if (v > 1000) {
  436. return Number(v / 1000.0).toFixed(0) + "Mbps";
  437. }
  438. return Number(v).toFixed(0) + "Kbps";
  439. };
  440. });
  441. scApp.filter("sc_filter_percent", function(){
  442. return function(v){
  443. return Number(v).toFixed(2) + "%";
  444. };
  445. });
  446. scApp.filter("sc_filter_percent2", function(){
  447. return function(v){
  448. return Number(v).toFixed(0) + "%";
  449. };
  450. });
  451. scApp.filter("sc_filter_percentf", function(){
  452. return function(v){
  453. return Number(v * 100).toFixed(2) + "%";
  454. };
  455. });
  456. scApp.filter("sc_filter_percentf2", function(){
  457. return function(v){
  458. return Number(v * 100).toFixed(0) + "%";
  459. };
  460. });
  461. scApp.filter("sc_filter_video", function(){
  462. return function(v){
  463. // set default value for SRS2.
  464. v.width = v.width? v.width : 0;
  465. v.height = v.height? v.height : 0;
  466. return v? v.codec + "/" + v.profile + "/" + v.level + "/" + v.width + "x" + v.height : "无视频";
  467. };
  468. });
  469. scApp.filter("sc_filter_audio", function(){
  470. return function(v){
  471. return v? v.codec + "/" + v.sample_rate + "/" + (v.channel === 2? "Stereo":"Mono") + "/" + v.profile : "无音频";
  472. };
  473. });
  474. scApp.filter("sc_filter_number", function(){
  475. return function(v){
  476. return Number(v).toFixed(2);
  477. };
  478. });
  479. scApp.filter("sc_filter_less", function(){
  480. return function(v) {
  481. return v? (v.length > 15? v.slice(0, 15) + "...":v):v;
  482. };
  483. });
  484. scApp.filter('sc_filter_style_error', function(){
  485. return function(v){
  486. return v? 'alert-danger':'';
  487. };
  488. });
  489. scApp.filter('sc_filter_preview_url', ['$sc_server', function($sc_server){
  490. return function(v){
  491. var page = $sc_server.schema + `://${$sc_server.host}:${$sc_server.http}/players/srs_player.html`;
  492. var http = $sc_server.http[$sc_server.http.length - 1];
  493. var query = "vhost=" + v.owner.name + "&app=" + v.app + "&stream=" + v.name + ".flv";
  494. query += "&server=" + $sc_server.host +"&port=" + http + "&autostart=true&schema=" + $sc_server.schema;
  495. return v? page+"?" + query:"javascript:void(0)";
  496. };
  497. }]);
  498. scApp.filter('sc_filter_streamURL', function(){
  499. return function(v){
  500. if (!v || !v.url) return '';
  501. const pos = v.url.lastIndexOf('/');
  502. const stream = pos < 0 ? '' : v.url.substr(pos);
  503. // Use name or extract from url.
  504. let streamName = v.name ? v.name : stream;
  505. if (streamName && streamName.indexOf('/') !== 0) streamName = `/${streamName}`;
  506. const pos2 = v.tcUrl.indexOf('?');
  507. const tcUrl = pos2 < 0 ? v.tcUrl : v.tcUrl.substr(0, pos2);
  508. let params = pos2 < 0 ? '' : v.tcUrl.substr(pos2);
  509. if (params === '?vhost=__defaultVhost__' || params === '?domain=__defaultVhost__') params = '';
  510. return `${tcUrl}${streamName}${params}`;
  511. };
  512. });
  513. // the sc nav is the nevigator
  514. scApp.provider("$sc_nav", function(){
  515. this.$get = function(){
  516. return {
  517. selected: null,
  518. in_control: function(){
  519. this.selected = "/console";
  520. },
  521. in_summary: function(){
  522. this.selected = "/summaries";
  523. },
  524. in_vhosts: function(){
  525. this.selected = "/vhosts";
  526. },
  527. in_streams: function(){
  528. this.selected = "/streams";
  529. },
  530. in_clients: function(){
  531. this.selected = "/clients";
  532. },
  533. in_configs: function(){
  534. this.selected = "/configs";
  535. },
  536. go_summary: function($location){
  537. $location.path("/summaries");
  538. },
  539. is_selected: function(v){
  540. return v === this.selected;
  541. }
  542. };
  543. };
  544. });
  545. // the sc server is the server we connected to.
  546. scApp.provider("$sc_server", [function(){
  547. this.$get = function(){
  548. var self = {
  549. schema: "http",
  550. host: null,
  551. port: 1985,
  552. rtmp: [1935],
  553. http: [8080],
  554. baseurl: function(){
  555. return self.schema + "://" + self.host + (self.port === 80? "": ":" + self.port);
  556. },
  557. jsonp: function(url){
  558. return self.baseurl() + url + "?callback=JSON_CALLBACK";
  559. },
  560. jsonp_delete: function(url) {
  561. return self.jsonp(url) + "&method=DELETE";
  562. },
  563. jsonp_query: function(url, query){
  564. return self.baseurl() + url + "?callback=JSON_CALLBACK&" + query;
  565. },
  566. buildNavUrl: function () {
  567. var $location = self.$location;
  568. var url = $location.absUrl();
  569. if (url.indexOf('?') > 0) {
  570. url = url.slice(0, url.indexOf('?'));
  571. }
  572. url += '?';
  573. var query = {};
  574. for (var key in $location.search()) {
  575. query[key] = $location.search()[key];
  576. }
  577. query['schema'] = self.schema;
  578. query['host'] = self.host;
  579. query['port'] = self.port;
  580. var queries = [];
  581. for (var key in query) {
  582. var value = query[key];
  583. if (!Array.isArray(value)) {
  584. value = [value];
  585. }
  586. for (var i in value) {
  587. queries.push(key + '=' + value[i]);
  588. }
  589. }
  590. url += queries.join('&');
  591. return url;
  592. },
  593. init: function($location, MSCApi) {
  594. self.$location = $location;
  595. // Set the right schema for proxy.
  596. if ($location.search().schema) {
  597. self.schema = $location.search().schema;
  598. } else {
  599. self.schema = $location.protocol();
  600. }
  601. // query string then url.
  602. if ($location.search().host) {
  603. self.host = $location.search().host;
  604. } else {
  605. self.host = $location.host();
  606. }
  607. if ($location.search().port) {
  608. self.port = $location.search().port;
  609. } else {
  610. self.port = $location.port();
  611. }
  612. if ($location.search().http) {
  613. self.http = [$location.search().http];
  614. }
  615. }
  616. };
  617. return self;
  618. };
  619. }]);
  620. // the sc utility is a set of helper utilities.
  621. scApp.provider("$sc_utility", function(){
  622. this.$get = ["$rootScope", function($rootScope){
  623. return {
  624. log: function(level, msg) {
  625. $rootScope.$broadcast("$sc_utility_log", level, msg);
  626. },
  627. http_error: function(status, response) {
  628. $rootScope.$broadcast("$sc_utility_http_error", status, response);
  629. },
  630. find_siblings: function(elem, className) {
  631. if (elem.hasClass(className)) {
  632. return elem;
  633. }
  634. if (!elem[0].nextSibling) {
  635. return null;
  636. }
  637. var sibling = angular.element(elem[0].nextSibling);
  638. return this.find_siblings(sibling, className);
  639. },
  640. array_actual_equals: function(a, b) {
  641. // all elements of a in b.
  642. for (var i = 0; i < a.length; i++) {
  643. if (!system_array_contains(b, a[i])) {
  644. return false;
  645. }
  646. }
  647. // all elements of b in a.
  648. for (i = 0; i < b.length; i++) {
  649. if (!system_array_contains(a, b[i])) {
  650. return false;
  651. }
  652. }
  653. return true;
  654. },
  655. object2arr: function(obj, each) {
  656. var arr = [];
  657. for (var k in obj) {
  658. var v = obj[k];
  659. if (each) {
  660. each(v);
  661. }
  662. arr.push(v);
  663. }
  664. return arr;
  665. },
  666. /**
  667. * transform the api data to angularjs perfer, for instance:
  668. data.global.listen = ["1935, "1936"];
  669. data.global.http_api.listen = "1985";
  670. * parsed to:
  671. global.listen = {
  672. key: 'listen',
  673. value: ["1935", "1936"],
  674. error: false
  675. }
  676. global.http_api.listen = {
  677. key: 'http_api.listen',
  678. value: "1985",
  679. error: false
  680. }
  681. * where the error is used for commit error.
  682. */
  683. object2complex: function(complex, obj, prefix) {
  684. for (var k in obj) {
  685. var v = obj[k];
  686. //console.log("k=" + key + ", v=" + typeof v + ", " + v);
  687. var key = prefix? prefix + "." + k : k;
  688. if (key === "vhosts") {
  689. // use name as vhost id.
  690. complex[k] = this.object2arr(v, function(e) { e.vid = e.name; });
  691. continue;
  692. }
  693. if (typeof v === "object" && v.constructor !== Array) {
  694. var cv = {};
  695. complex[k] = cv;
  696. this.object2complex(cv, v, key);
  697. continue;
  698. }
  699. // convert number to str for select to
  700. // choose the right default one.
  701. if (key === "pithy_print_ms") {
  702. v = String(v);
  703. }
  704. complex[k] = {
  705. key: system_string_trim(key, 'global.'),
  706. value: v,
  707. error: false
  708. };
  709. }
  710. return complex;
  711. },
  712. copy_object: function(dst, src) {
  713. for (var k in src) {
  714. dst[k] = src[k];
  715. }
  716. },
  717. dvr_id: function(stream) {
  718. var url = '/dvr/' + stream.owner.name
  719. + '/' + stream.id
  720. + '/' + escape(stream.app.replace('/', '___'))
  721. + '/' + escape(stream.name.replace('/', '___'));
  722. return url;
  723. },
  724. raw_api_enabled: function(data) {
  725. return data.http_api && data.http_api.enabled && data.http_api.raw_api && data.http_api.raw_api.enabled;
  726. },
  727. refresh: async_refresh2
  728. };
  729. }];
  730. });
  731. // sc-collapse: scCollapse
  732. /**
  733. * Usage:
  734. <div class="accordion">
  735. <div class="accordion-group">
  736. <div class="accordion-heading" sc-collapse="in">
  737. <a class="accordion-toggle" href="javascript:void(0)">
  738. HTTP RAW API
  739. </a>
  740. </div>
  741. <div class="accordion-body collapse">
  742. <div class="accordion-inner">
  743. 该服务器不支持HTTP RAW API,或者配置中禁用了该功能。
  744. </div>
  745. </div>
  746. </div>
  747. </div>
  748. */
  749. scApp.directive('scCollapse', ["$sc_utility", function($sc_utility){
  750. return {
  751. restrict: 'A',
  752. scope: true,
  753. controller: ['$scope', function($scope) {
  754. }],
  755. compile: function(elem, attrs) {
  756. return function(scope, elem, attrs){
  757. if (attrs.scCollapse === "in") {
  758. var obj = $sc_utility.find_siblings(elem, 'accordion-body');
  759. obj.addClass('in');
  760. }
  761. elem.on('click', function(){
  762. var obj = $sc_utility.find_siblings(elem, 'accordion-body');
  763. obj.toggleClass('in');
  764. });
  765. };
  766. }
  767. };
  768. }]);
  769. // sc-pretty: scPretty
  770. /**
  771. * Usage:
  772. <tr sc-pretty scp-key="http_api.enabled" scp-value="http_api.enabled" scp-bool="true"
  773. scp-desc="是否开启HTTP API,开启后就可以访问SRS提供的API管理服务器。默认: {{false| sc_filter_enabled}}">
  774. </tr>
  775. */
  776. scApp.directive("scPretty", [function(){
  777. return {
  778. restrict: 'A',
  779. scope: {
  780. key: '@scpKey',
  781. value: '=scpValue',
  782. desc: '@scpDesc',
  783. bool: '@scpBool'
  784. },
  785. template: ''
  786. + '<td>{{key}}</td>'
  787. + '<td>'
  788. + '<span class="{{value == undefined? \'label\':\'\'}}">'
  789. + '<span ng-show="bool && value != undefined">{{value| sc_filter_enabled}}</span>'
  790. + '<span ng-show="!bool || value === undefined">{{value| sc_filter_obj}}</span>'
  791. + '</span>'
  792. + '</td>'
  793. + '<td>{{desc}}</td>'
  794. + '<td>只读</td>'
  795. };
  796. }]);
  797. // sc-pretty2: scPretty2
  798. /**
  799. * Usage:
  800. <tr sc-pretty2 scp-data="global.daemon" scp-bool="true"
  801. scp-desc="是否以后台启动SRS。默认: {{true| sc_filter_yesno}}">
  802. </tr>
  803. */
  804. scApp.directive("scPretty2", [function(){
  805. return {
  806. restrict: 'A',
  807. scope: {
  808. data: '=scpData',
  809. desc: '@scpDesc',
  810. bool: '@scpBool',
  811. link: '@scpLink'
  812. },
  813. controller: ['$scope', function($scope){
  814. }],
  815. template: ''
  816. + '<td>{{key}}</td>'
  817. + '<td>'
  818. + '<span ng-if="link">'
  819. + '<a href="{{link}}">{{data.value}}</a>'
  820. + '</span>'
  821. + '<span class="{{data.value == undefined? \'label\':\'\'}}" ng-if="!link">'
  822. + '<span ng-show="bool && data.value != undefined">{{data.value| sc_filter_enabled}}</span>'
  823. + '<span ng-show="!bool || data.value == undefined">{{data.value| sc_filter_obj}}</span>'
  824. + '</span>'
  825. + '</td>'
  826. + '<td>{{desc}}</td>'
  827. + '<td>Readonly</td>',
  828. link: function(scope, elem, attrs){
  829. scope.key = system_string_trim(attrs.scpData, "global.");
  830. }
  831. };
  832. }]);
  833. // sc-directive: scDirective
  834. /**
  835. * Usage:
  836. <tr sc-directive scd-data="obj"
  837. scd-desc="侦听的端口" scd-default="1935" scd-span="span3"
  838. scd-array="true" scd-bool="true" scd-select="1935,1936,1937"
  839. scd-submit="submit(obj)">
  840. </tr>
  841. * where obj is:
  842. {
  843. key: "listen",
  844. value: ["1935", "1936"],
  845. error: false
  846. }
  847. */
  848. scApp.directive("scDirective", ["$sc_utility", function($sc_utility){
  849. return {
  850. restrict: 'A',
  851. scope: {
  852. data: '=scdData',
  853. desc: '@scdDesc',
  854. submit: '&scdSubmit',
  855. span: '@scdSpan',
  856. default: '@scdDefault',
  857. array: '@scdArray',
  858. bool: '@scdBool',
  859. select: '@scdSelect'
  860. },
  861. controller: ['$scope', function($scope) {
  862. // whether current directive is editing.
  863. $scope.editing = false;
  864. // previous old value, for cancel and array value.
  865. $scope.old_data = {
  866. init: false,
  867. value: undefined,
  868. reset: function(){
  869. this.init = false;
  870. this.value = undefined;
  871. }
  872. };
  873. // split select to array.
  874. if (typeof $scope.select === "string" && $scope.select && !$scope.selects) {
  875. $scope.selects = $scope.select.split(",");
  876. }
  877. $scope.edit = function() {
  878. $scope.editing = true;
  879. };
  880. $scope.commit = function() {
  881. // for array, string to array.
  882. if ($scope.array === "true" && typeof $scope.data.value === "string") {
  883. $scope.data.value = $scope.data.value.split(",");
  884. }
  885. if ($scope.old_data.init && !$scope.submit()) {
  886. return;
  887. }
  888. $scope.editing = false;
  889. $scope.old_data.reset();
  890. };
  891. $scope.load_default = function(){
  892. if ($scope.default !== undefined) {
  893. if ($scope.bool === "true") {
  894. $scope.data.value = $scope.default === "true";
  895. } else if ($scope.array === "true") {
  896. $scope.data.value = $scope.default.split(",");
  897. } else {
  898. $scope.data.value = $scope.default;
  899. }
  900. }
  901. };
  902. $scope.cancel = function() {
  903. if ($scope.old_data.init) {
  904. $scope.data.value = $scope.old_data.value;
  905. }
  906. // for array, always restore it when cancel.
  907. if ($scope.array === "true") {
  908. $scope.data.value = $scope.old_data.value;
  909. }
  910. $scope.editing = false;
  911. $scope.old_data.reset();
  912. };
  913. $scope.$watch("editing", function(nv, ov){
  914. // init, ignore.
  915. if (!nv && !nv) {
  916. return;
  917. }
  918. // when server not set this option, the whole data is undefined.
  919. if (!$scope.data) {
  920. $scope.data = {
  921. key: $scope.key,
  922. value: undefined,
  923. error: false
  924. };
  925. }
  926. // save the old value.
  927. if (!$scope.old_data.init) {
  928. $scope.old_data.value = $scope.data.value;
  929. $scope.old_data.init = true;
  930. }
  931. // start editing.
  932. if (nv && !ov) {
  933. // for array, array to string.
  934. if ($scope.array === "true") {
  935. $scope.data.value = $scope.data.value.join(",");
  936. }
  937. }
  938. });
  939. }],
  940. template: scDirectiveTemplate,
  941. link: function(scope, elem, attrs){
  942. scope.key = system_string_trim(attrs.scdData, "global.");
  943. }
  944. };
  945. }]);
  946. // config the http interceptor.
  947. scApp.config(['$httpProvider', function($httpProvider){
  948. $httpProvider.interceptors.push('MHttpInterceptor');
  949. }]);
  950. // the http interceptor.
  951. scApp.factory('MHttpInterceptor', ["$q", "$sc_utility", function($q, $sc_utility){
  952. // register the interceptor as a service
  953. // @see: https://code.angularjs.org/1.2.0-rc.3/docs/api/ng.$http
  954. // @remark: the function($q) should never add other params.
  955. return {
  956. 'request': function(config) {
  957. return config || $q.when(config);
  958. },
  959. 'requestError': function(rejection) {
  960. return $q.reject(rejection);
  961. },
  962. 'response': function(response) {
  963. if (response.data.code && response.data.code !== 0) {
  964. $sc_utility.http_error(response.status, response.data);
  965. // the $q.reject, will cause the error function of controller.
  966. // @see: https://code.angularjs.org/1.2.0-rc.3/docs/api/ng.$q
  967. return $q.reject(response);
  968. }
  969. return response || $q.when(response);
  970. },
  971. 'responseError': function(rejection) {
  972. $sc_utility.http_error(rejection.status, rejection.data);
  973. return $q.reject(rejection);
  974. }
  975. };
  976. }]);