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(FEC-11632): expose stream timed metadata #509

Merged
merged 3 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
64 changes: 37 additions & 27 deletions src/common/cuepoint/cuepoint-manager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@flow
import {Cue, FakeEvent, TextTrack, EventType} from '@playkit-js/playkit-js';
import {FakeEvent, TextTrack, EventType, createTextTrackCue} from '@playkit-js/playkit-js';
import {CUE_POINTS_TEXT_TRACK, CUE_POINT_KEY} from './cuepoint-type';

interface CuePoint {
Expand All @@ -20,43 +20,52 @@ export class CuePointManager {
this._textTrack = this._player.addTextTrack(TextTrack.KIND.METADATA, CUE_POINTS_TEXT_TRACK);
}

_createTextTrackCue(data: CuePoint): window.VTTCue | typeof Cue {
let cue = {};
if (window.VTTCue) {
cue = new window.VTTCue(data.startTime, data.endTime, '');
} else if (window.TextTrackCue) {
// IE11 support
cue = new Cue(data.startTime, data.endTime, '');
}
const cueValue = {key: CUE_POINT_KEY, data};
cue.id = data.id;
cue.value = cueValue;
return cue;
_getMetadataTracks(): Array<TextTrack> {
return this._player.getTextTracks().filter(track => track.kind === TextTrack.KIND.METADATA);
}

getAllCuePoints() {
return this._textTrack?.cues || [];
_createTextTrackCue(data: CuePoint): TextTrackCue {
const {startTime, endTime, id} = data;
return createTextTrackCue({startTime, endTime, id, type: CUE_POINT_KEY, metadata: data});
}

getActiveCuePoints() {
return this._textTrack?.activeCues || [];
_cuesSorter(a: TextTrackCue, b: TextTrackCue): number {
return a.startTime - b.startTime;
}

getCuePointById(id: string) {
return this._textTrack?.cues?.getCueById(id);
getAllCuePoints(): Array<TextTrackCue> {
const metadataTracks = this._getMetadataTracks();
return metadataTracks.reduce((cues, track) => cues.concat(...track.cues), []).sort(this._cuesSorter);
}

removeCuePoint(cuePoint: window.VTTCue | typeof Cue) {
this._textTrack?.removeCue(cuePoint);
getActiveCuePoints(): Array<TextTrackCue> {
const metadataTracks = this._getMetadataTracks();
return metadataTracks.reduce((cues, track) => cues.concat(...track.activeCues), []).sort(this._cuesSorter);
}

getCuePointById(id: string): ?TextTrackCue {
const metadataTracks = this._getMetadataTracks();
metadataTracks.forEach(track => {
const cuePoint = track.cues.getCueById(id);
if (cuePoint) {
return cuePoint;
}
});
}

removeCuePoint(cuePoint: TextTrackCue) {
const metadataTracks = this._getMetadataTracks();
metadataTracks.forEach(track => {
track.removeCue(cuePoint);
});
}

addCuePoints(data: CuePoint[]) {
this._player.ready().then(() => {
if (!this._textTrack) {
this._addTextTrack();
}
const newCuePoints: window.VTTCue | typeof Cue = [];

const newCuePoints: Array<TextTrackCue> = [];
data.forEach((cuePoint: CuePoint) => {
const textTrackCue = this._createTextTrackCue(cuePoint);
const exisedCue = this.getCuePointById(textTrackCue.id);
Expand All @@ -71,11 +80,12 @@ export class CuePointManager {
}

clearAllCuePoints() {
if (this._textTrack && this._textTrack.cues.length) {
while (this._textTrack.cues.length) {
this.removeCuePoint(this._textTrack.cues[0]);
const metadataTracks = this._getMetadataTracks();
metadataTracks.forEach(track => {
while (track.cues.length) {
this.removeCuePoint(track.cues[0]);
}
}
});
}

reset() {
Expand Down
10 changes: 10 additions & 0 deletions src/kaltura-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,16 @@ class KalturaPlayer extends FakeEventTarget {
return this._localPlayer.addTextTrack(kind, label);
}

/**
* get text tracks
* @function getTextTracks
* @returns {Array<TextTrack>} - The TextTracks array.
* @public
*/
getTextTracks(): Array<TextTrack> {
return this._localPlayer.getTextTracks();
}

get remotePlayerManager() {
return this._remotePlayerManager;
}
Expand Down