[svn r573] New function to get the information about the program info path directiory.
3 __author__ = "Gustavo Sverzut Barbieri / Artur Duque de Souza"
4 __author_email__ = "barbieri@gmail.com / artur.souza@indt.org.br"
15 import lib.utils as utils
18 __all__ = ("Transcoder", "RequestHandler", "Server", "serve_forever",
19 "load_plugins_transcoders")
21 class Transcoder(object):
22 log = log.getLogger("gmyth-stream.transcoder")
23 priority = 0 # negative values have higher priorities
24 name = None # to be used in requests
26 def __init__(self, params):
31 def params_first(self, key, default=None):
33 return self.params[key][0]
36 return self.params[key][0]
42 def get_mimetype(self):
43 mux = self.params_first("mux", "mpg")
48 return "video/x-msvideo"
50 return "application/octet-stream"
54 def start(self, outfile):
65 return '%s("%s", mux="%s", params=%s)' % \
66 (self.__class__.__name__,
67 self.params_first("uri", "None"),
68 self.params_first("mux", "mpg"),
75 class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
76 log = log.getLogger("gmyth-stream.request")
78 transcoders = utils.PluginSet(Transcoder)
81 def load_plugins_transcoders(cls, directory):
82 cls.transcoders.load_from_directory(directory)
84 if cls.def_transcoder is None and cls.transcoders:
85 cls.def_transcoder = cls.transcoders[0].name
86 # load_plugins_transcoders()
89 def do_dispatch(self, body):
92 pieces = urlparse.urlparse(self.path)
94 self.query = cgi.parse_qs(pieces[4])
98 elif self.path == "/shutdown.do":
99 self.serve_shutdown(body)
100 elif self.path == "/stop-transcoder.do":
101 self.serve_stop_transcoder(body)
102 elif self.path == "/status.do":
103 self.serve_status(body)
104 elif self.path == "/play.do":
105 self.serve_play(body)
106 elif self.path == "/stream.do":
107 self.serve_stream(body)
109 self.send_error(404, "File not found")
114 self.do_dispatch(True)
119 self.do_dispatch(False)
123 def _nav_items(self):
124 self.wfile.write("""\
125 <li><a href="/play.do">Play</a></li>
126 <li><a href="/status.do">Status</a></li>
127 <li><a href="/stop-transcoder.do">Stop transcoders</a></li>
128 <li><a href="/shutdown.do">Shutdown Server</a></li>
133 def serve_main(self, body):
134 self.send_response(200)
135 self.send_header("Content-Type", "text/html")
136 self.send_header('Connection', 'close')
139 self.wfile.write("""\
141 <head><title>Catota Server</title></head>
143 <h1>Welcome to Catota Server</h1>
147 self.wfile.write("""\
155 def serve_play(self, body):
156 self.send_response(200)
157 self.send_header("Content-Type", "text/html")
158 self.send_header('Connection', 'close')
161 self.wfile.write("""\
163 <head><title>Catota Server</title></head>
166 <form action="/stream.do" method="GET">
167 <input type="text" name="type" value="file" />://<input type="text" name="location" value=""/>
168 <input type="submit" />
173 self.wfile.write("""\
181 def serve_shutdown(self, body):
182 self.send_response(200)
183 self.send_header("Content-Type", "text/html")
184 self.send_header('Connection', 'close')
187 self.wfile.write("""\
189 <head><title>Catota Server Exited</title></head>
191 <h1>Catota is not running anymore</h1>
195 self.server.server_close()
199 def serve_stop_all_transcoders(self, body):
200 self.send_response(200)
201 self.send_header("Content-Type", "text/html")
202 self.send_header('Connection', 'close')
205 self.server.stop_transcoders()
206 self.wfile.write("""\
208 <head><title>Catota Server Stopped Transcoders</title></head>
210 <h1>Catota stopped running transcoders</h1>
214 self.wfile.write("""\
219 # serve_stop_all_transcoders()
222 def serve_stop_selected_transcoders(self, body, requests):
223 self.send_response(200)
224 self.send_header("Content-Type", "text/html")
225 self.send_header('Connection', 'close')
228 self.wfile.write("""\
230 <head><title>Catota Server Stopped Transcoders</title></head>
232 <h1>Catota stopped running transcoders:</h1>
235 transcoders = self.server.get_transcoders()
239 host, port = req.split(":")
246 for t, r in transcoders:
247 if r.client_address == addr:
249 self.server.del_transcoders(self, t)
250 self.wfile.write("""\
252 """ % (t, addr[0], addr[1]))
255 self.wfile.write("""\
260 self.wfile.write("""\
265 # serve_stop_selected_transcoders()
268 def serve_stop_transcoder(self, body):
269 req = self.query.get("request", None)
270 if req and "all" in req:
271 self.serve_stop_all_transcoders(body)
273 self.serve_stop_selected_transcoders(body, req)
275 self.serve_status(body)
276 # serve_stop_transcoder()
279 def serve_status(self, body):
280 self.send_response(200)
281 self.send_header("Content-Type", "text/html")
282 self.send_header('Connection', 'close')
285 self.wfile.write("""\
287 <head><title>Catota Server Status</title></head>
289 <h1>Catota Status</h1>
291 tl = self.server.get_transcoders()
293 self.wfile.write("<p>No running transcoder.</p>\n")
295 self.wfile.write("<p>Running transcoders:</p>\n")
296 self.wfile.write("""\
298 <li><a href="/stop-transcoder.do?request=all">[STOP ALL]</a></li>
300 for transcoder, request in tl:
301 self.wfile.write("""\
302 <li>%s: %s:%s <a href="/stop-transcoder.do?request=%s:%s">[STOP]</a></li>
303 """ % (transcoder, request.client_address[0], request.client_address[1],
304 request.client_address[0], request.client_address[1]))
306 self.wfile.write("""\
311 self.wfile.write("""\
319 def _get_transcoder(self):
320 # get transcoder option: mencoder is the default
321 request_transcoders = self.query.get("transcoder", ["mencoder"])
323 for t in request_transcoders:
324 transcoder = self.transcoders.get(t)
329 return self.transcoders[self.def_transcoder]
333 def serve_stream(self, body):
334 transcoder = self._get_transcoder()
336 obj = transcoder(self.query)
338 self.send_error(500, str(e))
341 self.send_response(200)
342 self.send_header("Content-Type", obj.get_mimetype())
343 self.send_header('Connection', 'close')
347 self.server.add_transcoders(self, obj)
348 obj.start(self.wfile)
349 self.server.del_transcoders(self, obj)
353 def log_request(self, code='-', size='-'):
354 self.log.info('"%s" %s %s', self.requestline, str(code), str(size))
358 def log_error(self, format, *args):
359 self.log.error("%s: %s" % (self.address_string(), format % args))
363 def log_message(self, format, *args):
364 self.log.info("%s: %s" % (self.address_string(), format % args))
370 class Server(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
371 log = log.getLogger("gmyth-streamer.server")
374 _lock = threading.RLock()
376 def serve_forever(self):
377 self.log.info("GMyth-Streamer serving HTTP on %s:%s" %
378 self.socket.getsockname())
381 self.handle_request()
382 except KeyboardInterrupt, e:
385 self.log.debug("Stopping all remaining transcoders...")
386 self.stop_transcoders()
387 self.log.debug("Transcoders stopped!")
391 def get_request(self):
393 old = skt.gettimeout()
400 except socket.timeout, e:
402 raise socket.error("Not running")
406 def server_close(self):
408 self.stop_transcoders()
410 BaseHTTPServer.HTTPServer.server_close(self)
414 def stop_transcoders(self):
416 for transcoder, request in self._transcoders.iteritems():
417 self.log.info("Stop transcoder: %s, client=%s" %
418 (transcoder, request.client_address))
424 def get_transcoders(self):
427 return self._transcoders.items()
433 def add_transcoders(self, request, transcoder):
436 self._transcoders[transcoder] = request
442 def del_transcoders(self, request, transcoder):
445 del self._transcoders[transcoder]
453 def serve_forever(host="0.0.0.0", port=40000):
456 RequestHandler.protocol_version = "HTTP/1.0"
457 httpd = Server(addr, RequestHandler)
458 httpd.serve_forever()
462 def load_plugins_transcoders(directory):
463 RequestHandler.load_plugins_transcoders(directory)
464 # load_plugins_transcoders()