rtc_player.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>SRS</title>
  5. <meta charset="utf-8">
  6. <style>
  7. body{
  8. padding-top: 55px;
  9. }
  10. </style>
  11. <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
  12. <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
  13. <script type="text/javascript" src="js/adapter-7.4.0.min.js"></script>
  14. <script type="text/javascript" src="js/winlin.utility.js"></script>
  15. <script type="text/javascript" src="js/srs.page.js"></script>
  16. </head>
  17. <body>
  18. <img src='https://ossrs.net/gif/v1/sls.gif?site=ossrs.net&path=/player/rtcplayer'/>
  19. <div class="navbar navbar-fixed-top">
  20. <div class="navbar-inner">
  21. <div class="container">
  22. <a id="srs_index" class="brand" href="https://github.com/ossrs/srs">SRS</a>
  23. <div class="nav-collapse collapse">
  24. <ul class="nav">
  25. <li><a id="nav_srs_player" href="srs_player.html">SRS播放器</a></li>
  26. <li class="active"><a id="nav_rtc_player" href="rtc_player.html">RTC播放器</a></li>
  27. <li><a id="nav_rtc_publisher" href="rtc_publisher.html">RTC推流</a></li>
  28. <li><a href="http://ossrs.net/srs.release/releases/app.html">iOS/Andriod</a></li>
  29. <!--<li><a id="nav_srs_publisher" href="srs_publisher.html">SRS编码器</a></li>-->
  30. <!--<li><a id="nav_srs_chat" href="srs_chat.html">SRS会议</a></li>-->
  31. <!--<li><a id="nav_srs_bwt" href="srs_bwt.html">SRS测网速</a></li>-->
  32. <!--<li><a id="nav_vlc" href="vlc.html">VLC播放器</a></li>-->
  33. <li><a id="nav_gb28181" href="srs_gb28181.html">GB28181</a></li>
  34. <li>
  35. <a href="https://github.com/ossrs/srs">
  36. <img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/ossrs/srs?style=social">
  37. </a>
  38. </li>
  39. </ul>
  40. </div>
  41. </div>
  42. </div>
  43. </div>
  44. <div class="container">
  45. <div class="form-inline">
  46. URL:
  47. <input type="text" id="txt_url" class="input-xxlarge" value="">
  48. <button class="btn btn-primary" id="btn_play">播放视频</button>
  49. </div>
  50. <label></label>
  51. <video id="rtc_media_player" controls autoplay></video>
  52. <label></label>
  53. SessionID: <span id='sessionid'></span>
  54. <label></label>
  55. Simulator: <a href='#' id='simulator-drop'>Drop</a>
  56. <footer>
  57. <p></p>
  58. <p><a href="https://github.com/ossrs/srs">SRS Team &copy; 2020</a></p>
  59. </footer>
  60. </div>
  61. <script type="text/javascript">
  62. $(function(){
  63. // Async-await-promise based SRS RTC Player.
  64. function SrsRtcPlayerAsync() {
  65. var self = {};
  66. // @see https://github.com/rtcdn/rtcdn-draft
  67. // @url The WebRTC url to play with, for example:
  68. // webrtc://r.ossrs.net/live/livestream
  69. // or specifies the API port:
  70. // webrtc://r.ossrs.net:11985/live/livestream
  71. // or autostart the play:
  72. // webrtc://r.ossrs.net/live/livestream?autostart=true
  73. // or change the app from live to myapp:
  74. // webrtc://r.ossrs.net:11985/myapp/livestream
  75. // or change the stream from livestream to mystream:
  76. // webrtc://r.ossrs.net:11985/live/mystream
  77. // or set the api server to myapi.domain.com:
  78. // webrtc://myapi.domain.com/live/livestream
  79. // or set the candidate(ip) of answer:
  80. // webrtc://r.ossrs.net/live/livestream?eip=39.107.238.185
  81. // or force to access https API:
  82. // webrtc://r.ossrs.net/live/livestream?schema=https
  83. // or use plaintext, without SRTP:
  84. // webrtc://r.ossrs.net/live/livestream?encrypt=false
  85. // or any other information, will pass-by in the query:
  86. // webrtc://r.ossrs.net/live/livestream?vhost=xxx
  87. // webrtc://r.ossrs.net/live/livestream?token=xxx
  88. self.play = async function(url) {
  89. var conf = self.__internal.prepareUrl(url);
  90. self.pc.addTransceiver("audio", {direction: "recvonly"});
  91. self.pc.addTransceiver("video", {direction: "recvonly"});
  92. var offer = await self.pc.createOffer();
  93. await self.pc.setLocalDescription(offer);
  94. var session = await new Promise(function(resolve, reject) {
  95. // @see https://github.com/rtcdn/rtcdn-draft
  96. var data = {
  97. api: conf.apiUrl, streamurl: conf.streamUrl, clientip: null, sdp: offer.sdp
  98. };
  99. console.log("Generated offer: ", data);
  100. $.ajax({
  101. type: "POST", url: conf.apiUrl, data: JSON.stringify(data),
  102. contentType:'application/json', dataType: 'json'
  103. }).done(function(data) {
  104. console.log("Got answer: ", data);
  105. if (data.code) {
  106. reject(data); return;
  107. }
  108. resolve(data);
  109. }).fail(function(reason){
  110. reject(reason);
  111. });
  112. });
  113. await self.pc.setRemoteDescription(
  114. new RTCSessionDescription({type: 'answer', sdp: session.sdp})
  115. );
  116. return session;
  117. };
  118. // Close the publisher.
  119. self.close = function() {
  120. self.pc.close();
  121. };
  122. // The callback when got remote stream.
  123. self.onaddstream = function (event) {};
  124. // Internal APIs.
  125. self.__internal = {
  126. defaultPath: '/rtc/v1/play/',
  127. prepareUrl: function (webrtcUrl) {
  128. var urlObject = self.__internal.parse(webrtcUrl);
  129. // If user specifies the schema, use it as API schema.
  130. var schema = urlObject.user_query.schema;
  131. schema = schema ? schema + ':' : window.location.protocol;
  132. var port = urlObject.port || 1985;
  133. if (schema === 'https:') {
  134. port = urlObject.port || 443;
  135. }
  136. // @see https://github.com/rtcdn/rtcdn-draft
  137. var api = urlObject.user_query.play || self.__internal.defaultPath;
  138. if (api.lastIndexOf('/') !== api.length - 1) {
  139. api += '/';
  140. }
  141. apiUrl = schema + '//' + urlObject.server + ':' + port + api;
  142. for (var key in urlObject.user_query) {
  143. if (key !== 'api' && key !== 'play') {
  144. apiUrl += '&' + key + '=' + urlObject.user_query[key];
  145. }
  146. }
  147. // Replace /rtc/v1/play/&k=v to /rtc/v1/play/?k=v
  148. var apiUrl = apiUrl.replace(api + '&', api + '?');
  149. var streamUrl = urlObject.url;
  150. return {apiUrl: apiUrl, streamUrl: streamUrl, schema: schema, urlObject: urlObject, port: port};
  151. },
  152. parse: function (url) {
  153. // @see: http://stackoverflow.com/questions/10469575/how-to-use-location-object-to-parse-url-without-redirecting-the-page-in-javascri
  154. var a = document.createElement("a");
  155. a.href = url.replace("rtmp://", "http://")
  156. .replace("webrtc://", "http://")
  157. .replace("rtc://", "http://");
  158. var vhost = a.hostname;
  159. var app = a.pathname.substr(1, a.pathname.lastIndexOf("/") - 1);
  160. var stream = a.pathname.substr(a.pathname.lastIndexOf("/") + 1);
  161. // parse the vhost in the params of app, that srs supports.
  162. app = app.replace("...vhost...", "?vhost=");
  163. if (app.indexOf("?") >= 0) {
  164. var params = app.substr(app.indexOf("?"));
  165. app = app.substr(0, app.indexOf("?"));
  166. if (params.indexOf("vhost=") > 0) {
  167. vhost = params.substr(params.indexOf("vhost=") + "vhost=".length);
  168. if (vhost.indexOf("&") > 0) {
  169. vhost = vhost.substr(0, vhost.indexOf("&"));
  170. }
  171. }
  172. }
  173. // when vhost equals to server, and server is ip,
  174. // the vhost is __defaultVhost__
  175. if (a.hostname === vhost) {
  176. var re = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
  177. if (re.test(a.hostname)) {
  178. vhost = "__defaultVhost__";
  179. }
  180. }
  181. // parse the schema
  182. var schema = "rtmp";
  183. if (url.indexOf("://") > 0) {
  184. schema = url.substr(0, url.indexOf("://"));
  185. }
  186. var port = a.port;
  187. if (!port) {
  188. if (schema === 'http') {
  189. port = 80;
  190. } else if (schema === 'https') {
  191. port = 443;
  192. } else if (schema === 'rtmp') {
  193. port = 1935;
  194. }
  195. }
  196. var ret = {
  197. url: url,
  198. schema: schema,
  199. server: a.hostname, port: port,
  200. vhost: vhost, app: app, stream: stream
  201. };
  202. self.__internal.fill_query(a.search, ret);
  203. // For webrtc API, we use 443 if page is https, or schema specified it.
  204. if (!ret.port) {
  205. if (schema === 'webrtc' || schema === 'rtc') {
  206. if (ret.user_query.schema === 'https') {
  207. ret.port = 443;
  208. } else if (window.location.href.indexOf('https://') === 0) {
  209. ret.port = 443;
  210. } else {
  211. // For WebRTC, SRS use 1985 as default API port.
  212. ret.port = 1985;
  213. }
  214. }
  215. }
  216. return ret;
  217. },
  218. fill_query: function (query_string, obj) {
  219. // pure user query object.
  220. obj.user_query = {};
  221. if (query_string.length === 0) {
  222. return;
  223. }
  224. // split again for angularjs.
  225. if (query_string.indexOf("?") >= 0) {
  226. query_string = query_string.split("?")[1];
  227. }
  228. var queries = query_string.split("&");
  229. for (var i = 0; i < queries.length; i++) {
  230. var elem = queries[i];
  231. var query = elem.split("=");
  232. obj[query[0]] = query[1];
  233. obj.user_query[query[0]] = query[1];
  234. }
  235. // alias domain for vhost.
  236. if (obj.domain) {
  237. obj.vhost = obj.domain;
  238. }
  239. }
  240. };
  241. self.pc = new RTCPeerConnection(null);
  242. self.pc.onaddstream = function (event) {
  243. if (self.onaddstream) {
  244. self.onaddstream(event);
  245. }
  246. };
  247. return self;
  248. }
  249. var sdk = null; // Global handler to do cleanup when replaying.
  250. var startPlay = function() {
  251. $('#rtc_media_player').show();
  252. // Close PC when user replay.
  253. if (sdk) {
  254. sdk.close();
  255. }
  256. sdk = new SrsRtcPlayerAsync();
  257. sdk.onaddstream = function (event) {
  258. console.log('Start play, event: ', event);
  259. $('#rtc_media_player').prop('srcObject', event.stream);
  260. };
  261. // For example:
  262. // webrtc://r.ossrs.net/live/livestream
  263. var url = $("#txt_url").val();
  264. sdk.play(url).then(function(session){
  265. $('#sessionid').html(session.sessionid);
  266. $('#simulator-drop').attr('href', session.simulator + '?drop=1&username=' + session.sessionid);
  267. }).catch(function (reason) {
  268. sdk.close();
  269. $('#rtc_media_player').hide();
  270. console.error(reason);
  271. });
  272. };
  273. $('#rtc_media_player').hide();
  274. var query = parse_query_string();
  275. srs_init_rtc("#txt_url", query);
  276. $("#btn_play").click(function() {
  277. $('#rtc_media_player').prop('muted', false);
  278. startPlay();
  279. });
  280. if (query.autostart === 'true') {
  281. $('#rtc_media_player').prop('muted', true);
  282. console.warn('For autostart, we should mute it, see https://www.jianshu.com/p/c3c6944eed5a ' +
  283. 'or https://developers.google.com/web/updates/2017/09/autoplay-policy-changes#audiovideo_elements');
  284. startPlay();
  285. }
  286. });
  287. </script>
  288. </body>
  289. </html>