srs.page.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // to query the swf anti cache.
  2. function srs_get_version_code() { return "1.33"; }
  3. /**
  4. * player specified size.
  5. */
  6. function srs_get_player_modal() { return 740; }
  7. function srs_get_player_width() { return srs_get_player_modal() - 30; }
  8. function srs_get_player_height() { return srs_get_player_width() * 9 / 19; }
  9. /**
  10. * update the navigator, add same query string.
  11. */
  12. function update_nav() {
  13. $("#nav_srs_player").attr("href", "srs_player.html" + window.location.search);
  14. $("#nav_rtc_player").attr("href", "rtc_player.html" + window.location.search);
  15. $("#nav_rtc_publisher").attr("href", "rtc_publisher.html" + window.location.search);
  16. $("#nav_whip").attr("href", "whip.html" + window.location.search);
  17. $("#nav_whep").attr("href", "whep.html" + window.location.search);
  18. $("#nav_srs_publisher").attr("href", "srs_publisher.html" + window.location.search);
  19. $("#nav_srs_chat").attr("href", "srs_chat.html" + window.location.search);
  20. $("#nav_srs_bwt").attr("href", "srs_bwt.html" + window.location.search);
  21. $("#nav_vlc").attr("href", "vlc.html" + window.location.search);
  22. }
  23. // Special extra params, such as auth_key.
  24. function user_extra_params(query, params, rtc) {
  25. var queries = params || [];
  26. for (var key in query.user_query) {
  27. if (key === 'app' || key === 'autostart' || key === 'dir'
  28. || key === 'filename' || key === 'host' || key === 'hostname'
  29. || key === 'http_port' || key === 'pathname' || key === 'port'
  30. || key === 'server' || key === 'stream' || key === 'buffer'
  31. || key === 'schema' || key === 'vhost' || key === 'api'
  32. || key === 'path'
  33. ) {
  34. continue;
  35. }
  36. if (query[key]) {
  37. queries.push(key + '=' + query[key]);
  38. }
  39. }
  40. return queries;
  41. }
  42. function is_default_port(schema, port) {
  43. return (schema === 'http' && port === 80)
  44. || (schema === 'https' && port === 443)
  45. || (schema === 'webrtc' && port === 1985)
  46. || (schema === 'rtmp' && port === 1935);
  47. }
  48. /**
  49. @param server the ip of server. default to window.location.hostname
  50. @param vhost the vhost of HTTP-FLV. default to window.location.hostname
  51. @param port the port of HTTP-FLV. default to 1935
  52. @param app the app of HTTP-FLV. default to live.
  53. @param stream the stream of HTTP-FLV. default to livestream.flv
  54. */
  55. function build_default_flv_url() {
  56. var query = parse_query_string();
  57. var schema = (!query.schema)? "http":query.schema;
  58. var server = (!query.server)? window.location.hostname:query.server;
  59. var port = (!query.port)? (schema==="http"? 8080:1935) : Number(query.port);
  60. var vhost = (!query.vhost)? window.location.hostname:query.vhost;
  61. var app = (!query.app)? "live":query.app;
  62. var stream = (!query.stream)? "livestream.flv":query.stream;
  63. var queries = [];
  64. if (server !== vhost && vhost !== "__defaultVhost__") {
  65. queries.push("vhost=" + vhost);
  66. }
  67. queries = user_extra_params(query, queries);
  68. var uri = schema + "://" + server;
  69. if (!is_default_port(schema, port)) {
  70. uri += ":" + port;
  71. }
  72. uri += "/" + app + "/" + stream + "?" + queries.join('&');
  73. while (uri.indexOf("?") === uri.length - 1) {
  74. uri = uri.slice(0, uri.length - 1);
  75. }
  76. return uri;
  77. }
  78. function build_default_rtc_url(query) {
  79. // The format for query string to overwrite configs of server.
  80. console.log('?eip=x.x.x.x to overwrite candidate. 覆盖服务器candidate(外网IP)配置');
  81. console.log('?api=x to overwrite WebRTC API(1985).');
  82. console.log('?schema=http|https to overwrite WebRTC API protocol.');
  83. var server = (!query.server)? window.location.hostname:query.server;
  84. var vhost = (!query.vhost)? window.location.hostname:query.vhost;
  85. var app = (!query.app)? "live":query.app;
  86. var stream = (!query.stream)? "livestream":query.stream;
  87. var api = query.api? ':'+query.api : '';
  88. var queries = [];
  89. if (server !== vhost && vhost !== "__defaultVhost__") {
  90. queries.push("vhost=" + vhost);
  91. }
  92. if (query.schema && window.location.protocol !== query.schema + ':') {
  93. queries.push('schema=' + query.schema);
  94. }
  95. queries = user_extra_params(query, queries, true);
  96. var uri = "webrtc://" + server + api + "/" + app + "/" + stream + "?" + queries.join('&');
  97. while (uri.lastIndexOf("?") === uri.length - 1) {
  98. uri = uri.slice(0, uri.length - 1);
  99. }
  100. return uri;
  101. };
  102. function build_default_whip_whep_url(query, apiPath) {
  103. // The format for query string to overwrite configs of server.
  104. console.log('?eip=x.x.x.x to overwrite candidate. 覆盖服务器candidate(外网IP)配置');
  105. console.log('?api=x to overwrite WebRTC API(1985).');
  106. console.log('?schema=http|https to overwrite WebRTC API protocol.');
  107. console.log(`?path=xxx to overwrite default ${apiPath}`);
  108. var server = (!query.server)? window.location.hostname:query.server;
  109. var vhost = (!query.vhost)? window.location.hostname:query.vhost;
  110. var app = (!query.app)? "live":query.app;
  111. var stream = (!query.stream)? "livestream":query.stream;
  112. var api = ':' + (query.api || (window.location.protocol === 'http:' ? '1985' : '1990'));
  113. const realApiPath = query.path || apiPath;
  114. var queries = [];
  115. if (server !== vhost && vhost !== "__defaultVhost__") {
  116. queries.push("vhost=" + vhost);
  117. }
  118. if (query.schema && window.location.protocol !== query.schema + ':') {
  119. queries.push('schema=' + query.schema);
  120. }
  121. queries = user_extra_params(query, queries, true);
  122. var uri = window.location.protocol + "//" + server + api + realApiPath + "?app=" + app + "&stream=" + stream + "&" + queries.join('&');
  123. while (uri.lastIndexOf("?") === uri.length - 1) {
  124. uri = uri.slice(0, uri.length - 1);
  125. }
  126. while (uri.lastIndexOf("&") === uri.length - 1) {
  127. uri = uri.slice(0, uri.length - 1);
  128. }
  129. return uri;
  130. }
  131. /**
  132. * initialize the page.
  133. * @param flv_url the div id contains the flv stream url to play
  134. * @param hls_url the div id contains the hls stream url to play
  135. * @param modal_player the div id contains the modal player
  136. */
  137. function srs_init_flv(flv_url, modal_player) {
  138. update_nav();
  139. if (flv_url) {
  140. $(flv_url).val(build_default_flv_url());
  141. }
  142. if (modal_player) {
  143. $(modal_player).width(srs_get_player_modal() + "px");
  144. $(modal_player).css("margin-left", "-" + srs_get_player_modal() / 2 +"px");
  145. }
  146. }
  147. function srs_init_rtc(id, query) {
  148. update_nav();
  149. $(id).val(build_default_rtc_url(query));
  150. }
  151. function srs_init_whip(id, query) {
  152. update_nav();
  153. $(id).val(build_default_whip_whep_url(query, '/rtc/v1/whip/'));
  154. }
  155. function srs_init_whep(id, query) {
  156. update_nav();
  157. $(id).val(build_default_whip_whep_url(query, '/rtc/v1/whep/'));
  158. }