forked from filmor/pyomxplayer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplay.py
130 lines (109 loc) · 3.96 KB
/
play.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
import sys
import os
import time
from pyomxplayer import OMXPlayer
import salt.utils.event
import subprocess
from datetime import datetime, timedelta
from dateutil import parser
import pytz
est=pytz.timezone('America/New_York')
# Logging
try:
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# create a file handler
handler = logging.FileHandler('/home/pi/play.log',mode='a')
handler.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(handler)
logger.addHandler(ch)
except Exception, e:
logger.error('Failed to open logger', exc_info=True)
DELAY_AFTER_PRESSING_START = .5
DELAY_IN_CHECK_POS = 2
SOCK_DIR = '/var/run/salt/minion'
if len(sys.argv) <= 1:
logger.debug("Need a file to play")
sys.exit()
logger.info("Attempting to play {}".format(str(sys.argv[1])))
#checks to see if movie is moving or if it's over
def checkpos():
p = o.position
time.sleep(DELAY_IN_CHECK_POS)
if p == o.position:
time.sleep(DELAY_IN_CHECK_POS)
if p == o.position:
logger.info(o.position)
logger.debug("Stopped for slow playback.")
return 'stopped'
else:
return "playing"
def wait_for_starttime(iso_format_starttime):
try:
starttime = parser.parse(iso_format_starttime)
print (starttime - est.localize(datetime.now())).seconds
logger.info("It is currently {}".format(str(datetime.now())))
logger.info("Waiting until {}".format(str(starttime)))
while(est.localize(datetime.now()) < starttime):
time.sleep(.1)
logger.info("Done waiting")
except Exception, e:
print "exception"
print Exception
print e
logger.debug("Improperly formatted starttime: was it ISO?")
return
#load movie and start paused
try:
o = OMXPlayer(sys.argv[1]) # carpenter1.mov 'carp/carp/carpenter1_ge.mov'
except Exception, e:
logger.error('Failed to open movie?', exc_info=True)
logger.info("OMXPlayer is: {}".format(str(o)))
o.pause()
#check to see if there's a starttime, in iso format, in which case wait
if len(sys.argv) >= 3:
wait_for_starttime(sys.argv[2])
os.system('killall cat;clear')
o.play()
logger.info("Playing")
time.sleep(DELAY_AFTER_PRESSING_START) # helps prevent instant stopping if computer hangs on start of play
while(1):
status = checkpos()
if status == 'stopped':
logger.info("Stopped.")
o.stop() # to ensure we don't lock up the machine by leaving omxplayer hanging
# regain screen if omxplayer has hogged it
subprocess.Popen(["startx"])
time.sleep(.4)
os.system('sudo killall Xorg')
# restart screen saver
os.system('cat /dev/urandom > /dev/tty1 &')
# notify salt master that it's over
#logger.info('Phoning home.')
#payload = {'data': 'stopped'}
#os.system("""salt-call event.fire_master '{"data": "stopped"}' 'omx'""")
a = os.popen(' ps -e | grep salt-minion').read().strip().split('\n')
logger.info("There are {} salt minions".format(len(a)))
try:
logger.info("ps yields:\n{}".format(str(a)))
except Exception, e:
logger.error("couldn't log?", exc_info=True)
try:
if len(a)>1:
logger.info("killing {} salt minions".format(len(a)))
output = os.popen('sudo killall salt-minion').read()
logger.info(output)
output = os.popen('sudo salt-minion -d').read()
logger.info(output)
except Exception, e:
logger.error("couldn't kill minions?", exc_info=True)
logger.info('Play me out, piano cat.')
sys.exit("Stopped")