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

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