main.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. var videoPreview;
  2. var mediaRecorder;
  3. var recordedChunks = [];
  4. //CHARGEMENT DE LA PAGE
  5. function init_plugin_screenshare(){
  6. switch($.urlParam('page')){
  7. default:
  8. break;
  9. }
  10. videoPreview = $("#screenshare-preview").get(0);
  11. $('#screen-videos').sortable_table({
  12. onSort : screenshare_screen_video_search
  13. });
  14. }
  15. //Enregistrement des configurations
  16. function screenshare_setting_save(){
  17. $.action({
  18. action : 'screenshare_setting_save',
  19. fields : $('#screenshare-setting-form').toJson()
  20. },function(){ $.message('info','Configuration enregistrée'); });
  21. }
  22. /** SCREENVIDEO **/
  23. function screenshare_screen_mouse_toggle(){
  24. var mouseButton = $('.mouse-button');
  25. if(mouseButton.attr('data-mode') == 'active'){
  26. mouseButton.attr('data-mode','inactive');
  27. }else{
  28. mouseButton.attr('data-mode','active');
  29. }
  30. }
  31. function screenshare_screen_sound_toggle(){
  32. var soundButton = $('.sound-button');
  33. if(soundButton.attr('data-mode') == 'active'){
  34. soundButton.attr('data-mode','inactive');
  35. }else{
  36. soundButton.attr('data-mode','active');
  37. }
  38. }
  39. async function screenshare_screen_video_record() {
  40. var button = $('.record-button');
  41. var mouseButton = $('.mouse-button');
  42. var soundButton = $('.sound-button');
  43. if(button.attr('data-mode') == 'recording')
  44. {
  45. //arret de capture
  46. button.attr('data-mode','');
  47. button.find('i').removeClass('far fa-circle').addClass('fas fa-circle');
  48. mediaRecorder.stop();
  49. var tracks = videoPreview.srcObject.getTracks();
  50. tracks.forEach(track => track.stop());
  51. videoPreview.srcObject = null;
  52. }else{
  53. //démarrage de capture
  54. button.attr('data-mode','recording');
  55. button.find('i').removeClass('fas fa-circle').addClass('far fa-circle');
  56. try {
  57. var options={
  58. video: {
  59. cursor: mouseButton.attr('data-mode') == 'active' ? "always" : "never"
  60. },
  61. audio: soundButton.attr('data-mode') == 'active'
  62. };
  63. stream = await navigator.mediaDevices.getDisplayMedia(options);
  64. mediaRecorder = new MediaRecorder(stream, { mimeType: "video/webm; codecs=vp9" });
  65. mediaRecorder.ondataavailable = screenshare_screen_video_recording;
  66. stream.getVideoTracks()[0].onended = function(){ screenshare_screen_video_record(); };
  67. mediaRecorder.start();
  68. videoPreview.srcObject =stream;
  69. } catch(error) {
  70. console.log("'"+error+"'");
  71. button.attr('data-mode','');
  72. button.find('i').removeClass('far fa-circle').addClass('fas fa-circle');
  73. if(""+error.toString()!='NotAllowedError: Permission denied')
  74. $.message('error','Erreur lors de l\'enregistrement '+JSON.stringify(error));
  75. }
  76. }
  77. }
  78. function screenshare_screen_video_progress(mode){
  79. if(mode){
  80. $('.screenshare-progress').removeClass('hidden');
  81. }else{
  82. $('.screenshare-progress').addClass('hidden').find('>span').text('');
  83. $('.screenshare-progress').find('.progress progress-bar').css('width','0%');
  84. }
  85. }
  86. function screenshare_screen_video_recording(event) {
  87. screenshare_screen_video_progress(true);
  88. if (event.data.size > 0) {
  89. recordedChunks.push(event.data);
  90. var blob = new Blob(recordedChunks, {
  91. type: "video/webm"
  92. });
  93. var url = URL.createObjectURL(blob);
  94. var filename = "capture.webm";
  95. var data = new FormData();
  96. data.append('name', filename);
  97. data.append('tmp_name', url);
  98. data.append('data', blob);
  99. //todo $.action
  100. var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new activeXObject("Microsoft.XMLHTTP");
  101. xhr.open( 'post', 'action.php?action=screenshare_screen_video_save', true);
  102. var progress = $('#screenshare-progress .progress .progress-bar');
  103. var progressText = $('.screenshare-progress').find('>span');
  104. xhr.upload.addEventListener("progress", function(evt){
  105. if (evt.lengthComputable) {
  106. var percentComplete = (evt.loaded / evt.total) * 100;
  107. percentComplete = Math.round(percentComplete * 100) / 100;
  108. progress.css('width',percentComplete+'%');
  109. progressText.text(percentComplete+'%');
  110. }
  111. }, false);
  112. xhr.onreadystatechange = function () {
  113. if(xhr.readyState === 4 && xhr.status === 200) {
  114. screenshare_screen_video_progress(false);
  115. var response = xhr.responseText;
  116. screenshare_screen_video_search();
  117. }
  118. };
  119. xhr.send(data);
  120. }
  121. }
  122. //Récuperation d'une liste de screenvideo dans le tableau #screenvideos
  123. function screenshare_screen_video_search(callback){
  124. var box = new FilterBox('#filters');
  125. $('#screen-videos').fill({
  126. action:'screenshare_screen_video_search',
  127. filters : box.filters(),
  128. sort : $('#screen-videos').sortable_table('get')
  129. },function(response){
  130. $('.results-count span').text(response.pagination.total);
  131. init_components();
  132. if(callback!=null) callback();
  133. });
  134. }
  135. //Ajout ou modification d'élément screenvideo
  136. function screenshare_screen_video_save(element){
  137. var li = $(element).closest('li');
  138. data.action = 'screenshare_screen_video_save';
  139. data.label = li.find('.label').val();
  140. data.expireHour = li.find('.expireHour').val();
  141. data.expireDate = li.find('.expireDate').val();
  142. data.id = li.attr('data-id');
  143. $.action(data,function(r){
  144. $.message('success','Enregistré');
  145. });
  146. }
  147. function screenshare_screen_video_preview(element){
  148. var line = $(element).closest('li');
  149. $("#screenshare-preview").attr('src',"action.php?action=screenshare_screen_video_preview&id="+line.attr('data-id'));
  150. }
  151. //Suppression d'élement screenvideo
  152. function screenshare_screen_video_delete(element){
  153. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  154. var line = $(element).closest('li');
  155. $.action({
  156. action : 'screenshare_screen_video_delete',
  157. id : line.attr('data-id')
  158. },function(r){
  159. line.remove();
  160. $.message('info','Élement supprimé');
  161. });
  162. }