Files
playlet-piped/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs
T

342 lines
12 KiB
Plaintext

import "pkg:/source/utils/TimeUtils.bs"
import "pkg:/source/services/SponsorBlock.bs"
import "pkg:/components/VideoPlayer/Video.bs"
import "pkg:/components/VideoPlayer/VideoDetailsTask.bs"
function Init()
m.videoContainer = GetVideoContainer()
SetupSponsorBlock()
SetupAnimation()
SetupBusySpinner()
m.top.trickPlayBar.filledBarBlendColor = "0xFF0000FF"
m.top.bufferingBar.filledBarBlendColor = "0xFF0000FF"
m.top.retrievingBar.filledBarBlendColor = "0xFF0000FF"
m.top.enableUI = m.videoContainer.fullscreen
m.videoContainer.observeFieldScoped("fullscreen", "OnFullScreenChange")
m.videoPlayingSuccess = false
m.ignoreNextFinishedState = false
m.top.observeField("control", "OnControlChange")
m.top.observeField("videoId", "StartVideoDetailsTask")
m.top.ObserveField("state", "OnVideoPlayerStateChange")
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 ToggleVideoPictureInPicture()
return true
end if
end if
if key = "back"
m.videoContainer@.watchedVideosClear()
CloseVideo()
return true
end if
return false
end function
function OnControlChange()
if m.top.control = "stop"
if m.videoDetailsTask <> invalid
m.videoDetailsTask.control = "stop"
end if
if m.sponsorBlockTask <> invalid
m.sponsorBlockTask.control = "stop"
end if
end if
end function
function SetMetadata() as void
if m.top.metadata = invalid
return
end if
metadata = m.top.metadata
contentNode = CreateObject("roSGNode", "ContentNode")
contentNode.addFields({ metadata: metadata })
contentNode.title = metadata.title
contentNode.secondaryTitle = metadata.author
m.top.content = contentNode
end function
function StartVideoDetailsTask()
if m.videoDetailsTask <> invalid
m.videoDetailsTask.control = "stop"
end if
SetMetadata()
m.videoDetailsTask = StartAsyncTask(VideoDetailsTask, { videoId: m.top.videoId }, OnVideoDetailsTaskResults)
end function
function OnVideoDetailsTaskResults(output as object) as void
videoId = output.result.videoId
metadata = output.result.metadata
if videoId <> m.top.videoId
return
end if
if not metadata.success
m.busySpinner.visible = false
m.busySpinner.control = "stop"
message = `Failed to load video information for ${videoId}`
message += `\nStatus Code: ${metadata.statusCode}`
if isstr(metadata.data)
message += `\nError: ${metadata.data}`
else if isAssocArray(metadata.data)
message += `\nError: ${FormatJson(metadata.data)}`
end if
ShowDialog(message, "Video load fail")
CloseVideo()
return
end if
metadata = metadata.data
playletStreamUrls = []
if metadata.hlsUrl <> invalid
hlsUrl = metadata.hlsUrl
playletStreamUrls.push(hlsUrl)
if hlsUrl.InStr("?") = -1
hlsUrlLocal = hlsUrl + "?local=true"
else
hlsUrlLocal = hlsUrl + "&local=true"
end if
playletStreamUrls.push(hlsUrlLocal)
else
if metadata.formatStreams.Count() > 0
stream = metadata.formatStreams[metadata.formatStreams.Count() - 1]
itag = stream.itag
playletStreamUrls.push(Invidious.GetVideoUrl(videoId, itag, false))
playletStreamUrls.push(Invidious.GetVideoUrl(videoId, itag, true))
end if
if metadata.dashUrl <> invalid
#if DASH_THUMBNAILS
' Redirect to our server so we can inject thumbnails (storyboards) into the DASH manifest
dashUrl = `http://${GetLocalIpAddress()}:8888/dash?v=${videoId}`
#else
dashUrl = metadata.dashUrl
#end if
playletStreamUrls.push(dashUrl)
if dashUrl.InStr("?") = -1
dashUrlLocal = dashUrl + "?local=true"
else
dashUrlLocal = dashUrl + "&local=true"
end if
playletStreamUrls.push(dashUrlLocal)
end if
end if
metadata.playletStreamUrls = playletStreamUrls
metadata.playletStreamUrlIndex = 0
contentNode = CreateObject("roSGNode", "ContentNode")
contentNode.addFields({ metadata: metadata })
contentNode.title = metadata.title
contentNode.secondaryTitle = metadata.author
SetCaptions(metadata, m.top, contentNode)
contentNode.url = metadata.playletStreamUrls[metadata.playletStreamUrlIndex]
m.busySpinner.visible = false
m.busySpinner.control = "stop"
m.top.content = contentNode
m.top.control = "play"
m.videoContainer@.watchedVideosAdd(videoId)
end function
function FindChildNodeOfType(node as object, nodeType as string) as object
if node.subtype() = nodeType
return node
end if
childCount = node.getChildCount()
children = node.getChildren(childCount, 0)
for i = 0 to childCount - 1
child = FindChildNodeOfType(children[i], nodeType)
if child <> invalid
return child
end if
end for
return invalid
end function
function SetupBusySpinner()
' busySpinner is our spinner, shown when loading the video details (like links, title, etc)
m.busySpinner = m.top.findNode("BusySpinner")
' Once the Content is ready, the rokuBusySpinner shows up, which is loading the video feed
m.rokuBusySpinner = FindChildNodeOfType(m.top, "BusySpinner")
' The label that shows the buffering percentage
m.rokuBusySpinnerLabel = FindChildNodeOfType(m.rokuBusySpinner, "Label")
m.rokuBusySpinner.poster.width = 150
m.rokuBusySpinner.poster.height = 150
m.rokuBusySpinner.poster.uri = "pkg:/images/spinner.png"
m.rokuBusySpinner.observeField("translation", "OnRokuSpinnerMoved")
m.top.observeField("width", "OnRokuSpinnerMoved")
m.top.observeField("height", "OnRokuSpinnerMoved")
m.top.observeField("translation", "OnRokuSpinnerMoved")
m.rokuBusySpinnerLabel.width = 150
m.rokuBusySpinnerLabel.height = 150
m.rokuBusySpinnerLabel.translation = [0, 0]
m.rokuBusySpinnerLabel.observeField("translation", "OnRokuSpinnerLabelMoved")
m.top.observeField("width", "OnRokuSpinnerLabelMoved")
m.top.observeField("height", "OnRokuSpinnerLabelMoved")
m.top.observeField("translation", "OnRokuSpinnerLabelMoved")
m.busySpinner.poster.width = 150
m.busySpinner.poster.height = 150
m.busySpinner.poster.uri = "pkg:/images/spinner.png"
m.top.observeField("width", "PositionSpinner")
m.top.observeField("height", "PositionSpinner")
m.top.observeField("translation", "PositionSpinner")
PositionSpinner()
m.busySpinner.visible = true
m.busySpinner.control = "start"
end function
function OnRokuSpinnerMoved()
currentTranslation = m.rokuBusySpinner.translation
parentRect = m.top.boundingRect()
centerx = (parentRect.width - m.rokuBusySpinner.poster.width) / 2
centery = (parentRect.height - m.rokuBusySpinner.poster.height) / 2
if currentTranslation[0] <> centerx or currentTranslation[1] <> centery
m.rokuBusySpinner.translation = [centerx, centery]
end if
end function
function OnRokuSpinnerLabelMoved()
currentTranslation = m.rokuBusySpinnerLabel.translation
if currentTranslation[0] <> 0 or currentTranslation[1] <> 0
m.rokuBusySpinnerLabel.translation = [0, 0]
end if
end function
function PositionSpinner()
centerx = (m.top.width - m.busySpinner.poster.width) / 2
centery = (m.top.height - m.busySpinner.poster.height) / 2
m.busySpinner.translation = [centerx, centery]
end function
function OnVideoPlayerStateChange() as void
state = m.top.state
' 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"
m.videoPlayingSuccess = true
end if
if state = "error"
' A hack to see if we could use the proxy here
if m.videoPlayingSuccess <> true
errorInfo = m.top.errorInfo
' http for stream format, mediaerror for DASH
if errorInfo.category = "http" or errorInfo.category = "mediaerror"
currentUrl = m.top.content.metadata.playletStreamUrls[m.top.content.metadata.playletStreamUrlIndex]
metadata = m.top.content.metadata
metadata.playletStreamUrlIndex++
m.top.content.metadata = metadata
if m.top.content.metadata.playletStreamUrlIndex < m.top.content.metadata.playletStreamUrls.Count()
nextUrl = m.top.content.metadata.playletStreamUrls[m.top.content.metadata.playletStreamUrlIndex]
print(`Stream ${currentUrl} failed to play. Trying next stream ${nextUrl}`)
m.top.content.url = nextUrl
' This video errored, and is about to finish, so don't close the video yet
' TODO: perhaps creating a second player is better?
m.ignoreNextFinishedState = true
m.top.control = "play"
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 = m.top.content?.metadata
if not PlayNextRecommendedVideo(metadata?.recommendedVideos)
CloseVideo()
end if
end if
if state = "error"
errorInfo = m.top.errorInfo
messageLines = [`VideoId: ${m.top.videoId}`]
for each info in errorInfo
messageLines.push(`${info}: ${errorInfo[info]}`)
end for
ShowDialog(messageLines, "Error playing video")
CloseVideo()
end if
end function
function PlayNextRecommendedVideo(recommendedVideos as object) as boolean
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.videoContainer@.watchedVideosContain(videoId)
PlayVideo(videoId, { metadata: metadata })
return true
end if
end for
return false
end function
function SetupAnimation()
m.widthAnimation = m.top.findNode("widthAnimation")
m.widthAnimationInterpolator = m.top.findNode("widthAnimationInterpolator")
m.heightAnimation = m.top.findNode("heightAnimation")
m.heightAnimationInterpolator = m.top.findNode("heightAnimationInterpolator")
m.translationAnimation = m.top.findNode("translationAnimation")
m.translationAnimationInterpolator = m.top.findNode("translationAnimationInterpolator")
end function
function OnFullScreenChange()
m.widthAnimationInterpolator.reverse = m.videoContainer.fullscreen
m.heightAnimationInterpolator.reverse = m.videoContainer.fullscreen
m.translationAnimationInterpolator.reverse = m.videoContainer.fullscreen
m.widthAnimation.control = "start"
m.heightAnimation.control = "start"
m.translationAnimation.control = "start"
m.top.enableUI = m.videoContainer.fullscreen
SponsorBlockNotifcationSetVisible(m.videoContainer.fullscreen)
end function