From ab51ea02a12525cf5e33e82484e78173f58a2edc Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Sun, 31 Jan 2021 15:24:19 -0500 Subject: [PATCH] Improve Detection of ffmpeg/vlc application paths --- fHDHR/device/tuners/tuner.py | 2 +- fHDHR_web/api/tuners.py | 10 +++- .../fHDHR_plugin_stream_ffmpeg/__init__.py | 57 ++++++++++++++++--- .../ffmpeg_conf.json | 2 +- plugins/fHDHR_plugin_stream_vlc/__init__.py | 57 ++++++++++++++++--- plugins/fHDHR_plugin_stream_vlc/vlc_conf.json | 2 +- 6 files changed, 108 insertions(+), 22 deletions(-) diff --git a/fHDHR/device/tuners/tuner.py b/fHDHR/device/tuners/tuner.py index 798f0f5..d068cb0 100644 --- a/fHDHR/device/tuners/tuner.py +++ b/fHDHR/device/tuners/tuner.py @@ -85,7 +85,7 @@ class Tuner(): def get_stream(self, stream_args, tuner): stream = Stream(self.fhdhr, stream_args, tuner) - return stream.get() + return stream def set_status(self, stream_args): if self.status["status"] != "Active": diff --git a/fHDHR_web/api/tuners.py b/fHDHR_web/api/tuners.py index bb5748d..a1f0232 100644 --- a/fHDHR_web/api/tuners.py +++ b/fHDHR_web/api/tuners.py @@ -132,7 +132,15 @@ class Tuners(): tuner.set_status(stream_args) session["tuner_used"] = tunernum - return Response(stream_with_context(tuner.get_stream(stream_args, tuner)), mimetype=stream_args["content_type"]) + try: + stream = tuner.get_stream(stream_args, tuner) + except TunerError as e: + response.headers["X-fHDHR-Error"] = str(e) + self.fhdhr.logger.error(response.headers["X-fHDHR-Error"]) + tuner.close() + abort(response) + + return Response(stream_with_context(stream.get()), mimetype=stream_args["content_type"]) elif method == "close": diff --git a/plugins/fHDHR_plugin_stream_ffmpeg/__init__.py b/plugins/fHDHR_plugin_stream_ffmpeg/__init__.py index 27db2c8..4a5deab 100644 --- a/plugins/fHDHR_plugin_stream_ffmpeg/__init__.py +++ b/plugins/fHDHR_plugin_stream_ffmpeg/__init__.py @@ -1,23 +1,59 @@ +import os import sys import subprocess +from fHDHR.exceptions import TunerError + def setup(plugin): - try: - ffmpeg_command = [plugin.config.dict["ffmpeg"]["path"], - "-version", - "pipe:stdout" - ] - ffmpeg_proc = subprocess.Popen(ffmpeg_command, stdout=subprocess.PIPE) - ffmpeg_version = ffmpeg_proc.stdout.read() + # Check config for ffmpeg path + ffmpeg_path = None + if plugin.config.dict["ffmpeg"]["path"]: + # verify path is valid + if os.path.isfile(plugin.config.dict["ffmpeg"]["path"]): + ffmpeg_path = plugin.config.dict["ffmpeg"]["path"] + else: + plugin.logger.warning("Failed to find ffmpeg at %s." % plugin.config.dict["ffmpeg"]["path"]) + + if not ffmpeg_path: + plugin.logger.info("Attempting to find ffmpeg in PATH.") + if plugin.config.internal["versions"]["Operating System"]["version"] in ["Linux", "Darwin"]: + find_ffmpeg_command = ["which", "ffmpeg"] + elif plugin.config.internal["versions"]["Operating System"]["version"] in ["Windows"]: + find_ffmpeg_command = ["where", "ffmpeg"] + + ffmpeg_proc = subprocess.Popen(find_ffmpeg_command, stdout=subprocess.PIPE) + ffmpeg_path = ffmpeg_proc.stdout.read().decode().strip("\n") ffmpeg_proc.terminate() ffmpeg_proc.communicate() ffmpeg_proc.kill() - ffmpeg_version = ffmpeg_version.decode().split("version ")[1].split(" ")[0] - except FileNotFoundError: + if not ffmpeg_path: + ffmpeg_path = None + elif ffmpeg_path.isspace(): + ffmpeg_path = None + + if ffmpeg_path: + plugin.config.dict["ffmpeg"]["path"] = ffmpeg_path + + if ffmpeg_path: + ffmpeg_command = [ffmpeg_path, "-version", "pipe:stdout"] + try: + ffmpeg_proc = subprocess.Popen(ffmpeg_command, stdout=subprocess.PIPE) + ffmpeg_version = ffmpeg_proc.stdout.read().decode().split("version ")[1].split(" ")[0] + except FileNotFoundError: + ffmpeg_version = None + except PermissionError: + ffmpeg_version = None + finally: + ffmpeg_proc.terminate() + ffmpeg_proc.communicate() + ffmpeg_proc.kill() + + if not ffmpeg_version: ffmpeg_version = "Missing" plugin.logger.warning("Failed to find ffmpeg.") + plugin.config.register_version("ffmpeg", ffmpeg_version, "env") @@ -29,6 +65,9 @@ class Plugin_OBJ(): self.stream_args = stream_args self.tuner = tuner + if self.plugin_utils.config.internal["versions"]["ffmpeg"] == "Missing": + raise TunerError("806 - Tune Failed: FFMPEG Missing") + self.bytes_per_read = int(plugin_utils.config.dict["streaming"]["bytes_per_read"]) self.ffmpeg_command = self.ffmpeg_command_assemble(stream_args) diff --git a/plugins/fHDHR_plugin_stream_ffmpeg/ffmpeg_conf.json b/plugins/fHDHR_plugin_stream_ffmpeg/ffmpeg_conf.json index b57386c..6389c73 100644 --- a/plugins/fHDHR_plugin_stream_ffmpeg/ffmpeg_conf.json +++ b/plugins/fHDHR_plugin_stream_ffmpeg/ffmpeg_conf.json @@ -1,7 +1,7 @@ { "ffmpeg":{ "path":{ - "value": "ffmpeg", + "value": "none", "config_file": true, "config_web": true } diff --git a/plugins/fHDHR_plugin_stream_vlc/__init__.py b/plugins/fHDHR_plugin_stream_vlc/__init__.py index 23a9b3b..5a298dc 100644 --- a/plugins/fHDHR_plugin_stream_vlc/__init__.py +++ b/plugins/fHDHR_plugin_stream_vlc/__init__.py @@ -1,23 +1,59 @@ +import os import sys import subprocess +from fHDHR.exceptions import TunerError + def setup(plugin): - try: - vlc_command = [plugin.config.dict["vlc"]["path"], - "--version", - "pipe:stdout" - ] - vlc_proc = subprocess.Popen(vlc_command, stdout=subprocess.PIPE) - vlc_version = vlc_proc.stdout.read() + # Check config for vlc path + vlc_path = None + if plugin.config.dict["vlc"]["path"]: + # verify path is valid + if os.path.isfile(plugin.config.dict["vlc"]["path"]): + vlc_path = plugin.config.dict["vlc"]["path"] + else: + plugin.logger.warning("Failed to find vlc at %s." % plugin.config.dict["vlc"]["path"]) + + if not vlc_path: + plugin.logger.info("Attempting to find vlc in PATH.") + if plugin.config.internal["versions"]["Operating System"]["version"] in ["Linux", "Darwin"]: + find_vlc_command = ["which", "vlc"] + elif plugin.config.internal["versions"]["Operating System"]["version"] in ["Windows"]: + find_vlc_command = ["where", "vlc"] + + vlc_proc = subprocess.Popen(find_vlc_command, stdout=subprocess.PIPE) + vlc_path = vlc_proc.stdout.read().decode().strip("\n") vlc_proc.terminate() vlc_proc.communicate() vlc_proc.kill() - vlc_version = vlc_version.decode().split("version ")[1].split('\n')[0] - except FileNotFoundError: + if not vlc_path: + vlc_path = None + elif vlc_path.isspace(): + vlc_path = None + + if vlc_path: + plugin.config.dict["vlc"]["path"] = vlc_path + + if vlc_path: + vlc_command = [vlc_path, "--version", "pipe:stdout"] + try: + vlc_proc = subprocess.Popen(vlc_command, stdout=subprocess.PIPE) + vlc_version = vlc_proc.stdout.read().decode().split("version ")[1].split('\n')[0] + except FileNotFoundError: + vlc_version = None + except PermissionError: + vlc_version = None + finally: + vlc_proc.terminate() + vlc_proc.communicate() + vlc_proc.kill() + + if not vlc_version: vlc_version = "Missing" plugin.logger.warning("Failed to find vlc.") + plugin.config.register_version("vlc", vlc_version, "env") @@ -29,6 +65,9 @@ class Plugin_OBJ(): self.stream_args = stream_args self.tuner = tuner + if self.plugin_utils.config.internal["versions"]["vlc"] == "Missing": + raise TunerError("806 - Tune Failed: VLC Missing") + self.bytes_per_read = int(self.plugin_utils.config.dict["streaming"]["bytes_per_read"]) self.vlc_command = self.vlc_command_assemble(stream_args) diff --git a/plugins/fHDHR_plugin_stream_vlc/vlc_conf.json b/plugins/fHDHR_plugin_stream_vlc/vlc_conf.json index bb14110..195fa81 100644 --- a/plugins/fHDHR_plugin_stream_vlc/vlc_conf.json +++ b/plugins/fHDHR_plugin_stream_vlc/vlc_conf.json @@ -1,7 +1,7 @@ { "vlc":{ "path":{ - "value": "cvlc", + "value": "none", "config_file": true, "config_web": true }