diff --git a/guacamole-common-js/src/main/webapp/modules/AudioPlayer.js b/guacamole-common-js/src/main/webapp/modules/AudioPlayer.js index abda411d0..08bf4c82c 100644 --- a/guacamole-common-js/src/main/webapp/modules/AudioPlayer.js +++ b/guacamole-common-js/src/main/webapp/modules/AudioPlayer.js @@ -66,6 +66,70 @@ Guacamole.AudioPlayer.getTimestamp = function() { }; +/** + * Determines whether the given mimetype is supported by any built-in + * implementation of Guacamole.AudioPlayer, and thus will be properly handled + * by Guacamole.AudioPlayer.getInstance(). + * + * @param {String} mimetype + * The mimetype to check. + * + * @returns {Boolean} + * true if the given mimetype is supported by any built-in + * Guacamole.AudioPlayer, false otherwise. + */ +Guacamole.AudioPlayer.isSupportedType = function isSupportedType(mimetype) { + + return Guacamole.RawAudioPlayer.isSupportedType(mimetype); + +}; + +/** + * Returns a list of all mimetypes supported by any built-in + * Guacamole.AudioPlayer, in rough order of priority. Beware that only the core + * mimetypes themselves will be listed. Any mimetype parameters, even required + * ones, will not be included in the list. For example, "audio/L8" is a + * supported raw audio mimetype that is supported, but it is invalid without + * additional parameters. Something like "audio/L8;rate=44100" would be valid, + * however (see https://tools.ietf.org/html/rfc4856). + * + * @returns {String[]} + * A list of all mimetypes supported by any built-in Guacamole.AudioPlayer, + * excluding any parameters. + */ +Guacamole.AudioPlayer.getSupportedTypes = function getSupportedTypes() { + + return Guacamole.RawAudioPlayer.getSupportedTypes(); + +}; + +/** + * Returns an instance of Guacamole.AudioPlayer providing support for the given + * audio format. If support for the given audio format is not available, null + * is returned. + * + * @param {Guacamole.InputStream} stream + * The Guacamole.InputStream to read audio data from. + * + * @param {String} mimetype + * The mimetype of the audio data in the provided stream. + * + * @return {Guacamole.AudioPlayer} + * A Guacamole.AudioPlayer instance supporting the given mimetype and + * reading from the given stream, or null if support for the given mimetype + * is absent. + */ +Guacamole.AudioPlayer.getInstance = function getInstance(stream, mimetype) { + + // Use raw audio player if possible + if (Guacamole.RawAudioPlayer.isSupportedType(mimetype)) + return new Guacamole.RawAudioPlayer(stream, mimetype); + + // No support for given mimetype + return null; + +}; + /** * Implementation of Guacamole.AudioPlayer providing support for raw PCM format * audio. This player relies only on the Web Audio API and does not require any diff --git a/guacamole-common-js/src/main/webapp/modules/Client.js b/guacamole-common-js/src/main/webapp/modules/Client.js index 8104159d4..894888738 100644 --- a/guacamole-common-js/src/main/webapp/modules/Client.js +++ b/guacamole-common-js/src/main/webapp/modules/Client.js @@ -635,13 +635,11 @@ Guacamole.Client = function(tunnel) { if (guac_client.onaudio) audioPlayer = guac_client.onaudio(stream, mimetype); - // If unsuccessful, use a default implementation - if (!audioPlayer) { - if (Guacamole.RawAudioPlayer.isSupportedType(mimetype)) - audioPlayer = new Guacamole.RawAudioPlayer(stream, mimetype); - } + // If unsuccessful, try to use a default implementation + if (!audioPlayer) + audioPlayer = Guacamole.AudioPlayer.getInstance(stream, mimetype); - // If player somehow successfully retrieved, send success response + // If we have successfully retrieved an audio player, send success response if (audioPlayer) { audioPlayers[stream_index] = audioPlayer; guac_client.sendAck(stream_index, "OK", 0x0000);