[svn r836] - Cleanup of svn (deleted old versions of gms as they are on repository)
1.1 --- a/gmyth-stream/server/0.1/lib.py Tue Aug 28 15:41:35 2007 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,36 +0,0 @@
1.4 -import time
1.5 -import logging
1.6 -import os
1.7 -import stat
1.8 -
1.9 -ext = ['mpg', 'avi', 'mp4', 'nuv', 'mpeg', 'mov']
1.10 -
1.11 -def now():
1.12 - return time.strftime("%Y-%m-%d %H:%M:%S");
1.13 -
1.14 -def log(msg):
1.15 - logging.log(logging.DEBUG, msg)
1.16 - new_msg = "[%s] %s" % (now(), msg)
1.17 - return new_msg
1.18 -
1.19 -
1.20 -bin_path_list = os.environ["PATH"].split(os.pathsep)
1.21 -def which(prg):
1.22 - for d in bin_path_list:
1.23 - path = os.path.join(d, prg)
1.24 - if os.path.exists(path):
1.25 - st = os.stat(path)
1.26 - if st[stat.ST_MODE] & 0111:
1.27 - return path
1.28 - return ""
1.29 -
1.30 -def list_media_files(directory, file_list):
1.31 - for root, dirs, files in os.walk(directory):
1.32 - for name in files:
1.33 - if os.path.splitext(name)[1].strip(".") in ext:
1.34 - media = os.path.join(root,name)
1.35 - if media not in file_list:
1.36 - file_list.append(os.path.join(root,name))
1.37 -
1.38 - return True
1.39 -
2.1 --- a/gmyth-stream/server/0.1/main.py Tue Aug 28 15:41:35 2007 +0100
2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
2.3 @@ -1,185 +0,0 @@
2.4 -#!/usr/bin/python
2.5 -
2.6 -import os
2.7 -import lib
2.8 -import sys
2.9 -import imp
2.10 -import ConfigParser
2.11 -import logging as log
2.12 -
2.13 -log.basicConfig(level=log.DEBUG,
2.14 - format="%(asctime)s %(levelname)-8s %(message)s",
2.15 - datefmt='%Y-%m-%d %H:%M:%S')
2.16 -
2.17 -config = ConfigParser.ConfigParser()
2.18 -config.read("stream.conf")
2.19 -
2.20 -def load_module(pathlist, name):
2.21 - fp, path, desc = imp.find_module(name, pathlist)
2.22 - try:
2.23 - module = imp.load_module(name, fp, path, desc)
2.24 - return module
2.25 - finally:
2.26 - if fp:
2.27 - fp.close()
2.28 -
2.29 -
2.30 -media_plugin = config.get("Media", "engine")
2.31 -media_plugin_module = load_module(["./plugins/media"], media_plugin)
2.32 -media = media_plugin_module.Media(config)
2.33 -
2.34 -comm_plugin = config.get("Comm", "engine")
2.35 -comm_plugin_module = load_module(["./plugins/comm"], comm_plugin)
2.36 -server = comm_plugin_module.Server(config)
2.37 -
2.38 -log.info("Starting GMyth-Stream server")
2.39 -
2.40 -
2.41 -'''
2.42 -PROTOCOL DESCRIPTION
2.43 -=====================
2.44 -
2.45 -COMMAND OPTIONS
2.46 -
2.47 --> SETUP DESCRIPTION
2.48 -|-> used to setup transcoding and streaming parameters
2.49 -|-> must be used before any "PLAY" command
2.50 -|-> e.g:
2.51 -
2.52 -file://file_name mux vcodec vbitrate fps acodec abitrate width height options
2.53 -dvd://title_number mux vcodec vbitrate fps acodec abitrate width height options
2.54 -
2.55 --> PLAY DESCRIPTION
2.56 - |-> used to start transcoding and streaming of file
2.57 - |-> must be used just if SETUP was used before
2.58 - |-> after it, _must_ send STOP
2.59 -
2.60 --> STOP DESCRIPTION
2.61 - |-> used to stop transcoding and streaming process
2.62 - |-> must be used just if PLAY was used before
2.63 - |-> must be used after PLAY
2.64 -
2.65 --> QUIT DESCRIPTION
2.66 - |-> used to quit the main loop (quit program)
2.67 -
2.68 -'''
2.69 -nextport = 0
2.70 -setup = (False, "STOPPED")
2.71 -
2.72 -def do_setup(server, filename, mux, vcodec, vbitrate, fps, acodec, abitrate,
2.73 - width, height, *options):
2.74 - global nextport
2.75 - global setup
2.76 -
2.77 - if setup[1] != "PLAYING":
2.78 - nextport += 1
2.79 - ret = media.setup(filename, mux, vcodec, vbitrate, fps, acodec,
2.80 - abitrate, width, height, nextport, options)
2.81 - if ret[0]:
2.82 - server.sendOk()
2.83 - else:
2.84 - server.sendNotOk(ret[1])
2.85 -
2.86 - setup = (True, setup[1])
2.87 -
2.88 - else: server.sendNotOk("You must STOP before SETingUP again")
2.89 -
2.90 - return True
2.91 -
2.92 -def do_play(server):
2.93 - global setup
2.94 -
2.95 - if setup[0] and setup[1] == "STOPPED":
2.96 - setup = (setup[0], "PLAYING")
2.97 - ret = media.play()
2.98 - if ret[0]:
2.99 - server.sendOk("%d" % nextport)
2.100 - else:
2.101 - server.sendNotOk(ret[1])
2.102 -
2.103 - else:
2.104 - if setup[1] == "STOPPED":
2.105 - server.sendNotOk("You must SETUP before PLAYing")
2.106 - else:
2.107 - server.sendNotOk("You must STOP before PLAYing again")
2.108 -
2.109 - return True
2.110 -
2.111 -def do_stop(server):
2.112 - global setup
2.113 -
2.114 - media.stop()
2.115 - setup = (False, "STOPPED")
2.116 - server.sendOk()
2.117 - return True
2.118 -
2.119 -def do_list(server, *directory):
2.120 - file_list = []
2.121 - for j in directory:
2.122 - lib.list_media_files(j, file_list)
2.123 -
2.124 - server.sendOk(file_list)
2.125 - return True
2.126 -
2.127 -def do_quit(server):
2.128 - server.finish = 1
2.129 - media.stop()
2.130 - server.sendOk()
2.131 - return True
2.132 -
2.133 -
2.134 -mapping = {
2.135 - "SETUP": do_setup,
2.136 - "PLAY": do_play,
2.137 - "STOP": do_stop,
2.138 - "LIST": do_list,
2.139 - "QUIT": do_quit,
2.140 - }
2.141 -
2.142 -
2.143 -def dispatch(server, msg):
2.144 - pieces = msg.split()
2.145 - if len(pieces) < 1:
2.146 - log.error("Invalid client command format: %r" % msg)
2.147 - server.sendNotOk("Invalid Format")
2.148 - return False
2.149 -
2.150 - cmd = pieces[0]
2.151 - f = mapping.get(cmd, None)
2.152 - if not f:
2.153 - log.error("Unknow command: %r" % msg)
2.154 - server.sendNotOk("Unknow Command")
2.155 - return False
2.156 -
2.157 - try:
2.158 - return f(server, *pieces[1:])
2.159 - except Exception, e:
2.160 - log.error("Could not execute %r: %s" % (msg, e))
2.161 - server.sendNotOk(str(e))
2.162 - return False
2.163 -
2.164 -
2.165 -
2.166 -while not server.finish:
2.167 - conn, client, port = server.getRequest()
2.168 - if nextport == 0:
2.169 - nextport = port
2.170 -
2.171 - while not server.finish:
2.172 - msg = server.getMsg()
2.173 - if not msg:
2.174 - break
2.175 -
2.176 - log.info("Client %s sent command: %r" % (client, msg))
2.177 - dispatch(server, msg)
2.178 -
2.179 - log.info("Closing connection with %s" % (client,))
2.180 - server.disconnect_client(conn)
2.181 - try:
2.182 - os.wait()
2.183 - except Exception, e:
2.184 - log.error(e)
2.185 -
2.186 -server.stop()
2.187 -log.info("Server stopped. Closing...")
2.188 -
3.1 --- a/gmyth-stream/server/0.1/plugins/comm/tcp.py Tue Aug 28 15:41:35 2007 +0100
3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
3.3 @@ -1,79 +0,0 @@
3.4 -import lib
3.5 -import time
3.6 -import socket
3.7 -import logging as log
3.8 -
3.9 -class Server(object):
3.10 -
3.11 - def __init__(self, config):
3.12 - self.host = '0.0.0.0'
3.13 - self.port = int(config.get("Comm", "port"))
3.14 - self.finish = 0
3.15 -
3.16 - addr = (self.host, self.port)
3.17 - log.debug("Setup TCP server at %s:%s" % addr)
3.18 - self.tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
3.19 - self.tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
3.20 - self.tcp.bind(addr)
3.21 - self.tcp.listen(1)
3.22 - log.info("TCP server listening at %s:%s (sock=%d)" %
3.23 - (self.host, self.port, self.tcp.fileno()))
3.24 -
3.25 - def getMsg(self):
3.26 - bytes = []
3.27 - try:
3.28 - while 1:
3.29 - c = self.con.recv(1)
3.30 - bytes.append(c)
3.31 - if not c or c == "\n":
3.32 - break
3.33 - except Exception, e:
3.34 - log.error("Error reading message from client: %s" % e)
3.35 - return None
3.36 -
3.37 - if not bytes or bytes[-1] != "\n":
3.38 - msg = "".join(bytes)
3.39 - log.error("Invalid message from client: %r" % msg)
3.40 - return None
3.41 -
3.42 - # remove \n and \r
3.43 - bytes.pop()
3.44 - if bytes[-1] == "\r":
3.45 - bytes.pop()
3.46 -
3.47 - msg = "".join(bytes)
3.48 - log.debug("RECV: %r" % msg)
3.49 - return msg
3.50 -
3.51 - def sendMsg(self, msg):
3.52 - log.debug("SEND: %r" % msg)
3.53 - self.con.send(msg + "\n")
3.54 -
3.55 - def sendOk(self, payload=None):
3.56 - self.sendMsg("OK %d" % bool(payload is not None))
3.57 - if payload is not None:
3.58 - if not isinstance(payload, (tuple, list)):
3.59 - payload = (payload,)
3.60 - for e in payload:
3.61 - self.sendMsg("+%s" % e)
3.62 - self.sendMsg(".")
3.63 -
3.64 - def sendNotOk(self, reason=""):
3.65 - self.sendMsg("NOTOK %r" % reason)
3.66 -
3.67 - def getRequest(self):
3.68 - log.debug("Wait for client request at %s:%s (sock=%d)" %
3.69 - (self.host, self.port, self.tcp.fileno()))
3.70 - self.con, self.client = self.tcp.accept()
3.71 - log.info("Incoming request from %s (con=%s)" %
3.72 - (self.client, self.con.fileno()))
3.73 - return (self.con, self.client, self.port)
3.74 -
3.75 - def disconnect_client(self, connection):
3.76 - log.info("Closed request from %s (con=%s)" %
3.77 - (self.client, self.con.fileno()))
3.78 - connection.close()
3.79 -
3.80 - def stop(self):
3.81 - log.debug("Stop")
3.82 - self.tcp.close()
4.1 --- a/gmyth-stream/server/0.1/plugins/comm/xmlrpc.py Tue Aug 28 15:41:35 2007 +0100
4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
4.3 @@ -1,102 +0,0 @@
4.4 -import lib
4.5 -import SimpleXMLRPCServer
4.6 -
4.7 -
4.8 -class Handler:
4.9 -
4.10 - def __init__(self, recv_pool, send_pool):
4.11 - self.recv_pool = recv_pool
4.12 - self.send_pool = send_pool
4.13 - self.getMsg = self.sendMsg
4.14 -
4.15 - def _listMethods(self):
4.16 - return ['setup', 'play', 'stop', 'close', 'getMsg']
4.17 -
4.18 - def _methodHelp(self, method):
4.19 -
4.20 - if method == 'setup':
4.21 - return "Setup the Media: setup( filename, mux, vcodec, vbitrate,"\
4.22 - " fps, acodec, abitrate, width, height, port, options"
4.23 - elif method == 'play':
4.24 - return "Play the Media: play()"
4.25 - elif method == 'stop':
4.26 - return "Stop the Media: stop()"
4.27 - elif method == 'close':
4.28 - return "Close the connection: close()"
4.29 - elif method == 'getMsg':
4.30 - return "Return the first message in the pool: getMsg()"
4.31 - else:
4.32 - # By convention, return empty
4.33 - # string if no help is available
4.34 - return ""
4.35 -
4.36 - def setup(self, filename, mux, vcodec, vbitrate,\
4.37 - fps, acodec, abitrate, width, height, port, options):
4.38 -
4.39 - msg = "%s %s %s %s %s %s %s" % (filename, mux, vcodec, vbitrate,\
4.40 - fps, acodec, abitrate, width, height, port)
4.41 -
4.42 - if len(options) > 0:
4.43 - for opt in options:
4.44 - msg += " %s" % opt
4.45 -
4.46 - self.recv_pool.append("SETUP")
4.47 - self.recv_pool.append(msg)
4.48 - return self.sendMsg()
4.49 -
4.50 - def play(self):
4.51 - self.recv_pool.append("PLAY")
4.52 - return self.sendMsg()
4.53 -
4.54 - def stop(self):
4.55 - self.recv_pool.append("STOP")
4.56 - return self.sendMsg()
4.57 -
4.58 - def close(self):
4.59 - self.recv_pool.append("CLOSE")
4.60 - return self.sendMsg()
4.61 -
4.62 - def sendMsg(self):
4.63 - if self.send_pool != []:
4.64 - return self.send_pool.pop(0)
4.65 - else:
4.66 - return ""
4.67 -
4.68 -
4.69 -class Server:
4.70 -
4.71 - def __init__(self, config):
4.72 - self.host = 'localhost'
4.73 - self.port = int(config.get("Comm", "port"))
4.74 - self.finish = 0
4.75 - self.recv_pool = []
4.76 - self.send_pool = []
4.77 -
4.78 - self.handler = Handler(self.recv_pool, self.send_pool)
4.79 -
4.80 - self.xmlrpc = SimpleXMLRPCServer.SimpleXMLRPCServer((self.host, self.port))
4.81 - self.xmlrpc.register_instance(self.handler)
4.82 -
4.83 -
4.84 - def getMsg(self, size):
4.85 - if self.recv_pool != []:
4.86 - return self.recv_pool.pop(0)
4.87 - else:
4.88 - return ""
4.89 -
4.90 - def sendMsg(self, msg):
4.91 - self.send_pool.append(msg)
4.92 -
4.93 - def Ack(self, command):
4.94 - msg = "[%s] Command %s received" % (lib.now(), command)
4.95 - self.sendMsg(msg + "\n")
4.96 -
4.97 - def getRequest(self):
4.98 - self.xmlrpc.handle_request()
4.99 - return (0, "RPC Client")
4.100 -
4.101 - def disconnect_client(self, connection):
4.102 - connection = 0
4.103 -
4.104 - def stop(self):
4.105 - self.xmlrpc.server_close()
5.1 --- a/gmyth-stream/server/0.1/plugins/media/ffmpeg.py Tue Aug 28 15:41:35 2007 +0100
5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
5.3 @@ -1,91 +0,0 @@
5.4 -import os
5.5 -import sys
5.6 -import lib
5.7 -import time
5.8 -import socket
5.9 -import ConfigParser
5.10 -
5.11 -class Media:
5.12 -
5.13 - def __init__(self, config):
5.14 -
5.15 - self.config = config
5.16 - self.socket = None
5.17 - self.child_pid = None
5.18 -
5.19 - def setup(self, filename, mux, vcodec, vbitrate,\
5.20 - fps, acodec, abitrate, width, height, port):
5.21 -
5.22 - self.filename = filename
5.23 - self.mux = mux
5.24 - self.vcodec = vcodec
5.25 - self.vbitrate = int(vbitrate)
5.26 - self.fps = int(fps)
5.27 - self.acodec = acodec
5.28 - self.abitrate = int(abitrate)
5.29 - self.width = int(width)
5.30 - self.height = int(height)
5.31 -
5.32 - self.port = int(port)
5.33 -
5.34 - # good one: /tmp/mpg/cpm.mpg mpeg mpeg1video 400 25 mp2 192 320 240 5000
5.35 - self.path = self.config.get("FFmpeg", "path")
5.36 - self.path += " -i %s -f %s -vcodec %s -b %d -r %d -acodec %s -ab %d -s %dx%d -" % (
5.37 - self.filename, self.mux, self.vcodec, self.vbitrate,\
5.38 - self.fps, self.acodec, self.abitrate, self.width, self.height)
5.39 -
5.40 - if (self.socket != None):
5.41 - del(self.socket)
5.42 -
5.43 - self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
5.44 - self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
5.45 - self.socket.bind( ('', self.port) )
5.46 - self.socket.settimeout(10)
5.47 - self.socket.listen(1)
5.48 -
5.49 - def play(self):
5.50 -
5.51 - lib.log("Starting FFmpeg: %s" % self.path)
5.52 -
5.53 - # exec FFmpeg and get stdout
5.54 - child_stdin, child_stdout = os.popen2(self.path)
5.55 - child_stdin.close()
5.56 -
5.57 - self.child_pid = os.fork()
5.58 - if (self.child_pid == 0):
5.59 - #child
5.60 -
5.61 - conn,addr= self.socket.accept()
5.62 - lib.log("Sending Data to client: %s" % addr[0])
5.63 - data = child_stdout.read(1024)
5.64 - conn.settimeout(5)
5.65 - retry = 0
5.66 -
5.67 - while( data != "" and retry < 5):
5.68 - try:
5.69 - conn.send(data)
5.70 - except socket.error:
5.71 - lib.log("Socket error (maybe timeout ?)")
5.72 - retry = retry + 1
5.73 -
5.74 - data = child_stdout.read(1024)
5.75 -
5.76 - if (retry < 5):
5.77 - lib.log("Finished sending Data to client: %s" % addr[0])
5.78 - else:
5.79 - lib.log("Client timed out")
5.80 -
5.81 - child_stdout.close()
5.82 - #conn.close()
5.83 - #sys.exit()
5.84 -
5.85 -
5.86 - def stop(self):
5.87 -
5.88 - if (self.socket != None):
5.89 - lib.log("Closing socket")
5.90 - self.socket.close()
5.91 -
5.92 - lib.log("Trying to stop FFmpeg process")
5.93 - if (self.child_pid != None):
5.94 - os.kill(self.child_pid, 9)
6.1 --- a/gmyth-stream/server/0.1/plugins/media/gstreamer-rtp.py Tue Aug 28 15:41:35 2007 +0100
6.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
6.3 @@ -1,218 +0,0 @@
6.4 -import pygst
6.5 -pygst.require("0.10")
6.6 -import gst
6.7 -import gobject
6.8 -
6.9 -class Media:
6.10 - class StreamData:
6.11 - stream_count = 0
6.12 -
6.13 - def __init__ (self, pipe, abin, vbin):
6.14 -
6.15 - self.stream_count += 1
6.16 - self.Id = self.stream_count
6.17 - self.Pipe = pipe
6.18 - self.Abin = abin
6.19 - self.Vbin = vbin
6.20 - self.Loop = gobject.MainLoop()
6.21 - self.ACaps = ""
6.22 - self.VCaps = ""
6.23 - self.Ready = False
6.24 -
6.25 -
6.26 - def __init__(self, config):
6.27 - # set gstreamer basic options
6.28 - self.config = config
6.29 - self.pipe = None
6.30 - self.streams = []
6.31 -
6.32 -
6.33 - def setup(self, filename, mux, vcodec, vbitrate,
6.34 - fps, acodec, abitrate, width, height, port, options):
6.35 -
6.36 - ## Pipelines
6.37 - self.pipe = gst.Pipeline ()
6.38 - uri = "file://" + filename
6.39 - print "Opening Uri:" + uri
6.40 - src = gst.element_make_from_uri (gst.URI_SRC, uri, "src")
6.41 - if (src is None):
6.42 - return None
6.43 -
6.44 - decode = gst.element_factory_make ("decodebin", "decode")
6.45 - if (decode is None):
6.46 - return None
6.47 -
6.48 -
6.49 - #video encode
6.50 - #queue ! videoscale ! video/x-raw-yuv,width=240,height=144 ! videorate ! ffenc_h263p bitrate=256000 me-method=2 ! rtph263ppay ! udpsink host=224.0.0.1 port=5000
6.51 - vbin = gst.Bin ()
6.52 - vqueue = gst.element_factory_make ("queue", "vqueue")
6.53 - vscale = gst.element_factory_make ("videoscale", "vscale")
6.54 - vrate = gst.element_factory_make ("videorate", "vrate")
6.55 - vencode = gst.element_factory_make ("ffenc_mpeg4", "vencode")
6.56 - vpay = gst.element_factory_make ("rtpmp4vpay", "vpay")
6.57 - vsink = gst.element_factory_make ("udpsink", "vsink")
6.58 -
6.59 - if (None in [vbin, vqueue, vscale, vrate, vencode, vpay, vsink]):
6.60 - print "Fail to create video encode elements."
6.61 - return None
6.62 -
6.63 - vscale_pad = vscale.get_pad("sink")
6.64 - if (vscale_pad is None):
6.65 - print "Fail to get vscale sink pad."
6.66 - return None
6.67 -
6.68 - vscale_caps = gst.caps_from_string ("video/x-raw-yuv, width=%s, height=%s" % (width, height))
6.69 - if (vscale_caps is None):
6.70 - print "Fail to create video caps"
6.71 - return None
6.72 -
6.73 - if (not vscale_pad.set_caps (vscale_caps)):
6.74 - print "Fail to set video output caps"
6.75 - return None
6.76 -
6.77 - vencode.set_property ("bitrate", 256000)
6.78 - vencode.set_property ("me-method", 2)
6.79 -
6.80 - vsink.set_property ("host", "224.0.0.1")
6.81 - vsink.set_property ("port", 5000)
6.82 -
6.83 - vbin.add (vqueue, vscale, vrate, vencode, vpay, vsink)
6.84 - if (not gst.element_link_many (vqueue, vscale, vrate, vencode, vpay, vsink)):
6.85 - print "Fail to link video elements"
6.86 - return None
6.87 -
6.88 - vbin.add_pad (gst.GhostPad ("sink", vqueue.get_pad ("sink")))
6.89 -
6.90 - #audio encode
6.91 - #audio/x-raw-int ! queue ! audioconvert ! faac ! rtpmp4gpay ! udpsink name=upd_audio host=224.0.0.1 port=5002
6.92 - abin = gst.Bin ()
6.93 - aqueue = gst.element_factory_make ("queue", "vqueue")
6.94 - aconvert = gst.element_factory_make ("audioconvert", "aconvert")
6.95 - aencode = gst.element_factory_make ("faac", "aencode")
6.96 - apay = gst.element_factory_make ("rtpmp4gpay", "apay")
6.97 - asink = gst.element_factory_make ("udpsink", "asink")
6.98 -
6.99 - if (None in [abin, aqueue, aconvert, aencode, apay, asink]):
6.100 - print "Fail to create video encode elements."
6.101 - return None
6.102 -
6.103 - asink.set_property ("host", "224.0.0.1")
6.104 - asink.set_property ("port", 5002)
6.105 -
6.106 - abin.add (aqueue, aconvert, aencode, apay, asink)
6.107 - if (not gst.element_link_many (aqueue, aconvert, aencode, apay, asink)):
6.108 - print "Fail to link video elements"
6.109 - return None
6.110 -
6.111 - abin.add_pad (gst.GhostPad ("sink", aqueue.get_pad ("sink")))
6.112 -
6.113 - self.pipe.add (src, decode, abin, vbin)
6.114 - gst.element_link_many (src, decode)
6.115 -
6.116 - stream_data = self.StreamData (self.pipe, abin, vbin)
6.117 -
6.118 - bus = self.pipe.get_bus()
6.119 - bus.add_signal_watch()
6.120 - bus.connect("message", self.__on_bus_message, stream_data)
6.121 -
6.122 - decode.connect("new-decoded-pad", self.__on_decode_new_pad, stream_data)
6.123 - decode.connect("unknown-type", self.__on_decode_unknown_type, stream_data)
6.124 -
6.125 -
6.126 - self.pipe.set_state (gst.STATE_PAUSED)
6.127 - print "Running Pipe"
6.128 - stream_data.Loop.run ()
6.129 - print "End run"
6.130 -
6.131 - a_caps = stream_data.ACaps
6.132 - v_caps = stream_data.VCaps
6.133 - stream_id = stream_data.Id
6.134 -
6.135 - self.streams.append (stream_data)
6.136 -
6.137 - def play(self):
6.138 -
6.139 - print "Trying to play pipeline: %s" % self.pipe
6.140 - try:
6.141 - if (self.pipe):
6.142 - self.pipe.set_state(gst.STATE_PLAYING)
6.143 - except gobject.GError, e:
6.144 - print "Error: " + str(e)
6.145 -
6.146 -
6.147 - def stop(self):
6.148 -
6.149 - print "Trying to stop pipeline: %s" % self.pipe
6.150 - try:
6.151 - if (self.pipeline):
6.152 - self.pipeline.set_state(gst.STATE_NULL)
6.153 - except gobject.GError, e:
6.154 - print "Error: " + str(e)
6.155 -
6.156 - def __on_bus_message (self, bus, message, stream_data):
6.157 -
6.158 - t = message.type
6.159 - if (t == gst.MESSAGE_STATE_CHANGED):
6.160 - oldstate = -1
6.161 - newstate = -1
6.162 - pending = -1
6.163 - oldstate, newstate, pending = message.parse_state_changed ()
6.164 - if ((oldstate == gst.STATE_READY) and \
6.165 - (newstate == gst.STATE_PAUSED) and \
6.166 - (pending == gst.STATE_VOID_PENDING) and \
6.167 - (stream_data.Ready == False)):
6.168 - state_changed_status, current_state, pending_state = stream_data.Pipe.get_state ()
6.169 - if ((current_state == gst.STATE_PAUSED) and \
6.170 - (pending_state == gst.STATE_VOID_PENDING)):
6.171 - print "Pipe paused"
6.172 - self.__fill_sink_pads (stream_data)
6.173 - stream_data.Loop.quit ()
6.174 - stream_data.Ready = True
6.175 - elif (t == gst.MESSAGE_ERROR):
6.176 - err, debug = message.parse_error()
6.177 - print "Error: %s" % err, debug
6.178 - stream_data.Loop.quit ()
6.179 - stream_data.Ready = False
6.180 -
6.181 - return True
6.182 -
6.183 -
6.184 - def __fill_sink_pads (self, stream_data):
6.185 -
6.186 - asink = stream_data.Abin.get_by_name ("asink")
6.187 - vsink = stream_data.Vbin.get_by_name ("vsink")
6.188 -
6.189 - asink_pad = asink.get_pad ("sink")
6.190 - stream_data.ACaps = asink_pad.get_negotiated_caps().to_string()
6.191 - print "ACAPS " + stream_data.ACaps
6.192 -
6.193 - vsink_pad = vsink.get_pad ("sink")
6.194 - stream_data.VCaps = vsink_pad.get_negotiated_caps().to_string()
6.195 - print "ACAPS " + stream_data.VCaps
6.196 -
6.197 -
6.198 -
6.199 - def __on_decode_unknown_type (self, decode, pad, caps, stream_data):
6.200 -
6.201 - print "Unknown Type"
6.202 - return None
6.203 -
6.204 - def __on_decode_new_pad (self, decode, pad, arg1, stream_data):
6.205 -
6.206 - caps = pad.get_caps().to_string()
6.207 - print "New pad " + caps
6.208 - if (caps.rfind ("audio") != -1):
6.209 - apad = stream_data.Abin.get_pad ("sink")
6.210 - if (pad.link (apad) != gst.PAD_LINK_OK):
6.211 - print "Error on link audio pad"
6.212 - return None
6.213 - elif (caps.rfind ("video") != -1):
6.214 - vpad = stream_data.Vbin.get_pad ("sink")
6.215 - if (pad.link (vpad) != gst.PAD_LINK_OK):
6.216 - print "Error on link video pad"
6.217 - return None
6.218 - else:
6.219 - print "Invalid caps"
6.220 -
6.221 -
7.1 --- a/gmyth-stream/server/0.1/plugins/media/gstreamer.py Tue Aug 28 15:41:35 2007 +0100
7.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
7.3 @@ -1,290 +0,0 @@
7.4 -#vim:ts=4:sw=4:et
7.5 -import pygst
7.6 -pygst.require("0.10")
7.7 -import gst
7.8 -import gobject
7.9 -import socket
7.10 -import time
7.11 -from threading import Thread
7.12 -
7.13 -class Media:
7.14 - class StreamListener(Thread):
7.15 - def __init__ (self, stream_data):
7.16 - Thread.__init__(self)
7.17 - self.stream = stream_data
7.18 - print "Thread Created"
7.19 -
7.20 - def run (self):
7.21 - #Create socket
7.22 - print "Waiting connection"
7.23 - self.stream.Socket.listen(1)
7.24 - self.stream.Connection, self.stream.Addr = self.stream.Socket.accept ()
7.25 - print "Connection requested"
7.26 - self.stream.Sink.set_property ("fd", self.stream.Connection.fileno())
7.27 - self.stream.Pipe.set_state(gst.STATE_PLAYING)
7.28 - print "PLAYING"
7.29 -
7.30 -
7.31 - class StreamData:
7.32 - stream_count = 0
7.33 -
7.34 - def __init__ (self, pipe, abin, vbin, sink):
7.35 - self.stream_count += 1
7.36 - self.Id = self.stream_count
7.37 - self.Pipe = pipe
7.38 - self.Abin = abin
7.39 - self.Vbin = vbin
7.40 - self.Sink = sink
7.41 - self.Loop = gobject.MainLoop()
7.42 - self.ACaps = ""
7.43 - self.VCaps = ""
7.44 - self.Ready = False
7.45 - self.Socket = None
7.46 - self.Connection = None
7.47 - self.Addr = None
7.48 -
7.49 - def __init__(self, config):
7.50 - # set gstreamer basic options
7.51 - self.config = config
7.52 - self.streams = []
7.53 - self.socket = None
7.54 - self.connection = None
7.55 - self.addr = None
7.56 - self.ready = False
7.57 - self.current = None
7.58 -
7.59 -
7.60 - def setup(self, uri, mux, vcodec, vbitrate,
7.61 - fps, acodec, abitrate, width, height, port, options):
7.62 -
7.63 - ## Pipelines
7.64 - pipe = gst.Pipeline ()
7.65 - print "Opening Uri:" + uri
7.66 - src = gst.element_make_from_uri (gst.URI_SRC, uri, "src")
7.67 - #src = gst.element_factory_make ("gnomevfssrc", "src")
7.68 - src.set_property ("location", uri)
7.69 - if (src is None):
7.70 - print "Fail to create src element"
7.71 - return None
7.72 -
7.73 - print ("Create source")
7.74 - decode = gst.element_factory_make ("decodebin", "decode")
7.75 - if (decode is None):
7.76 - print "Fail to create decodebin"
7.77 - return None
7.78 -
7.79 - print ("Create source")
7.80 - mux = gst.element_factory_make ("avimux", "mux")
7.81 - if (mux is None):
7.82 - print "Fail to create mux"
7.83 - return None
7.84 -
7.85 - sink = gst.element_factory_make ("fdsink", "sink")
7.86 - if (sink is None):
7.87 - print "Fail to create fdsink"
7.88 - return None
7.89 -
7.90 - print ("Create source")
7.91 -
7.92 - #video encode
7.93 - #queue ! videoscale ! video/x-raw-yuv,width=240,height=144 ! videorate ! ffenc_h263p bitrate=256000 me-method=2 ! rtph263ppay ! udpsink host=224.0.0.1 port=5000
7.94 - vbin = gst.Bin ()
7.95 - vqueue = gst.element_factory_make ("queue", "vqueue")
7.96 - colorspace = gst.element_factory_make ("ffmpegcolorspace", "")
7.97 - vrate = gst.element_factory_make ("videorate", "vrate")
7.98 - vencode = gst.element_factory_make ("ffenc_mpeg4", "vencode")
7.99 - #vencode = gst.element_factory_make ("ffenc_msmpeg4v1", "vencode")
7.100 - vqueue_src = gst.element_factory_make ("queue", "vqueue_src")
7.101 -
7.102 - #if (int(vbitrate) > 0):
7.103 - vencode.set_property ("bitrate", 200)
7.104 - #vencode.set_property ("quant-type", 1)
7.105 - vencode.set_property ("pass", 2)
7.106 - vencode.set_property ("quantizer", 5)
7.107 - #vencode.set_property ("me-method", 1)
7.108 -
7.109 -
7.110 - if (None in [vbin, vqueue, vrate, vencode, vqueue_src]):
7.111 - print "Fail to create video encode elements."
7.112 - return None
7.113 -
7.114 - vbin.add (vqueue)
7.115 - if ((int(width) > 0) and (int(height) > 0)):
7.116 - print ("formating output to %d / %d" % (int(width), int(height)))
7.117 -
7.118 - vscale = gst.element_factory_make ("ffvideoscale", "vscale")
7.119 -
7.120 - vbin.add (vscale);
7.121 - if (not vqueue.link (vscale)):
7.122 - print "Fail to link video elements"
7.123 - return None
7.124 -
7.125 - vbin.add (colorspace)
7.126 -
7.127 - if (not vscale.link (colorspace, \
7.128 - gst.caps_from_string ("video/x-raw-yuv,width=(int)%d,height=(int)%d" % (int(width), int(height))))):
7.129 - print "Fail to link video elements"
7.130 - return None
7.131 - else:
7.132 - vbin.add (colorspace)
7.133 - vqueue.link (colorspace)
7.134 -
7.135 - vbin.add (vrate, vencode, vqueue_src)
7.136 - if (not colorspace.link (vrate)):
7.137 - print "Fail to colorspace with vrate"
7.138 - return None
7.139 -
7.140 -
7.141 - if (not vrate.link (vencode, \
7.142 - gst.caps_from_string ("video/x-raw-yuv,framerate=(fraction)10/1"))):
7.143 - print "Fail to link vrate element"
7.144 - return None
7.145 -
7.146 - if (not vencode.link (vqueue_src)):
7.147 - print "Fail to link video encode with queue"
7.148 - return None
7.149 -
7.150 - vbin.add_pad (gst.GhostPad ("sink", vqueue.get_pad ("sink")))
7.151 - vbin.add_pad (gst.GhostPad ("src", vqueue_src.get_pad ("src")))
7.152 -
7.153 - #audio encode
7.154 - #audio/x-raw-int ! queue ! audioconvert ! faac ! rtpmp4gpay ! udpsink name=upd_audio host=224.0.0.1 port=5002
7.155 - abin = gst.Bin ()
7.156 - aqueue = gst.element_factory_make ("queue", "aqueue")
7.157 - aconvert = gst.element_factory_make ("audioconvert", "aconvert")
7.158 - arate = gst.element_factory_make ("audioresample", "arate")
7.159 - #aencode = gst.element_factory_make ("ffenc_ac3", "aencode")
7.160 - aencode = gst.element_factory_make ("queue", "aencode")
7.161 - #aencode = gst.element_factory_make ("lame", "aencode")
7.162 - #aencode = gst.element_factory_make ("ffenc_mp2", "aencode")
7.163 - aqueue_src = gst.element_factory_make ("queue", "aqueue_src")
7.164 -
7.165 - if (None in [abin, aqueue, arate, aencode, aqueue_src]):
7.166 - print "Fail to create video encode elements."
7.167 - return None
7.168 -
7.169 - abin.add (aqueue, aconvert, arate, aencode, aqueue_src)
7.170 - if (not gst.element_link_many (aqueue, aconvert, arate, aencode, aqueue_src)):
7.171 - print "Fail to link video elements"
7.172 - return None
7.173 -
7.174 - abin.add_pad (gst.GhostPad ("sink", aqueue.get_pad ("sink")))
7.175 - abin.add_pad (gst.GhostPad ("src", aqueue_src.get_pad ("src")))
7.176 -
7.177 - #Finish Pipeline
7.178 - pipe.add (src, decode, abin, vbin, mux, sink)
7.179 - gst.element_link_many (src, decode)
7.180 - gst.element_link_many (mux, sink)
7.181 -
7.182 - #Linking decode with mux
7.183 - mux_audio = mux.get_pad ("audio_0")
7.184 - mux_video = mux.get_pad ("video_0")
7.185 -
7.186 - audio_pad = abin.get_pad ("src")
7.187 - video_pad = vbin.get_pad ("src")
7.188 -
7.189 - if (audio_pad.link (mux_audio) != gst.PAD_LINK_OK):
7.190 - print "Fail to link audio with mux"
7.191 - return None
7.192 -
7.193 - if (video_pad.link (mux_video) != gst.PAD_LINK_OK):
7.194 - print "Fail to link audio with mux"
7.195 - return None
7.196 -
7.197 - stream_data = self.StreamData (pipe, abin, vbin, sink)
7.198 - bus = pipe.get_bus()
7.199 - bus.add_signal_watch()
7.200 - bus.connect ("message", self.__on_bus_message, stream_data)
7.201 -
7.202 - decode.connect("new-decoded-pad", self.__on_decode_new_pad, stream_data)
7.203 - decode.connect("unknown-type", self.__on_decode_unknown_type, stream_data)
7.204 -
7.205 - print ("Create source")
7.206 - pipe.set_state (gst.STATE_PAUSED)
7.207 - print "Running Pipe"
7.208 - stream_data.Loop.run ()
7.209 - print "End run"
7.210 -
7.211 -
7.212 - #Create socket
7.213 - stream_data.Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7.214 - print "Bind on port %d" % port
7.215 - stream_data.Socket.bind(('', int (port)))
7.216 - self.streams.append (stream_data)
7.217 - return (True, "")
7.218 -
7.219 - def play(self):
7.220 - print "Play"
7.221 - stream = self.streams[0]
7.222 - self.current = self.StreamListener(stream)
7.223 - self.current.start ()
7.224 - time.sleep (1)
7.225 - return (True, "")
7.226 -
7.227 - def stop(self):
7.228 - self.current.join ()
7.229 - self.current = None
7.230 - stream = self.streams[0]
7.231 - stream.Pipe.set_state(gst.STATE_NULL)
7.232 - del (stream.Pipe)
7.233 - stream.Pipe = None
7.234 - stream.Abin = None
7.235 - stream.Vbin = None
7.236 - stream.Sink = None
7.237 - if (stream.Connection != None):
7.238 - stream.Connection.close ()
7.239 -
7.240 - self.streams = []
7.241 - time.sleep (5)
7.242 - return (True, "")
7.243 -
7.244 -
7.245 - def __on_bus_message (self, bus, message, stream_data):
7.246 -
7.247 - t = message.type
7.248 - if (t == gst.MESSAGE_STATE_CHANGED):
7.249 - oldstate = -1
7.250 - newstate = -1
7.251 - pending = -1
7.252 - oldstate, newstate, pending = message.parse_state_changed ()
7.253 - if ((oldstate == gst.STATE_READY) and \
7.254 - (newstate == gst.STATE_PAUSED) and \
7.255 - (pending == gst.STATE_VOID_PENDING) and \
7.256 - (stream_data.Ready == False)):
7.257 - state_changed_status, current_state, pending_state = stream_data.Pipe.get_state ()
7.258 - if ((current_state == gst.STATE_PAUSED) and \
7.259 - (pending_state == gst.STATE_VOID_PENDING)):
7.260 - print "Pipe paused"
7.261 - stream_data.Loop.quit ()
7.262 - stream_data.Ready = True
7.263 - elif (t == gst.MESSAGE_ERROR):
7.264 - err, debug = message.parse_error()
7.265 - print "Error: %s" % err, debug
7.266 - stream_data.Loop.quit ()
7.267 - stream_data.Ready = False
7.268 -
7.269 - return True
7.270 -
7.271 - def __on_decode_unknown_type (self, decode, pad, caps, stream_data):
7.272 -
7.273 - print "Unknown Type"
7.274 - return None
7.275 -
7.276 - def __on_decode_new_pad (self, decode, pad, arg1, stream_data):
7.277 -
7.278 - caps = pad.get_caps().to_string()
7.279 - print "New pad " + caps
7.280 - if (caps.rfind ("audio") != -1):
7.281 - apad = stream_data.Abin.get_pad ("sink")
7.282 - if (pad.link (apad) != gst.PAD_LINK_OK):
7.283 - print "Error on link audio pad"
7.284 - return None
7.285 - elif (caps.rfind ("video") != -1):
7.286 - vpad = stream_data.Vbin.get_pad ("sink")
7.287 - if (pad.link (vpad) != gst.PAD_LINK_OK):
7.288 - print "Error on link video pad"
7.289 - return None
7.290 - else:
7.291 - print "Invalid caps"
7.292 - print "Linked"
7.293 -
8.1 --- a/gmyth-stream/server/0.1/plugins/media/mencoder.py Tue Aug 28 15:41:35 2007 +0100
8.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
8.3 @@ -1,411 +0,0 @@
8.4 -from __future__ import division
8.5 -
8.6 -import os
8.7 -import sys
8.8 -import lib
8.9 -import time
8.10 -import shlex
8.11 -import signal
8.12 -import socket
8.13 -import ConfigParser
8.14 -import logging as log
8.15 -
8.16 -from select import *
8.17 -from subprocess import *
8.18 -
8.19 -class Media(object):
8.20 -
8.21 - def __init__(self, config):
8.22 -
8.23 - self.config = config
8.24 - self.do_cleanup()
8.25 -
8.26 - # __init__()
8.27 -
8.28 -
8.29 - def do_cleanup(self):
8.30 - self.path = ""
8.31 - self.args = []
8.32 - self.language = None
8.33 - self.subtitle = None
8.34 - self.mpegopts = None
8.35 - self.socket = None
8.36 - self.child_pid = None
8.37 - self.mplayer = None
8.38 - self.mencoder_pid = None
8.39 - self.mplayer_pid = None
8.40 - self.audio_opts = None
8.41 - self.video_opts = None
8.42 - self.gst_pipe = None
8.43 - self.gst_pid = None
8.44 - self.transcode_local = None
8.45 -
8.46 - # do_cleanup()
8.47 -
8.48 -
8.49 - def setup_opts(self, options):
8.50 -
8.51 - for opt in options:
8.52 -
8.53 - if opt == "local":
8.54 - self.mplayer = lib.which("mplayer")
8.55 -
8.56 - elif opt.find("language=") >= 0:
8.57 - try:
8.58 - lan = opt.split("=")[1]
8.59 - if len(lan) < 2:
8.60 - self.language = lan
8.61 - except Exception, e:
8.62 - log.error("Bad language option: %s" % opt)
8.63 -
8.64 - elif opt.find("subtitle=") >= 0:
8.65 - try:
8.66 - sub = opt.split("=")[1]
8.67 - if len(sub) < 2:
8.68 - self.language = sub
8.69 - except Exception, e:
8.70 - log.error("Bad subtitle option: %s" % opt)
8.71 -
8.72 - elif opt.find("format=") >= 0:
8.73 - try:
8.74 - self.mpegopts = opt.split("=")[1]
8.75 - except Exception, e:
8.76 - log.error("Bad format option: %s" % opt)
8.77 -
8.78 - elif opt.find("outfile=") >= 0:
8.79 - try:
8.80 - self.transcode_local = opt.split("=")[1]
8.81 - except Exception, e:
8.82 - log.error("Bad outfile option: %s" % opt)
8.83 -
8.84 - # setup_opts()
8.85 -
8.86 -
8.87 - def run_mplayer(self):
8.88 - msg = self.filename
8.89 -
8.90 - if self.kind == "dvd":
8.91 - msg = "dvd://" + msg
8.92 -
8.93 - self.mplayer_pid = Popen([self.mplayer, self.filename, "1> %s" % os.devnull,\
8.94 - "2> %s" % os.devnull], stdout=PIPE, close_fds=True)
8.95 -
8.96 - # run_mplayer()
8.97 -
8.98 -
8.99 - def setup_mencoder(self):
8.100 - self.path = self.config.get("Mencoder", "path")
8.101 - mp = Popen([self.path], stdout=PIPE, close_fds=True)
8.102 -
8.103 - version = mp.stdout.read().split("MEncoder ")[1].split(" (C)")[0].split("-")[-1]
8.104 -
8.105 - if version > "4.1.1": self.mencoder_old = False
8.106 - else: self.mencoder_old = True
8.107 -
8.108 - os.kill(mp.pid, signal.SIGKILL)
8.109 - log.info("Mencoder version: %s" % version)
8.110 -
8.111 - if self.mencoder_old:
8.112 - try:
8.113 - self.fifo = self.config.get("Mencoder", "fifo_path")
8.114 - os.mkfifo(self.fifo)
8.115 - except Exception, e:
8.116 - log.info("Fifo: %s" % e)
8.117 - else:
8.118 - self.fifo = "-"
8.119 -
8.120 - # setup_mencoder()
8.121 -
8.122 -
8.123 - def setup_audio(self):
8.124 -
8.125 - if self.acodec == "mp3lame":
8.126 - return "-oac mp3lame -lameopts cbr:br=%s vol=5" % self.abitrate
8.127 - else:
8.128 - return "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (\
8.129 - self.acodec, self.abitrate)
8.130 -
8.131 - # setup_audio()
8.132 -
8.133 -
8.134 - def setup_video(self):
8.135 -
8.136 - video = ""
8.137 -
8.138 - video += " -of %s" % self.mux
8.139 - video += " -ofps %s" % self.fps
8.140 -
8.141 - if self.vcodec == "nuv" or self.vcodec == "xvid"\
8.142 - or self.vcodec == "qtvideo" or self.vcodec == "copy":
8.143 - video += " -ovc %s" % self.vcodec
8.144 - else:
8.145 - video += " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s" % (
8.146 - self.vcodec, self.vbitrate)
8.147 -
8.148 - if self.mux == "mpeg" and self.mpegopts is not None:
8.149 - video += " -mpegopts format=%s" % self.mpegopts
8.150 -
8.151 - video += " -vf scale=%s:%s" % (self.width, self.height)
8.152 -
8.153 - return video
8.154 -
8.155 - # setup_video()
8.156 -
8.157 -
8.158 - def arg_append(self, args, options):
8.159 - l = shlex.split(options)
8.160 - for i in l:
8.161 - args.append(i)
8.162 -
8.163 - # arg_append()
8.164 -
8.165 -
8.166 - def setup_args(self, args):
8.167 -
8.168 - args.append(self.path)
8.169 -
8.170 - #args.append(self.filename)
8.171 - args.append("-")
8.172 -
8.173 - if self.language != None:
8.174 - self.arg_append(args, "-alang %s" % self.language)
8.175 -
8.176 - if self.subtitle != None:
8.177 - self.arg_append(args, "-slang %s" % self.subtitle)
8.178 - self.arg_append(args, "-subfps %s" % self.fps)
8.179 -
8.180 - self.arg_append(args, "-idx")
8.181 - self.arg_append(args, self.audio_opts)
8.182 - self.arg_append(args, self.video_opts)
8.183 -
8.184 - self.arg_append(args, "-really-quiet")
8.185 - self.arg_append(args, "-o %s" % self.fifo)
8.186 - self.arg_append(args, "2> %s" % os.devnull)
8.187 -
8.188 - # setup_args()
8.189 -
8.190 -
8.191 - def setup_filename(self, filename):
8.192 - try:
8.193 - self.kind, self.filename = filename.split("://")
8.194 - except:
8.195 - return (False, "Wrong filename protocol")
8.196 -
8.197 - if self.kind == "file":
8.198 - if not os.path.exists(self.filename):
8.199 - msg = "File requested does not exist. SETUP failed."
8.200 - log.error(msg)
8.201 - return (False, msg)
8.202 -
8.203 - elif self.kind == "dvd":
8.204 - self.filename = "dvd://" + filename
8.205 -
8.206 - elif self.kind == "myth":
8.207 - self.filename = filename
8.208 - self.gst_pipe = os.pipe()
8.209 - print self.gst_pipe[0]
8.210 - print self.gst_pipe[1]
8.211 -
8.212 - return (True, "")
8.213 -
8.214 - # setup_filename()
8.215 -
8.216 -
8.217 - def setup_socket(self):
8.218 - if self.socket != None:
8.219 - self.socket = None
8.220 -
8.221 - self.socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
8.222 - self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
8.223 -
8.224 - try:
8.225 - self.socket.bind( ('', self.port) )
8.226 - self.socket.listen(1)
8.227 - except Exception, e:
8.228 - log.error("Could not create socket: %s" % e)
8.229 - return (False, e)
8.230 -
8.231 - return (True, "")
8.232 -
8.233 - # setup_socket()
8.234 -
8.235 -
8.236 - '''
8.237 - MENCODER SETUP DESCRIPTION
8.238 - ===========================
8.239 -
8.240 - -> mux, vcodecs and acodecs
8.241 - |-> mencoder (-of | -ovc | -oac) help
8.242 -
8.243 - -> if used mpeg as mux:
8.244 - |-> to setup format: format=%s as an option at the end
8.245 -
8.246 - '''
8.247 -
8.248 -
8.249 - # good one: /tmp/dvb.mpg avi mpeg4 400 25 mp3lame 192 320 240
8.250 - # file:///tmp/dvb.mpg mpeg mpeg1video 400 25 mp2 192 320 240 format=mpeg1
8.251 - # dvd://4 mpeg mpeg1video 400 25 mp3lame 192 400 240 language=en local
8.252 - # file:///tmp/mpg/bad_day.mpg avi mpeg4 400 25 mp3 192 320 240
8.253 -
8.254 - def setup(self, filename, mux, vcodec, vbitrate,\
8.255 - fps, acodec, abitrate, width, height, port, options):
8.256 -
8.257 - if self.args != []:
8.258 - self.do_cleanup()
8.259 -
8.260 - self.mux = mux
8.261 - self.vcodec = vcodec
8.262 - self.vbitrate = vbitrate
8.263 - self.fps = fps
8.264 - self.acodec = acodec
8.265 - self.abitrate = abitrate
8.266 - self.width = width
8.267 - self.height = height
8.268 - self.port = int(port)
8.269 -
8.270 - self.setup_mencoder()
8.271 -
8.272 - ret_val = self.setup_filename(filename)
8.273 -
8.274 - if not ret_val[0]:
8.275 - return ret_val
8.276 -
8.277 - self.setup_opts(options)
8.278 - self.audio_opts = self.setup_audio()
8.279 - self.video_opts = self.setup_video()
8.280 - self.setup_args(self.args)
8.281 -
8.282 - ret_val = self.setup_socket()
8.283 - return ret_val
8.284 -
8.285 - # setup()
8.286 -
8.287 - def play_loop(self, conn):
8.288 - data = self.pout.read(4096)
8.289 -
8.290 - conn.settimeout(5)
8.291 - retry = 0
8.292 -
8.293 - if not self.transcode_local:
8.294 - while data != "" and retry < 5:
8.295 - try:
8.296 - conn.send(data)
8.297 - r, w, x = select([conn], [], [], 0)
8.298 - if conn in r:
8.299 - back = conn.recv(1024)
8.300 - if back == "OK" and self.mplayer and not self.mplayer_pid:
8.301 - self.run_mplayer()
8.302 -
8.303 - except socket.error, e:
8.304 - log.error("Socket error: %s" % e)
8.305 - retry += 1
8.306 -
8.307 - data = self.pout.read(4096)
8.308 -
8.309 - else:
8.310 - local = open(self.transcode_local, "w")
8.311 - total = os.path.getsize(self.filename)
8.312 - partial = 4096
8.313 -
8.314 - while data != "":
8.315 - try:
8.316 - local.write(data)
8.317 - except Exception, e:
8.318 - log.error("Write error: %s" % e)
8.319 -
8.320 - data = self.pout.read(4096)
8.321 - partial += len(data)
8.322 - conn.send("%.2f\n" % (partial * 100 / total) )
8.323 -
8.324 - local.close()
8.325 - conn.send("DONE\n")
8.326 -
8.327 - return retry
8.328 -
8.329 - # play_loop()
8.330 -
8.331 -
8.332 - def play(self):
8.333 -
8.334 - if self.gst_pipe:
8.335 - try:
8.336 - gst = [ lib.which("gst-launch-0.10"), "--gst-debug-level=0" ]
8.337 - self.arg_append(gst, "mythtvsrc location=%s" % self.filename)
8.338 - self.arg_append(gst, "! fdsink fd=2")
8.339 - self.gst_pid = Popen(gst, close_fds=True)
8.340 - log.info("Running Gstreamer: %s" % gst);
8.341 - except Exception, e:
8.342 - msg = "Could not init Gstreamer: %s" % e
8.343 - log.error(msg)
8.344 - return (False, msg)
8.345 -
8.346 -
8.347 - log.info("Starting Mencoder: %s" % self.args )
8.348 - try:
8.349 - if not self.gst_pipe:
8.350 - self.stdin = open(self.filename)
8.351 - else:
8.352 - self.stdin = self.gst_pid.stdout
8.353 -
8.354 - self.mencoder_pid = Popen(self.args, stdin=self.stdin, stdout=PIPE, close_fds=True)
8.355 - except Exception, e:
8.356 - msg = "Could not init Mencoder: %s" % e
8.357 - log.error(msg)
8.358 - return (False, msg)
8.359 -
8.360 - if self.mencoder_old: self.pout = open(self.fifo)
8.361 - else: self.pout = self.mencoder_pid.stdout
8.362 -
8.363 - self.child_pid = os.fork()
8.364 -
8.365 - if self.child_pid == 0:
8.366 - conn, addr = self.socket.accept()
8.367 -
8.368 - log.info("Sending Data to client: %s" % addr[0])
8.369 - retry = self.play_loop(conn)
8.370 -
8.371 - if retry < 5:
8.372 - log.info("Finished sending Data to client: %s" % addr[0])
8.373 - else:
8.374 - log.error("Client timed out, retried more than %s times" % retry)
8.375 -
8.376 - os.kill(self.mencoder_pid.pid, signal.SIGKILL)
8.377 - sys.exit(0)
8.378 -
8.379 - return (True, "")
8.380 -
8.381 - # play()
8.382 -
8.383 -
8.384 - def stop(self):
8.385 - try:
8.386 -
8.387 - if self.mencoder_pid:
8.388 - os.kill(self.mencoder_pid.pid, signal.SIGTERM)
8.389 - self.mencoder_pid = None
8.390 -
8.391 - if self.mplayer_pid:
8.392 - os.kill(self.mplayer_pid.pid, signal.SIGTERM)
8.393 - self.mplayer_pid = None
8.394 -
8.395 - if self.socket:
8.396 - self.socket.close()
8.397 - self.socket = None
8.398 -
8.399 - if self.child_pid:
8.400 - os.kill(self.child_pid, signal.SIGTERM)
8.401 - self.child_pid = None
8.402 -
8.403 - if self.gst_pid:
8.404 - os.kill(self.gst_pid.pid, signal.SIGTERM)
8.405 - self.gst_pid = None
8.406 -
8.407 - self.do_cleanup()
8.408 -
8.409 - os.wait()
8.410 -
8.411 - except Exception, e:
8.412 - log.error("Stop error: %s" % e)
8.413 -
8.414 - # stop()
9.1 --- a/gmyth-stream/server/0.1/plugins/media/vlc.py Tue Aug 28 15:41:35 2007 +0100
9.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
9.3 @@ -1,80 +0,0 @@
9.4 -import os
9.5 -import sys
9.6 -import time
9.7 -import socket
9.8 -import ConfigParser
9.9 -
9.10 -class Media:
9.11 -
9.12 - def __init__(self, config):
9.13 -
9.14 - self.config = config
9.15 - self.pipe = ""
9.16 - self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
9.17 -
9.18 - self.path = config.get("Vlc", "path")
9.19 - self.host = config.get("Vlc", "host")
9.20 - self.port = int(config.get("Vlc", "port"))
9.21 - self.pwd = config.get("Vlc", "pwd")
9.22 -
9.23 - # exec VLC
9.24 - pid = os.fork()
9.25 - if (pid == 0):
9.26 - #child
9.27 - print "ESTOU EM CHILD"
9.28 - self.path += " -I telnet -d 1> /dev/null 2> /dev/null &"
9.29 - os.system(self.path)
9.30 - sys.exit(0)
9.31 - else:
9.32 - print "ESTOU EM PARENT 1"
9.33 - time.sleep(3)
9.34 - print "ESTOU EM PARENT 2"
9.35 - self.sock.connect( (self.host, self.port) )
9.36 - self.sock.send("%s\n" % self.pwd)
9.37 -
9.38 -
9.39 - def insert_file(self, filename):
9.40 -
9.41 - self.sock.send("setup output0 input %s\n" % filename)
9.42 -
9.43 -
9.44 -
9.45 - def setup(self, filename, mux, vcodec, vbitrate,\
9.46 - fps, acodec, abitrate, width, height, port):
9.47 -
9.48 - self.filename = filename
9.49 - self.mux = mux
9.50 - self.vcodec = vcodec
9.51 - self.vbitrate = int(vbitrate)
9.52 - self.fps = int(fps)
9.53 - self.acodec = acodec
9.54 - self.abitrate = int(abitrate)
9.55 - self.width = int(width)
9.56 - self.height = int(height)
9.57 -
9.58 - self.port = int(port)
9.59 -
9.60 -
9.61 - self.pipe = "#transcode{vcodec=%s,vb=%d,"\
9.62 - "fps=25.0,scale=1,acodec=mpga,"\
9.63 - "ab=64,channels=1,width=%d,height=%d}"\
9.64 - ":duplicate{dst=std{access=http,"\
9.65 - "mux=mpeg1,dst=:%d}}" % (self.vcodec, self.vbitrate,\
9.66 - self.widht, self.height,\
9.67 - self.port)
9.68 -
9.69 - self.sock.send("setup output0 broadcast %s\n" % self.pipe)
9.70 - self.insert_file(self.filename)
9.71 -
9.72 - def play(self):
9.73 -
9.74 - print "Trying to play: %s" % self.pipe
9.75 - self.sock.send("control output0 play\n")
9.76 -
9.77 -
9.78 - def stop(self):
9.79 -
9.80 - print "Trying to stop: %s" % self.pipe
9.81 - self.sock.send("control output0 stop\n")
9.82 -
9.83 -
10.1 --- a/gmyth-stream/server/0.1/stream.conf Tue Aug 28 15:41:35 2007 +0100
10.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
10.3 @@ -1,23 +0,0 @@
10.4 -[Comm]
10.5 -engine = tcp
10.6 -port = 50000
10.7 -
10.8 -
10.9 -[Media]
10.10 -engine = mencoder
10.11 -
10.12 -
10.13 -[Vlc]
10.14 -path = /usr/local/bin/vlc
10.15 -host = 127.0.0.1
10.16 -port = 4212
10.17 -pwd = admin
10.18 -
10.19 -
10.20 -[FFmpeg]
10.21 -path = /usr/local/bin/ffmpeg
10.22 -
10.23 -
10.24 -[Mencoder]
10.25 -path = /usr/local/bin/mencoder
10.26 -fifo_path = /tmp/teste
11.1 --- a/gmyth-stream/server/0.1/tests/client.py Tue Aug 28 15:41:35 2007 +0100
11.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
11.3 @@ -1,51 +0,0 @@
11.4 -import os
11.5 -import sys
11.6 -import time
11.7 -import socket
11.8 -
11.9 -
11.10 -if len(sys.argv) < 2:
11.11 - HOST = 'localhost'
11.12 - PORT = 50000
11.13 -elif len(sys.argv) == 2:
11.14 - HOST = sys.argv[1]
11.15 - PORT = 50000
11.16 -else:
11.17 - HOST = sys.argv[1]
11.18 - PORT = int(sys.argv[2])
11.19 -
11.20 -socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
11.21 -socket.settimeout(10)
11.22 -
11.23 -try:
11.24 - socket.connect( (HOST,PORT) )
11.25 -except:
11.26 - print "\n--> Could not connect to ('%s':'%d')\n" % (HOST,PORT)
11.27 - sys.exit(-1)
11.28 -
11.29 -
11.30 -mplayer = os.popen("which mplayer").read().strip()
11.31 -mplayer += " -idx - -vo x11 1> /dev/null"
11.32 -pin, pout = os.popen2(mplayer)
11.33 -
11.34 -#teste = open("teste.avi", "w")
11.35 -
11.36 -data = socket.recv(4096)
11.37 -i = 0
11.38 -
11.39 -while (data != ""):
11.40 - pin.write(data)
11.41 - #teste.write(data)
11.42 - data = socket.recv(4096)
11.43 - #if (i == 500):
11.44 - # socket.send("OK")
11.45 - i += 1
11.46 -
11.47 -pin.close()
11.48 -socket.close()
11.49 -#teste.close()
11.50 -
11.51 -# from select import select
11.52 -# r, w, x = select([pout], []. [], 0)
11.53 -# if pout in r:
11.54 -# pout.read(32)
12.1 --- a/gmyth-stream/server/0.2/gms.py Tue Aug 28 15:41:35 2007 +0100
12.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
12.3 @@ -1,20 +0,0 @@
12.4 -#!/usr/bin/env python
12.5 -
12.6 -import sys
12.7 -import os
12.8 -import logging as log
12.9 -from lib.server import serve_forever, load_plugins_transcoders
12.10 -
12.11 -log_level = log.INFO
12.12 -for p in sys.argv[1:]:
12.13 - if p == "-v" or p == "--verbose":
12.14 - log_level -= 10
12.15 -
12.16 -log.basicConfig(level=log_level,
12.17 - format=("### %(asctime)s %(name)-18s \t%(levelname)-8s "
12.18 - "\t%(message)s"),
12.19 - datefmt="%Y-%m-%d %H:%M:%S")
12.20 -
12.21 -pd = os.path.join("plugins", "transcoders")
12.22 -load_plugins_transcoders(pd)
12.23 -serve_forever()
13.1 --- a/gmyth-stream/server/0.2/html/index.html Tue Aug 28 15:41:35 2007 +0100
13.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
13.3 @@ -1,9 +0,0 @@
13.4 -<html>
13.5 - <head><title>GMyth-Streamer Server</title></head>
13.6 - <body>
13.7 -<h1>Welcome to GMyth-Streamer Server</h1>
13.8 -<ul>
13.9 -%(menu)s
13.10 -</ul>
13.11 - </body>
13.12 -</html>
13.13 \ No newline at end of file
14.1 --- a/gmyth-stream/server/0.2/html/menu.html Tue Aug 28 15:41:35 2007 +0100
14.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
14.3 @@ -1,1 +0,0 @@
14.4 -<li><a href="%(url)s">%(name)s</a></li>
15.1 --- a/gmyth-stream/server/0.2/html/shutdown.html Tue Aug 28 15:41:35 2007 +0100
15.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
15.3 @@ -1,6 +0,0 @@
15.4 -<html>
15.5 - <head><title>GMyth-Streamer Server Exited</title></head>
15.6 - <body>
15.7 - <h1>GMyth-Streamer is not running anymore</h1>
15.8 - </body>
15.9 -</html>
15.10 \ No newline at end of file
16.1 --- a/gmyth-stream/server/0.2/html/status.html Tue Aug 28 15:41:35 2007 +0100
16.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
16.3 @@ -1,14 +0,0 @@
16.4 -<html>
16.5 - <head><title>GMyth-Streamer Server Status</title></head>
16.6 - <body>
16.7 - <h1>GMyth-Streamer Status</h1>
16.8 - <ul>
16.9 - %(running)s
16.10 - %(stopall)s
16.11 - %(stopone)s
16.12 - </ul>
16.13 - <ul>
16.14 - %(menu)s
16.15 - </ul>
16.16 - </body>
16.17 -</html>
17.1 --- a/gmyth-stream/server/0.2/html/stop_all.html Tue Aug 28 15:41:35 2007 +0100
17.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
17.3 @@ -1,9 +0,0 @@
17.4 -<html>
17.5 - <head><title>GMyth-Streamer Server Stopped Transcoders</title></head>
17.6 - <body>
17.7 - <h1>GMyth-Streamer stopped running transcoders</h1>
17.8 - <ul>
17.9 - %(menu)s
17.10 - </ul>
17.11 - </body>
17.12 -</html>
17.13 \ No newline at end of file
18.1 --- a/gmyth-stream/server/0.2/html/stop_selected.html Tue Aug 28 15:41:35 2007 +0100
18.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
18.3 @@ -1,12 +0,0 @@
18.4 -<html>
18.5 - <head><title>GMyth-Streamer Server Stopped Transcoders</title></head>
18.6 - <body>
18.7 - <h1>GMyth-Streamer stopped running transcoders:</h1>
18.8 - <ul>
18.9 - %(opts)s
18.10 - </ul>
18.11 - <ul>
18.12 - %(menu)s
18.13 - </ul>
18.14 - </body>
18.15 -</html>
19.1 --- a/gmyth-stream/server/0.2/lib/server.py Tue Aug 28 15:41:35 2007 +0100
19.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
19.3 @@ -1,424 +0,0 @@
19.4 -#!/usr/bin/env python
19.5 -
19.6 -__author__ = "Gustavo Sverzut Barbieri / Artur Duque de Souza"
19.7 -__author_email__ = "barbieri@gmail.com / artur.souza@indt.org.br"
19.8 -__license__ = "GPL"
19.9 -__version__ = "0.2"
19.10 -
19.11 -import os
19.12 -import threading
19.13 -import SocketServer
19.14 -import BaseHTTPServer
19.15 -import socket
19.16 -import urlparse
19.17 -import cgi
19.18 -import lib.utils as utils
19.19 -import logging as log
19.20 -
19.21 -__all__ = ("Transcoder", "RequestHandler", "Server", "serve_forever",
19.22 - "load_plugins_transcoders")
19.23 -
19.24 -class Transcoder(object):
19.25 - log = log.getLogger("gms.transcoder")
19.26 - priority = 0 # negative values have higher priorities
19.27 - name = None # to be used in requests
19.28 - status = None
19.29 - tid = -1
19.30 -
19.31 - def __init__(self, params):
19.32 - self.params = params
19.33 - # __init__()
19.34 -
19.35 -
19.36 - def params_first(self, key, default=None):
19.37 - if default is None:
19.38 - return self.params[key][0]
19.39 - else:
19.40 - try:
19.41 - return self.params[key][0]
19.42 - except:
19.43 - return default
19.44 - # params_first()
19.45 -
19.46 -
19.47 - def get_mimetype(self):
19.48 - mux = self.params_first("mux", "mpg")
19.49 -
19.50 - if mux == "mpeg":
19.51 - return "video/mpeg"
19.52 - elif mux == "avi":
19.53 - return "video/x-msvideo"
19.54 - else:
19.55 - return "application/octet-stream"
19.56 - # get_mimetype()
19.57 -
19.58 -
19.59 - def start(self, outfile):
19.60 - return True
19.61 - # start()
19.62 -
19.63 -
19.64 - def stop(self):
19.65 - return True
19.66 - # stop()
19.67 -
19.68 -
19.69 - def __str__(self):
19.70 - return '%s( params=%s )' % \
19.71 - (self.__class__.__name__,
19.72 - self.params)
19.73 - # __str__()
19.74 -# Transcoder
19.75 -
19.76 -
19.77 -
19.78 -class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
19.79 - log = log.getLogger("gms.request")
19.80 - def_transcoder = None
19.81 - transcoders = utils.PluginSet(Transcoder)
19.82 -
19.83 - menu = {
19.84 - "Stop": "/stop-transcoder.do",
19.85 - "Status": "/status.do",
19.86 - "Version": "/version.do",
19.87 - "Shutdown": "/shutdown.do"
19.88 - }
19.89 -
19.90 - @classmethod
19.91 - def load_plugins_transcoders(cls, directory):
19.92 - cls.transcoders.load_from_directory(directory)
19.93 -
19.94 - if cls.def_transcoder is None and cls.transcoders:
19.95 - cls.def_transcoder = cls.transcoders[0].name
19.96 - # load_plugins_transcoders()
19.97 -
19.98 -
19.99 - def do_dispatch(self, body):
19.100 - self.url = self.path
19.101 -
19.102 - pieces = urlparse.urlparse(self.path)
19.103 - self.path = pieces[2]
19.104 - self.query = cgi.parse_qs(pieces[4])
19.105 -
19.106 - if self.path == "/":
19.107 - self.serve_main(body)
19.108 - elif self.path == "/shutdown.do":
19.109 - self.serve_shutdown(body)
19.110 - elif self.path == "/stop-transcoder.do":
19.111 - self.serve_stop_transcoder(body)
19.112 - elif self.path == "/status.do":
19.113 - self.serve_status(body)
19.114 - elif self.path == "/version.do":
19.115 - self.serve_version(body)
19.116 - elif self.path == "/stream.do":
19.117 - self.serve_stream(body)
19.118 - else:
19.119 - action = self.query.get("action", None)
19.120 - if "stream.do" in action:
19.121 - self.serve_stream(body)
19.122 - else:
19.123 - self.send_error(404, "File not found")
19.124 - # do_dispatch()
19.125 -
19.126 -
19.127 - def do_GET(self):
19.128 - self.do_dispatch(True)
19.129 - # do_GET()
19.130 -
19.131 -
19.132 - def do_HEAD(self):
19.133 - self.do_dispatch(False)
19.134 - # do_HEAD()
19.135 -
19.136 -
19.137 - def _nav_items(self):
19.138 - ret = ""
19.139 - for name, url in self.menu.items():
19.140 - ret += utils.getHTML("menu", {"name": name, "url": url})
19.141 -
19.142 - return ret
19.143 - # _nav_items()
19.144 -
19.145 - def serve_main(self, body):
19.146 - self.send_response(200)
19.147 - self.send_header("Content-Type", "text/html")
19.148 - self.send_header('Connection', 'close')
19.149 - self.end_headers()
19.150 - if body:
19.151 - self.wfile.write(utils.getHTML("index", {"menu": self._nav_items()}))
19.152 - # serve_main()
19.153 -
19.154 - def serve_version(self, body):
19.155 - self.send_response(200)
19.156 - self.send_header("Content-Type", "text/html")
19.157 - self.send_header('Connection', 'close')
19.158 - self.end_headers()
19.159 - if body:
19.160 - self.wfile.write("Version: %s" % __version__)
19.161 -
19.162 -
19.163 - def serve_shutdown(self, body):
19.164 - self.send_response(200)
19.165 - self.send_header("Content-Type", "text/html")
19.166 - self.send_header('Connection', 'close')
19.167 - self.end_headers()
19.168 - if body:
19.169 - self.wfile.write(utils.getHTML("shutdown"))
19.170 - self.server.server_close()
19.171 - # serve_shutdown()
19.172 -
19.173 -
19.174 - def serve_stop_all_transcoders(self, body):
19.175 - self.send_response(200)
19.176 - self.send_header("Content-Type", "text/html")
19.177 - self.send_header('Connection', 'close')
19.178 - self.end_headers()
19.179 - if body:
19.180 - self.server.stop_transcoders()
19.181 - self.wfile.write(utils.getHTML("stop_all", {"menu": self._nav_items()}))
19.182 - # serve_stop_all_transcoders()
19.183 -
19.184 -
19.185 - def serve_stop_selected_transcoders(self, body, requests):
19.186 - self.send_response(200)
19.187 - self.send_header("Content-Type", "text/html")
19.188 - self.send_header('Connection', 'close')
19.189 - self.end_headers()
19.190 - opts = ""
19.191 - if body:
19.192 - transcoders = self.server.get_transcoders()
19.193 -
19.194 - for req in requests:
19.195 - try:
19.196 - host, port = req.split(":")
19.197 - except IndexError:
19.198 - continue
19.199 -
19.200 - port = int(port)
19.201 - addr = (host, port)
19.202 -
19.203 - for t, r in transcoders:
19.204 - if r.client_address == addr:
19.205 - try:
19.206 - t.stop()
19.207 - except Exception, e:
19.208 - self.log.info("Plugin already stopped")
19.209 -
19.210 - opts += self._create_html_item("%s: %s:%s" % (
19.211 - t, addr[0], addr[1]))
19.212 -
19.213 - break
19.214 -
19.215 - self.wfile.write(utils.getHTML("stop_selected",
19.216 - {"menu": self._nav_items(),
19.217 - "opts": opts}))
19.218 - # serve_stop_selected_transcoders()
19.219 -
19.220 -
19.221 - def serve_stop_transcoder(self, body):
19.222 - req = self.query.get("request", None)
19.223 - if req and "all" in req:
19.224 - self.serve_stop_all_transcoders(body)
19.225 - elif req:
19.226 - self.serve_stop_selected_transcoders(body, req)
19.227 - else:
19.228 - self.serve_status(body)
19.229 - # serve_stop_transcoder()
19.230 -
19.231 -
19.232 - def serve_status(self, body):
19.233 - self.send_response(200)
19.234 - self.send_header("Content-Type", "text/html")
19.235 - self.send_header('Connection', 'close')
19.236 - self.end_headers()
19.237 - stopone = ""
19.238 -
19.239 - if body:
19.240 - tl = self.server.get_transcoders()
19.241 - if not tl:
19.242 - running = "<p>No running transcoder.</p>\n"
19.243 - stopall = ""
19.244 -
19.245 - elif self.query.get("ip") and self.query.get("file"):
19.246 - for transcoder, request in tl:
19.247 - filename = "%s" % self.query.get("file")[0]
19.248 - tfilename = "%s" % transcoder.params_first("uri")
19.249 -
19.250 - if tfilename.find(filename) >= 0 and \
19.251 - request.client_address[0] == self.query.get("ip")[0]:
19.252 - self.wfile.write("Status: %s %%" % transcoder.status)
19.253 - return True
19.254 -
19.255 - return False
19.256 -
19.257 - else:
19.258 - running = "<p>Running transcoders:</p>\n"
19.259 - stopall = utils._create_html_item("<a href='%s?request=all'>"
19.260 - "[STOP ALL]</a>" %
19.261 - self.menu["Stop"])
19.262 -
19.263 - for transcoder, request in tl:
19.264 - stopone += utils._create_html_item("%s: %s:%s<a href='%s?"
19.265 - "request=%s:%s'>"
19.266 - "[STOP]</a> - "
19.267 - "Status: %s%%"\
19.268 - % (
19.269 - transcoder, request.client_address[0],
19.270 - request.client_address[1],
19.271 - self.menu["Stop"], request.client_address[0],
19.272 - request.client_address[1],
19.273 - transcoder.status) )
19.274 -
19.275 - self.wfile.write(utils.getHTML("status",
19.276 - {"menu": self._nav_items(),
19.277 - "running": running,
19.278 - "stopall": stopall,
19.279 - "stopone": stopone}))
19.280 - # serve_status()
19.281 -
19.282 -
19.283 - def _get_transcoder(self):
19.284 - # get transcoder option: mencoder is the default
19.285 - request_transcoders = self.query.get("transcoder", ["mencoder"])
19.286 -
19.287 - for t in request_transcoders:
19.288 - transcoder = self.transcoders.get(t)
19.289 - if transcoder:
19.290 - return transcoder
19.291 -
19.292 - if not transcoder:
19.293 - return self.transcoders[self.def_transcoder]
19.294 - # _get_transcoder()
19.295 -
19.296 -
19.297 - def serve_stream(self, body):
19.298 - transcoder = self._get_transcoder()
19.299 - try:
19.300 - obj = transcoder(self.query)
19.301 - except Exception, e:
19.302 - self.send_error(500, str(e))
19.303 - return
19.304 -
19.305 - self.send_response(200)
19.306 - self.send_header("Content-Type", obj.get_mimetype())
19.307 - self.send_header('Connection', 'close')
19.308 - self.end_headers()
19.309 -
19.310 - if body:
19.311 - self.server.add_transcoders(self, obj)
19.312 - obj.start(self.wfile)
19.313 - self.server.del_transcoders(self, obj)
19.314 - # serve_stream()
19.315 -
19.316 -
19.317 - def log_request(self, code='-', size='-'):
19.318 - self.log.info('"%s" %s %s', self.requestline, str(code), str(size))
19.319 - # log_request()
19.320 -
19.321 -
19.322 - def log_error(self, format, *args):
19.323 - self.log.error("%s: %s" % (self.address_string(), format % args))
19.324 - # log_error()
19.325 -
19.326 -
19.327 - def log_message(self, format, *args):
19.328 - self.log.info("%s: %s" % (self.address_string(), format % args))
19.329 - # log_message()
19.330 -# RequestHandler
19.331 -
19.332 -
19.333 -
19.334 -class Server(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
19.335 - log = log.getLogger("gms.server")
19.336 - run = True
19.337 - _transcoders = {}
19.338 - _lock = threading.RLock()
19.339 -
19.340 - def serve_forever(self):
19.341 - self.log.info("GMyth-Streamer serving HTTP on %s:%s" %
19.342 - self.socket.getsockname())
19.343 - try:
19.344 - while self.run:
19.345 - self.handle_request()
19.346 - except KeyboardInterrupt, e:
19.347 - pass
19.348 -
19.349 - self.log.debug("Stopping all remaining transcoders...")
19.350 - self.stop_transcoders()
19.351 - self.log.debug("Transcoders stopped!")
19.352 - # serve_forever()
19.353 -
19.354 -
19.355 - def get_request(self):
19.356 - skt = self.socket
19.357 - old = skt.gettimeout()
19.358 - skt.settimeout(0.5)
19.359 - while self.run:
19.360 - try:
19.361 - r = skt.accept()
19.362 - skt.settimeout(old)
19.363 - return r
19.364 - except socket.timeout, e:
19.365 - pass
19.366 - raise socket.error("Not running")
19.367 - # get_request()
19.368 -
19.369 -
19.370 - def server_close(self):
19.371 - self.run = False
19.372 - self.stop_transcoders()
19.373 -
19.374 - BaseHTTPServer.HTTPServer.server_close(self)
19.375 - # server_close()
19.376 -
19.377 -
19.378 - def stop_transcoders(self):
19.379 - self._lock.acquire()
19.380 - for transcoder, request in self._transcoders.iteritems():
19.381 - self.log.info("Stop transcoder: %s, client=%s" %
19.382 - (transcoder, request.client_address))
19.383 - transcoder.stop()
19.384 - self._lock.release()
19.385 - # stop_transcoders()
19.386 -
19.387 -
19.388 - def get_transcoders(self):
19.389 - self._lock.acquire()
19.390 - try:
19.391 - return self._transcoders.items()
19.392 - finally:
19.393 - self._lock.release()
19.394 - # get_transcoders()
19.395 -
19.396 -
19.397 - def add_transcoders(self, request, transcoder):
19.398 - self._lock.acquire()
19.399 - try:
19.400 - self._transcoders[transcoder] = request
19.401 - finally:
19.402 - self._lock.release()
19.403 - # add_transcoders()
19.404 -
19.405 -
19.406 - def del_transcoders(self, request, transcoder):
19.407 - self._lock.acquire()
19.408 - try:
19.409 - del self._transcoders[transcoder]
19.410 - finally:
19.411 - self._lock.release()
19.412 - # del_transcoders()
19.413 -# Server
19.414 -
19.415 -
19.416 -
19.417 -def serve_forever(host="0.0.0.0", port=40000):
19.418 - addr = (host, port)
19.419 - RequestHandler.protocol_version = "HTTP/1.0"
19.420 - httpd = Server(addr, RequestHandler)
19.421 - httpd.serve_forever()
19.422 -# serve_forever()
19.423 -
19.424 -
19.425 -def load_plugins_transcoders(directory):
19.426 - RequestHandler.load_plugins_transcoders(directory)
19.427 -# load_plugins_transcoders()
20.1 --- a/gmyth-stream/server/0.2/lib/utils.py Tue Aug 28 15:41:35 2007 +0100
20.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
20.3 @@ -1,178 +0,0 @@
20.4 -#!/usr/bin/env
20.5 -
20.6 -__author__ = "Gustavo Sverzut Barbieri / Artur Duque de Souza"
20.7 -__author_email__ = "barbieri@gmail.com / artur.souza@indt.org.br"
20.8 -__license__ = "GPL"
20.9 -__version__ = "0.3"
20.10 -
20.11 -import os
20.12 -import stat
20.13 -import sys
20.14 -import logging
20.15 -import urllib
20.16 -import gobject
20.17 -import imp
20.18 -
20.19 -log = logging.getLogger("gms.utils")
20.20 -
20.21 -__all__ = ("which", "load_plugins", "PluginSet", "getHTML")
20.22 -
20.23 -def which(app):
20.24 - """Function to implement which(1) unix command"""
20.25 - pl = os.environ["PATH"].split(os.pathsep)
20.26 - for p in pl:
20.27 - path = os.path.join(p, app)
20.28 - if os.path.isfile(path):
20.29 - st = os.stat(path)
20.30 - if st[stat.ST_MODE] & 0111:
20.31 - return path
20.32 - return ""
20.33 -# which()
20.34 -
20.35 -
20.36 -def _load_module(pathlist, name):
20.37 - fp, path, desc = imp.find_module(name, pathlist)
20.38 - try:
20.39 - module = imp.load_module(name, fp, path, desc)
20.40 - return module
20.41 - finally:
20.42 - if fp:
20.43 - fp.close()
20.44 -# _load_module()
20.45 -
20.46 -
20.47 -class PluginSet(object):
20.48 - def __init__(self, basetype, *items):
20.49 - self.basetype = basetype
20.50 - self.map = {}
20.51 - self.list = []
20.52 -
20.53 - for i in items:
20.54 - self._add(i)
20.55 - self._sort()
20.56 - # __init__()
20.57 -
20.58 -
20.59 - def _add(self, item):
20.60 - self.map[item.name] = item
20.61 - self.list.append(item)
20.62 - # _add()
20.63 -
20.64 -
20.65 - def add(self, item):
20.66 - self._add()
20.67 - self._sort()
20.68 - # add()
20.69 -
20.70 -
20.71 - def __getitem__(self, spec):
20.72 - if isinstance(spec, basestring):
20.73 - return self.map[spec]
20.74 - else:
20.75 - return self.list[spec]
20.76 - # __getitem__()
20.77 -
20.78 -
20.79 - def get(self, name, default=None):
20.80 - return self.map.get(name, default)
20.81 - # get()
20.82 -
20.83 -
20.84 - def __iter__(self):
20.85 - return self.list.__iter__()
20.86 - # __iter__()
20.87 -
20.88 -
20.89 - def __len__(self):
20.90 - return len(self.list)
20.91 - # __len__()
20.92 -
20.93 -
20.94 - def _sort(self):
20.95 - self.list.sort(lambda a, b: cmp(a.priority, b.priority))
20.96 - # _sort()
20.97 -
20.98 -
20.99 - def update(self, pluginset):
20.100 - self.map.update(pluginset.map)
20.101 - self.list.extend(pluginset.list)
20.102 - self._sort()
20.103 - # update()
20.104 -
20.105 -
20.106 - def load_from_directory(self, directory):
20.107 - for i in load_plugins(directory, self.basetype):
20.108 - self._add(i)
20.109 - self._sort()
20.110 - # load_from_directory()
20.111 -
20.112 -
20.113 - def __str__(self):
20.114 - lst = []
20.115 - for o in self.list:
20.116 - lst.append('"%s" (%s)' % (o.name, o.__name__))
20.117 -
20.118 - return "%s(basetype=%s, items=[%s])" % \
20.119 - (self.__class__.__name__,
20.120 - self.basetype.__name__,
20.121 - ", ".join(lst))
20.122 - # __str__()
20.123 -# PluginSet
20.124 -
20.125 -
20.126 -def load_plugins(directory, basetype):
20.127 - """Function to load plugins from a given directory"""
20.128 - tn = basetype.__name__
20.129 - log.debug("Loading plugins from %s, type=%s" % (directory, tn))
20.130 -
20.131 -
20.132 - plugins = []
20.133 - for d in os.listdir(directory):
20.134 - if not d.endswith(".py"):
20.135 - continue
20.136 -
20.137 - name = d[0: -3]
20.138 - if name == "__init__":
20.139 - continue
20.140 -
20.141 - directory.replace(os.path.sep, ".")
20.142 - mod = _load_module([directory], name)
20.143 - for sym in dir(mod):
20.144 - cls = getattr(mod, sym)
20.145 - if isinstance(cls, type) and issubclass(cls, basetype) and \
20.146 - cls != basetype:
20.147 - plugins.append(cls)
20.148 - log.info("Loaded %s (%s) from %s" % \
20.149 - (cls.__name__, tn, os.path.join(directory, d)))
20.150 -
20.151 - return plugins
20.152 -# load_plugins()
20.153 -
20.154 -def getHTML(html_file, params={}):
20.155 - """This function parses a file 'html_file.html' with the given
20.156 - parameters and returns a formated web-page"""
20.157 - try:
20.158 - filename = os.path.join(sys.path[0], "html", html_file + ".html")
20.159 - html = open(filename).read() % params
20.160 - return html
20.161 - except Exception, e:
20.162 - return "HTML format error. Wrong keys: %s" % e
20.163 -
20.164 -# getHTML
20.165 -
20.166 -def _create_html_item(opt):
20.167 - return "<li>%s</li>\n" % opt
20.168 -# _create_html_item
20.169 -
20.170 -def progress_bar(log, value, max, barsize):
20.171 - chars = int(value * barsize / float(max))
20.172 - percent = int((value / float(max)) * 100)
20.173 - sys.stdout.write("#" * chars)
20.174 - sys.stdout.write(" " * (barsize - chars + 2))
20.175 - if value >= max:
20.176 - sys.stdout.write("done. \n\n")
20.177 - else:
20.178 - sys.stdout.write("[%3i%%]\r" % (percent))
20.179 - sys.stdout.flush()
20.180 - return percent
20.181 -# progress_bar by osantana
21.1 --- a/gmyth-stream/server/0.2/plugins/transcoders/gmencoder.py Tue Aug 28 15:41:35 2007 +0100
21.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
21.3 @@ -1,91 +0,0 @@
21.4 -import os
21.5 -import shlex
21.6 -import signal
21.7 -import subprocess
21.8 -import time
21.9 -
21.10 -import select
21.11 -
21.12 -import lib.utils as utils
21.13 -import lib.server as server
21.14 -
21.15 -__all__ = ("TranscoderGMencoder",)
21.16 -
21.17 -class TranscoderGMencoder(server.Transcoder):
21.18 - gmencoder_path = utils.which("gmencoder")
21.19 - name = "gmencoder"
21.20 - priority = -1
21.21 - proc = None
21.22 -
21.23 - def __init__(self, params):
21.24 - server.Transcoder.__init__(self, params)
21.25 - self.opts = []
21.26 - # __init__()
21.27 -
21.28 - def _insert_param (self, name, value):
21.29 - if (value != ""):
21.30 - self.opts.append(name)
21.31 - self.opts.append(value)
21.32 -
21.33 - def _parser_params (self):
21.34 - self._insert_param("-i", \
21.35 - "%s://%s" % (self.params_first("type", "file"), self.params_first("uri", "")))
21.36 - self._insert_param("--video-encode", self.params_first("ve", ""))
21.37 - self._insert_param("--video-opts", "bitrate=200,pass=2,quantizer=5")
21.38 - self._insert_param("--video-fps", self.params_first("fps", ""))
21.39 - self._insert_param("--video-width", self.params_first("width", ""))
21.40 - self._insert_param("--video-height", self.params_first("height", ""))
21.41 - self._insert_param("--audio-rate", "32000")
21.42 - self._insert_param("--audio-encode", self.params_first("ae", ""))
21.43 - # _parse_params
21.44 -
21.45 - def start(self, outfd):
21.46 - self.opts.append (self.gmencoder_path)
21.47 - self._parser_params ()
21.48 - self._insert_param ("-o", "fd://%d" % outfd.fileno())
21.49 -
21.50 - cmd = " ".join(self.opts)
21.51 - self.log.info ("GMencoder: %s", cmd)
21.52 -
21.53 - try:
21.54 - self.proc = subprocess.Popen(self.opts, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
21.55 - except Exception, e:
21.56 - self.log.error("Error executing GMencoder: %s" % e)
21.57 - return False
21.58 -
21.59 - try:
21.60 - while (self.proc and self.proc.poll() == None):
21.61 - r, w, x = select.select([self.proc.stdout], [], [], 0)
21.62 - if self.proc.stdout in r:
21.63 - progress = self.proc.stdout.readline()
21.64 - self.log.info ("stdout %s" % progress)
21.65 - if (progress.find ("PROGRESS") >= 0):
21.66 - self.status = progress.split (":")[1]
21.67 - #if (progress.find ("DONE") >= 0):
21.68 - # break
21.69 - self.log.info ("Process exit")
21.70 - except Exception, e:
21.71 - self.log.error("Problems handling data: %s" % e)
21.72 - return False
21.73 -
21.74 - return True
21.75 - # start()
21.76 -
21.77 -
21.78 - def stop(self):
21.79 - if self.proc:
21.80 - self.log.info ("STOPED GMencoder plugin")
21.81 - try:
21.82 - self.proc.stdin.write ("QUIT\n")
21.83 - except Exception, e:
21.84 - pass
21.85 -
21.86 - try:
21.87 - self.proc.wait()
21.88 - except Exception, e:
21.89 - pass
21.90 -
21.91 - self.proc = None
21.92 - # stop()
21.93 -
21.94 -# TranscoderGMencoder
22.1 --- a/gmyth-stream/server/0.2/plugins/transcoders/mencoder.py Tue Aug 28 15:41:35 2007 +0100
22.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
22.3 @@ -1,293 +0,0 @@
22.4 -import os
22.5 -import shlex
22.6 -import signal
22.7 -import subprocess
22.8 -import time
22.9 -import fcntl
22.10 -
22.11 -import lib.utils as utils
22.12 -import lib.server as server
22.13 -import plugins.transcoders.mencoder_lib.mythtv as mythtv
22.14 -
22.15 -from select import select
22.16 -
22.17 -__all__ = ("TranscoderMencoder",)
22.18 -
22.19 -class TranscoderMencoder(server.Transcoder):
22.20 - """Transcoder class that implements a transcoder using Mencoder"""
22.21 - mencoder_path = utils.which("mencoder")
22.22 - name = "mencoder"
22.23 - priority = -1
22.24 - args = {}
22.25 - proc = None
22.26 - gmyth = None
22.27 -
22.28 - # only works with avi container
22.29 - status = 0
22.30 -
22.31 - def _setup_params(self):
22.32 - params_first = self.params_first
22.33 -
22.34 - # general_opts
22.35 - self.args["local"] = params_first("local", False)
22.36 - self.args["language"] = params_first("language", False)
22.37 - self.args["subtitle"] = params_first("subtitle", False)
22.38 - self.args["format"] = params_first("format", "mpeg1")
22.39 - self.args["outfile"] = params_first("outfile", "-")
22.40 -
22.41 - # input_opt
22.42 - self.args["type"] = params_first("type", "file")
22.43 - self.args["input"] = params_first("uri", "-")
22.44 -
22.45 - # audio_opts
22.46 - self.args["acodec"] = params_first("acodec", "mp2")
22.47 - self.args["abitrate"] = params_first("abitrate", 192)
22.48 - self.args["volume"] = params_first("volume", 5)
22.49 -
22.50 - # video_opts
22.51 - self.args["mux"] = params_first("mux", "mpeg")
22.52 - self.args["fps"] = params_first("fps", 25)
22.53 - self.args["vcodec"] = params_first("vcodec", "mpeg1video")
22.54 - self.args["vbitrate"] = params_first("vbitrate", 400)
22.55 - self.args["width"] = params_first("width", 320)
22.56 - self.args["height"] = params_first("height", 240)
22.57 - # _setup_params()
22.58 -
22.59 -
22.60 - def _setup_audio(self):
22.61 - if self.args["acodec"] == "mp3lame":
22.62 - audio = "-oac mp3lame -lameopts cbr:br=%s vol=%s" % (
22.63 - self.args["abitrate"], self.args["volume"])
22.64 - else:
22.65 - audio = "-oac lavc -lavcopts acodec=%s:abitrate=%s" % (
22.66 - self.args["acodec"], self.args["abitrate"])
22.67 -
22.68 - return audio
22.69 - # _setup_audio()
22.70 -
22.71 -
22.72 - def _setup_video(self):
22.73 - video = " -of %s" % self.args["mux"]
22.74 - video += " -ofps %s" % self.args["fps"]
22.75 -
22.76 - vcodec = self.args["vcodec"]
22.77 - if vcodec == "nuv" or vcodec == "xvid"\
22.78 - or vcodec == "qtvideo" or vcodec == "copy":
22.79 - video += " -ovc %s" % vcodec
22.80 - else:
22.81 - video += " -ovc lavc -lavcopts vcodec=%s:vbitrate=%s" % (
22.82 - vcodec, self.args["vbitrate"])
22.83 -
22.84 - if self.args["mux"] == "mpeg":
22.85 - video += " -mpegopts format=%s" % self.args["format"]
22.86 - video += " -vf scale=%s:%s" % (self.args["width"], self.args["height"])
22.87 -
22.88 - return video
22.89 - # _setup_video()
22.90 -
22.91 -
22.92 - def _arg_append(self, args, options):
22.93 - for arg in shlex.split(options):
22.94 - args.append(arg)
22.95 - # arg_append()
22.96 -
22.97 - def _setup_mencoder_opts(self, args):
22.98 - args.append(self.mencoder_path)
22.99 -
22.100 - if self.args["outfile"] == "-" and self.args["type"]:
22.101 - args.append(self.args["input"])
22.102 - else:
22.103 - args.append("-")
22.104 -
22.105 - if self.args["language"]:
22.106 - self._arg_append(args, "-alang %s" % self.args["language"])
22.107 -
22.108 - if self.args["subtitle"]:
22.109 - self._arg_append(args, "-slang %s" % self.args["subtitle"])
22.110 - self._arg_append(args, "-subfps %s" % self.args["fps"])
22.111 -
22.112 - self._arg_append(args, "-idx")
22.113 - self._arg_append(args, "-cache 1024")
22.114 - self._arg_append(args, self._setup_audio())
22.115 - self._arg_append(args, self._setup_video())
22.116 -
22.117 - self._arg_append(args, "-really-quiet")
22.118 - self._arg_append(args, "-o %s" % self.args["outfile"])
22.119 - self._arg_append(args, "2>%s" % os.devnull)
22.120 - # _setup_args()
22.121 -
22.122 - def _setup_filename(self):
22.123 - """This function setups the file to encode parsing the uri.
22.124 - So, type can be:
22.125 - * file
22.126 - * dvd
22.127 - * myth
22.128 -
22.129 - If the last one is detected we have to parse the uri to find args.
22.130 - Then we store all the args inside a dictionary: self.args['gmyth-cat']
22.131 - """
22.132 - _type = self.args["type"]
22.133 -
22.134 - if _type == "file":
22.135 - if not os.path.exists(self.args["input"]):
22.136 - raise IOError,\
22.137 - "File requested does not exist: %s." % self.args["input"]
22.138 - else:
22.139 - self.args["input"] = "file://%s" % self.args["input"]
22.140 -
22.141 - elif _type == "dvd":
22.142 - self.args["input"] = "dvd://".join(self.args["input"])
22.143 -
22.144 - elif _type == "myth":
22.145 - self.args["gmyth-cat"] = mythtv._setup_mythfilename(self)
22.146 - # _setup_filename()
22.147 -
22.148 -
22.149 - def __init__(self, params):
22.150 - server.Transcoder.__init__(self, params)
22.151 - self.mencoder_opts = []
22.152 -
22.153 - try:
22.154 - self._setup_params()
22.155 - self._setup_filename()
22.156 - self._setup_mencoder_opts(self.mencoder_opts)
22.157 - except Exception, e:
22.158 - self.log.error(e)
22.159 - # __init__()
22.160 -
22.161 -
22.162 - def _check_opened_file(self, stdw, _stdin):
22.163 - loop = True
22.164 - while loop:
22.165 - try:
22.166 - return open(self.args["outfile"])
22.167 - except:
22.168 - os.write(stdw, _stdin.read(1024))
22.169 - # _check_opened_file
22.170 -
22.171 -
22.172 - def _start_outfile(self, outfd):
22.173 - finished = False
22.174 -
22.175 - # fix this (not necessary)
22.176 - outfd.write("OK")
22.177 -
22.178 - # Configuring stdin
22.179 - try:
22.180 - _stdin = open(self.args["input"])
22.181 - size = int(os.path.getsize(self.args["input"]))
22.182 - except Exception, e:
22.183 - self.log.error("Mencoder stdin setup error: %s" % e)
22.184 - return False
22.185 -
22.186 - self.status = 0
22.187 - total_read = 0
22.188 -
22.189 - # Configuring pipes
22.190 - stdr, stdw = os.pipe()
22.191 -
22.192 - if not self._run_mencoder(input=stdr):
22.193 - return False
22.194 -
22.195 - stdout = self._check_opened_file(stdw, _stdin)
22.196 -
22.197 - try:
22.198 - while self.proc and self.proc.poll() == None:
22.199 - if not finished:
22.200 - data_in = _stdin.read(4096)
22.201 - if data_in != "":
22.202 - os.write(stdw, data_in)
22.203 - total_read += 4096
22.204 - d = stdout.read(4096)
22.205 - self.status = utils.progress_bar(self.log,
22.206 - int(total_read),
22.207 - int(size), 50)
22.208 - else:
22.209 - finished = True
22.210 - os.close(stdw)
22.211 -
22.212 - else:
22.213 - d = stdout.read(4096)
22.214 -
22.215 - except Exception, e:
22.216 - self.log.error("Problems handling data: %s" % e)
22.217 - self.stop()
22.218 - return False
22.219 -
22.220 - self.log.info("%s: Finished sending data to client" % repr(self))
22.221 - return True
22.222 - # _start_outfile()
22.223 -
22.224 - def _start(self, outfd):
22.225 - # Play a file on disk or DVD
22.226 - if not self._run_mencoder(output=subprocess.PIPE):
22.227 - return False
22.228 -
22.229 - try:
22.230 - while self.proc and self.proc.poll() == None:
22.231 - d = self.proc.stdout.read(1024)
22.232 - outfd.write(d)
22.233 - except Exception, e:
22.234 - self.log.error("Problems handling data: %s" % e)
22.235 - return False
22.236 -
22.237 - self.log.info("%s: Finished sending data to client" % repr(self))
22.238 - return True
22.239 - # _start()
22.240 -
22.241 - def _run_mencoder(self, input=None, output=None):
22.242 - try:
22.243 - self.proc = subprocess.Popen(self.mencoder_opts, stdin=input,
22.244 - stdout=output, close_fds=True)
22.245 - except Exception, e:
22.246 - self.log.error("Error executing mencoder: %s" % e)
22.247 - return False
22.248 -
22.249 - return True
22.250 - # _run_mencoder()
22.251 -
22.252 - def start(self, outfd):
22.253 - cmd = " ".join(self.mencoder_opts)
22.254 - self.log.debug("Mencoder: %s" % cmd)
22.255 -
22.256 - ret = False
22.257 -
22.258 - if self.args["outfile"] == "-" and \
22.259 - self.args["type"] in ["file", "dvd"]:
22.260 - ret = self._start(outfd)
22.261 -
22.262 - elif self.args["type"] == "myth":
22.263 - ret = mythtv.start_myth(self, outfd)
22.264 -
22.265 - else:
22.266 - ret = self._start_outfile(outfd)
22.267 -
22.268 - self.stop()
22.269 -
22.270 - if not ret:
22.271 - self.log.error("Problems while starting streaming.")
22.272 -
22.273 - return ret
22.274 - # start()
22.275 -
22.276 - def _aux_stop(self, obj):
22.277 - if obj:
22.278 - try:
22.279 - os.kill(obj.pid, signal.SIGKILL)
22.280 - except OSError, e:
22.281 - pass
22.282 -
22.283 - try:
22.284 - obj.wait()
22.285 - except Exception, e:
22.286 - pass
22.287 -
22.288 - obj = None
22.289 - # _aux_stop
22.290 -
22.291 - def stop(self):
22.292 - self._aux_stop(self.proc)
22.293 - self._aux_stop(self.gmyth)
22.294 - # stop()
22.295 -
22.296 -# TranscoderMencoder
23.1 --- a/gmyth-stream/server/0.2/plugins/transcoders/mencoder_lib/mythtv.py Tue Aug 28 15:41:35 2007 +0100
23.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
23.3 @@ -1,106 +0,0 @@
23.4 -import os
23.5 -import subprocess
23.6 -import fcntl
23.7 -
23.8 -import lib.utils as utils
23.9 -import lib.server as server
23.10 -
23.11 -from select import select
23.12 -
23.13 -def _setup_mythfilename(self):
23.14 - # mythtv:mythtv@192.168.3.110:6543/1002_20070426230000.nuv
23.15 - try:
23.16 - _mysql = self.args["input"].split("@")[0].split(":")
23.17 - except IndexError, e:
23.18 - _mysql = ["mythtv", "mythtv"]
23.19 -
23.20 - try:
23.21 - _args = self.args["input"].split("@")[1].split(":")
23.22 - except IndexError, e:
23.23 - _args = self.args["input"].split(":")
23.24 -
23.25 - gmyth_dict = {}
23.26 - gmyth_dict["mysql"] = _mysql
23.27 - gmyth_dict["backend"] = _args[0]
23.28 - gmyth_dict["port"] = _args[1].split("/", 1)[0]
23.29 -
23.30 - _tmp_file = _args[1].split("/", 1)[1]
23.31 -
23.32 - if _tmp_file.find("channel") >= 0:
23.33 - gmyth_dict["kind"] = "c"
23.34 - gmyth_dict["cfile"] = _tmp_file.split("=")[1]
23.35 - else:
23.36 - gmyth_dict["kind"] = "f"
23.37 - gmyth_dict["cfile"] = _tmp_file
23.38 -
23.39 - self.args["input"] = "-"
23.40 - return gmyth_dict
23.41 -# _setup_mythfilename
23.42 -
23.43 -def _setup_mythfile(err):
23.44 - size = err.readline().split("Size:")[1]
23.45 - flags = fcntl.fcntl (err, fcntl.F_GETFL, 0) | os.O_NONBLOCK
23.46 - fcntl.fcntl(err, fcntl.F_SETFL, flags)
23.47 - return size
23.48 -# _setup_mythfile
23.49 -
23.50 -def _setup_gmythcat(self):
23.51 - gmyth_cat = utils.which("gmyth-cat")
23.52 - if self.args.has_key("gmyth-cat"):
23.53 - return [ utils.which("gmyth-cat"),
23.54 - "-h", self.args["gmyth-cat"]["backend"],
23.55 - "-p", self.args["gmyth-cat"]["port"],
23.56 - "-" + self.args["gmyth-cat"]["kind"],
23.57 - self.args["gmyth-cat"]["cfile"]
23.58 - ]
23.59 - else:
23.60 - self.log.error("URI error")
23.61 - return []
23.62 -# _setup_gmythcat
23.63 -
23.64 -def start_myth(self, outfd):
23.65 - opts = _setup_gmythcat(self)
23.66 - try:
23.67 - self.gmyth = subprocess.Popen(opts, stdout=subprocess.PIPE,
23.68 - stderr=subprocess.PIPE,
23.69 - close_fds=True)
23.70 - except Exception, e:
23.71 - self.log.error("Error executing gmyth-cat: %s" % e)
23.72 - return False
23.73 -
23.74 - if not self._run_mencoder(input=self.gmyth.stdout,
23.75 - output=subprocess.PIPE):
23.76 - return False
23.77 -
23.78 - if self.args["gmyth-cat"]["kind"] == "f":
23.79 - try:
23.80 - size = _setup_mythfile(self.gmyth.stderr)
23.81 - self.log.debug("Size of file: %s" % size)
23.82 - except Exception, e:
23.83 - self.log.error("Problems getting size of file: %s" % e)
23.84 - return False
23.85 -
23.86 - try:
23.87 - while self.proc and self.proc.poll() == None:
23.88 - r, w, x = select([self.gmyth.stderr, self.proc.stdout],
23.89 - [], [], 0)
23.90 - if self.proc.stdout in r:
23.91 - d = self.proc.stdout.read(4096)
23.92 - outfd.write(d)
23.93 -
23.94 - if self.gmyth.stderr in r:
23.95 - partial = self.gmyth.stderr.read(50).split("\n")[-2]
23.96 - if partial != "":
23.97 - self.status = utils.progress_bar(self.log,
23.98 - int(partial),
23.99 - int(size), 50)
23.100 -
23.101 - except IndexError, e:
23.102 - pass
23.103 - except Exception, e:
23.104 - self.log.error("Problems handling data: %s" % e)
23.105 - return False
23.106 -
23.107 - self.log.info("Finished sending data")
23.108 - return True
23.109 -# _start_myth()