diff --git a/modules/communication/liquidsoap/initthread.py b/modules/communication/liquidsoap/initthread.py index 1079fd5271ce65e00da2de35df4d4faa376c9d4c..20b491964ef9234df420cae1b8af04b68324d737 100644 --- a/modules/communication/liquidsoap/initthread.py +++ b/modules/communication/liquidsoap/initthread.py @@ -29,67 +29,92 @@ import threading from libraries.enum.auraenumerations import ScheduleEntryType +""" +LiquidSoapInitThread class. + +Starts the LiquidSoap player including the current show. +""" class LiquidSoapInitThread(threading.Thread): + logger = None active_entry = None liquidsoapcommunicator = None - # ------------------------------------------------------------------------------------------ # + def __init__(self, liquidsoapcommunicator, active_entry): + """ + Initialize the thread. + """ threading.Thread.__init__(self) self.logger = logging.getLogger("AuraEngine") self.liquidsoapcommunicator = liquidsoapcommunicator self.active_entry = active_entry - # ------------------------------------------------------------------------------------------ # + + def run(self): + """ + Starts the LiquidSoap player including the current show. + """ try: - # sleep needed, because the socket is created too slow by liquidsoap + # Sleep needed, because the socket is created too slow by liquidsoap time.sleep(1) self.logger.info("Waited 1s for liquidsoap. Jez soit a si gspian") - # enable lqs transaction self.liquidsoapcommunicator.enable_transaction() - # wait another second. lqs really starts slow.. be prepared you liquidsoap you! + # Wait another second. lqs really starts slow.. be prepared you liquidsoap you! time.sleep(1) - - # set some parameters self.set_start_parameters() - - # set active self.set_active_show() - # disable lqs transaction again self.liquidsoapcommunicator.disable_transaction() - - # the rest of the system now can use liquidsoap connection + # The rest of the system now can use liquidsoap connection self.liquidsoapcommunicator.is_liquidsoap_running = True + except Exception as e: - self.logger.critical("Liquidsoap connection ERROR! Restart LQ Server! Reason: "+str(e)) + self.logger.critical("Liquidsoap connection ERROR! Restart LQ Server! Reason: "+str(e), e) + + def set_active_show(self): + """ + Sets and resumes the show which should be playing at the time of starting + the LiquidSoap player. + """ if self.active_entry is not None: - self.logger.info("LiquidSoapInitThread sets activechannel: " + str(self.active_entry)) - channel = self.active_entry.type - - # have to seek? + self.logger.info("LiquidSoapInitThread sets activechannel '%s' for entry '%s'" % (channel, str(self.active_entry))) + if channel == ScheduleEntryType.FILESYSTEM: - # calc how many seconds were missed + + # Have to seek? Calc how many seconds were missed now_unix = time.mktime(datetime.datetime.now().timetuple()) seconds_to_seek = now_unix - self.active_entry.start_unix - # and seek these seconds forward + # TODO For some reason LiquidSoap cue-points don't work. Actually, + # this would be the ideal approach to seek. Investigate some solution! + # if seconds_to_seek < 0: + # seconds_to_seek = 0 + # self.liquidsoapcommunicator.activate(self.active_entry, seconds_to_seek) + + self.liquidsoapcommunicator.activate(self.active_entry, seconds_to_seek) + + # And seek these seconds forward if seconds_to_seek > 0: - self.liquidsoapcommunicator.playlist_seek(seconds_to_seek) + # Without plenty of timeout (10s) the seek doesn't work + seconds_to_seek += 10 + time.sleep(10) + response = self.liquidsoapcommunicator.playlist_seek(seconds_to_seek) + self.logger.info("LiquidSoap seek response: " + response) + - # finally make something hearable :-) - if channel != "" and channel is not None: - # activate http stream if needed + # Finally make something hearable :-) + if channel: + # Activate HTTP stream if needed self.liquidsoapcommunicator.http_start_stop(channel == ScheduleEntryType.STREAM) - # finally set the volume up + # Finally set the volume up self.liquidsoapcommunicator.channel_volume(channel.value, self.active_entry.volume) else: self.logger.error("Channel is NULL or empty! Cannot set ") @@ -97,18 +122,23 @@ class LiquidSoapInitThread(threading.Thread): else: self.logger.warning("No active entry in the scheduler! Is a programme loaded?") + + def set_start_parameters(self): - # reset channels and reload them + """ + Set initial parameters for the LiquidSoap player startup. + """ + # Reset channels and reload them channels = self.liquidsoapcommunicator.reload_channels() - # for all available channels + # For all available channels for c in channels: - # set volume to zero + # Set volume to zero self.liquidsoapcommunicator.channel_volume(c, "0") - # and activate this channel + # And activate this channel self.liquidsoapcommunicator.channel_activate(c, True) - # setting init params like a blank file.. + # Setting init params like a blank file.. install_dir = self.liquidsoapcommunicator.config.get("install_dir") self.liquidsoapcommunicator.playlist_push(install_dir + "/configuration/blank.flac") # .. or the radio fro stream (it is overwritten as soon as one http overtake is planned)