renatofilho@484
|
1 |
#!/usr/bin/python
|
renatofilho@484
|
2 |
|
renatofilho@484
|
3 |
import os
|
renatofilho@484
|
4 |
import lib
|
renatofilho@484
|
5 |
import sys
|
morphbr@511
|
6 |
import imp
|
renatofilho@484
|
7 |
import ConfigParser
|
morphbr@511
|
8 |
import logging as log
|
morphbr@511
|
9 |
|
morphbr@511
|
10 |
log.basicConfig(level=log.DEBUG,
|
morphbr@511
|
11 |
format="%(asctime)s %(levelname)-8s %(message)s",
|
morphbr@511
|
12 |
datefmt='%Y-%m-%d %H:%M:%S')
|
renatofilho@484
|
13 |
|
renatofilho@484
|
14 |
config = ConfigParser.ConfigParser()
|
renatofilho@484
|
15 |
config.read("stream.conf")
|
renatofilho@484
|
16 |
|
morphbr@511
|
17 |
def load_module(pathlist, name):
|
morphbr@511
|
18 |
fp, path, desc = imp.find_module(name, pathlist)
|
morphbr@511
|
19 |
try:
|
morphbr@511
|
20 |
module = imp.load_module(name, fp, path, desc)
|
morphbr@511
|
21 |
return module
|
morphbr@511
|
22 |
finally:
|
morphbr@511
|
23 |
if fp:
|
morphbr@511
|
24 |
fp.close()
|
morphbr@511
|
25 |
|
morphbr@511
|
26 |
|
renatofilho@484
|
27 |
media_plugin = config.get("Media", "engine")
|
morphbr@511
|
28 |
media_plugin_module = load_module(["./plugins/media"], media_plugin)
|
morphbr@511
|
29 |
media = media_plugin_module.Media(config)
|
renatofilho@484
|
30 |
|
renatofilho@484
|
31 |
comm_plugin = config.get("Comm", "engine")
|
morphbr@511
|
32 |
comm_plugin_module = load_module(["./plugins/comm"], comm_plugin)
|
morphbr@511
|
33 |
server = comm_plugin_module.Server(config)
|
renatofilho@484
|
34 |
|
morphbr@511
|
35 |
log.info("Starting GMyth-Stream server")
|
renatofilho@484
|
36 |
|
morphbr@504
|
37 |
|
morphbr@504
|
38 |
'''
|
morphbr@504
|
39 |
PROTOCOL DESCRIPTION
|
morphbr@504
|
40 |
=====================
|
morphbr@504
|
41 |
|
morphbr@504
|
42 |
COMMAND OPTIONS
|
morphbr@504
|
43 |
|
morphbr@504
|
44 |
-> SETUP DESCRIPTION
|
morphbr@504
|
45 |
|-> used to setup transcoding and streaming parameters
|
morphbr@504
|
46 |
|-> must be used before any "PLAY" command
|
morphbr@504
|
47 |
|-> e.g:
|
morphbr@504
|
48 |
|
morphbr@504
|
49 |
file://file_name mux vcodec vbitrate fps acodec abitrate width height options
|
morphbr@504
|
50 |
dvd://title_number mux vcodec vbitrate fps acodec abitrate width height options
|
morphbr@504
|
51 |
|
morphbr@504
|
52 |
-> PLAY DESCRIPTION
|
morphbr@504
|
53 |
|-> used to start transcoding and streaming of file
|
morphbr@504
|
54 |
|-> must be used just if SETUP was used before
|
morphbr@504
|
55 |
|-> after it, _must_ send STOP
|
morphbr@504
|
56 |
|
morphbr@504
|
57 |
-> STOP DESCRIPTION
|
morphbr@504
|
58 |
|-> used to stop transcoding and streaming process
|
morphbr@504
|
59 |
|-> must be used just if PLAY was used before
|
morphbr@504
|
60 |
|-> must be used after PLAY
|
morphbr@504
|
61 |
|
morphbr@504
|
62 |
-> QUIT DESCRIPTION
|
morphbr@504
|
63 |
|-> used to quit the main loop (quit program)
|
morphbr@504
|
64 |
|
morphbr@504
|
65 |
'''
|
morphbr@504
|
66 |
nextport = 0
|
morphbr@504
|
67 |
|
morphbr@511
|
68 |
def do_setup(server, filename, mux, vcodec, vbitrate, fps, acodec, abitrate,
|
morphbr@511
|
69 |
width, height, *options):
|
morphbr@511
|
70 |
global nextport
|
morphbr@511
|
71 |
nextport += 1
|
morphbr@511
|
72 |
ret = media.setup(filename, mux, vcodec, vbitrate, fps, acodec,
|
morphbr@511
|
73 |
abitrate, width, height, nextport, options)
|
morphbr@516
|
74 |
if ret:
|
morphbr@511
|
75 |
server.sendOk()
|
morphbr@511
|
76 |
else:
|
morphbr@511
|
77 |
server.sendNotOk(ret)
|
morphbr@516
|
78 |
|
morphbr@511
|
79 |
return True
|
morphbr@511
|
80 |
|
morphbr@511
|
81 |
def do_play(server):
|
morphbr@516
|
82 |
ret = media.play()
|
morphbr@516
|
83 |
if ret:
|
morphbr@516
|
84 |
server.sendOk("%d" % nextport)
|
morphbr@516
|
85 |
else:
|
morphbr@516
|
86 |
server.sendNotOk(ret)
|
morphbr@516
|
87 |
|
morphbr@511
|
88 |
return True
|
morphbr@511
|
89 |
|
morphbr@511
|
90 |
def do_stop(server):
|
morphbr@511
|
91 |
media.stop()
|
morphbr@511
|
92 |
server.sendOk()
|
morphbr@511
|
93 |
return True
|
morphbr@511
|
94 |
|
morphbr@516
|
95 |
def do_list(server, *directory):
|
morphbr@516
|
96 |
file_list = []
|
morphbr@516
|
97 |
for j in directory:
|
morphbr@516
|
98 |
list_media_files(j, file_list)
|
morphbr@516
|
99 |
|
morphbr@516
|
100 |
server.sendOk()
|
morphbr@516
|
101 |
return True
|
morphbr@516
|
102 |
|
morphbr@511
|
103 |
def do_quit(server):
|
morphbr@511
|
104 |
server.finish = 1
|
morphbr@511
|
105 |
media.stop()
|
morphbr@511
|
106 |
server.sendOk()
|
morphbr@511
|
107 |
return True
|
morphbr@511
|
108 |
|
morphbr@511
|
109 |
|
morphbr@511
|
110 |
mapping = {
|
morphbr@511
|
111 |
"SETUP": do_setup,
|
morphbr@511
|
112 |
"PLAY": do_play,
|
morphbr@511
|
113 |
"STOP": do_stop,
|
morphbr@516
|
114 |
"LIST": do_list,
|
morphbr@511
|
115 |
"QUIT": do_quit,
|
morphbr@511
|
116 |
}
|
morphbr@511
|
117 |
|
morphbr@511
|
118 |
def dispatch(server, msg):
|
morphbr@511
|
119 |
pieces = msg.split()
|
morphbr@511
|
120 |
if len(pieces) < 1:
|
morphbr@511
|
121 |
log.error("Invalid client command format: %r" % msg)
|
morphbr@511
|
122 |
server.sendNotOk("Invalid Format")
|
morphbr@511
|
123 |
return False
|
morphbr@511
|
124 |
|
morphbr@511
|
125 |
cmd = pieces[0]
|
morphbr@511
|
126 |
f = mapping.get(cmd, None)
|
morphbr@511
|
127 |
if not f:
|
morphbr@511
|
128 |
log.error("Unknow command: %r" % msg)
|
morphbr@511
|
129 |
server.sendNotOk("Unknow Command")
|
morphbr@511
|
130 |
return False
|
morphbr@511
|
131 |
|
morphbr@511
|
132 |
try:
|
morphbr@511
|
133 |
return f(server, *pieces[1:])
|
morphbr@511
|
134 |
except Exception, e:
|
morphbr@511
|
135 |
log.error("Could not execute %r: %s" % (msg, e))
|
morphbr@511
|
136 |
server.sendNotOk(str(e))
|
morphbr@511
|
137 |
return False
|
morphbr@511
|
138 |
|
morphbr@511
|
139 |
|
morphbr@511
|
140 |
|
morphbr@511
|
141 |
while not server.finish:
|
morphbr@504
|
142 |
conn, client, port = server.getRequest()
|
morphbr@504
|
143 |
if nextport == 0:
|
morphbr@504
|
144 |
nextport = port
|
renatofilho@484
|
145 |
|
morphbr@511
|
146 |
while not server.finish:
|
morphbr@511
|
147 |
msg = server.getMsg()
|
morphbr@511
|
148 |
if not msg:
|
renatofilho@484
|
149 |
break
|
renatofilho@484
|
150 |
|
morphbr@511
|
151 |
log.info("Client %s sent command: %r" % (client, msg))
|
morphbr@511
|
152 |
dispatch(server, msg)
|
morphbr@511
|
153 |
|
morphbr@511
|
154 |
|
morphbr@511
|
155 |
log.info("Closing connection with %s" % (client,))
|
morphbr@504
|
156 |
server.disconnect_client(conn)
|
renatofilho@484
|
157 |
|
renatofilho@484
|
158 |
server.stop()
|
morphbr@511
|
159 |
log.info("Server stopped. Closing...")
|
renatofilho@484
|
160 |
|