From 5b017e0b7729ae4b06f758e5c4ed580183043882 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Tue, 13 Nov 2012 12:17:14 -0800 Subject: [PATCH] Use high-resolution timestamps if available. --- .../src/main/resources/audio.js | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/guacamole-common-js/src/main/resources/audio.js b/guacamole-common-js/src/main/resources/audio.js index 463bb5a14..2ac3287ed 100644 --- a/guacamole-common-js/src/main/resources/audio.js +++ b/guacamole-common-js/src/main/resources/audio.js @@ -73,15 +73,11 @@ Guacamole.AudioChannel = function() { var packet = new Guacamole.AudioChannel.Packet(mimetype, data); - var now; - if (Guacamole.AudioChannel.context) - now = Guacamole.AudioChannel.context.currentTime * 1000; - else - now = new Date().getTime(); + var now = Guacamole.AudioChannel.getTimestamp(); - // If underflow is detected, delay start + // If underflow is detected, reschedule new packets relative to now. if (next_packet_time < now) - next_packet_time = now + 50; + next_packet_time = now; // Schedule next packet packet.play(next_packet_time); @@ -96,6 +92,28 @@ if (window.webkitAudioContext) { Guacamole.AudioChannel.context = new webkitAudioContext(); } +Guacamole.AudioChannel.getTimestamp = function() { + + // If we have an audio context, use its timestamp + if (Guacamole.AudioChannel.context) + return Guacamole.AudioChannel.context.currentTime * 1000; + + // If we have high-resolution timers, use those + if (window.performance) { + + if (window.performance.now) + return window.peformance.now(); + + if (window.performance.webkitNow) + return window.performance.webkitNow(); + + } + + // Fallback to millisecond-resolution system time + return new Date().getTime(); + +}; + /** * Abstract representation of an audio packet. * @@ -173,7 +191,7 @@ Guacamole.AudioChannel.Packet = function(mimetype, data) { this.play = function(when) { // Calculate time until play - var now = new Date().getTime(); + var now = Guacamole.AudioChannel.getTimestamp(); var delay = when - now; // Play now if too late