mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-07 05:31:22 +00:00
Use proper timeline sheduling, leverage existing noteOn() semantics.
This commit is contained in:
@@ -73,21 +73,20 @@ Guacamole.AudioChannel = function() {
|
|||||||
var packet =
|
var packet =
|
||||||
new Guacamole.AudioChannel.Packet(mimetype, data);
|
new Guacamole.AudioChannel.Packet(mimetype, data);
|
||||||
|
|
||||||
var now = new Date().getTime();
|
var now;
|
||||||
|
if (Guacamole.AudioChannel.context)
|
||||||
|
now = Guacamole.AudioChannel.context.currentTime * 1000;
|
||||||
|
else
|
||||||
|
now = new Date().getTime();
|
||||||
|
|
||||||
// If time not initialized, initialize now
|
// If underflow is detected, delay start
|
||||||
if (next_packet_time == 0 || next_packet_time < now) {
|
if (next_packet_time < now)
|
||||||
next_packet_time = now + 50;
|
next_packet_time = now + 50;
|
||||||
console.log("underflow");
|
|
||||||
}
|
|
||||||
|
|
||||||
var time_until_play = next_packet_time - now;
|
|
||||||
|
|
||||||
// Schedule next packet
|
// Schedule next packet
|
||||||
|
packet.play(next_packet_time);
|
||||||
next_packet_time += duration;
|
next_packet_time += duration;
|
||||||
|
|
||||||
packet.play(time_until_play);
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -129,43 +128,23 @@ Guacamole.AudioChannel.Packet = function(mimetype, data) {
|
|||||||
// Get context and start decoding
|
// Get context and start decoding
|
||||||
Guacamole.AudioChannel.context.decodeAudioData(
|
Guacamole.AudioChannel.context.decodeAudioData(
|
||||||
arrayBuffer,
|
arrayBuffer,
|
||||||
function(buffer) { handleReady(buffer); },
|
function(buffer) { handleReady(buffer); }
|
||||||
function(e) { console.log("play error", e); }
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Set up buffer source
|
// Set up buffer source
|
||||||
var source = Guacamole.AudioChannel.context.createBufferSource();
|
var source = Guacamole.AudioChannel.context.createBufferSource();
|
||||||
source.connect(Guacamole.AudioChannel.context.destination);
|
source.connect(Guacamole.AudioChannel.context.destination);
|
||||||
|
|
||||||
var play_call;
|
var play_when;
|
||||||
var play_delay;
|
|
||||||
|
|
||||||
function playDelayed(buffer) {
|
function playDelayed(buffer) {
|
||||||
|
|
||||||
// Calculate time since call to play()
|
|
||||||
var offset = new Date().getTime() - play_call;
|
|
||||||
var delay = play_delay - offset;
|
|
||||||
|
|
||||||
source.buffer = buffer;
|
source.buffer = buffer;
|
||||||
|
source.noteOn(play_when / 1000);
|
||||||
if (delay <= 0)
|
|
||||||
source.noteOn(0);
|
|
||||||
else
|
|
||||||
window.setTimeout(function() {
|
|
||||||
source.noteOn(0);
|
|
||||||
}, delay);
|
|
||||||
|
|
||||||
//source.noteOn(Math.max(play_delay - offset, 0) / 1000);
|
|
||||||
|
|
||||||
if (offset > play_delay)
|
|
||||||
console.log("processing lag", offset - play_delay);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.play = function(delay) {
|
this.play = function(when) {
|
||||||
|
|
||||||
play_call = new Date().getTime();
|
play_when = when;
|
||||||
play_delay = delay;
|
|
||||||
|
|
||||||
// If buffer available, play it NOW
|
// If buffer available, play it NOW
|
||||||
if (readyBuffer)
|
if (readyBuffer)
|
||||||
@@ -191,10 +170,22 @@ Guacamole.AudioChannel.Packet = function(mimetype, data) {
|
|||||||
/**
|
/**
|
||||||
* Plays the sound data contained by this packet immediately.
|
* Plays the sound data contained by this packet immediately.
|
||||||
*/
|
*/
|
||||||
this.play = function(delay) {
|
this.play = function(when) {
|
||||||
|
|
||||||
|
// Calculate time until play
|
||||||
|
var now = new Date().getTime();
|
||||||
|
var delay = when - now;
|
||||||
|
|
||||||
|
// Play now if too late
|
||||||
|
if (delay < 0)
|
||||||
|
audio.play();
|
||||||
|
|
||||||
|
// Otherwise, schedule later playback
|
||||||
|
else
|
||||||
window.setTimeout(function() {
|
window.setTimeout(function() {
|
||||||
audio.play();
|
audio.play();
|
||||||
}, delay);
|
}, delay);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user