Skip to content

Commit

Permalink
feat(FEC-11970): switch from dynamic manifest to static
Browse files Browse the repository at this point in the history
Allow switch from dynamic to static manifest, where the manifest files have different time ranges (shaka can handle a switch but not with mismatched time ranges)
  • Loading branch information
SivanA-Kaltura authored Feb 15, 2023
1 parent 8a75572 commit 14a839d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
57 changes: 50 additions & 7 deletions src/dash-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ export default class DashAdapter extends BaseMediaSourceAdapter {
_isStartOver: boolean = true;
_seekRangeStart: number = 0;
_startOverTimeout: TimeoutID;
_isLive: boolean = false;
_isStaticLive: boolean = false;
_selectedVideoTrack: ?VideoTrack = null;

/**
* Factory method to create media source adapter.
Expand Down Expand Up @@ -253,6 +256,9 @@ export default class DashAdapter extends BaseMediaSourceAdapter {
if (typeof streaming.trackEmsgEvents === 'boolean') {
adapterConfig.trackEmsgEvents = streaming.trackEmsgEvents;
}
if (typeof streaming.switchDynamicToStatic === 'boolean') {
adapterConfig.switchDynamicToStatic = streaming.switchDynamicToStatic;
}
}
if (Utils.Object.hasPropertyPath(config, 'sources.options')) {
const options = config.sources.options;
Expand Down Expand Up @@ -671,20 +677,22 @@ export default class DashAdapter extends BaseMediaSourceAdapter {
/**
* detach media - will remove the media source from handling the video
* @public
* @returns {void}
* @returns {Promise<void>} - detach promise
*/
detachMediaSource(): void {
detachMediaSource(): Promise<void> {
if (this._shaka) {
// 1 second different between duration and current time will signal as end - will enable replay button
if (Math.floor(this.duration - this.currentTime) === 0) {
this._lastTimeDetach = 0;
} else if (this.currentTime > 0) {
this._lastTimeDetach = this.currentTime;
}
this._reset().then(() => {
return this._reset().then(() => {
this._shaka = null;
this._loadPromise = null;
});
} else {
return Promise.resolve();
}
}

Expand Down Expand Up @@ -767,6 +775,13 @@ export default class DashAdapter extends BaseMediaSourceAdapter {
this._parseManifest(response.data);
this._playbackActualUri = response.uri;
this._trigger(EventType.MANIFEST_LOADED, {miliSeconds: response.timeMs});
setTimeout(() => {
this._isLive = this._isLive || this._shaka?.isLive();
if (this._isLive && !this._shaka?.isLive() && !this._isStaticLive && this._config.switchDynamicToStatic) {
this._sourceObj.url = response.uri;
this._switchFromDynamicToStatic();
}
});
break;
}
});
Expand All @@ -784,6 +799,34 @@ export default class DashAdapter extends BaseMediaSourceAdapter {
}, (segmentDuration + 1) * 1000);
}

async _switchFromDynamicToStatic() {
DashAdapter._logger.info('Switching from dynamic manifest to static');
this._dispatchNativeEvent(EventType.WAITING);

const newCurrentTime = this._videoElement.currentTime - this._seekRangeStart;
const isAdaptiveBitrateEnabled = this.isAdaptiveBitrateEnabled();
const isPaused = this._videoElement.paused;

await this.detachMediaSource();

this._isStaticLive = true;
this._isLive = true;
this.attachMediaSource();

await this.load();

this._videoElement.currentTime = newCurrentTime;
if (!isPaused) {
this._videoElement.play();
}

if (isAdaptiveBitrateEnabled) {
this._onAdaptation();
} else if (this._selectedVideoTrack) {
this.selectVideoTrack(this._selectedVideoTrack);
}
}

_setLowLatencyMode() {
this._shaka.configure({
streaming: {
Expand Down Expand Up @@ -890,6 +933,8 @@ export default class DashAdapter extends BaseMediaSourceAdapter {
this._buffering = false;
this._waitingSent = false;
this._playingSent = false;
this._isLive = false;
this._isStaticLive = false;
this._requestFilterError = false;
this._responseFilterError = false;
this._manifestParser = null;
Expand Down Expand Up @@ -1093,6 +1138,7 @@ export default class DashAdapter extends BaseMediaSourceAdapter {
this._trigger(EventType.ABR_MODE_CHANGED, {mode: 'manual'});
}
if (!selectedVideoTrack.active) {
this._selectedVideoTrack = videoTrack;
this._shaka.selectVariantTrack(videoTracks[videoTrack.index], true);
this._onTrackChanged(videoTrack);
}
Expand Down Expand Up @@ -1226,10 +1272,7 @@ export default class DashAdapter extends BaseMediaSourceAdapter {
* @public
*/
isLive(): boolean {
if (this._shaka) {
return this._shaka.isLive();
}
return false;
return this._shaka?.isLive() || this._isLive;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/default-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
}
},
"forceRedirectExternalStreams": false,
"trackEmsgEvents": true
"trackEmsgEvents": true,
"switchDynamicToStatic": false
}

0 comments on commit 14a839d

Please sign in to comment.