You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AudioLevels.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* global APP, interfaceConfig, $ */
  2. /* jshint -W101 */
  3. import CanvasUtil from './CanvasUtils';
  4. import BottomToolbar from '../toolbars/BottomToolbar';
  5. const LOCAL_LEVEL = 'local';
  6. let ASDrawContext = null;
  7. let audioLevelCanvasCache = {};
  8. function initDominantSpeakerAudioLevels() {
  9. let ASRadius = interfaceConfig.DOMINANT_SPEAKER_AVATAR_SIZE / 2;
  10. let ASCenter = (interfaceConfig.DOMINANT_SPEAKER_AVATAR_SIZE + ASRadius) / 2;
  11. // Draw a circle.
  12. ASDrawContext.arc(ASCenter, ASCenter, ASRadius, 0, 2 * Math.PI);
  13. // Add a shadow around the circle
  14. ASDrawContext.shadowColor = interfaceConfig.SHADOW_COLOR;
  15. ASDrawContext.shadowOffsetX = 0;
  16. ASDrawContext.shadowOffsetY = 0;
  17. }
  18. /**
  19. * Resizes the given audio level canvas to match the given thumbnail size.
  20. */
  21. function resizeAudioLevelCanvas(audioLevelCanvas, thumbnailWidth, thumbnailHeight) {
  22. audioLevelCanvas.width = thumbnailWidth + interfaceConfig.CANVAS_EXTRA;
  23. audioLevelCanvas.height = thumbnailHeight + interfaceConfig.CANVAS_EXTRA;
  24. }
  25. /**
  26. * Draws the audio level canvas into the cached canvas object.
  27. *
  28. * @param id of the user for whom we draw the audio level
  29. * @param audioLevel the newAudio level to render
  30. */
  31. function drawAudioLevelCanvas(id, audioLevel) {
  32. if (!audioLevelCanvasCache[id]) {
  33. let videoSpanId = getVideoSpanId(id);
  34. let audioLevelCanvasOrig = $(`#${videoSpanId}>canvas`).get(0);
  35. /*
  36. * FIXME Testing has shown that audioLevelCanvasOrig may not exist.
  37. * In such a case, the method CanvasUtil.cloneCanvas may throw an
  38. * error. Since audio levels are frequently updated, the errors have
  39. * been observed to pile into the console, strain the CPU.
  40. */
  41. if (audioLevelCanvasOrig) {
  42. audioLevelCanvasCache[id] = CanvasUtil.cloneCanvas(audioLevelCanvasOrig);
  43. }
  44. }
  45. let canvas = audioLevelCanvasCache[id];
  46. if (!canvas) {
  47. return;
  48. }
  49. let drawContext = canvas.getContext('2d');
  50. drawContext.clearRect(0, 0, canvas.width, canvas.height);
  51. let shadowLevel = getShadowLevel(audioLevel);
  52. if (shadowLevel > 0) {
  53. // drawContext, x, y, w, h, r, shadowColor, shadowLevel
  54. CanvasUtil.drawRoundRectGlow(drawContext,
  55. interfaceConfig.CANVAS_EXTRA / 2, interfaceConfig.CANVAS_EXTRA / 2,
  56. canvas.width - interfaceConfig.CANVAS_EXTRA,
  57. canvas.height - interfaceConfig.CANVAS_EXTRA,
  58. interfaceConfig.CANVAS_RADIUS,
  59. interfaceConfig.SHADOW_COLOR,
  60. shadowLevel);
  61. }
  62. }
  63. /**
  64. * Returns the shadow/glow level for the given audio level.
  65. *
  66. * @param audioLevel the audio level from which we determine the shadow
  67. * level
  68. */
  69. function getShadowLevel (audioLevel) {
  70. let shadowLevel = 0;
  71. if (audioLevel <= 0.3) {
  72. shadowLevel = Math.round(interfaceConfig.CANVAS_EXTRA/2*(audioLevel/0.3));
  73. } else if (audioLevel <= 0.6) {
  74. shadowLevel = Math.round(interfaceConfig.CANVAS_EXTRA/2*((audioLevel - 0.3) / 0.3));
  75. } else {
  76. shadowLevel = Math.round(interfaceConfig.CANVAS_EXTRA/2*((audioLevel - 0.6) / 0.4));
  77. }
  78. return shadowLevel;
  79. }
  80. /**
  81. * Returns the video span id corresponding to the given user id
  82. */
  83. function getVideoSpanId(id) {
  84. let videoSpanId = null;
  85. if (id === LOCAL_LEVEL || APP.conference.isLocalId(id)) {
  86. videoSpanId = 'localVideoContainer';
  87. } else {
  88. videoSpanId = `participant_${id}`;
  89. }
  90. return videoSpanId;
  91. }
  92. /**
  93. * The audio Levels plugin.
  94. */
  95. const AudioLevels = {
  96. init () {
  97. ASDrawContext = $('#dominantSpeakerAudioLevel')[0].getContext('2d');
  98. initDominantSpeakerAudioLevels();
  99. },
  100. /**
  101. * Updates the audio level canvas for the given id. If the canvas
  102. * didn't exist we create it.
  103. */
  104. updateAudioLevelCanvas (id, thumbWidth, thumbHeight) {
  105. let videoSpanId = 'localVideoContainer';
  106. if (id) {
  107. videoSpanId = `participant_${id}`;
  108. }
  109. let videoSpan = document.getElementById(videoSpanId);
  110. if (!videoSpan) {
  111. if (id) {
  112. console.error("No video element for id", id);
  113. } else {
  114. console.error("No video element for local video.");
  115. }
  116. return;
  117. }
  118. let audioLevelCanvas = $(`#${videoSpanId}>canvas`);
  119. if (!audioLevelCanvas || audioLevelCanvas.length === 0) {
  120. audioLevelCanvas = document.createElement('canvas');
  121. audioLevelCanvas.className = "audiolevel";
  122. audioLevelCanvas.style.bottom = `-${interfaceConfig.CANVAS_EXTRA/2}px`;
  123. audioLevelCanvas.style.left = `-${interfaceConfig.CANVAS_EXTRA/2}px`;
  124. resizeAudioLevelCanvas(audioLevelCanvas, thumbWidth, thumbHeight);
  125. videoSpan.appendChild(audioLevelCanvas);
  126. } else {
  127. audioLevelCanvas = audioLevelCanvas.get(0);
  128. resizeAudioLevelCanvas(audioLevelCanvas, thumbWidth, thumbHeight);
  129. }
  130. },
  131. /**
  132. * Updates the audio level UI for the given id.
  133. *
  134. * @param id id of the user for whom we draw the audio level
  135. * @param audioLevel the newAudio level to render
  136. */
  137. updateAudioLevel (id, audioLevel, largeVideoId) {
  138. drawAudioLevelCanvas(id, audioLevel);
  139. let videoSpanId = getVideoSpanId(id);
  140. let audioLevelCanvas = $(`#${videoSpanId}>canvas`).get(0);
  141. if (!audioLevelCanvas) {
  142. return;
  143. }
  144. let drawContext = audioLevelCanvas.getContext('2d');
  145. let canvasCache = audioLevelCanvasCache[id];
  146. drawContext.clearRect(
  147. 0, 0, audioLevelCanvas.width, audioLevelCanvas.height
  148. );
  149. drawContext.drawImage(canvasCache, 0, 0);
  150. if (id === LOCAL_LEVEL) {
  151. id = APP.conference.localId;
  152. if (!id) {
  153. return;
  154. }
  155. }
  156. if(id === largeVideoId) {
  157. window.requestAnimationFrame(function () {
  158. AudioLevels.updateDominantSpeakerAudioLevel(audioLevel);
  159. });
  160. }
  161. },
  162. updateDominantSpeakerAudioLevel (audioLevel) {
  163. if($("#domiantSpeaker").css("visibility") == "hidden" || ASDrawContext === null) {
  164. return;
  165. }
  166. ASDrawContext.clearRect(0, 0, 300, 300);
  167. if (!audioLevel) {
  168. return;
  169. }
  170. ASDrawContext.shadowBlur = getShadowLevel(audioLevel);
  171. // Fill the shape.
  172. ASDrawContext.fill();
  173. },
  174. updateCanvasSize (thumbWidth, thumbHeight) {
  175. let canvasWidth = thumbWidth + interfaceConfig.CANVAS_EXTRA;
  176. let canvasHeight = thumbHeight + interfaceConfig.CANVAS_EXTRA;
  177. BottomToolbar.getThumbs().children('canvas').width(canvasWidth).height(canvasHeight);
  178. Object.keys(audioLevelCanvasCache).forEach(function (id) {
  179. audioLevelCanvasCache[id].width = canvasWidth;
  180. audioLevelCanvasCache[id].height = canvasHeight;
  181. });
  182. }
  183. };
  184. export default AudioLevels;