ffplayout/tests/run_time_warp.py

73 lines
1.7 KiB
Python
Raw Normal View History

2021-02-10 17:46:49 +01:00
#!/usr/bin/env python3
2021-02-11 09:44:58 +01:00
"""
Test script, for simulating speed up the clock.
With the WARP_FACTOR you can transform a second to a fraction.
With this functionality it is possible to run a 24 hours playlist in a minute,
and debug the playlist reader.
"""
2021-02-11 09:29:39 +01:00
import datetime
2021-02-10 17:46:49 +01:00
import os
import sys
import time
import time_machine
2021-03-01 12:50:44 +01:00
try:
from zoneinfo import ZoneInfo
except ImportError:
from backports.zoneinfo import ZoneInfo
2021-02-10 17:46:49 +01:00
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# set time zone
_TZ = ZoneInfo("Europe/Berlin")
# fake date and time
2021-02-11 09:29:39 +01:00
SOURCE_TIME = [2021, 2, 12, 5, 0, 0]
USE_TIME_MACHINE = True
2021-02-10 17:46:49 +01:00
# warp time by factor
WARP_FACTOR = 1000
2021-02-11 09:29:39 +01:00
def warp_time():
2021-02-10 17:46:49 +01:00
get_source = GetSourceFromPlaylist()
stamp = time.time()
duration = 0
with time_machine.travel(stamp, tick=False) as traveller:
2021-02-17 08:45:16 +01:00
for node in get_source.next():
2021-02-10 17:46:49 +01:00
duration = node['out'] - node['seek']
messenger.info(f'Play: "{node["source"]}"')
warp_duration = duration / WARP_FACTOR
2021-02-11 09:29:39 +01:00
messenger.debug(f'Original duration {duration} '
f'warped to {warp_duration:.3f}')
2021-02-10 17:46:49 +01:00
time.sleep(warp_duration)
stamp += duration
traveller.move_to(stamp)
2021-02-11 09:29:39 +01:00
@time_machine.travel(datetime.datetime(*SOURCE_TIME, tzinfo=_TZ))
def run_in_time_machine():
warp_time()
def run_in_time_warp():
warp_time()
2021-02-10 17:46:49 +01:00
if __name__ == '__main__':
from ffplayout.playlist import GetSourceFromPlaylist
from ffplayout.utils import messenger
try:
2021-02-11 09:29:39 +01:00
if USE_TIME_MACHINE:
run_in_time_machine()
else:
warp_time()
2021-02-10 17:46:49 +01:00
except KeyboardInterrupt:
print('Interrupted')