Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(demo): add a representations selector to the demo page #901

Merged
merged 1 commit into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
<body>
<div id="player-fixture">
</div>
<label>Representations</label>
<select id='representations'></select>
<h3>Options</h3>

<div class="options">
Expand Down
57 changes: 56 additions & 1 deletion scripts/index-demo-page.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
/* global window document */
/* eslint-disable vars-on-top, no-var, object-shorthand, no-console */
(function(window) {
var representationsEl = document.getElementById('representations');

representationsEl.addEventListener('change', function() {
var selectedIndex = representationsEl.selectedIndex;

if (!selectedIndex || selectedIndex < 1 || !window.vhs) {
return;
}
var selectedOption = representationsEl.options[representationsEl.selectedIndex];

if (!selectedOption) {
return;
}

var id = selectedOption.value;

window.vhs.representations().forEach(function(rep) {
rep.enabled(rep.id === id);
});

});
var hlsOptGroup = document.querySelector('[label="hls"]');
var dashOptGroup = document.querySelector('[label="dash"]');
var drmOptGroup = document.querySelector('[label="drm"]');
Expand Down Expand Up @@ -165,6 +185,36 @@
onload();
};

var regenerateRepresentations = function() {
while (representationsEl.firstChild) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As things are blacklisted representations can be removed/added.

representationsEl.removeChild(representationsEl.firstChild);
}

var selectedIndex;

window.vhs.representations().forEach(function(rep, i) {
var option = document.createElement('option');

option.value = rep.id;
option.innerText = JSON.stringify({
id: rep.id,
videoCodec: rep.codecs.video,
audioCodec: rep.codecs.audio,
bandwidth: rep.bandwidth,
heigth: rep.heigth,
width: rep.width
});

if (window.mpc.media().id === rep.id) {
selectedIndex = i;
}

representationsEl.appendChild(option);
});

representationsEl.selectedIndex = selectedIndex;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not trigger a change event on representationsEl as that only happens when not using the js api.

};

['debug', 'autoplay', 'muted', 'minified', 'liveui', 'partial', 'url', 'type', 'keysystems'].forEach(function(name) {
stateEls[name] = document.getElementById(name);
});
Expand Down Expand Up @@ -277,10 +327,15 @@
} else {
sources.dispatchEvent(newEvent('change'));
}
player.on('loadedmetadata', () => {
player.on('loadedmetadata', function() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can guess, ie 11 didn't like this

if (player.vhs) {
window.vhs = player.tech_.vhs;
window.mpc = player.tech_.vhs.masterPlaylistController_;
window.mpc.masterPlaylistLoader_.on('mediachange', function() {
regenerateRepresentations();
});
regenerateRepresentations();

} else {
window.vhs = null;
window.mpc = null;
Expand Down