2022-03-13 14:22:55 +00:00
function VideoPlayer(id, mediaSourceId = invalid, audio_stream_idx = 1, subtitle_idx = -1)
2021-06-12 15:03:47 +00:00
2021-07-09 20:08:32 +00:00
' Get video controls and UI
video = CreateObject("roSGNode", "JFVideo")
video.id = id
2022-03-13 14:22:55 +00:00
AddVideoContent(video, mediaSourceId, audio_stream_idx, subtitle_idx)
2021-06-12 15:03:47 +00:00
2021-07-09 20:08:32 +00:00
if video.content = invalid
return invalid
end if
jellyfin_blue = "#00a4dcFF"
2019-03-19 00:33:44 +00:00
2021-07-09 20:08:32 +00:00
video.retrievingBar.filledBarBlendColor = jellyfin_blue
video.bufferingBar.filledBarBlendColor = jellyfin_blue
video.trickPlayBar.filledBarBlendColor = jellyfin_blue
return video
2019-01-31 04:56:15 +00:00
end function
2022-03-13 14:22:55 +00:00
sub AddVideoContent(video, mediaSourceId, audio_stream_idx = 1, subtitle_idx = -1, playbackPosition = -1)
2021-06-12 15:03:47 +00:00
2021-07-09 20:08:32 +00:00
video.content = createObject("RoSGNode", "ContentNode")
2021-06-12 15:03:47 +00:00
2021-07-09 20:08:32 +00:00
meta = ItemMetaData(video.id)
if meta = invalid
2021-06-12 15:03:47 +00:00
video.content = invalid
return
2020-03-04 02:46:26 +00:00
end if
2021-06-12 15:03:47 +00:00
2021-12-18 06:03:33 +00:00
' Special handling for "Programs" launched from "On Now"
if meta.json.type = "Program"
meta.title = meta.json.EpisodeTitle
meta.showID = meta.json.id
meta.live = true
video.id = meta.json.ChannelId
end if
2021-07-09 20:08:32 +00:00
video.content.title = meta.title
video.showID = meta.showID
if playbackPosition = -1
playbackPosition = meta.json.UserData.PlaybackPositionTicks
if playbackPosition > 0
dialogResult = startPlayBackOver(playbackPosition)
'Dialog returns -1 when back pressed, 0 for resume, and 1 for start over
if dialogResult = -1
'User pressed back, return invalid and don't load video
video.content = invalid
return
else if dialogResult = 1
'Start Over selected, change position to 0
playbackPosition = 0
else if dialogResult = 2
'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
end if
end if
video.content.PlayStart = int(playbackPosition / 10000000)
' Call PlayInfo from server
2022-03-13 14:22:55 +00:00
if mediaSourceId = invalid
mediaSourceId = video.id
end if
2021-07-09 20:08:32 +00:00
if meta.live then mediaSourceId = "" ' Don't send mediaSourceId for Live media
2022-03-13 14:22:55 +00:00
2021-07-09 20:08:32 +00:00
playbackInfo = ItemPostPlaybackInfo(video.id, mediaSourceId, audio_stream_idx, subtitle_idx, playbackPosition)
2020-02-22 00:50:20 +00:00
2021-07-09 20:08:32 +00:00
video.videoId = video.id
2022-03-13 14:24:08 +00:00
video.mediaSourceId = mediaSourceId
2021-07-09 20:08:32 +00:00
video.audioIndex = audio_stream_idx
2020-05-31 14:44:58 +00:00
2021-07-09 20:08:32 +00:00
if playbackInfo = invalid
video.content = invalid
return
end if
2020-05-31 14:44:58 +00:00
2021-07-09 20:08:32 +00:00
params = {}
video.PlaySessionId = playbackInfo.PlaySessionId
2020-05-31 13:46:33 +00:00
2021-07-09 20:08:32 +00:00
if meta.live
video.content.live = true
video.content.StreamFormat = "hls"
end if
2020-05-31 13:46:33 +00:00
2021-07-09 20:08:32 +00:00
video.container = getContainerType(meta)
2020-05-31 13:46:33 +00:00
2021-07-09 20:08:32 +00:00
subtitles = sortSubtitles(meta.id, playbackInfo.MediaSources[0].MediaStreams)
video.Subtitles = subtitles["all"]
2020-02-15 15:34:29 +00:00
2021-07-09 20:08:32 +00:00
if meta.live
video.transcodeParams = {
"MediaSourceId": playbackInfo.MediaSources[0].Id,
"LiveStreamId": playbackInfo.MediaSources[0].LiveStreamId,
"PlaySessionId": video.PlaySessionId
}
end if
2020-05-31 13:46:33 +00:00
2021-07-09 20:08:32 +00:00
video.content.SubtitleTracks = subtitles["text"]
2020-03-28 20:04:57 +00:00
2021-07-09 20:08:32 +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-07-09 20:08:32 +00:00
video.directPlaySupported = playbackInfo.MediaSources[0].SupportsDirectPlay
2020-03-11 03:50:47 +00:00
2021-07-09 20:08:32 +00:00
if video.directPlaySupported
params.append({
"Static": "true",
"Container": video.container,
"PlaySessionId": video.PlaySessionId,
"AudioStreamIndex": audio_stream_idx
})
2022-03-13 14:22:55 +00:00
if mediaSourceId <> ""
params.MediaSourceId = mediaSourceId
end if
2021-07-09 20:08:32 +00:00
video.content.url = buildURL(Substitute("Videos/{0}/stream", video.id), params)
video.isTranscoded = false
2022-01-15 12:40:19 +00:00
video.audioTrack = (audio_stream_idx + 1).ToStr() ' Roku's track indexes count from 1. Our index is zero based
2021-07-09 20:08:32 +00:00
else
2021-12-03 09:40:15 +00:00
' If server does not provide a transcode URL, display a message to the user
if playbackInfo.MediaSources[0].TranscodingUrl = invalid
m.global.sceneManager.callFunc("userMessage", tr("Error Getting Playback Information"), tr("An error was encountered while playing this item. Server did not provide required transcoding data."))
video.content = invalid
return
end if
2021-06-12 15:03:47 +00:00
2021-07-09 20:08:32 +00:00
' Get transcoding reason
video.transcodeReasons = getTranscodeReasons(playbackInfo.MediaSources[0].TranscodingUrl)
2021-06-12 15:03:47 +00:00
2021-07-09 20:08:32 +00:00
video.content.url = buildURL(playbackInfo.MediaSources[0].TranscodingUrl)
video.isTranscoded = true
end if
2019-01-31 04:56:15 +00:00
2021-07-09 20:08:32 +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
2020-10-11 16:32:34 +00:00
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
2020-10-11 16:32:34 +00:00
2021-07-09 20:08:32 +00:00
regex = CreateObject("roRegex", "&TranscodeReasons=([^&]*)", "")
match = regex.Match(url)
2020-10-11 16:32:34 +00:00
2021-07-09 20:08:32 +00:00
if match.count() > 1
return match[1].Split(",")
end if
2020-10-11 16:32:34 +00:00
2021-07-09 20:08:32 +00:00
return []
2020-03-11 03:50:47 +00:00
end function
2021-06-12 15:03:47 +00:00
2020-02-29 03:13:12 +00:00
2020-02-22 00:50:20 +00:00
'Opens dialog asking user if they want to resume video or start playback over
2021-07-09 20:08:32 +00:00
function startPlayBackOver(time as longinteger) as integer
if m.scene.focusedChild.overhangTitle = "Home"
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."])
end if
2020-02-22 00:50:20 +00:00
end function
2020-02-15 01:47:06 +00:00
function directPlaySupported(meta as object) as boolean
2021-07-09 20:08:32 +00:00
devinfo = CreateObject("roDeviceInfo")
if meta.json.MediaSources[0] <> invalid and meta.json.MediaSources[0].SupportsDirectPlay = false
return false
end if
if meta.json.MediaStreams[0] = invalid
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
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
'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"
streamInfo.Container = "mp4"
else
streamInfo.Container = meta.json.MediaSources[0].container
end if
2021-01-10 20:06:16 +00:00
end if
2021-04-30 07:23:38 +00:00
2021-07-09 20:08:32 +00:00
decodeResult = devinfo.CanDecodeVideo(streamInfo)
return decodeResult <> invalid and decodeResult.result
2021-04-30 07:23:38 +00:00
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
2021-07-09 20:08:32 +00:00
' Determine the file type of the video file source
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
2019-04-22 05:09:16 +00:00
2021-07-09 20:08:32 +00:00
return container
2019-04-22 05:09:16 +00:00
end function
function getAudioFormat(meta as object) as string
2021-07-09 20:08:32 +00:00
' Determine the codec of the audio file source
if meta.json.mediaSources = invalid then return ""
2019-04-22 05:09:16 +00:00
2021-07-09 20:08:32 +00:00
audioInfo = getAudioInfo(meta)
if audioInfo.count() = 0 or audioInfo[0].codec = invalid then return ""
return audioInfo[0].codec
2019-04-22 05:09:16 +00:00
end function
function getAudioInfo(meta as object) as object
2021-07-09 20:08:32 +00:00
' Return audio metadata for a given stream
results = []
for each source in meta.json.mediaSources[0].mediaStreams
if source["type"] = "Audio"
results.push(source)
end if
end for
return results
2019-04-22 05:09:16 +00:00
end function
2020-02-17 19:30:04 +00:00
2021-09-14 02:03:32 +00:00
sub autoPlayNextEpisode(videoID as string, showID as string)
2021-07-09 20:08:32 +00:00
' use web client setting
if m.user.Configuration.EnableNextEpisodeAutoPlay
' 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
' remove finished video node
2021-10-10 01:51:33 +00:00
m.global.sceneManager.callFunc("popScene")
2021-07-09 20:08:32 +00:00
' setup new video node
nextVideo = CreateVideoPlayerGroup(data.Items[1].Id)
2021-09-14 02:03:32 +00:00
if nextVideo <> invalid
2021-10-10 01:51:33 +00:00
m.global.sceneManager.callFunc("pushScene", nextVideo)
2021-09-14 02:03:32 +00:00
else
2021-10-10 01:51:33 +00:00
m.global.sceneManager.callFunc("popScene")
2021-09-14 02:03:32 +00:00
end if
2021-07-09 20:08:32 +00:00
else
' can't play next episode
2021-10-10 01:51:33 +00:00
m.global.sceneManager.callFunc("popScene")
2021-07-09 20:08:32 +00:00
end if
2020-12-11 07:38:19 +00:00
else
2021-10-10 01:51:33 +00:00
m.global.sceneManager.callFunc("popScene")
2020-12-11 07:38:19 +00:00
end if
2021-09-14 02:03:32 +00:00
end sub