From e9a3b453165f1b6ef20a6f99d881593194ea0953 Mon Sep 17 00:00:00 2001 From: Lyubomir Marinov Date: Wed, 23 Jul 2014 19:05:56 +0300 Subject: [PATCH] Fixes an error caused by trying to get a property of undefined related to audio levels. --- audio_levels.js | 20 ++++++++++++++++---- canvas_util.js | 10 +++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/audio_levels.js b/audio_levels.js index 3f9734378..a25da0116 100644 --- a/audio_levels.js +++ b/audio_levels.js @@ -117,15 +117,27 @@ var AudioLevels = (function(my) { var audioLevelCanvasOrig = $('#' + videoSpanId + '>canvas').get(0); - audioLevelCanvasCache[resourceJid] - = CanvasUtil.cloneCanvas(audioLevelCanvasOrig); + /* + * 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) + { + audioLevelCanvasCache[resourceJid] + = CanvasUtil.cloneCanvas(audioLevelCanvasOrig); + } } var canvas = audioLevelCanvasCache[resourceJid]; + if (!canvas) + return; + var drawContext = canvas.getContext('2d'); - drawContext.clearRect (0, 0, canvas.width, canvas.height); + drawContext.clearRect(0, 0, canvas.width, canvas.height); var shadowLevel = getShadowLevel(audioLevel); @@ -190,4 +202,4 @@ var AudioLevels = (function(my) { return my; -})(AudioLevels || {}); \ No newline at end of file +})(AudioLevels || {}); diff --git a/canvas_util.js b/canvas_util.js index bc96f030a..b8a1b0e9e 100644 --- a/canvas_util.js +++ b/canvas_util.js @@ -81,6 +81,14 @@ var CanvasUtil = (function(my) { * @return the new cloned canvas. */ my.cloneCanvas = function (oldCanvas) { + /* + * FIXME Testing has shown that oldCanvas 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 (!oldCanvas) + return oldCanvas; //create a new canvas var newCanvas = document.createElement('canvas'); @@ -98,4 +106,4 @@ var CanvasUtil = (function(my) { }; return my; -})(CanvasUtil || {}); \ No newline at end of file +})(CanvasUtil || {});