From e34f04cf57f1ae788e3547898a1ee82df1d74010 Mon Sep 17 00:00:00 2001 From: Mike Lang Date: Wed, 26 Dec 2018 21:29:42 -0800 Subject: [PATCH] restreamer: Harden generate_media_playlist to handle weird inputs and defaults --- restreamer/restreamer/main.py | 36 +++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/restreamer/restreamer/main.py b/restreamer/restreamer/main.py index 7f32ad5..41adfd0 100644 --- a/restreamer/restreamer/main.py +++ b/restreamer/restreamer/main.py @@ -129,13 +129,37 @@ def generate_master_playlist(stream): @app.route('/playlist//.m3u8') @has_path_args def generate_media_playlist(stream, variant): - #TODO handle no start/end - #TODO error handling of args - # TODO lots of other stuff - start = dateutil.parser.parse(request.args['start']) - end = dateutil.parser.parse(request.args['end']) + """Returns a HLS media playlist for the given stream and variant. + Takes optional params: + start, end: The time to begin and end the stream at. + Must be in ISO 8601 format (ie. yyyy-mm-ddTHH:MM:SS). + If not given, effectively means "infinity", ie. no start means + any time ago, no end means any time in the future. + """ + hours_path = os.path.join(app.static_folder, stream, variant) - segments = get_best_segments(hours_path, start, end) + if not os.path.isdir(hours_path): + abort(404) + + start = dateutil.parser.parse(request.args['start']) if 'start' in request.args else None + end = dateutil.parser.parse(request.args['end']) if 'end' in request.args else None + if start is None or end is None: + # If start or end are not given, use the earliest/latest time available + first, last = time_range_for_variant(stream, variant) + if start is None: + start = first + if end is None: + end = last + + # get_best_segments requires start be before end, special case that as no segments + # (not an error because someone might ask for a specific start, no end, but we ended up with + # end before start because that's the latest time we have) + if start < end: + segments = get_best_segments(hours_path, start, end) + else: + # Note the None to indicate there was a "hole" at both start and end + segments = [None] + return generate_hls.generate_media(segments, os.path.join(app.static_url_path, stream, variant))