diff --git a/python_apps/pypo/liquidsoap_scripts/ls_lib.liq b/python_apps/pypo/liquidsoap_scripts/ls_lib.liq index 9d48eebbb..9e7903751 100644 --- a/python_apps/pypo/liquidsoap_scripts/ls_lib.liq +++ b/python_apps/pypo/liquidsoap_scripts/ls_lib.liq @@ -402,6 +402,11 @@ def set_dynamic_source_id(id) = string_of(!current_dyn_id) end +def get_dynamic_source_id() = + string_of(!current_dyn_id) +end + + # Function to create a playlist source and output it. def create_dynamic_source(uri) = # The playlist source diff --git a/python_apps/pypo/liquidsoap_scripts/ls_script.liq b/python_apps/pypo/liquidsoap_scripts/ls_script.liq index 0d60cd9c3..7a6d8c61d 100644 --- a/python_apps/pypo/liquidsoap_scripts/ls_script.liq +++ b/python_apps/pypo/liquidsoap_scripts/ls_script.liq @@ -19,7 +19,7 @@ queue = amplify(1., override="replay_gain", queue) #live stream setup set("harbor.bind_addr", "0.0.0.0") -current_dyn_id = ref '' +current_dyn_id = ref '-1' pypo_data = ref '0' stream_metadata_type = ref 0 @@ -95,6 +95,13 @@ server.register(namespace="dynamic_source", usage="id ", "id", fun (s) -> begin log("dynamic_source.id") set_dynamic_source_id(s) end) + +server.register(namespace="dynamic_source", + description="Get the cc_schedule row id", + usage="get_id", + "get_id", + fun (s) -> begin log("dynamic_source.get_id") get_dynamic_source_id() end) + server.register(namespace="dynamic_source", description="Start a new dynamic source.", usage="start ", diff --git a/python_apps/pypo/pypopush.py b/python_apps/pypo/pypopush.py index ad28abef9..5bcce21b6 100644 --- a/python_apps/pypo/pypopush.py +++ b/python_apps/pypo/pypopush.py @@ -55,7 +55,6 @@ class PypoPush(Thread): self.pushed_objects = {} self.logger = logging.getLogger('push') - self.current_stream_info = None self.current_prebuffering_stream_id = None def main(self): @@ -78,6 +77,7 @@ class PypoPush(Thread): #We get to the following lines only if a schedule was received. liquidsoap_queue_approx = self.get_queue_items_from_liquidsoap() + liquidsoap_stream_id = self.get_current_stream_id_from_liquidsoap() tnow = datetime.utcnow() current_event_chain, original_chain = self.get_current_chain(chains, tnow) @@ -92,7 +92,7 @@ class PypoPush(Thread): #is scheduled. We need to verify whether the schedule we just received matches #what Liquidsoap is playing, and if not, correct it. - self.handle_new_schedule(media_schedule, liquidsoap_queue_approx, current_event_chain) + self.handle_new_schedule(media_schedule, liquidsoap_queue_approx, liquidsoap_stream_id, current_event_chain) #At this point everything in the present has been taken care of and Liquidsoap @@ -134,6 +134,25 @@ class PypoPush(Thread): loops = 0 loops += 1 + def get_current_stream_id_from_liquidsoap(self): + response = "-1" + try: + self.telnet_lock.acquire() + tn = telnetlib.Telnet(LS_HOST, LS_PORT) + + msg = 'dynamic_source.get_id\n' + tn.write(msg) + response = tn.read_until("\r\n").strip(" \r\n") + tn.write('exit\n') + tn.read_all() + except Exception, e: + self.logger.error("Error connecting to Liquidsoap: %s", e) + response = [] + finally: + self.telnet_lock.release() + + return response + def get_queue_items_from_liquidsoap(self): """ This function connects to Liquidsoap to find what media items are in its queue. @@ -175,10 +194,10 @@ class PypoPush(Thread): return liquidsoap_queue_approx - def is_correct_current_item(self, media_item, liquidsoap_queue_approx): + def is_correct_current_item(self, media_item, liquidsoap_queue_approx, liquidsoap_stream_id): correct = False if media_item is None: - correct = (len(liquidsoap_queue_approx) == 0 and self.current_stream_info is None) + correct = (len(liquidsoap_queue_approx) == 0 and liquidsoap_stream_id == "-1") else: if is_file(media_item): if len(liquidsoap_queue_approx) == 0: @@ -188,10 +207,7 @@ class PypoPush(Thread): liquidsoap_queue_approx[0]['row_id'] == media_item['row_id'] and \ liquidsoap_queue_approx[0]['end'] == media_item['end'] elif is_stream(media_item): - if self.current_stream_info is None: - correct = False - else: - correct = self.current_stream_info['row_id'] == media_item['row_id'] + correct = liquidsoap_stream_id == str(media_item['row_id']) self.logger.debug("Is current item correct?: %s", str(correct)) return correct @@ -202,7 +218,7 @@ class PypoPush(Thread): self.remove_from_liquidsoap_queue(0, None) self.stop_web_stream_all() - def handle_new_schedule(self, media_schedule, liquidsoap_queue_approx, current_event_chain): + def handle_new_schedule(self, media_schedule, liquidsoap_queue_approx, liquidsoap_stream_id, current_event_chain): """ This function's purpose is to gracefully handle situations where Liquidsoap already has a track in its queue, but the schedule @@ -213,14 +229,13 @@ class PypoPush(Thread): file_chain = filter(lambda item: (item["type"] == "file"), current_event_chain) stream_chain = filter(lambda item: (item["type"] == "stream_output_start"), current_event_chain) - self.logger.debug(self.current_stream_info) self.logger.debug(current_event_chain) #Take care of the case where the current playing may be incorrect if len(current_event_chain) > 0: current_item = current_event_chain[0] - if not self.is_correct_current_item(current_item, liquidsoap_queue_approx): + if not self.is_correct_current_item(current_item, liquidsoap_queue_approx, liquidsoap_stream_id): self.clear_all_liquidsoap_items() if is_stream(current_item): if current_item['row_id'] != self.current_prebuffering_stream_id: @@ -234,7 +249,7 @@ class PypoPush(Thread): #we've changed the queue, so let's refetch it liquidsoap_queue_approx = self.get_queue_items_from_liquidsoap() - elif not self.is_correct_current_item(None, liquidsoap_queue_approx): + elif not self.is_correct_current_item(None, liquidsoap_queue_approx, liquidsoap_stream_id): #Liquidsoap is playing something even though it shouldn't be self.clear_all_liquidsoap_items() @@ -490,7 +505,6 @@ class PypoPush(Thread): self.logger.debug(tn.read_all()) self.current_prebuffering_stream_id = None - self.current_stream_info = media_item except Exception, e: self.logger.error(str(e)) finally: @@ -509,10 +523,13 @@ class PypoPush(Thread): self.logger.debug(msg) tn.write(msg) + msg = 'dynamic_source.id -1\n' + self.logger.debug(msg) + tn.write(msg) + tn.write("exit\n") self.logger.debug(tn.read_all()) - self.current_stream_info = None except Exception, e: self.logger.error(str(e)) finally: @@ -528,10 +545,13 @@ class PypoPush(Thread): self.logger.debug(msg) tn.write(msg) + msg = 'dynamic_source.id -1\n' + self.logger.debug(msg) + tn.write(msg) + tn.write("exit\n") self.logger.debug(tn.read_all()) - self.current_stream_info = None except Exception, e: self.logger.error(str(e)) finally: @@ -550,7 +570,6 @@ class PypoPush(Thread): tn.write("exit\n") self.logger.debug(tn.read_all()) - self.current_stream_info = None except Exception, e: self.logger.error(str(e)) finally: