From 2639336ab0caf9a24b8a976bd8738a01f18f24ac Mon Sep 17 00:00:00 2001 From: starkillerOG Date: Fri, 14 Jun 2024 21:38:53 +0200 Subject: [PATCH] Prefer mp4 playback in Reolink (#119630) * If possible use PLAYBACK of mp4 files * bring test_coverage back to 100% * Do not reasign the vod_type multiple times Co-authored-by: Dave T <17680170+davet2001@users.noreply.github.com> * fix indent * add white space * fix tests * Update homeassistant/components/reolink/media_source.py Co-authored-by: Dave T <17680170+davet2001@users.noreply.github.com> --------- Co-authored-by: Dave T <17680170+davet2001@users.noreply.github.com> --- .../components/reolink/media_source.py | 20 +++++++++++--- tests/components/reolink/test_media_source.py | 26 ++++++++++++++++--- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/reolink/media_source.py b/homeassistant/components/reolink/media_source.py index c22a0fc28e7..c941f5ed055 100644 --- a/homeassistant/components/reolink/media_source.py +++ b/homeassistant/components/reolink/media_source.py @@ -59,19 +59,31 @@ class ReolinkVODMediaSource(MediaSource): data: dict[str, ReolinkData] = self.hass.data[DOMAIN] host = data[config_entry_id].host - vod_type = VodRequestType.RTMP - if host.api.is_nvr: - vod_type = VodRequestType.FLV + def get_vod_type() -> VodRequestType: + if filename.endswith(".mp4"): + return VodRequestType.PLAYBACK + if host.api.is_nvr: + return VodRequestType.FLV + return VodRequestType.RTMP + + vod_type = get_vod_type() mime_type, url = await host.api.get_vod_source( channel, filename, stream_res, vod_type ) if _LOGGER.isEnabledFor(logging.DEBUG): - url_log = f"{url.split('&user=')[0]}&user=xxxxx&password=xxxxx" + url_log = url + if "&user=" in url_log: + url_log = f"{url_log.split('&user=')[0]}&user=xxxxx&password=xxxxx" + elif "&token=" in url_log: + url_log = f"{url_log.split('&token=')[0]}&token=xxxxx" _LOGGER.debug( "Opening VOD stream from %s: %s", host.api.camera_name(channel), url_log ) + if mime_type == "video/mp4": + return PlayMedia(url, mime_type) + stream = create_stream(self.hass, url, {}, DynamicStreamSettings()) stream.add_provider("hls", timeout=3600) stream_url: str = stream.endpoint_url("hls") diff --git a/tests/components/reolink/test_media_source.py b/tests/components/reolink/test_media_source.py index 1eb45945eee..3e3cdd02b46 100644 --- a/tests/components/reolink/test_media_source.py +++ b/tests/components/reolink/test_media_source.py @@ -51,11 +51,14 @@ TEST_DAY2 = 15 TEST_HOUR = 13 TEST_MINUTE = 12 TEST_FILE_NAME = f"{TEST_YEAR}{TEST_MONTH}{TEST_DAY}{TEST_HOUR}{TEST_MINUTE}00" +TEST_FILE_NAME_MP4 = f"{TEST_YEAR}{TEST_MONTH}{TEST_DAY}{TEST_HOUR}{TEST_MINUTE}00.mp4" TEST_STREAM = "main" TEST_CHANNEL = "0" TEST_MIME_TYPE = "application/x-mpegURL" -TEST_URL = "http:test_url" +TEST_MIME_TYPE_MP4 = "video/mp4" +TEST_URL = "http:test_url&user=admin&password=test" +TEST_URL2 = "http:test_url&token=test" @pytest.fixture(autouse=True) @@ -85,18 +88,35 @@ async def test_resolve( """Test resolving Reolink media items.""" assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - - reolink_connect.get_vod_source.return_value = (TEST_MIME_TYPE, TEST_URL) caplog.set_level(logging.DEBUG) file_id = ( f"FILE|{config_entry.entry_id}|{TEST_CHANNEL}|{TEST_STREAM}|{TEST_FILE_NAME}" ) + reolink_connect.get_vod_source.return_value = (TEST_MIME_TYPE, TEST_URL) play_media = await async_resolve_media( hass, f"{URI_SCHEME}{DOMAIN}/{file_id}", None ) + assert play_media.mime_type == TEST_MIME_TYPE + file_id = f"FILE|{config_entry.entry_id}|{TEST_CHANNEL}|{TEST_STREAM}|{TEST_FILE_NAME_MP4}" + reolink_connect.get_vod_source.return_value = (TEST_MIME_TYPE_MP4, TEST_URL2) + + play_media = await async_resolve_media( + hass, f"{URI_SCHEME}{DOMAIN}/{file_id}", None + ) + assert play_media.mime_type == TEST_MIME_TYPE_MP4 + + file_id = ( + f"FILE|{config_entry.entry_id}|{TEST_CHANNEL}|{TEST_STREAM}|{TEST_FILE_NAME}" + ) + reolink_connect.get_vod_source.return_value = (TEST_MIME_TYPE, TEST_URL) + reolink_connect.is_nvr = False + + play_media = await async_resolve_media( + hass, f"{URI_SCHEME}{DOMAIN}/{file_id}", None + ) assert play_media.mime_type == TEST_MIME_TYPE