rtcpushchat.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>WebRTC webcam</title>
  7. <style>
  8. button {
  9. padding: 8px 16px;
  10. }
  11. video {
  12. width: 100%;
  13. }
  14. .option {
  15. margin-bottom: 8px;
  16. }
  17. #media {
  18. max-width: 1280px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="option">
  24. <input id="use-stun" type="checkbox"/>
  25. <label for="use-stun">Use STUN server</label>
  26. </div>
  27. <button class="btn btn-primary" id="btn_play">Start</button>
  28. <form class="form-inline" id="echo-form">
  29. <div class="form-group">
  30. <p>input text</p>
  31. <textarea cols="2" rows="3" style="width:600px;height:50px;" class="form-control" id="message">test</textarea>
  32. </div>
  33. <button type="submit" class="btn btn-default">Send</button>
  34. </form>
  35. <div id="media">
  36. <h2>Media</h2>
  37. <video id="rtc_media_player" style="width:600px;" controls autoplay></video>
  38. </div>
  39. <script src="srs.sdk.js"></script>
  40. <script type="text/javascript" src="http://cdn.sockjs.org/sockjs-0.3.4.js"></script>
  41. <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
  42. </body>
  43. <script type="text/javascript" charset="utf-8">
  44. $(document).ready(function() {
  45. // var host = window.location.hostname
  46. // var ws = new WebSocket("ws://"+host+":8000/humanecho");
  47. // //document.getElementsByTagName("video")[0].setAttribute("src", aa["video"]);
  48. // ws.onopen = function() {
  49. // console.log('Connected');
  50. // };
  51. // ws.onmessage = function(e) {
  52. // console.log('Received: ' + e.data);
  53. // data = e
  54. // var vid = JSON.parse(data.data);
  55. // console.log(typeof(vid),vid)
  56. // //document.getElementsByTagName("video")[0].setAttribute("src", vid["video"]);
  57. // };
  58. // ws.onclose = function(e) {
  59. // console.log('Closed');
  60. // };
  61. $('#echo-form').on('submit', function(e) {
  62. e.preventDefault();
  63. var message = $('#message').val();
  64. console.log('Sending: ' + message);
  65. fetch('/human', {
  66. body: JSON.stringify({
  67. text: message,
  68. type: 'chat',
  69. }),
  70. headers: {
  71. 'Content-Type': 'application/json'
  72. },
  73. method: 'POST'
  74. });
  75. //ws.send(message);
  76. $('#message').val('');
  77. });
  78. });
  79. $(function(){
  80. var sdk = null; // Global handler to do cleanup when republishing.
  81. var startPlay = function() {
  82. $('#rtc_media_player').show();
  83. // Close PC when user replay.
  84. if (sdk) {
  85. sdk.close();
  86. }
  87. sdk = new SrsRtcWhipWhepAsync();
  88. // User should set the stream when publish is done, @see https://webrtc.org/getting-started/media-devices
  89. // However SRS SDK provides a consist API like https://webrtc.org/getting-started/remote-streams
  90. $('#rtc_media_player').prop('srcObject', sdk.stream);
  91. // Optional callback, SDK will add track to stream.
  92. // sdk.ontrack = function (event) { console.log('Got track', event); sdk.stream.addTrack(event.track); };
  93. var host = window.location.hostname
  94. // For example: webrtc://r.ossrs.net/live/livestream
  95. var url = "http://"+host+":1985/rtc/v1/whep/?app=live&stream=livestream"
  96. sdk.play(url).then(function(session){
  97. //$('#sessionid').html(session.sessionid);
  98. //$('#simulator-drop').attr('href', session.simulator + '?drop=1&username=' + session.sessionid);
  99. }).catch(function (reason) {
  100. sdk.close();
  101. $('#rtc_media_player').hide();
  102. console.error(reason);
  103. });
  104. };
  105. $('#rtc_media_player').hide();
  106. // var query = parse_query_string();
  107. // srs_init_whep("#txt_url", query);
  108. $("#btn_play").click(startPlay);
  109. // Never play util windows loaded @see https://github.com/ossrs/srs/issues/2732
  110. // if (query.autostart === 'true') {
  111. // $('#rtc_media_player').prop('muted', true);
  112. // console.warn('For autostart, we should mute it, see https://www.jianshu.com/p/c3c6944eed5a ' +
  113. // 'or https://developers.google.com/web/updates/2017/09/autoplay-policy-changes#audiovideo_elements');
  114. // window.addEventListener("load", function(){ startPlay(); });
  115. // }
  116. });
  117. </script>
  118. </html>