2015-12-04 15:25:24 +00:00
|
|
|
/* global APP, interfaceConfig, $ */
|
2015-09-11 02:42:15 +00:00
|
|
|
/* jshint -W101 */
|
2015-12-11 16:16:07 +00:00
|
|
|
|
2015-12-04 15:25:24 +00:00
|
|
|
import CanvasUtil from './CanvasUtils';
|
2016-02-24 21:05:24 +00:00
|
|
|
import FilmStrip from '../videolayout/FilmStrip';
|
2015-01-07 14:54:03 +00:00
|
|
|
|
2015-12-11 16:16:07 +00:00
|
|
|
const LOCAL_LEVEL = 'local';
|
|
|
|
|
|
|
|
let ASDrawContext = null;
|
|
|
|
let audioLevelCanvasCache = {};
|
2016-03-03 23:38:40 +00:00
|
|
|
let dominantSpeakerAudioElement = null;
|
2015-02-11 16:29:20 +00:00
|
|
|
|
2016-03-03 23:38:40 +00:00
|
|
|
function initDominantSpeakerAudioLevels(dominantSpeakerAvatarSize) {
|
|
|
|
let ASRadius = dominantSpeakerAvatarSize / 2;
|
|
|
|
let ASCenter = (dominantSpeakerAvatarSize + ASRadius) / 2;
|
2015-02-11 16:29:20 +00:00
|
|
|
|
2015-07-28 21:52:32 +00:00
|
|
|
// Draw a circle.
|
2016-03-03 23:38:40 +00:00
|
|
|
ASDrawContext.beginPath();
|
2015-02-11 16:29:20 +00:00
|
|
|
ASDrawContext.arc(ASCenter, ASCenter, ASRadius, 0, 2 * Math.PI);
|
2016-03-03 23:38:40 +00:00
|
|
|
ASDrawContext.closePath();
|
2015-02-11 16:29:20 +00:00
|
|
|
|
2015-07-28 21:52:32 +00:00
|
|
|
// Add a shadow around the circle
|
2015-02-11 16:29:20 +00:00
|
|
|
ASDrawContext.shadowColor = interfaceConfig.SHADOW_COLOR;
|
|
|
|
ASDrawContext.shadowOffsetX = 0;
|
|
|
|
ASDrawContext.shadowOffsetY = 0;
|
|
|
|
}
|
|
|
|
|
2014-07-21 10:36:11 +00:00
|
|
|
/**
|
2015-12-11 16:16:07 +00:00
|
|
|
* Resizes the given audio level canvas to match the given thumbnail size.
|
|
|
|
*/
|
|
|
|
function resizeAudioLevelCanvas(audioLevelCanvas, thumbnailWidth, thumbnailHeight) {
|
|
|
|
audioLevelCanvas.width = thumbnailWidth + interfaceConfig.CANVAS_EXTRA;
|
|
|
|
audioLevelCanvas.height = thumbnailHeight + interfaceConfig.CANVAS_EXTRA;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws the audio level canvas into the cached canvas object.
|
|
|
|
*
|
|
|
|
* @param id of the user for whom we draw the audio level
|
|
|
|
* @param audioLevel the newAudio level to render
|
2014-07-21 10:36:11 +00:00
|
|
|
*/
|
2015-12-11 16:16:07 +00:00
|
|
|
function drawAudioLevelCanvas(id, audioLevel) {
|
|
|
|
if (!audioLevelCanvasCache[id]) {
|
|
|
|
|
|
|
|
let videoSpanId = getVideoSpanId(id);
|
|
|
|
|
|
|
|
let audioLevelCanvasOrig = $(`#${videoSpanId}>canvas`).get(0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME Testing has shown that audioLevelCanvasOrig may not exist.
|
|
|
|
* In such a case, the method CanvasUtil.cloneCanvas may throw an
|
|
|
|
* error. Since audio levels are frequently updated, the errors have
|
|
|
|
* been observed to pile into the console, strain the CPU.
|
|
|
|
*/
|
|
|
|
if (audioLevelCanvasOrig) {
|
2016-02-05 17:32:09 +00:00
|
|
|
audioLevelCanvasCache[id]
|
|
|
|
= CanvasUtil.cloneCanvas(audioLevelCanvasOrig);
|
2015-12-11 16:16:07 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-21 10:36:11 +00:00
|
|
|
|
2015-12-11 16:16:07 +00:00
|
|
|
let canvas = audioLevelCanvasCache[id];
|
2014-07-24 14:14:37 +00:00
|
|
|
|
2015-12-11 16:16:07 +00:00
|
|
|
if (!canvas) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let drawContext = canvas.getContext('2d');
|
|
|
|
|
|
|
|
drawContext.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
|
|
|
|
let shadowLevel = getShadowLevel(audioLevel);
|
|
|
|
|
|
|
|
if (shadowLevel > 0) {
|
|
|
|
// drawContext, x, y, w, h, r, shadowColor, shadowLevel
|
2016-02-05 17:32:09 +00:00
|
|
|
CanvasUtil.drawRoundRectGlow(
|
|
|
|
drawContext,
|
|
|
|
interfaceConfig.CANVAS_EXTRA / 2, interfaceConfig.CANVAS_EXTRA / 2,
|
|
|
|
canvas.width - interfaceConfig.CANVAS_EXTRA,
|
|
|
|
canvas.height - interfaceConfig.CANVAS_EXTRA,
|
|
|
|
interfaceConfig.CANVAS_RADIUS,
|
|
|
|
interfaceConfig.SHADOW_COLOR,
|
|
|
|
shadowLevel);
|
2015-12-11 16:16:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the shadow/glow level for the given audio level.
|
|
|
|
*
|
|
|
|
* @param audioLevel the audio level from which we determine the shadow
|
|
|
|
* level
|
|
|
|
*/
|
|
|
|
function getShadowLevel (audioLevel) {
|
|
|
|
let shadowLevel = 0;
|
|
|
|
|
|
|
|
if (audioLevel <= 0.3) {
|
2016-03-03 23:38:40 +00:00
|
|
|
shadowLevel = Math.round(
|
|
|
|
interfaceConfig.CANVAS_EXTRA/2*(audioLevel/0.3));
|
2015-12-11 16:16:07 +00:00
|
|
|
} else if (audioLevel <= 0.6) {
|
2016-03-03 23:38:40 +00:00
|
|
|
shadowLevel = Math.round(
|
|
|
|
interfaceConfig.CANVAS_EXTRA/2*((audioLevel - 0.3) / 0.3));
|
2015-12-11 16:16:07 +00:00
|
|
|
} else {
|
2016-03-03 23:38:40 +00:00
|
|
|
shadowLevel = Math.round(
|
|
|
|
interfaceConfig.CANVAS_EXTRA/2*((audioLevel - 0.6) / 0.4));
|
2015-12-11 16:16:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return shadowLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the video span id corresponding to the given user id
|
|
|
|
*/
|
|
|
|
function getVideoSpanId(id) {
|
|
|
|
let videoSpanId = null;
|
|
|
|
|
|
|
|
if (id === LOCAL_LEVEL || APP.conference.isLocalId(id)) {
|
|
|
|
videoSpanId = 'localVideoContainer';
|
|
|
|
} else {
|
|
|
|
videoSpanId = `participant_${id}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return videoSpanId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The audio Levels plugin.
|
|
|
|
*/
|
|
|
|
const AudioLevels = {
|
|
|
|
|
|
|
|
init () {
|
2016-03-03 23:38:40 +00:00
|
|
|
dominantSpeakerAudioElement = $('#dominantSpeakerAudioLevel')[0];
|
|
|
|
ASDrawContext = dominantSpeakerAudioElement.getContext('2d');
|
|
|
|
|
|
|
|
let parentContainer = $("#dominantSpeaker");
|
|
|
|
let dominantSpeakerWidth = parentContainer.width();
|
|
|
|
let dominantSpeakerHeight = parentContainer.height();
|
|
|
|
|
|
|
|
dominantSpeakerAudioElement.width = dominantSpeakerWidth;
|
|
|
|
dominantSpeakerAudioElement.height = dominantSpeakerHeight;
|
|
|
|
|
|
|
|
let dominantSpeakerAvatar = $("#dominantSpeakerAvatar");
|
|
|
|
initDominantSpeakerAudioLevels(dominantSpeakerAvatar.width());
|
2015-12-11 16:16:07 +00:00
|
|
|
},
|
2015-02-11 16:29:20 +00:00
|
|
|
|
2014-07-21 10:36:11 +00:00
|
|
|
/**
|
2015-12-04 15:25:24 +00:00
|
|
|
* Updates the audio level canvas for the given id. If the canvas
|
2014-07-21 10:36:11 +00:00
|
|
|
* didn't exist we create it.
|
|
|
|
*/
|
2015-12-30 10:55:51 +00:00
|
|
|
updateAudioLevelCanvas (id, thumbWidth, thumbHeight) {
|
2015-12-04 15:25:24 +00:00
|
|
|
let videoSpanId = 'localVideoContainer';
|
|
|
|
if (id) {
|
|
|
|
videoSpanId = `participant_${id}`;
|
2014-07-21 10:36:11 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 15:25:24 +00:00
|
|
|
let videoSpan = document.getElementById(videoSpanId);
|
2014-07-21 10:36:11 +00:00
|
|
|
|
|
|
|
if (!videoSpan) {
|
2015-12-04 15:25:24 +00:00
|
|
|
if (id) {
|
|
|
|
console.error("No video element for id", id);
|
|
|
|
} else {
|
2014-07-21 10:36:11 +00:00
|
|
|
console.error("No video element for local video.");
|
2015-12-04 15:25:24 +00:00
|
|
|
}
|
2014-07-21 10:36:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-04 15:25:24 +00:00
|
|
|
let audioLevelCanvas = $(`#${videoSpanId}>canvas`);
|
2014-07-21 10:36:11 +00:00
|
|
|
|
|
|
|
if (!audioLevelCanvas || audioLevelCanvas.length === 0) {
|
|
|
|
|
|
|
|
audioLevelCanvas = document.createElement('canvas');
|
|
|
|
audioLevelCanvas.className = "audiolevel";
|
2016-03-03 23:38:40 +00:00
|
|
|
audioLevelCanvas.style.bottom
|
|
|
|
= `-${interfaceConfig.CANVAS_EXTRA/2}px`;
|
|
|
|
audioLevelCanvas.style.left
|
|
|
|
= `-${interfaceConfig.CANVAS_EXTRA/2}px`;
|
2015-12-30 10:55:51 +00:00
|
|
|
resizeAudioLevelCanvas(audioLevelCanvas, thumbWidth, thumbHeight);
|
2014-07-21 10:36:11 +00:00
|
|
|
|
|
|
|
videoSpan.appendChild(audioLevelCanvas);
|
|
|
|
} else {
|
|
|
|
audioLevelCanvas = audioLevelCanvas.get(0);
|
|
|
|
|
2015-12-30 10:55:51 +00:00
|
|
|
resizeAudioLevelCanvas(audioLevelCanvas, thumbWidth, thumbHeight);
|
2014-07-21 10:36:11 +00:00
|
|
|
}
|
2015-12-11 16:16:07 +00:00
|
|
|
},
|
2014-07-21 10:36:11 +00:00
|
|
|
|
|
|
|
/**
|
2015-12-04 15:25:24 +00:00
|
|
|
* Updates the audio level UI for the given id.
|
2014-07-21 10:36:11 +00:00
|
|
|
*
|
2015-12-04 15:25:24 +00:00
|
|
|
* @param id id of the user for whom we draw the audio level
|
2014-07-21 10:36:11 +00:00
|
|
|
* @param audioLevel the newAudio level to render
|
|
|
|
*/
|
2015-12-11 16:16:07 +00:00
|
|
|
updateAudioLevel (id, audioLevel, largeVideoId) {
|
2015-12-04 15:25:24 +00:00
|
|
|
drawAudioLevelCanvas(id, audioLevel);
|
2014-07-21 10:36:11 +00:00
|
|
|
|
2015-12-04 15:25:24 +00:00
|
|
|
let videoSpanId = getVideoSpanId(id);
|
2014-07-21 10:36:11 +00:00
|
|
|
|
2015-12-04 15:25:24 +00:00
|
|
|
let audioLevelCanvas = $(`#${videoSpanId}>canvas`).get(0);
|
2014-07-21 10:36:11 +00:00
|
|
|
|
2015-12-04 15:25:24 +00:00
|
|
|
if (!audioLevelCanvas) {
|
2014-07-24 14:14:37 +00:00
|
|
|
return;
|
2015-12-04 15:25:24 +00:00
|
|
|
}
|
2014-07-21 10:36:11 +00:00
|
|
|
|
2015-12-04 15:25:24 +00:00
|
|
|
let drawContext = audioLevelCanvas.getContext('2d');
|
2014-07-21 10:36:11 +00:00
|
|
|
|
2015-12-04 15:25:24 +00:00
|
|
|
let canvasCache = audioLevelCanvasCache[id];
|
2014-07-21 10:36:11 +00:00
|
|
|
|
2015-12-04 15:25:24 +00:00
|
|
|
drawContext.clearRect(
|
|
|
|
0, 0, audioLevelCanvas.width, audioLevelCanvas.height
|
|
|
|
);
|
2014-07-21 10:36:11 +00:00
|
|
|
drawContext.drawImage(canvasCache, 0, 0);
|
2014-12-10 09:52:40 +00:00
|
|
|
|
2015-12-11 16:16:07 +00:00
|
|
|
if (id === LOCAL_LEVEL) {
|
2016-07-08 01:44:04 +00:00
|
|
|
id = APP.conference.getMyUserId();
|
2015-12-04 15:25:24 +00:00
|
|
|
if (!id) {
|
2014-12-11 11:37:41 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-12-10 09:52:40 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 15:25:24 +00:00
|
|
|
if(id === largeVideoId) {
|
2015-02-11 16:29:20 +00:00
|
|
|
window.requestAnimationFrame(function () {
|
2016-01-12 00:14:01 +00:00
|
|
|
AudioLevels.updateDominantSpeakerAudioLevel(audioLevel);
|
2015-02-11 16:29:20 +00:00
|
|
|
});
|
2014-12-10 09:52:40 +00:00
|
|
|
}
|
2015-12-11 16:16:07 +00:00
|
|
|
},
|
2014-12-10 09:52:40 +00:00
|
|
|
|
2016-01-12 00:14:01 +00:00
|
|
|
updateDominantSpeakerAudioLevel (audioLevel) {
|
2016-02-05 17:32:09 +00:00
|
|
|
if($("#dominantSpeaker").css("visibility") == "hidden"
|
|
|
|
|| ASDrawContext === null) {
|
2015-02-11 16:29:20 +00:00
|
|
|
return;
|
2015-12-04 15:25:24 +00:00
|
|
|
}
|
2014-12-10 09:52:40 +00:00
|
|
|
|
2016-03-03 23:38:40 +00:00
|
|
|
ASDrawContext.clearRect(0, 0,
|
|
|
|
dominantSpeakerAudioElement.width,
|
|
|
|
dominantSpeakerAudioElement.height);
|
|
|
|
|
2015-12-04 15:25:24 +00:00
|
|
|
if (!audioLevel) {
|
2015-02-11 16:29:20 +00:00
|
|
|
return;
|
2015-12-04 15:25:24 +00:00
|
|
|
}
|
2014-12-10 09:52:40 +00:00
|
|
|
|
2015-02-11 16:29:20 +00:00
|
|
|
ASDrawContext.shadowBlur = getShadowLevel(audioLevel);
|
2014-12-10 09:52:40 +00:00
|
|
|
|
|
|
|
// Fill the shape.
|
2015-02-11 16:29:20 +00:00
|
|
|
ASDrawContext.fill();
|
2015-12-25 16:55:45 +00:00
|
|
|
},
|
|
|
|
|
2015-12-30 10:55:51 +00:00
|
|
|
updateCanvasSize (thumbWidth, thumbHeight) {
|
|
|
|
let canvasWidth = thumbWidth + interfaceConfig.CANVAS_EXTRA;
|
|
|
|
let canvasHeight = thumbHeight + interfaceConfig.CANVAS_EXTRA;
|
2015-12-25 16:55:45 +00:00
|
|
|
|
2016-02-24 21:05:24 +00:00
|
|
|
FilmStrip.getThumbs().children('canvas').each(function () {
|
2016-02-05 17:32:09 +00:00
|
|
|
$(this).attr('width', canvasWidth);
|
|
|
|
$(this).attr('height', canvasHeight);
|
|
|
|
});
|
2015-12-25 16:55:45 +00:00
|
|
|
|
2015-12-30 10:55:51 +00:00
|
|
|
Object.keys(audioLevelCanvasCache).forEach(function (id) {
|
|
|
|
audioLevelCanvasCache[id].width = canvasWidth;
|
|
|
|
audioLevelCanvasCache[id].height = canvasHeight;
|
|
|
|
});
|
2015-12-14 12:26:50 +00:00
|
|
|
}
|
2015-12-11 16:16:07 +00:00
|
|
|
};
|
2015-01-07 14:54:03 +00:00
|
|
|
|
2015-12-11 16:16:07 +00:00
|
|
|
export default AudioLevels;
|