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