-
Notifications
You must be signed in to change notification settings - Fork 9
/
trimeplay.brs
222 lines (202 loc) · 9.2 KB
/
trimeplay.brs
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
' trimePlay
' This was written in less than a week after I decided I would really like to view photos from my recent holiday on my TV
' So don't expect that much ;)
' The key to understanding this nightmare of spaghetti is as follows:
' Brightscript is single-threaded. Therefore nothing may block
' Everything is a finite state machine. When data arrives on a socket then we look up the FSM for that socket, and call
' appropriate function to parse from (or write) to the socket.
' There are 4 kinds of these machines:
' * UDP messages, which handle mDNS to announce the roku on the network. Handled in mdns.brs
' * HTTP connections *from* the iDevice. These are like POST /play and GET /scrub. handled in http_reply.brs
' * HTTP connection replies to messages sent via the reverse HTTP (typically these are not very interesting, but must be handled)
' * HTTP connection replies to MP4 data requests. These are more interesting, and are handled in mp4.brs
Function Main()
'ztest()
'moov_test()
msgPort = createobject("roMessagePort")
'm.mac = "CA:FE:BA:BE:FA:17" ' FIXME: fake?
'm.mac = "B8:3E:59:39:9F:FD"
'm.features_hex = "0x3"
'm.features_dec = "3"
'm.features_hex = "0x39f7"
'm.features_dec = "14839"
m.features_hex = "0x77"
m.features_dec = "16383"
' Set up some stuff so we can display screens later in the http handlers
m.port = msgPort
m.state = "none"
m.video_state = 0
m.video_position = 0
device_info = createobject("roDeviceInfo")
m.display_size = device_info.getDisplaySize()
default_screen = createobject("roImageCanvas")
default_screen.SetMessagePort(msgPort)
default_screen.SetLayer(0, {Color:"#FF000000", CompositionMode:"Source"})
default_screen.SetLayer(1, {text:"Waiting for connection"
TextAttrs:{Color:"#FFCCCCCC", Font:"Medium",
HAlign:"HCenter", VAlign:"VCenter",
Direction:"LeftToRight"}
TargetRect:{x:0,y:0,w:m.display_size.w,h:m.display_size.h}})
default_screen.show()
m.mac_no_colon = UCase(get_mac(msgPort))
m.mac = Mid(m.mac_no_colon, 1, 2) + ":" + Mid(m.mac_no_colon, 3, 2) + ":" + Mid(m.mac_no_colon, 5, 2) + ":" + Mid(m.mac_no_colon, 7, 2) + ":" + Mid(m.mac_no_colon, 9, 2) + ":" + Mid(m.mac_no_colon, 11, 2)
print "Mac: " ; m.mac
m.reverse_connection = invalid
m.connections = {}
m.sockets = {}
udp = createobject("roDatagramSocket")
udp.setMessagePort(msgPort)
udp_bind_addr = createobject("roSocketAddress")
udp_bind_addr.setPort(5353)
udp.setAddress(udp_bind_addr)
group = createobject("roSocketAddress")
group.setHostName("224.0.0.251")
result = udp.joinGroup(group)
udp.setMulticastLoop(false)
' Set up the about-to-be-advertised TCP socket
tcp = createobject("roStreamSocket")
tcp.setMessagePort(msgPort)
tcp_bind_addr = CreateObject("roSocketAddress")
tcp_bind_addr.setPort(7000)
tcp.setAddress(tcp_bind_addr)
tcp.notifyReadable(true)
tcp.listen(4)
if not tcp.eOK()
print "Could not create TCP socket"
stop
end if
' Set up the unadvertised mirroring TCP socket just for fun
mirror = createobject("roStreamSocket")
mirror.setMessagePort(msgPort)
mirror_bind_addr = CreateObject("roSocketAddress")
mirror_bind_addr.setPort(7100)
mirror.setAddress(mirror_bind_addr)
mirror.notifyReadable(true)
mirror.listen(4)
if not mirror.eOK()
print "Could not create mirror TCP socket"
stop
end if
' Need to broadcast that we are an Apple TV, rather than just waiting to be polled. Sometimes this helps
udp.setBroadcast(true)
addr = createobject("roSocketAddress")
addr.setPort(5353)
addr.setHostName("224.0.0.251")
udp.setSendToAddress(addr)
announce = announce_packet()
print "Announcing our existence to the network"
result = udp.send(announce, 0, announce.Count())
udp.notifyReadable(true)
While true
print "Waiting in main loop"
event = wait(0, msgPort)
If type(event)="roSocketEvent"
'print "Got event on " ; event.getSocketID()
If event.getSocketID() = udp.getID()
If udp.isReadable()
message = createobject("roByteArray")
size = udp.getCountRcvBuf()
' Work around bug in upd.receive() :-S
message[size] = 0
message[size] = invalid
udp.receive(message, 0, size)
from = udp.getReceivedFromAddress()
'print "Received message of length " ; str(size) ; " from " ; from.getAddress()
dns = parse_dns(message)
respond_to_dns(dns, udp)
End If
Else If event.getSocketID() = tcp.getID()
client = tcp.accept()
If client = Invalid
print "Accept failed"
Else
client.notifyReadable(true)
client.setMessagePort(msgPort)
m.sockets[Stri(client.getID())] = client
End If
Else if event.getSocketID() = mirror.getID()
print "MIRRORING CONNECTION?"
client = mirror.accept()
If client = Invalid
print "Accept failed"
Else
client.notifyReadable(true)
client.setMessagePort(msgPort)
m.sockets[Stri(client.getID())] = client
End If
Else
' Must be a client connection!
connection = m.sockets[Stri(event.getSocketID())]
' If connection is invalid, what does that mean?
if connection <> invalid
' FIXME: Is this still right since I added the mp4 stuff? Do we actually correctly close sockets?
if connection.isReadable() and connection.getCountRcvBuf() = 0 Then
' Apparently this means the connection has been closed
' What a terrible way to indicate it
print "Connection " ; connection.getID() ; " is closed"
connection.close()
m.sockets[Stri(event.getSocketID())] = invalid
Else
'print "tcp " ; event.getSocketID() ; connection.eOK() ; connection.status() ; connection.isConnected() ; connection.isReadable() ; connection.getCountRcvBuf() = 0
handle_tcp(connection)
End If
else
print "Invalid connection"
stop
End If
End If
Else If type(event)="roVideoScreenEvent" Then
if event.isStreamStarted()
m.video_state = 1 ' playing
send_event("state", "playing")
m.video_position = event.GetIndex()
else if event.isPlaybackPosition()
m.video_position = event.GetIndex()
else if event.isPaused()
m.video_state = 2 'paused
send_event("state", "paused")
else if event.isResumed()
send_event("state", "playing")
m.video_state = 1 ' playing
else if event.isRequestFailed()
print "Request failed: " ; event.GetMessage() ; event.GetInfo() ; event.GetIndex()
else if event.isStatusMessage()
print "Status message: " ; event.GetMessage()
else
print "Unexpected roVideoScreenEvent"
End If
'print "Position is now "; m.video_position
Else
print "Unexpected event: " ; type(event)
End If
End While
udp.close()
End Function
Sub handle_tcp(connection as Object)
request = m.connections[Stri(connection.getID())]
If request = invalid Then
' An unsolicited request. Create an http handler for it
request = create_new_request()
m.connections[Stri(connection.getID())] = request
End if
'print "Reading some stuff"
status = request.read_data(request, connection)
If status = false Then ' More data is required
'print "Not yet finished"
return
Else if status = true Then 'Data is complete. Execute handler
' Regardless of whether the socket is to be closed, the HTTP request has finished. We have to invalidate it here
' since the process_data() call might set up something else
m.connections[Stri(connection.getID())] = invalid
should_close = request.process_data(request, connection)
if should_close then
m.sockets[Stri(connection.getID())] = invalid
connection.close()
end if
Else ' Error condition. Not handled (FIXME! Need to return ints instead of booleans so we have a third case. Or invalid?)
stop
End If
End Sub
Function starts_with(source as String, prefix as String)
return left(source, len(prefix)) = prefix
End Function