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