webrtcapi.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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?v=20260312"></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. // 页面加载时检查必要的 DOM 元素
  51. function checkDOMElements() {
  52. const audio = document.getElementById('audio');
  53. const video = document.getElementById('video');
  54. console.log('DOM elements check:');
  55. console.log('- Audio element:', audio ? '✅ found' : '❌ not found');
  56. console.log('- Video element:', video ? '✅ found' : '❌ not found');
  57. if (!audio) {
  58. console.error('Audio element is missing! Creating it dynamically...');
  59. const newAudio = document.createElement('audio');
  60. newAudio.id = 'audio';
  61. newAudio.autoplay = true;
  62. document.body.appendChild(newAudio);
  63. }
  64. if (!video) {
  65. console.error('Video element is missing! Creating it dynamically...');
  66. const newVideo = document.createElement('video');
  67. newVideo.id = 'video';
  68. newVideo.style.width = '600px';
  69. newVideo.autoplay = true;
  70. newVideo.playsInline = true;
  71. document.body.appendChild(newVideo);
  72. }
  73. }
  74. $(document).ready(function() {
  75. // 首先检查 DOM 元素
  76. checkDOMElements();
  77. // var host = window.location.hostname
  78. // var ws = new WebSocket("ws://"+host+":8000/humanecho");
  79. // //document.getElementsByTagName("video")[0].setAttribute("src", aa["video"]);
  80. // ws.onopen = function() {
  81. // console.log('Connected');
  82. // };
  83. // ws.onmessage = function(e) {
  84. // console.log('Received: ' + e.data);
  85. // data = e
  86. // var vid = JSON.parse(data.data);
  87. // console.log(typeof(vid),vid)
  88. // //document.getElementsByTagName("video")[0].setAttribute("src", vid["video"]);
  89. // };
  90. // ws.onclose = function(e) {
  91. // console.log('Closed');
  92. // };
  93. $('#echo-form').on('submit', function(e) {
  94. e.preventDefault();
  95. var message = $('#message').val();
  96. console.log('Sending: ' + message);
  97. console.log('sessionid: ',document.getElementById('sessionid').value);
  98. fetch('/human', {
  99. body: JSON.stringify({
  100. text: message,
  101. type: 'echo',
  102. interrupt: true,
  103. sessionid:parseInt(document.getElementById('sessionid').value),
  104. }),
  105. headers: {
  106. 'Content-Type': 'application/json'
  107. },
  108. method: 'POST'
  109. });
  110. //ws.send(message);
  111. $('#message').val('');
  112. });
  113. $('#btn_start_record').click(function() {
  114. // 开始录制
  115. console.log('Starting recording...');
  116. fetch('/record', {
  117. body: JSON.stringify({
  118. type: 'start_record',
  119. sessionid:parseInt(document.getElementById('sessionid').value),
  120. }),
  121. headers: {
  122. 'Content-Type': 'application/json'
  123. },
  124. method: 'POST'
  125. }).then(function(response) {
  126. if (response.ok) {
  127. console.log('Recording started.');
  128. $('#btn_start_record').prop('disabled', true);
  129. $('#btn_stop_record').prop('disabled', false);
  130. // $('#btn_download').prop('disabled', true);
  131. } else {
  132. console.error('Failed to start recording.');
  133. }
  134. }).catch(function(error) {
  135. console.error('Error:', error);
  136. });
  137. });
  138. $('#btn_stop_record').click(function() {
  139. // 结束录制
  140. console.log('Stopping recording...');
  141. fetch('/record', {
  142. body: JSON.stringify({
  143. type: 'end_record',
  144. sessionid:parseInt(document.getElementById('sessionid').value),
  145. }),
  146. headers: {
  147. 'Content-Type': 'application/json'
  148. },
  149. method: 'POST'
  150. }).then(function(response) {
  151. if (response.ok) {
  152. console.log('Recording stopped.');
  153. $('#btn_start_record').prop('disabled', false);
  154. $('#btn_stop_record').prop('disabled', true);
  155. // $('#btn_download').prop('disabled', false);
  156. } else {
  157. console.error('Failed to stop recording.');
  158. }
  159. }).catch(function(error) {
  160. console.error('Error:', error);
  161. });
  162. });
  163. // $('#btn_download').click(function() {
  164. // // 下载视频文件
  165. // console.log('Downloading video...');
  166. // fetch('/record_lasted.mp4', {
  167. // method: 'GET'
  168. // }).then(function(response) {
  169. // if (response.ok) {
  170. // return response.blob();
  171. // } else {
  172. // throw new Error('Failed to download the video.');
  173. // }
  174. // }).then(function(blob) {
  175. // // 创建一个 Blob 对象
  176. // const url = window.URL.createObjectURL(blob);
  177. // // 创建一个隐藏的可下载链接
  178. // const a = document.createElement('a');
  179. // a.style.display = 'none';
  180. // a.href = url;
  181. // a.download = 'record_lasted.mp4';
  182. // document.body.appendChild(a);
  183. // // 触发下载
  184. // a.click();
  185. // // 清理
  186. // window.URL.revokeObjectURL(url);
  187. // document.body.removeChild(a);
  188. // console.log('Video downloaded successfully.');
  189. // }).catch(function(error) {
  190. // console.error('Error:', error);
  191. // });
  192. // });
  193. });
  194. </script>
  195. </html>