From fffd2f73e1c833769c1cad17a591ee9b8c287f0a Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Fri, 9 Oct 2020 11:15:56 -0400 Subject: [PATCH] Better Streaming --- fHDHR/fHDHRweb/__init__.py | 19 +++++++++++++----- fHDHR/fHDHRweb/fHDHRdevice/watch.py | 30 ++++++++++++++++++----------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/fHDHR/fHDHRweb/__init__.py b/fHDHR/fHDHRweb/__init__.py index 4ff735d..3aab69b 100644 --- a/fHDHR/fHDHRweb/__init__.py +++ b/fHDHR/fHDHRweb/__init__.py @@ -65,8 +65,11 @@ class HDHR_Hub(): def get_image(self, request_args): return self.images.get_image(request_args) - def get_stream(self, request_args): - return self.watch.get_stream(request_args) + def get_stream_info(self, request_args): + return self.watch.get_stream_info(request_args) + + def get_stream(self, channel_id, method, channelUri, content_type): + return self.watch.get_stream(channel_id, method, channelUri, content_type) hdhr = HDHR_Hub() @@ -148,9 +151,15 @@ class HDHR_HTTP_Server(): @app.route('/watch', methods=['GET']) def watch(): if 'method' in list(request.args.keys()) and 'channel' in list(request.args.keys()): - return Response(stream_with_context(hdhr.get_stream(request.args))) - else: - abort(503) + channel_id = str(request.args["channel"]) + method = str(request.args["method"]) + method, channelUri, content_type = hdhr.get_stream_info(request.args) + if channelUri: + if method == "direct": + return Response(hdhr.get_stream(channel_id, method, channelUri, content_type), content_type=content_type, direct_passthrough=True) + elif method == "ffmpeg": + return Response(stream_with_context(hdhr.get_stream(channel_id, method, channelUri, content_type)), mimetype="video/mpeg") + abort(503) @app.route('/lineup.post', methods=['POST']) def lineup_post(): diff --git a/fHDHR/fHDHRweb/fHDHRdevice/watch.py b/fHDHR/fHDHRweb/fHDHRdevice/watch.py index 7ad66f7..9123e1f 100644 --- a/fHDHR/fHDHRweb/fHDHRdevice/watch.py +++ b/fHDHR/fHDHRweb/fHDHRdevice/watch.py @@ -12,7 +12,7 @@ class WatchStream(): self.tuners = tuners self.web = fHDHR.tools.WebReq() - def direct_stream(self, channelUri): + def direct_stream(self, channel_id, method, channelUri, content_type): chunksize = int(self.tuners.config.dict["direct_stream"]['chunksize']) req = self.web.session.get(channelUri, stream=True) @@ -28,7 +28,7 @@ class WatchStream(): return generate() - def ffmpeg_stream(self, channelUri): + def ffmpeg_stream(self, channel_id, method, channelUri, content_type): bytes_per_read = int(self.config.dict["ffmpeg"]["bytes_per_read"]) ffmpeg_command = [self.config.dict["ffmpeg"]["ffmpeg_path"], @@ -61,10 +61,7 @@ class WatchStream(): self.tuners.tuner_close() return generate() - def get_stream(self, request_args): - - method = str(request_args["method"]) - channel_id = str(request_args["channel"]) + def get_stream(self, channel_id, method, channelUri, content_type): try: self.tuners.tuner_grab() @@ -75,10 +72,21 @@ class WatchStream(): print("Attempting a " + method + " stream request for channel " + str(channel_id)) - channelUri = self.origserv.get_channel_stream(channel_id) - # print("Proxy URL determined as " + str(channelUri)) - if method == "ffmpeg": - return self.ffmpeg_stream(channelUri) + return self.ffmpeg_stream(channel_id, method, channelUri, content_type) elif method == "direct": - return self.direct_stream(channelUri) + return self.direct_stream(channel_id, method, channelUri, content_type) + + def get_stream_info(self, request_args): + + method = str(request_args["method"]) + channel_id = str(request_args["channel"]) + + channelUri = self.origserv.get_channel_stream(channel_id) + if not channelUri: + return None, None, None + + channelUri_headers = self.web.session.head(channelUri).headers + content_type = channelUri_headers['Content-Type'] + + return method, channelUri, content_type