gmyth-stream/server/main.py
author renatofilho
Thu Apr 05 15:26:33 2007 +0100 (2007-04-05)
branchtrunk
changeset 506 4fcd764b21a6
parent 498 7a0c3906dce2
child 511 16312d0021cb
permissions -rwxr-xr-x
[svn r511] bug fix
     1 #!/usr/bin/python
     2 
     3 import os
     4 import lib
     5 import sys
     6 import ConfigParser
     7 
     8 config = ConfigParser.ConfigParser()
     9 config.read("stream.conf")
    10 
    11 media_plugin = config.get("Media", "engine")
    12 exec("from plugins.media.%s import *" % media_plugin)
    13 
    14 media = Media(config)
    15 
    16 comm_plugin = config.get("Comm", "engine")
    17 exec("from plugins.comm.%s import *" % comm_plugin)
    18 
    19 # Start Our Server:
    20 server = Server(config)
    21 
    22 lib.log("Starting GMyth-Stream server")
    23 
    24 
    25 '''
    26 PROTOCOL DESCRIPTION
    27 =====================
    28 
    29 COMMAND OPTIONS
    30 
    31 -> SETUP DESCRIPTION
    32 |-> used to setup transcoding and streaming parameters
    33 |-> must be used before any "PLAY" command
    34 |-> e.g:
    35 
    36 file://file_name mux vcodec vbitrate fps acodec abitrate width height options
    37 dvd://title_number mux vcodec vbitrate fps acodec abitrate width height options
    38 
    39 -> PLAY DESCRIPTION
    40  |-> used to start transcoding and streaming of file
    41  |-> must be used just if SETUP was used before
    42  |-> after it, _must_ send STOP
    43 
    44 -> STOP DESCRIPTION
    45  |-> used to stop transcoding and streaming process
    46  |-> must be used just if PLAY was used before
    47  |-> must be used after PLAY
    48 
    49 -> QUIT DESCRIPTION
    50  |-> used to quit the main loop (quit program)
    51 
    52 '''
    53 nextport = 0
    54 
    55 while (server.finish == 0):
    56     conn, client, port = server.getRequest()
    57     server.sendMsg("Welcome to GMyth-Streamer Master")
    58     if nextport == 0:
    59         nextport = port
    60 
    61     while True:
    62         msg = server.getMsg(1024).strip()
    63 
    64         if not msg: break
    65 
    66         lib.log("Received %s from: %s" % (msg, client) )
    67 
    68         if msg == "SETUP":
    69             setup = server.getMsg(1024).strip().split(" ")
    70             size = len(setup)
    71             options = []
    72 
    73             if size < 10:
    74                 server.sendMsg(lib.log("Wrong SETUP command from: %s" % client[0]))
    75 
    76             else:
    77 
    78                 if size > 10:
    79                     i = 10
    80                     while (i < size):
    81                         options.append(setup[i])
    82                         i += 1
    83 
    84                 nextport += 1
    85                 ret = media.setup(setup[0], setup[1], setup[2], \
    86                                   setup[3], setup[4], setup[5],
    87                                   setup[6], setup[7], setup[8],
    88                                   nextport, options)
    89 
    90                 if ret == 0:
    91                     server.Ack("SETUP")
    92                 else:
    93                     server.sendMsg(lib.log(ret))
    94 
    95 
    96         elif msg == "PLAY":
    97             media.play()
    98             server.Ack("PLAY")
    99             server.sendMsg("STREAM PORT=%d" % nextport)
   100 
   101         elif msg == "STOP":
   102             media.stop()
   103             server.Ack("STOP")
   104 
   105         elif msg == "QUIT":
   106             server.finish = 1
   107             media.stop()
   108             server.Ack("QUIT")
   109             break
   110 
   111     lib.log("Closing connection with %s" % client[0])
   112     server.disconnect_client(conn)
   113 
   114 server.stop()
   115 del(server)
   116 lib.log("Server stopped. Closing...")
   117