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

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