Live Streams Component (#21473)

* initial commit of streams

* refactor stream component

* refactor so stream formats are not considered a platform

* initial test and minor refactor

* fix linting

* update requirements

* need av in tests as well

* fix import in class def vs method

* fix travis and docker builds

* address code review comments

* fix logger, add stream start/stop logs, listen to HASS stop

* address additional code review comments

* beef up tests

* fix tests

* fix lint

* add stream_source to onvif camera

* address pr comments

* add keepalive to camera play_stream service

* remove keepalive and move import

* implement registry and have output provider remove itself from stream after idle, set libav log level to error
This commit is contained in:
Jason Hunter 2019-03-11 22:57:10 -04:00 committed by Paulus Schoutsen
parent 0a6ba14444
commit 7ccd0bba9a
18 changed files with 993 additions and 5 deletions

View file

@ -1,12 +1,12 @@
"""The tests for the camera component."""
import asyncio
import base64
from unittest.mock import patch, mock_open
from unittest.mock import patch, mock_open, PropertyMock
import pytest
from homeassistant.setup import setup_component, async_setup_component
from homeassistant.const import ATTR_ENTITY_PICTURE
from homeassistant.const import (ATTR_ENTITY_ID, ATTR_ENTITY_PICTURE)
from homeassistant.components import camera, http
from homeassistant.components.websocket_api.const import TYPE_RESULT
from homeassistant.exceptions import HomeAssistantError
@ -16,6 +16,7 @@ from tests.common import (
get_test_home_assistant, get_test_instance_port, assert_setup_component,
mock_coro)
from tests.components.camera import common
from tests.components.stream.common import generate_h264_video
@pytest.fixture
@ -32,6 +33,14 @@ def mock_camera(hass):
yield
@pytest.fixture
def mock_stream(hass):
"""Initialize a demo camera platform with streaming."""
assert hass.loop.run_until_complete(async_setup_component(hass, 'stream', {
'stream': {}
}))
class TestSetupCamera:
"""Test class for setup camera."""
@ -156,3 +165,88 @@ async def test_webocket_camera_thumbnail(hass, hass_ws_client, mock_camera):
assert msg['result']['content_type'] == 'image/jpeg'
assert msg['result']['content'] == \
base64.b64encode(b'Test').decode('utf-8')
async def test_webocket_stream_no_source(hass, hass_ws_client,
mock_camera, mock_stream):
"""Test camera/stream websocket command."""
await async_setup_component(hass, 'camera')
with patch('homeassistant.components.camera.request_stream',
return_value='http://home.assistant/playlist.m3u8') \
as mock_request_stream:
# Request playlist through WebSocket
client = await hass_ws_client(hass)
await client.send_json({
'id': 6,
'type': 'camera/stream',
'entity_id': 'camera.demo_camera',
})
msg = await client.receive_json()
# Assert WebSocket response
assert not mock_request_stream.called
assert msg['id'] == 6
assert msg['type'] == TYPE_RESULT
assert not msg['success']
async def test_webocket_camera_stream(hass, hass_ws_client, hass_client,
mock_camera, mock_stream):
"""Test camera/stream websocket command."""
await async_setup_component(hass, 'camera')
with patch('homeassistant.components.camera.request_stream',
return_value='http://home.assistant/playlist.m3u8'
) as mock_request_stream, \
patch('homeassistant.components.camera.demo.DemoCamera.stream_source',
new_callable=PropertyMock) as mock_stream_source:
mock_stream_source.return_value = generate_h264_video()
# Request playlist through WebSocket
client = await hass_ws_client(hass)
await client.send_json({
'id': 6,
'type': 'camera/stream',
'entity_id': 'camera.demo_camera',
})
msg = await client.receive_json()
# Assert WebSocket response
assert mock_request_stream.called
assert msg['id'] == 6
assert msg['type'] == TYPE_RESULT
assert msg['success']
assert msg['result']['url'][-13:] == 'playlist.m3u8'
async def test_play_stream_service_no_source(hass, mock_camera, mock_stream):
"""Test camera play_stream service."""
data = {
ATTR_ENTITY_ID: 'camera.demo_camera',
camera.ATTR_MEDIA_PLAYER: 'media_player.test'
}
with patch('homeassistant.components.camera.request_stream'), \
pytest.raises(HomeAssistantError):
# Call service
await hass.services.async_call(
camera.DOMAIN, camera.SERVICE_PLAY_STREAM, data, blocking=True)
async def test_handle_play_stream_service(hass, mock_camera, mock_stream):
"""Test camera play_stream service."""
await async_setup_component(hass, 'media_player')
data = {
ATTR_ENTITY_ID: 'camera.demo_camera',
camera.ATTR_MEDIA_PLAYER: 'media_player.test'
}
with patch('homeassistant.components.camera.request_stream'
) as mock_request_stream, \
patch('homeassistant.components.camera.demo.DemoCamera.stream_source',
new_callable=PropertyMock) as mock_stream_source:
mock_stream_source.return_value = generate_h264_video()
# Call service
await hass.services.async_call(
camera.DOMAIN, camera.SERVICE_PLAY_STREAM, data, blocking=True)
# So long as we request the stream, the rest should be covered
# by the play_media service tests.
assert mock_request_stream.called

View file

@ -0,0 +1 @@
"""The tests for stream platforms."""

View file

@ -0,0 +1,63 @@
"""Collection of test helpers."""
import io
from homeassistant.components.stream import Stream
from homeassistant.components.stream.const import (
DOMAIN, ATTR_STREAMS)
def generate_h264_video():
"""
Generate a test video.
See: http://docs.mikeboers.com/pyav/develop/cookbook/numpy.html
"""
import numpy as np
import av
duration = 5
fps = 24
total_frames = duration * fps
output = io.BytesIO()
output.name = 'test.ts'
container = av.open(output, mode='w')
stream = container.add_stream('libx264', rate=fps)
stream.width = 480
stream.height = 320
stream.pix_fmt = 'yuv420p'
for frame_i in range(total_frames):
img = np.empty((480, 320, 3))
img[:, :, 0] = 0.5 + 0.5 * np.sin(
2 * np.pi * (0 / 3 + frame_i / total_frames))
img[:, :, 1] = 0.5 + 0.5 * np.sin(
2 * np.pi * (1 / 3 + frame_i / total_frames))
img[:, :, 2] = 0.5 + 0.5 * np.sin(
2 * np.pi * (2 / 3 + frame_i / total_frames))
img = np.round(255 * img).astype(np.uint8)
img = np.clip(img, 0, 255)
frame = av.VideoFrame.from_ndarray(img, format='rgb24')
for packet in stream.encode(frame):
container.mux(packet)
# Flush stream
for packet in stream.encode():
container.mux(packet)
# Close the file
container.close()
output.seek(0)
return output
def preload_stream(hass, stream_source):
"""Preload a stream for use in tests."""
stream = Stream(hass, stream_source)
hass.data[DOMAIN][ATTR_STREAMS][stream_source] = stream
return stream

View file

@ -0,0 +1,117 @@
"""The tests for hls streams."""
from datetime import timedelta
from urllib.parse import urlparse
from homeassistant.setup import async_setup_component
from homeassistant.components.stream import request_stream
import homeassistant.util.dt as dt_util
from tests.common import async_fire_time_changed
from tests.components.stream.common import (
generate_h264_video, preload_stream)
async def test_hls_stream(hass, hass_client):
"""
Test hls stream.
Purposefully not mocking anything here to test full
integration with the stream component.
"""
await async_setup_component(hass, 'stream', {
'stream': {}
})
# Setup demo HLS track
source = generate_h264_video()
stream = preload_stream(hass, source)
stream.add_provider('hls')
# Request stream
url = request_stream(hass, source)
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
# Fetch segment
playlist = await playlist_response.text()
playlist_url = '/'.join(parsed_url.path.split('/')[:-1])
segment_url = playlist_url + playlist.splitlines()[-1][1:]
segment_response = await http_client.get(segment_url)
assert segment_response.status == 200
# Stop stream, if it hasn't quit already
stream.stop()
# Ensure playlist not accessable after stream ends
fail_response = await http_client.get(parsed_url.path)
assert fail_response.status == 404
async def test_stream_timeout(hass, hass_client):
"""Test hls stream timeout."""
await async_setup_component(hass, 'stream', {
'stream': {}
})
# Setup demo HLS track
source = generate_h264_video()
stream = preload_stream(hass, source)
stream.add_provider('hls')
# Request stream
url = request_stream(hass, source)
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
# Wait 5 minutes
future = dt_util.utcnow() + timedelta(minutes=5)
async_fire_time_changed(hass, future)
# Ensure playlist not accessable
fail_response = await http_client.get(parsed_url.path)
assert fail_response.status == 404
async def test_stream_ended(hass):
"""Test hls stream packets ended."""
await async_setup_component(hass, 'stream', {
'stream': {}
})
# Setup demo HLS track
source = generate_h264_video()
stream = preload_stream(hass, source)
track = stream.add_provider('hls')
track.num_segments = 2
# Request stream
request_stream(hass, source)
# Run it dead
segments = 0
while await track.recv() is not None:
segments += 1
assert segments == 3
assert not track.get_segment()
# Stop stream, if it hasn't quit already
stream.stop()