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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 (id, thumbWidth, thumbHeight) {
  122. let videoSpanId = (id === "local")
  123. ? "localVideoContainer"
  124. : `participant_${id}`;
  125. let videoSpan = document.getElementById(videoSpanId);
  126. if (!videoSpan) {
  127. if (id) {
  128. console.error("No video element for id", id);
  129. } else {
  130. console.error("No video element for local video.");
  131. }
  132. return;
  133. }
  134. let audioLevelCanvas = $(`#${videoSpanId}>canvas`);
  135. if (!audioLevelCanvas || audioLevelCanvas.length === 0) {
  136. audioLevelCanvas = document.createElement('canvas');
  137. audioLevelCanvas.className = "audiolevel";
  138. audioLevelCanvas.style.bottom
  139. = `-${interfaceConfig.CANVAS_EXTRA/2}px`;
  140. audioLevelCanvas.style.left
  141. = `-${interfaceConfig.CANVAS_EXTRA/2}px`;
  142. _resizeAudioLevelCanvas(audioLevelCanvas, thumbWidth, thumbHeight);
  143. videoSpan.appendChild(audioLevelCanvas);
  144. } else {
  145. audioLevelCanvas = audioLevelCanvas.get(0);
  146. _resizeAudioLevelCanvas(audioLevelCanvas, thumbWidth, thumbHeight);
  147. }
  148. },
  149. /**
  150. * Updates the audio level UI for the given id.
  151. *
  152. * @param id id of the user for whom we draw the audio level
  153. * @param audioLevel the newAudio level to render
  154. */
  155. updateAudioLevel (id, audioLevel, largeVideoId) {
  156. drawAudioLevelCanvas(id, audioLevel);
  157. let videoSpanId = getVideoSpanId(id);
  158. let audioLevelCanvas = $(`#${videoSpanId}>canvas`).get(0);
  159. if (!audioLevelCanvas) {
  160. return;
  161. }
  162. let drawContext = audioLevelCanvas.getContext('2d');
  163. let canvasCache = audioLevelCanvasCache[id];
  164. drawContext.clearRect(
  165. 0, 0, audioLevelCanvas.width, audioLevelCanvas.height
  166. );
  167. drawContext.drawImage(canvasCache, 0, 0);
  168. if (id === LOCAL_LEVEL) {
  169. id = APP.conference.getMyUserId();
  170. if (!id) {
  171. return;
  172. }
  173. }
  174. if(id === largeVideoId) {
  175. window.requestAnimationFrame(function () {
  176. AudioLevels.updateDominantSpeakerAudioLevel(audioLevel);
  177. });
  178. }
  179. },
  180. updateDominantSpeakerAudioLevel (audioLevel) {
  181. if($("#dominantSpeaker").css("visibility") == "hidden"
  182. || ASDrawContext === null) {
  183. return;
  184. }
  185. ASDrawContext.clearRect(0, 0,
  186. dominantSpeakerAudioElement.width,
  187. dominantSpeakerAudioElement.height);
  188. if (!audioLevel) {
  189. return;
  190. }
  191. ASDrawContext.shadowBlur = getShadowLevel(audioLevel);
  192. // Fill the shape.
  193. ASDrawContext.fill();
  194. },
  195. updateCanvasSize (localVideo, remoteVideo) {
  196. let localCanvasWidth
  197. = localVideo.thumbWidth + interfaceConfig.CANVAS_EXTRA;
  198. let localCanvasHeight
  199. = localVideo.thumbHeight + interfaceConfig.CANVAS_EXTRA;
  200. let remoteCanvasWidth
  201. = remoteVideo.thumbWidth + interfaceConfig.CANVAS_EXTRA;
  202. let remoteCanvasHeight
  203. = remoteVideo.thumbHeight + interfaceConfig.CANVAS_EXTRA;
  204. let { remoteThumbs, localThumb } = FilmStrip.getThumbs();
  205. remoteThumbs.children('canvas').each(function () {
  206. $(this).attr('width', remoteCanvasWidth);
  207. $(this).attr('height', remoteCanvasHeight);
  208. });
  209. if(localThumb) {
  210. localThumb.children('canvas').each(function () {
  211. $(this).attr('width', localCanvasWidth);
  212. $(this).attr('height', localCanvasHeight);
  213. });
  214. }
  215. }
  216. };
  217. export default AudioLevels;