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

feat: add support for a list of QualityLevels #7897

Merged
merged 14 commits into from
Oct 4, 2022
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
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ <h2>Navigation</h2>
<li><a href="sandbox/language.html">Language Demo</a></li>
<li><a href="sandbox/load-media.html"><code>loadMedia</code> Demo</a></li>
<li><a href="sandbox/hls.html">Hls Demo</a></li>
<li><a href="sandbox/quality-levels.html">QualityLevels Demo</a></li>
<li><a href="sandbox/autoplay-tests.html">Autoplay Tests</a></li>
<li><a href="sandbox/noUITitleAttributes.html">noUITitleAttributes Demo</a></li>
<li><a href="sandbox/debug.html">Videojs debug build test page</a></li>
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"mpd-parser": "1.0.0",
"mux.js": "6.2.0",
"safe-json-parse": "4.0.0",
"videojs-contrib-quality-levels": "^2.2.0",
"videojs-font": "3.2.0",
"videojs-vtt.js": "^0.15.3"
},
Expand Down
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export default cliargs => [
plugins: [
alias({
'video.js': path.resolve(__dirname, './src/js/video.js'),
'videojs-contrib-quality-levels': path.resolve(__dirname, './node_modules/videojs-contrib-quality-levels/dist/videojs-contrib-quality-levels.es.js'),
'@videojs/http-streaming': path.resolve(__dirname, './node_modules/@videojs/http-streaming/dist/videojs-http-streaming.es.js')
}),
replace({
Expand Down
109 changes: 109 additions & 0 deletions sandbox/quality-levels.html.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Video.js Sandbox</title>
<link href="../dist/video-js.css" rel="stylesheet" type="text/css">
<style>
.btn {
background-color: #5cb85c;
border-radius: 0.5em;
border: 1px solid #18ab29;
display: inline-block;
cursor: pointer;
color: #ffffff;
font-size: 1em;
padding: 0.5em;
margin: 0.25em 0.25em 0.25em 0;
}

.btn-success {
font-weight: bold;
background-color: #337ab7;
}
</style>
<script src="../dist/video.js"></script>
</head>
<body>
<div
style="background-color:#eee; border: 1px solid #777; padding: 10px; margin-bottom: 20px; font-size: .8em; line-height: 1.5em; font-family: Verdana, sans-serif;">
<p>You can use /sandbox/ for writing and testing your own code. Nothing in /sandbox/ will get checked into the repo,
except files that end in .example (so don't edit or add those files). To get started make a copy of
index.html.example and rename it to index.html.</p>
<pre>cp sandbox/index.html.example sandbox/index.html</pre>
<pre>npm run start</pre>
<pre>open http://localhost:9999/sandbox/index.html</pre>
</div>
<div style="width: 65%;">
<video-js id="vid1" controls preload="auto" class="vjs-fluid">
<source src="https://d2zihajmogu5jn.cloudfront.net/bipbop-advanced/bipbop_16x9_variant.m3u8"
type="application/x-mpegURL">
<p class="vjs-no-js">To view this video please enable JavaScript, and consider upgrading to a web browser that <a
href="https://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a></p>
</video-js>
</div>
<div id="currentLevelControl" style="width: 100%;">
<button id="autoBtn" type="button" class="btn btn-success">Auto</button>
</div>
<script>
const vid = document.getElementById('vid1');
const player = videojs(vid, {
qualityLevels: true
});
player.one('loadedmetadata', () => {
const container = document.getElementById('currentLevelControl');
const autoBtn = document.getElementById('autoBtn');
const btnList = [];
// create a button for every video quality level
for (let i = 0; i < player.qualityLevels().length; i++) {
let level = player.qualityLevels()[i];
if (level.width === undefined) {
continue;
}
let levelElm = document.createElement('button');
levelElm.classList.add('btn');
if (player.qualityLevels().selectedIndex === i) {
levelElm.classList.add('btn-success');
}
levelElm.setAttribute('title', level.label);
levelElm.setAttribute('type', 'button');
levelElm.setAttribute('data-level', i);
levelElm.innerText = `${i} ('${level.width}': ${level.height}p, ${(level.bitrate / 1024).toFixed(0)}kb)`;
btnList.push(levelElm);
container.append(levelElm);
}
// attach a click handler to buttons
for (const btn of btnList) {
btn.addEventListener('click', (event) => {
const selectedIndex = player.qualityLevels().selectedIndex;
const btnIndex = event.target.dataset.level;
autoBtn.classList.remove('btn-success');
if (selectedIndex == btnIndex) {
return;
}
btnList.forEach((elm) => {
player.qualityLevels()[elm.dataset.level].enabled = elm.dataset.level === btnIndex;
});
});
}
// update buttons on video quality changes
player.qualityLevels().on('change', (event) => {
for (let btn of btnList) {
if (btn.dataset.level == event.selectedIndex) {
btn.classList.add('btn-success');
} else {
btn.classList.remove('btn-success');
}
}
});
// add a click handler to reset previously selected video quality by the user
autoBtn.addEventListener('click', (event) => {
btnList.forEach((elm) => {
player.qualityLevels()[elm.dataset.level].enabled = true;
});
autoBtn.classList.toggle('btn-success', true);
});
});
</script>
</body>
</html>
1 change: 1 addition & 0 deletions src/js/debug.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import videojs from './video';
import 'videojs-contrib-quality-levels';
import '@videojs/http-streaming';
import DomData from './utils/dom-data.js';

Expand Down
1 change: 1 addition & 0 deletions src/js/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import videojs from './video';
import 'videojs-contrib-quality-levels';
import '@videojs/http-streaming';
export default videojs;