import telnetlib
import logging
from vlcmessages import *
-
+import time
+from pprint import pprint
class VlcException(Exception):
# 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))
# 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()
try:
# Write message
- logger.debug('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))
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:
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='', qtype='default'):
logger = logging.getLogger("VlcClient_startBroadcast")
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():
+ print stream,lasttime
+ if time.time()-lasttime>timeout:
+ to_stop.add(stream)
+ for stream in to_stop:
+ self.stopBroadcast(stream)
+ self._resultlock.release()
+
def pauseBroadcast(self, stream_name):
return self._write(VlcMessage.request.pauseBroadcast(stream_name))
# 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)