2
0

rtc_publisher.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>SRS</title>
  5. <meta charset="utf-8">
  6. <style>
  7. body{
  8. padding-top: 30px;
  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/srs.sdk.js"></script>
  15. <script type="text/javascript" src="js/winlin.utility.js"></script>
  16. <script type="text/javascript" src="js/srs.page.js"></script>
  17. </head>
  18. <body>
  19. <img src='https://ossrs.net/gif/v1/sls.gif?site=ossrs.net&path=/player/rtcpublisher'/>
  20. <div class="navbar navbar-fixed-top">
  21. <div class="navbar-inner">
  22. <div class="container">
  23. <a id="srs_index" class="brand" href="https://github.com/ossrs/srs">SRS</a>
  24. <div class="nav-collapse collapse">
  25. <ul class="nav">
  26. <li><a id="nav_srs_player" href="srs_player.html">SRS播放器</a></li>
  27. <li><a id="nav_rtc_player" href="rtc_player.html">RTC播放器</a></li>
  28. <li class="active"><a id="nav_rtc_publisher" href="rtc_publisher.html">RTC推流</a></li>
  29. <li><a href="http://ossrs.net/srs.release/releases/app.html">iOS/Andriod</a></li>
  30. <!--<li><a id="nav_srs_publisher" href="srs_publisher.html">SRS编码器</a></li>-->
  31. <!--<li><a id="nav_srs_chat" href="srs_chat.html">SRS会议</a></li>-->
  32. <!--<li><a id="nav_srs_bwt" href="srs_bwt.html">SRS测网速</a></li>-->
  33. <!--<li><a id="nav_vlc" href="vlc.html">VLC播放器</a></li>-->
  34. <!--<li><a id="nav_gb28181" href="srs_gb28181.html">GB28181</a></li>-->
  35. <li>
  36. <a href="https://github.com/ossrs/srs">
  37. <img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/ossrs/srs?style=social">
  38. </a>
  39. </li>
  40. </ul>
  41. </div>
  42. </div>
  43. </div>
  44. </div>
  45. <div class="container">
  46. <div class="form-inline">
  47. URL:
  48. <input type="text" id="txt_url" class="input-xxlarge" value="">
  49. <button class="btn btn-primary" id="btn_publish">开始推流</button>
  50. </div>
  51. <label></label>
  52. <video id="rtc_media_player" width="320" autoplay muted></video>
  53. <label></label>
  54. SessionID: <span id='sessionid'></span>
  55. <label></label>
  56. Audio: <span id='acodecs'></span><br/>
  57. Video: <span id='vcodecs'></span>
  58. <label></label>
  59. Simulator: <a href='#' id='simulator-drop'>Drop</a>
  60. <footer>
  61. <p></p>
  62. <p><a href="https://github.com/ossrs/srs">SRS Team &copy; 2020</a></p>
  63. </footer>
  64. </div>
  65. <script type="text/javascript">
  66. $(function(){
  67. var sdk = null; // Global handler to do cleanup when republishing.
  68. var startPublish = function() {
  69. $('#rtc_media_player').show();
  70. // Close PC when user replay.
  71. if (sdk) {
  72. sdk.close();
  73. }
  74. sdk = new SrsRtcPublisherAsync();
  75. // User should set the stream when publish is done, @see https://webrtc.org/getting-started/media-devices
  76. // However SRS SDK provides a consist API like https://webrtc.org/getting-started/remote-streams
  77. $('#rtc_media_player').prop('srcObject', sdk.stream);
  78. // Optional callback, SDK will add track to stream.
  79. // sdk.ontrack = function (event) { console.log('Got track', event); sdk.stream.addTrack(event.track); };
  80. // https://developer.mozilla.org/en-US/docs/Web/Media/Formats/WebRTC_codecs#getting_the_supported_codecs
  81. sdk.pc.onicegatheringstatechange = function (event) {
  82. if (sdk.pc.iceGatheringState === "complete") {
  83. $('#acodecs').html(SrsRtcFormatSenders(sdk.pc.getSenders(), "audio"));
  84. $('#vcodecs').html(SrsRtcFormatSenders(sdk.pc.getSenders(), "video"));
  85. }
  86. };
  87. // For example: webrtc://r.ossrs.net/live/livestream
  88. var url = $("#txt_url").val();
  89. sdk.publish(url).then(function(session){
  90. $('#sessionid').html(session.sessionid);
  91. $('#simulator-drop').attr('href', session.simulator + '?drop=1&username=' + session.sessionid);
  92. }).catch(function (reason) {
  93. // Throw by sdk.
  94. if (reason instanceof SrsError) {
  95. if (reason.name === 'HttpsRequiredError') {
  96. alert(`WebRTC推流必须是HTTPS或者localhost:${reason.name} ${reason.message}`);
  97. } else {
  98. alert(`${reason.name} ${reason.message}`);
  99. }
  100. }
  101. // See https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia#exceptions
  102. if (reason instanceof DOMException) {
  103. if (reason.name === 'NotFoundError') {
  104. alert(`找不到麦克风和摄像头设备:getUserMedia ${reason.name} ${reason.message}`);
  105. } else if (reason.name === 'NotAllowedError') {
  106. alert(`你禁止了网页访问摄像头和麦克风:getUserMedia ${reason.name} ${reason.message}`);
  107. } else if (['AbortError', 'NotAllowedError', 'NotFoundError', 'NotReadableError', 'OverconstrainedError', 'SecurityError', 'TypeError'].includes(reason.name)) {
  108. alert(`getUserMedia ${reason.name} ${reason.message}`);
  109. }
  110. }
  111. sdk.close();
  112. $('#rtc_media_player').hide();
  113. console.error(reason);
  114. });
  115. };
  116. $('#rtc_media_player').hide();
  117. var query = parse_query_string();
  118. srs_init_rtc("#txt_url", query);
  119. $("#btn_publish").click(startPublish);
  120. // Never play util windows loaded @see https://github.com/ossrs/srs/issues/2732
  121. if (query.autostart === 'true') {
  122. window.addEventListener("load", function(){ startPublish(); });
  123. }
  124. });
  125. </script>
  126. </body>
  127. </html>