1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
"""Base functionality of ffmpeg HA wrapper."""
import asyncio
import logging
import re
import shlex
from typing import List, Optional, Set
from .timeout import asyncio_timeout
_LOGGER = logging.getLogger(__name__)
FFMPEG_STDOUT = "stdout"
FFMPEG_STDERR = "stderr"
_BACKGROUND_TASKS: Set[asyncio.Task] = set()
class HAFFmpeg:
"""HA FFmpeg process async.
Object is iterable or use the process property to call from Popen object.
"""
def __init__(self, ffmpeg_bin: str):
"""Base initialize."""
self._loop = asyncio.get_running_loop()
self._ffmpeg = ffmpeg_bin
self._argv = None
self._proc: Optional["asyncio.subprocess.Process"] = None
@property
def process(self) -> "asyncio.subprocess.Process":
"""Return a Popen object or None of not running."""
return self._proc
@property
def is_running(self) -> bool:
"""Return True if ffmpeg is running."""
if self._proc is None or self._proc.returncode is not None:
return False
return True
def _generate_ffmpeg_cmd(
self,
cmd: List[str],
input_source: Optional[str],
output: Optional[str],
extra_cmd: Optional[str] = None,
) -> None:
"""Generate ffmpeg command line."""
self._argv = [self._ffmpeg]
# start command init
if input_source is not None:
self._put_input(input_source)
self._argv.extend(cmd)
# exists a extra cmd from customer
if extra_cmd is not None:
self._argv.extend(shlex.split(extra_cmd))
self._merge_filters()
self._put_output(output)
def _put_input(self, input_source: str) -> None:
"""Put input string to ffmpeg command."""
input_cmd = shlex.split(str(input_source))
if len(input_cmd) > 1:
self._argv.extend(input_cmd)
else:
self._argv.extend(["-i", input_source])
def _put_output(self, output: Optional[str]) -> None:
"""Put output string to ffmpeg command."""
if output is None:
self._argv.extend(["-f", "null", "-"])
return
output_cmd = shlex.split(str(output))
if len(output_cmd) > 1:
self._argv.extend(output_cmd)
else:
self._argv.append(output)
def _merge_filters(self) -> None:
"""Merge all filter config in command line."""
for opts in (["-filter:a", "-af"], ["-filter:v", "-vf"]):
filter_list = []
new_argv = []
cmd_iter = iter(self._argv)
for element in cmd_iter:
if element in opts:
filter_list.insert(0, next(cmd_iter))
else:
new_argv.append(element)
# update argv if changes
if filter_list:
new_argv.extend([opts[0], ",".join(filter_list)])
self._argv = new_argv.copy()
def _clear(self) -> None:
"""Clear member variable after close."""
self._argv = None
self._proc = None
async def open(
self,
cmd: List[str],
input_source: Optional[str],
output: Optional[str] = "-",
extra_cmd: Optional[str] = None,
stdout_pipe: bool = True,
stderr_pipe: bool = False,
) -> bool:
"""Start a ffmpeg instance and pipe output."""
stdout = asyncio.subprocess.PIPE if stdout_pipe else asyncio.subprocess.DEVNULL
stderr = asyncio.subprocess.PIPE if stderr_pipe else asyncio.subprocess.DEVNULL
if self.is_running:
_LOGGER.warning("FFmpeg is already running!")
return True
# set command line
self._generate_ffmpeg_cmd(cmd, input_source, output, extra_cmd)
# start ffmpeg
_LOGGER.debug("Start FFmpeg with %s", str(self._argv))
try:
self._proc = await asyncio.create_subprocess_exec(
*self._argv,
bufsize=0,
stdin=asyncio.subprocess.PIPE,
stdout=stdout,
stderr=stderr,
close_fds=False,
)
except Exception as err: # pylint: disable=broad-except
_LOGGER.exception("FFmpeg fails %s", err)
self._clear()
return False
return self._proc is not None
async def close(self, timeout=5) -> None:
"""Stop a ffmpeg instance."""
if not self.is_running:
_LOGGER.debug("FFmpeg isn't running!")
return
# Can't use communicate because we attach the output to a streamreader
# send stop to ffmpeg
try:
self._proc.stdin.write(b"q")
async with asyncio_timeout(timeout):
await self._proc.wait()
_LOGGER.debug("Close FFmpeg process")
except (asyncio.TimeoutError, ValueError):
_LOGGER.warning("Timeout while waiting of FFmpeg")
self.kill()
finally:
self._clear()
def kill(self) -> None:
"""Kill ffmpeg job."""
self._proc.kill()
background_task = asyncio.create_task(self._proc.communicate())
_BACKGROUND_TASKS.add(background_task)
background_task.add_done_callback(_BACKGROUND_TASKS.remove)
async def get_reader(self, source=FFMPEG_STDOUT) -> asyncio.StreamReader:
"""Create and return streamreader."""
if source == FFMPEG_STDOUT:
return self._proc.stdout
return self._proc.stderr
class HAFFmpegWorker(HAFFmpeg):
"""Read FFmpeg output to queue."""
def __init__(self, ffmpeg_bin: str):
"""Init noise sensor."""
super().__init__(ffmpeg_bin)
self._queue = asyncio.Queue()
self._input = None
self._read_task = None
async def close(self, timeout: int = 5) -> None:
"""Stop a ffmpeg instance.
Return a coroutine
"""
if self._read_task is not None and not self._read_task.cancelled():
self._read_task.cancel()
return await super().close(timeout)
async def _process_lines(self, pattern: Optional[str] = None) -> None:
"""Read line from pipe they match with pattern."""
if pattern is not None:
cmp = re.compile(pattern)
_LOGGER.debug("Start working with pattern '%s'.", pattern)
# read lines
while self.is_running:
try:
line = await self._input.readline()
if not line:
break
line = line.decode()
except Exception: # pylint: disable=broad-except
break
match = True if pattern is None else cmp.search(line)
if match:
_LOGGER.debug("Process: %s", line)
await self._queue.put(line)
try:
await self._proc.wait()
finally:
await self._queue.put(None)
_LOGGER.debug("Stopped reading ffmpeg output.")
async def _worker_process(self) -> None:
"""Process output line."""
raise NotImplementedError()
async def start_worker(
self,
cmd: List[str],
input_source: str,
output: Optional[str] = None,
extra_cmd: Optional[str] = None,
pattern: Optional[str] = None,
reading: str = FFMPEG_STDERR,
) -> None:
"""Start ffmpeg do process data from output."""
if self.is_running:
_LOGGER.warning("Can't start worker. It is allready running!")
return
if reading == FFMPEG_STDERR:
stdout = False
stderr = True
else:
stdout = True
stderr = False
# start ffmpeg and reading to queue
await self.open(
cmd=cmd,
input_source=input_source,
output=output,
extra_cmd=extra_cmd,
stdout_pipe=stdout,
stderr_pipe=stderr,
)
self._input = await self.get_reader(reading)
# start background processing
self._read_task = self._loop.create_task(self._process_lines(pattern))
self._loop.create_task(self._worker_process())
|