2
0

srs.page.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. //////////////////////////////////////////////////////////////////////////////////
  2. //////////////////////////////////////////////////////////////////////////////////
  3. //////////////////////////////////////////////////////////////////////////////////
  4. // to query the swf anti cache.
  5. function srs_get_version_code() { return "1.25"; }
  6. /**
  7. * player specified size.
  8. */
  9. function srs_get_player_modal() { return 740; }
  10. function srs_get_player_width() { return srs_get_player_modal() - 30; }
  11. function srs_get_player_height() { return srs_get_player_width() * 9 / 19; }
  12. // get the default vhost for players.
  13. function srs_get_player_vhost() { return "players"; }
  14. // the api server port, for chat room.
  15. function srs_get_api_server_port() { return 8085; }
  16. // the srs http server port
  17. function srs_get_srs_http_server_port() { return 8080; }
  18. // get the stream published to vhost,
  19. // generally we need to transcode the stream to support HLS and filters.
  20. // for example, src_vhost is "players", we transcode stream to vhost "players_pub".
  21. // if not equals to the player vhost, return the orignal vhost.
  22. function srs_get_player_publish_vhost(src_vhost) { return (src_vhost != srs_get_player_vhost())? src_vhost:(src_vhost + "_pub"); }
  23. // for chat, use rtmp only vhost, low latecy, without gop cache.
  24. function srs_get_player_chat_vhost(src_vhost) { return (src_vhost != srs_get_player_vhost())? src_vhost:(src_vhost + "_chat"); }
  25. //////////////////////////////////////////////////////////////////////////////////
  26. //////////////////////////////////////////////////////////////////////////////////
  27. //////////////////////////////////////////////////////////////////////////////////
  28. /**
  29. * update the navigator, add same query string.
  30. */
  31. function update_nav() {
  32. $("#srs_index").attr("href", "index.html" + window.location.search);
  33. $("#nav_srs_player").attr("href", "srs_player.html" + window.location.search);
  34. $("#nav_srs_publisher").attr("href", "srs_publisher.html" + window.location.search);
  35. $("#nav_srs_chat").attr("href", "srs_chat.html" + window.location.search);
  36. $("#nav_srs_bwt").attr("href", "srs_bwt.html" + window.location.search);
  37. $("#nav_jwplayer6").attr("href", "jwplayer6.html" + window.location.search);
  38. $("#nav_osmf").attr("href", "osmf.html" + window.location.search);
  39. $("#nav_vlc").attr("href", "vlc.html" + window.location.search);
  40. }
  41. /**
  42. @param server the ip of server. default to window.location.hostname
  43. @param vhost the vhost of rtmp. default to window.location.hostname
  44. @param port the port of rtmp. default to 1935
  45. @param app the app of rtmp. default to live.
  46. @param stream the stream of rtmp. default to livestream.
  47. */
  48. function build_default_rtmp_url() {
  49. var query = parse_query_string();
  50. var server = (query.server == undefined)? window.location.hostname:query.server;
  51. var port = (query.port == undefined)? 1935:query.port;
  52. var vhost = (query.vhost == undefined)? window.location.hostname:query.vhost;
  53. var app = (query.app == undefined)? "live":query.app;
  54. var stream = (query.stream == undefined)? "demo":query.stream;
  55. if (server == vhost || vhost == "") {
  56. return "rtmp://" + server + ":" + port + "/" + app + "/" + stream;
  57. } else {
  58. return "rtmp://" + server + ":" + port + "/" + app + "...vhost..." + vhost + "/" + stream;
  59. }
  60. }
  61. // for the chat to init the publish url.
  62. function build_default_publish_rtmp_url() {
  63. var query = parse_query_string();
  64. var server = (query.server == undefined)? window.location.hostname:query.server;
  65. var port = (query.port == undefined)? 1935:query.port;
  66. var vhost = (query.vhost == undefined)? window.location.hostname:query.vhost;
  67. var app = (query.app == undefined)? "live":query.app;
  68. var stream = (query.stream == undefined)? "demo":query.stream;
  69. if (server == vhost || vhost == "") {
  70. return "rtmp://" + server + ":" + port + "/" + app + "/" + stream;
  71. } else {
  72. vhost = srs_get_player_chat_vhost(vhost);
  73. return "rtmp://" + server + ":" + port + "/" + app + "...vhost..." + vhost + "/" + stream;
  74. }
  75. }
  76. // for the bandwidth tool to init page
  77. function build_default_bandwidth_rtmp_url() {
  78. var query = parse_query_string();
  79. var server = (query.server == undefined)? window.location.hostname:query.server;
  80. var port = (query.port == undefined)? 1935:query.port;
  81. var vhost = "bandcheck.srs.com";
  82. var app = (query.app == undefined)? "app":query.app;
  83. var key = (query.key == undefined)? "35c9b402c12a7246868752e2878f7e0e":query.key;
  84. return "rtmp://" + server + ":" + port + "/" + app + "?key=" + key + "&vhost=" + vhost;
  85. }
  86. /**
  87. @param server the ip of server. default to window.location.hostname
  88. @param vhost the vhost of hls. default to window.location.hostname
  89. @param hls_vhost the vhost of hls. override the server if specified.
  90. @param hls_port the port of hls. default to window.location.port
  91. @param app the app of hls. default to live.
  92. @param stream the stream of hls. default to livestream.
  93. */
  94. function build_default_hls_url() {
  95. var query = parse_query_string();
  96. // for http, use hls_vhost to override server if specified.
  97. var server = window.location.hostname;
  98. if (query.server != undefined) {
  99. server = query.server;
  100. } else if (query.hls_vhost != undefined) {
  101. server = query.hls_vhost;
  102. }
  103. var port = (query.hls_port == undefined)? window.location.port:query.hls_port;
  104. var app = (query.app == undefined)? "live":query.app;
  105. var stream = (query.stream == undefined)? "demo":query.stream;
  106. if (port == "" || port == null || port == undefined) {
  107. port = 8080;
  108. }
  109. if (stream.indexOf(".flv") >= 0) {
  110. return "http://" + server + ":" + port + "/" + app + "/" + stream;
  111. }
  112. return "http://" + server + ":" + port + "/" + app + "/" + stream + ".m3u8";
  113. }
  114. /**
  115. * initialize the page.
  116. * @param rtmp_url the div id contains the rtmp stream url to play
  117. * @param hls_url the div id contains the hls stream url to play
  118. * @param modal_player the div id contains the modal player
  119. */
  120. function srs_init_rtmp(rtmp_url, modal_player) {
  121. srs_init(rtmp_url, null, modal_player);
  122. }
  123. function srs_init_hls(hls_url, modal_player) {
  124. srs_init(null, hls_url, modal_player);
  125. }
  126. function srs_init(rtmp_url, hls_url, modal_player) {
  127. update_nav();
  128. if (rtmp_url) {
  129. $(rtmp_url).val(build_default_rtmp_url());
  130. }
  131. if (hls_url) {
  132. $(hls_url).val(build_default_hls_url());
  133. }
  134. if (modal_player) {
  135. $(modal_player).width(srs_get_player_modal() + "px");
  136. $(modal_player).css("margin-left", "-" + srs_get_player_modal() / 2 +"px");
  137. }
  138. }
  139. // for the chat to init the publish url.
  140. function srs_init_publish(rtmp_url) {
  141. update_nav();
  142. if (rtmp_url) {
  143. $(rtmp_url).val(build_default_publish_rtmp_url());
  144. }
  145. }
  146. // for bw to init url
  147. // url: scheme://host:port/path?query#fragment
  148. function srs_init_bwt(rtmp_url, hls_url) {
  149. update_nav();
  150. if (rtmp_url) {
  151. $(rtmp_url).val(build_default_bandwidth_rtmp_url());
  152. }
  153. }
  154. // check whether can republish
  155. function srs_can_republish() {
  156. var browser = get_browser_agents();
  157. if (browser.Chrome || browser.Firefox) {
  158. return true;
  159. }
  160. if (browser.MSIE || browser.QQBrowser) {
  161. return false;
  162. }
  163. return false;
  164. }
  165. // without default values set.
  166. function srs_initialize_codec_page(
  167. cameras, microphones,
  168. sl_cameras, sl_microphones, sl_vcodec, sl_profile, sl_level, sl_gop, sl_size, sl_fps, sl_bitrate,
  169. sl_acodec
  170. ) {
  171. $(sl_cameras).empty();
  172. for (var i = 0; i < cameras.length; i++) {
  173. $(sl_cameras).append("<option value='" + i + "'>" + cameras[i] + "</option");
  174. }
  175. // optional: select the except matches
  176. matchs = ["virtual"];
  177. for (var i = 0; i < cameras.length; i++) {
  178. for (var j = 0; j < matchs.length; j++) {
  179. if (cameras[i].toLowerCase().indexOf(matchs[j]) == -1) {
  180. $(sl_cameras + " option[value='" + i + "']").attr("selected", true);
  181. break;
  182. }
  183. }
  184. if (j < matchs.length) {
  185. break;
  186. }
  187. }
  188. // optional: select the first matched.
  189. matchs = ["truevision", "integrated"];
  190. for (var i = 0; i < cameras.length; i++) {
  191. for (var j = 0; j < matchs.length; j++) {
  192. if (cameras[i].toLowerCase().indexOf(matchs[j]) >= 0) {
  193. $(sl_cameras + " option[value='" + i + "']").attr("selected", true);
  194. break;
  195. }
  196. }
  197. if (j < matchs.length) {
  198. break;
  199. }
  200. }
  201. $(sl_microphones).empty();
  202. for (var i = 0; i < microphones.length; i++) {
  203. $(sl_microphones).append("<option value='" + i + "'>" + microphones[i] + "</option");
  204. }
  205. // optional: select the except matches
  206. matchs = ["default"];
  207. for (var i = 0; i < microphones.length; i++) {
  208. for (var j = 0; j < matchs.length; j++) {
  209. if (microphones[i].toLowerCase().indexOf(matchs[j]) == -1) {
  210. $(sl_microphones + " option[value='" + i + "']").attr("selected", true);
  211. break;
  212. }
  213. }
  214. if (j < matchs.length) {
  215. break;
  216. }
  217. }
  218. // optional: select the first matched.
  219. matchs = ["realtek", "内置式麦克风"];
  220. for (var i = 0; i < microphones.length; i++) {
  221. for (var j = 0; j < matchs.length; j++) {
  222. if (microphones[i].toLowerCase().indexOf(matchs[j]) >= 0) {
  223. $(sl_microphones + " option[value='" + i + "']").attr("selected", true);
  224. break;
  225. }
  226. }
  227. if (j < matchs.length) {
  228. break;
  229. }
  230. }
  231. $(sl_vcodec).empty();
  232. var vcodecs = ["h264", "vp6"];
  233. vcodecs = ["h264"]; // h264 only.
  234. for (var i = 0; i < vcodecs.length; i++) {
  235. $(sl_vcodec).append("<option value='" + vcodecs[i] + "'>" + vcodecs[i] + "</option");
  236. }
  237. $(sl_profile).empty();
  238. var profiles = ["baseline", "main"];
  239. for (var i = 0; i < profiles.length; i++) {
  240. $(sl_profile).append("<option value='" + profiles[i] + "'>" + profiles[i] + "</option");
  241. }
  242. $(sl_level).empty();
  243. var levels = ["1", "1b", "1.1", "1.2", "1.3",
  244. "2", "2.1", "2.2", "3", "3.1", "3.2", "4", "4.1", "4.2", "5", "5.1"];
  245. for (var i = 0; i < levels.length; i++) {
  246. $(sl_level).append("<option value='" + levels[i] + "'>" + levels[i] + "</option");
  247. }
  248. $(sl_gop).empty();
  249. var gops = ["0.3", "0.5", "1", "2", "3", "4",
  250. "5", "6", "7", "8", "9", "10", "15", "20"];
  251. for (var i = 0; i < gops.length; i++) {
  252. $(sl_gop).append("<option value='" + gops[i] + "'>" + gops[i] + "秒</option");
  253. }
  254. $(sl_size).empty();
  255. var sizes = ["176x144", "320x240", "352x240",
  256. "352x288", "480x360", "640x480", "720x480", "720x576", "800x600",
  257. "1024x768", "1280x720", "1360x768", "1920x1080"];
  258. for (i = 0; i < sizes.length; i++) {
  259. $(sl_size).append("<option value='" + sizes[i] + "'>" + sizes[i] + "</option");
  260. }
  261. $(sl_fps).empty();
  262. var fpses = ["5", "10", "15", "20", "24", "25", "29.97", "30"];
  263. for (i = 0; i < fpses.length; i++) {
  264. $(sl_fps).append("<option value='" + fpses[i] + "'>" + Number(fpses[i]).toFixed(2) + " 帧/秒</option");
  265. }
  266. $(sl_bitrate).empty();
  267. var bitrates = ["50", "200", "350", "500", "650", "800",
  268. "950", "1000", "1200", "1500", "1800", "2000", "3000", "5000"];
  269. for (i = 0; i < bitrates.length; i++) {
  270. $(sl_bitrate).append("<option value='" + bitrates[i] + "'>" + bitrates[i] + " kbps</option");
  271. }
  272. $(sl_acodec).empty();
  273. var bitrates = ["speex", "nellymoser", "pcma", "pcmu"];
  274. for (i = 0; i < bitrates.length; i++) {
  275. $(sl_acodec).append("<option value='" + bitrates[i] + "'>" + bitrates[i] + "</option");
  276. }
  277. }
  278. /**
  279. * when publisher ready, init the page elements.
  280. */
  281. function srs_publisher_initialize_page(
  282. cameras, microphones,
  283. sl_cameras, sl_microphones, sl_vcodec, sl_profile, sl_level, sl_gop, sl_size, sl_fps, sl_bitrate,
  284. sl_acodec
  285. ) {
  286. srs_initialize_codec_page(
  287. cameras, microphones,
  288. sl_cameras, sl_microphones, sl_vcodec, sl_profile, sl_level, sl_gop, sl_size, sl_fps, sl_bitrate,
  289. sl_acodec
  290. );
  291. //var profiles = ["baseline", "main"];
  292. $(sl_profile + " option[value='main']").attr("selected", true);
  293. //var levels = ["1", "1b", "1.1", "1.2", "1.3",
  294. // "2", "2.1", "2.2", "3", "3.1", "3.2", "4", "4.1", "4.2", "5", "5.1"];
  295. $(sl_level + " option[value='4.1']").attr("selected", true);
  296. //var gops = ["0.3", "0.5", "1", "2", "3", "4",
  297. // "5", "6", "7", "8", "9", "10", "15", "20"];
  298. $(sl_gop + " option[value='10']").attr("selected", true);
  299. //var sizes = ["176x144", "320x240", "352x240",
  300. // "352x288", "480x360", "640x480", "720x480", "720x576", "800x600",
  301. // "1024x768", "1280x720", "1360x768", "1920x1080"];
  302. $(sl_size + " option[value='640x480']").attr("selected", true);
  303. //var fpses = ["5", "10", "15", "20", "24", "25", "29.97", "30"];
  304. $(sl_fps + " option[value='20']").attr("selected", true);
  305. //var bitrates = ["50", "200", "350", "500", "650", "800",
  306. // "950", "1000", "1200", "1500", "1800", "2000", "3000", "5000"];
  307. $(sl_bitrate + " option[value='500']").attr("selected", true);
  308. // speex
  309. $(sl_acodec + " option[value='speex']").attr("selected", true);
  310. }
  311. /**
  312. * for chat, use low latecy settings.
  313. */
  314. function srs_chat_initialize_page(
  315. cameras, microphones,
  316. sl_cameras, sl_microphones, sl_vcodec, sl_profile, sl_level, sl_gop, sl_size, sl_fps, sl_bitrate,
  317. sl_acodec
  318. ) {
  319. srs_initialize_codec_page(
  320. cameras, microphones,
  321. sl_cameras, sl_microphones, sl_vcodec, sl_profile, sl_level, sl_gop, sl_size, sl_fps, sl_bitrate,
  322. sl_acodec
  323. );
  324. //var profiles = ["baseline", "main"];
  325. $(sl_profile + " option[value='baseline']").attr("selected", true);
  326. //var levels = ["1", "1b", "1.1", "1.2", "1.3",
  327. // "2", "2.1", "2.2", "3", "3.1", "3.2", "4", "4.1", "4.2", "5", "5.1"];
  328. $(sl_level + " option[value='3.1']").attr("selected", true);
  329. //var gops = ["0.3", "0.5", "1", "2", "3", "4",
  330. // "5", "6", "7", "8", "9", "10", "15", "20"];
  331. $(sl_gop + " option[value='2']").attr("selected", true);
  332. //var sizes = ["176x144", "320x240", "352x240",
  333. // "352x288", "480x360", "640x480", "720x480", "720x576", "800x600",
  334. // "1024x768", "1280x720", "1360x768", "1920x1080"];
  335. $(sl_size + " option[value='480x360']").attr("selected", true);
  336. //var fpses = ["5", "10", "15", "20", "24", "25", "29.97", "30"];
  337. $(sl_fps + " option[value='15']").attr("selected", true);
  338. //var bitrates = ["50", "200", "350", "500", "650", "800",
  339. // "950", "1000", "1200", "1500", "1800", "2000", "3000", "5000"];
  340. $(sl_bitrate + " option[value='350']").attr("selected", true);
  341. // speex
  342. $(sl_acodec + " option[value='speex']").attr("selected", true);
  343. }
  344. /**
  345. * get the vcodec and acodec.
  346. */
  347. function srs_publiser_get_codec(
  348. vcodec, acodec,
  349. sl_cameras, sl_microphones, sl_vcodec, sl_profile, sl_level, sl_gop, sl_size, sl_fps, sl_bitrate,
  350. sl_acodec
  351. ) {
  352. acodec.codec = $(sl_acodec).val();
  353. acodec.device_code = $(sl_microphones).val();
  354. acodec.device_name = $(sl_microphones).text();
  355. vcodec.device_code = $(sl_cameras).find("option:selected").val();
  356. vcodec.device_name = $(sl_cameras).find("option:selected").text();
  357. vcodec.codec = $(sl_vcodec).find("option:selected").val();
  358. vcodec.profile = $(sl_profile).find("option:selected").val();
  359. vcodec.level = $(sl_level).find("option:selected").val();
  360. vcodec.fps = $(sl_fps).find("option:selected").val();
  361. vcodec.gop = $(sl_gop).find("option:selected").val();
  362. vcodec.size = $(sl_size).find("option:selected").val();
  363. vcodec.bitrate = $(sl_bitrate).find("option:selected").val();
  364. }