2020-10-24 16:23:20 +00:00
function VideoPlayer(id, audio_stream_idx = 1)
2019-04-29 16:44:37 +00:00
' Get video controls and UI
2019-10-12 21:00:07 +00:00
video = CreateObject("roSGNode", "JFVideo")
2020-02-17 19:30:04 +00:00
video.id = id
2020-10-24 16:23:20 +00:00
video = VideoContent(video, audio_stream_idx)
2020-03-04 02:46:26 +00:00
if video = invalid
return invalid
end if
2019-03-19 00:33:44 +00:00
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
2020-10-24 16:23:20 +00:00
function VideoContent(video, audio_stream_idx = 1) as object
2019-04-29 16:44:37 +00:00
' Get video stream
2020-02-17 19:30:04 +00:00
video.content = createObject("RoSGNode", "ContentNode")
2020-02-29 03:13:12 +00:00
params = {}
2019-01-31 04:56:15 +00:00
2020-02-17 19:30:04 +00:00
meta = ItemMetaData(video.id)
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
2020-02-22 00:50:20 +00:00
' If there is a last playback positon, ask user if they want to resume
position = meta.json.UserData.PlaybackPositionTicks
2020-03-04 02:46:26 +00:00
if position > 0 then
2020-12-08 16:27:34 +00:00
dialogResult = startPlayBackOver(position)
2020-03-04 02:46:26 +00:00
'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
return invalid
else if dialogResult = 1 then
'Start Over selected, change position to 0
position = 0
2020-12-08 16:27:34 +00:00
else if dialogResult = 2 then
2020-12-12 20:21:51 +00:00
'Mark this item as watched, refresh the page, and return invalid so we don't load the video
2020-12-08 16:27:34 +00:00
MarkItemWatched(video.id)
2020-12-12 20:21:51 +00:00
video.content.watched = not video.content.watched
group = m.scene.focusedChild
group.timeLastRefresh = CreateObject("roDateTime").AsSeconds()
group.callFunc("refresh")
2020-12-08 16:27:34 +00:00
return invalid
2020-03-04 02:46:26 +00:00
end if
2020-02-22 00:50:20 +00:00
end if
2020-04-29 16:26:12 +00:00
video.content.PlayStart = int(position/10000000)
2020-02-22 00:50:20 +00:00
2020-05-31 13:46:33 +00:00
playbackInfo = ItemPostPlaybackInfo(video.id, position)
2020-05-31 14:44:58 +00:00
if playbackInfo = invalid then
return invalid
end if
2020-05-31 13:46:33 +00:00
video.PlaySessionId = playbackInfo.PlaySessionId
if meta.live then
video.content.live = true
video.content.StreamFormat = "hls"
2020-11-16 10:39:34 +00:00
'Original MediaSource seems to be a placeholder and real stream data is available
2020-05-31 13:46:33 +00:00
'after POSTing to PlaybackInfo
json = meta.json
json.AddReplace("MediaSources", playbackInfo.MediaSources)
json.AddReplace("MediaStreams", playbackInfo.MediaSources[0].MediaStreams)
meta.json = json
end if
container = getContainerType(meta)
video.container = container
2020-10-24 16:23:20 +00:00
transcodeParams = getTranscodeParameters(meta, audio_stream_idx)
2020-03-11 03:50:47 +00:00
transcodeParams.append({"PlaySessionId": video.PlaySessionId})
2020-02-15 15:34:29 +00:00
2020-05-31 13:46:33 +00:00
if meta.live then
_livestream_params = {
"MediaSourceId": playbackInfo.MediaSources[0].Id,
"LiveStreamId": playbackInfo.MediaSources[0].LiveStreamId,
"MinSegments": 2 'This is a guess about initial buffer size, segments are 3s each
}
params.append(_livestream_params)
transcodeParams.append(_livestream_params)
end if
2020-04-04 17:21:43 +00:00
subtitles = sortSubtitles(meta.id,meta.json.MediaStreams)
2020-03-28 20:04:57 +00:00
video.Subtitles = subtitles["all"]
video.content.SubtitleTracks = subtitles["text"]
2020-04-04 17:21:43 +00:00
'TODO: allow user selection of subtitle track before playback initiated, for now set to first track
if video.Subtitles.count() then
video.SelectedSubtitle = 0
else
video.SelectedSubtitle = -1
end if
2020-10-24 16:23:20 +00:00
if video.SelectedSubtitle <> -1 and displaySubtitlesByUserConfig(video.Subtitles[video.SelectedSubtitle], meta.json.MediaStreams[audio_stream_idx]) then
2020-02-29 03:13:12 +00:00
if video.Subtitles[0].IsTextSubtitleStream then
2020-03-28 20:04:57 +00:00
video.subtitleTrack = video.availableSubtitleTracks[video.Subtitles[0].TextIndex].TrackName
video.suppressCaptions = false
2020-03-11 03:50:47 +00:00
else
2020-03-28 20:04:57 +00:00
video.suppressCaptions = true
2020-03-11 03:50:47 +00:00
'Watch to see if system overlay opened/closed to change transcoding if caption mode changed
m.device.EnableAppFocusEvent(True)
video.captionMode = video.globalCaptionMode
if video.globalCaptionMode = "On" or (video.globalCaptionMode = "When mute" and m.mute = true) then
'Only transcode if subtitles are turned on
transcodeParams.append({"SubtitleStreamIndex" : video.Subtitles[0].index })
end if
2020-02-29 03:13:12 +00:00
end if
2020-04-04 17:21:43 +00:00
else
video.suppressCaptions = true
video.SelectedSubtitle = -1
2020-02-29 03:13:12 +00:00
end if
2020-03-11 03:50:47 +00:00
video.directPlaySupported = directPlaySupported(meta)
2020-10-24 16:23:20 +00:00
video.decodeAudioSupported = decodeAudioSupported(meta, audio_stream_idx)
2020-03-11 03:50:47 +00:00
video.transcodeParams = transcodeParams
2020-03-11 05:03:00 +00:00
if video.directPlaySupported and video.decodeAudioSupported and transcodeParams.SubtitleStreamIndex = invalid then
2020-02-29 03:13:12 +00:00
params.append({
"Static": "true",
2020-10-24 16:23:20 +00:00
"Container": 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)
2020-02-17 19:30:04 +00:00
video.content.streamformat = container
video.content.switchingStrategy = ""
2020-03-11 03:50:47 +00:00
video.isTranscode = False
2020-02-15 01:47:06 +00:00
else
2020-03-11 03:50:47 +00:00
video.content.url = buildURL(Substitute("Videos/{0}/master.m3u8", video.id), transcodeParams)
video.isTranscoded = true
2020-02-15 01:47:06 +00:00
end if
2020-02-17 19:30:04 +00:00
video.content = authorize_request(video.content)
2019-01-31 04:56:15 +00:00
2019-04-22 05:09:16 +00:00
' todo - audioFormat is read only
2020-02-17 19:30:04 +00:00
video.content.audioFormat = getAudioFormat(meta)
2020-02-19 08:47:29 +00:00
video.content.setCertificatesFile("common:/certs/ca-bundle.crt")
2020-02-17 19:30:04 +00:00
return video
2019-01-31 04:56:15 +00:00
end function
2019-04-22 05:09:16 +00:00
2020-10-11 16:32:34 +00:00
2020-10-24 16:23:20 +00:00
function getTranscodeParameters(meta as object, audio_stream_idx = 1)
2020-10-11 16:32:34 +00:00
2020-10-24 16:23:20 +00:00
params = {"AudioStreamIndex": audio_stream_idx}
if decodeAudioSupported(meta, audio_stream_idx) and meta.json.MediaStreams[audio_stream_idx] <> invalid and meta.json.MediaStreams[audio_stream_idx].Type = "Audio" then
audioCodec = meta.json.MediaStreams[audio_stream_idx].codec
audioChannels = meta.json.MediaStreams[audio_stream_idx].channels
2020-03-11 03:50:47 +00:00
else
2020-10-11 16:32:34 +00:00
params.Append({"AudioCodec": "aac"})
2020-05-01 16:50:40 +00:00
' If 5.1 Audio Output is connected then allow transcoding to 5.1
di = CreateObject("roDeviceInfo")
2020-05-02 08:45:06 +00:00
if di.GetAudioOutputChannel() = "5.1 surround" and di.CanDecodeAudio({ Codec: "aac", ChCnt: 6 }).result then
2020-10-11 16:32:34 +00:00
params.Append({"MaxAudioChannels": "6"})
else
params.Append({"MaxAudioChannels": "2"})
2020-05-01 16:50:40 +00:00
end if
2020-03-11 03:50:47 +00:00
end if
2020-10-11 16:32:34 +00:00
2020-10-27 17:12:18 +00:00
streamInfo = {}
if meta.json.MediaStreams[0] <> invalid and meta.json.MediaStreams[0].codec <> invalid then
streamInfo.Codec = meta.json.MediaStreams[0].codec
end if
if meta.json.MediaStreams[0] <> invalid and meta.json.MediaStreams[0].Profile <> invalid and meta.json.MediaStreams[0].Profile.len() > 0 then
2020-10-11 16:32:34 +00:00
streamInfo.Profile = LCase(meta.json.MediaStreams[0].Profile)
end if
2020-10-27 17:12:18 +00:00
if meta.json.MediaSources[0] <> invalid and meta.json.MediaSources[0].container <> invalid and meta.json.MediaSources[0].container.len() > 0 then
2020-10-11 16:32:34 +00:00
streamInfo.Container = meta.json.MediaSources[0].container
end if
devinfo = CreateObject("roDeviceInfo")
res = devinfo.CanDecodeVideo(streamInfo)
if res.result = false then
params.Append({"VideoCodec": "h264"})
streamInfo.Profile = "h264"
streamInfo.Container = "ts"
end if
params.Append({"MediaSourceId": meta.id})
return params
2020-03-11 03:50:47 +00:00
end function
2020-04-04 17:21:43 +00:00
'Checks available subtitle tracks and puts subtitles in forced, default, and non-default/forced but preferred language at the top
function sortSubtitles(id as string, MediaStreams)
tracks = { "forced": [], "default": [], "normal": [] }
2020-02-29 03:13:12 +00:00
'Too many args for using substitute
dashedid = id.left(8) + "-" + id.mid(8,4) + "-" + id.mid(12,4) + "-" + id.mid(16,4) + "-" + id.right(12)
2020-04-04 17:21:43 +00:00
prefered_lang = m.user.Configuration.SubtitleLanguagePreference
2020-02-29 03:13:12 +00:00
for each stream in MediaStreams
if stream.type = "Subtitle" then
'Documentation lists that srt, ttml, and dfxp can be sideloaded but only srt was working in my testing,
'forcing srt for all text subtitles
url = Substitute("{0}/Videos/{1}/{2}/Subtitles/{3}/0/", get_url(), dashedid, id, stream.index.tostr())
url = url + Substitute("Stream.js?api_key={0}&format=srt", get_setting("active_user"))
2020-03-28 20:04:57 +00:00
stream = {
"Track": { "Language" : stream.language, "Description": stream.displaytitle , "TrackName": url },
"IsTextSubtitleStream": stream.IsTextSubtitleStream,
"Index": stream.index,
2020-04-04 17:21:43 +00:00
"TextIndex": -1,
"IsDefault": stream.IsDefault,
"IsForced": stream.IsForced
2020-03-28 20:04:57 +00:00
}
2020-04-04 17:21:43 +00:00
if stream.isForced then
trackType = "forced"
else if stream.IsDefault then
trackType = "default"
else
trackType = "normal"
2020-03-28 20:04:57 +00:00
end if
2020-04-04 17:21:43 +00:00
if prefered_lang <> "" and prefered_lang = stream.Track.Language then
tracks[trackType].unshift(stream)
2020-02-29 03:13:12 +00:00
else
2020-04-04 17:21:43 +00:00
tracks[trackType].push(stream)
2020-02-29 03:13:12 +00:00
end if
end if
end for
2020-04-04 17:21:43 +00:00
tracks["default"].append(tracks["normal"])
tracks["forced"].append(tracks["default"])
textTracks = []
for i = 0 to tracks["forced"].count() - 1
if tracks["forced"][i].IsTextSubtitleStream then tracks["forced"][i].TextIndex = textTracks.count()
textTracks.push(tracks["forced"][i].Track)
end for
return { "all" : tracks["forced"], "text": textTracks }
2020-02-29 03:13:12 +00:00
end function
2020-02-22 00:50:20 +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
2021-01-06 01:40:11 +00:00
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
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
2020-02-21 04:02:21 +00:00
devinfo = CreateObject("roDeviceInfo")
2020-06-01 16:26:19 +00:00
if meta.json.MediaSources[0] <> invalid and meta.json.MediaSources[0].SupportsDirectPlay = false then
2020-05-31 10:59:17 +00:00
return false
end if
2020-10-27 17:12:18 +00:00
if meta.json.MediaStreams[0] = invalid then
return false
end if
2020-06-05 11:20:54 +00:00
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)
2020-06-05 11:20:54 +00:00
end if
if meta.json.MediaSources[0].container <> invalid and meta.json.MediaSources[0].container.len() > 0 then
2021-01-10 20:06:16 +00:00
'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
2020-06-05 11:20:54 +00:00
end if
return devinfo.CanDecodeVideo(streamInfo).result
2020-02-21 04:02:21 +00:00
end function
2020-10-24 16:23:20 +00:00
function decodeAudioSupported(meta as object, audio_stream_idx = 1) as boolean
2020-07-11 08:32:46 +00:00
'Check for missing audio and allow playing
2020-10-24 16:23:20 +00:00
if meta.json.MediaStreams[audio_stream_idx] = invalid or meta.json.MediaStreams[audio_stream_idx].Type <> "Audio" then return true
2020-07-11 08:32:46 +00:00
2020-02-21 04:02:21 +00:00
devinfo = CreateObject("roDeviceInfo")
2020-10-24 16:23:20 +00:00
codec = meta.json.MediaStreams[audio_stream_idx].codec
streamInfo = { Codec: codec, ChCnt: meta.json.MediaStreams[audio_stream_idx].channels }
2020-07-11 07:52:52 +00:00
'Otherwise check Roku can decode stream and channels
2020-07-22 19:30:59 +00:00
canDecode = devinfo.CanDecodeAudio(streamInfo)
2020-07-11 07:52:52 +00:00
return canDecode.result
2020-02-15 01:47:06 +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)
2020-10-25 09:41:15 +00:00
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
function ReportPlayback(video, state = "update" as string)
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"),
}
2020-05-31 13:46:33 +00:00
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 function
function StopPlayback()
video = m.scene.focusedchild
2020-11-30 07:46:20 +00:00
if video.state = "finished" then MarkItemWatched(video.id)
2020-03-19 03:56:56 +00:00
video.control = "stop"
2020-03-11 03:50:47 +00:00
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-04-04 17:21:43 +00:00
function displaySubtitlesByUserConfig(subtitleTrack, audioTrack)
subtitleMode = m.user.Configuration.SubtitleMode
audioLanguagePreference = m.user.Configuration.AudioLanguagePreference
subtitleLanguagePreference = m.user.Configuration.SubtitleLanguagePreference
if subtitleMode = "Default"
return (subtitleTrack.isForced or subtitleTrack.isDefault)
else if subtitleMode = "Smart"
return (audioLanguagePreference <> "" and audioTrack.Language <> invalid and subtitleLanguagePreference <> "" and subtitleTrack.Track.Language <> invalid and subtitleLanguagePreference = subtitleTrack.Track.Language and audioLanguagePreference <> audioTrack.Language)
else if subtitleMode = "OnlyForced"
return subtitleTrack.IsForced
else if subtitleMode = "Always"
return true
else if subtitleMode = "None"
return false
else
return false
end if
end function
2020-11-30 07:46:20 +00:00
2020-12-11 07:38:19 +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")
else
' can't play next episode
RemoveCurrentGroup()
end if
2020-11-30 07:46:20 +00:00
else
RemoveCurrentGroup()
end if
2021-01-10 20:06:16 +00:00
end function