webrtcchat.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 id="start" onclick="start()">Start</button>
  28. <button id="stop" style="display: none" onclick="stop()">Stop</button>
  29. <button class="btn btn-primary" id="btn_start_record">Start Recording</button>
  30. <button class="btn btn-primary" id="btn_stop_record" disabled>Stop Recording</button>
  31. <!-- <button class="btn btn-primary" id="btn_download">Download Video</button> -->
  32. <input type="hidden" id="sessionid" value="0">
  33. <form class="form-inline" id="echo-form">
  34. <div class="form-group">
  35. <p>input text</p>
  36. <textarea cols="2" rows="3" style="width:600px;height:50px;" class="form-control" id="message">test</textarea>
  37. </div>
  38. <button type="submit" class="btn btn-default">Send</button>
  39. </form>
  40. <div id="media">
  41. <h2>Media</h2>
  42. <audio id="audio" autoplay="true"></audio>
  43. <video id="video" style="width:600px;" autoplay="true" playsinline="true"></video>
  44. </div>
  45. <script src="client.js"></script>
  46. <script type="text/javascript" src="http://cdn.sockjs.org/sockjs-0.3.4.js"></script>
  47. <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
  48. </body>
  49. <script type="text/javascript" charset="utf-8">
  50. $(document).ready(function() {
  51. // var host = window.location.hostname
  52. // var ws = new WebSocket("ws://"+host+":8000/humanecho");
  53. // //document.getElementsByTagName("video")[0].setAttribute("src", aa["video"]);
  54. // ws.onopen = function() {
  55. // console.log('Connected');
  56. // };
  57. // ws.onmessage = function(e) {
  58. // console.log('Received: ' + e.data);
  59. // data = e
  60. // var vid = JSON.parse(data.data);
  61. // console.log(typeof(vid),vid)
  62. // //document.getElementsByTagName("video")[0].setAttribute("src", vid["video"]);
  63. // };
  64. // ws.onclose = function(e) {
  65. // console.log('Closed');
  66. // };
  67. $('#echo-form').on('submit', function(e) {
  68. e.preventDefault();
  69. var message = $('#message').val();
  70. console.log('Sending: ' + message);
  71. console.log('sessionid: ',document.getElementById('sessionid').value);
  72. fetch('/human', {
  73. body: JSON.stringify({
  74. text: message,
  75. type: 'chat',
  76. interrupt: true,
  77. sessionid:parseInt(document.getElementById('sessionid').value),
  78. }),
  79. headers: {
  80. 'Content-Type': 'application/json'
  81. },
  82. method: 'POST'
  83. });
  84. //ws.send(message);
  85. $('#message').val('');
  86. });
  87. $('#btn_start_record').click(function() {
  88. // 开始录制
  89. console.log('Starting recording...');
  90. fetch('/record', {
  91. body: JSON.stringify({
  92. type: 'start_record',
  93. }),
  94. headers: {
  95. 'Content-Type': 'application/json'
  96. },
  97. method: 'POST'
  98. }).then(function(response) {
  99. if (response.ok) {
  100. console.log('Recording started.');
  101. $('#btn_start_record').prop('disabled', true);
  102. $('#btn_stop_record').prop('disabled', false);
  103. // $('#btn_download').prop('disabled', true);
  104. } else {
  105. console.error('Failed to start recording.');
  106. }
  107. }).catch(function(error) {
  108. console.error('Error:', error);
  109. });
  110. });
  111. $('#btn_stop_record').click(function() {
  112. // 结束录制
  113. console.log('Stopping recording...');
  114. fetch('/record', {
  115. body: JSON.stringify({
  116. type: 'end_record',
  117. }),
  118. headers: {
  119. 'Content-Type': 'application/json'
  120. },
  121. method: 'POST'
  122. }).then(function(response) {
  123. if (response.ok) {
  124. console.log('Recording stopped.');
  125. $('#btn_start_record').prop('disabled', false);
  126. $('#btn_stop_record').prop('disabled', true);
  127. // $('#btn_download').prop('disabled', false);
  128. } else {
  129. console.error('Failed to stop recording.');
  130. }
  131. }).catch(function(error) {
  132. console.error('Error:', error);
  133. });
  134. });
  135. // $('#btn_download').click(function() {
  136. // // 下载视频文件
  137. // console.log('Downloading video...');
  138. // fetch('/record_lasted.mp4', {
  139. // method: 'GET'
  140. // }).then(function(response) {
  141. // if (response.ok) {
  142. // return response.blob();
  143. // } else {
  144. // throw new Error('Failed to download the video.');
  145. // }
  146. // }).then(function(blob) {
  147. // // 创建一个 Blob 对象
  148. // const url = window.URL.createObjectURL(blob);
  149. // // 创建一个隐藏的可下载链接
  150. // const a = document.createElement('a');
  151. // a.style.display = 'none';
  152. // a.href = url;
  153. // a.download = 'record_lasted.mp4';
  154. // document.body.appendChild(a);
  155. // // 触发下载
  156. // a.click();
  157. // // 清理
  158. // window.URL.revokeObjectURL(url);
  159. // document.body.removeChild(a);
  160. // console.log('Video downloaded successfully.');
  161. // }).catch(function(error) {
  162. // console.error('Error:', error);
  163. // });
  164. // });
  165. });
  166. </script>
  167. </html>