1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/gmyth-stream/server/0.1/main.py Tue May 15 21:39:04 2007 +0100
1.3 @@ -0,0 +1,185 @@
1.4 +#!/usr/bin/python
1.5 +
1.6 +import os
1.7 +import lib
1.8 +import sys
1.9 +import imp
1.10 +import ConfigParser
1.11 +import logging as log
1.12 +
1.13 +log.basicConfig(level=log.DEBUG,
1.14 + format="%(asctime)s %(levelname)-8s %(message)s",
1.15 + datefmt='%Y-%m-%d %H:%M:%S')
1.16 +
1.17 +config = ConfigParser.ConfigParser()
1.18 +config.read("stream.conf")
1.19 +
1.20 +def load_module(pathlist, name):
1.21 + fp, path, desc = imp.find_module(name, pathlist)
1.22 + try:
1.23 + module = imp.load_module(name, fp, path, desc)
1.24 + return module
1.25 + finally:
1.26 + if fp:
1.27 + fp.close()
1.28 +
1.29 +
1.30 +media_plugin = config.get("Media", "engine")
1.31 +media_plugin_module = load_module(["./plugins/media"], media_plugin)
1.32 +media = media_plugin_module.Media(config)
1.33 +
1.34 +comm_plugin = config.get("Comm", "engine")
1.35 +comm_plugin_module = load_module(["./plugins/comm"], comm_plugin)
1.36 +server = comm_plugin_module.Server(config)
1.37 +
1.38 +log.info("Starting GMyth-Stream server")
1.39 +
1.40 +
1.41 +'''
1.42 +PROTOCOL DESCRIPTION
1.43 +=====================
1.44 +
1.45 +COMMAND OPTIONS
1.46 +
1.47 +-> SETUP DESCRIPTION
1.48 +|-> used to setup transcoding and streaming parameters
1.49 +|-> must be used before any "PLAY" command
1.50 +|-> e.g:
1.51 +
1.52 +file://file_name mux vcodec vbitrate fps acodec abitrate width height options
1.53 +dvd://title_number mux vcodec vbitrate fps acodec abitrate width height options
1.54 +
1.55 +-> PLAY DESCRIPTION
1.56 + |-> used to start transcoding and streaming of file
1.57 + |-> must be used just if SETUP was used before
1.58 + |-> after it, _must_ send STOP
1.59 +
1.60 +-> STOP DESCRIPTION
1.61 + |-> used to stop transcoding and streaming process
1.62 + |-> must be used just if PLAY was used before
1.63 + |-> must be used after PLAY
1.64 +
1.65 +-> QUIT DESCRIPTION
1.66 + |-> used to quit the main loop (quit program)
1.67 +
1.68 +'''
1.69 +nextport = 0
1.70 +setup = (False, "STOPPED")
1.71 +
1.72 +def do_setup(server, filename, mux, vcodec, vbitrate, fps, acodec, abitrate,
1.73 + width, height, *options):
1.74 + global nextport
1.75 + global setup
1.76 +
1.77 + if setup[1] != "PLAYING":
1.78 + nextport += 1
1.79 + ret = media.setup(filename, mux, vcodec, vbitrate, fps, acodec,
1.80 + abitrate, width, height, nextport, options)
1.81 + if ret[0]:
1.82 + server.sendOk()
1.83 + else:
1.84 + server.sendNotOk(ret[1])
1.85 +
1.86 + setup = (True, setup[1])
1.87 +
1.88 + else: server.sendNotOk("You must STOP before SETingUP again")
1.89 +
1.90 + return True
1.91 +
1.92 +def do_play(server):
1.93 + global setup
1.94 +
1.95 + if setup[0] and setup[1] == "STOPPED":
1.96 + setup = (setup[0], "PLAYING")
1.97 + ret = media.play()
1.98 + if ret[0]:
1.99 + server.sendOk("%d" % nextport)
1.100 + else:
1.101 + server.sendNotOk(ret[1])
1.102 +
1.103 + else:
1.104 + if setup[1] == "STOPPED":
1.105 + server.sendNotOk("You must SETUP before PLAYing")
1.106 + else:
1.107 + server.sendNotOk("You must STOP before PLAYing again")
1.108 +
1.109 + return True
1.110 +
1.111 +def do_stop(server):
1.112 + global setup
1.113 +
1.114 + media.stop()
1.115 + setup = (False, "STOPPED")
1.116 + server.sendOk()
1.117 + return True
1.118 +
1.119 +def do_list(server, *directory):
1.120 + file_list = []
1.121 + for j in directory:
1.122 + lib.list_media_files(j, file_list)
1.123 +
1.124 + server.sendOk(file_list)
1.125 + return True
1.126 +
1.127 +def do_quit(server):
1.128 + server.finish = 1
1.129 + media.stop()
1.130 + server.sendOk()
1.131 + return True
1.132 +
1.133 +
1.134 +mapping = {
1.135 + "SETUP": do_setup,
1.136 + "PLAY": do_play,
1.137 + "STOP": do_stop,
1.138 + "LIST": do_list,
1.139 + "QUIT": do_quit,
1.140 + }
1.141 +
1.142 +
1.143 +def dispatch(server, msg):
1.144 + pieces = msg.split()
1.145 + if len(pieces) < 1:
1.146 + log.error("Invalid client command format: %r" % msg)
1.147 + server.sendNotOk("Invalid Format")
1.148 + return False
1.149 +
1.150 + cmd = pieces[0]
1.151 + f = mapping.get(cmd, None)
1.152 + if not f:
1.153 + log.error("Unknow command: %r" % msg)
1.154 + server.sendNotOk("Unknow Command")
1.155 + return False
1.156 +
1.157 + try:
1.158 + return f(server, *pieces[1:])
1.159 + except Exception, e:
1.160 + log.error("Could not execute %r: %s" % (msg, e))
1.161 + server.sendNotOk(str(e))
1.162 + return False
1.163 +
1.164 +
1.165 +
1.166 +while not server.finish:
1.167 + conn, client, port = server.getRequest()
1.168 + if nextport == 0:
1.169 + nextport = port
1.170 +
1.171 + while not server.finish:
1.172 + msg = server.getMsg()
1.173 + if not msg:
1.174 + break
1.175 +
1.176 + log.info("Client %s sent command: %r" % (client, msg))
1.177 + dispatch(server, msg)
1.178 +
1.179 + log.info("Closing connection with %s" % (client,))
1.180 + server.disconnect_client(conn)
1.181 + try:
1.182 + os.wait()
1.183 + except Exception, e:
1.184 + log.error(e)
1.185 +
1.186 +server.stop()
1.187 +log.info("Server stopped. Closing...")
1.188 +