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

Implement Overlay events #31

Open
wants to merge 27 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
21147c7
[WIP] Add video overlay
jmt-qualabs Nov 7, 2024
c791af1
fix styling of overlay video to match video
ronalduQualabs Nov 19, 2024
aae54fb
Add iframe overlay
jmt-qualabs Nov 19, 2024
b3f25f7
Implement earliestResolutionTime
jmt-qualabs Nov 20, 2024
647c4c4
Change overlay tag
jmt-qualabs Nov 21, 2024
f45c568
Remove overlay at end of event duration
jmt-qualabs Nov 21, 2024
962df58
Implement overlay modes and refId
sebastianpiq Nov 21, 2024
35d8d45
Fix seek overlay
sebastianpiq Nov 21, 2024
f76b901
Implement overlay event z attribute
sebastianpiq Nov 22, 2024
477d070
Add OverlayController
jmt-qualabs Nov 25, 2024
4f10a07
Fix overlay loop
jmt-qualabs Nov 25, 2024
9280240
Implement overlay size and topleft
sebastianpiq Nov 25, 2024
f5f04c3
Implement overlay squeeze content
sebastianpiq Nov 25, 2024
905b24f
Use transitions to manage overlays size
sebastianpiq Nov 26, 2024
ed26429
Improve overlay scheduler
sebastianpiq Nov 27, 2024
418c418
Add missing semicolons
sebastianpiq Nov 27, 2024
121cb0a
Remove unused extend mode event id logic
sebastianpiq Nov 27, 2024
734dc38
Add new squeeze current percentage definition
sebastianpiq Nov 27, 2024
1b97763
Add missing semicolons
sebastianpiq Nov 28, 2024
4151332
Improve overlay select mode logic
sebastianpiq Nov 29, 2024
7973882
Implement unit tests
sebastianpiq Dec 2, 2024
2e69c67
Remove unnecessary it only tests
sebastianpiq Dec 2, 2024
b4dfd2a
Add missing semicolons and fix wrong let declarations
sebastianpiq Dec 2, 2024
cccf004
Fix squeeze current bug
sebastianpiq Dec 5, 2024
29790dd
Add overlay sample page
sebastianpiq Dec 13, 2024
7be71c2
Fix looped overlay
jmt-qualabs Dec 18, 2024
718bb74
Fix overlay controller test
jmt-qualabs Dec 19, 2024
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 src/dash/constants/DashConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export default {
MPD: 'MPD',
ORIGINAL_MPD_ID: 'mpdId',
ORIGINAL_PUBLISH_TIME: 'originalPublishTime',
OVERLAY: 'OverlayEvent',
PATCH_LOCATION: 'PatchLocation',
PERIOD: 'Period',
PRESENTATION_TIME: 'presentationTime',
Expand Down
8 changes: 8 additions & 0 deletions src/dash/models/DashManifestModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,14 @@ function DashManifestModel() {
} else {
event.id = null;
}
if (currentMpdEvent.hasOwnProperty(DashConstants.OVERLAY)) {
event.overlay = currentMpdEvent.OverlayEvent;
if (event.overlay.earliestResolutionTime) {
event.calculatedPresentationTime -= event.overlay.earliestResolutionTime / eventStream.timescale;
}
} else {
event.overlay = null;
}
sebastianpiq marked this conversation as resolved.
Show resolved Hide resolved

if (currentMpdEvent.Signal && currentMpdEvent.Signal.Binary) {
// toString is used to manage both regular and namespaced tags
Expand Down
25 changes: 25 additions & 0 deletions src/streaming/MediaPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import CatchupController from './controllers/CatchupController.js';
import ServiceDescriptionController from '../dash/controllers/ServiceDescriptionController.js';
import ContentSteeringController from '../dash/controllers/ContentSteeringController.js';
import MediaController from './controllers/MediaController.js';
import OverlayController from './controllers/OverlayController.js';
import BaseURLController from './controllers/BaseURLController.js';
import ManifestLoader from './ManifestLoader.js';
import ErrorHandler from './utils/ErrorHandler.js';
Expand Down Expand Up @@ -144,6 +145,7 @@ function MediaPlayer() {
schemeLoaderFactory,
timelineConverter,
mediaController,
overlayController,
protectionController,
metricsReportingController,
mssHandler,
Expand Down Expand Up @@ -257,6 +259,9 @@ function MediaPlayer() {
if (config.mediaController) {
mediaController = config.mediaController;
}
if (config.overlayController) {
overlayController = config.overlayController;
}
if (config.settings) {
settings = config.settings;
}
Expand Down Expand Up @@ -328,6 +333,10 @@ function MediaPlayer() {
mediaController = MediaController(context).getInstance();
}

if (!overlayController) {
overlayController = OverlayController(context).getInstance();
}

if (!streamController) {
streamController = StreamController(context).getInstance();
}
Expand Down Expand Up @@ -413,6 +422,9 @@ function MediaPlayer() {
videoModel
});

overlayController.setConfig({
sebastianpiq marked this conversation as resolved.
Show resolved Hide resolved
videoModel});

mediaPlayerModel.setConfig({
playbackController,
serviceDescriptionController
Expand Down Expand Up @@ -1490,6 +1502,18 @@ function MediaPlayer() {
videoModel.setVttRenderingDiv(div);
}

function attachOverlayRenderingDiv(overlayDiv) {
const videoElement = videoModel.getElement();
if (!videoElement) {
throw ELEMENT_NOT_ATTACHED_ERROR;
}
N1Knight marked this conversation as resolved.
Show resolved Hide resolved

overlayController.configureVideoElementForOverlay();
videoModel.setOverlayRenderingDiv(overlayDiv);

overlayController.setupOverlayEvents();
}

/*
---------------------------------------------------------------------------

Expand Down Expand Up @@ -2740,6 +2764,7 @@ function MediaPlayer() {
attachProtectionController,
attachSource,
attachTTMLRenderingDiv,
attachOverlayRenderingDiv,
attachView,
attachVttRenderingDiv,
clearDefaultUTCTimingSources,
Expand Down
9 changes: 9 additions & 0 deletions src/streaming/constants/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,5 +341,14 @@ export default {
DTSC: 'dtsc',
AVC: 'avc',
HEVC: 'hevc'
},

OVERLAY: {
SCHEME_ID: 'urn:scte:dash:scte214-events',
START_MODE: 'start',
STOP_MODE: 'stop',
EXTEND_MODE: 'extend',
VIDEO_MIMETYPE: 'video/mp4',
IFRMAE_MIMETYPE: 'text/html'
}
}
281 changes: 281 additions & 0 deletions src/streaming/controllers/OverlayController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
/**
* The copyright in this software is being made available under the BSD License,
* included below. This software may be subject to other third party and contributor
* rights, including patent rights, and no such rights are granted under this license.
*
* Copyright (c) 2013, Dash Industry Forum.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* * Neither the name of Dash Industry Forum nor the names of its
* contributors may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
import Constants from '../constants/Constants.js';
import EventBus from './../../core/EventBus.js';
import FactoryMaker from '../../core/FactoryMaker.js';
import PlaybackController from './PlaybackController.js';
import Utils from '../../core/Utils.js';

function OverlayController() {

const context = this.context;
const eventBus = EventBus(context).getInstance();
const playbackController = PlaybackController(context).getInstance();

let instance,
videoModel,
schedulerInitialized,
overlayList = [];

function setConfig(config) {
if (!config) {
return;
}

if (config.videoModel) {
videoModel = config.videoModel;
}
}

function configureVideoElementForOverlay() {
const videoElement = videoModel.getElement();
videoElement.style.width = '100%';
videoElement.style.height = '100%';
videoElement.style.transform = 'scale(1)';

const parent = videoElement.parentElement;
parent.style.position = 'relative';
parent.style.overflow = 'hidden';
}

function setupOverlayEvents() {
eventBus.on(Constants.OVERLAY.SCHEME_ID, _handleOverlayEvent);
}

function _initializeScheduler() {
if (!schedulerInitialized) {
schedulerInitialized = setInterval(_handleScheduleInerval, 100);
}
}

function _stopScheduler() {
clearInterval(schedulerInitialized);
schedulerInitialized = false;
}

function _handleScheduleInerval() {
if (overlayList.length) {
_startScheduledOverlay();
_stopScheduledOverlay();
}
}

function _startScheduledOverlay() {
const currentTime = playbackController.getTime();
overlayList.forEach((scheduledOverlay) => {
const { eventId, duration, presentationTime, overlay, overlayElement } = scheduledOverlay;
if (!scheduledOverlay.started && _canSetOverlayElement(presentationTime, currentTime, duration)) {
scheduledOverlay.started = true;
_stylizeOverlayContainter(overlay);
videoModel.setOverlayElement(overlayElement, eventId);
}
});

sebastianpiq marked this conversation as resolved.
Show resolved Hide resolved
}

function _stopScheduledOverlay() {
const currentTime = playbackController.getTime();
overlayList.forEach((scheduledOverlay) => {
const { eventId, duration, presentationTime } = scheduledOverlay;
if (duration && presentationTime + duration <= currentTime) {
sebastianpiq marked this conversation as resolved.
Show resolved Hide resolved
_stopOverlayEvent(eventId);
}
});
if (!overlayList.length) {
_stopScheduler();
}
}

function _handleOverlayEvent(e) {
let overlayElement

const { event } = e;
const overlayMode = event.overlay.mode ?? Constants.OVERLAY.START_MODE;

if (overlayMode === Constants.OVERLAY.START_MODE) {
if (event.overlay.mimeType === Constants.OVERLAY.VIDEO_MIMETYPE) {
overlayElement = _createVideoOverlayElement(event);
} else if (event.overlay.mimeType === Constants.OVERLAY.IFRMAE_MIMETYPE) {
overlayElement = _createIframeOverlayElement(event);
}

_adaptOverlayElement(overlayElement, event.overlay.uri);

let eventId = event.id ?? `${Utils.generateUuid()}`;
const presentationTime = event.presentationTime / 1000;
overlayList.push({
eventId,
duration: event.duration,
presentationTime,
overlay: event.overlay,
overlayElement,
started: false
});
_initializeScheduler();
return;
}

if (overlayMode === Constants.OVERLAY.EXTEND_MODE) {
if (event.duration) {
const overlayElement = overlayList.find(element => element.eventId == event.overlay.refId);
if (overlayElement) {
overlayElement.duration = event.duration;
overlayElement.presentationTime = event.presentationTime / 1000;
}
}
return;
}

if (overlayMode === Constants.OVERLAY.STOP_MODE) {
_stopOverlayEvent(event.overlay.refId);
return;
}
sebastianpiq marked this conversation as resolved.
Show resolved Hide resolved
}

function _stopOverlayEvent(refId) {
videoModel.removeOverlayElementById(refId);
configureVideoElementForOverlay();
// Do not filter if we want to reuse the overlays once they end.
overlayList = overlayList.filter((element) => element.eventId != refId);
}

function _canSetOverlayElement(presentationTime, currentTime, duration) {
return presentationTime <= currentTime && (presentationTime + duration > currentTime || !duration);
}

function _createVideoOverlayElement (event) {
const overlayElement = document.createElement('video');
overlayElement.preload = 'auto';
overlayElement.autoplay = true;
overlayElement.loop = event.loop === 'true';
_setVideoOverlayEvents(event, overlayElement);
return overlayElement;
}

function _setVideoOverlayEvents(event, overlayElement) {
eventBus.on(dashjs.MediaPlayer.events.PLAYBACK_PLAYING, function() {
overlayElement.play();
});

eventBus.on(dashjs.MediaPlayer.events.PLAYBACK_PAUSED, function() {
overlayElement.pause();
});

eventBus.on(dashjs.MediaPlayer.events.PLAYBACK_SEEKING, function() {
const presentationTime = event.presentationTime / 1000;
const seekTime = videoModel.getElement().currentTime - presentationTime;
if (seekTime > presentationTime && (seekTime <= presentationTime + event.duration || !event.duration)) {
overlayElement.currentTime = seekTime;
} else if (seekTime < 0) {
_stopOverlayEvent(event.id);
}
});
}

function _createIframeOverlayElement (event) {
const overlayElement = document.createElement('iframe');
overlayElement.style.border = 'none';
eventBus.on(dashjs.MediaPlayer.events.PLAYBACK_SEEKING, function() {
const presentationTime = event.presentationTime / 1000;
const seekTime = videoModel.getElement().currentTime - presentationTime;
if (seekTime < 0) {
_stopOverlayEvent(event.id);
}
});
return overlayElement;
}

function _adaptOverlayElement(overlayElement, uri) {
overlayElement.src = uri;
overlayElement.style.width = '100%';
overlayElement.style.height = '100%';
}

function _stylizeOverlayContainter(overlayEvent) {
const videoElement = videoModel.getElement();
const overlayDiv = videoModel.getOverlayRenderingDiv();
const { Viewport, Size, TopLeft, SqueezeCurrent, z } = overlayEvent;

if (!isNaN(z)) {
overlayDiv.style['z-index'] = z;
}

const squeezeCurrent = SqueezeCurrent.percentage;
if (SqueezeCurrent && z == -1) {
videoElement.style.transition = 'transform';
videoElement.style['transform-origin'] = 'top left';
videoElement.style.transform = `scale(${squeezeCurrent})`;
}

if (!Viewport || !Viewport?.x || !Viewport?.y ) {
return;
}

const { overlaySize, overlayTopLeft } = _calculateOverlayDimensions(Viewport, Size, TopLeft);
const resizeFunction = (entries) => {
const entry = entries[0];
const { width, height } = entry.contentRect;
overlayDiv.style.width = `${width * overlaySize.x}px`;
overlayDiv.style.height = `${height * overlaySize.y}px`;
overlayDiv.style.left = `${width * overlayTopLeft.x}px`;
overlayDiv.style.top = `${height * overlayTopLeft.y}px`;
};
const resizeObserver = new ResizeObserver(resizeFunction);
resizeObserver.observe(videoElement);
}

function _calculateOverlayDimensions(viewport, size, topLeft) {
const overlaySize = {
x: size && size.x ? size.x / viewport.x : 1,
y: size && size.y ? size.y / viewport.y : 1
};

const overlayTopLeft = {
x: topLeft && topLeft.x ? topLeft.x / viewport.x : 0,
y: topLeft && topLeft.y ? topLeft.y / viewport.y : 0
};

return { overlaySize, overlayTopLeft };
}

instance = {
setConfig,
configureVideoElementForOverlay,
setupOverlayEvents,
};

return instance;
}

OverlayController.__dashjs_factory_name = 'OverlayController';
const factory = FactoryMaker.getSingletonFactory(OverlayController);
FactoryMaker.updateSingletonFactory(OverlayController.__dashjs_factory_name, factory);
export default factory;
Loading
Loading