2020-09-27 12:51:29 +02:00
|
|
|
import signal
|
|
|
|
import subprocess
|
2024-02-17 00:01:47 +01:00
|
|
|
from typing import Iterable, Optional
|
2020-09-27 12:51:29 +02:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
from PIL.Image import Image as ImageType
|
|
|
|
|
|
|
|
from platypush.plugins.camera import CameraPlugin
|
|
|
|
from platypush.plugins.camera.ffmpeg.model import FFmpegCamera, FFmpegCameraInfo
|
2024-02-17 00:01:47 +01:00
|
|
|
from platypush.plugins.camera.model.camera import Camera
|
2020-09-27 12:51:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CameraFfmpegPlugin(CameraPlugin):
|
|
|
|
"""
|
|
|
|
Plugin to interact with a camera over FFmpeg.
|
|
|
|
"""
|
|
|
|
|
|
|
|
_camera_class = FFmpegCamera
|
|
|
|
_camera_info_class = FFmpegCameraInfo
|
|
|
|
|
2023-09-24 16:54:43 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
device: Optional[str] = '/dev/video0',
|
|
|
|
input_format: str = 'v4l2',
|
2024-02-17 00:01:47 +01:00
|
|
|
ffmpeg_args: Iterable[str] = (),
|
|
|
|
**opts,
|
2023-09-24 16:54:43 +02:00
|
|
|
):
|
2020-09-27 12:51:29 +02:00
|
|
|
"""
|
2020-09-28 02:33:27 +02:00
|
|
|
:param device: Path to the camera device (default: ``/dev/video0``).
|
2024-02-17 00:01:47 +01:00
|
|
|
:param input_format: FFmpeg input format for the camera device (default: ``v4l2``).
|
2020-09-27 12:51:29 +02:00
|
|
|
:param ffmpeg_args: Extra options to be passed to the FFmpeg executable.
|
|
|
|
:param opts: Camera options - see constructor of :class:`platypush.plugins.camera.CameraPlugin`.
|
|
|
|
"""
|
|
|
|
super().__init__(device=device, input_format=input_format, **opts)
|
2024-02-17 00:01:47 +01:00
|
|
|
self.camera_info.ffmpeg_args = ffmpeg_args or () # type: ignore
|
2020-09-27 12:51:29 +02:00
|
|
|
|
2024-02-19 01:07:29 +01:00
|
|
|
def prepare_device(self, device: Camera, **_) -> subprocess.Popen:
|
2024-02-17 00:01:47 +01:00
|
|
|
assert isinstance(device, FFmpegCamera)
|
|
|
|
warmup_seconds = self._get_warmup_seconds(device)
|
2023-09-24 16:54:43 +02:00
|
|
|
ffmpeg = [
|
2024-02-17 00:01:47 +01:00
|
|
|
device.info.ffmpeg_bin,
|
2023-09-24 16:54:43 +02:00
|
|
|
'-y',
|
|
|
|
'-f',
|
2024-02-17 00:01:47 +01:00
|
|
|
device.info.input_format,
|
2023-09-24 16:54:43 +02:00
|
|
|
'-i',
|
2024-02-17 00:01:47 +01:00
|
|
|
device.info.device,
|
2023-09-24 16:54:43 +02:00
|
|
|
'-s',
|
2024-02-17 00:01:47 +01:00
|
|
|
*(
|
|
|
|
(f'{device.info.resolution[0]}x{device.info.resolution[1]}',)
|
|
|
|
if device.info.resolution
|
|
|
|
else ()
|
|
|
|
),
|
2023-09-24 16:54:43 +02:00
|
|
|
'-ss',
|
|
|
|
str(warmup_seconds),
|
2024-02-17 00:01:47 +01:00
|
|
|
*(('-r', str(device.info.fps)) if device.info.fps else ()),
|
2023-09-24 16:54:43 +02:00
|
|
|
'-pix_fmt',
|
|
|
|
'rgb24',
|
|
|
|
'-f',
|
|
|
|
'rawvideo',
|
2024-02-17 00:01:47 +01:00
|
|
|
*device.info.ffmpeg_args,
|
2023-09-24 16:54:43 +02:00
|
|
|
'-',
|
|
|
|
]
|
2020-09-27 12:51:29 +02:00
|
|
|
|
2024-02-17 00:01:47 +01:00
|
|
|
self.logger.info('Running FFmpeg command: "%s"', ' '.join(ffmpeg))
|
2020-09-27 12:51:29 +02:00
|
|
|
proc = subprocess.Popen(ffmpeg, stdout=subprocess.PIPE)
|
|
|
|
# Start in suspended mode
|
|
|
|
proc.send_signal(signal.SIGSTOP)
|
|
|
|
return proc
|
|
|
|
|
2024-02-17 00:01:47 +01:00
|
|
|
def start_camera(self, camera: Camera, *args, preview: bool = False, **kwargs):
|
|
|
|
assert isinstance(camera, FFmpegCamera)
|
2020-09-27 12:51:29 +02:00
|
|
|
super().start_camera(*args, camera=camera, preview=preview, **kwargs)
|
|
|
|
if camera.object:
|
|
|
|
camera.object.send_signal(signal.SIGCONT)
|
|
|
|
|
2024-02-17 00:01:47 +01:00
|
|
|
def release_device(self, device: Camera):
|
|
|
|
assert isinstance(device, FFmpegCamera)
|
|
|
|
if device.object:
|
|
|
|
device.object.terminate()
|
|
|
|
if device.object.stdout:
|
|
|
|
device.object.stdout.close()
|
|
|
|
device.object = None # type: ignore
|
2020-09-27 12:51:29 +02:00
|
|
|
|
2024-02-17 00:01:47 +01:00
|
|
|
def wait_capture(self, camera: Camera):
|
|
|
|
assert isinstance(camera, FFmpegCamera)
|
2020-09-27 12:51:29 +02:00
|
|
|
if camera.object and camera.object.poll() is None:
|
|
|
|
try:
|
|
|
|
camera.object.wait(timeout=camera.info.capture_timeout)
|
|
|
|
except Exception as e:
|
2024-02-17 00:01:47 +01:00
|
|
|
self.logger.warning('Error on FFmpeg capture wait: %s', e)
|
|
|
|
|
|
|
|
def capture_frame(self, device: Camera, *_, **__) -> Optional[ImageType]:
|
|
|
|
assert isinstance(device, FFmpegCamera)
|
|
|
|
assert device.info.resolution, 'Resolution not set'
|
|
|
|
assert device.object.stdout, 'Camera not started'
|
2020-09-27 12:51:29 +02:00
|
|
|
|
2024-02-17 00:01:47 +01:00
|
|
|
raw_size = device.info.resolution[0] * device.info.resolution[1] * 3
|
|
|
|
data = device.object.stdout.read(raw_size)
|
2020-09-27 12:51:29 +02:00
|
|
|
if len(data) < raw_size:
|
|
|
|
return
|
2024-02-17 00:01:47 +01:00
|
|
|
return Image.frombytes('RGB', device.info.resolution, data)
|
2020-09-27 12:51:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|