srs_player.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 style="width: 0px; height: 0px;" 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. <audio id="audio_player" width="100%" autoplay controls></audio>
  58. </div>
  59. <p>
  60. Share: <a href="#" id="link_url" target="_blank">Please right-click and copy this link.</a>
  61. </p>
  62. <div id="main_tips">
  63. <p></p>
  64. <p>
  65. Recommended other players:
  66. <ul>
  67. <li><a href="https://github.com/xqq/mpegts.js">flv.js</a>, HTML5/MSE for playing HTTP-FLV</li>
  68. <li><a href="https://github.com/xqq/mpegts.js">mpegts.js</a>, HTML5/MSE for playing HTTP-TS</li>
  69. <li><a href="https://github.com/video-dev/hls.js/">hls.js</a>, HTML5/MSE for playing HLS</li>
  70. <li><a href="https://github.com/Dash-Industry-Forum/dash.js">dash.js</a>, HTML5/MSE for playing MPEG-DASH</li>
  71. </ul>
  72. </p>
  73. </div>
  74. </div>
  75. </div>
  76. <div name="detect_flash">
  77. <div id="main_flash_alert" class="alert alert-danger fade in hide">
  78. <button type="button" class="close" data-dismiss="alert">×</button>
  79. <p>
  80. <a href="https://www.adobe.com/products/flashplayer/end-of-life.html" target="_blank">Flash is dead</a>,
  81. Unable to play RTMP streams, you can use ffplay or VLC player to play.
  82. </p>
  83. <ul>
  84. <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>
  85. <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>
  86. <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>
  87. <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>
  88. </ul>
  89. <p>
  90. 更多信息,参考<a href="https://mp.weixin.qq.com/s/oYn5q4fF9afaged23Ueudg" target="_blank">没有Flash如何做直播?</a>
  91. </p>
  92. </div>
  93. </div>
  94. <footer>
  95. <p></p>
  96. <p><a href="https://github.com/ossrs/srs">SRS Team &copy; 2013</a></p>
  97. </footer>
  98. </div>
  99. </body>
  100. <script type="text/javascript" src="js/jquery-1.12.2.min.js"></script>
  101. <script type="text/javascript" src="js/bootstrap.min.js"></script>
  102. <script type="text/javascript" src="js/mpegts-1.7.3.min.js"></script>
  103. <script type="text/javascript" src="js/hls-1.4.14.min.js"></script>
  104. <script type="text/javascript" src="js/dash-v4.5.1.all.min.js"></script>
  105. <script type="text/javascript" src="js/json2.js"></script>
  106. <script type="text/javascript" src="js/srs.page.js"></script>
  107. <script type="text/javascript" src="js/srs.log.js"></script>
  108. <script type="text/javascript" src="js/srs.utility.js"></script>
  109. <script type="text/javascript" src="js/winlin.utility.js"></script>
  110. <script type="text/javascript">
  111. var flvPlayer = null;
  112. var tsPlayer = null;
  113. var hlsPlayer = null;
  114. var dashPlayer = null;
  115. var stopPlayers = function () {
  116. if (flvPlayer) {
  117. flvPlayer.destroy();
  118. flvPlayer = null;
  119. }
  120. if (tsPlayer) {
  121. tsPlayer.destroy();
  122. tsPlayer = null;
  123. }
  124. if (hlsPlayer) {
  125. hlsPlayer.destroy();
  126. hlsPlayer = null;
  127. }
  128. if (dashPlayer) {
  129. dashPlayer.destroy();
  130. dashPlayer = null;
  131. }
  132. };
  133. var hide_for_error = function () {
  134. $('#main_flash_alert').show();
  135. $('#main_info').hide();
  136. $('#main_tips').hide();
  137. $('#video_player').hide();
  138. $('#audio_player').hide();
  139. //$('#btn_play').hide();
  140. stopPlayers();
  141. };
  142. var show_for_video_ok = function () {
  143. $('#main_flash_alert').hide();
  144. $('#main_info').show();
  145. $('#main_tips').show();
  146. $('#video_player').show();
  147. $('#audio_player').hide();
  148. //$('#btn_play').show();
  149. };
  150. var show_for_audio_ok = function () {
  151. $('#main_flash_alert').hide();
  152. $('#main_info').show();
  153. $('#main_tips').show();
  154. $('#video_player').hide();
  155. $('#audio_player').show();
  156. //$('#btn_play').show();
  157. };
  158. var apply_url_change = function() {
  159. var r = parse_rtmp_url($("#txt_url").val());
  160. var url = window.location.protocol + "//" + query.host + query.pathname + "?autostart=true"
  161. + "&app=" + r.app + "&stream=" + r.stream + "&server=" + r.server + "&port=" + r.port;
  162. url += (query.shp_identify) ? "&shp_identify=" + query.shp_identify : '';
  163. url += (r.vhost === "__defaultVhost__") ? "&vhost=" + r.server : "&vhost=" + r.vhost;
  164. url += (r.schema !== "rtmp") ? "&schema=" + r.schema : '';
  165. url += (query.buffer) ? "&buffer=" + query.buffer : '';
  166. url += (query.api_port) ? "&api_port=" + query.api_port : '';
  167. var queries = user_extra_params(query);
  168. queries = user_extra_params(r, queries);
  169. if (queries && queries.length) {
  170. url += '&' + queries.join('&');
  171. }
  172. $("#player_url").text($("#txt_url").val()).attr("href", url);
  173. $("#link_url").attr("href", url);
  174. // For RTMP, not support.
  175. if (r.schema === 'rtmp') {
  176. hide_for_error();
  177. return;
  178. }
  179. return r;
  180. };
  181. var start_play = function (r) {
  182. stopPlayers();
  183. if (!r) return;
  184. const video = document.getElementById('video_player');
  185. // Use H5 native to play aac/mp3.
  186. if (r.stream.indexOf('.mp3') > 0 || r.stream.indexOf('.aac') > 0) {
  187. $('#audio_player').attr('src', r.url).show();
  188. show_for_audio_ok();
  189. return;
  190. }
  191. // Use H5 native to play mp4.
  192. if (r.stream.indexOf('.mp4') > 0) {
  193. $('#video_player').attr('src', r.url).show();
  194. show_for_video_ok();
  195. return;
  196. }
  197. // Start play HTTP-TS.
  198. if (r.stream.indexOf('.ts') > 0) {
  199. if (!mpegts.getFeatureList().mseLivePlayback) {
  200. hide_for_error();
  201. return;
  202. }
  203. show_for_video_ok();
  204. tsPlayer = mpegts.createPlayer({type: 'mpegts', url: r.url, isLive: true, enableStashBuffer: false});
  205. tsPlayer.attachMediaElement(video);
  206. tsPlayer.load();
  207. tsPlayer.play();
  208. return;
  209. }
  210. // Start play HLS.
  211. if (r.stream.indexOf('.m3u8') > 0) {
  212. if (!Hls.isSupported()) {
  213. hide_for_error();
  214. return;
  215. }
  216. show_for_video_ok();
  217. // See https://github.com/video-dev/hls.js/blob/master/docs/API.md#maxlivesyncplaybackrate
  218. // See https://github.com/video-dev/hls.js/issues/3077#issuecomment-705152394
  219. hlsPlayer = new Hls({
  220. enableWorker: true, // Improve performance and avoid lag/frame drops.
  221. lowLatencyMode: true, // Enable Low-Latency HLS part playlist and segment loading.
  222. liveSyncDurationCount: 2, // Start from the last few segments.
  223. liveMaxLatencyDurationCount: 10, // Maximum delay allowed from edge of live.
  224. maxBufferLength: 8, // Maximum buffer length in seconds.
  225. maxMaxBufferLength: 10, // The max Maximum buffer length in seconds.
  226. maxLiveSyncPlaybackRate: 1.2, // Catch up if the latency is large.
  227. liveDurationInfinity: true // Override current Media Source duration to Infinity for a live broadcast.
  228. });
  229. hlsPlayer.loadSource(r.url);
  230. hlsPlayer.attachMedia(video);
  231. video.play();
  232. return;
  233. }
  234. // Start play MPEG-DASH.
  235. if (r.stream.indexOf('.mpd') > 0) {
  236. show_for_video_ok();
  237. dashPlayer = dashjs.MediaPlayer().create();
  238. dashPlayer.initialize(document.querySelector("#video_player"), r.url, true);
  239. return;
  240. }
  241. // Start play HTTP-FLV.
  242. let isFlv = r.stream.indexOf('.flv') > 0;
  243. // Compatible with NGINX-HTTP-FLV module, see https://github.com/winshining/nginx-http-flv-module and the stream
  244. // url without .flv, such as:
  245. // http://localhost:8080/live?app=live&stream=livestream
  246. isFlv = isFlv || r.stream && r.url.indexOf('http') === 0;
  247. if (isFlv) {
  248. if (!mpegts.getFeatureList().mseLivePlayback) {
  249. hide_for_error();
  250. return;
  251. }
  252. show_for_video_ok();
  253. // See https://github.com/xqq/mpegts.js/blob/master/src/config.js#L32
  254. // See https://github.com/xqq/mpegts.js/blob/master/docs/api.md
  255. flvPlayer = mpegts.createPlayer({
  256. type: 'flv', url: r.url,
  257. isLive: true, enableStashBuffer: false, liveSync: true
  258. });
  259. flvPlayer.attachMediaElement(video);
  260. flvPlayer.load();
  261. flvPlayer.play();
  262. return;
  263. }
  264. $('#video_player').hide();
  265. const msg = `Not supported URL ${r.url} ${JSON.stringify(r)}`;
  266. console.error(msg);
  267. alert(msg);
  268. };
  269. $("#txt_url").change(function(){
  270. apply_url_change();
  271. });
  272. $("#btn_play").click(function(){
  273. $('#video_player').prop('muted', false);
  274. $('#audio_player').prop('muted', false);
  275. var r = apply_url_change();
  276. start_play(r);
  277. });
  278. /****
  279. * The parameters for this page:
  280. * schema, the protocol schema, rtmp or http.
  281. * server, the ip of the url.
  282. * port, the rtmp port of url.
  283. * vhost, the vhost of url, can equals to server.
  284. * app, the app of url.
  285. * stream, the stream of url, can endwith .flv or .mp4 or nothing for RTMP.
  286. * autostart, whether auto play the stream.
  287. * buffer, the buffer time in seconds.
  288. * extra params:
  289. * shp_identify, hls+ param.
  290. * for example:
  291. * http://localhost:8088/players/srs_player.html?vhost=ossrs.net&app=live&stream=livestream&server=ossrs.net&port=1935&autostart=true&schema=rtmp
  292. * http://localhost:8088/players/srs_player.html?vhost=ossrs.net&app=live&stream=livestream.flv&server=ossrs.net&port=8080&autostart=true&schema=http
  293. */
  294. var query = parse_query_string();
  295. // get the vhost and port to set the default url.
  296. // url set to: http://localhost:8080/live/livestream.flv
  297. srs_init_flv("#txt_url");
  298. if (query.autostart === "true") {
  299. // Note that only need to mute video player, because audio player is impossible to autostart whatever muted or
  300. // not, however you can preload the audio stream by setting the src of audio, see https://developer.mozilla.org/en-US/docs/Web/Media/Autoplay_guide#example_allowing_autoplay_and_fullscreen_mode
  301. $('#video_player').prop('muted', true);
  302. console.warn('For autostart, we should mute it, see https://www.jianshu.com/p/c3c6944eed5a ' +
  303. 'or https://developers.google.com/web/updates/2017/09/autoplay-policy-changes#audiovideo_elements');
  304. var r = apply_url_change();
  305. start_play(r);
  306. } else {
  307. $('#video_player').hide();
  308. $('#audio_player').hide();
  309. }
  310. </script>
  311. </html>