-
Notifications
You must be signed in to change notification settings - Fork 33
/
video_app_stream.go
419 lines (365 loc) · 12.8 KB
/
video_app_stream.go
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package main
import (
"fmt"
"os"
"sync"
"time"
"go.nanomsg.org/mangos/v3"
nanoPull "go.nanomsg.org/mangos/v3/protocol/pull"
nanoReq "go.nanomsg.org/mangos/v3/protocol/req"
// register transports
_ "go.nanomsg.org/mangos/v3/transport/all"
log "github.com/sirupsen/logrus"
)
type VideoStreamer interface {
mainLoop()
getControlChan() ( chan int )
setImageConsumer( imgConsumer *ImageConsumer )
forceOneFrame()
}
type AppStream struct {
stopChan chan bool
imgHandler *ImgHandler
controlSpec string
vidSpec string
logSpec string
udid string
controlSocket mangos.Socket
logSocket mangos.Socket
device *Device
controlMutex *sync.Mutex
}
func NewAppStream( stopChan chan bool, controlPort int, vidPort int, vidLogPort int, udid string, device *Device ) (*AppStream) {
self := &AppStream{
stopChan: stopChan,
imgHandler: NewImgHandler( stopChan, udid, device ),
controlSpec: fmt.Sprintf( "tcp://127.0.0.1:%d", controlPort ),
vidSpec: fmt.Sprintf( "tcp://127.0.0.1:%d", vidPort ),
logSpec: fmt.Sprintf( "tcp://127.0.0.1:%d", vidLogPort ),
udid: udid,
device: device,
controlMutex: &sync.Mutex{},
}
return self
}
func (self *AppStream) setImageConsumer( imgConsumer *ImageConsumer ) {
self.imgHandler.setImageConsumer( imgConsumer )
if self.controlSocket != nil {
self.controlMutex.Lock()
self.controlSocket.Send([]byte(`{"action": "oneframe"}`))
self.controlSocket.Recv()
self.controlMutex.Unlock()
}
}
func (self *AppStream) forceOneFrame() {
if self.controlSocket != nil {
self.controlMutex.Lock()
self.controlSocket.Send([]byte(`{"action": "oneframe"}`))
self.controlSocket.Recv()
self.controlMutex.Unlock()
}
}
func (self *AppStream) getControlChan() ( chan int ) {
return self.imgHandler.mainCh
}
func (self *AppStream) openControl() (mangos.Socket,bool,chan bool) {
var controlSocket mangos.Socket
var controlStopChan chan bool
failures := 0
for {
select {
case <- self.stopChan: return nil,true,nil
default:
}
var res int
controlSocket, res, controlStopChan = self.dialAppControl()
if res == 0 { break }
time.Sleep( time.Second * 10 )
failures = failures + 1
if failures >= 1 {
fmt.Printf("Failed to connect video app control 5 times. Giving up.")
return nil,true,nil
}
}
log.WithFields( log.Fields{
"type": "vidapp_control_connect",
"udid": censorUuid( self.udid ),
} ).Info("Vidapp - Control Connected")
// Health check
go func() {
for {
self.controlMutex.Lock()
if self.controlSocket == nil {
self.controlMutex.Unlock()
time.Sleep( time.Second * 1 )
continue
}
err := self.controlSocket.Send([]byte(`{"action": "ping"}`))
if err != nil {
fmt.Printf("video ping -> fail\n" )
self.controlMutex.Unlock()
controlStopChan <- true
break
}
_, err = self.controlSocket.Recv()
self.controlMutex.Unlock()
if err != nil {
fmt.Printf("video ping recv -> fail\n" )
controlStopChan <- true
break
}
//fmt.Printf("video ping -> %s\n", msg )
time.Sleep( time.Second * 2 )
}
}()
return controlSocket,false,controlStopChan
}
func (self *AppStream) openLog() (mangos.Socket,bool,chan bool) {
var logSocket mangos.Socket
var logStopChan chan bool
failures := 0
for {
select {
case <- self.stopChan: return nil,true,nil
default:
}
var res int
logSocket, res, logStopChan = self.dialAppLog()
if res == 0 { break }
time.Sleep( time.Second * 10 )
failures = failures + 1
if failures >= 1 {
fmt.Printf("Failed to connect video app log 5 times. Giving up.")
return nil,true,nil
}
}
log.WithFields( log.Fields{
"type": "vidapp_log_connect",
"udid": censorUuid( self.udid ),
} ).Info("Vidapp - Log Connected")
go func() {
for {
msg, err := self.logSocket.RecvMsg()
if err != nil { break }
log.WithFields( log.Fields{
"type": "vidapp_log",
"udid": censorUuid( self.udid ),
"msg": string(msg.Body),
} ).Info("Vidapp - Log")
}
}()
return logSocket,false,logStopChan
}
func (self *AppStream) openVideo() (mangos.Socket,bool,chan bool) {
var imgSocket mangos.Socket
var vidStopChan chan bool
//fmt.Printf("Attempting to connect to video\n")
failures := 0
for {
select {
case <- self.stopChan: return nil,true,nil
default:
}
var res int
imgSocket, res, vidStopChan = self.dialAppVideo()
if res == 0 { break }
time.Sleep( time.Second * 1 )
failures = failures + 1
if failures >= 1 {
fmt.Printf("Failed to connect video app stream 5 times. Giving up.")
return nil,true,nil
}
}
log.WithFields( log.Fields{
"type": "vidapp_control_connect",
"udid": censorUuid( self.udid ),
} ).Info("Vidapp - Video Connected")
return imgSocket,false,vidStopChan
}
func (self *AppStream) mainLoop() {
go func() {
//var controlSocket mangos.Socket
var imgSocket mangos.Socket
var controlStopChan chan bool
var vidStopChan chan bool
var logStopChan chan bool
//imgHandler := NewImgHandler( self.stopChan, self.udid )
self.imgHandler.setEnableStream( func() {
fmt.Printf("Sending start to vidapp\n")
self.controlMutex.Lock()
self.controlSocket.Send([]byte(`{"action": "start"}`))
self.controlSocket.Recv()
self.controlMutex.Unlock()
} )
self.imgHandler.setDisableStream( func() {
fmt.Printf("Sending stop to vidapp\n")
self.controlMutex.Lock()
self.controlSocket.Send([]byte(`{"action": "stop"}`))
self.controlSocket.Recv()
self.controlMutex.Unlock()
} )
firstConnect := true
for {
var done bool
if self.controlSocket == nil {
self.controlSocket,done,controlStopChan = self.openControl()
if done { break }
}
if self.logSocket == nil {
self.logSocket,done,logStopChan = self.openLog()
if done { break }
}
if imgSocket == nil {
imgSocket,done,vidStopChan = self.openVideo()
if done { break }
}
self.imgHandler.setSource( imgSocket )
if firstConnect {
self.controlMutex.Lock()
self.controlSocket.Send([]byte(`{"action": "start"}`))
self.controlSocket.Recv()
self.controlMutex.Unlock()
firstConnect = false
}
res := self.imgHandler.mainLoop( vidStopChan, controlStopChan, logStopChan )
if res == 1 { break } // stopChan
if res == 2 { imgSocket = nil } // imgSocket connection lost
if res == 3 { // controlSocket connection lost
// Either the app has died or network to it has been lost
// Check if the app is still alive
alive := self.device.vidAppIsAlive()
// If not restart it
if !alive {
fmt.Printf("Video broadcast died. Restarting\n")
config := self.device.config
res := self.device.bridge.LaunchApp( config.vidAppBidPrefix + "." + config.vidAppBid ) // com.dryark.vidstream
if res == false {
appPath := "bin/vidstream/vidstream.app"
if _, err := os.Stat(appPath); err == nil {
self.device.bridge.InstallApp( appPath )
} else if os.IsNotExist(err) {
// TODO: panic.
}
}
self.device.justStartBroadcast()
self.controlSocket = nil
imgSocket = nil
self.logSocket = nil
} else {
self.controlSocket = nil
}
}
if res == 4 { // lost send socket
fmt.Printf("Lost send socket; disabling video stream\n")
self.controlMutex.Lock()
self.controlSocket.Send([]byte(`{"action": "stop"}`))
self.controlSocket.Recv()
self.controlMutex.Unlock()
}
if res == 5 {
self.logSocket = nil
}
}
if self.controlSocket != nil { self.controlSocket.Close() }
if imgSocket != nil { imgSocket.Close() }
if self.logSocket != nil { self.logSocket.Close() }
}()
}
func ( self *AppStream) dialAppVideo() ( mangos.Socket, int, chan bool ) {
vidSpec := self.vidSpec
var err error
var pullSock mangos.Socket
if pullSock, err = nanoPull.NewSocket(); err != nil {
log.WithFields( log.Fields{
"type": "err_socket_new",
"zmq_spec": vidSpec,
"err": err,
} ).Info("Socket new error")
return nil, 1, nil
}
sec1, _ := time.ParseDuration( "2s" )
setError := pullSock.SetOption( mangos.OptionRecvDeadline, sec1 )
if setError != nil {
fmt.Printf("Set timeout error %s\n", setError )
os.Exit(0)
}
if err = pullSock.Dial(vidSpec); err != nil {
log.WithFields( log.Fields{
"type": "err_socket_dial",
"spec": vidSpec,
"err": err,
} ).Info("Socket dial error")
return nil, 2, nil
}
vidStopChan := make( chan bool )
pullSock.SetPipeEventHook( func( action mangos.PipeEvent, pipe mangos.Pipe ) {
//fmt.Printf("Pipe action %d\n", action )
if action == 2 { vidStopChan <- true }
} )
return pullSock, 0, vidStopChan
}
func (self *AppStream) dialAppControl() ( mangos.Socket, int, chan bool ) {
controlSpec := self.controlSpec
var err error
var reqSock mangos.Socket
if reqSock, err = nanoReq.NewSocket(); err != nil {
log.WithFields( log.Fields{
"type": "err_socket_new",
"zmq_spec": controlSpec,
"err": err,
} ).Info("Socket new error")
return nil, 1, nil
}
sec1, _ := time.ParseDuration( "1s" )
setError := reqSock.SetOption( mangos.OptionRecvDeadline, sec1 )
if setError != nil {
fmt.Printf("Set timeout error %s\n", setError )
os.Exit(0)
}
setError = reqSock.SetOption( mangos.OptionSendDeadline, sec1 )
if setError != nil {
fmt.Printf("Set timeout error %s\n", setError )
os.Exit(0)
}
if err = reqSock.Dial( controlSpec ); err != nil {
log.WithFields( log.Fields{
"type": "err_socket_dial",
"spec": controlSpec,
"err": err,
} ).Info("Socket dial error")
return nil, 2, nil
}
controlStopChan := make( chan bool )
reqSock.SetPipeEventHook( func( action mangos.PipeEvent, pipe mangos.Pipe ) {
//fmt.Printf("Pipe action %d\n", action )
if action == 2 { controlStopChan <- true }
} )
return reqSock, 0, controlStopChan
}
func (self *AppStream) dialAppLog() ( mangos.Socket, int, chan bool ) {
logSpec := self.logSpec
var err error
var reqSock mangos.Socket
if reqSock, err = nanoPull.NewSocket(); err != nil {
log.WithFields( log.Fields{
"type": "err_socket_new",
"zmq_spec": logSpec,
"err": err,
} ).Info("Socket new error")
return nil, 1, nil
}
if err = reqSock.Dial( logSpec ); err != nil {
log.WithFields( log.Fields{
"type": "err_socket_dial",
"spec": logSpec,
"err": err,
} ).Info("Socket dial error")
return nil, 2, nil
}
logStopChan := make( chan bool )
reqSock.SetPipeEventHook( func( action mangos.PipeEvent, pipe mangos.Pipe ) {
//fmt.Printf("Pipe action %d\n", action )
if action == 2 { logStopChan <- true }
} )
return reqSock, 0, logStopChan
}