renatofilho@484: #!/usr/bin/python renatofilho@484: renatofilho@484: import os renatofilho@484: import lib renatofilho@484: import sys renatofilho@484: import ConfigParser renatofilho@484: renatofilho@484: config = ConfigParser.ConfigParser() renatofilho@484: config.read("stream.conf") renatofilho@484: renatofilho@484: media_plugin = config.get("Media", "engine") renatofilho@484: exec("from plugins.media.%s import *" % media_plugin) renatofilho@484: renatofilho@484: media = Media(config) renatofilho@484: renatofilho@484: comm_plugin = config.get("Comm", "engine") renatofilho@484: exec("from plugins.comm.%s import *" % comm_plugin) renatofilho@484: renatofilho@484: # Start Our Server: renatofilho@484: server = Server(config) renatofilho@484: renatofilho@484: lib.log("Starting GMyth-Stream server") renatofilho@484: morphbr@504: morphbr@504: ''' morphbr@504: PROTOCOL DESCRIPTION morphbr@504: ===================== morphbr@504: morphbr@504: COMMAND OPTIONS morphbr@504: morphbr@504: -> SETUP DESCRIPTION morphbr@504: |-> used to setup transcoding and streaming parameters morphbr@504: |-> must be used before any "PLAY" command morphbr@504: |-> e.g: morphbr@504: morphbr@504: file://file_name mux vcodec vbitrate fps acodec abitrate width height options morphbr@504: dvd://title_number mux vcodec vbitrate fps acodec abitrate width height options morphbr@504: morphbr@504: -> PLAY DESCRIPTION morphbr@504: |-> used to start transcoding and streaming of file morphbr@504: |-> must be used just if SETUP was used before morphbr@504: |-> after it, _must_ send STOP morphbr@504: morphbr@504: -> STOP DESCRIPTION morphbr@504: |-> used to stop transcoding and streaming process morphbr@504: |-> must be used just if PLAY was used before morphbr@504: |-> must be used after PLAY morphbr@504: morphbr@504: -> QUIT DESCRIPTION morphbr@504: |-> used to quit the main loop (quit program) morphbr@504: morphbr@504: ''' morphbr@504: nextport = 0 morphbr@504: morphbr@498: while (server.finish == 0): morphbr@504: conn, client, port = server.getRequest() morphbr@504: server.sendMsg("Welcome to GMyth-Streamer Master") morphbr@504: if nextport == 0: morphbr@504: nextport = port renatofilho@484: renatofilho@484: while True: renatofilho@484: msg = server.getMsg(1024).strip() renatofilho@484: renatofilho@484: if not msg: break renatofilho@484: renatofilho@484: lib.log("Received %s from: %s" % (msg, client) ) renatofilho@484: morphbr@498: if msg == "SETUP": renatofilho@484: setup = server.getMsg(1024).strip().split(" ") renatofilho@484: size = len(setup) renatofilho@484: options = [] renatofilho@484: morphbr@497: if size < 10: renatofilho@484: server.sendMsg(lib.log("Wrong SETUP command from: %s" % client[0])) renatofilho@484: renatofilho@484: else: renatofilho@484: morphbr@497: if size > 10: renatofilho@484: i = 10 renatofilho@484: while (i < size): renatofilho@484: options.append(setup[i]) renatofilho@484: i += 1 renatofilho@484: morphbr@504: nextport += 1 renatofilho@484: ret = media.setup(setup[0], setup[1], setup[2], \ renatofilho@484: setup[3], setup[4], setup[5], renatofilho@484: setup[6], setup[7], setup[8], morphbr@504: nextport, options) renatofilho@484: morphbr@498: if ret == 0: renatofilho@484: server.Ack("SETUP") renatofilho@484: else: renatofilho@484: server.sendMsg(lib.log(ret)) renatofilho@484: renatofilho@484: morphbr@498: elif msg == "PLAY": renatofilho@484: media.play() renatofilho@484: server.Ack("PLAY") morphbr@504: server.sendMsg("STREAM PORT=%d" % nextport) renatofilho@484: morphbr@498: elif msg == "STOP": renatofilho@484: media.stop() renatofilho@484: server.Ack("STOP") renatofilho@484: morphbr@504: elif msg == "QUIT": renatofilho@484: server.finish = 1 renatofilho@484: media.stop() morphbr@504: server.Ack("QUIT") renatofilho@484: break renatofilho@484: renatofilho@484: lib.log("Closing connection with %s" % client[0]) morphbr@504: server.disconnect_client(conn) renatofilho@484: renatofilho@484: server.stop() renatofilho@484: del(server) renatofilho@484: lib.log("Server stopped. Closing...") renatofilho@484: