Skip to content

Commit

Permalink
Feature/preload with start time (#4169)
Browse files Browse the repository at this point in the history
* WiP: Preloading with start time

* Fix adjusting the seek target when preloading streams

* Fix reset of providedStartTime
  • Loading branch information
dsilhavy committed Apr 13, 2023
1 parent 36af412 commit 9a8f765
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/streaming/MediaPlayer.js
Expand Up @@ -132,6 +132,7 @@ function MediaPlayer() {
streamingInitialized,
playbackInitialized,
autoPlay,
providedStartTime,
abrController,
schemeLoaderFactory,
timelineConverter,
Expand Down Expand Up @@ -176,6 +177,7 @@ function MediaPlayer() {
playbackInitialized = false;
streamingInitialized = false;
autoPlay = true;
providedStartTime = NaN;
protectionController = null;
offlineController = null;
protectionData = null;
Expand Down Expand Up @@ -552,7 +554,7 @@ function MediaPlayer() {
return;
}
if (source) {
_initializePlayback();
_initializePlayback(providedStartTime);
} else {
throw SOURCE_NOT_ATTACHED_ERROR;
}
Expand Down Expand Up @@ -1427,15 +1429,15 @@ function MediaPlayer() {
_detectMss();

if (streamController) {
streamController.switchToVideoElement();
streamController.switchToVideoElement(providedStartTime);
}
}

if (playbackInitialized) { //Reset if we have been playing before, so this is a new element.
_resetPlaybackControllers();
}

_initializePlayback();
_initializePlayback(providedStartTime);
}

/**
Expand Down Expand Up @@ -1893,14 +1895,15 @@ function MediaPlayer() {
startTime = Math.max(0, startTime);
}

providedStartTime = startTime;
source = urlOrManifest;

if (streamingInitialized || playbackInitialized) {
_resetPlaybackControllers();
}

if (isReady()) {
_initializePlayback(startTime);
_initializePlayback(providedStartTime);
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/streaming/controllers/BufferController.js
Expand Up @@ -75,6 +75,7 @@ function BufferController(config) {
maximumIndex,
sourceBufferSink,
dischargeBuffer,
isPrebuffering,
dischargeFragments,
bufferState,
appendedBytesInfo,
Expand Down Expand Up @@ -184,6 +185,7 @@ function BufferController(config) {
return;
}
if (mediaSource) {
isPrebuffering = false;
_initializeSinkForMseBuffering(mediaInfo, oldBufferSinks)
.then((sink) => {
resolve(sink);
Expand All @@ -192,6 +194,7 @@ function BufferController(config) {
reject(e);
})
} else {
isPrebuffering = true;
_initializeSinkForPrebuffering()
.then((sink) => {
resolve(sink);
Expand Down Expand Up @@ -429,7 +432,7 @@ function BufferController(config) {
* @private
*/
function _adjustSeekTarget() {
if (isNaN(seekTarget)) return;
if (isNaN(seekTarget) || isPrebuffering) return;
// Check buffered data only for audio and video
if (type !== Constants.AUDIO && type !== Constants.VIDEO) {
seekTarget = NaN;
Expand Down Expand Up @@ -828,8 +831,13 @@ function BufferController(config) {

function _updateBufferLevel() {
if (playbackController) {
let referenceTime = playbackController.getTime() || 0;
// In case we are prebuffering we dont have a current time yet
if (isPrebuffering) {
referenceTime = !isNaN(seekTarget) ? seekTarget : 0;
}
const tolerance = settings.get().streaming.gaps.jumpGaps && !isNaN(settings.get().streaming.gaps.smallGapLimit) ? settings.get().streaming.gaps.smallGapLimit : NaN;
bufferLevel = Math.max(getBufferLength(playbackController.getTime() || 0, tolerance), 0);
bufferLevel = Math.max(getBufferLength(referenceTime, tolerance), 0);
_triggerEvent(Events.BUFFER_LEVEL_UPDATED, { mediaType: type, bufferLevel: bufferLevel });
checkIfSufficientBuffer();
}
Expand Down Expand Up @@ -1192,6 +1200,7 @@ function BufferController(config) {
wallclockTicked = 0;
pendingPruningRanges = [];
seekTarget = NaN;
isPrebuffering = false;

if (sourceBufferSink) {
let tmpSourceBufferSinkToReset = sourceBufferSink;
Expand Down
2 changes: 1 addition & 1 deletion src/streaming/controllers/PlaybackController.js
Expand Up @@ -205,7 +205,7 @@ function PlaybackController() {
* @param {boolean} adjustLiveDelay
*/
function seek(time, stickToBuffered = false, internal = false, adjustLiveDelay = false) {
if (!streamInfo || !videoModel) return;
if (!streamInfo || !videoModel || !videoModel.getElement()) return;

let currentTime = !isNaN(seekTarget) ? seekTarget : videoModel.getTime();
if (time === currentTime) return;
Expand Down
3 changes: 3 additions & 0 deletions src/streaming/controllers/StreamController.js
Expand Up @@ -440,6 +440,9 @@ function StreamController() {
const dvrInfo = dashMetrics.getCurrentDVRInfo();
mediaSourceController.setSeekable(dvrInfo.range.start, dvrInfo.range.end);
if (streamActivated) {
if (!isNaN(seekTime)) {
playbackController.seek(seekTime, true, true);
}
// Set the media source for all StreamProcessors
activeStream.setMediaSource(mediaSource)
.then(() => {
Expand Down

0 comments on commit 9a8f765

Please sign in to comment.