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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* global APP, interfaceConfig, $ */
  2. /* jshint -W101 */
  3. import CanvasUtil from './CanvasUtils';
  4. import FilmStrip from '../videolayout/FilmStrip';
  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]
  43. = CanvasUtil.cloneCanvas(audioLevelCanvasOrig);
  44. }
  45. }
  46. let canvas = audioLevelCanvasCache[id];
  47. if (!canvas) {
  48. return;
  49. }
  50. let drawContext = canvas.getContext('2d');
  51. drawContext.clearRect(0, 0, canvas.width, canvas.height);
  52. let shadowLevel = getShadowLevel(audioLevel);
  53. if (shadowLevel > 0) {
  54. // drawContext, x, y, w, h, r, shadowColor, shadowLevel
  55. CanvasUtil.drawRoundRectGlow(
  56. drawContext,
  57. interfaceConfig.CANVAS_EXTRA / 2, interfaceConfig.CANVAS_EXTRA / 2,
  58. canvas.width - interfaceConfig.CANVAS_EXTRA,
  59. canvas.height - interfaceConfig.CANVAS_EXTRA,
  60. interfaceConfig.CANVAS_RADIUS,
  61. interfaceConfig.SHADOW_COLOR,
  62. shadowLevel);
  63. }
  64. }
  65. /**
  66. * Returns the shadow/glow level for the given audio level.
  67. *
  68. * @param audioLevel the audio level from which we determine the shadow
  69. * level
  70. */
  71. function getShadowLevel (audioLevel) {
  72. let shadowLevel = 0;
  73. if (audioLevel <= 0.3) {
  74. shadowLevel
  75. = Math.round(interfaceConfig.CANVAS_EXTRA/2*(audioLevel/0.3));
  76. } else if (audioLevel <= 0.6) {
  77. shadowLevel
  78. = Math.round(interfaceConfig.CANVAS_EXTRA/2*((audioLevel - 0.3) / 0.3));
  79. } else {
  80. shadowLevel
  81. = Math.round(interfaceConfig.CANVAS_EXTRA/2*((audioLevel - 0.6) / 0.4));
  82. }
  83. return shadowLevel;
  84. }
  85. /**
  86. * Returns the video span id corresponding to the given user id
  87. */
  88. function getVideoSpanId(id) {
  89. let videoSpanId = null;
  90. if (id === LOCAL_LEVEL || APP.conference.isLocalId(id)) {
  91. videoSpanId = 'localVideoContainer';
  92. } else {
  93. videoSpanId = `participant_${id}`;
  94. }
  95. return videoSpanId;
  96. }
  97. /**
  98. * The audio Levels plugin.
  99. */
  100. const AudioLevels = {
  101. init () {
  102. ASDrawContext = $('#dominantSpeakerAudioLevel')[0].getContext('2d');
  103. initDominantSpeakerAudioLevels();
  104. },
  105. /**
  106. * Updates the audio level canvas for the given id. If the canvas
  107. * didn't exist we create it.
  108. */
  109. updateAudioLevelCanvas (id, thumbWidth, thumbHeight) {
  110. let videoSpanId = 'localVideoContainer';
  111. if (id) {
  112. videoSpanId = `participant_${id}`;
  113. }
  114. let videoSpan = document.getElementById(videoSpanId);
  115. if (!videoSpan) {
  116. if (id) {
  117. console.error("No video element for id", id);
  118. } else {
  119. console.error("No video element for local video.");
  120. }
  121. return;
  122. }
  123. let audioLevelCanvas = $(`#${videoSpanId}>canvas`);
  124. if (!audioLevelCanvas || audioLevelCanvas.length === 0) {
  125. audioLevelCanvas = document.createElement('canvas');
  126. audioLevelCanvas.className = "audiolevel";
  127. audioLevelCanvas.style.bottom = `-${interfaceConfig.CANVAS_EXTRA/2}px`;
  128. audioLevelCanvas.style.left = `-${interfaceConfig.CANVAS_EXTRA/2}px`;
  129. resizeAudioLevelCanvas(audioLevelCanvas, thumbWidth, thumbHeight);
  130. videoSpan.appendChild(audioLevelCanvas);
  131. } else {
  132. audioLevelCanvas = audioLevelCanvas.get(0);
  133. resizeAudioLevelCanvas(audioLevelCanvas, thumbWidth, thumbHeight);
  134. }
  135. },
  136. /**
  137. * Updates the audio level UI for the given id.
  138. *
  139. * @param id id of the user for whom we draw the audio level
  140. * @param audioLevel the newAudio level to render
  141. */
  142. updateAudioLevel (id, audioLevel, largeVideoId) {
  143. drawAudioLevelCanvas(id, audioLevel);
  144. let videoSpanId = getVideoSpanId(id);
  145. let audioLevelCanvas = $(`#${videoSpanId}>canvas`).get(0);
  146. if (!audioLevelCanvas) {
  147. return;
  148. }
  149. let drawContext = audioLevelCanvas.getContext('2d');
  150. let canvasCache = audioLevelCanvasCache[id];
  151. drawContext.clearRect(
  152. 0, 0, audioLevelCanvas.width, audioLevelCanvas.height
  153. );
  154. drawContext.drawImage(canvasCache, 0, 0);
  155. if (id === LOCAL_LEVEL) {
  156. id = APP.conference.localId;
  157. if (!id) {
  158. return;
  159. }
  160. }
  161. if(id === largeVideoId) {
  162. window.requestAnimationFrame(function () {
  163. AudioLevels.updateDominantSpeakerAudioLevel(audioLevel);
  164. });
  165. }
  166. },
  167. updateDominantSpeakerAudioLevel (audioLevel) {
  168. if($("#dominantSpeaker").css("visibility") == "hidden"
  169. || ASDrawContext === null) {
  170. return;
  171. }
  172. ASDrawContext.clearRect(0, 0, 300, 300);
  173. if (!audioLevel) {
  174. return;
  175. }
  176. ASDrawContext.shadowBlur = getShadowLevel(audioLevel);
  177. // Fill the shape.
  178. ASDrawContext.fill();
  179. },
  180. updateCanvasSize (thumbWidth, thumbHeight) {
  181. let canvasWidth = thumbWidth + interfaceConfig.CANVAS_EXTRA;
  182. let canvasHeight = thumbHeight + interfaceConfig.CANVAS_EXTRA;
  183. FilmStrip.getThumbs().children('canvas').each(function () {
  184. $(this).attr('width', canvasWidth);
  185. $(this).attr('height', canvasHeight);
  186. });
  187. Object.keys(audioLevelCanvasCache).forEach(function (id) {
  188. audioLevelCanvasCache[id].width = canvasWidth;
  189. audioLevelCanvasCache[id].height = canvasHeight;
  190. });
  191. }
  192. };
  193. export default AudioLevels;