Report playback progress in a separate thread to reduce render latency.

This commit is contained in:
JD Layman 2021-08-31 19:39:35 -05:00
parent 641056435e
commit 4a0907ca21
4 changed files with 68 additions and 53 deletions

View File

@ -1,51 +1,48 @@
function PlaystateUpdate(id, state as string, params = {})
if state = "start"
url = "Sessions/Playing"
else if state = "stop"
url = "Sessions/Playing/Stopped"
else if state = "update"
url = "Sessions/Playing/Progress"
else
' Unknow State
return {}
end if
params = PlaystateDefaults(id, params)
resp = APIRequest(url)
return postJson(resp, params)
end function
function PlaystateDefaults(id = "" as string, params = {} as object)
new_params = {
'"CanSeek": false
'"Item": "{}", ' TODO!
'"NowPlayingQueue": "[]", ' TODO!
'"PlaylistItemId": "",
"ItemId": id,
'"SessionId": "", ' TODO!
'"MediaSourceId": id,
'"AudioStreamIndex": 1,
'"SubtitleStreamIndex": 0,
"IsPaused": false,
'"IsMuted": false,
"PositionTicks": 0,
'"PlaybackStartTimeTicks": 0,
'"VolumeLevel": 100,
'"Brightness": 100,
'"AspectRatio": "16x9",
'"PlayMethod": "DirectStream"
'"LiveStreamId": "",
'"PlaySessionId": "",
'"RepeatMode": "RepeatNone"
}
for each p in params.items()
new_params[p.key] = p.value
end for
return FormatJson(new_params)
end function
sub deleteTranscode(id)
devinfo = CreateObject("roDeviceInfo")
req = APIRequest("/Videos/ActiveEncodings", { "deviceID": devinfo.getChannelClientID(), "PlaySessionId": id })
req.setRequest("DELETE")
postVoid(req)
end sub
sub init()
m.top.functionName = "PlaystateUpdate"
end sub
sub PlaystateUpdate()
if m.top.status = "start"
url = "Sessions/Playing"
else if m.top.status = "stop"
url = "Sessions/Playing/Stopped"
else if m.top.status = "update"
url = "Sessions/Playing/Progress"
else
' Unknown State
return
end if
params = PlaystateDefaults(m.top.params)
resp = APIRequest(url)
postJson(resp, params)
end sub
function PlaystateDefaults(params = {} as object)
new_params = {
'"CanSeek": false
'"Item": "{}", ' TODO!
'"NowPlayingQueue": "[]", ' TODO!
'"PlaylistItemId": "",
'"ItemId": id,
'"SessionId": "", ' TODO!
'"MediaSourceId": id,
'"AudioStreamIndex": 1,
'"SubtitleStreamIndex": 0,
"IsPaused": false,
'"IsMuted": false,
"PositionTicks": 0,
'"PlaybackStartTimeTicks": 0,
'"VolumeLevel": 100,
'"Brightness": 100,
'"AspectRatio": "16x9",
'"PlayMethod": "DirectStream"
'"LiveStreamId": "",
'"PlaySessionId": "",
'"RepeatMode": "RepeatNone"
}
for each p in params.items()
new_params[p.key] = p.value
end for
return FormatJson(new_params)
end function

11
components/PlaystateTask.xml Executable file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<component name="PlaystateTask" extends="Task">
<interface>
<field id="status" type="string" />
<field id="params" type="assocarray" />
</interface>
<script type="text/brightscript" uri="PlaystateTask.brs" />
<script type="text/brightscript" uri="pkg:/source/api/baserequest.brs" />
<script type="text/brightscript" uri="pkg:/source/utils/config.brs" />
</component>

View File

@ -17,7 +17,11 @@ sub Main (args as dynamic) as void
' Set any initial Global Variables
m.global = m.screen.getGlobalNode()
m.global.addFields({ app_loaded: false })
playstateTask = CreateObject("roSGNode", "PlaystateTask")
playstateTask.id = "playstateTask"
m.global.addFields({ app_loaded: false, playstateTask: playstateTask })
m.overhang = CreateObject("roSGNode", "JFOverhang")
m.scene.insertChild(m.overhang, 0)

View File

@ -213,6 +213,7 @@ sub ReportPlayback(video, state = "update" as string)
if video = invalid or video.position = invalid then return
params = {
"ItemId": video.id
"PlaySessionId": video.PlaySessionId,
"PositionTicks": int(video.position) * 10000000&, 'Ensure a LongInteger is used
"IsPaused": (video.state = "paused"),
@ -223,7 +224,9 @@ sub ReportPlayback(video, state = "update" as string)
"LiveStreamId": video.transcodeParams.LiveStreamId
})
end if
PlaystateUpdate(video.id, state, params)
playstateTask = m.global.playstateTask
playstateTask.setFields({ status: state, params: params })
playstateTask.control = "RUN"
end sub
sub StopPlayback()