diff --git a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs index f5eee09b..df930c20 100644 --- a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs +++ b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs @@ -194,28 +194,70 @@ function OnVideoContentTaskResults(output as object) as void content = m.top.content SponsorBlock.FetchSponsorBlock(content.metadata) - timestamp = content.timestamp - percentDurationWatched = content.percentDurationWatched - hasTimestamp = IsInt(timestamp) - hasPercentDurationWatched = IsFloat(percentDurationWatched) - - if IsLiveVideo() and not hasTimestamp and not hasPercentDurationWatched - ' If live, seek to end of stream - m.seekToPercent = 100.0 - SeekToPercentOfDuration() - else - if hasTimestamp and timestamp > 0 - m.top.seek = content.timestamp - else if hasPercentDurationWatched - m.seekToPercent = percentDurationWatched - SeekToPercentOfDuration() - end if - end if + SeekToInitialPosition(content) PlayAfterPreviousPlayerHasStopped() MarkVideoWatched(content.videoId) end function +function SeekToInitialPosition(content as object) as void + if content = invalid + return + end if + + ' For trickplay of live streams, apps should seek to a position of + ' 999,999 seconds (for both DASH and HLS streams). The Roku media + ' player clips that position to the current availability window and + ' provides a robust buffer. + ' https://developer.roku.com/en-ca/docs/specs/media/streaming-specifications.md + if content.Live = true + LogInfo("Seeking to enge of live stream") + m.top.seek = 999999 + return + end if + + timestamp = ValidInt(content.timestamp) + if timestamp > 0 + LogInfo("Seeking to timestamp:", timestamp) + m.top.seek = content.timestamp + return + end if + + percentDurationWatched = ValidFloat(content.percentDurationWatched) + if percentDurationWatched > 0 + m.seekToPercent = percentDurationWatched + SeekToPercentOfDuration() + end if +end function + +function SeekToPercentOfDuration() as void + m.top.UnobserveField("duration") + + seekToPercent = m.seekToPercent + if seekToPercent = invalid + return + end if + + duration = m.top.duration + if duration = 0 + ' If the duration is not available, wait for it to be set + m.top.ObserveField("duration", FuncName(SeekToPercentOfDuration)) + return + end if + + ' If the stream is less than 30 seconds, it's not worth seeking + if duration < 30 + return + end if + + m.seekToPercent = invalid + seek = (MathUtils.Clamp(seekToPercent, 0, 100) / 100.0) * duration + if seek > 0 + LogInfo("Seeking to percent duration watched", seek) + m.top.seek = seek + end if +end function + function PlayAfterPreviousPlayerHasStopped() as void previousPlayer = m.top.previousPlayer m.top.previousPlayer = invalid @@ -393,10 +435,10 @@ function OnVideoPlayerStateChange() as void nextUrl = playletStreamUrls[playletStreamUrlIndex] LogError(`Stream ${currentUrl} failed to play. Trying next stream ${nextUrl}`) - ' TODO:P2 handle SeekToEndOfStream for live videos on error - m.top.UnobserveField("duration") - content.url = nextUrl + + SeekToInitialPosition(content) + ' This video errored, and is about to finish, so don't close the video yet ' That's why we should ignore the next "finished" event m.ignoreNextFinishedState = true @@ -452,46 +494,6 @@ function OnVideoPlayerStateChange() as void end if end function -function SeekToEndOfStream() as void - m.top.UnobserveField("duration") - duration = m.top.duration - ' If the stream is less than 30 seconds, it's not worth seeking - if duration < 30 - return - end if - - LogInfo("Seeking to end of live stream", duration) - m.top.seek = duration -end function - -function SeekToPercentOfDuration() as void - m.top.UnobserveField("duration") - - seekToPercent = m.seekToPercent - if seekToPercent = invalid - return - end if - - duration = m.top.duration - if duration = 0 - ' If the duration is not available, wait for it to be set - m.top.ObserveField("duration", FuncName(SeekToPercentOfDuration)) - return - end if - - ' If the stream is less than 30 seconds, it's not worth seeking - if duration < 30 - return - end if - - m.seekToPercent = invalid - seek = (MathUtils.Clamp(seekToPercent, 0, 100) / 100.0) * duration - if seek > 0 - LogInfo("Seeking to percent duration watched", seek) - m.top.seek = seek - end if -end function - function OnVideoFinished() as void if not VideoQueueUtils.PlayNext(m.videoQueue, m.invidious, m.preferences) m.videoQueue.closePlayer = true