-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomxserver.py
executable file
·131 lines (115 loc) · 3.39 KB
/
omxserver.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
#!/usr/bin/python
########################################################################################
#
# Copyright by Stefan Koch <StefanKoch@gmx.org>, 2015
#
# This file is part of omxAutomation
#
# omxAutomation is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# omxAutomation is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
#
########################################################################################
import paho.mqtt.client as mqtt
import subprocess
import time
import signal
import sys
#######################################################################################
#Defines you need to adapt
install_path = "/home/stefan/bin"
mqtt_broker_ip = "192.168.2.100"
mqtt_broker_port = "1883"
mqtt_omx_topic = "/omxserver/play"
#######################################################################################
omxproc = subprocess.Popen
playing = 0
run = True
def sigint_handler(signal, frame):
global run
print("OMXServer will terminate (SIGINT)")
run = False
def play_quit():
global playing
if (playing == 1):
print("QUIT PLAY, wait for termination")
omxproc.stdin.write('q')
while omxproc.poll() is None:
time.sleep(0.5)
print("Player has ended")
playing = 0
def start_play(url):
global playing
global omxproc
print url
if (playing == 1):
play_quit()
playing = 1
print("play "+url)
omxproc = subprocess.Popen(['/usr/bin/omxplayer',
'--key-config', install_path+'/omxkeys.txt',
'--win', '1 1 1920 1080',
'-o','local',
url],stdout=subprocess.PIPE,stdin=subprocess.PIPE)
def seek_fwd():
if (playing == 1):
print("SEEK FWD")
omxproc.stdin.write('r')
def seek_back():
if (playing == 1):
print("SEEK BACK")
omxproc.stdin.write('l')
def play_pause():
if (playing == 1):
print("PLAY/PAUSE")
omxproc.stdin.write(' ')
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(mqtt_omx_topic)
def dispatch_message(cmd, arg):
if (cmd == 'play'):
start_play(arg)
return
if (cmd == 'fwd'):
seek_fwd()
return
if (cmd == 'back'):
seek_back()
return
if (cmd == 'playpause'):
play_pause()
return
if (cmd == 'quit'):
play_quit()
def on_message(client, userdata, msg):
#print(msg.topic+" "+str(msg.payload))
message = msg.payload.split(",")
if (len(message) == 2):
cmd = message[0]
arg = message[1]
print("cmd="+cmd+" arg="+arg)
dispatch_message(cmd, arg)
#######################################################################################
print("OMXPLAYER Server")
signal.signal(signal.SIGINT, sigint_handler)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(mqtt_broker_ip, mqtt_broker_port, 60)
while run:
client.loop()
#fixme shutdown not as clean as desired now (hardcoded time)
print("player shuddown")
if (playing == 1):
omxproc.stdin.write('q')
time.sleep(2)
playing = 0