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.7KB

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