Skip to content

Commit e485f5d

Browse files
authored
fix: emit spec progress event from HTML5 adapter (#110)
* fix: emit spec progress event from HTML5 adapter The HTML5 adapter emitted 'buffered', which is not in the spec, so player.on('progress') never fired. Its "percent" was video.buffered.length, the number of buffered time ranges, not a fraction (#79). Emit 'progress' with how far the media has buffered: seconds (end of the last buffered range) and duration, as SPEC.rst defines, plus percent (0-1) as the README documents. duration and percent are null when the duration is unknown or infinite. The test server gains ?throttle=1 to send media slowly, so the progress test sees a real download instead of the local file arriving at once. * fix: keep emitting legacy buffered event alongside progress Existing embedders listen for the HTML5 adapter's 'buffered' event, so it stays, with its original payload ({ percent: <number of buffered ranges> }). 'progress' is emitted in addition, not instead. Adds a test that the legacy event still fires with that payload.
1 parent c5414a4 commit e485f5d

4 files changed

Lines changed: 85 additions & 11 deletions

File tree

‎e2e/contract.spec.ts‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,42 @@ test.describe('HTML5Adapter specifics', () => {
140140
expect(result.time.seconds).toBeGreaterThanOrEqual(0);
141141
});
142142

143-
// BUG: the adapter emits 'buffered' (not in the spec) instead of 'progress',
144-
// so player.on('progress') never fires. src/adapters/html.js
145-
test.fail('fires progress while buffering', async ({ page }) => {
143+
// Regression: the adapter used to emit only 'buffered' (not in the spec),
144+
// so player.on('progress') never fired (#79).
145+
test('fires progress with how far the media has buffered', async ({ page }) => {
146+
// preload="none" (?early=1) so buffering starts only once we play, after
147+
// the listener is in place, and a throttled download (?throttle=1) so the
148+
// browser reports progress instead of receiving the file in one go.
149+
await openPlayer(page, 'html5.html?early=1&throttle=1');
146150
const progress = await page.evaluate(async () => {
147151
const p = nextEvent('progress', 4000);
148-
player.setCurrentTime(10);
149152
player.play();
150153
return p;
151154
});
152-
expect(progress).not.toBe('timeout');
155+
expect(progress).toEqual({
156+
seconds: expect.any(Number),
157+
duration: expect.any(Number),
158+
percent: expect.any(Number),
159+
});
160+
expect(progress.seconds).toBeGreaterThan(0);
161+
expect(progress.seconds).toBeLessThanOrEqual(progress.duration);
162+
expect(progress.percent).toBeCloseTo(progress.seconds / progress.duration, 5);
163+
expect(progress.percent).toBeLessThanOrEqual(1);
164+
});
165+
166+
// Compatibility: the legacy, non-spec 'buffered' event is still emitted
167+
// with its original payload, where "percent" is the number of buffered
168+
// time ranges. Existing embedders listen for it.
169+
test('still fires the legacy buffered event', async ({ page }) => {
170+
await openPlayer(page, 'html5.html?early=1&throttle=1');
171+
const buffered = await page.evaluate(async () => {
172+
const p = nextEvent('buffered', 4000);
173+
player.play();
174+
return p;
175+
});
176+
expect(buffered).toEqual({ percent: expect.any(Number) });
177+
expect(Number.isInteger(buffered.percent)).toBe(true);
178+
expect(buffered.percent).toBeGreaterThanOrEqual(0);
153179
});
154180
});
155181

‎e2e/fixtures/html5.html‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,22 @@
1111
var video = document.getElementById('video');
1212
var adapter = window.adapter = playerjs.HTML5Adapter(video);
1313

14+
var params = new URLSearchParams(location.search);
15+
16+
// ?throttle=1 makes the test server send the video slowly.
17+
var src = '/lib/videos/demo.mp4' + (params.get('throttle') ? '?throttle=1' : '');
18+
1419
// ?early=1 mirrors the original test page: ready immediately, before any
1520
// media metadata exists. Otherwise wait for metadata like a real
1621
// integration should.
17-
if (new URLSearchParams(location.search).get('early')) {
22+
if (params.get('early')) {
1823
video.preload = 'none';
19-
video.src = '/lib/videos/demo.mp4';
24+
video.src = src;
2025
adapter.ready();
2126
} else {
2227
video.preload = 'auto';
2328
video.addEventListener('loadedmetadata', function () { adapter.ready(); }, { once: true });
24-
video.src = '/lib/videos/demo.mp4';
29+
video.src = src;
2530
}
2631
</script>
2732
</body>

‎e2e/server.mjs‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@ async function bundle() {
4545
return parts.join('\n');
4646
}
4747

48+
// Write a response body, optionally in small timed chunks (?throttle=1) so
49+
// the browser sees a slow download, e.g. to observe media 'progress' events.
50+
function sendBody(res, buf, throttle) {
51+
if (!throttle) {
52+
res.end(buf);
53+
return;
54+
}
55+
const CHUNK = 32 * 1024;
56+
let offset = 0;
57+
const timer = setInterval(() => {
58+
if (res.destroyed || offset >= buf.length) {
59+
clearInterval(timer);
60+
res.end();
61+
return;
62+
}
63+
res.write(buf.subarray(offset, offset + CHUNK));
64+
offset += CHUNK;
65+
}, 100);
66+
res.on('close', () => clearInterval(timer));
67+
}
68+
4869
async function handle(req, res) {
4970
const url = new URL(req.url, 'http://x');
5071

@@ -74,6 +95,7 @@ async function handle(req, res) {
7495
}
7596

7697
const body = await readFile(path);
98+
const throttle = url.searchParams.has('throttle');
7799
const type = TYPES[extname(path)] ?? 'application/octet-stream';
78100

79101
// Video elements need byte-range support to seek.
@@ -87,12 +109,12 @@ async function handle(req, res) {
87109
'accept-ranges': 'bytes',
88110
'content-length': end - start + 1,
89111
});
90-
res.end(body.subarray(start, end + 1));
112+
sendBody(res, body.subarray(start, end + 1), throttle);
91113
return;
92114
}
93115

94116
res.writeHead(200, { 'content-type': type, 'accept-ranges': 'bytes', 'cache-control': 'no-store' });
95-
res.end(body);
117+
sendBody(res, body, throttle);
96118
} catch {
97119
res.writeHead(404).end('not found');
98120
}

‎src/adapters/html.js‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,29 @@ playerjs.HTML5Adapter.prototype.init = function(video){
3535
});
3636

3737
video.addEventListener('progress', function(){
38+
var buffered = video.buffered;
39+
40+
// Legacy, non-spec event kept for existing listeners. Its "percent" is
41+
// the number of buffered time ranges, not a fraction; use 'progress'.
3842
receiver.emit('buffered', {
39-
percent: video.buffered.length
43+
percent: buffered.length
44+
});
45+
46+
if (!buffered || buffered.length === 0) {
47+
return;
48+
}
49+
50+
// How far the media has buffered: the end of the last buffered range.
51+
// seconds/duration follow SPEC.rst; percent (0-1) is what the README
52+
// documents.
53+
var seconds = buffered.end(buffered.length - 1);
54+
var duration = video.duration;
55+
var known = isFinite(duration) && duration > 0;
56+
57+
receiver.emit('progress', {
58+
seconds: seconds,
59+
duration: known ? duration : null,
60+
percent: known ? Math.min(1, seconds / duration) : null
4061
});
4162
});
4263

0 commit comments

Comments
 (0)