index.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="apple-mobile-web-app-capable" content="yes" />
  5. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
  6. <meta charset="UTF-8">
  7. <meta name="theme-color" content="#4b75ff">
  8. <link rel="stylesheet" href="../script/semantic/semantic.min.css">
  9. <script src="../script/jquery.min.js"></script>
  10. <script src="../script/ao_module.js"></script>
  11. <script src="../script/semantic/semantic.min.js"></script>
  12. <script src="js/recorder.js"></script>
  13. <script src="js/RecordRTC.js"></script>
  14. <title>Recorder</title>
  15. <style>
  16. body{
  17. background-color:white;
  18. }
  19. .statusbar{
  20. position: fixed;
  21. bottom: 0px;
  22. left:0px;
  23. padding: 5px;
  24. padding-left: 12px;
  25. }
  26. .uitab{
  27. border: 0px solid transparent !important;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div class="ui top attached small tabular menu">
  33. <a class="active item tab" onclick="opentab('audio', this);">
  34. <i class="ui red microphone icon"></i>
  35. </a>
  36. <a class="item tab" onclick="opentab('video', this);"">
  37. <i class="ui red video icon"></i>
  38. </a>
  39. <a class="item tab" onclick="opentab('setting', this);">
  40. <i class="ui setting icon"></i>
  41. </a>
  42. </div>
  43. <div id="audioTab" class="ui bottom attached basic segment uitab" style="margin-bottom: 0px;">
  44. <button id="record" class="ui red button" onclick="startRecording();">
  45. <i class="ui circle icon"></i> Record
  46. </button>
  47. <button id="stop" class="ui disabled button" onclick="stopRecording();">
  48. <i class="ui stop icon"></i> Stop
  49. </button>
  50. <button class="ui button" onclick="openStorageFolder();">
  51. <i class="ui folder open icon"></i> Open Folder
  52. </button>
  53. </div>
  54. <div id="videoTab" class="ui bottom attached basic segment uitab" style="display:none; margin-bottom: 0px;">
  55. <button id="capture" class="ui red button" onclick="startVideoCapture();">
  56. <i class="ui circle icon"></i> Capture
  57. </button>
  58. <button id="vidstop" class="ui disabled button" onclick="stopVideoCapture()">
  59. <i class="ui stop icon"></i> Stop
  60. </button>
  61. <button class="ui button" onclick="openStorageFolder();">
  62. <i class="ui folder open icon"></i> Open Folder
  63. </button>
  64. <br>
  65. <small>Notes: This function require websocket upload to be enabled.</small>
  66. <br>
  67. <div class="ui selection fluid dropdown" style="margin-top: 0.4em;">
  68. <input type="hidden" name="format" id="captureFormat" value="video/webm">
  69. <i class="dropdown icon"></i>
  70. <div class="default text">Encoding Codec</div>
  71. <div class="menu">
  72. <div class="item" data-value="video/webm">webm (VP8/9)</div>
  73. <div class="item" data-value="video/mp4">mp4 (H264)</div>
  74. </div>
  75. </div>
  76. </div>
  77. <div id="settingTab" class="ui bottom attached basic segment uitab" style="display:none; margin-bottom: 0px;">
  78. <p style="margin-bottom: 4px;">Select Output Folder</p>
  79. <div class="ui icon fluid input">
  80. <input id="saveFolder" type="text" placeholder="user:/Document/Capture/" value="user:/Document/Capture/">
  81. <i class="folder open link icon" onclick="selectSaveFolder();"></i>
  82. </div>
  83. </div>
  84. <div class="statusbar">
  85. Status: <span id="status">Ready</span>
  86. </div>
  87. </div>
  88. <script>
  89. //Define the basic context for the recorder
  90. var audio_context;
  91. var recorder;
  92. var recordTimer;
  93. var recordCounter = 0;
  94. //On window load, adjust the floatWindow settings
  95. //Disable window resizing
  96. ao_module_setFixedWindowSize();
  97. $(".dropdown").dropdown();
  98. //Check if there are previous saved path. If yes, update save target
  99. var previousSavePath = localStorage.getItem("recorder-savepath");
  100. if (previousSavePath != null){
  101. $("#saveFolder").val(previousSavePath);
  102. }
  103. //Create save dir if not exists
  104. createSaveDirIfNotExists();
  105. function opentab(tabName, object){
  106. $(".uitab").hide();
  107. $(".active.tab").removeClass("active");
  108. if (tabName == "audio"){
  109. $("#audioTab").show();
  110. }else if (tabName == "video"){
  111. $("#videoTab").show();
  112. }else if (tabName == "setting"){
  113. $("#settingTab").show();
  114. }
  115. $(object).addClass("active");
  116. }
  117. function createSaveDirIfNotExists(){
  118. //Check if the folder exists. If not, create it
  119. ao_module_agirun("Recorder/backend/createIfNotExists.js", {
  120. savedir: $("#saveFolder").val()
  121. }, function(data){
  122. console.log(data);
  123. });
  124. }
  125. //Handle recording stuffs
  126. function startRecording(){
  127. recorder && recorder.record();
  128. $("#record").addClass("disabled");
  129. $("#stop").removeClass("disabled");
  130. updateStatus("Recording Started");
  131. recordCounter = 0;
  132. recordTimer = setInterval(function(){
  133. recordCounter++;
  134. updateStatus("Recording T: " + secondsToHms(recordCounter))
  135. }, 1000);
  136. }
  137. function stopRecording(){
  138. recorder && recorder.stop();
  139. $("#record").removeClass("disabled");
  140. $("#stop").addClass("disabled");
  141. clearInterval(recordTimer);
  142. updateStatus("Recording Stopped")
  143. renderWAV(function(blob){
  144. //Process the blob data here
  145. updateStatus("Saving")
  146. //Convert the blob to a file object
  147. var fileName = new Date().toLocaleDateString().split("/").join("-") + "_" + new Date().toLocaleTimeString(undefined, {hour12: false}).split(":").join("-") + ".wav"
  148. var fileObjcet = ao_module_utils.blobToFile(blob, fileName, blob.type)
  149. console.log(fileObjcet);
  150. //Upload the file to server
  151. ao_module_uploadFile(fileObjcet, $("#saveFolder").val(), function(resp){
  152. resp = JSON.parse(resp);
  153. if (resp.error !== undefined){
  154. updateStatus("Failed: " + resp.error)
  155. }else{
  156. updateStatus("Saved as " + fileName)
  157. }
  158. console.log(resp);
  159. }, undefined, function(){
  160. updateStatus("File Saving Failed")
  161. });
  162. });
  163. }
  164. function renderWAV(callback){
  165. recorder && recorder.exportWAV(function(blob) {
  166. callback(blob);
  167. });
  168. }
  169. function secondsToHms(d) {
  170. d = Number(d);
  171. var h = Math.floor(d / 3600);
  172. var m = Math.floor(d % 3600 / 60);
  173. var s = Math.floor(d % 3600 % 60);
  174. return (h+"").padStart(2, '0') + ":" + (m+"").padStart(2, '0') + ":" + (s+"").padStart(2, '0');
  175. }
  176. function openStorageFolder(){
  177. var targetFolder = $("#saveFolder").val();
  178. ao_module_openPath(targetFolder);
  179. }
  180. function selectSaveFolder(){
  181. var defaultFolder = $("#saveFolder").val();
  182. if (defaultFolder == ""){
  183. defaultFolder = "user:/"
  184. }
  185. ao_module_openFileSelector(fileSelected, defaultFolder, "folder",false);
  186. }
  187. function fileSelected(filedata){
  188. for (var i=0; i < filedata.length; i++){
  189. var filename = filedata[i].filename;
  190. var filepath = filedata[i].filepath;
  191. $("#saveFolder").val(filepath);
  192. localStorage.setItem("recorder-savepath",filepath);
  193. }
  194. createSaveDirIfNotExists();
  195. }
  196. function updateStatus(statusText){
  197. ao_module_setWindowTitle("Recorder - " + statusText);
  198. $("#status").text(statusText);
  199. }
  200. function startUserMedia(stream) {
  201. var input = audio_context.createMediaStreamSource(stream);
  202. recorder = new Recorder(input);
  203. updateStatus("Ready");
  204. console.log('Recorder initialised.');
  205. }
  206. //Try to setup Audio context
  207. window.onload = function init() {
  208. updateStatus("Starting")
  209. try {
  210. // webkit shim
  211. window.AudioContext = window.AudioContext || window.webkitAudioContext;
  212. navigator.getUserMedia = (navigator.getUserMedia ||
  213. navigator.webkitGetUserMedia ||
  214. navigator.mozGetUserMedia ||
  215. navigator.msGetUserMedia);
  216. window.URL = window.URL || window.webkitURL;
  217. audio_context = new AudioContext;
  218. console.log('Audio context set up.');
  219. console.log('navigator.getUserMedia ' + (navigator.getUserMedia ? 'available.' : 'not present!'));
  220. } catch (e) {
  221. console.log(e);
  222. alert('No web audio support in this browser!');
  223. }
  224. try{
  225. navigator.getUserMedia({audio: true}, startUserMedia, function(e) {
  226. console.log('No live audio input: ' + e);
  227. });
  228. }catch(ex){
  229. $("#status").text("Audio API not usable due to invalid TLS config")
  230. ao_module_setWindowTitle("Recorder - Startup Failed");
  231. }
  232. };
  233. function downloadBlob(blob, name = 'file.txt') {
  234. if (
  235. window.navigator &&
  236. window.navigator.msSaveOrOpenBlob
  237. ) return window.navigator.msSaveOrOpenBlob(blob);
  238. // For other browsers:
  239. // Create a link pointing to the ObjectURL containing the blob.
  240. const data = window.URL.createObjectURL(blob);
  241. const link = document.createElement('a');
  242. link.href = data;
  243. link.download = name;
  244. // this is necessary as link.click() does not work on the latest firefox
  245. link.dispatchEvent(
  246. new MouseEvent('click', {
  247. bubbles: true,
  248. cancelable: true,
  249. view: window
  250. })
  251. );
  252. setTimeout(() => {
  253. // For Firefox it is necessary to delay revoking the ObjectURL
  254. window.URL.revokeObjectURL(data);
  255. link.remove();
  256. }, 100);
  257. }
  258. /*
  259. Screen recording code
  260. */
  261. var websocket;
  262. var videCaptureRecorder;
  263. function getWebsocketUploadEndpoint(){
  264. return getWebSocketEndpoint() + "/system/file_system/lowmemUpload?filename=" + encodeURIComponent(getVideoFilename()) + "&path=" + encodeURIComponent($("#saveFolder").val());
  265. }
  266. function getVideoFilename(){
  267. var fileName = new Date().toLocaleDateString().split("/").join("-") + "_" + new Date().toLocaleTimeString(undefined, {hour12: false}).split(":").join("-") + ".webm"
  268. return fileName;
  269. }
  270. function getWebSocketEndpoint(){
  271. let protocol = "wss://";
  272. if (location.protocol !== 'https:') {
  273. protocol = "ws://";
  274. }
  275. let port = window.location.port;
  276. if (window.location.port == ""){
  277. if (location.protocol !== 'https:') {
  278. port = "80";
  279. }else{
  280. port = "443";
  281. }
  282. }
  283. let wsept = (protocol + window.location.hostname + ":" + port);
  284. return wsept;
  285. }
  286. function captureCamera(callback) {
  287. navigator.mediaDevices.getDisplayMedia({
  288. displaySurface: 'monitor',
  289. logicalSurface: true,
  290. cursor: 'always'
  291. }).then(function(camera) {
  292. callback(camera);
  293. }).catch(function(error) {
  294. updateStatus('Failed to start screen capture');
  295. console.error(error);
  296. });
  297. }
  298. function stopRecordingCallback() {
  299. videCaptureRecorder.camera.stop();
  300. videCaptureRecorder = null;
  301. }
  302. function startVideoCapture() {
  303. this.disabled = true;
  304. websocket = new WebSocket(getWebsocketUploadEndpoint());
  305. //websocket.binaryType = 'blob';
  306. captureCamera(function(camera) {
  307. videCaptureRecorder = RecordRTC(camera, {
  308. recorderType: MediaStreamRecorder,
  309. mimeType: 'video/webm',
  310. timeSlice: 100,
  311. getNativeBlob: true,
  312. ondataavailable: function(blob) {
  313. websocket.send(blob);
  314. }
  315. });
  316. videCaptureRecorder.startRecording();
  317. // release camera on stopRecording
  318. videCaptureRecorder.camera = camera;
  319. recordTimer = setInterval(function(){
  320. recordCounter++;
  321. updateStatus("Capturing T: " + secondsToHms(recordCounter))
  322. }, 1000);
  323. $("#capture").addClass("disabled");
  324. $("#vidstop").removeClass("disabled");
  325. });
  326. }
  327. function stopVideoCapture(){
  328. videCaptureRecorder.stopRecording(stopRecordingCallback);
  329. websocket.send("done");
  330. clearInterval(recordTimer);
  331. updateStatus("Capturing Stopped")
  332. $("#vidstop").addClass("disabled");
  333. $("#capture").removeClass("disabled");
  334. }
  335. </script>
  336. </body>
  337. </html>