This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOMXPlayerServer.py
122 lines (104 loc) · 3.52 KB
/
OMXPlayerServer.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
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn
from omxplayer import OMXPlayer
from subprocess import call
import threading
import json
import os
import shutil
import time
import sys
import getopt
SUPPORTED_FORMATS = ('.mkv', '.avi', '.mp4', '.mov', '.srt')
class ServerHandler(BaseHTTPRequestHandler):
def do_POST(self):
global player
global home_path
jsonData = json.loads(self.rfile.read(int(self.headers.getheader('content-length'))))
print 'Data received from phone : '
print jsonData
command = jsonData['command'];
if command == 'pause' and 'player' in globals():
player.pause()
elif command == 'seek_backward' and 'player' in globals():
player.action(19)
elif command == 'seek_forward' and 'player' in globals():
player.action(20)
elif command == 'seek_fast_backward' and 'player' in globals():
player.action(21)
elif command == 'seek_fast_forward' and 'player' in globals():
player.action(22)
elif command == 'previous_audio_stream' and 'player' in globals():
player.action(6)
elif command == 'next_audio_stream' and 'player' in globals():
player.action(7)
elif command == 'previous_subtitle_stream' and 'player' in globals():
player.action(10)
elif command == 'next_subtitle_stream' and 'player' in globals():
player.action(11)
elif command == 'decrease_subtitle_delay' and 'player' in globals():
player.action(13)
elif command == 'increase_subtitle_delay' and 'player' in globals():
player.action(14)
elif command == 'stop' and 'player' in globals():
player.stop()
player.quit()
del player
elif command == 'delete_file':
os.remove(jsonData['path'])
elif command == 'delete_folder':
shutil.rmtree(jsonData['path'])
elif command == 'poweroff':
call(['sudo', 'poweroff'])
elif command == 'reboot':
call(['sudo', 'reboot'])
elif command == 'play':
if 'player' in globals():
player.stop()
player.quit()
player = OMXPlayer(jsonData['path'], args=['-b'])
player.play()
elif command == 'is_playing':
if 'player' in globals():
SendResponse(self, {'command': command, 'path': player.get_filename()})
else:
SendResponse(self, {'command': command, 'path': ''})
elif command == 'list_dir':
path = jsonData['path'] if 'path' in jsonData else home_path
files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f)) and os.path.splitext(f)[1] in SUPPORTED_FORMATS]
dirs = [f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))]
files.sort()
dirs.sort()
SendResponse(self, {'command': command, 'path': path, 'files': files, 'dirs': dirs})
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
def SendResponse(self, response):
print 'Sending data to phone :'
print response
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(response))
def PrintUsage():
print 'Usage : python OMXPlayerServer.py -a <server ip> -p <server port> -d <home path>'
if __name__ == '__main__':
server_ip = ''
server_port = ''
home_path = ''
try:
opts, args = getopt.getopt(sys.argv[1:], 'ha:p:d:')
except getopt.GetoptError:
PrintUsage()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
PrintUsage()
sys.exit()
elif opt in ("-a"):
server_ip = arg
elif opt in ("-p"):
server_port = arg
elif opt in ("-d"):
home_path = arg
httpd = ThreadedHTTPServer((server_ip, int(server_port)), ServerHandler)
httpd.serve_forever()