Skip to content

Commit 49249d5

Browse files
fix: do not request manifests until play when preload is none (#1060)
Fixes #126
1 parent 6279675 commit 49249d5

4 files changed

Lines changed: 55 additions & 10 deletions

File tree

index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
input[type=url], select {
3333
min-width: 600px;
3434
}
35+
#preload {
36+
min-width: auto;
37+
}
3538
h3 {
3639
margin-bottom: 5px;
3740
}
@@ -85,6 +88,13 @@ <h3>Options</h3>
8588
<input id=override-native type="checkbox" checked>
8689
Override Native (reloads player)
8790
</label>
91+
<label>
92+
Preload (reloads player)
93+
<select id=preload>
94+
<option selected>auto</option>
95+
<option>none</option>
96+
<option>metadata</option>
97+
</select>
8898
</div>
8999

90100
<h3>Load a URL</h3>

scripts/index-demo-page.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
var getInputValue = function(el) {
7474
if (el.type === 'url' || el.type === 'text' || el.nodeName.toLowerCase() === 'textarea') {
7575
return encodeURIComponent(el.value);
76+
} else if (el.type === 'select-one') {
77+
return el.options[el.selectedIndex].value;
7678
} else if (el.type === 'checkbox') {
7779
return el.checked;
7880
}
@@ -84,6 +86,12 @@
8486
var setInputValue = function(el, value) {
8587
if (el.type === 'url' || el.type === 'text' || el.nodeName.toLowerCase() === 'textarea') {
8688
el.value = decodeURIComponent(value);
89+
} else if (el.type === 'select-one') {
90+
for (let i = 0; i < el.options.length; i++) {
91+
if (el.options[i].value === value) {
92+
el.options[i].selected = true;
93+
}
94+
}
8795
} else {
8896
// get the `value` into a Boolean.
8997
el.checked = JSON.parse(value);
@@ -217,7 +225,7 @@
217225
representationsEl.selectedIndex = selectedIndex;
218226
};
219227

220-
['debug', 'autoplay', 'muted', 'minified', 'sync-workers', 'liveui', 'partial', 'url', 'type', 'keysystems', 'buffer-water', 'override-native'].forEach(function(name) {
228+
['debug', 'autoplay', 'muted', 'minified', 'sync-workers', 'liveui', 'partial', 'url', 'type', 'keysystems', 'buffer-water', 'override-native', 'preload'].forEach(function(name) {
221229
stateEls[name] = document.getElementById(name);
222230
});
223231

@@ -251,6 +259,12 @@
251259
stateEls.minified.dispatchEvent(newEvent('change'));
252260
});
253261

262+
stateEls.preload.addEventListener('change', function(event) {
263+
saveState();
264+
// reload the player and scripts
265+
stateEls.minified.dispatchEvent(newEvent('change'));
266+
});
267+
254268
stateEls.debug.addEventListener('change', function(event) {
255269
saveState();
256270
window.videojs.log.level(event.target.checked ? 'debug' : 'info');
@@ -315,6 +329,7 @@
315329
var videoEl = document.createElement('video-js');
316330

317331
videoEl.setAttribute('controls', '');
332+
videoEl.setAttribute('preload', stateEls.preload.options[stateEls.preload.selectedIndex].value || 'auto');
318333
videoEl.className = 'vjs-default-skin';
319334
fixture.appendChild(videoEl);
320335

src/master-playlist-controller.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,16 @@ export class MasterPlaylistController extends videojs.EventTarget {
279279
this.logger_ = logger('MPC');
280280

281281
this.triggeredFmp4Usage = false;
282-
this.masterPlaylistLoader_.load();
282+
if (this.tech_.preload() === 'none') {
283+
this.loadOnPlay_ = () => {
284+
this.loadOnPlay_ = null;
285+
this.masterPlaylistLoader_.load();
286+
};
287+
288+
this.tech_.one('play', this.loadOnPlay_);
289+
} else {
290+
this.masterPlaylistLoader_.load();
291+
}
283292
}
284293

285294
/**
@@ -381,6 +390,9 @@ export class MasterPlaylistController extends videojs.EventTarget {
381390
});
382391

383392
this.masterPlaylistLoader_.on('loadedplaylist', () => {
393+
if (this.loadOnPlay_) {
394+
this.tech_.off('play', this.loadOnPlay_);
395+
}
384396
let updatedPlaylist = this.masterPlaylistLoader_.media();
385397

386398
if (!updatedPlaylist) {
@@ -1406,6 +1418,10 @@ export class MasterPlaylistController extends videojs.EventTarget {
14061418
this.masterPlaylistLoader_.dispose();
14071419
this.mainSegmentLoader_.dispose();
14081420

1421+
if (this.loadOnPlay_) {
1422+
this.tech_.off('play', this.loadOnPlay_);
1423+
}
1424+
14091425
['AUDIO', 'SUBTITLES'].forEach((type) => {
14101426
const groups = this.mediaTypes_[type].groups;
14111427

test/videojs-http-streaming.test.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3718,7 +3718,7 @@ QUnit.test('switching playlists with an outstanding key request aborts request a
37183718
);
37193719
});
37203720

3721-
QUnit.test('does not download segments if preload option set to none', function(assert) {
3721+
QUnit.test('does not download anything until play if preload option set to none', function(assert) {
37223722
this.player.preload('none');
37233723
this.player.src({
37243724
src: 'master.m3u8',
@@ -3728,19 +3728,23 @@ QUnit.test('does not download segments if preload option set to none', function(
37283728
this.clock.tick(1);
37293729

37303730
openMediaSource(this.player, this.clock);
3731-
// master
3732-
this.standardXHRResponse(this.requests.shift());
3733-
// media
3734-
this.standardXHRResponse(this.requests.shift());
37353731
this.clock.tick(10 * 1000);
37363732

3737-
this.requests = this.requests.filter(function(request) {
3738-
return !(/m3u8$/).test(request.uri);
3739-
});
37403733
assert.equal(this.requests.length, 0, 'did not download any segments');
37413734

37423735
// verify stats
37433736
assert.equal(this.player.tech_.vhs.stats.bandwidth, 4194304, 'default');
3737+
3738+
this.player.tech_.paused = () => false;
3739+
this.player.tech_.trigger('play');
3740+
3741+
// master
3742+
this.standardXHRResponse(this.requests.shift());
3743+
3744+
// media
3745+
this.standardXHRResponse(this.requests.shift());
3746+
3747+
assert.equal(this.requests.length, 1, 'requested segment');
37443748
});
37453749

37463750
// workaround https://bugzilla.mozilla.org/show_bug.cgi?id=548397

0 commit comments

Comments
 (0)