Skip to content

Commit

Permalink
Merge pull request #5 from ygoto3/dashjs-sd-sustainable-binder
Browse files Browse the repository at this point in the history
feat: Add dashjs-sd-sustainable-binder package
  • Loading branch information
ygoto3 authored Mar 7, 2024
2 parents fe0cfd0 + 6844cb9 commit 0ddbfa6
Show file tree
Hide file tree
Showing 22 changed files with 20,877 additions and 22 deletions.
35 changes: 35 additions & 0 deletions package-lock.json

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

15 changes: 15 additions & 0 deletions packages/core/src/AdBreak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type AdPod from './AdPod';

export default class AdBreak {

constructor(offsetTime: number, id?: string, adPod?: AdPod) {
this.offsetTime = offsetTime;
this.id = id;
this.adPod = adPod;
}

readonly offsetTime: number;
readonly id?: string;
readonly adPod?: AdPod;

}
1 change: 1 addition & 0 deletions packages/core/src/ClientEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export default {
COMPLETE: 'complete',
AD_POD_PREPARATION_REQUESTED: 'adPodPreparationRequested',
AD_POD_INSERTION_REQUESTED: 'adPodInsertionRequested',
AD_POD_INSERTION_REQUEST_FAILED: 'adPodInsertionRequestFailed',

} as const;
9 changes: 6 additions & 3 deletions packages/core/src/IBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import type TBinderState from "./TBinderState";
/** Interface for OMAP binder implementations */
export default interface IOmapBinder {

/** The promise that is resolved when the binder is ready */
// readonly ready?: Promise<void>;

/** The binder's state */
readonly state: TBinderState;

/**
* The tricky media player handler can be provided where some interaction with the media player needs to be done in a tricky way.
* In that case, use the tricky media player handler's methods instead of those of the media player itself.
*/
readonly trickyMediaPlayerHandler?: Object;

/**
* Will bind an OMAP client to the media player that the binder uses.
Expand Down
32 changes: 26 additions & 6 deletions packages/core/src/IClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AdBreak } from "@ygoto3/omap-vmap-parser";
import type { AdBreak as ParserAdBreak } from "@ygoto3/omap-vmap-parser";
import type Ad from "./Ad";
import type AdBreak from "./AdBreak";
import type AdCreative from "./AdCreative";
import type AdPodInsertionRequest from "./AdPodInsertionRequest";
import type OmapClientEvent from "./ClientEvent";
Expand Down Expand Up @@ -57,16 +58,21 @@ export default interface IOmapClient {
*/
notifyAdPlaybackError(ad: Ad): void;

/**
* Use the method to check if the client has ad pod insertion point at the time.
*/
hasAdPodInsertionAt(time: number): boolean;

/**
* Will replace ad insertion decider.
* @param filter the custom ad decider function
*/
attachAdInsertionDecider(
filter: (
currentTime: number,
adBreaks: AdBreak[],
countAdBreakConsumption: (adBreak: AdBreak
) => number) => AdBreak | undefined
adBreaks: ParserAdBreak[],
countAdBreakConsumption: (adBreak: ParserAdBreak) => number,
) => ParserAdBreak | undefined
): void;

/**
Expand Down Expand Up @@ -102,7 +108,7 @@ export default interface IOmapClient {
* @param type the type of event : `OmapClientEvent.LOADED`
* @param listener the listener function
*/
on(type: typeof OmapClientEvent.LOADED, listener: () => void): void;
on(type: typeof OmapClientEvent.LOADED, listener: (adBreaks: AdBreak[]) => void): void;

/**
* Use the on method to listen for the `LOAD_ERROR` event.
Expand Down Expand Up @@ -132,6 +138,13 @@ export default interface IOmapClient {
*/
on(type: typeof OmapClientEvent.AD_POD_INSERTION_REQUESTED, listener: (adPodInsertionRequest: AdPodInsertionRequest) => void): void;

