GUAC-1511: Refactor private Guacamole.RawAudioPlayer._Format to public Guacamole.RawAudioFormat.

This commit is contained in:
Michael Jumper
2016-03-30 09:17:30 -07:00
parent 0d39a04a1e
commit b9de1d74c1
2 changed files with 149 additions and 130 deletions

View File

@@ -126,9 +126,9 @@ Guacamole.RawAudioPlayer = function RawAudioPlayer(stream, mimetype) {
* The format of audio this player will decode.
*
* @private
* @type {Guacamole.RawAudioPlayer._Format}
* @type {Guacamole.RawAudioFormat}
*/
var format = Guacamole.RawAudioPlayer._Format.parse(mimetype);
var format = Guacamole.RawAudioFormat.parse(mimetype);
/**
* An instance of a Web Audio API AudioContext object, or null if the
@@ -474,133 +474,6 @@ Guacamole.RawAudioPlayer = function RawAudioPlayer(stream, mimetype) {
Guacamole.RawAudioPlayer.prototype = new Guacamole.AudioPlayer();
/**
* A description of the format of raw PCM audio received by a
* Guacamole.RawAudioPlayer. This object describes the number of bytes per
* sample, the number of channels, and the overall sample rate.
*
* @private
* @constructor
* @param {Guacamole.RawAudioPlayer._Format|Object} template
* The object whose properties should be copied into the corresponding
* properties of the new Guacamole.RawAudioPlayer._Format.
*/
Guacamole.RawAudioPlayer._Format = function _Format(template) {
/**
* The number of bytes in each sample of audio data. This value is
* independent of the number of channels.
*
* @type {Number}
*/
this.bytesPerSample = template.bytesPerSample;
/**
* The number of audio channels (ie: 1 for mono, 2 for stereo).
*
* @type {Number}
*/
this.channels = template.channels;
/**
* The number of samples per second, per channel.
*
* @type {Number}
*/
this.rate = template.rate;
};
/**
* Parses the given mimetype, returning a new Guacamole.RawAudioPlayer._Format
* which describes the type of raw audio data represented by that mimetype. If
* the mimetype is not supported by Guacamole.RawAudioPlayer, null is returned.
*
* @private
* @param {String} mimetype
* The audio mimetype to parse.
*
* @returns {Guacamole.RawAudioPlayer._Format}
* A new Guacamole.RawAudioPlayer._Format which describes the type of raw
* audio data represented by the given mimetype, or null if the given
* mimetype is not supported.
*/
Guacamole.RawAudioPlayer._Format.parse = function parseFormat(mimetype) {
var bytesPerSample;
// Rate is absolutely required - if null is still present later, the
// mimetype must not be supported
var rate = null;
// Default for both "audio/L8" and "audio/L16" is one channel
var channels = 1;
// "audio/L8" has one byte per sample
if (mimetype.substring(0, 9) === 'audio/L8;') {
mimetype = mimetype.substring(9);
bytesPerSample = 1;
}
// "audio/L16" has two bytes per sample
else if (mimetype.substring(0, 10) === 'audio/L16;') {
mimetype = mimetype.substring(10);
bytesPerSample = 2;
}
// All other types are unsupported
else
return null;
// Parse all parameters
var parameters = mimetype.split(',');
for (var i = 0; i < parameters.length; i++) {
var parameter = parameters[i];
// All parameters must have an equals sign separating name from value
var equals = parameter.indexOf('=');
if (equals === -1)
return null;
// Parse name and value from parameter string
var name = parameter.substring(0, equals);
var value = parameter.substring(equals+1);
// Handle each supported parameter
switch (name) {
// Number of audio channels
case 'channels':
channels = parseInt(value);
break;
// Sample rate
case 'rate':
rate = parseInt(value);
break;
// All other parameters are unsupported
default:
return null;
}
};
// The rate parameter is required
if (rate === null)
return null;
// Return parsed format details
return new Guacamole.RawAudioPlayer._Format({
bytesPerSample : bytesPerSample,
channels : channels,
rate : rate
});
};
/**
* Determines whether the given mimetype is supported by
* Guacamole.RawAudioPlayer.
@@ -618,7 +491,7 @@ Guacamole.RawAudioPlayer.isSupportedType = function isSupportedType(mimetype) {
if (!window.AudioContext && !window.webkitAudioContext)
return false;
return Guacamole.RawAudioPlayer._Format.parse(mimetype) !== null;
return Guacamole.RawAudioFormat.parse(mimetype) !== null;
};