95 lines
2.3 KiB
JavaScript
95 lines
2.3 KiB
JavaScript
// Slide media startup and teardown helpers.
|
|
|
|
function initializeRenderedVideoPlayback(root, delayMs) {
|
|
if (!root) {
|
|
return;
|
|
}
|
|
|
|
var startDelayMs = Math.max(0, Number(delayMs || 0));
|
|
var videos = root.querySelectorAll('.template-region.video video');
|
|
Array.prototype.forEach.call(videos, function (video) {
|
|
if (!video) {
|
|
return;
|
|
}
|
|
|
|
var playbackScheduled = false;
|
|
|
|
video.autoplay = false;
|
|
video.loop = !(video.dataset && video.dataset.loop === '0');
|
|
video.muted = !(video.dataset && video.dataset.disableAudio === '0');
|
|
video.playsInline = true;
|
|
|
|
function startPlayback() {
|
|
var playPromise = video.play && video.play();
|
|
if (playPromise && typeof playPromise.catch === 'function') {
|
|
playPromise.catch(function () {
|
|
return null;
|
|
});
|
|
}
|
|
}
|
|
|
|
function schedulePlaybackStart() {
|
|
if (playbackScheduled) {
|
|
return;
|
|
}
|
|
playbackScheduled = true;
|
|
if (startDelayMs > 0) {
|
|
window.setTimeout(startPlayback, startDelayMs);
|
|
return;
|
|
}
|
|
startPlayback();
|
|
}
|
|
|
|
if (video.readyState >= 2) {
|
|
schedulePlaybackStart();
|
|
return;
|
|
}
|
|
|
|
video.addEventListener('canplay', schedulePlaybackStart, { once: true });
|
|
video.addEventListener('loadedmetadata', function () {
|
|
if (video.readyState >= 2) {
|
|
schedulePlaybackStart();
|
|
}
|
|
}, { once: true });
|
|
});
|
|
}
|
|
|
|
function pauseRenderedVideoPlayback(root, delayMs) {
|
|
if (!root) {
|
|
return;
|
|
}
|
|
|
|
var pauseDelayMs = Math.max(0, Number(delayMs || 0));
|
|
var videos = root.querySelectorAll('.template-region.video video, .slide-media video');
|
|
Array.prototype.forEach.call(videos, function (video) {
|
|
if (!video || typeof video.pause !== 'function') {
|
|
return;
|
|
}
|
|
|
|
window.setTimeout(function () {
|
|
try {
|
|
video.pause();
|
|
} catch (_error) {
|
|
return null;
|
|
}
|
|
}, pauseDelayMs);
|
|
});
|
|
}
|
|
|
|
function initializeSlideMedia(root, delayMs) {
|
|
if (!root || root.isConnected === false) {
|
|
return;
|
|
}
|
|
|
|
if (typeof syncRtmpRegions === 'function') {
|
|
syncRtmpRegions(root);
|
|
}
|
|
initializeRenderedVideoPlayback(root, delayMs);
|
|
}
|
|
|
|
function destroySlideMedia(root) {
|
|
if (typeof destroyRtmpRegions === 'function') {
|
|
destroyRtmpRegions(root);
|
|
}
|
|
}
|