-
Notifications
You must be signed in to change notification settings - Fork 3
/
pp_chromeplayer.py
671 lines (538 loc) · 23.9 KB
/
pp_chromeplayer.py
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
#sudo pip3 install selenium
#sudo apt install chromium-chromedriver
import os
import copy
from selenium import webdriver
from selenium.common.exceptions import *
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from pp_player import Player
from pp_displaymanager import DisplayManager
class ChromePlayer(Player):
# ***************************************
# EXTERNAL COMMANDS
# ***************************************
def __init__(self,
show_id,
showlist,
root,
canvas,
show_params,
track_params ,
pp_dir,
pp_home,
pp_profile,
end_callback,
command_callback):
# initialise items common to all players
Player.__init__( self,
show_id,
showlist,
root,
canvas,
show_params,
track_params ,
pp_dir,
pp_home,
pp_profile,
end_callback,
command_callback)
self.mon.trace(self,'')
# and initialise things for this player
self.dm=DisplayManager()
# get duration limit (secs ) from profile
if self.track_params['duration'] != '':
self.duration_text=self.track_params['duration']
else:
self.duration_text= self.show_params['duration']
# process chrome window
if self.track_params['chrome-window'] != '':
self.chrome_window_text= self.track_params['chrome-window']
else:
self.chrome_window_text= self.show_params['chrome-window']
# process chrome things
if self.track_params['chrome-freeze-at-end'] != '':
self.freeze_at_end= self.track_params['chrome-freeze-at-end']
else:
self.freeze_at_end= self.show_params['chrome-freeze-at-end']
if self.track_params['chrome-zoom'] != '':
self.chrome_zoom_text= self.track_params['chrome-zoom']
else:
self.chrome_zoom_text= self.show_params['chrome-zoom']
if self.track_params['chrome-other-options'] != '':
self.chrome_other_options= self.track_params['chrome-other-options']
else:
self.chrome_other_options= self.show_params['chrome-other-options']
# Initialize variables
self.command_timer=None
self.tick_timer=None
self.quit_signal=False # signal that user has pressed stop
# initialise the play state
self.play_state='initialised'
self.load_state=''
# LOAD - loads the browser and show stuff
def load(self,track,loaded_callback,enable_menu):
# instantiate arguments
self.track=track
self.loaded_callback=loaded_callback # callback when loaded
self.mon.trace(self,'')
status,message,duration100=Player.parse_duration(self.duration_text)
if status =='error':
self.mon.err(self,message)
self.play_state='load-failed'
if self.loaded_callback is not None:
self.loaded_callback('error',message)
return
self.duration=2*duration100
# Is display valid and connected
status,message,self.display_id=self.dm.id_of_display(self.show_canvas_display_name)
if status == 'error':
self.mon.err(self,message)
self.play_state='load-failed'
if self.loaded_callback is not None:
self.loaded_callback('error','cannot find file; '+track )
return
# does media exist
if not ':' in self.track:
if not os.path.exists(self.track):
self.mon.err(self, 'cannot find file; '+self.track )
self.play_state='load-failed'
if self.loaded_callback is not None:
self.loaded_callback('error','cannot find file; '+self.track )
return
# do common bits of load
Player.pre_load(self)
# load the plugin, this may modify self.track and enable the plugin drawing to canvas
if self.track_params['plugin'] != '':
status,message=self.load_plugin()
if status == 'error':
self.mon.err(self,message)
self.play_state='load-failed'
if self.loaded_callback is not None:
self.loaded_callback('error',message)
return
# add file:// to files.
if ':' in self.track:
self.current_url=self.track
else:
self.current_url='file://'+self.track
# prepare chromium options
status,message=self.process_chrome_options()
if status == 'error':
self.mon.err(self, message)
self.play_state='load-failed'
if self.loaded_callback is not None:
self.loaded_callback('error',message)
return
# parse browser commands to self.command_list
reason,message=self.parse_commands(self.track_params['browser-commands'])
if reason == 'error':
self.mon.err(self,message)
self.play_state='load-failed'
if self.loaded_callback is not None:
self.loaded_callback('error',message)
return
# start loading the browser
self.play_state='loading'
# load the images and text
status,message=self.load_x_content(enable_menu)
if status == 'error':
self.mon.err(self,message)
self.play_state='load-failed'
if self.loaded_callback is not None:
self.loaded_callback('error',message)
return
#start the browser
self.driver_open()
# for kiosk and fullscreen need to get the url - in browser command for app mode
if self.app_mode is False:
self.driver_get(self.current_url)
self.mon.log (self,'Loading browser from show Id: '+ str(self.show_id))
self.play_state='loaded'
# and start executing the browser commands
self.play_commands()
self.mon.log(self," State machine: chromium loaded")
if self.loaded_callback is not None:
self.loaded_callback('normal','browser loaded')
return
# UNLOAD - abort a load when browser is loading or loaded
def unload(self):
self.mon.trace(self,'')
self.mon.log(self,">unload received from show Id: "+ str(self.show_id))
self.driver_close()
self.play_state = 'closed'
# SHOW - show a track from its loaded state
def show(self,ready_callback,finished_callback,closed_callback):
# instantiate arguments
self.ready_callback=ready_callback # callback when ready to show a web page-
self.finished_callback=finished_callback # callback when finished showing - not used
self.closed_callback=closed_callback # callback when closed
self.mon.trace(self,'')
self.play_state='showing'
# init state and signals
self.quit_signal=False
# do common bits
Player.pre_show(self)
#self.driver.get(self.current_url)
self.duration_count=self.duration
self.tick_timer=self.canvas.after(10, self.show_state_machine)
def show_state_machine(self):
if self.play_state == 'showing':
self.duration_count -= 1
# self.mon.log(self," Show state machine: " + self.show_state)
# service any queued stop signals and test duration count
if self.quit_signal is True or (self.duration != 0 and self.duration_count == 0):
self.mon.log(self," Service stop required signal or timeout")
if self.command_timer != None:
self.canvas.after_cancel(self.command_timer)
self.command_timer=None
if self.quit_signal is True:
self.quit_signal=False
if self.freeze_at_end =='yes':
self.mon.log(self,'chrome says pause_at_end')
if self.finished_callback is not None:
self.finished_callback('pause_at_end','pause at end')
self.tick_timer=self.canvas.after(50, self.show_state_machine)
else:
self.mon.log(self,'chrome says niceday')
self.driver_close()
self.play_state='closed'
if self.closed_callback is not None:
self.closed_callback('normal','chromedriver closed')
return
else:
self.tick_timer=self.canvas.after(50, self.show_state_machine)
# CLOSE - nothing to do in browserplayer - x content is removed by ready callback and hide browser does not implement pause_at_end
def close(self,closed_callback):
self.mon.trace(self,'')
self.closed_callback=closed_callback
self.mon.log(self,">close received from show Id: "+ str(self.show_id))
self.driver_close()
self.play_state='closed'
# PP does not use close callback but it does read self.play_state
def input_pressed(self,symbol):
self.mon.trace(self,symbol)
if symbol == 'pause':
self.pause()
elif symbol == 'pause-on':
self.pause_on()
elif symbol == 'pause-off':
self.pause_off()
elif symbol=='stop':
self.stop()
# browsers do not do pause
def pause(self):
self.mon.log(self,"!<pause rejected")
return False
# browsers do not do pause
def pause_on(self):
self.mon.log(self,"!<pause on rejected")
return False
# browsers do not do pause
def pause_off(self):
self.mon.log(self,"!<pause off rejected")
return False
# respond to normal stop
def stop(self):
# send signal to stop the track to the state machine
self.mon.log(self,">stop received")
self.quit_signal=True
# ***********************
# veneer for controlling chromium browser
# ***********************
def driver_open(self):
path = '/bin/chromedriver'
service = Service(executable_path=path) # Here
tries=4
while tries>0:
try:
self.driver = webdriver.Chrome(service=service,options=self.chrome_options)
return
except Exception as e:
print ("Failed to open Chromium\n", e, e.__class__,'\n')
tries-=1
def driver_close(self):
try:
self.driver.quit()
except WebDriverException as e:
self.mon.warn(self,'Browser Closed in Close !!!!!!\n'+str(e))
except Exception as e:
print("Oops!", e, e.__class__, "occurred.")
else:
return
def driver_refresh(self):
try:
self.driver.refresh()
except WebDriverException as e:
self.mon.warn(self,'Browser Closed in Refresh !!!!!!\n'+str(e))
except Exception as e:
print("Oops!", e, e.__class__, "occurred.")
else:
return
def driver_get(self,url):
self.mon.log(self,'get: '+url)
try:
self.driver.get(url)
except WebDriverException as e:
self.mon.warn(self,'Browser Closed in Get !!!!!!\n'+str(e))
except Exception as e:
print("Oops!",e, e.__class__, "occurred.")
else:
return
# *******************
# browser commands
# ***********************
def parse_commands(self,command_text):
self.command_list=[]
self.max_loops=-1 #loop continuous if no loop command
lines = command_text.split('\n')
for line in lines:
if line.strip() == '':
continue
#print (line)
reason,entry=self.parse_command(line)
if reason != 'normal':
return 'error',entry
self.command_list.append(copy.deepcopy(entry))
num_loops=0
for entry in self.command_list:
if entry[0]=='loop':
num_loops+=1
if num_loops>1:
return 'error', str(num_loops) + ' loop commands in browser commands'
return 'normal',''
def parse_command(self,line):
fields = line.split()
#print (fields)
if len(fields) not in (1,2):
return 'error',"incorrect number of fields in command: " + line
command=fields[0]
arg=''
if command not in ('load','refresh','wait','loop'):
return 'error','unknown browser command: '+ line
if command in ('refresh',) and len(fields) !=1:
return 'error','incorrect number of fields for '+ command + 'in: ' + line
if command in ('refresh',):
return 'normal',[command,'']
if command == 'load':
if len(fields)!=2:
return 'error','incorrect number of fields for '+ command + 'in: ' + line
arg=fields[1]
track=self.complete_path(arg)
# does media exist
if not ':' in track:
if not os.path.exists(track):
return 'error','cannot find file: '+track
# add file:// to files.
if ':' in track:
url=track
else:
url='file://'+track
return 'normal',[command,url]
if command == 'loop':
if len(fields)==1:
arg='-1'
self.max_loops=-1 #loop continuously if no argument
return 'normal',[command,arg]
elif len(fields)==2:
if not fields[1].isdigit() or fields[1]=='0':
return 'error','Argument for Loop is not a positive number in: ' + line
else:
arg = fields[1]
self.max_loops=int(arg)
return 'normal',[command,arg]
else:
return 'error','incorrect number of fields for '+ command + 'in: ' + line
if command == 'wait':
if len(fields)!=2:
return 'error','incorrect number of fields for '+ command + 'in: ' + line
else:
arg = fields[1]
if not arg.isdigit():
return 'error','Argument for Wait is not 0 or positive number in: ' + line
else:
return 'normal',[command,arg]
def play_commands(self):
# init
if len(self.command_list)==0:
return
self.loop_index=-1 # -1 no loop comand found
self.loop_count=0
self.command_index=0
self.next_command_index=0 #start at beginning
#loop round executing the commands
self.canvas.after(100,self.execute_command)
def execute_command(self):
self.command_index=self.next_command_index
if self.command_index==len(self.command_list):
# past end of command list
self.quit_signal = True
return
if self.command_index==len(self.command_list)-1 and self.loop_index!=-1:
# last in list and need to loop
self.next_command_index=self.loop_index
else:
self.next_command_index=self.command_index+1
entry=self.command_list[self.command_index]
command=entry[0]
arg=entry[1]
self.mon.log (self,str(self.command_index) + ' Do '+command+' '+arg + ' Next: '+str(self.next_command_index))
# and execute command
if command == 'load':
self.driver_get(arg)
self.command_timer=self.canvas.after(10,self.execute_command)
elif command == 'refresh':
self.driver_refresh()
self.command_timer=self.canvas.after(10,self.execute_command)
elif command == 'wait':
self.command_timer=self.canvas.after(1000*int(arg),self.execute_command)
elif command=='loop':
if self.loop_index==-1:
# found loop for first time
self.loop_index=self.command_index
self.loop_count=0
self.mon.log (self,'Loop init To: '+str(self.loop_index) + ' Count: '+str(self.loop_count))
self.command_timer=self.canvas.after(10,self.execute_command)
else:
self.loop_count+=1
self.mon.log (self,'Inc loop count: '+ ' Count: '+str(self.loop_count))
# hit loop command after the requied number of loops
if self.loop_count==self.max_loops: #max loops is -1 for continuous
self.mon.log (self,'end of loop: '+ ' Count: '+str(self.loop_count))
self.quit_signal=True
return
else:
self.mon.log (self,'Looping to: '+str(self.loop_index) + ' Count: '+str(self.loop_count))
self.command_timer=self.canvas.after(10,self.execute_command)
elif command=='exit':
self.quit_signal=True
return
def process_chrome_options(self):
self.chrome_options = Options()
#self.add_option("--incognito")
self.add_option("--noerrdialogs")
self.add_option("--disable-infobars")
self.add_option("--check-for-update-interval=31536000")
self.add_option('--disable-overlay-scrollbar')
self.chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
try:
self.zoom=float(self.chrome_zoom_text)
except ValueError:
return 'error','Chrome Zoom is not a number'+self.chrome_zoom_text
self.add_option('--force-device-scale-factor='+self.chrome_zoom_text)
status,message= self.process_chrome_window(self.chrome_window_text)
if status == 'error':
return 'error',message
status,message=self.add_other_options()
if status == 'error':
return 'error',message
return 'normal',''
def add_other_options(self):
opts_list=self.chrome_other_options.split (' ')
#print (opts_list)
for opt in opts_list:
if opt=='':
continue
if opt[0:2] != '--':
return 'error','option is not preceded by -- : '+ opt
else:
self.add_option(opt)
return 'normal',''
def add_option(self,option):
#print ('Adding Option: ',option)
self.chrome_options.add_argument(option)
def process_chrome_window(self,line):
#parse chrome window
# kiosk,fullscreen,showcanvas,display
# obxprop | grep "^_OB_APP" and click the window
self.app_mode=False
# showcanvas|display + [x+y+w*h]
words=line.split()
if len(words) not in (1,2):
return 'error','bad Chrome Web Window form '+line
if words[0] not in ('display','showcanvas','kiosk','fullscreen'):
return 'error','No or invalid Chrome Web Window mode: '+line
if len(words)==1 and words[0] == 'kiosk':
self.add_option('--kiosk')
x_org,y_org=self.dm.real_display_position(self.display_id)
self.add_option('--window-position='+str(x_org)+','+str(y_org))
return 'normal',''
if len(words)==1 and words[0] == 'fullscreen':
self.add_option('--start-fullscreen')
x_org,y_org=self.dm.real_display_position(self.display_id)
self.add_option('--window-position='+str(x_org)+','+str(y_org))
return 'normal',''
# display or showcanvas with or without dimensions
self.app_mode=True
if words[0] == 'display':
x_org,y_org=self.dm.real_display_position(self.display_id)
width,height= self.dm.real_display_dimensions(self.display_id)
if words[0] == 'showcanvas':
x_org,y_org=self.dm.real_display_position(self.display_id)
x_org+=self.show_canvas_x1
y_org+= self.show_canvas_y1
width=self.show_canvas_width
height=self.show_canvas_height
x_offset=0
y_offset=0
#calc offset and width/height from dimensions
if len(words)>1:
status,message,x_offset,y_offset,width,height=self.parse_dimensions(words[1],width,height)
if status =='error':
return 'error',message
#correct for zoom
width=int(width/self.zoom)
height=int(height/self.zoom)
x= x_org+x_offset
y= y_org+y_offset
self.chrome_window_x=x
self.chrome_window_y=y
self.chrome_window_width=width
self.chrome_window_height=height
#print ('app',self.app_mode,x,y,width,height)
self.add_option('--app='+self.current_url)
self.add_option('--window-size='+str(width)+','+str(height))
self.add_option('--window-position='+str(x)+','+str(y))
return 'normal',''
def parse_dimensions(self,dim_text,show_width,show_height):
if '+' in dim_text:
# parse x+y+width*height
fields=dim_text.split('+')
if len(fields) != 3:
return 'error','bad chrome window form '+dim_text,0,0,0,0
if not fields[0].isdigit():
return 'error','x is not a positive decimal in chrome web window '+dim_text,0,0,0,0
else:
x=int(fields[0])
if not fields[1].isdigit():
return 'error','y is not a positive decimal in chrome webwindow '+dim_text,0,0,0,0
else:
y=int(fields[1])
dimensions=fields[2].split('*')
if len(dimensions)!=2:
return 'error','bad chrome web window dimensions '+dim_text,'',0,0,0,0
if not dimensions[0].isdigit():
return 'error','width is not a positive decimal in chrome web window '+dim_text,0,0,0,0
else:
width=int(dimensions[0])
if not dimensions[1].isdigit():
return 'error','height is not a positive decimal in chrome web window '+dim_text,0,0,0,0
else:
height=int(dimensions[1])
return 'normal','',x,y,width,height
else:
#width*height
dimensions=dim_text.split('*')
if len(dimensions)!=2:
return 'error','bad chrome web window dimensions '+line,'',0,0,0,0
if not dimensions[0].isdigit():
return 'error','width is not a positive decimal in chrome web window '+line,'',0,0,0,0
else:
window_width=int(dimensions[0])
if not dimensions[1].isdigit():
return 'error','height is not a positive decimal in chrome web window '+line,'',0,0,0,0
else:
window_height=int(dimensions[1])
x=int((show_width-window_width)/2)
y=int((show_height-window_height)/2)
return 'normal','',x,y,window_width,window_height