/
PlayerTask.bs
100 lines (82 loc) · 2.72 KB
/
PlayerTask.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import "pkg:/source/roku_modules/harmonicinc_vos_roku_rafx_ssai/SSAI.bs"
library "Roku_Ads.brs"
const url = ""
sub init()
m.top.adExited = false
m.top.functionName = "runTask"
m.top.adPods = []
m.ssai = false
end sub
sub runTask()
adapter = loadAdapter()
loadStream(adapter)
runLoop(adapter)
end sub
function loadAdapter()
adapter = new harmonicinc.vos.roku.rafx.ssai.RAFX_SSAI()
return adapter
end function
sub loadStream(adapter as Object)
if adapter = invalid then return
result = adapter.getStreamInfo(url)
if result.ssai
m.ssai = true
m.top.video.content.url = result.streamUrl
end if
end sub
sub runLoop(adapter as Object)
if adapter = invalid or not m.ssai
m.top.video.control = "play"
return
end if
addCallbacks(adapter)
port = CreateObject("roMessagePort")
adapter.enableAds({
player: {
sgNode: m.top.video,
port: port
},
useStitched: true
})
m.top.video.observeFieldScoped("position", port)
m.top.video.observeFieldScoped("control", port)
m.top.video.observeFieldScoped("state", port)
' Play video
m.top.video.control = "play"
while true
msg = wait(1000, port)
if type(msg) = "roSGNodeEvent" and msg.getField() = "control" and msg.getNode() = m.top.video.id and msg.getData() = "stop" or m.top.video = invalid
exit while
end if
curAd = adapter.onMessage(msg)
if curAd = invalid
m.top.video.setFocus(true)
end if
m.top.adExited = curAd?.adExited ?? false
if "roSGNodeEvent" = type(msg) and "state" = msg.getField() and "finished" = msg.getData() and msg.getNode() = m.top.video.id
exit while
end if
end while
m.top.video.unobserveFieldScoped("position")
m.top.video.unobserveFieldScoped("control")
m.top.video.unobserveFieldScoped("state")
end sub
sub addCallbacks(adapter as object)
adapter.addEventListener(adapter.AdEvent.PODS, podsCallback)
adapter.addEventListener(adapter.AdEvent.POD_START, podStartCallback)
adapter.addEventListener(adapter.AdEvent.IMPRESSION, adEventCallback)
adapter.addEventListener(adapter.AdEvent.FIRST_QUARTILE, adEventCallback)
adapter.addEventListener(adapter.AdEvent.MIDPOINT, adEventCallback)
adapter.addEventListener(adapter.AdEvent.THIRD_QUARTILE, adEventCallback)
adapter.addEventListener(adapter.AdEvent.COMPLETE, adEventCallback)
adapter.addEventListener(adapter.AdEvent.POD_END, podEndCallback)
end sub
sub podsCallback(event as object)
m.top.adPods = event.adPods
end sub
sub podStartCallback()
end sub
sub adEventCallback()
end sub
sub podEndCallback()
end sub