[svn r458] trunk
authormorphbr
Tue Mar 27 00:13:27 2007 +0100 (2007-03-27)
branchtrunk
changeset 453a806d8ad0ff0
parent 452 8efecea98bd7
child 454 f76bb8743b22
[svn r458]
gmyth-stream/main.py
gmyth-stream/plugins/__init__.py
gmyth-stream/plugins/comm/__init__.py
gmyth-stream/plugins/comm/tcp.py
gmyth-stream/plugins/comm/xmlrpc.py
gmyth-stream/plugins/media/__init__.py
gmyth-stream/plugins/media/gstreamer.py
gmyth-stream/plugins/media/vlc.py
gmyth-stream/stream.conf
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gmyth-stream/main.py	Tue Mar 27 00:13:27 2007 +0100
     1.3 @@ -0,0 +1,85 @@
     1.4 + '''
     1.5 + # GMyth-Stream
     1.6 + #
     1.7 + # @file main.py
     1.8 + #
     1.9 + # @brief <p> Plugin for GMyth-Stream
    1.10 + #
    1.11 + # Copyright (C) 2007 INdT - Instituto Nokia de Tecnologia.
    1.12 + # @author Artur Duque de Souza <artur.souza@indt.org.br>
    1.13 + #
    1.14 + #
    1.15 + # This program is free software; you can redistribute it and/or modify
    1.16 + # it under the terms of the GNU Lesser General Public License as published by
    1.17 + # the Free Software Foundation; either version 2 of the License, or
    1.18 + # (at your option) any later version.
    1.19 + #
    1.20 + # This program is distributed in the hope that it will be useful,
    1.21 + # but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.22 + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.23 + # GNU General Public License for more details.
    1.24 + #
    1.25 + # You should have received a copy of the GNU Lesser General Public License
    1.26 + # along with this program; if not, write to the Free Software
    1.27 + # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.28 + #
    1.29 + '''
    1.30 +
    1.31 +#!/usr/bin/python
    1.32 +
    1.33 +import sys
    1.34 +import os
    1.35 +import ConfigParser
    1.36 +
    1.37 +def now():
    1.38 +    return time.strftime("%Y-%m-%d %H:%M:%S");
    1.39 +
    1.40 +
    1.41 +config = ConfigParser.ConfigParser()
    1.42 +config.read("stream.conf")
    1.43 +
    1.44 +media_plugin = config.get("Media", "engine")
    1.45 +exec("from plugins.media.%s import *" % media_plugin)
    1.46 +
    1.47 +media = Media(config)
    1.48 +
    1.49 +comm_plugin = config.get("Comm", "engine")
    1.50 +exec("from plugins.comm.%s import *" % comm_plugin)
    1.51 +
    1.52 +# Start Our Server:
    1.53 +server = Server(config)
    1.54 +
    1.55 +print "--> Starting the server..."
    1.56 +
    1.57 +while (server.finish == 0):
    1.58 +    con, client = server.getRequest()
    1.59 +
    1.60 +    while True:
    1.61 +        msg = server.getMsg(1024).strip()
    1.62 +
    1.63 +        if not msg: break
    1.64 +
    1.65 +        elif (msg == "SETUP"):
    1.66 +            setup = server.getMsg(1024).strip().split(" ")
    1.67 +            media.setup(setup[0], setup[1], setup[2], \
    1.68 +                        setup[3], setup[4], setup[5])
    1.69 +
    1.70 +        elif (msg == "PLAY"):
    1.71 +            media.play()
    1.72 +
    1.73 +        elif (msg == "STOP"):
    1.74 +            media.stop()
    1.75 +
    1.76 +        elif (msg == "CLOSE"):
    1.77 +            server.finish = 1
    1.78 +            media.stop()
    1.79 +            break
    1.80 +
    1.81 +        print "[%s] %s: %s" % (now(), client, msg)
    1.82 +
    1.83 +    print "[%s] Closing connection with %s" % (now(), client)
    1.84 +    server.disconnect_client(con)
    1.85 +
    1.86 +server.stop()
    1.87 +
    1.88 +print "--> Server stopped..."
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/gmyth-stream/plugins/comm/tcp.py	Tue Mar 27 00:13:27 2007 +0100
     2.3 @@ -0,0 +1,58 @@
     2.4 + '''
     2.5 + # GMyth-Stream
     2.6 + #
     2.7 + # @file main.py
     2.8 + #
     2.9 + # @brief <p> Plugin for GMyth-Stream
    2.10 + #
    2.11 + # Copyright (C) 2007 INdT - Instituto Nokia de Tecnologia.
    2.12 + # @author Artur Duque de Souza <artur.souza@indt.org.br>
    2.13 + #
    2.14 + #
    2.15 + # This program is free software; you can redistribute it and/or modify
    2.16 + # it under the terms of the GNU Lesser General Public License as published by
    2.17 + # the Free Software Foundation; either version 2 of the License, or
    2.18 + # (at your option) any later version.
    2.19 + #
    2.20 + # This program is distributed in the hope that it will be useful,
    2.21 + # but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.22 + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.23 + # GNU General Public License for more details.
    2.24 + #
    2.25 + # You should have received a copy of the GNU Lesser General Public License
    2.26 + # along with this program; if not, write to the Free Software
    2.27 + # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    2.28 + #
    2.29 + '''
    2.30 +
    2.31 +import time
    2.32 +import socket
    2.33 +
    2.34 +class Server:
    2.35 +
    2.36 +    def __init__(self, config):
    2.37 +        self.host = ''
    2.38 +        self.port = int(config.get("Comm", "port"))
    2.39 +        self.finish = 0
    2.40 +
    2.41 +        self.tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    2.42 +        self.tcp.bind( (self.host, self.port) )
    2.43 +        self.tcp.listen(1)
    2.44 +
    2.45 +    def now(self):
    2.46 +        return time.strftime("%Y-%m-%d %H:%M:%S");
    2.47 +
    2.48 +    def getMsg(self, size):
    2.49 +        con = self.data[0]
    2.50 +        return con.recv(size)
    2.51 +
    2.52 +    def getRequest(self):
    2.53 +        self.data = self.tcp.accept()
    2.54 +        print "[%s] Received request from ip=%s" % (self.now(), self.data[1] )
    2.55 +        return self.data
    2.56 +
    2.57 +    def disconnect_client(self, connection):
    2.58 +        connection.close()
    2.59 +
    2.60 +    def stop(self):
    2.61 +        self.tcp.close()
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/gmyth-stream/plugins/comm/xmlrpc.py	Tue Mar 27 00:13:27 2007 +0100
     3.3 @@ -0,0 +1,102 @@
     3.4 + '''
     3.5 + # GMyth-Stream
     3.6 + #
     3.7 + # @file plugins/comm/xmlrpc.py
     3.8 + #
     3.9 + # @brief <p> Plugin for GMyth-Stream
    3.10 + #
    3.11 + # Copyright (C) 2007 INdT - Instituto Nokia de Tecnologia.
    3.12 + # @author Artur Duque de Souza <artur.souza@indt.org.br>
    3.13 + #
    3.14 + #
    3.15 + # This program is free software; you can redistribute it and/or modify
    3.16 + # it under the terms of the GNU Lesser General Public License as published by
    3.17 + # the Free Software Foundation; either version 2 of the License, or
    3.18 + # (at your option) any later version.
    3.19 + #
    3.20 + # This program is distributed in the hope that it will be useful,
    3.21 + # but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.22 + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.23 + # GNU General Public License for more details.
    3.24 + #
    3.25 + # You should have received a copy of the GNU Lesser General Public License
    3.26 + # along with this program; if not, write to the Free Software
    3.27 + # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    3.28 + #
    3.29 + '''
    3.30 +
    3.31 +import SimpleXMLRPCServer
    3.32 +
    3.33 +
    3.34 +class Handler:
    3.35 +
    3.36 +    def __init__(self, pool):
    3.37 +        self.pool = pool
    3.38 +
    3.39 +    def _listMethods(self):
    3.40 +        return ['Setup', 'Play', 'Stop', 'Close']
    3.41 +
    3.42 +    def _methodHelp(self, method):
    3.43 +
    3.44 +        if method == 'Setup':
    3.45 +            return "Setup the Media: Setup(filename,codec,bitrate,widht,height,port)"
    3.46 +        elif method == 'Play':
    3.47 +            return "Play the Media: Play()"
    3.48 +        elif method == 'Stop':
    3.49 +            return "Stop the Media: Stop()"
    3.50 +        elif method == 'Close':
    3.51 +            return "Close the connection: Close()"
    3.52 +        else:
    3.53 +            # By convention, return empty
    3.54 +            # string if no help is available
    3.55 +            return ""
    3.56 +
    3.57 +
    3.58 +    def Setup(self, filename, codec, bitrate, width, height, port):
    3.59 +        self.pool.append("SETUP")
    3.60 +        self.pool.append("%s %s %s %s %s %s" % (filename, codec, bitrate,\
    3.61 +                                             width, height, port))
    3.62 +        return 0
    3.63 +
    3.64 +    def Play(self):
    3.65 +        self.pool.append("PLAY")
    3.66 +        return 0
    3.67 +
    3.68 +    def Stop(self):
    3.69 +        self.pool.append("STOP")
    3.70 +        return 0
    3.71 +
    3.72 +    def Close(self):
    3.73 +        self.pool.append("CLOSE")
    3.74 +        return 0
    3.75 +
    3.76 +
    3.77 +class Server:
    3.78 +
    3.79 +    def __init__(self, config):
    3.80 +        self.host = 'localhost'
    3.81 +        self.port = int(config.get("Comm", "port"))
    3.82 +        self.finish = 0
    3.83 +        self.pool = []
    3.84 +
    3.85 +        self.handler = Handler(self.pool)
    3.86 +
    3.87 +        self.xmlrpc = SimpleXMLRPCServer.SimpleXMLRPCServer((self.host, self.port))
    3.88 +        self.xmlrpc.register_instance(self.handler)
    3.89 +
    3.90 +
    3.91 +    def getMsg(self, size):
    3.92 +        try:
    3.93 +            return self.pool.pop(0)
    3.94 +        except IndexError:
    3.95 +            return ""
    3.96 +
    3.97 +    def getRequest(self):
    3.98 +        self.xmlrpc.handle_request()
    3.99 +        return (0, "RPC Client")
   3.100 +
   3.101 +    def disconnect_client(self, connection):
   3.102 +        connection = 0
   3.103 +
   3.104 +    def stop(self):
   3.105 +        self.xmlrpc.server_close()
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/gmyth-stream/plugins/media/gstreamer.py	Tue Mar 27 00:13:27 2007 +0100
     4.3 @@ -0,0 +1,90 @@
     4.4 + '''
     4.5 + # GMyth-Stream
     4.6 + #
     4.7 + # @file main.py
     4.8 + #
     4.9 + # @brief <p> Plugin for GMyth-Stream
    4.10 + #
    4.11 + # Copyright (C) 2007 INdT - Instituto Nokia de Tecnologia.
    4.12 + # @author Artur Duque de Souza <artur.souza@indt.org.br>
    4.13 + #
    4.14 + #
    4.15 + # This program is free software; you can redistribute it and/or modify
    4.16 + # it under the terms of the GNU Lesser General Public License as published by
    4.17 + # the Free Software Foundation; either version 2 of the License, or
    4.18 + # (at your option) any later version.
    4.19 + #
    4.20 + # This program is distributed in the hope that it will be useful,
    4.21 + # but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.22 + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.23 + # GNU General Public License for more details.
    4.24 + #
    4.25 + # You should have received a copy of the GNU Lesser General Public License
    4.26 + # along with this program; if not, write to the Free Software
    4.27 + # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    4.28 + #
    4.29 + '''
    4.30 +
    4.31 +import pygst
    4.32 +pygst.require("0.10")
    4.33 +import gst
    4.34 +import gobject
    4.35 +
    4.36 +class Media:
    4.37 +
    4.38 +    def __init__(self, config):
    4.39 +
    4.40 +        # set gstreamer basic options
    4.41 +        self.config = config
    4.42 +        self.pipe = ""
    4.43 +
    4.44 +    def setup(self, filename, codec, bitrate, width, height, port):
    4.45 +
    4.46 +        self.filename = filename
    4.47 +        self.codec = codec
    4.48 +        self.bitrate = int(bitrate)
    4.49 +        self.width = int(width)
    4.50 +        self.height = int(height)
    4.51 +        self.port = int(port)
    4.52 +
    4.53 +        ## Pipelines
    4.54 +
    4.55 +        #queue ! videoscale ! video/x-raw-yuv,width=240,height=144\
    4.56 +        #! videorate ! ffenc_h263p bitrate=256000 me-method=2 \
    4.57 +        #! rtph263ppay ! udpsink  host=224.0.0.1 port=5000
    4.58 +
    4.59 +
    4.60 +        #audio/x-raw-int ! queue ! audioconvert ! faac ! rtpmp4gpay\
    4.61 +        #!  udpsink name=upd_audio host=224.0.0.1 port=5002
    4.62 +
    4.63 +        self.pipe = "filesrc location=%s ! decodebin name=d ! queue ! videoscale !"\
    4.64 +                    "video/x-raw-yuv,width=(int)%d,height=(int)%d ! ffenc_h263p bitrate=%d"\
    4.65 +                    " me-method=2 ! rtph263ppay ! udpsink host=224.0.0.1 port=%d d. ! "\
    4.66 +                    "queue  ! audioconvert ! faac ! rtpmp4gpay ! udpsink name=udp_audio "\
    4.67 +                    "host=224.0.0.1 port=%d" % (self.filename, self.width, self.height,\
    4.68 +                                                self.bitrate, self.port, self.port+2)
    4.69 +
    4.70 +        #self.pipe = "filesrc location=/tmp/mpg/cpm.mpg ! decodebin ! ffmpegcolorspace ! ximagesink"
    4.71 +
    4.72 +        self.pipeline = gst.parse_launch(self.pipe)
    4.73 +
    4.74 +
    4.75 +    def play(self):
    4.76 +
    4.77 +        print "Trying to play pipeline: %s" % self.pipe
    4.78 +        try:
    4.79 +            if (self.pipeline):
    4.80 +                self.pipeline.set_state(gst.STATE_PLAYING)
    4.81 +        except gobject.GError, e:
    4.82 +            print "Error: " + str(e)
    4.83 +
    4.84 +
    4.85 +    def stop(self):
    4.86 +
    4.87 +        print "Trying to stop pipeline: %s" % self.pipe
    4.88 +        try:
    4.89 +            if (self.pipeline):
    4.90 +                self.pipeline.set_state(gst.STATE_NULL)
    4.91 +        except gobject.GError, e:
    4.92 +            print "Error: " + str(e)
    4.93 +
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/gmyth-stream/plugins/media/vlc.py	Tue Mar 27 00:13:27 2007 +0100
     5.3 @@ -0,0 +1,99 @@
     5.4 +'''
     5.5 + # GMyth-Stream
     5.6 + #
     5.7 + # @file main.py
     5.8 + #
     5.9 + # @brief <p> Plugin for GMyth-Stream
    5.10 + #
    5.11 + # Copyright (C) 2007 INdT - Instituto Nokia de Tecnologia.
    5.12 + # @author Artur Duque de Souza <artur.souza@indt.org.br>
    5.13 + #
    5.14 + #
    5.15 + # This program is free software; you can redistribute it and/or modify
    5.16 + # it under the terms of the GNU Lesser General Public License as published by
    5.17 + # the Free Software Foundation; either version 2 of the License, or
    5.18 + # (at your option) any later version.
    5.19 + #
    5.20 + # This program is distributed in the hope that it will be useful,
    5.21 + # but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.22 + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    5.23 + # GNU General Public License for more details.
    5.24 + #
    5.25 + # You should have received a copy of the GNU Lesser General Public License
    5.26 + # along with this program; if not, write to the Free Software
    5.27 + # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    5.28 + #
    5.29 + '''
    5.30 +
    5.31 +import os
    5.32 +import sys
    5.33 +import time
    5.34 +import socket
    5.35 +import ConfigParser
    5.36 +
    5.37 +class Media:
    5.38 +
    5.39 +    def __init__(self, config):
    5.40 +
    5.41 +        self.config = config
    5.42 +        self.pipe = ""
    5.43 +        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    5.44 +
    5.45 +        self.path = config.get("Vlc", "path")
    5.46 +        self.host = config.get("Vlc", "host")
    5.47 +        self.port = int(config.get("Vlc", "port"))
    5.48 +        self.pwd = config.get("Vlc", "pwd")
    5.49 +
    5.50 +        # exec VLC
    5.51 +        pid = os.fork()
    5.52 +        if (pid == 0):
    5.53 +            #child
    5.54 +            print "ESTOU EM CHILD"
    5.55 +            self.path += " -I telnet -d 1> /dev/null 2> /dev/null &"
    5.56 +            os.system(self.path)
    5.57 +            sys.exit(0)
    5.58 +
    5.59 +        print "ESTOU EM PARENT 1"
    5.60 +        time.sleep(3)
    5.61 +        print "ESTOU EM PARENT 2"
    5.62 +        self.sock.connect( (self.host, self.port) )
    5.63 +        self.sock.send("%s\n" % self.pwd)
    5.64 +
    5.65 +
    5.66 +    def insert_file(self, filename):
    5.67 +
    5.68 +        self.sock.send("setup output0 input %s\n" % filename)
    5.69 +
    5.70 +
    5.71 +    def setup(self, filename, codec, bitrate, width, height, port):
    5.72 +
    5.73 +        self.filename = filename
    5.74 +        self.codec = codec
    5.75 +        self.bitrate = int(bitrate)
    5.76 +        self.width = int(width)
    5.77 +        self.height = int(height)
    5.78 +        self.port = int(port)
    5.79 +
    5.80 +        self.pipe = "#transcode{vcodec=%s,vb=%d,"\
    5.81 +                    "fps=25.0,scale=1,acodec=mpga,"\
    5.82 +                    "ab=64,channels=1,width=%d,height=%d}"\
    5.83 +                    ":duplicate{dst=std{access=http,"\
    5.84 +                    "mux=mpeg1,dst=:%d}}" % (self.codec, self.bitrate,\
    5.85 +                                             self.widht, self.height,\
    5.86 +                                             self.port)
    5.87 +
    5.88 +        self.sock.send("setup output0 broadcast %s\n" % self.pipe)
    5.89 +        self.insert_file(self.filename)
    5.90 +
    5.91 +    def play(self):
    5.92 +
    5.93 +        print "Trying to play: %s" % self.pipe
    5.94 +        self.sock.send("control output0 play\n")
    5.95 +
    5.96 +
    5.97 +    def stop(self):
    5.98 +
    5.99 +        print "Trying to stop: %s" % self.pipe
   5.100 +        self.sock.send("control output0 stop\n")
   5.101 +
   5.102 +
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/gmyth-stream/stream.conf	Tue Mar 27 00:13:27 2007 +0100
     6.3 @@ -0,0 +1,12 @@
     6.4 +[Media]
     6.5 +engine = gstreamer
     6.6 +
     6.7 +[Comm]
     6.8 +engine = tcp
     6.9 +port = 12344
    6.10 +
    6.11 +[Vlc]
    6.12 +path = /usr/local/bin/vlc
    6.13 +host = 127.0.0.1
    6.14 +port = 4212
    6.15 +pwd = admin