srs.page.js 15 KB

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