rtc_publisher.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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/rtcpublisher'/>
  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><a id="nav_rtc_player" href="rtc_player.html">RTC播放器</a></li>
  27. <li class="active"><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_publish">开始推流</button>
  49. </div>
  50. <label></label>
  51. <video id="rtc_media_player" width="320" autoplay muted></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. var pc = null; // Global handler to do cleanup when replaying.
  63. $(function(){
  64. // Async-awat-prmise based SRS RTC Publisher.
  65. function SrsRtcPublisherAsync() {
  66. var self = {};
  67. // @see https://github.com/rtcdn/rtcdn-draft
  68. // @url The WebRTC url to play with, for example:
  69. // webrtc://r.ossrs.net/live/livestream
  70. // or specifies the API port:
  71. // webrtc://r.ossrs.net:11985/live/livestream
  72. // or autostart the publish:
  73. // webrtc://r.ossrs.net/live/livestream?autostart=true
  74. // or change the app from live to myapp:
  75. // webrtc://r.ossrs.net:11985/myapp/livestream
  76. // or change the stream from livestream to mystream:
  77. // webrtc://r.ossrs.net:11985/live/mystream
  78. // or set the api server to myapi.domain.com:
  79. // webrtc://myapi.domain.com/live/livestream
  80. // or set the candidate(ip) of answer:
  81. // webrtc://r.ossrs.net/live/livestream?eip=39.107.238.185
  82. // or force to access https API:
  83. // webrtc://r.ossrs.net/live/livestream?schema=https
  84. // or use plaintext, without SRTP:
  85. // webrtc://r.ossrs.net/live/livestream?encrypt=false
  86. // or any other information, will pass-by in the query:
  87. // webrtc://r.ossrs.net/live/livestream?vhost=xxx
  88. // webrtc://r.ossrs.net/live/livestream?token=xxx
  89. self.publish = async function (url) {
  90. var conf = self.__internal.prepareUrl(url);
  91. self.pc.addTransceiver("audio", {direction: "sendonly"});
  92. self.pc.addTransceiver("video", {direction: "sendonly"});
  93. var stream = await navigator.mediaDevices.getUserMedia(
  94. {audio: true, video: {height: {max: 320}}}
  95. );
  96. // @see https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/addStream#Migrating_to_addTrack
  97. stream.getTracks().forEach(function (track) {
  98. self.pc.addTrack(track);
  99. });
  100. var offer = await self.pc.createOffer();
  101. await self.pc.setLocalDescription(offer);
  102. var session = await new Promise(function (resolve, reject) {
  103. // @see https://github.com/rtcdn/rtcdn-draft
  104. var data = {
  105. api: conf.apiUrl, streamurl: conf.streamUrl, clientip: null, sdp: offer.sdp
  106. };
  107. console.log("Generated offer: ", data);
  108. $.ajax({
  109. type: "POST", url: conf.apiUrl, data: JSON.stringify(data),
  110. contentType: 'application/json', dataType: 'json'
  111. }).done(function (data) {
  112. console.log("Got answer: ", data);
  113. if (data.code) {
  114. reject(data);
  115. return;
  116. }
  117. resolve(data);
  118. }).fail(function (reason) {
  119. reject(reason);
  120. });
  121. });
  122. await self.pc.setRemoteDescription(
  123. new RTCSessionDescription({type: 'answer', sdp: session.sdp})
  124. );
  125. session.simulator = conf.schema + '//' + conf.urlObject.server + ':' + conf.port + '/rtc/v1/nack/';
  126. // Notify about local stream when success.
  127. self.onaddstream && self.onaddstream({stream: stream});
  128. return session;
  129. };
  130. // Close the publisher.
  131. self.close = function () {
  132. self.pc.close();
  133. };
  134. // The callback when got local stream.
  135. self.onaddstream = function (event) {
  136. };
  137. // Internal APIs.
  138. self.__internal = {
  139. defaultPath: '/rtc/v1/publish/',
  140. prepareUrl: function (webrtcUrl) {
  141. var urlObject = self.__internal.parse(webrtcUrl);
  142. // If user specifies the schema, use it as API schema.
  143. var schema = urlObject.user_query.schema;
  144. schema = schema ? schema + ':' : window.location.protocol;
  145. var port = urlObject.port || 1985;
  146. if (schema === 'https:') {
  147. port = urlObject.port || 443;
  148. }
  149. // @see https://github.com/rtcdn/rtcdn-draft
  150. var api = urlObject.user_query.play || self.__internal.defaultPath;
  151. if (api.lastIndexOf('/') !== api.length - 1) {
  152. api += '/';
  153. }
  154. apiUrl = schema + '//' + urlObject.server + ':' + port + api;
  155. for (var key in urlObject.user_query) {
  156. if (key !== 'api' && key !== 'play') {
  157. apiUrl += '&' + key + '=' + urlObject.user_query[key];
  158. }
  159. }
  160. // Replace /rtc/v1/play/&k=v to /rtc/v1/play/?k=v
  161. var apiUrl = apiUrl.replace(api + '&', api + '?');
  162. var streamUrl = urlObject.url;
  163. return {apiUrl: apiUrl, streamUrl: streamUrl, schema: schema, urlObject: urlObject, port: port};
  164. },
  165. parse: function (url) {
  166. // @see: http://stackoverflow.com/questions/10469575/how-to-use-location-object-to-parse-url-without-redirecting-the-page-in-javascri
  167. var a = document.createElement("a");
  168. a.href = url.replace("rtmp://", "http://")
  169. .replace("webrtc://", "http://")
  170. .replace("rtc://", "http://");
  171. var vhost = a.hostname;
  172. var app = a.pathname.substr(1, a.pathname.lastIndexOf("/") - 1);
  173. var stream = a.pathname.substr(a.pathname.lastIndexOf("/") + 1);
  174. // parse the vhost in the params of app, that srs supports.
  175. app = app.replace("...vhost...", "?vhost=");
  176. if (app.indexOf("?") >= 0) {
  177. var params = app.substr(app.indexOf("?"));
  178. app = app.substr(0, app.indexOf("?"));
  179. if (params.indexOf("vhost=") > 0) {
  180. vhost = params.substr(params.indexOf("vhost=") + "vhost=".length);
  181. if (vhost.indexOf("&") > 0) {
  182. vhost = vhost.substr(0, vhost.indexOf("&"));
  183. }
  184. }
  185. }
  186. // when vhost equals to server, and server is ip,
  187. // the vhost is __defaultVhost__
  188. if (a.hostname === vhost) {
  189. var re = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
  190. if (re.test(a.hostname)) {
  191. vhost = "__defaultVhost__";
  192. }
  193. }
  194. // parse the schema
  195. var schema = "rtmp";
  196. if (url.indexOf("://") > 0) {
  197. schema = url.substr(0, url.indexOf("://"));
  198. }
  199. var port = a.port;
  200. if (!port) {
  201. if (schema === 'http') {
  202. port = 80;
  203. } else if (schema === 'https') {
  204. port = 443;
  205. } else if (schema === 'rtmp') {
  206. port = 1935;
  207. }
  208. }
  209. var ret = {
  210. url: url,
  211. schema: schema,
  212. server: a.hostname, port: port,
  213. vhost: vhost, app: app, stream: stream
  214. };
  215. self.__internal.fill_query(a.search, ret);
  216. // For webrtc API, we use 443 if page is https, or schema specified it.
  217. if (!ret.port) {
  218. if (schema === 'webrtc' || schema === 'rtc') {
  219. if (ret.user_query.schema === 'https') {
  220. ret.port = 443;
  221. } else if (window.location.href.indexOf('https://') === 0) {
  222. ret.port = 443;
  223. } else {
  224. // For WebRTC, SRS use 1985 as default API port.
  225. ret.port = 1985;
  226. }
  227. }
  228. }
  229. return ret;
  230. },
  231. fill_query: function (query_string, obj) {
  232. // pure user query object.
  233. obj.user_query = {};
  234. if (query_string.length === 0) {
  235. return;
  236. }
  237. // split again for angularjs.
  238. if (query_string.indexOf("?") >= 0) {
  239. query_string = query_string.split("?")[1];
  240. }
  241. var queries = query_string.split("&");
  242. for (var i = 0; i < queries.length; i++) {
  243. var elem = queries[i];
  244. var query = elem.split("=");
  245. obj[query[0]] = query[1];
  246. obj.user_query[query[0]] = query[1];
  247. }
  248. // alias domain for vhost.
  249. if (obj.domain) {
  250. obj.vhost = obj.domain;
  251. }
  252. }
  253. };
  254. self.pc = new RTCPeerConnection(null);
  255. return self;
  256. }
  257. var sdk = null; // Global handler to do cleanup when republishing.
  258. var startPublish = function() {
  259. $('#rtc_media_player').show();
  260. // Close PC when user replay.
  261. if (sdk) {
  262. sdk.close();
  263. }
  264. sdk = new SrsRtcPublisherAsync();
  265. sdk.onaddstream = function (event) {
  266. console.log('Start publish, event: ', event);
  267. $('#rtc_media_player').prop('srcObject', event.stream);
  268. };
  269. // For example:
  270. // webrtc://r.ossrs.net/live/livestream
  271. var url = $("#txt_url").val();
  272. sdk.publish(url).then(function(session){
  273. $('#sessionid').html(session.sessionid);
  274. $('#simulator-drop').attr('href', session.simulator + '?drop=1&username=' + session.sessionid);
  275. }).catch(function (reason) {
  276. sdk.close();
  277. $('#rtc_media_player').hide();
  278. console.error(reason);
  279. });
  280. };
  281. $('#rtc_media_player').hide();
  282. var query = parse_query_string();
  283. srs_init_rtc("#txt_url", query);
  284. $("#btn_publish").click(startPublish);
  285. if (query.autostart === 'true') {
  286. startPublish();
  287. }
  288. });
  289. </script>
  290. </body>
  291. </html>
  292. </html>