2011-03-03 06:22:28 +01:00
|
|
|
import calendar
|
2011-03-21 00:34:43 +01:00
|
|
|
import math
|
2013-01-31 18:33:14 +01:00
|
|
|
import os
|
2021-06-03 15:20:39 +02:00
|
|
|
import sys
|
|
|
|
import telnetlib
|
|
|
|
import time
|
|
|
|
import traceback
|
|
|
|
from datetime import datetime, timedelta
|
2020-01-16 15:32:51 +01:00
|
|
|
from queue import Empty, Queue
|
2012-03-06 02:47:14 +01:00
|
|
|
from threading import Thread
|
2012-03-16 04:14:19 +01:00
|
|
|
|
2022-01-02 21:02:55 +01:00
|
|
|
from libretime_api_client import version1 as api_client
|
2022-01-13 16:11:37 +01:00
|
|
|
from loguru import logger
|
2021-06-03 15:20:39 +02:00
|
|
|
|
2022-01-18 20:59:11 +01:00
|
|
|
from .config import Config
|
2021-06-03 15:20:39 +02:00
|
|
|
from .pypofetch import PypoFetch
|
|
|
|
from .pypoliqqueue import PypoLiqQueue
|
2020-01-16 15:32:51 +01:00
|
|
|
from .timeout import ls_timeout
|
2011-03-03 06:22:28 +01:00
|
|
|
|
2013-04-26 04:11:26 +02:00
|
|
|
PUSH_INTERVAL = 2
|
|
|
|
|
2011-03-03 06:22:28 +01:00
|
|
|
|
2012-08-31 23:21:24 +02:00
|
|
|
def is_stream(media_item):
|
2021-05-27 16:23:02 +02:00
|
|
|
return media_item["type"] == "stream_output_start"
|
|
|
|
|
2012-08-31 23:21:24 +02:00
|
|
|
|
|
|
|
def is_file(media_item):
|
2021-05-27 16:23:02 +02:00
|
|
|
return media_item["type"] == "file"
|
|
|
|
|
2012-08-31 23:21:24 +02:00
|
|
|
|
2011-03-21 00:34:43 +01:00
|
|
|
class PypoPush(Thread):
|
2022-01-18 20:59:11 +01:00
|
|
|
def __init__(self, q, telnet_lock, pypo_liquidsoap, config: Config):
|
2011-03-21 00:34:43 +01:00
|
|
|
Thread.__init__(self)
|
2012-07-16 21:33:44 +02:00
|
|
|
self.api_client = api_client.AirtimeApiClient()
|
2011-03-21 00:34:43 +01:00
|
|
|
self.queue = q
|
|
|
|
|
2012-03-16 04:14:19 +01:00
|
|
|
self.telnet_lock = telnet_lock
|
2013-04-26 04:11:26 +02:00
|
|
|
self.config = config
|
2011-03-03 06:22:28 +01:00
|
|
|
|
2012-03-13 20:24:24 +01:00
|
|
|
self.pushed_objects = {}
|
2012-08-17 23:10:42 +02:00
|
|
|
self.current_prebuffering_stream_id = None
|
2013-03-14 21:50:55 +01:00
|
|
|
self.queue_id = 0
|
|
|
|
|
2013-03-14 23:29:52 +01:00
|
|
|
self.future_scheduled_queue = Queue()
|
2013-04-26 19:58:44 +02:00
|
|
|
self.pypo_liquidsoap = pypo_liquidsoap
|
2013-03-22 17:16:17 +01:00
|
|
|
|
2022-01-13 16:11:37 +01:00
|
|
|
self.plq = PypoLiqQueue(self.future_scheduled_queue, self.pypo_liquidsoap)
|
2013-03-15 17:50:23 +01:00
|
|
|
self.plq.daemon = True
|
|
|
|
self.plq.start()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2012-03-21 23:26:16 +01:00
|
|
|
def main(self):
|
|
|
|
loops = 0
|
2012-06-27 04:41:11 +02:00
|
|
|
heartbeat_period = math.floor(30 / PUSH_INTERVAL)
|
|
|
|
|
2012-03-20 20:51:54 +01:00
|
|
|
media_schedule = None
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2012-03-20 20:51:54 +01:00
|
|
|
while True:
|
|
|
|
try:
|
2013-03-15 20:07:55 +01:00
|
|
|
media_schedule = self.queue.get(block=True)
|
2020-01-16 15:32:51 +01:00
|
|
|
except Exception as e:
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.error(str(e))
|
2013-03-15 20:07:55 +01:00
|
|
|
raise
|
2013-03-14 21:50:55 +01:00
|
|
|
else:
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.debug(media_schedule)
|
2021-05-27 16:23:02 +02:00
|
|
|
# separate media_schedule list into currently_playing and
|
|
|
|
# scheduled_for_future lists
|
|
|
|
currently_playing, scheduled_for_future = self.separate_present_future(
|
|
|
|
media_schedule
|
|
|
|
)
|
2013-03-14 21:50:55 +01:00
|
|
|
|
2013-03-22 17:16:17 +01:00
|
|
|
self.pypo_liquidsoap.verify_correct_present_media(currently_playing)
|
2013-03-14 21:50:55 +01:00
|
|
|
self.future_scheduled_queue.put(scheduled_for_future)
|
|
|
|
|
2012-03-21 23:26:16 +01:00
|
|
|
if loops % heartbeat_period == 0:
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.info("heartbeat")
|
2012-03-21 23:26:16 +01:00
|
|
|
loops = 0
|
|
|
|
loops += 1
|
2012-03-20 20:51:54 +01:00
|
|
|
|
2013-03-14 21:50:55 +01:00
|
|
|
def separate_present_future(self, media_schedule):
|
|
|
|
tnow = datetime.utcnow()
|
|
|
|
|
2013-03-14 23:29:52 +01:00
|
|
|
present = []
|
2013-03-14 21:50:55 +01:00
|
|
|
future = {}
|
|
|
|
|
|
|
|
sorted_keys = sorted(media_schedule.keys())
|
|
|
|
for mkey in sorted_keys:
|
|
|
|
media_item = media_schedule[mkey]
|
|
|
|
|
2021-10-11 23:43:25 +02:00
|
|
|
# Ignore track that already ended
|
|
|
|
if media_item["end"] < tnow:
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.debug(f"ignoring ended media_item: {media_item}")
|
2021-10-11 23:43:25 +02:00
|
|
|
continue
|
|
|
|
|
2021-05-27 16:23:02 +02:00
|
|
|
diff_td = tnow - media_item["start"]
|
2013-03-14 21:50:55 +01:00
|
|
|
diff_sec = self.date_interval_to_seconds(diff_td)
|
|
|
|
|
|
|
|
if diff_sec >= 0:
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.debug(f"adding media_item to present: {media_item}")
|
2013-03-14 23:29:52 +01:00
|
|
|
present.append(media_item)
|
2013-03-14 21:50:55 +01:00
|
|
|
else:
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.debug(f"adding media_item to future: {media_item}")
|
2013-05-30 20:02:05 +02:00
|
|
|
future[mkey] = media_item
|
2013-03-14 21:50:55 +01:00
|
|
|
|
|
|
|
return present, future
|
|
|
|
|
2012-03-21 23:26:16 +01:00
|
|
|
def date_interval_to_seconds(self, interval):
|
2012-10-16 17:59:37 +02:00
|
|
|
"""
|
|
|
|
Convert timedelta object into int representing the number of seconds. If
|
|
|
|
number of seconds is less than 0, then return 0.
|
|
|
|
"""
|
2021-05-27 16:23:02 +02:00
|
|
|
seconds = (
|
|
|
|
interval.microseconds
|
|
|
|
+ (interval.seconds + interval.days * 24 * 3600) * 10 ** 6
|
|
|
|
) / float(10 ** 6)
|
2012-10-16 17:59:37 +02:00
|
|
|
|
|
|
|
return seconds
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2013-06-11 21:55:17 +02:00
|
|
|
@ls_timeout
|
2012-08-31 23:21:24 +02:00
|
|
|
def stop_web_stream_all(self):
|
|
|
|
try:
|
|
|
|
self.telnet_lock.acquire()
|
2022-01-18 20:59:11 +01:00
|
|
|
tn = telnetlib.Telnet(
|
|
|
|
self.config.playout.liquidsoap_host,
|
|
|
|
self.config.playout.liquidsoap_port,
|
|
|
|
)
|
2012-08-31 23:21:24 +02:00
|
|
|
|
2021-05-27 16:23:02 +02:00
|
|
|
# msg = 'dynamic_source.read_stop_all xxx\n'
|
|
|
|
msg = "http.stop\n"
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.debug(msg)
|
2012-08-31 23:21:24 +02:00
|
|
|
tn.write(msg)
|
|
|
|
|
2021-05-27 16:23:02 +02:00
|
|
|
msg = "dynamic_source.output_stop\n"
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.debug(msg)
|
2012-08-31 23:21:24 +02:00
|
|
|
tn.write(msg)
|
|
|
|
|
2021-05-27 16:23:02 +02:00
|
|
|
msg = "dynamic_source.id -1\n"
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.debug(msg)
|
2012-10-19 22:29:31 +02:00
|
|
|
tn.write(msg)
|
|
|
|
|
2012-08-31 23:21:24 +02:00
|
|
|
tn.write("exit\n")
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.debug(tn.read_all())
|
2012-08-31 23:21:24 +02:00
|
|
|
|
2020-01-16 15:32:51 +01:00
|
|
|
except Exception as e:
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.error(str(e))
|
2012-08-31 23:21:24 +02:00
|
|
|
finally:
|
|
|
|
self.telnet_lock.release()
|
|
|
|
|
2011-03-21 00:34:43 +01:00
|
|
|
def run(self):
|
2014-03-11 23:01:29 +01:00
|
|
|
while True:
|
2021-05-27 16:23:02 +02:00
|
|
|
try:
|
|
|
|
self.main()
|
2020-01-16 15:32:51 +01:00
|
|
|
except Exception as e:
|
2014-03-11 23:01:29 +01:00
|
|
|
top = traceback.format_exc()
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.error("Pypo Push Exception: %s", top)
|
2014-03-11 23:01:29 +01:00
|
|
|
time.sleep(5)
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.info("PypoPush thread exiting")
|