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>
This commit is contained in:
starkillerOG 2024-06-14 21:38:53 +02:00 committed by GitHub
parent c2e31e9846
commit 2639336ab0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 7 deletions

View File

@ -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")

View File

@ -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