X-Git-Url: https://git.rvb.name/vpproxy.git/blobdiff_plain/1e2c0e679beb33f10c2e551768c17ccd77576b54..db69bdf8a7d5d345009f548b72c4a2ae40c565cc:/vlcclient/vlcclient.py diff --git a/vlcclient/vlcclient.py b/vlcclient/vlcclient.py index ebe83f7..9492156 100644 --- a/vlcclient/vlcclient.py +++ b/vlcclient/vlcclient.py @@ -8,7 +8,7 @@ import gevent.coros import telnetlib import logging from vlcmessages import * - +import time class VlcException(Exception): @@ -25,8 +25,8 @@ class VlcClient(object): ''' def __init__( - self, host='127.0.0.1', port=4212, password='admin', connect_timeout=5, - result_timeout=5, out_port=8081): + self, host='127.0.0.1', port=4212, password='admin', connect_timeout=10, + result_timeout=10, out_port=8081): # Receive buffer self._recvbuffer = None # Output port @@ -51,10 +51,13 @@ class VlcClient(object): # Logger logger = logging.getLogger('VlcClient_init') + # Streams + self.streams=dict() + # Making connection try: self._socket = telnetlib.Telnet(host, port, connect_timeout) - logger.debug("Successfully connected with VLC socket!") + logger.info("Successfully connected with VLC socket!") except Exception as e: raise VlcException( "Socket creation error! VLC is not running? ERROR: " + repr(e)) @@ -89,7 +92,7 @@ class VlcClient(object): # If socket is still alive (connected) if self._socket: try: - logger.debug("Destroying VlcClient...") + logger.info("Destroying VlcClient...") self._write(VlcMessage.request.SHUTDOWN) # Set shuttingDown flag for recvData self._shuttingDown.set() @@ -98,18 +101,21 @@ class VlcClient(object): pass def _write(self, message): + + logger = logging.getLogger("VlcClient_write") + # Return if in the middle of destroying if self._shuttingDown.isSet(): return try: # Write message - print 'VLC command:',message + logger.info('VLC command: ' + message) self._socket.write(message + "\r\n") except EOFError as e: raise VlcException("Vlc Write error! ERROR: " + repr(e)) - def _broadcast(self, brtype, stream_name, input=None, muxer='ts', pre_access=''): + def _broadcast(self, brtype, stream_name, input=None, muxer='ts', pre_access='', qtype='default'): if self._shuttingDown.isSet(): return @@ -127,9 +133,13 @@ class VlcClient(object): self._resultlock.acquire() # Write message to VLC socket if brtype == True: - self._write(VlcMessage.request.startBroadcast( - stream_name, input, self._out_port, muxer, pre_access)) + msg = VlcMessage.request.startBroadcast(stream_name, input, self._out_port, muxer, pre_access, qtype) + self._write(msg) else: + if stream_name not in self.streams: + self._resultlock.release() + logger.error("Attempting to delete not existing stream %s" % stream_name) + return self._write(VlcMessage.request.stopBroadcast(stream_name)) try: @@ -142,19 +152,50 @@ class VlcClient(object): logger.error(broadcast + " result timeout") raise VlcException(broadcast + " result timeout") finally: + logger.info("working with %s stream: %s" % (stream_name,broadcast)) + if brtype == True: + self.streams[stream_name]=time.time() + else: + del self.streams[stream_name] self._resultlock.release() + logger.info("worked with %s stream: %s" % (stream_name,broadcast)) if brtype == True: - logger.debug("Broadcast started") + logger.info("Broadcast started") else: - logger.debug("Broadcast stopped") + logger.info("Broadcast stopped") - def startBroadcast(self, stream_name, input, muxer='ts', pre_access=''): - return self._broadcast(True, stream_name, input, muxer, pre_access) + def startBroadcast(self, stream_name, input, muxer='ts', pre_access='', qtype='default'): + logger = logging.getLogger("VlcClient_startBroadcast") + logger.debug("Starting broadcast......") + return self._broadcast(True, stream_name, input, muxer, pre_access, qtype) def stopBroadcast(self, stream_name): return self._broadcast(False, stream_name) + def mark(self,stream_name): + self.streams[stream_name]=time.time() + + def clean_streams(self,timeout=15): + self._resultlock.acquire() + to_stop=set() + for stream,lasttime in self.streams.iteritems(): + if time.time()-lasttime>timeout: + to_stop.add(stream) + for stream in to_stop: + try: + self.stopBroadcast(stream) + except: + pass + self._resultlock.release() + + def check_stream(self,stream_name): + if stream_name in self.streams: + self.streams[stream_name]=time.time() + return True + else: + return False + def pauseBroadcast(self, stream_name): return self._write(VlcMessage.request.pauseBroadcast(stream_name)) @@ -215,10 +256,10 @@ class VlcClient(object): # Do not move this before error handlers! elif self._recvbuffer.startswith(VlcMessage.response.STARTOK): # Broadcast started - logger.debug("Broadcast started") + logger.info("Broadcast started") self._result.set(True) elif self._recvbuffer.startswith(VlcMessage.response.STOPOK): # Broadcast stopped - logger.debug("Broadcast stopped") + logger.info("Broadcast stopped") self._result.set(True)