366 lines
12 KiB
Plaintext
366 lines
12 KiB
Plaintext
sub init()
|
|
for each id in ["video", "weather", "banner", "channelTitle", "clock", "nowTitle", "nextTitle", "progressTrack", "progress", "playbackStatus", "panel", "panelTitle", "panelStatus", "menu", "bannerTimer", "clockTimer", "retryTimer", "guideDetails", "guideChannelTitle", "guideNow", "guideNext", "guideLater", "remoteFocus"]
|
|
m[id] = m.top.FindNode(id)
|
|
end for
|
|
m.channels = []
|
|
m.channelIndex = -1
|
|
m.previousIndex = -1
|
|
m.retryCount = 0
|
|
m.serverRequest = invalid
|
|
m.schedule = invalid
|
|
m.clockReference = CreateObject("roTimespan")
|
|
|
|
m.menu.ObserveField("itemSelected", "onMenuSelect")
|
|
m.menu.ObserveField("itemFocused", "onGuideFocus")
|
|
m.video.ObserveField("state", "onVideoState")
|
|
initWeather()
|
|
m.bannerTimer.ObserveField("fire", "hideBanner")
|
|
m.clockTimer.ObserveField("fire", "refreshClock")
|
|
m.retryTimer.ObserveField("fire", "retryPlayback")
|
|
m.clockTimer.control = "start"
|
|
findServer("", true)
|
|
end sub
|
|
|
|
sub findServer(baseUrl as string, useSaved as boolean)
|
|
if m.serverRequest <> invalid then return
|
|
m.banner.visible = false
|
|
m.weather.visible = false
|
|
stopWeather()
|
|
m.video.control = "stop"
|
|
m.videoChannelId = invalid
|
|
m.video.visible = false
|
|
m.playbackStatus.visible = false
|
|
m.retryTimer.control = "stop"
|
|
m.channelIndex = -1
|
|
showPanel("servers", "Connect to retroLabs", "Looking for the bridge…", [])
|
|
m.serverRequest = startRequest({
|
|
kind: "discover"
|
|
baseUrl: baseUrl
|
|
serverId: ""
|
|
manual: baseUrl <> ""
|
|
useSaved: useSaved
|
|
}, "onServers")
|
|
end sub
|
|
|
|
sub onServers(event as object)
|
|
m.serverRequest = invalid
|
|
m.servers = event.GetData().servers
|
|
if m.servers.Count() = 1
|
|
connectServer(m.servers[0])
|
|
return
|
|
end if
|
|
|
|
titles = []
|
|
for each server in m.servers
|
|
titles.Push(server.name + " " + server.baseUrl)
|
|
end for
|
|
titles.Push("Enter server address")
|
|
titles.Push("Search again")
|
|
status = "Select a bridge on your network."
|
|
if m.servers.Count() = 0 then status = "No bridge found. Check the server or enter its address."
|
|
showPanel("servers", "Connect to retroLabs", status, titles)
|
|
end sub
|
|
|
|
sub connectServer(server as object)
|
|
m.server = server
|
|
loadLineup()
|
|
end sub
|
|
|
|
sub loadLineup()
|
|
if m.serverRequest <> invalid then return
|
|
showPanel("lineup", m.server.name, "Loading channels…", [])
|
|
m.serverRequest = startRequest({kind: "connect", server: m.server}, "onChannels")
|
|
end sub
|
|
|
|
sub onChannels(event as object)
|
|
m.serverRequest = invalid
|
|
data = event.GetData().data
|
|
if data = invalid or GetInterface(data.channels, "ifArray") = invalid
|
|
showPanel("lineup", m.server.name, "Could not load channels.", ["Retry", "Find another server"])
|
|
return
|
|
end if
|
|
m.channels = data.channels
|
|
if m.channels.Count() = 0
|
|
showPanel("lineup", m.server.name, "Set up a channel in Tunarr, then refresh the lineup.", ["Refresh", "Find another server"])
|
|
return
|
|
end if
|
|
|
|
m.previousIndex = -1
|
|
openGuide()
|
|
end sub
|
|
|
|
function startRequest(request as object, callback as string) as object
|
|
worker = CreateObject("roSGNode", "BridgeTask")
|
|
worker.request = request
|
|
worker.ObserveFieldScoped("result", callback)
|
|
worker.control = "run"
|
|
return worker
|
|
end function
|
|
|
|
sub openAddressDialog()
|
|
dialog = CreateObject("roSGNode", "KeyboardDialog")
|
|
dialog.title = "Enter server IP or IP:port"
|
|
dialog.buttons = ["Connect", "Cancel"]
|
|
if m.server <> invalid then dialog.text = Mid(m.server.baseUrl, 8)
|
|
dialog.ObserveField("buttonSelected", "onAddressSelected")
|
|
m.top.dialog = dialog
|
|
end sub
|
|
|
|
sub onAddressSelected(event as object)
|
|
dialog = m.top.dialog
|
|
if event.GetData() <> 0
|
|
dialog.close = true
|
|
return
|
|
end if
|
|
|
|
address = dialog.text.Trim()
|
|
if LCase(Left(address, 7)) = "http://" then address = Mid(address, 8)
|
|
if Right(address, 1) = "/" then address = Left(address, Len(address) - 1)
|
|
if Instr(1, address, ":") = 0 then address += ":4242"
|
|
pattern = CreateObject("roRegex", "^([A-Za-z0-9.-]+):([0-9]{1,5})$", "")
|
|
match = pattern.Match(address)
|
|
if match.Count() = 0
|
|
dialog.title = "Enter an IP address or hostname"
|
|
return
|
|
end if
|
|
port = match[2]
|
|
if Val(port) < 1 or Val(port) > 65535
|
|
dialog.title = "Enter a port from 1 to 65535"
|
|
return
|
|
end if
|
|
dialog.close = true
|
|
findServer("http://" + match[1] + ":" + port, false)
|
|
end sub
|
|
|
|
sub tune(index as integer)
|
|
if index < 0 or index >= m.channels.Count() then return
|
|
if m.channelIndex <> index
|
|
m.previousIndex = m.channelIndex
|
|
m.channelIndex = index
|
|
end if
|
|
m.retryTimer.control = "stop"
|
|
m.retryCount = 0
|
|
channel = m.channels[index]
|
|
m.schedule = invalid
|
|
m.channelTitle.text = channelLabel(channel)
|
|
m.nowTitle.text = "NOW —"
|
|
m.nextTitle.text = "NEXT —"
|
|
m.progressTrack.visible = false
|
|
m.progress.width = 0
|
|
startPlayback()
|
|
showBanner()
|
|
refreshSchedule()
|
|
end sub
|
|
|
|
sub startPlayback()
|
|
if m.channelIndex < 0 then return
|
|
channel = m.channels[m.channelIndex]
|
|
m.weather.visible = channel.type = "weather"
|
|
m.playbackStatus.visible = false
|
|
if m.weather.visible
|
|
startWeather()
|
|
return
|
|
end if
|
|
stopWeather()
|
|
m.video.visible = true
|
|
m.video.translation = [0, 0]
|
|
m.video.width = 1280
|
|
m.video.height = 720
|
|
m.video.mute = false
|
|
if m.videoChannelId = channel.id and m.video.state = "playing" then return
|
|
setPlaybackStatus("Tuning…")
|
|
playLinear(channel)
|
|
end sub
|
|
|
|
sub playLinear(channel as object)
|
|
m.video.control = "stop"
|
|
m.videoChannelId = invalid
|
|
streamUrl = channel.streamUrl
|
|
if GetInterface(streamUrl, "ifString") = invalid or streamUrl = ""
|
|
setPlaybackStatus("This channel has no stream.")
|
|
return
|
|
end if
|
|
if Left(streamUrl, 1) = "/" then streamUrl = m.server.baseUrl + streamUrl
|
|
content = CreateObject("roSGNode", "ContentNode")
|
|
content.url = streamUrl
|
|
content.streamFormat = "hls"
|
|
content.live = true
|
|
content.title = channel.name
|
|
m.video.content = content
|
|
m.videoChannelId = channel.id
|
|
m.video.control = "play"
|
|
restoreRemoteFocus()
|
|
end sub
|
|
|
|
sub refreshSchedule()
|
|
if m.channelIndex < 0 then return
|
|
if m.scheduleRequest <> invalid
|
|
m.scheduleRequest.UnobserveFieldScoped("result")
|
|
m.scheduleRequest.control = "stop"
|
|
end if
|
|
channel = m.channels[m.channelIndex]
|
|
m.scheduleRequestedAt = CreateObject("roTimespan")
|
|
m.scheduleRequestedAt.Mark()
|
|
m.scheduleRequest = startRequest({
|
|
url: m.server.baseUrl + "/api/v1/channels/" + channel.id + "/now"
|
|
}, "onSchedule")
|
|
end sub
|
|
|
|
sub onSchedule(event as object)
|
|
if m.scheduleRequest = invalid or not m.scheduleRequest.IsSameNode(event.GetRoSGNode()) then return
|
|
m.scheduleRequest = invalid
|
|
if m.channelIndex < 0 then return
|
|
data = event.GetData().data
|
|
if data = invalid then return
|
|
m.schedule = data
|
|
syncClock(data.serverTime)
|
|
m.nowTitle.text = "NOW " + programTitle(data.now)
|
|
m.nextTitle.text = "NEXT " + programTitle(data.next)
|
|
refreshClock()
|
|
end sub
|
|
|
|
sub showBanner()
|
|
m.banner.visible = true
|
|
m.bannerTimer.control = "stop"
|
|
m.bannerTimer.control = "start"
|
|
refreshClock()
|
|
end sub
|
|
|
|
sub hideBanner()
|
|
m.banner.visible = false
|
|
end sub
|
|
|
|
sub syncClock(serverTime as dynamic)
|
|
epoch = parseUtc(serverTime)
|
|
if epoch = 0 then return
|
|
m.serverEpoch = epoch
|
|
m.clockReference.Mark()
|
|
end sub
|
|
|
|
function parseUtc(value as dynamic) as longinteger
|
|
if GetInterface(value, "ifString") = invalid or Len(value) < 19 then return 0
|
|
timestamp = CreateObject("roDateTime")
|
|
timestamp.FromISO8601String(Left(value, 19))
|
|
return timestamp.AsSeconds()
|
|
end function
|
|
|
|
function currentEpoch() as longinteger
|
|
if m.serverEpoch <> invalid then return m.serverEpoch + Int(m.clockReference.TotalMilliseconds() / 1000)
|
|
return CreateObject("roDateTime").AsSeconds()
|
|
end function
|
|
|
|
sub refreshClock()
|
|
if not m.banner.visible then return
|
|
now = currentEpoch()
|
|
clock = CreateObject("roDateTime")
|
|
clock.FromSeconds(now)
|
|
clock.ToLocalTime()
|
|
hour = clock.GetHours() MOD 12
|
|
if hour = 0 then hour = 12
|
|
meridiem = " AM"
|
|
if clock.GetHours() >= 12 then meridiem = " PM"
|
|
m.clock.text = hour.ToStr() + ":" + Right("0" + clock.GetMinutes().ToStr(), 2) + meridiem
|
|
|
|
if m.schedule = invalid or m.schedule.now = invalid then return
|
|
starts = parseUtc(m.schedule.now.start)
|
|
stops = parseUtc(m.schedule.now.stop)
|
|
if starts = 0 or stops <= starts then return
|
|
elapsed = now - starts
|
|
if elapsed < 0 then elapsed = 0
|
|
if now >= stops
|
|
elapsed = stops - starts
|
|
if m.scheduleRequestedAt.TotalMilliseconds() >= 10000 and m.scheduleRequest = invalid then refreshSchedule()
|
|
end if
|
|
m.progressTrack.visible = true
|
|
m.progress.width = 1124.0 * elapsed / (stops - starts)
|
|
end sub
|
|
|
|
function programTitle(program as dynamic) as string
|
|
if program = invalid or GetInterface(program.title, "ifString") = invalid then return "—"
|
|
return program.title
|
|
end function
|
|
|
|
function channelLabel(channel as object) as string
|
|
return channel.number.ToStr() + " " + channel.name
|
|
end function
|
|
|
|
sub setPlaybackStatus(message as string)
|
|
m.playbackStatus.text = message
|
|
m.playbackStatus.visible = true
|
|
end sub
|
|
|
|
sub restoreRemoteFocus()
|
|
if m.top.dialog <> invalid and m.top.dialog.IsInFocusChain() then return
|
|
if m.panel.visible and m.menu.content <> invalid and m.menu.content.GetChildCount() > 0
|
|
if not m.menu.HasFocus() then m.menu.SetFocus(true)
|
|
else if not m.remoteFocus.HasFocus()
|
|
m.remoteFocus.SetFocus(true)
|
|
end if
|
|
end sub
|
|
|
|
sub onVideoState()
|
|
restoreRemoteFocus()
|
|
if m.weather.visible
|
|
if m.video.state = "error" or m.video.state = "finished" then hideWeatherPreview()
|
|
return
|
|
end if
|
|
if m.channelIndex < 0 then return
|
|
if m.video.state = "playing"
|
|
m.playbackStatus.visible = false
|
|
m.retryCount = 0
|
|
else if m.video.state = "buffering"
|
|
setPlaybackStatus("Tuning…")
|
|
else if m.video.state = "error" or m.video.state = "finished"
|
|
if m.retryCount < 3
|
|
m.retryCount++
|
|
setPlaybackStatus("Reconnecting…")
|
|
m.retryTimer.control = "start"
|
|
else
|
|
setPlaybackStatus("Channel unavailable. Press OK to retry.")
|
|
end if
|
|
end if
|
|
end sub
|
|
|
|
sub retryPlayback()
|
|
startPlayback()
|
|
end sub
|
|
|
|
function onKeyEvent(key as string, press as boolean) as boolean
|
|
if not press then return false
|
|
if key = "back" and m.panel.visible and m.channelIndex >= 0
|
|
closePanel()
|
|
return true
|
|
end if
|
|
if key = "options" and (m.channelIndex >= 0 or m.panelMode = "guide" or m.panelMode = "options")
|
|
if m.panelMode = "guide"
|
|
showPanel("options", "retroLabs", m.server.name + " " + m.server.baseUrl, ["Program guide", "Refresh channels", "Change server"])
|
|
else
|
|
openGuide()
|
|
end if
|
|
return true
|
|
end if
|
|
if m.panel.visible or m.channelIndex < 0 then return false
|
|
|
|
if key = "up" or key = "down"
|
|
direction = 1
|
|
if key = "up" then direction = -1
|
|
openGuide(direction)
|
|
return true
|
|
else if key = "right"
|
|
openGuide()
|
|
return true
|
|
else if key = "left" or key = "replay"
|
|
if m.previousIndex >= 0 then tune(m.previousIndex)
|
|
return true
|
|
else if LCase(key) = "ok"
|
|
if not m.weather.visible and (m.video.state = "error" or m.video.state = "finished")
|
|
tune(m.channelIndex)
|
|
else
|
|
showBanner()
|
|
refreshSchedule()
|
|
end if
|
|
return true
|
|
end if
|
|
return false
|
|
end function
|