index.html 16 KB

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