298 lines
10 KiB
Plaintext
298 lines
10 KiB
Plaintext
import "pkg:/components/Dialog/DialogUtils.bs"
|
|
import "pkg:/components/Services/Invidious/InvidiousToContentNode.bs"
|
|
import "pkg:/components/VideoPlayer/MarkVideoWatchedTask.bs"
|
|
import "pkg:/components/VideoPlayer/SponsorBlock.bs"
|
|
import "pkg:/components/VideoPlayer/VideoContentTask.bs"
|
|
import "pkg:/components/VideoPlayer/VideoPlayerStyle.bs"
|
|
import "pkg:/components/VideoPlayer/VideoUtils.bs"
|
|
import "pkg:/source/asyncTask/asyncTask.bs"
|
|
import "pkg:/source/utils/ErrorUtils.bs"
|
|
import "pkg:/source/utils/Logging.bs"
|
|
|
|
function Init()
|
|
SetPlayerStyle()
|
|
SetupAnimation()
|
|
|
|
' videoPlayingSuccess indicates if we started playing a video successfully
|
|
' We use it to detect if we should try another link for a video, or if another error
|
|
' occurred (such as a next work error)
|
|
m.videoPlayingSuccess = false
|
|
' ignoreNextFinishedState indicates if we should ignore the next finished state
|
|
' This is used to prevent the video from playing the next video when we are
|
|
' still trying to find a working stream url
|
|
m.ignoreNextFinishedState = false
|
|
end function
|
|
|
|
function OnNodeReady()
|
|
rect = m.container.fullscreen ? m.top.maxRect : m.top.minRect
|
|
|
|
m.top.width = rect.width
|
|
m.top.height = rect.height
|
|
m.top.translation = [rect.x, rect.y]
|
|
|
|
' TODO:P2 show a loading spinner if video is loading minimized
|
|
m.top.enableUI = m.container.fullscreen
|
|
|
|
m.container.observeFieldScoped("fullscreen", FuncName(OnFullScreenChange))
|
|
m.top.ObserveField("state", FuncName(OnVideoPlayerStateChange))
|
|
m.top.observeField("control", FuncName(OnControlChange))
|
|
|
|
SponsorBlock.SetupSponsorBlock()
|
|
end function
|
|
|
|
function PlayWithContent(contentNode as object)
|
|
videoContentNode = CreateObject("roSGNode", "ContentNode")
|
|
|
|
if not StringUtils.IsNullOrEmpty(contentNode.videoId)
|
|
videoContentNode.AddFields({ videoId: contentNode.videoId })
|
|
end if
|
|
if IsInt(contentNode.timestamp)
|
|
videoContentNode.AddFields({ timestamp: contentNode.timestamp })
|
|
end if
|
|
if not StringUtils.IsNullOrEmpty(contentNode.title)
|
|
videoContentNode.title = contentNode.title
|
|
end if
|
|
if not StringUtils.IsNullOrEmpty(contentNode.author)
|
|
videoContentNode.secondaryTitle = contentNode.author
|
|
end if
|
|
|
|
StartVideoContentTask(videoContentNode)
|
|
end function
|
|
|
|
function StartVideoContentTask(videoContentNode as object)
|
|
if m.videoContentTask <> invalid
|
|
m.videoContentTask.cancel = true
|
|
end if
|
|
|
|
MarkVideoWatched(videoContentNode.videoId)
|
|
|
|
m.top.content = videoContentNode
|
|
|
|
m.videoContentTask = StartAsyncTask(VideoContentTask, {
|
|
content: videoContentNode,
|
|
invidious: m.top.invidious,
|
|
preferences: m.top.preferences,
|
|
playletServerUrl: m.top.webServer@.GetServerAddress(invalid)
|
|
}, OnVideoContentTaskResults)
|
|
end function
|
|
|
|
function OnVideoContentTaskResults(output as object) as void
|
|
m.videoContentTask = invalid
|
|
if output.cancelled
|
|
return
|
|
end if
|
|
|
|
if not output.success or not output.result.success
|
|
' output.error for unhandled exception
|
|
error = output.error
|
|
if error = invalid
|
|
' output.result.error for network errors
|
|
error = output.result.error
|
|
end if
|
|
error = ErrorUtils.Format(error)
|
|
LogError(error)
|
|
videoId = output.task.input.content.videoId
|
|
message = `Failed to load video information for ${videoId}\n${error}`
|
|
DialogUtils.ShowDialog(message, "Video load fail", true)
|
|
' TODO:P1 play next video in the queue after showing the error dialog
|
|
VideoUtils.CloseVideo()
|
|
return
|
|
end if
|
|
|
|
SponsorBlock.FetchSponsorBlock(m.top.content.metadata)
|
|
|
|
m.top.control = "play"
|
|
if ValidInt(m.top.content.timestamp) > 0
|
|
m.top.seek = m.top.content.timestamp
|
|
end if
|
|
end function
|
|
|
|
function MarkVideoWatched(videoId as string)
|
|
if m.top.invidious.authToken <> invalid
|
|
StartAsyncTask(MarkVideoWatchedTask, {
|
|
videoId: videoId,
|
|
invidious: m.top.invidious
|
|
})
|
|
end if
|
|
end function
|
|
|
|
function Close(unused as dynamic)
|
|
m.top.control = "stop"
|
|
m.top.content = invalid
|
|
parent = m.top.getParent()
|
|
if parent <> invalid
|
|
parent.RemoveChild(m.top)
|
|
end if
|
|
SponsorBlock.RemoveNotifcation(m.notifications)
|
|
end function
|
|
|
|
function OnControlChange()
|
|
if m.top.control = "stop"
|
|
if m.videoContentTask <> invalid
|
|
m.videoContentTask.cancel = true
|
|
m.videoContentTask = invalid
|
|
end if
|
|
if m.sponsorBlockTask <> invalid
|
|
m.sponsorBlockTask.cancel = true
|
|
m.sponsorBlockTask = invalid
|
|
end if
|
|
end if
|
|
end function
|
|
|
|
function OnVideoPlayerStateChange() as void
|
|
state = m.top.state
|
|
content = m.top.content
|
|
' If we successfully played the video, then any error that comes later is not due to a 403 (FORBIDDEN).
|
|
' This is to reduce false positives, as we do not want retry another link if the first link is working fine.
|
|
if state = "playing" and not m.videoPlayingSuccess
|
|
LogInfo(`Now playing video ${content.url}`)
|
|
m.videoPlayingSuccess = true
|
|
end if
|
|
|
|
if state = "error"
|
|
errorInfo = m.top.errorInfo
|
|
LogError(`Error playing video ${content.videoId}`, errorInfo)
|
|
if m.videoPlayingSuccess <> true
|
|
' http for stream format, mediaerror for DASH
|
|
if errorInfo.category = "http" or errorInfo.category = "mediaerror"
|
|
currentUrl = content.url
|
|
playletStreamUrls = content.playletStreamUrls
|
|
playletStreamUrlIndex = content.playletStreamUrlIndex
|
|
playletStreamUrlIndex++
|
|
content.playletStreamUrlIndex = playletStreamUrlIndex
|
|
if playletStreamUrlIndex < playletStreamUrls.Count()
|
|
nextUrl = playletStreamUrls[playletStreamUrlIndex]
|
|
LogError(`Stream ${currentUrl} failed to play. Trying next stream ${nextUrl}`)
|
|
|
|
content.url = nextUrl
|
|
' 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
|
|
m.top.control = "play"
|
|
|
|
if ValidInt(content.timestamp) > 0
|
|
m.top.seek = content.timestamp
|
|
end if
|
|
return
|
|
end if
|
|
end if
|
|
end if
|
|
end if
|
|
|
|
if state = "finished" and m.ignoreNextFinishedState = true
|
|
m.ignoreNextFinishedState = false
|
|
return
|
|
end if
|
|
|
|
if state = "finished"
|
|
metadata = content?.metadata
|
|
if not PlayNext(metadata?.recommendedVideos)
|
|
m.container@.watchedVideosClear(invalid)
|
|
VideoUtils.CloseVideo()
|
|
end if
|
|
end if
|
|
|
|
if state = "error"
|
|
messageLines = []
|
|
messageLines.push(`errorMsg: ${m.top.errorMsg}`)
|
|
messageLines.push(`errorCode: ${m.top.errorCode}`)
|
|
messageLines.push(`errorStr: ${m.top.errorStr}`)
|
|
|
|
errorInfo = m.top.errorInfo
|
|
if errorInfo <> invalid
|
|
for each info in errorInfo
|
|
messageLines.push(`${info}: ${errorInfo[info]}`)
|
|
end for
|
|
end if
|
|
|
|
title = `Error playing video ${content.videoId}`
|
|
LogError(title, messageLines)
|
|
DialogUtils.ShowDialog(messageLines, title, true)
|
|
' TODO:P1 play next video in the queue after showing the error dialog
|
|
VideoUtils.CloseVideo()
|
|
end if
|
|
end function
|
|
|
|
function PlayNext(recommendedVideos as object) as boolean
|
|
if m.playQueue@.PlayNextItem(invalid)
|
|
return true
|
|
end if
|
|
|
|
return PlayNextRecommendedVideo(recommendedVideos)
|
|
end function
|
|
|
|
function PlayNextRecommendedVideo(recommendedVideos as object) as boolean
|
|
if m.preferences["playback.autoplay"] <> true
|
|
return false
|
|
end if
|
|
|
|
if recommendedVideos = invalid or recommendedVideos.Count() = 0
|
|
return false
|
|
end if
|
|
|
|
for each metadata in recommendedVideos
|
|
videoId = metadata?.videoId
|
|
if videoId = invalid
|
|
continue for
|
|
end if
|
|
if not m.container@.watchedVideosContain(videoId)
|
|
LogInfo(`Playing next recommended video (${videoId})`)
|
|
node = InvidiousContent.ToVideoContentNode(invalid, metadata, invalid)
|
|
VideoUtils.PlayVideo(node)
|
|
return true
|
|
end if
|
|
end for
|
|
|
|
return false
|
|
end function
|
|
|
|
function OnkeyEvent(key as string, press as boolean) as boolean
|
|
if press = false
|
|
return false
|
|
end if
|
|
' Unfortunately, a Video node cannot capture the "options" key (because, Roku...)
|
|
' https://community.roku.com/t5/Roku-Developer-Program/Bug-in-10-0-1-Options-key-is-not-being-consumed-by-onKeyEvent-when-Video-node-is-in-focus/m-p/709200/highlight/true#M49312
|
|
' Because of that, the button "down" is used to shrink the video for picture in picture mode
|
|
if key = "down"
|
|
if VideoUtils.ToggleVideoPictureInPicture()
|
|
return true
|
|
end if
|
|
end if
|
|
|
|
if key = "back"
|
|
m.container@.watchedVideosClear(invalid)
|
|
VideoUtils.CloseVideo()
|
|
return true
|
|
end if
|
|
return false
|
|
end function
|
|
|
|
function SetupAnimation()
|
|
m.minimizeAnimation = m.top.findNode("minimizeAnimation")
|
|
m.widthInterpolator = m.top.findNode("widthInterpolator")
|
|
m.heightInterpolator = m.top.findNode("heightInterpolator")
|
|
m.translationInterpolator = m.top.findNode("translationInterpolator")
|
|
|
|
minRect = m.top.minRect
|
|
maxRect = m.top.maxRect
|
|
|
|
m.widthInterpolator.keyValue = [maxRect.width, (maxRect.width + minRect.width) / 2, minRect.width]
|
|
m.heightInterpolator.keyValue = [maxRect.height, (maxRect.height + minRect.height) / 2, minRect.height]
|
|
maxTranslation = [maxRect.x, maxRect.y]
|
|
minTranslation = [minRect.x, minRect.y]
|
|
midTranslation = [(maxRect.x + minRect.x) / 2, (maxRect.y + minRect.y) / 2]
|
|
m.translationInterpolator.keyValue = [maxTranslation, midTranslation, minTranslation]
|
|
end function
|
|
|
|
function OnFullScreenChange()
|
|
m.widthInterpolator.reverse = m.container.fullscreen
|
|
m.heightInterpolator.reverse = m.container.fullscreen
|
|
m.translationInterpolator.reverse = m.container.fullscreen
|
|
|
|
m.minimizeAnimation.control = "start"
|
|
|
|
m.top.enableUI = m.container.fullscreen
|
|
|
|
SponsorBlock.SetVisible(m.notifications, m.container.fullscreen)
|
|
end function
|