jf-roku/source/VideoPlayer.brs

268 lines
8.2 KiB
Plaintext
Raw Normal View History

2021-06-12 15:03:47 +00:00
function VideoPlayer(id, audio_stream_idx = 1, subtitle_idx = -1)
2019-04-29 16:44:37 +00:00
' Get video controls and UI
video = CreateObject("roSGNode", "JFVideo")
2020-02-17 19:30:04 +00:00
video.id = id
2021-06-12 15:03:47 +00:00
AddVideoContent(video, audio_stream_idx, subtitle_idx)
if video.content = invalid
2020-03-04 02:46:26 +00:00
return invalid
end if
jellyfin_blue = "#00a4dcFF"
video.retrievingBar.filledBarBlendColor = jellyfin_blue
video.bufferingBar.filledBarBlendColor = jellyfin_blue
video.trickPlayBar.filledBarBlendColor = jellyfin_blue
2019-03-05 04:59:31 +00:00
return video
2019-01-31 04:56:15 +00:00
end function
2021-06-12 15:03:47 +00:00
sub AddVideoContent(video, audio_stream_idx = 1, subtitle_idx = -1, playbackPosition = -1)
2020-02-17 19:30:04 +00:00
video.content = createObject("RoSGNode", "ContentNode")
2019-01-31 04:56:15 +00:00
2020-02-17 19:30:04 +00:00
meta = ItemMetaData(video.id)
2021-06-12 15:03:47 +00:00
if meta = invalid then
video.content = invalid
return
end if
2020-11-30 07:51:20 +00:00
video.content.title = meta.title
2020-11-30 07:46:20 +00:00
video.showID = meta.showID
2021-06-12 15:03:47 +00:00
if playbackPosition = -1 then
playbackPosition = meta.json.UserData.PlaybackPositionTicks
if playbackPosition > 0 then
dialogResult = startPlayBackOver(playbackPosition)
'Dialog returns -1 when back pressed, 0 for resume, and 1 for start over
if dialogResult = -1 then
'User pressed back, return invalid and don't load video
video.content = invalid
return
else if dialogResult = 1 then
'Start Over selected, change position to 0
playbackPosition = 0
else if dialogResult = 2 then
'Mark this item as watched, refresh the page, and return invalid so we don't load the video
MarkItemWatched(video.id)
video.content.watched = not video.content.watched
group = m.scene.focusedChild
group.timeLastRefresh = CreateObject("roDateTime").AsSeconds()
group.callFunc("refresh")
video.content = invalid
return
end if
2020-03-04 02:46:26 +00:00
end if
end if
2021-06-12 15:03:47 +00:00
video.content.PlayStart = int(playbackPosition / 10000000)
' Call PlayInfo from server
mediaSourceId = video.id
if meta.live then mediaSourceId = "" ' Don't send mediaSourceId for Live media
playbackInfo = ItemPostPlaybackInfo(video.id, mediaSourceId, audio_stream_idx, subtitle_idx, playbackPosition)
2021-06-12 15:03:47 +00:00
video.videoId = video.id
video.mediaSourceId = video.id
video.audioIndex = audio_stream_idx
if playbackInfo = invalid then
2021-06-12 15:03:47 +00:00
video.content = invalid
return
end if
2021-06-12 15:03:47 +00:00
params = {}
video.PlaySessionId = playbackInfo.PlaySessionId
if meta.live then
video.content.live = true
video.content.StreamFormat = "hls"
end if
2021-06-12 15:03:47 +00:00
video.container = getContainerType(meta)
2021-06-12 15:03:47 +00:00
subtitles = sortSubtitles(meta.id, playbackInfo.MediaSources[0].MediaStreams)
video.Subtitles = subtitles["all"]
2020-02-15 15:34:29 +00:00
if meta.live then
2021-06-12 15:03:47 +00:00
video.transcodeParams = {
"MediaSourceId": playbackInfo.MediaSources[0].Id,
"LiveStreamId": playbackInfo.MediaSources[0].LiveStreamId,
2021-06-12 15:03:47 +00:00
"PlaySessionId": video.PlaySessionId
}
end if
video.content.SubtitleTracks = subtitles["text"]
2021-06-12 15:03:47 +00:00
' 'TODO: allow user selection of subtitle track before playback initiated, for now set to no subtitles
video.SelectedSubtitle = -1
2020-02-29 03:13:12 +00:00
2021-06-12 15:03:47 +00:00
video.directPlaySupported = playbackInfo.MediaSources[0].SupportsDirectPlay
2021-06-12 15:03:47 +00:00
if video.directPlaySupported then
2020-02-29 03:13:12 +00:00
params.append({
"Static": "true",
2021-06-12 15:03:47 +00:00
"Container": video.container,
"PlaySessionId": video.PlaySessionId,
"AudioStreamIndex": audio_stream_idx
2020-02-15 15:26:36 +00:00
})
2020-02-29 03:13:12 +00:00
video.content.url = buildURL(Substitute("Videos/{0}/stream", video.id), params)
2021-06-12 15:03:47 +00:00
video.isTranscoded = false
else
2021-06-12 15:03:47 +00:00
' Get transcoding reason
video.transcodeReasons = getTranscodeReasons(playbackInfo.MediaSources[0].TranscodingUrl)
video.content.url = buildURL(playbackInfo.MediaSources[0].TranscodingUrl)
video.isTranscoded = true
end if
2019-01-31 04:56:15 +00:00
2021-06-12 15:03:47 +00:00
video.content = authorize_request(video.content)
video.content.setCertificatesFile("common:/certs/ca-bundle.crt")
2019-04-22 05:09:16 +00:00
2021-06-12 15:03:47 +00:00
end sub
2021-06-12 15:03:47 +00:00
'
' Extract array of Transcode Reasons from the content URL
' @returns Array of Strings
function getTranscodeReasons(url as string) as object
2021-06-12 15:03:47 +00:00
regex = CreateObject("roRegex", "&TranscodeReasons=([^&]*)", "")
match = regex.Match(url)
2021-06-12 15:03:47 +00:00
if match.count() > 1
return match[1].Split(",")
end if
2021-06-12 15:03:47 +00:00
return []
end function
2021-06-12 15:03:47 +00:00
2020-02-29 03:13:12 +00:00
'Opens dialog asking user if they want to resume video or start playback over
2020-03-04 04:15:02 +00:00
function startPlayBackOver(time as LongInteger) as integer
if m.scene.focusedChild.overhangTitle = "Home" then
return option_dialog([ "Resume playing at " + ticksToHuman(time) + ".", "Start over from the beginning.", "Watched"])
else
return option_dialog([ "Resume playing at " + ticksToHuman(time) + ".", "Start over from the beginning."])
endif
end function
function directPlaySupported(meta as object) as boolean
2020-02-21 04:02:21 +00:00
devinfo = CreateObject("roDeviceInfo")
if meta.json.MediaSources[0] <> invalid and meta.json.MediaSources[0].SupportsDirectPlay = false then
return false
end if
2020-10-27 17:12:18 +00:00
if meta.json.MediaStreams[0] = invalid then
return false
end if
streamInfo = { Codec: meta.json.MediaStreams[0].codec }
if meta.json.MediaStreams[0].Profile <> invalid and meta.json.MediaStreams[0].Profile.len() > 0 then
2020-07-11 07:52:52 +00:00
streamInfo.Profile = LCase(meta.json.MediaStreams[0].Profile)
end if
if meta.json.MediaSources[0].container <> invalid and meta.json.MediaSources[0].container.len() > 0 then
'CanDecodeVideo() requires the .container to be format: “mp4”, “hls”, “mkv”, “ism”, “dash”, “ts” if its to direct stream
if meta.json.MediaSources[0].container = "mov" then
streamInfo.Container = "mp4"
else
streamInfo.Container = meta.json.MediaSources[0].container
end if
end if
decodeResult = devinfo.CanDecodeVideo(streamInfo)
return decodeResult <> invalid and decodeResult.result
2020-02-21 04:02:21 +00:00
end function
2019-04-22 05:09:16 +00:00
function getContainerType(meta as object) as string
2019-04-29 16:44:37 +00:00
' Determine the file type of the video file source
2019-04-22 05:09:16 +00:00
if meta.json.mediaSources = invalid then return ""
container = meta.json.mediaSources[0].container
if container = invalid
container = ""
else if container = "m4v" or container = "mov"
container = "mp4"
end if
return container
end function
function getAudioFormat(meta as object) as string
2019-04-29 16:44:37 +00:00
' Determine the codec of the audio file source
2019-04-22 05:09:16 +00:00
if meta.json.mediaSources = invalid then return ""
audioInfo = getAudioInfo(meta)
if audioInfo.count() = 0 or audioInfo[0].codec = invalid then return ""
2019-04-22 05:09:16 +00:00
return audioInfo[0].codec
end function
function getAudioInfo(meta as object) as object
2019-04-29 16:44:37 +00:00
' Return audio metadata for a given stream
2019-04-22 05:09:16 +00:00
results = []
for each source in meta.json.mediaSources[0].mediaStreams
if source["type"] = "Audio"
results.push(source)
end if
end for
return results
end function
2020-02-17 19:30:04 +00:00
sub ReportPlayback(video, state = "update" as string)
2021-06-12 15:03:47 +00:00
if video = invalid or video.position = invalid then return invalid
2020-02-17 19:30:04 +00:00
params = {
"PlaySessionId": video.PlaySessionId,
2020-12-01 09:39:50 +00:00
"PositionTicks": int(video.position) * 10000000&, 'Ensure a LongInteger is used
2020-02-17 19:30:04 +00:00
"IsPaused": (video.state = "paused"),
}
if video.content.live then
params.append({
"MediaSourceId": video.transcodeParams.MediaSourceId,
"LiveStreamId": video.transcodeParams.LiveStreamId
})
end if
2020-02-17 19:30:04 +00:00
PlaystateUpdate(video.id, state, params)
end sub
2020-02-17 19:30:04 +00:00
function StopPlayback()
video = m.scene.focusedchild
video.control = "stop"
m.device.EnableAppFocusEvent(False)
2020-02-17 19:30:04 +00:00
video.findNode("playbackTimer").control = "stop"
2020-02-21 04:02:21 +00:00
ReportPlayback(video, "stop")
2020-02-17 19:30:04 +00:00
end function
2020-11-30 07:46:20 +00:00
function autoPlayNextEpisode(videoID as string, showID as string)
' use web client setting
if m.user.Configuration.EnableNextEpisodeAutoPlay then
' query API for next episode ID
url = Substitute("Shows/{0}/Episodes", showID)
urlParams = { "UserId": get_setting("active_user")}
urlParams.Append({ "StartItemId": videoID })
urlParams.Append({ "Limit": 2 })
resp = APIRequest(url, urlParams)
data = getJson(resp)
if data <> invalid and data.Items.Count() = 2 then
' remove finished video node
n = m.scene.getChildCount() - 1
m.scene.removeChildIndex(n)
' setup new video node
nextVideo = CreateVideoPlayerGroup(data.Items[1].Id)
m.scene.appendChild(nextVideo)
nextVideo.setFocus(true)
nextVideo.control = "play"
ReportPlayback(nextVideo, "start")
return nextVideo
else
' can't play next episode
RemoveCurrentGroup()
end if
2020-11-30 07:46:20 +00:00
else
RemoveCurrentGroup()
end if
return invalid
end function