Skip to content

Commit

Permalink
Specify rtsp_transport for Onvif Camera (home-assistant#31918)
Browse files Browse the repository at this point in the history
* specify rtsp_transport for onvif camera

* remove used variable

* Update homeassistant/components/stream/__init__.py

Co-Authored-By: Paulus Schoutsen <[email protected]>

* change options to stream_options

Co-authored-by: Paulus Schoutsen <[email protected]>
  • Loading branch information
engrbm87 and balloob authored Mar 6, 2020
1 parent ac94524 commit e1cc2ac
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
25 changes: 21 additions & 4 deletions homeassistant/components/camera/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@ async def async_request_stream(hass, entity_id, fmt):
f"{camera.entity_id} does not support play stream service"
)

return request_stream(hass, source, fmt=fmt, keepalive=camera_prefs.preload_stream)
return request_stream(
hass,
source,
fmt=fmt,
keepalive=camera_prefs.preload_stream,
options=camera.options,
)


@bind_hass
Expand Down Expand Up @@ -256,7 +262,7 @@ async def preload_stream(hass, _):
if not source:
continue

request_stream(hass, source, keepalive=True)
request_stream(hass, source, keepalive=True, options=camera.stream_options)

async_when_setup(hass, DOMAIN_STREAM, preload_stream)

Expand Down Expand Up @@ -312,6 +318,7 @@ class Camera(Entity):
def __init__(self):
"""Initialize a camera."""
self.is_streaming = False
self.stream_options = {}
self.content_type = DEFAULT_CONTENT_TYPE
self.access_tokens: collections.deque = collections.deque([], 2)
self.async_update_token()
Expand Down Expand Up @@ -581,7 +588,11 @@ async def ws_camera_stream(hass, connection, msg):

fmt = msg["format"]
url = request_stream(
hass, source, fmt=fmt, keepalive=camera_prefs.preload_stream
hass,
source,
fmt=fmt,
keepalive=camera_prefs.preload_stream,
options=camera.stream_options,
)
connection.send_result(msg["id"], {"url": url})
except HomeAssistantError as ex:
Expand Down Expand Up @@ -666,7 +677,13 @@ async def async_handle_play_stream_service(camera, service_call):
fmt = service_call.data[ATTR_FORMAT]
entity_ids = service_call.data[ATTR_MEDIA_PLAYER]

url = request_stream(hass, source, fmt=fmt, keepalive=camera_prefs.preload_stream)
url = request_stream(
hass,
source,
fmt=fmt,
keepalive=camera_prefs.preload_stream,
options=camera.stream_options,
)
data = {
ATTR_ENTITY_ID: entity_ids,
ATTR_MEDIA_CONTENT_ID: f"{hass.config.api.base_url}{url}",
Expand Down
6 changes: 6 additions & 0 deletions homeassistant/components/onvif/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
DEFAULT_PROFILE = 0

CONF_PROFILE = "profile"
CONF_RTSP_TRANSPORT = "rtsp_transport"

ATTR_PAN = "pan"
ATTR_TILT = "tilt"
Expand Down Expand Up @@ -71,6 +72,7 @@
ONVIF_DATA = "onvif"
ENTITIES = "entities"

RTSP_TRANS_PROTOCOLS = ["tcp", "udp", "udp_multicast", "http"]

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
Expand All @@ -80,6 +82,9 @@
vol.Optional(CONF_USERNAME, default=DEFAULT_USERNAME): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_EXTRA_ARGUMENTS, default=DEFAULT_ARGUMENTS): cv.string,
vol.Optional(CONF_RTSP_TRANSPORT, default=RTSP_TRANS_PROTOCOLS[0]): vol.In(
RTSP_TRANS_PROTOCOLS
),
vol.Optional(CONF_PROFILE, default=DEFAULT_PROFILE): vol.All(
vol.Coerce(int), vol.Range(min=0)
),
Expand Down Expand Up @@ -161,6 +166,7 @@ def __init__(self, hass, config):
self._profile_index = config.get(CONF_PROFILE)
self._ptz_service = None
self._input = None
self.stream_options[CONF_RTSP_TRANSPORT] = config.get(CONF_RTSP_TRANSPORT)
self._mac = None

_LOGGER.debug(
Expand Down
13 changes: 6 additions & 7 deletions homeassistant/components/stream/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ def request_stream(hass, stream_source, *, fmt="hls", keepalive=False, options=N
options = {}

# For RTSP streams, prefer TCP
if (
isinstance(stream_source, str)
and stream_source[:7] == "rtsp://"
and not options
):
options["rtsp_flags"] = "prefer_tcp"
options["stimeout"] = "5000000"
if isinstance(stream_source, str) and stream_source[:7] == "rtsp://":
options = {
"rtsp_flags": "prefer_tcp",
"stimeout": "5000000",
**options,
}

try:
streams = hass.data[DOMAIN][ATTR_STREAMS]
Expand Down

0 comments on commit e1cc2ac

Please sign in to comment.