/**
* Use the on method to listen for the `AD_POD_INSERTION_REQUEST_FAILED` event.
* @param type the type of event : `OmapClientEvent.AD_POD_INSERTION_REQUEST_FAILED`
* @param listener the listener function
*/
on(type: typeof OmapClientEvent.AD_POD_INSERTION_REQUEST_FAILED, listener: () => void): void;

/**
* Use the on method to listen for the `AD_POD_PREPARATION_REQUESTED` event.
* @param type the type of event : `OmapClientEvent.AD_POD_PREPARATION_REQUESTED`
Expand Down Expand Up @@ -172,7 +185,7 @@ export default interface IOmapClient {
* @param type the type of event : `OmapClientEvent.LOADED`
* @param listener the listener function
*/
off(type: typeof OmapClientEvent.LOADED, listener: () => void): void;
off(type: typeof OmapClientEvent.LOADED, listener: (adBreaks: AdBreak[]) => void): void;

/**
* Use the off method to remove listener for the `LOAD_ERROR` event.
Expand Down Expand Up @@ -202,6 +215,13 @@ export default interface IOmapClient {
*/
off(type: typeof OmapClientEvent.AD_POD_INSERTION_REQUESTED, listener: (adPodInsertionRequest: AdPodInsertionRequest) => void): void;

/**
* Use the off method to remove listener for the `AD_POD_INSERTION_REQUEST_FAILED` event.
* @param type the type of event : `OmapClientEvent.AD_POD_INSERTION_REQUEST_FAILED`
* @param listener the listener function
*/
off(type: typeof OmapClientEvent.AD_POD_INSERTION_REQUEST_FAILED, listener: () => void): void;

