webrtcapi.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. <div id="media">
  34. <h2>Media</h2>
  35. <audio id="audio" autoplay="true"></audio>
  36. <video id="video" style="width:600px;" autoplay="true" playsinline="true"></video>
  37. </div>
  38. <script src="client.js?v=20260312"></script>
  39. <script type="text/javascript" src="http://cdn.sockjs.org/sockjs-0.3.4.js"></script>
  40. <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
  41. </body>
  42. <script type="text/javascript" charset="utf-8">
  43. // 页面加载时检查必要的 DOM 元素
  44. function checkDOMElements() {
  45. const audio = document.getElementById('audio');
  46. const video = document.getElementById('video');
  47. console.log('DOM elements check:');
  48. console.log('- Audio element:', audio ? '✅ found' : '❌ not found');
  49. console.log('- Video element:', video ? '✅ found' : '❌ not found');
  50. if (!audio) {
  51. console.error('Audio element is missing! Creating it dynamically...');
  52. const newAudio = document.createElement('audio');
  53. newAudio.id = 'audio';
  54. newAudio.autoplay = true;
  55. document.body.appendChild(newAudio);
  56. }
  57. if (!video) {
  58. console.error('Video element is missing! Creating it dynamically...');
  59. const newVideo = document.createElement('video');
  60. newVideo.id = 'video';
  61. newVideo.style.width = '600px';
  62. newVideo.autoplay = true;
  63. newVideo.playsInline = true;
  64. document.body.appendChild(newVideo);
  65. }
  66. }
  67. $(document).ready(function() {
  68. // 首先检查 DOM 元素
  69. checkDOMElements();
  70. $('#btn_start_record').click(function() {
  71. // 开始录制
  72. console.log('Starting recording...');
  73. fetch('/record', {
  74. body: JSON.stringify({
  75. type: 'start_record',
  76. sessionid:parseInt(document.getElementById('sessionid').value),
  77. }),
  78. headers: {
  79. 'Content-Type': 'application/json'
  80. },
  81. method: 'POST'
  82. }).then(function(response) {
  83. if (response.ok) {
  84. console.log('Recording started.');
  85. $('#btn_start_record').prop('disabled', true);
  86. $('#btn_stop_record').prop('disabled', false);
  87. // $('#btn_download').prop('disabled', true);
  88. } else {
  89. console.error('Failed to start recording.');
  90. }
  91. }).catch(function(error) {
  92. console.error('Error:', error);
  93. });
  94. });
  95. $('#btn_stop_record').click(function() {
  96. // 结束录制
  97. console.log('Stopping recording...');
  98. fetch('/record', {
  99. body: JSON.stringify({
  100. type: 'end_record',
  101. sessionid:parseInt(document.getElementById('sessionid').value),
  102. }),
  103. headers: {
  104. 'Content-Type': 'application/json'
  105. },
  106. method: 'POST'
  107. }).then(function(response) {
  108. if (response.ok) {
  109. console.log('Recording stopped.');
  110. $('#btn_start_record').prop('disabled', false);
  111. $('#btn_stop_record').prop('disabled', true);
  112. // $('#btn_download').prop('disabled', false);
  113. } else {
  114. console.error('Failed to stop recording.');
  115. }
  116. }).catch(function(error) {
  117. console.error('Error:', error);
  118. });
  119. });
  120. // $('#btn_download').click(function() {
  121. // // 下载视频文件
  122. // console.log('Downloading video...');
  123. // fetch('/record_lasted.mp4', {
  124. // method: 'GET'
  125. // }).then(function(response) {
  126. // if (response.ok) {
  127. // return response.blob();
  128. // } else {
  129. // throw new Error('Failed to download the video.');
  130. // }
  131. // }).then(function(blob) {
  132. // // 创建一个 Blob 对象
  133. // const url = window.URL.createObjectURL(blob);
  134. // // 创建一个隐藏的可下载链接
  135. // const a = document.createElement('a');
  136. // a.style.display = 'none';
  137. // a.href = url;
  138. // a.download = 'record_lasted.mp4';
  139. // document.body.appendChild(a);
  140. // // 触发下载
  141. // a.click();
  142. // // 清理
  143. // window.URL.revokeObjectURL(url);
  144. // document.body.removeChild(a);
  145. // console.log('Video downloaded successfully.');
  146. // }).catch(function(error) {
  147. // console.error('Error:', error);
  148. // });
  149. // });
  150. });
  151. </script>
  152. </html>