srs_player.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>SRS</title>
  5. <meta charset="utf-8">
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
  7. <style>
  8. body{
  9. padding-top: 30px;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <img src='//ossrs.net/gif/v1/sls.gif?site=ossrs.net&path=/player/srsplayer'/>
  15. <div class="navbar navbar-fixed-top">
  16. <div class="navbar-inner">
  17. <div class="container">
  18. <a class="brand" href="https://github.com/ossrs/srs" target="_blank">SRS</a>
  19. <div class="nav-collapse collapse">
  20. <ul class="nav">
  21. <li class="active"><a id="nav_srs_player" href="srs_player.html">LivePlayer</a></li>
  22. <!--<li><a id="nav_rtc_player" href="rtc_player.html">RTC播放器</a></li>-->
  23. <!--<li><a id="nav_rtc_publisher" href="rtc_publisher.html">RTC推流</a></li>-->
  24. <li><a id="nav_whip" href="whip.html">WHIP</a></li>
  25. <li><a id="nav_whep" href="whip.html">WHEP</a></li>
  26. <li><a href="http://ossrs.net/srs.release/releases/app.html">iOS/Andriod</a></li>
  27. <!--<li><a id="nav_srs_publisher" href="srs_publisher.html">SRS编码器</a></li>-->
  28. <!--<li><a id="nav_srs_chat" href="srs_chat.html">SRS会议</a></li>-->
  29. <!--<li><a id="nav_srs_bwt" href="srs_bwt.html">SRS测网速</a></li>-->
  30. <!--li><a id="nav_vlc" href="vlc.html">VLC播放器</a></li>-->
  31. <!--<li><a id="nav_gb28181" href="srs_gb28181.html">GB28181</a></li>-->
  32. <!--<li>
  33. <a href="https://github.com/ossrs/srs">
  34. <img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/ossrs/srs?style=social">
  35. </a>
  36. </li>-->
  37. </ul>
  38. </div>
  39. </div>
  40. </div>
  41. </div>
  42. <div class="container">
  43. <div id="main_content">
  44. <div id="main_info" class="alert alert-info fade in">
  45. <button type="button" class="close" data-dismiss="alert">×</button>
  46. <strong><span>Usage:</span></strong> <span>Enter the HTTP-FLV/HLS URL and click "Play" to start playing the video.</span>
  47. </div>
  48. <div class="form-inline">
  49. <div>
  50. URL:
  51. <input type="text" id="txt_url" class="input-xxlarge" value="">
  52. <button class="btn btn-primary" id="btn_play">Play</button>
  53. </div>
  54. <p></p>
  55. <div>
  56. <video id="video_player" width="100%" autoplay controls></video>
  57. </div>
  58. <p>
  59. Share: <a href="#" id="link_url" target="_blank">Please right-click and copy this link.</a>
  60. </p>
  61. <div id="main_tips">
  62. <p></p>
  63. <p>
  64. Recommended other players:
  65. <ul>
  66. <li><a href="https://github.com/xqq/mpegts.js">flv.js</a>, HTML5/MSE for playing HTTP-FLV</li>
  67. <li><a href="https://github.com/xqq/mpegts.js">mpegts.js</a>, HTML5/MSE for playing HTTP-TS</li>
  68. <li><a href="https://github.com/video-dev/hls.js/">hls.js</a>, HTML5/MSE for playing HLS</li>
  69. <li><a href="https://github.com/Dash-Industry-Forum/dash.js">dash.js</a>, HTML5/MSE for playing MPEG-DASH</li>
  70. </ul>
  71. </p>
  72. </div>
  73. </div>
  74. </div>
  75. <div name="detect_flash">
  76. <div id="main_flash_alert" class="alert alert-danger fade in hide">
  77. <button type="button" class="close" data-dismiss="alert">×</button>
  78. <p>
  79. <a href="https://www.adobe.com/products/flashplayer/end-of-life.html" target="_blank">Flash is dead</a>,
  80. Unable to play RTMP streams, you can use ffplay or VLC player to play.
  81. </p>
  82. <ul>
  83. <li>If you want low-latency live streaming (3-5 seconds), you can use HTTP-FLV with the <a href="https://github.com/xqq/mpegts.js">flv.js</a> player for H5/MSE playback of HTTP-FLV.</li>
  84. <li>If you want low-latency live streaming (3-5 seconds), you can use HTTP-TS with the <a href="https://github.com/xqq/mpegts.js">mpegts.js</a> player for H5/MSE playback of HTTP-TS.</li>
  85. <li>If you are not sensitive to latency (5-10 seconds) and want better cross-platform compatibility, you can use HLS with the <a href="https://github.com/video-dev/hls.js/">hls.js</a> player for H5/MSE playback of HLS.</li>
  86. <li>If you want ultra-low latency (within 1 second) and only need to support mainstream browsers, you can use WebRTC with the <a href="rtc_player.html">RTC player</a>.</li>
  87. </ul>
  88. <p>
  89. 更多信息,参考<a href="https://mp.weixin.qq.com/s/oYn5q4fF9afaged23Ueudg" target="_blank">没有Flash如何做直播?</a>
  90. </p>
  91. </div>
  92. </div>
  93. <footer>
  94. <p></p>
  95. <p><a href="https://github.com/ossrs/srs">SRS Team &copy; 2013</a></p>
  96. </footer>
  97. </div>
  98. </body>
  99. <script type="text/javascript" src="js/jquery-1.12.2.min.js"></script>
  100. <script type="text/javascript" src="js/bootstrap.min.js"></script>
  101. <script type="text/javascript" src="js/mpegts-1.7.2.min.js"></script>
  102. <script type="text/javascript" src="js/hls-0.14.17.min.js"></script>
  103. <script type="text/javascript" src="js/dash-v4.5.1.all.min.js"></script>
  104. <script type="text/javascript" src="js/json2.js"></script>
  105. <script type="text/javascript" src="js/srs.page.js"></script>
  106. <script type="text/javascript" src="js/srs.log.js"></script>
  107. <script type="text/javascript" src="js/srs.utility.js"></script>
  108. <script type="text/javascript" src="js/winlin.utility.js"></script>
  109. <script type="text/javascript">
  110. var flvPlayer = null;
  111. var tsPlayer = null;
  112. var hlsPlayer = null;
  113. var dashPlayer = null;
  114. var stopPlayers = function () {
  115. if (flvPlayer) {
  116. flvPlayer.destroy();
  117. flvPlayer = null;
  118. }
  119. if (tsPlayer) {
  120. tsPlayer.destroy();
  121. tsPlayer = null;
  122. }
  123. if (hlsPlayer) {
  124. hlsPlayer.destroy();
  125. hlsPlayer = null;
  126. }
  127. if (dashPlayer) {
  128. dashPlayer.destroy();
  129. dashPlayer = null;
  130. }
  131. };
  132. var hide_for_error = function () {
  133. $('#main_flash_alert').show();
  134. $('#main_info').hide();
  135. $('#main_tips').hide();
  136. $('#video_player').hide();
  137. //$('#btn_play').hide();
  138. stopPlayers();
  139. };
  140. var show_for_ok = function () {
  141. $('#main_flash_alert').hide();
  142. $('#main_info').show();
  143. $('#main_tips').show();
  144. $('#video_player').show();
  145. //$('#btn_play').show();
  146. };
  147. var apply_url_change = function() {
  148. var r = parse_rtmp_url($("#txt_url").val());
  149. var url = window.location.protocol + "//" + query.host + query.pathname + "?autostart=true"
  150. + "&app=" + r.app + "&stream=" + r.stream + "&server=" + r.server + "&port=" + r.port;
  151. url += (query.shp_identify) ? "&shp_identify=" + query.shp_identify : '';
  152. url += (r.vhost === "__defaultVhost__") ? "&vhost=" + r.server : "&vhost=" + r.vhost;
  153. url += (r.schema !== "rtmp") ? "&schema=" + r.schema : '';
  154. url += (query.buffer) ? "&buffer=" + query.buffer : '';
  155. url += (query.api_port) ? "&api_port=" + query.api_port : '';
  156. var queries = user_extra_params(query);
  157. queries = user_extra_params(r, queries);
  158. if (queries && queries.length) {
  159. url += '&' + queries.join('&');
  160. }
  161. $("#player_url").text($("#txt_url").val()).attr("href", url);
  162. $("#link_url").attr("href", url);
  163. // For RTMP, not support.
  164. if (r.schema === 'rtmp') {
  165. hide_for_error();
  166. return;
  167. }
  168. return r;
  169. };
  170. var start_play = function (r) {
  171. stopPlayers();
  172. if (!r) return;
  173. // Use H5 native to play mp4.
  174. if (r.stream.indexOf('.mp4') > 0) {
  175. $('#video_player').attr('src', r.url).show();
  176. return;
  177. }
  178. // Start play HTTP-TS.
  179. if (r.stream.indexOf('.ts') > 0) {
  180. if (!mpegts.getFeatureList().mseLivePlayback) {
  181. hide_for_error();
  182. return;
  183. }
  184. show_for_ok();
  185. tsPlayer = mpegts.createPlayer({type: 'mpegts', url: r.url, isLive: true, enableStashBuffer: false});
  186. tsPlayer.attachMediaElement(document.getElementById('video_player'));
  187. tsPlayer.load();
  188. tsPlayer.play();
  189. return;
  190. }
  191. // Start play HLS.
  192. if (r.stream.indexOf('.m3u8') > 0) {
  193. if (!Hls.isSupported()) {
  194. hide_for_error();
  195. return;
  196. }
  197. show_for_ok();
  198. hlsPlayer = new Hls();
  199. hlsPlayer.loadSource(r.url);
  200. hlsPlayer.attachMedia(document.getElementById('video_player'));
  201. return;
  202. }
  203. // Start play MPEG-DASH.
  204. if (r.stream.indexOf('.mpd') > 0) {
  205. show_for_ok();
  206. dashPlayer = dashjs.MediaPlayer().create();
  207. dashPlayer.initialize(document.querySelector("#video_player"), r.url, true);
  208. return;
  209. }
  210. // Start play HTTP-FLV.
  211. let isFlv = r.stream.indexOf('.flv') > 0;
  212. // Compatible with NGINX-HTTP-FLV module, see https://github.com/winshining/nginx-http-flv-module and the stream
  213. // url without .flv, such as:
  214. // http://localhost:8080/live?app=live&stream=livestream
  215. isFlv = isFlv || r.stream && r.url.indexOf('http') === 0;
  216. if (isFlv) {
  217. if (!mpegts.getFeatureList().mseLivePlayback) {
  218. hide_for_error();
  219. return;
  220. }
  221. show_for_ok();
  222. flvPlayer = mpegts.createPlayer({type: 'flv', url: r.url, isLive: true, enableStashBuffer: false});
  223. flvPlayer.attachMediaElement(document.getElementById('video_player'));
  224. flvPlayer.load();
  225. flvPlayer.play();
  226. return;
  227. }
  228. console.error('不支持的URL', r.url, r);
  229. $('#video_player').hide();
  230. };
  231. $("#txt_url").change(function(){
  232. apply_url_change();
  233. });
  234. $("#btn_play").click(function(){
  235. $('#video_player').prop('muted', false);
  236. var r = apply_url_change();
  237. start_play(r);
  238. });
  239. /****
  240. * The parameters for this page:
  241. * schema, the protocol schema, rtmp or http.
  242. * server, the ip of the url.
  243. * port, the rtmp port of url.
  244. * vhost, the vhost of url, can equals to server.
  245. * app, the app of url.
  246. * stream, the stream of url, can endwith .flv or .mp4 or nothing for RTMP.
  247. * autostart, whether auto play the stream.
  248. * buffer, the buffer time in seconds.
  249. * extra params:
  250. * shp_identify, hls+ param.
  251. * for example:
  252. * http://localhost:8088/players/srs_player.html?vhost=ossrs.net&app=live&stream=livestream&server=ossrs.net&port=1935&autostart=true&schema=rtmp
  253. * http://localhost:8088/players/srs_player.html?vhost=ossrs.net&app=live&stream=livestream.flv&server=ossrs.net&port=8080&autostart=true&schema=http
  254. */
  255. var query = parse_query_string();
  256. // get the vhost and port to set the default url.
  257. // url set to: http://localhost:8080/live/livestream.flv
  258. srs_init_flv("#txt_url");
  259. if (query.autostart === "true") {
  260. $('#video_player').prop('muted', true);
  261. console.warn('For autostart, we should mute it, see https://www.jianshu.com/p/c3c6944eed5a ' +
  262. 'or https://developers.google.com/web/updates/2017/09/autoplay-policy-changes#audiovideo_elements');
  263. var r = apply_url_change();
  264. start_play(r);
  265. } else {
  266. $('#video_player').hide();
  267. }
  268. </script>
  269. </html>