2021-01-20 13:44:24 +00:00
|
|
|
"""The tests for hls streams."""
|
2019-03-12 02:57:10 +00:00
|
|
|
from datetime import timedelta
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
2019-03-12 02:57:10 +00:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
2020-09-03 16:22:00 +00:00
|
|
|
import av
|
2021-02-15 04:32:37 +00:00
|
|
|
import pytest
|
2019-04-28 11:58:19 +00:00
|
|
|
|
2021-02-09 03:53:28 +00:00
|
|
|
from homeassistant.components.stream import create_stream
|
2021-02-15 04:32:37 +00:00
|
|
|
from homeassistant.components.stream.const import MAX_SEGMENTS, NUM_PLAYLIST_SEGMENTS
|
|
|
|
from homeassistant.components.stream.core import Segment
|
2020-04-08 22:57:47 +00:00
|
|
|
from homeassistant.const import HTTP_NOT_FOUND
|
2019-10-14 21:20:18 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2019-03-12 02:57:10 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
|
|
|
|
|
|
|
from tests.common import async_fire_time_changed
|
2021-02-09 03:53:28 +00:00
|
|
|
from tests.components.stream.common import generate_h264_video
|
2019-03-12 02:57:10 +00:00
|
|
|
|
2021-02-15 04:32:37 +00:00
|
|
|
STREAM_SOURCE = "some-stream-source"
|
2021-05-26 08:19:09 +00:00
|
|
|
INIT_BYTES = b"init"
|
|
|
|
MOOF_BYTES = b"some-bytes"
|
2021-02-15 04:32:37 +00:00
|
|
|
DURATION = 10
|
2021-03-13 09:53:26 +00:00
|
|
|
TEST_TIMEOUT = 5.0 # Lower than 9s home assistant timeout
|
|
|
|
MAX_ABORT_SEGMENTS = 20 # Abort test to avoid looping forever
|
2021-02-15 04:32:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HlsClient:
|
|
|
|
"""Test fixture for fetching the hls stream."""
|
|
|
|
|
|
|
|
def __init__(self, http_client, parsed_url):
|
|
|
|
"""Initialize HlsClient."""
|
|
|
|
self.http_client = http_client
|
|
|
|
self.parsed_url = parsed_url
|
|
|
|
|
|
|
|
async def get(self, path=None):
|
|
|
|
"""Fetch the hls stream for the specified path."""
|
|
|
|
url = self.parsed_url.path
|
|
|
|
if path:
|
|
|
|
# Strip off the master playlist suffix and replace with path
|
|
|
|
url = "/".join(self.parsed_url.path.split("/")[:-1]) + path
|
|
|
|
return await self.http_client.get(url)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def hls_stream(hass, hass_client):
|
|
|
|
"""Create test fixture for creating an HLS client for a stream."""
|
|
|
|
|
|
|
|
async def create_client_for_stream(stream):
|
|
|
|
http_client = await hass_client()
|
2021-02-20 14:49:39 +00:00
|
|
|
parsed_url = urlparse(stream.endpoint_url("hls"))
|
2021-02-15 04:32:37 +00:00
|
|
|
return HlsClient(http_client, parsed_url)
|
|
|
|
|
|
|
|
return create_client_for_stream
|
|
|
|
|
|
|
|
|
2021-02-18 12:26:02 +00:00
|
|
|
def make_segment(segment, discontinuity=False):
|
|
|
|
"""Create a playlist response for a segment."""
|
|
|
|
response = []
|
|
|
|
if discontinuity:
|
|
|
|
response.append("#EXT-X-DISCONTINUITY")
|
|
|
|
response.extend(["#EXTINF:10.0000,", f"./segment/{segment}.m4s"]),
|
|
|
|
return "\n".join(response)
|
|
|
|
|
|
|
|
|
|
|
|
def make_playlist(sequence, discontinuity_sequence=0, segments=[]):
|
2021-02-15 04:32:37 +00:00
|
|
|
"""Create a an hls playlist response for tests to assert on."""
|
|
|
|
response = [
|
|
|
|
"#EXTM3U",
|
|
|
|
"#EXT-X-VERSION:7",
|
|
|
|
"#EXT-X-TARGETDURATION:10",
|
|
|
|
'#EXT-X-MAP:URI="init.mp4"',
|
|
|
|
f"#EXT-X-MEDIA-SEQUENCE:{sequence}",
|
2021-02-18 12:26:02 +00:00
|
|
|
f"#EXT-X-DISCONTINUITY-SEQUENCE:{discontinuity_sequence}",
|
2021-02-15 04:32:37 +00:00
|
|
|
]
|
2021-02-18 12:26:02 +00:00
|
|
|
response.extend(segments)
|
2021-02-15 04:32:37 +00:00
|
|
|
response.append("")
|
|
|
|
return "\n".join(response)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_hls_stream(hass, hls_stream, stream_worker_sync):
|
2019-03-12 02:57:10 +00:00
|
|
|
"""
|
|
|
|
Test hls stream.
|
|
|
|
|
|
|
|
Purposefully not mocking anything here to test full
|
|
|
|
integration with the stream component.
|
|
|
|
"""
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_setup_component(hass, "stream", {"stream": {}})
|
2019-03-12 02:57:10 +00:00
|
|
|
|
2021-01-20 13:44:24 +00:00
|
|
|
stream_worker_sync.pause()
|
2021-01-11 13:34:45 +00:00
|
|
|
|
2019-03-12 02:57:10 +00:00
|
|
|
# Setup demo HLS track
|
|
|
|
source = generate_h264_video()
|
2021-02-09 03:53:28 +00:00
|
|
|
stream = create_stream(hass, source)
|
2019-03-12 02:57:10 +00:00
|
|
|
|
|
|
|
# Request stream
|
2021-02-20 14:49:39 +00:00
|
|
|
stream.add_provider("hls")
|
2021-02-09 03:53:28 +00:00
|
|
|
stream.start()
|
2019-03-12 02:57:10 +00:00
|
|
|
|
2021-02-15 04:32:37 +00:00
|
|
|
hls_client = await hls_stream(stream)
|
2019-03-12 02:57:10 +00:00
|
|
|
|
|
|
|
# Fetch playlist
|
2021-02-15 04:32:37 +00:00
|
|
|
playlist_response = await hls_client.get()
|
2019-03-12 02:57:10 +00:00
|
|
|
assert playlist_response.status == 200
|
|
|
|
|
2020-08-11 21:12:41 +00:00
|
|
|
# Fetch init
|
|
|
|
playlist = await playlist_response.text()
|
2021-02-15 04:32:37 +00:00
|
|
|
init_response = await hls_client.get("/init.mp4")
|
2020-08-11 21:12:41 +00:00
|
|
|
assert init_response.status == 200
|
|
|
|
|
2019-03-12 02:57:10 +00:00
|
|
|
# Fetch segment
|
|
|
|
playlist = await playlist_response.text()
|
2021-02-15 04:32:37 +00:00
|
|
|
segment_url = "/" + playlist.splitlines()[-1]
|
|
|
|
segment_response = await hls_client.get(segment_url)
|
2019-03-12 02:57:10 +00:00
|
|
|
assert segment_response.status == 200
|
|
|
|
|
2021-01-20 13:44:24 +00:00
|
|
|
stream_worker_sync.resume()
|
2021-01-11 13:34:45 +00:00
|
|
|
|
2019-03-12 02:57:10 +00:00
|
|
|
# Stop stream, if it hasn't quit already
|
|
|
|
stream.stop()
|
|
|
|
|
2020-01-31 16:33:00 +00:00
|
|
|
# Ensure playlist not accessible after stream ends
|
2021-02-15 04:32:37 +00:00
|
|
|
fail_response = await hls_client.get()
|
2020-04-08 22:57:47 +00:00
|
|
|
assert fail_response.status == HTTP_NOT_FOUND
|
2019-03-12 02:57:10 +00:00
|
|
|
|
|
|
|
|
2021-01-20 13:44:24 +00:00
|
|
|
async def test_stream_timeout(hass, hass_client, stream_worker_sync):
|
2019-03-12 02:57:10 +00:00
|
|
|
"""Test hls stream timeout."""
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_setup_component(hass, "stream", {"stream": {}})
|
2019-03-12 02:57:10 +00:00
|
|
|
|
2021-01-20 13:44:24 +00:00
|
|
|
stream_worker_sync.pause()
|
2021-01-11 13:34:45 +00:00
|
|
|
|
2019-03-12 02:57:10 +00:00
|
|
|
# Setup demo HLS track
|
|
|
|
source = generate_h264_video()
|
2021-02-09 03:53:28 +00:00
|
|
|
stream = create_stream(hass, source)
|
2019-03-12 02:57:10 +00:00
|
|
|
|
|
|
|
# Request stream
|
2021-02-20 14:49:39 +00:00
|
|
|
stream.add_provider("hls")
|
2021-02-09 03:53:28 +00:00
|
|
|
stream.start()
|
2021-02-20 14:49:39 +00:00
|
|
|
url = stream.endpoint_url("hls")
|
2019-03-12 02:57:10 +00:00
|
|
|
|
|
|
|
http_client = await hass_client()
|
|
|
|
|
|
|
|
# Fetch playlist
|
|
|
|
parsed_url = urlparse(url)
|
|
|
|
playlist_response = await http_client.get(parsed_url.path)
|
|
|
|
assert playlist_response.status == 200
|
|
|
|
|
|
|
|
# Wait a minute
|
|
|
|
future = dt_util.utcnow() + timedelta(minutes=1)
|
|
|
|
async_fire_time_changed(hass, future)
|
|
|
|
|
|
|
|
# Fetch again to reset timer
|
|
|
|
playlist_response = await http_client.get(parsed_url.path)
|
|
|
|
assert playlist_response.status == 200
|
|
|
|
|
2021-01-20 13:44:24 +00:00
|
|
|
stream_worker_sync.resume()
|
2021-01-11 13:34:45 +00:00
|
|
|
|
2019-03-12 02:57:10 +00:00
|
|
|
# Wait 5 minutes
|
|
|
|
future = dt_util.utcnow() + timedelta(minutes=5)
|
|
|
|
async_fire_time_changed(hass, future)
|
2021-02-08 15:19:41 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-03-12 02:57:10 +00:00
|
|
|
|
2020-01-31 16:33:00 +00:00
|
|
|
# Ensure playlist not accessible
|
2019-03-12 02:57:10 +00:00
|
|
|
fail_response = await http_client.get(parsed_url.path)
|
2020-04-08 22:57:47 +00:00
|
|
|
assert fail_response.status == HTTP_NOT_FOUND
|
2019-03-12 02:57:10 +00:00
|
|
|
|
|
|
|
|
2021-02-16 20:10:26 +00:00
|
|
|
async def test_stream_timeout_after_stop(hass, hass_client, stream_worker_sync):
|
|
|
|
"""Test hls stream timeout after the stream has been stopped already."""
|
|
|
|
await async_setup_component(hass, "stream", {"stream": {}})
|
|
|
|
|
|
|
|
stream_worker_sync.pause()
|
|
|
|
|
|
|
|
# Setup demo HLS track
|
|
|
|
source = generate_h264_video()
|
|
|
|
stream = create_stream(hass, source)
|
|
|
|
|
|
|
|
# Request stream
|
2021-02-20 14:49:39 +00:00
|
|
|
stream.add_provider("hls")
|
2021-02-16 20:10:26 +00:00
|
|
|
stream.start()
|
|
|
|
|
|
|
|
stream_worker_sync.resume()
|
|
|
|
stream.stop()
|
|
|
|
|
|
|
|
# Wait 5 minutes and fire callback. Stream should already have been
|
|
|
|
# stopped so this is a no-op.
|
|
|
|
future = dt_util.utcnow() + timedelta(minutes=5)
|
|
|
|
async_fire_time_changed(hass, future)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
|
2020-09-03 16:22:00 +00:00
|
|
|
async def test_stream_keepalive(hass):
|
|
|
|
"""Test hls stream retries the stream when keepalive=True."""
|
|
|
|
await async_setup_component(hass, "stream", {"stream": {}})
|
|
|
|
|
|
|
|
# Setup demo HLS track
|
|
|
|
source = "test_stream_keepalive_source"
|
2021-02-09 03:53:28 +00:00
|
|
|
stream = create_stream(hass, source)
|
2021-02-20 14:49:39 +00:00
|
|
|
track = stream.add_provider("hls")
|
2020-09-03 16:22:00 +00:00
|
|
|
track.num_segments = 2
|
|
|
|
|
|
|
|
cur_time = 0
|
|
|
|
|
|
|
|
def time_side_effect():
|
|
|
|
nonlocal cur_time
|
|
|
|
if cur_time >= 80:
|
|
|
|
stream.keepalive = False # Thread should exit and be joinable.
|
|
|
|
cur_time += 40
|
|
|
|
return cur_time
|
|
|
|
|
|
|
|
with patch("av.open") as av_open, patch(
|
2021-02-08 15:19:41 +00:00
|
|
|
"homeassistant.components.stream.time"
|
2020-11-11 17:32:56 +00:00
|
|
|
) as mock_time, patch(
|
2021-02-08 15:19:41 +00:00
|
|
|
"homeassistant.components.stream.STREAM_RESTART_INCREMENT", 0
|
2020-11-11 17:32:56 +00:00
|
|
|
):
|
2020-09-03 16:22:00 +00:00
|
|
|
av_open.side_effect = av.error.InvalidDataError(-2, "error")
|
|
|
|
mock_time.time.side_effect = time_side_effect
|
|
|
|
# Request stream
|
2021-02-09 03:53:28 +00:00
|
|
|
stream.keepalive = True
|
|
|
|
stream.start()
|
2020-09-03 16:22:00 +00:00
|
|
|
stream._thread.join()
|
|
|
|
stream._thread = None
|
|
|
|
assert av_open.call_count == 2
|
|
|
|
|
|
|
|
# Stop stream, if it hasn't quit already
|
|
|
|
stream.stop()
|
2021-02-15 04:32:37 +00:00
|
|
|
|
|
|
|
|
2021-02-20 14:49:39 +00:00
|
|
|
async def test_hls_playlist_view_no_output(hass, hass_client, hls_stream):
|
2021-02-15 04:32:37 +00:00
|
|
|
"""Test rendering the hls playlist with no output segments."""
|
|
|
|
await async_setup_component(hass, "stream", {"stream": {}})
|
|
|
|
|
|
|
|
stream = create_stream(hass, STREAM_SOURCE)
|
2021-02-20 14:49:39 +00:00
|
|
|
stream.add_provider("hls")
|
2021-02-15 04:32:37 +00:00
|
|
|
|
|
|
|
hls_client = await hls_stream(stream)
|
|
|
|
|
|
|
|
# Fetch playlist
|
|
|
|
resp = await hls_client.get("/playlist.m3u8")
|
|
|
|
assert resp.status == 404
|
|
|
|
|
|
|
|
|
|
|
|
async def test_hls_playlist_view(hass, hls_stream, stream_worker_sync):
|
|
|
|
"""Test rendering the hls playlist with 1 and 2 output segments."""
|
|
|
|
await async_setup_component(hass, "stream", {"stream": {}})
|
|
|
|
|
|
|
|
stream = create_stream(hass, STREAM_SOURCE)
|
|
|
|
stream_worker_sync.pause()
|
2021-02-20 14:49:39 +00:00
|
|
|
hls = stream.add_provider("hls")
|
2021-02-15 04:32:37 +00:00
|
|
|
|
2021-05-26 08:19:09 +00:00
|
|
|
hls.put(Segment(1, INIT_BYTES, MOOF_BYTES, DURATION))
|
2021-02-15 04:32:37 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
hls_client = await hls_stream(stream)
|
|
|
|
|
|
|
|
resp = await hls_client.get("/playlist.m3u8")
|
|
|
|
assert resp.status == 200
|
2021-02-18 12:26:02 +00:00
|
|
|
assert await resp.text() == make_playlist(sequence=1, segments=[make_segment(1)])
|
2021-02-15 04:32:37 +00:00
|
|
|
|
2021-05-26 08:19:09 +00:00
|
|
|
hls.put(Segment(2, INIT_BYTES, MOOF_BYTES, DURATION))
|
2021-02-15 04:32:37 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
resp = await hls_client.get("/playlist.m3u8")
|
|
|
|
assert resp.status == 200
|
2021-02-18 12:26:02 +00:00
|
|
|
assert await resp.text() == make_playlist(
|
|
|
|
sequence=1, segments=[make_segment(1), make_segment(2)]
|
|
|
|
)
|
2021-02-15 04:32:37 +00:00
|
|
|
|
|
|
|
stream_worker_sync.resume()
|
|
|
|
stream.stop()
|
|
|
|
|
|
|
|
|
|
|
|
async def test_hls_max_segments(hass, hls_stream, stream_worker_sync):
|
|
|
|
"""Test rendering the hls playlist with more segments than the segment deque can hold."""
|
|
|
|
await async_setup_component(hass, "stream", {"stream": {}})
|
|
|
|
|
|
|
|
stream = create_stream(hass, STREAM_SOURCE)
|
|
|
|
stream_worker_sync.pause()
|
2021-02-20 14:49:39 +00:00
|
|
|
hls = stream.add_provider("hls")
|
2021-02-15 04:32:37 +00:00
|
|
|
|
|
|
|
hls_client = await hls_stream(stream)
|
|
|
|
|
|
|
|
# Produce enough segments to overfill the output buffer by one
|
|
|
|
for sequence in range(1, MAX_SEGMENTS + 2):
|
2021-05-26 08:19:09 +00:00
|
|
|
hls.put(Segment(sequence, INIT_BYTES, MOOF_BYTES, DURATION))
|
2021-02-15 04:32:37 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
resp = await hls_client.get("/playlist.m3u8")
|
|
|
|
assert resp.status == 200
|
|
|
|
|
|
|
|
# Only NUM_PLAYLIST_SEGMENTS are returned in the playlist.
|
|
|
|
start = MAX_SEGMENTS + 2 - NUM_PLAYLIST_SEGMENTS
|
2021-02-18 12:26:02 +00:00
|
|
|
segments = []
|
|
|
|
for sequence in range(start, MAX_SEGMENTS + 2):
|
|
|
|
segments.append(make_segment(sequence))
|
|
|
|
assert await resp.text() == make_playlist(
|
|
|
|
sequence=start,
|
|
|
|
segments=segments,
|
2021-02-15 04:32:37 +00:00
|
|
|
)
|
|
|
|
|
2021-05-26 08:19:09 +00:00
|
|
|
# The segment that fell off the buffer is not accessible
|
|
|
|
segment_response = await hls_client.get("/segment/1.m4s")
|
|
|
|
assert segment_response.status == 404
|
2021-02-15 04:32:37 +00:00
|
|
|
|
2021-05-26 08:19:09 +00:00
|
|
|
# However all segments in the buffer are accessible, even those that were not in the playlist.
|
|
|
|
for sequence in range(2, MAX_SEGMENTS + 2):
|
|
|
|
segment_response = await hls_client.get(f"/segment/{sequence}.m4s")
|
|
|
|
assert segment_response.status == 200
|
2021-02-15 04:32:37 +00:00
|
|
|
|
|
|
|
stream_worker_sync.resume()
|
|
|
|
stream.stop()
|
2021-02-18 12:26:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_hls_playlist_view_discontinuity(hass, hls_stream, stream_worker_sync):
|
|
|
|
"""Test a discontinuity across segments in the stream with 3 segments."""
|
|
|
|
await async_setup_component(hass, "stream", {"stream": {}})
|
|
|
|
|
|
|
|
stream = create_stream(hass, STREAM_SOURCE)
|
|
|
|
stream_worker_sync.pause()
|
2021-02-20 14:49:39 +00:00
|
|
|
hls = stream.add_provider("hls")
|
2021-02-18 12:26:02 +00:00
|
|
|
|
2021-05-26 08:19:09 +00:00
|
|
|
hls.put(Segment(1, INIT_BYTES, MOOF_BYTES, DURATION, stream_id=0))
|
|
|
|
hls.put(Segment(2, INIT_BYTES, MOOF_BYTES, DURATION, stream_id=0))
|
|
|
|
hls.put(Segment(3, INIT_BYTES, MOOF_BYTES, DURATION, stream_id=1))
|
2021-02-18 12:26:02 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
hls_client = await hls_stream(stream)
|
|
|
|
|
|
|
|
resp = await hls_client.get("/playlist.m3u8")
|
|
|
|
assert resp.status == 200
|
|
|
|
assert await resp.text() == make_playlist(
|
|
|
|
sequence=1,
|
|
|
|
segments=[
|
|
|
|
make_segment(1),
|
|
|
|
make_segment(2),
|
|
|
|
make_segment(3, discontinuity=True),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
stream_worker_sync.resume()
|
|
|
|
stream.stop()
|
|
|
|
|
|
|
|
|
|
|
|
async def test_hls_max_segments_discontinuity(hass, hls_stream, stream_worker_sync):
|
|
|
|
"""Test a discontinuity with more segments than the segment deque can hold."""
|
|
|
|
await async_setup_component(hass, "stream", {"stream": {}})
|
|
|
|
|
|
|
|
stream = create_stream(hass, STREAM_SOURCE)
|
|
|
|
stream_worker_sync.pause()
|
2021-02-20 14:49:39 +00:00
|
|
|
hls = stream.add_provider("hls")
|
2021-02-18 12:26:02 +00:00
|
|
|
|
|
|
|
hls_client = await hls_stream(stream)
|
|
|
|
|
2021-05-26 08:19:09 +00:00
|
|
|
hls.put(Segment(1, INIT_BYTES, MOOF_BYTES, DURATION, stream_id=0))
|
2021-02-18 12:26:02 +00:00
|
|
|
|
|
|
|
# Produce enough segments to overfill the output buffer by one
|
|
|
|
for sequence in range(1, MAX_SEGMENTS + 2):
|
2021-05-26 08:19:09 +00:00
|
|
|
hls.put(Segment(sequence, INIT_BYTES, MOOF_BYTES, DURATION, stream_id=1))
|
2021-02-18 12:26:02 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
resp = await hls_client.get("/playlist.m3u8")
|
|
|
|
assert resp.status == 200
|
|
|
|
|
|
|
|
# Only NUM_PLAYLIST_SEGMENTS are returned in the playlist causing the
|
|
|
|
# EXT-X-DISCONTINUITY tag to be omitted and EXT-X-DISCONTINUITY-SEQUENCE
|
|
|
|
# returned instead.
|
|
|
|
start = MAX_SEGMENTS + 2 - NUM_PLAYLIST_SEGMENTS
|
|
|
|
segments = []
|
|
|
|
for sequence in range(start, MAX_SEGMENTS + 2):
|
|
|
|
segments.append(make_segment(sequence))
|
|
|
|
assert await resp.text() == make_playlist(
|
|
|
|
sequence=start,
|
|
|
|
discontinuity_sequence=1,
|
|
|
|
segments=segments,
|
|
|
|
)
|
|
|
|
|
|
|
|
stream_worker_sync.resume()
|
|
|
|
stream.stop()
|