Modify audio support to handle blob packets rather than base64.

This commit is contained in:
Michael Jumper
2013-09-24 14:08:39 -07:00
parent 68e2e2b028
commit efcda74912

View File

@@ -68,7 +68,7 @@ Guacamole.AudioChannel = function() {
* @param {String} mimetype The mimetype of the data provided. * @param {String} mimetype The mimetype of the data provided.
* @param {Number} duration The duration of the data provided, in * @param {Number} duration The duration of the data provided, in
* milliseconds. * milliseconds.
* @param {String} data The base64-encoded data to play. * @param {Blob} data The blob data to play.
*/ */
this.play = function(mimetype, duration, data) { this.play = function(mimetype, duration, data) {
@@ -130,7 +130,7 @@ Guacamole.AudioChannel.getTimestamp = function() {
* @constructor * @constructor
* *
* @param {String} mimetype The mimetype of the data contained by this packet. * @param {String} mimetype The mimetype of the data contained by this packet.
* @param {String} data The base64-encoded sound data contained by this packet. * @param {Blob} data The blob of sound data contained by this packet.
*/ */
Guacamole.AudioChannel.Packet = function(mimetype, data) { Guacamole.AudioChannel.Packet = function(mimetype, data) {
@@ -154,19 +154,15 @@ Guacamole.AudioChannel.Packet = function(mimetype, data) {
readyBuffer = buffer; readyBuffer = buffer;
}; };
// Convert to ArrayBuffer // Read data and start decoding
var binary = window.atob(data); var reader = new FileReader();
var arrayBuffer = new ArrayBuffer(binary.length); reader.onload = function() {
var bufferView = new Uint8Array(arrayBuffer); Guacamole.AudioChannel.context.decodeAudioData(
reader.result,
for (var i=0; i<binary.length; i++) function(buffer) { handleReady(buffer); }
bufferView[i] = binary.charCodeAt(i); );
};
// Get context and start decoding reader.readAsArrayBuffer(data);
Guacamole.AudioChannel.context.decodeAudioData(
arrayBuffer,
function(buffer) { handleReady(buffer); }
);
// Set up buffer source // Set up buffer source
var source = Guacamole.AudioChannel.context.createBufferSource(); var source = Guacamole.AudioChannel.context.createBufferSource();
@@ -198,12 +194,43 @@ Guacamole.AudioChannel.Packet = function(mimetype, data) {
else { else {
// Build data URI var play_on_load = false;
var data_uri = "data:" + mimetype + ";base64," + data;
// Create audio element to house and play the data // Create audio element to house and play the data
var audio = new Audio(); var audio = new Audio();
audio.src = data_uri;
// Read data and start decoding
var reader = new FileReader();
reader.onload = function() {
var binary = "";
var bytes = new Uint8Array(reader.result);
// Produce binary string from bytes in buffer
for (var i=0; i<bytes.byteLength; i++)
binary += String.fromCharCode(bytes[i]);
// Convert to data URI
audio.src = "data:" + mimetype + ";base64," + window.btoa(binary);
// Play if play was attempted but packet wasn't loaded yet
if (play_on_load)
audio.play();
};
reader.readAsArrayBuffer(data);
function play() {
// If audio data is ready, play now
if (audio.src)
audio.play();
// Otherwise, play when loaded
else
play_on_load = true;
}
/** @ignore */ /** @ignore */
this.play = function(when) { this.play = function(when) {
@@ -214,13 +241,11 @@ Guacamole.AudioChannel.Packet = function(mimetype, data) {
// Play now if too late // Play now if too late
if (delay < 0) if (delay < 0)
audio.play(); play();
// Otherwise, schedule later playback // Otherwise, schedule later playback
else else
window.setTimeout(function() { window.setTimeout(play, delay);
audio.play();
}, delay);
}; };