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

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