2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Cameras with FFmpeg as decoder."""
|
2021-08-11 00:33:06 +00:00
|
|
|
from __future__ import annotations
|
2016-08-09 00:34:46 +00:00
|
|
|
|
2019-10-14 08:51:37 +00:00
|
|
|
from haffmpeg.camera import CameraMjpeg
|
2020-11-03 08:31:43 +00:00
|
|
|
from haffmpeg.tools import IMAGE_JPEG
|
2019-12-09 10:48:22 +00:00
|
|
|
import voluptuous as vol
|
2016-08-09 00:34:46 +00:00
|
|
|
|
2022-04-06 08:53:03 +00:00
|
|
|
from homeassistant.components.camera import PLATFORM_SCHEMA, Camera, CameraEntityFeature
|
2017-03-30 07:50:53 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
2021-12-27 16:56:13 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_aiohttp_proxy_stream
|
2016-08-09 00:34:46 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-12-27 16:56:13 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2020-11-03 08:31:43 +00:00
|
|
|
from . import CONF_EXTRA_ARGUMENTS, CONF_INPUT, DATA_FFMPEG, async_get_image
|
2016-08-27 20:43:33 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "FFmpeg"
|
2019-04-03 11:46:41 +00:00
|
|
|
DEFAULT_ARGUMENTS = "-pred 1"
|
2016-08-27 20:43:33 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_INPUT): cv.string,
|
|
|
|
vol.Optional(CONF_EXTRA_ARGUMENTS, default=DEFAULT_ARGUMENTS): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-08-09 00:34:46 +00:00
|
|
|
|
|
|
|
|
2021-12-27 16:56:13 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up a FFmpeg camera."""
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities([FFmpegCamera(hass, config)])
|
2016-08-09 00:34:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FFmpegCamera(Camera):
|
2016-08-11 09:14:24 +00:00
|
|
|
"""An implementation of an FFmpeg camera."""
|
2016-08-09 00:34:46 +00:00
|
|
|
|
2022-04-06 08:53:03 +00:00
|
|
|
_attr_supported_features = CameraEntityFeature.STREAM
|
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
def __init__(self, hass, config):
|
2016-08-09 00:34:46 +00:00
|
|
|
"""Initialize a FFmpeg camera."""
|
|
|
|
super().__init__()
|
2017-01-21 05:56:22 +00:00
|
|
|
|
|
|
|
self._manager = hass.data[DATA_FFMPEG]
|
2016-08-09 00:34:46 +00:00
|
|
|
self._name = config.get(CONF_NAME)
|
|
|
|
self._input = config.get(CONF_INPUT)
|
|
|
|
self._extra_arguments = config.get(CONF_EXTRA_ARGUMENTS)
|
|
|
|
|
2019-05-23 16:45:30 +00:00
|
|
|
async def stream_source(self):
|
2019-03-30 00:03:02 +00:00
|
|
|
"""Return the stream source."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self._input.split(" ")[-1]
|
2019-03-30 00:03:02 +00:00
|
|
|
|
2021-08-11 00:33:06 +00:00
|
|
|
async def async_camera_image(
|
|
|
|
self, width: int | None = None, height: int | None = None
|
|
|
|
) -> bytes | None:
|
2016-08-09 00:34:46 +00:00
|
|
|
"""Return a still image response from the camera."""
|
2020-11-03 08:31:43 +00:00
|
|
|
return await async_get_image(
|
|
|
|
self.hass,
|
|
|
|
self._input,
|
|
|
|
output_format=IMAGE_JPEG,
|
|
|
|
extra_cmd=self._extra_arguments,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-08-09 00:34:46 +00:00
|
|
|
|
2018-07-23 12:36:36 +00:00
|
|
|
async def handle_async_mjpeg_stream(self, request):
|
2016-08-09 00:34:46 +00:00
|
|
|
"""Generate an HTTP MJPEG stream from the camera."""
|
2016-10-24 06:48:01 +00:00
|
|
|
|
2020-11-25 07:45:15 +00:00
|
|
|
stream = CameraMjpeg(self._manager.binary)
|
2019-07-31 19:25:30 +00:00
|
|
|
await stream.open_camera(self._input, extra_cmd=self._extra_arguments)
|
2016-10-24 06:48:01 +00:00
|
|
|
|
2018-07-23 12:36:36 +00:00
|
|
|
try:
|
2019-03-27 06:55:05 +00:00
|
|
|
stream_reader = await stream.get_reader()
|
2018-07-23 12:36:36 +00:00
|
|
|
return await async_aiohttp_proxy_stream(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass,
|
|
|
|
request,
|
|
|
|
stream_reader,
|
|
|
|
self._manager.ffmpeg_stream_content_type,
|
|
|
|
)
|
2018-07-23 12:36:36 +00:00
|
|
|
finally:
|
|
|
|
await stream.close()
|
2016-08-09 00:34:46 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of this camera."""
|
|
|
|
return self._name
|