2012-11-22 10:38:28 +00:00
<!DOCTYPE HTML>
< html >
< head >
< title > Test for the playbackRate property < / title >
< script type = "text/javascript" src = "/tests/SimpleTest/SimpleTest.js" > < / script >
< link rel = "stylesheet" type = "text/css" href = "/tests/SimpleTest/test.css" / >
< script type = "text/javascript" src = "manifest.js" > < / script >
< / head >
< body >
< pre id = "test" >
2017-02-22 21:10:07 +00:00
< script class = "testbody" type = 'application/javascript' >
2012-11-22 10:38:28 +00:00
let manager = new MediaTestManager;
function rangeCheck(lhs, rhs, threshold) {
var diff = Math.abs(lhs - rhs);
if (diff < threshold ) {
return true;
}
return false;
}
function checkPlaybackRate(wallclock, media, expected, threshold) {
if (rangeCheck(media / wallclock, expected, threshold)) {
return true;
}
return false;
}
2013-03-19 12:23:54 +00:00
// Those value are expected to match those at the top of HTMLMediaElement.cpp.
2017-06-28 17:00:58 +00:00
let VERY_SLOW_RATE = 1 / 32,
SLOW_RATE = 1 / 16,
FAST_RATE = 16,
2012-11-22 10:38:28 +00:00
VERY_FAST_RATE = 20,
NULL_RATE = 0.0;
function ontimeupdate(e) {
var t = e.target;
2014-09-29 17:23:00 +00:00
// Skip short files for SoundTouch doesn't work well on small number of samples.
if (t.gotEnded || t.duration < 2 ) {
2012-11-22 10:38:28 +00:00
return;
}
t.testedForSlowdown = true;
if (t.currentTime > t.duration / 2) {
t.oldCurrentTime = t.currentTime;
t.timestamp = Date.now();
var delta = t.oldCurrentTime,
delta_wallclock = (t.timestamp - t.startTimestamp - t.bufferingTime) / 1000;
t.mozPreservesPitch = false;
2016-01-08 07:52:25 +00:00
is(t.mozPreservesPitch, false, t.name + ": If we disable the pitch preservation, it should appear as such.");
2012-11-22 10:38:28 +00:00
t.bufferingTime = 0;
2016-01-08 07:52:25 +00:00
is(t.playbackRate, SLOW_RATE, t.name + ": The playback rate shoud be "+SLOW_RATE+".");
ok(checkPlaybackRate(delta_wallclock, delta, SLOW_RATE, 0.25), t.name + ": We are effectively slowing down playback. (" + delta_wallclock + ", " + delta + ")");
2012-11-22 10:38:28 +00:00
t.removeEventListener("timeupdate", ontimeupdate);
t.addEventListener("pause", onpaused);
t.playbackRate = NULL_RATE;
t.oldCurrentTime = t.currentTime;
setTimeout(function() {
afterNullPlaybackRate(e);
}, 100);
}
}
function onpaused(e) {
var t = e.target;
t.pausedReceived = true;
}
function afterNullPlaybackRate(e) {
var t = e.target;
2014-06-26 02:56:24 +00:00
// skip if we have received 'ended' event or 'ended' event is pending.
if (t.gotEnded || t.ended) {
2012-11-22 10:38:28 +00:00
return;
}
2014-06-26 02:56:24 +00:00
t.testedForNull = true;
2016-01-08 07:52:25 +00:00
ok(t.currentTime == t.oldCurrentTime, t.name + ": Current time should not change when playbackRate is null (" + t.currentTime + " " + t.oldCurrentTime + ").");
ok(!t.paused, t.name + ": The element should not be in paused state.");
2012-11-22 10:38:28 +00:00
t.removeEventListener("paused", onpaused);
2016-01-08 07:52:25 +00:00
is(t.pausedReceived, undefined, t.name + ": Paused event should not have been received.");
2012-11-22 10:38:28 +00:00
t.timestamp = Date.now();
t.oldCurrentTime = t.currentTime;
t.playbackRate = VERY_FAST_RATE;
2017-08-24 09:41:15 +00:00
is(t.playbackRate, VERY_FAST_RATE, t.name + ": Playback rate should be clamped to " + VERY_FAST_RATE + ".");
2012-11-22 10:38:28 +00:00
}
function onended(e) {
2014-06-26 02:56:24 +00:00
var t = e.target;
t.gotEnded = true;
2012-11-22 10:38:28 +00:00
t.bufferingTime = 0;
2014-06-26 02:56:24 +00:00
// If we got "ended" too early, skip these tests.
2012-11-22 10:38:28 +00:00
if (t.testedForSlowdown & & t.testedForNull) {
2016-01-08 07:52:25 +00:00
is(t.playbackRate, FAST_RATE, t.name + ": The playback rate should still be "+FAST_RATE+".");
ok(!t.muted, t.name + ": The audio should be muted when playing at high speed, but should not appear as such.");
is(t.currentTime, t.duration, t.name + ": Current time should be equal to the duration (not change by playback rate).");
2012-11-22 10:38:28 +00:00
}
2013-07-11 14:58:57 +00:00
finish_test(t);
2012-11-22 10:38:28 +00:00
}
function onratechange(e) {
if (!e.target.ratechangecount) {
e.target.ratechangecount = 0;
}
e.target.ratechangecount++;
}
function finish_test(element) {
2014-06-26 02:56:24 +00:00
removeNodeAndSource(element);
2012-11-22 10:38:28 +00:00
manager.finished(element.token);
}
// These two functions handle the case when the playback pauses for buffering. It
// adjusts the timestamps to be accurate. Despite the fact that the web servers
// is supposed to be on the same machine, buffering pauses can occur (rarely,
// but still).
function onplaying(e) {
var t = e.target;
if (t.bufferingTimestamp != undefined) {
t.bufferingTime += (Date.now() - t.bufferingTimestamp);
t.bufferingTimestamp = undefined;
}
}
function onwaiting(e) {
var t = e.target;
t.bufferingTimestamp = Date.now();
}
function onvolumechange(e) {
2016-01-08 07:52:25 +00:00
ok(false, e.target.name + ": We should not receive a volumechange event when changing the playback rate.");
2012-11-22 10:38:28 +00:00
}
function startTest(test, token) {
let elemType = /^audio/.test(test.type) ? "audio" : "video";
let element = document.createElement(elemType);
element.src = test.name;
2016-01-08 07:52:25 +00:00
element.name = test.name;
2013-07-31 07:35:44 +00:00
element.preload = "metadata";
2012-11-22 10:38:28 +00:00
element.token = token;
element.controls = true;
element.bufferingTime = 0;
document.body.appendChild(element);
element.addEventListener("ratechange", onratechange);
element.addEventListener("timeupdate", ontimeupdate);
element.addEventListener("ended", onended);
element.addEventListener("waiting", onwaiting);
element.addEventListener("playing", onplaying);
element.addEventListener("volumechange", onvolumechange);
manager.started(token);
element.startTimestamp = Date.now();
2016-01-08 07:52:25 +00:00
is(element.mozPreservesPitch, true, test.name + ": Pitch preservation should be enabled by default.");
2012-11-22 10:38:28 +00:00
element.addEventListener("loadedmetadata", function() {
2016-01-08 07:52:25 +00:00
is(element.playbackRate, 1.0, test.name + ": playbackRate should be initially 1.0");
is(element.defaultPlaybackRate, 1.0, test.name + ": defaultPlaybackRate should be initially 1.0");
2012-11-22 10:38:28 +00:00
element.playbackRate = VERY_SLOW_RATE;
2017-08-24 09:41:15 +00:00
is(element.playbackRate, VERY_SLOW_RATE, test.name + ": PlaybackRate should be " + VERY_SLOW_RATE + ".");
2012-11-22 10:38:28 +00:00
element.play();
element.playbackRate = SLOW_RATE;
});
}
manager.runTests(gPlayedTests, startTest);
< / script >
< / pre >
< div id = "elements" >
< / div >
< / body >
< / html >