GUAC-882: Wrap calls to new Audio() within try/catch.

This commit is contained in:
Michael Jumper
2014-10-12 20:54:57 -07:00
parent 2202ede609
commit 5731cb6b34
2 changed files with 60 additions and 43 deletions

View File

@@ -121,7 +121,7 @@ Guacamole.AudioChannel.Packet = function(mimetype, data) {
* @param {Number} when The time this packet should be played, in * @param {Number} when The time this packet should be played, in
* milliseconds. * milliseconds.
*/ */
this.play = undefined; // Defined conditionally depending on support this.play = function(when) { /* NOP */ }; // Defined conditionally depending on support
// If audio API available, use it. // If audio API available, use it.
if (Guacamole.AudioChannel.context) { if (Guacamole.AudioChannel.context) {
@@ -181,57 +181,63 @@ Guacamole.AudioChannel.Packet = function(mimetype, data) {
var play_on_load = false; var play_on_load = false;
// Create audio element to house and play the data // Create audio element to house and play the data
var audio = new Audio(); var audio = null;
try { audio = new Audio(); }
catch (e) {}
// Read data and start decoding if (audio) {
var reader = new FileReader();
reader.onload = function() {
var binary = ""; // Read data and start decoding
var bytes = new Uint8Array(reader.result); var reader = new FileReader();
reader.onload = function() {
// Produce binary string from bytes in buffer var binary = "";
for (var i=0; i<bytes.byteLength; i++) var bytes = new Uint8Array(reader.result);
binary += String.fromCharCode(bytes[i]);
// Convert to data URI // Produce binary string from bytes in buffer
audio.src = "data:" + mimetype + ";base64," + window.btoa(binary); for (var i=0; i<bytes.byteLength; i++)
binary += String.fromCharCode(bytes[i]);
// Play if play was attempted but packet wasn't loaded yet // Convert to data URI
if (play_on_load) audio.src = "data:" + mimetype + ";base64," + window.btoa(binary);
audio.play();
}; // Play if play was attempted but packet wasn't loaded yet
reader.readAsArrayBuffer(data); if (play_on_load)
audio.play();
function play() {
// If audio data is ready, play now };
if (audio.src) reader.readAsArrayBuffer(data);
audio.play();
function play() {
// Otherwise, play when loaded // If audio data is ready, play now
else if (audio.src)
play_on_load = true; audio.play();
// Otherwise, play when loaded
else
play_on_load = true;
}
/** @ignore */
this.play = function(when) {
// Calculate time until play
var now = Guacamole.AudioChannel.getTimestamp();
var delay = when - now;
// Play now if too late
if (delay < 0)
play();
// Otherwise, schedule later playback
else
window.setTimeout(play, delay);
};
} }
/** @ignore */
this.play = function(when) {
// Calculate time until play
var now = Guacamole.AudioChannel.getTimestamp();
var delay = when - now;
// Play now if too late
if (delay < 0)
play();
// Otherwise, schedule later playback
else
window.setTimeout(play, delay);
};
} }

View File

@@ -187,7 +187,18 @@ GuacUI.Audio = new (function() {
// Build array of supported audio formats // Build array of supported audio formats
codecs.forEach(function(mimetype) { codecs.forEach(function(mimetype) {
var audio = new Audio(); // Attempt to get audio element for mimetype testing
var audio = null;
try {
audio = new Audio();
}
catch (e) {
// Skip testing if audio is not available
return;
}
var support_level = audio.canPlayType(mimetype); var support_level = audio.canPlayType(mimetype);
// Trim semicolon and trailer // Trim semicolon and trailer