/**
* Use the off method to remove listener for the `AD_POD_PREPARATION_REQUESTED` event.
* @param type the type of event : `OmapClientEvent.AD_POD_PREPARATION_REQUESTED`
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { default as Ad } from "./Ad";
import { default as AdBreak } from "./AdBreak";
import { default as AdCreative } from "./AdCreative";
import { default as AdInformation } from "./AdInformation";
import { default as AdInformationBuilder } from "./AdInformationBuilder";
Expand All @@ -19,6 +20,7 @@ import type { default as TOmapClientEvent } from "./TClientEvent";

export {
Ad,
AdBreak,
AdCreative,AdInformation,
AdInformationBuilder,
AdMediaFile,
Expand Down
2 changes: 1 addition & 1 deletion packages/dashjs-sd-binder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ $ npm install @ygoto3/omap-dashjs-sd-binder --save
Then you can bind any OMAP client to dash.js.

```ts
import { OmapDashjsSDBinder } from ''@ygoto3/omap-dashjs-binder';
import { OmapDashjsSDBinder } from '@ygoto3/omap-dashjs-sd-binder';

const player = dashjs.MediaPlayer().create();
const adDisplayContainer = document.getElementById('ad-display-container') as HTMLDivElement;
Expand Down
11 changes: 6 additions & 5 deletions packages/dashjs-sd-binder/src/DashjsSDBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,29 @@ export default class OmapDashjsSDBinder extends OmapDashjsBinder implements IOma
super(player, adDisplayContainer, adVideoElement);
}

protected lastPlayheadTime: number = NaN;

protected override onContentPauseRequested(): void {
Debug.log("ContentPauseRequested");
super.onContentPauseRequested();
if (this.dashjs.isReady()) {
this._manifest = this.dashjs.getSource();
this._currentTime = this.dashjs.time();
this.lastPlayheadTime = this.dashjs.time();
}
}

protected override onContentResumeRequested(): void {
Debug.log("ContentResumeRequested");
const manifest = this._manifest;
if (manifest === undefined) return;
if (typeof this._manifest === 'undefined') return;
if (this.dashjs.isReady()) {
const manifest = this.dashjs.getDashAdapter().getMpd().manifest;
// Reattach the source
// to resume the content after the ad is played where only a single decoder is available.
this.dashjs.attachSource(manifest, this._currentTime);
this.dashjs.attachSource(manifest, this.lastPlayheadTime);
}
super.onContentResumeRequested();
}

private _currentTime: number = NaN;
private _manifest: string | object | undefined;

}
21 changes: 21 additions & 0 deletions packages/dashjs-sd-sustainable-binder/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Yusuke Goto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
41 changes: 41 additions & 0 deletions packages/dashjs-sd-sustainable-binder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# @ygoto3/omap-dashjs-sd-binder

## Overview

An OMAP binder implementation to bind [dash.js](https://github.com/Dash-Industry-Forum/dash.js) to an OMAP client. This binder is implemented to extend [@ygoto3/omap-dashjs-sd-binder](https://github.com/ygoto3/omap/tree/main/packages/dashjs-sd-binder) in order to playback a video stream in a sustainable way. The binder avoids fetching excessive media data that will be discarded after an ad pod is played, which is what [@ygoto3/omap-dashjs-sd-binder](https://github.com/ygoto3/omap/tree/main/packages/dashjs-sd-binder) does.

## Documentation

[API document](https://ygoto3.github.io/omap/modules/dashjs_sd_sustainable_binder_src.html) is available.

## Getting Started

To install `@ygoto3/omap-dashjs-sd-sustainable-binder`, run the command below.

```sh
$ npm install @ygoto3/omap-dashjs-sd-sustainable-binder --save
```

Then you can bind any OMAP client to dash.js.

```ts
import { OmapDashjsSDSustainableBinder } from '@ygoto3/omap-dashjs-sd-sustainable-binder';

const player = dashjs.MediaPlayer().create();
const adDisplayContainer = document.getElementById('ad-display-container') as HTMLDivElement;
const omapClient = new YourOMAPClient();
const omapBinder = new OmapDashjsSDSustainableBinder(player, adDisplayContainer);
omapBinder.bind(omapClient);
```

## trickyMediaPlayerHandler

You need to use a `trickyMediaPlayerHandler` when you want to seek or to get the duration of the media stream. Binded by OmapDashjsSDSustainableBinder, a dash.js player cannot neither accurately seek nor return the original duration since the OmapDashjsSDSustainableBinder manipulates the manifest in order to avoid downloading media segments that the dash.js player would redundantly download after playing an ad pod.

```ts
// Instead of dashjs.seek(10);
omapBinder.trickyMediaPlayerHandler.seek(10);

// Instead of dashjs.duration;
omapBinder.trickyMediaPlayerHandler.duration;
```
38 changes: 38 additions & 0 deletions packages/dashjs-sd-sustainable-binder/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@ygoto3/omap-dashjs-sd-sustainable-binder",
"version": "0.0.0",
"description": "Open Media Ad Player Dash.js Binder which manipulate the manifest to insert ads",
"main": "./dist/dashjs-sd-sustainable-binder/src/index.js",
"types": "./dist/dashjs-sd-sustainable-binder/src/index.d.ts",
"exports": {
".": {
"types": "./dist/dashjs-sd-sustainable-binder/src/index.d.ts",
"import": "./dist/dashjs-sd-sustainable-binder/src/index.js",
"require": "./dist/dashjs-sd-sustainable-binder/src/index.js"
},
"./package.json": "./package.json"
},
"repository": "https://github.com/ygoto3/omap",
"author": "ygoto3<my.important.apes@gmail.com>",
"license": "MIT",
"scripts": {
"build": "tsc -p tsconfig.json",
"test": "uvu -r ts-node/register tests"
},
"dependencies": {
"@ygoto3/omap-core": "^0.0.1",
"@ygoto3/omap-dashjs-sd-binder": "^0.0.2"
},
"devDependencies": {
"dashjs": "^4.7.2",
"ts-loader": "^9.5.1",
"typescript": "^5.3.3",
"uvu": "^0.5.6",
"webpack": "^5.65.0",
"webpack-cli": "^4.9.1",
"webpack-merge": "^5.10.0"
},
"files": [
"dist"
]
}
Loading

0 comments on commit 0ddbfa6

Please sign in to comment.