srs.page.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. $("#srs_index").attr("href", "index.html" + window.location.search);
  14. $("#nav_srs_player").attr("href", "srs_player.html" + window.location.search);
  15. $("#nav_rtc_player").attr("href", "rtc_player.html" + window.location.search);
  16. $("#nav_rtc_publisher").attr("href", "rtc_publisher.html" + window.location.search);
  17. $("#nav_srs_publisher").attr("href", "srs_publisher.html" + window.location.search);
  18. $("#nav_srs_chat").attr("href", "srs_chat.html" + window.location.search);
  19. $("#nav_srs_bwt").attr("href", "srs_bwt.html" + window.location.search);
  20. $("#nav_vlc").attr("href", "vlc.html" + window.location.search);
  21. }
  22. // Special extra params, such as auth_key.
  23. function user_extra_params(query, params, rtc) {
  24. var queries = params || [];
  25. for (var key in query.user_query) {
  26. if (key === 'app' || key === 'autostart' || key === 'dir'
  27. || key === 'filename' || key === 'host' || key === 'hostname'
  28. || key === 'http_port' || key === 'pathname' || key === 'port'
  29. || key === 'server' || key === 'stream' || key === 'buffer'
  30. || key === 'schema' || key === 'vhost' || key === 'api'
  31. ) {
  32. continue;
  33. }
  34. if (query[key]) {
  35. queries.push(key + '=' + query[key]);
  36. }
  37. }
  38. return queries;
  39. }
  40. function is_default_port(schema, port) {
  41. return (schema === 'http' && port === 80)
  42. || (schema === 'https' && port === 443)
  43. || (schema === 'webrtc' && port === 1985)
  44. || (schema === 'rtmp' && port === 1935);
  45. }
  46. /**
  47. @param server the ip of server. default to window.location.hostname
  48. @param vhost the vhost of HTTP-FLV. default to window.location.hostname
  49. @param port the port of HTTP-FLV. default to 1935
  50. @param app the app of HTTP-FLV. default to live.
  51. @param stream the stream of HTTP-FLV. default to livestream.flv
  52. */
  53. function build_default_flv_url() {
  54. var query = parse_query_string();
  55. var schema = (!query.schema)? "http":query.schema;
  56. var server = (!query.server)? window.location.hostname:query.server;
  57. var port = (!query.port)? (schema==="http"? 8080:1935) : Number(query.port);
  58. var vhost = (!query.vhost)? window.location.hostname:query.vhost;
  59. var app = (!query.app)? "live":query.app;
  60. var stream = (!query.stream)? "livestream.flv":query.stream;
  61. var queries = [];
  62. if (server !== vhost && vhost !== "__defaultVhost__") {
  63. queries.push("vhost=" + vhost);
  64. }
  65. queries = user_extra_params(query, queries);
  66. var uri = schema + "://" + server;
  67. if (!is_default_port(schema, port)) {
  68. uri += ":" + port;
  69. }
  70. uri += "/" + app + "/" + stream + "?" + queries.join('&');
  71. while (uri.indexOf("?") === uri.length - 1) {
  72. uri = uri.substr(0, uri.length - 1);
  73. }
  74. return uri;
  75. }
  76. function build_default_rtc_url(query) {
  77. // The format for query string to overwrite configs of server.
  78. console.log('?eip=x.x.x.x to overwrite candidate. 覆盖服务器candidate(外网IP)配置');
  79. console.log('?api=x to overwrite WebRTC API(1985).');
  80. console.log('?schema=http|https to overwrite WebRTC API protocol.');
  81. var server = (!query.server)? window.location.hostname:query.server;
  82. var vhost = (!query.vhost)? window.location.hostname:query.vhost;
  83. var app = (!query.app)? "live":query.app;
  84. var stream = (!query.stream)? "livestream":query.stream;
  85. var api = query.api? ':'+query.api : '';
  86. var queries = [];
  87. if (server !== vhost && vhost !== "__defaultVhost__") {
  88. queries.push("vhost=" + vhost);
  89. }
  90. if (query.schema && window.location.protocol !== query.schema + ':') {
  91. queries.push('schema=' + query.schema);
  92. }
  93. queries = user_extra_params(query, queries, true);
  94. var uri = "webrtc://" + server + api + "/" + app + "/" + stream + "?" + queries.join('&');
  95. while (uri.lastIndexOf("?") === uri.length - 1) {
  96. uri = uri.substr(0, uri.length - 1);
  97. }
  98. return uri;
  99. };
  100. /**
  101. * initialize the page.
  102. * @param flv_url the div id contains the flv stream url to play
  103. * @param hls_url the div id contains the hls stream url to play
  104. * @param modal_player the div id contains the modal player
  105. */
  106. function srs_init_flv(flv_url, modal_player) {
  107. update_nav();
  108. if (flv_url) {
  109. $(flv_url).val(build_default_flv_url());
  110. }
  111. if (modal_player) {
  112. $(modal_player).width(srs_get_player_modal() + "px");
  113. $(modal_player).css("margin-left", "-" + srs_get_player_modal() / 2 +"px");
  114. }
  115. }
  116. function srs_init_rtc(id, query) {
  117. update_nav();
  118. $(id).val(build_default_rtc_url(query));
  119. }