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