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:
1 parent
0a6ba14444
commit
7ccd0bba9a
18 files changed
+993
-5
No files matched your search
@@ -0,0 +1,153 @@
|
||||
"""
|
||||
Provide functionality to stream video source.
|
||||
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/stream/
|
||||
"""
|
||||
import logging
|
||||
import threading
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.auth.util import generate_secret
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.loader import bind_hass
|
||||
|
||||
from .const import DOMAIN, ATTR_STREAMS, ATTR_ENDPOINTS
|
||||
from .core import PROVIDERS
|
||||
from .worker import stream_worker
|
||||
from .hls import async_setup_hls
|
||||
|
||||
REQUIREMENTS = ['av==6.1.2']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEPENDENCIES = ['http']
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: vol.Schema({}),
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
# Set log level to error for libav
|
||||
logging.getLogger('libav').setLevel(logging.ERROR)
|
||||
|
||||
|
||||
@bind_hass
|
||||
def request_stream(hass, stream_source, *, fmt='hls',
|
||||
keepalive=False, options=None):
|
||||
"""Set up stream with token."""
|
||||
if DOMAIN not in hass.config.components:
|
||||
raise HomeAssistantError("Stream component is not set up.")
|
||||
|
||||
if options is None:
|
||||
options = {}
|
||||
|
||||
try:
|
||||
streams = hass.data[DOMAIN][ATTR_STREAMS]
|
||||
stream = streams.get(stream_source)
|
||||
if not stream:
|
||||
stream = Stream(hass, stream_source,
|
||||
options=options, keepalive=keepalive)
|
||||
streams[stream_source] = stream
|
||||
|
||||
# Add provider
|
||||
stream.add_provider(fmt)
|
||||
|
||||
if not stream.access_token:
|
||||
stream.access_token = generate_secret()
|
||||
stream.start()
|
||||
return hass.data[DOMAIN][ATTR_ENDPOINTS][fmt].format(
|
||||
hass.config.api.base_url, stream.access_token)
|
||||
except Exception:
|
||||
raise HomeAssistantError('Unable to get stream')
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Set up stream."""
|
||||
hass.data[DOMAIN] = {}
|
||||
hass.data[DOMAIN][ATTR_ENDPOINTS] = {}
|
||||
hass.data[DOMAIN][ATTR_STREAMS] = {}
|
||||
|
||||
# Setup HLS
|
||||
hls_endpoint = async_setup_hls(hass)
|
||||
hass.data[DOMAIN][ATTR_ENDPOINTS]['hls'] = hls_endpoint
|
||||
|
||||
@callback
|
||||
def shutdown(event):
|
||||
"""Stop all stream workers."""
|
||||
for stream in hass.data[DOMAIN][ATTR_STREAMS].values():
|
||||
stream.keepalive = False
|
||||
stream.stop()
|
||||
_LOGGER.info("Stopped stream workers.")
|
||||
|
||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class Stream:
|
||||
"""Represents a single stream."""
|
||||
|
||||
def __init__(self, hass, source, options=None, keepalive=False):
|
||||
"""Initialize a stream."""
|
||||
self.hass = hass
|
||||
self.source = source
|
||||
self.options = options
|
||||
self.keepalive = keepalive
|
||||
self.access_token = None
|
||||
self._thread = None
|
||||
self._thread_quit = None
|
||||
self._outputs = {}
|
||||
|
||||
if self.options is None:
|
||||
self.options = {}
|
||||
|
||||
@property
|
||||
def outputs(self):
|
||||
"""Return stream outputs."""
|
||||
return self._outputs
|
||||
|
||||
def add_provider(self, fmt):
|
||||
"""Add provider output stream."""
|
||||
provider = PROVIDERS[fmt](self)
|
||||
if not self._outputs.get(provider.format):
|
||||
self._outputs[provider.format] = provider
|
||||
return self._outputs[provider.format]
|
||||
|
||||
def remove_provider(self, provider):
|
||||
"""Remove provider output stream."""
|
||||
if provider.format in self._outputs:
|
||||
del self._outputs[provider.format]
|
||||
|
||||
if not self._outputs:
|
||||
self.stop()
|
||||
|
||||
def start(self):
|
||||
"""Start a stream."""
|
||||
if self._thread is None or not self._thread.isAlive():
|
||||
self._thread_quit = threading.Event()
|
||||
self._thread = threading.Thread(
|
||||
name='stream_worker',
|
||||
target=stream_worker,
|
||||
args=(
|
||||
self.hass, self, self._thread_quit))
|
||||
self._thread.start()
|
||||
_LOGGER.info("Started stream: %s", self.source)
|
||||
|
||||
def stop(self):
|
||||
"""Remove outputs and access token."""
|
||||
self._outputs = {}
|
||||
self.access_token = None
|
||||
|
||||
if not self.keepalive:
|
||||
self._stop()
|
||||
|
||||
def _stop(self):
|
||||
"""Stop worker thread."""
|
||||
if self._thread is not None:
|
||||
self._thread_quit.set()
|
||||
self._thread.join()
|
||||
self._thread = None
|
||||
_LOGGER.info("Stopped stream: %s", self.source)
|
||||
@@ -0,0 +1,14 @@
|
||||
"""Constants for Stream component."""
|
||||
DOMAIN = 'stream'
|
||||
|
||||
ATTR_ENDPOINTS = 'endpoints'
|
||||
ATTR_STREAMS = 'streams'
|
||||
ATTR_KEEPALIVE = 'keepalive'
|
||||
|
||||
OUTPUT_FORMATS = ['hls']
|
||||
|
||||
FORMAT_CONTENT_TYPE = {
|
||||
'hls': 'application/vnd.apple.mpegurl'
|
||||
}
|
||||
|
||||
AUDIO_SAMPLE_RATE = 44100
|
||||
@@ -0,0 +1,162 @@
|
||||
"""Provides core stream functionality."""
|
||||
import asyncio
|
||||
from collections import deque
|
||||
import io
|
||||
from typing import List, Any
|
||||
|
||||
import attr
|
||||
from aiohttp import web
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.components.http import HomeAssistantView
|
||||
from homeassistant.helpers.event import async_call_later
|
||||
from homeassistant.util.decorator import Registry
|
||||
|
||||
from .const import DOMAIN, ATTR_STREAMS
|
||||
|
||||
PROVIDERS = Registry()
|
||||
|
||||
|
||||
@attr.s
|
||||
class StreamBuffer:
|
||||
"""Represent a segment."""
|
||||
|
||||
segment = attr.ib(type=io.BytesIO)
|
||||
output = attr.ib() # type=av.OutputContainer
|
||||
vstream = attr.ib() # type=av.VideoStream
|
||||
astream = attr.ib(default=None) # type=av.AudioStream
|
||||
|
||||
|
||||
@attr.s
|
||||
class Segment:
|
||||
"""Represent a segment."""
|
||||
|
||||
sequence = attr.ib(type=int)
|
||||
segment = attr.ib(type=io.BytesIO)
|
||||
duration = attr.ib(type=float)
|
||||
|
||||
|
||||
class StreamOutput:
|
||||
"""Represents a stream output."""
|
||||
|
||||
num_segments = 3
|
||||
|
||||
def __init__(self, stream) -> None:
|
||||
"""Initialize a stream output."""
|
||||
self._stream = stream
|
||||
self._cursor = None
|
||||
self._event = asyncio.Event()
|
||||
self._segments = deque(maxlen=self.num_segments)
|
||||
self._unsub = None
|
||||
|
||||
@property
|
||||
def format(self) -> str:
|
||||
"""Return container format."""
|
||||
return None
|
||||
|
||||
@property
|
||||
def audio_codec(self) -> str:
|
||||
"""Return desired audio codec."""
|
||||
return None
|
||||
|
||||
@property
|
||||
def video_codec(self) -> str:
|
||||
"""Return desired video codec."""
|
||||
return None
|
||||
|
||||
@property
|
||||
def segments(self) -> List[int]:
|
||||
"""Return current sequence from segments."""
|
||||
return [s.sequence for s in self._segments]
|
||||
|
||||
@property
|
||||
def target_duration(self) -> int:
|
||||
"""Return the average duration of the segments in seconds."""
|
||||
durations = [s.duration for s in self._segments]
|
||||
return round(sum(durations) // len(self._segments)) or 1
|
||||
|
||||
def get_segment(self, sequence: int = None) -> Any:
|
||||
"""Retrieve a specific segment, or the whole list."""
|
||||
# Reset idle timeout
|
||||
if self._unsub is not None:
|
||||
self._unsub()
|
||||
self._unsub = async_call_later(self._stream.hass, 300, self._cleanup)
|
||||
|
||||
if not sequence:
|
||||
return self._segments
|
||||
|
||||
for segment in self._segments:
|
||||
if segment.sequence == sequence:
|
||||
return segment
|
||||
return None
|
||||
|
||||
async def recv(self) -> Segment:
|
||||
"""Wait for and retrieve the latest segment."""
|
||||
last_segment = max(self.segments, default=0)
|
||||
if self._cursor is None or self._cursor <= last_segment:
|
||||
await self._event.wait()
|
||||
|
||||
if not self._segments:
|
||||
return None
|
||||
|
||||
segment = self.get_segment()[-1]
|
||||
self._cursor = segment.sequence
|
||||
return segment
|
||||
|
||||
@callback
|
||||
def put(self, segment: Segment) -> None:
|
||||
"""Store output."""
|
||||
# Start idle timeout when we start recieving data
|
||||
if self._unsub is None:
|
||||
self._unsub = async_call_later(
|
||||
self._stream.hass, 300, self._cleanup)
|
||||
|
||||
if segment is None:
|
||||
self._event.set()
|
||||
# Cleanup provider
|
||||
if self._unsub is not None:
|
||||
self._unsub()
|
||||
self._cleanup()
|
||||
return
|
||||
|
||||
self._segments.append(segment)
|
||||
self._event.set()
|
||||
self._event.clear()
|
||||
|
||||
@callback
|
||||
def _cleanup(self, _now=None):
|
||||
"""Remove provider."""
|
||||
self._segments = []
|
||||
self._stream.remove_provider(self)
|
||||
|
||||
|
||||
class StreamView(HomeAssistantView):
|
||||
"""
|
||||
Base StreamView.
|
||||
|
||||
For implementation of a new stream format, define `url` and `name`
|
||||
attributes, and implement `handle` method in a child class.
|
||||
"""
|
||||
|
||||
requires_auth = False
|
||||
platform = None
|
||||
|
||||
async def get(self, request, token, sequence=None):
|
||||
"""Start a GET request."""
|
||||
hass = request.app['hass']
|
||||
|
||||
stream = next((
|
||||
s for s in hass.data[DOMAIN][ATTR_STREAMS].values()
|
||||
if s.access_token == token), None)
|
||||
|
||||
if not stream:
|
||||
raise web.HTTPNotFound()
|
||||
|
||||
# Start worker if not already started
|
||||
stream.start()
|
||||
|
||||
return await self.handle(request, stream, sequence)
|
||||
|
||||
async def handle(self, request, stream, sequence):
|
||||
"""Handle the stream request."""
|
||||
raise NotImplementedError()
|
||||
@@ -0,0 +1,126 @@
|
||||
"""
|
||||
Provide functionality to stream HLS.
|
||||
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/stream/hls
|
||||
"""
|
||||
from aiohttp import web
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from .const import FORMAT_CONTENT_TYPE
|
||||
from .core import StreamView, StreamOutput, PROVIDERS
|
||||
|
||||
|
||||
@callback
|
||||
def async_setup_hls(hass):
|
||||
"""Set up api endpoints."""
|
||||
hass.http.register_view(HlsPlaylistView())
|
||||
hass.http.register_view(HlsSegmentView())
|
||||
return '{}/api/hls/{}/playlist.m3u8'
|
||||
|
||||
|
||||
class HlsPlaylistView(StreamView):
|
||||
"""Stream view to serve a M3U8 stream."""
|
||||
|
||||
url = r'/api/hls/{token:[a-f0-9]+}/playlist.m3u8'
|
||||
name = 'api:stream:hls:playlist'
|
||||
cors_allowed = True
|
||||
|
||||
async def handle(self, request, stream, sequence):
|
||||
"""Return m3u8 playlist."""
|
||||
renderer = M3U8Renderer(stream)
|
||||
track = stream.add_provider('hls')
|
||||
stream.start()
|
||||
# Wait for a segment to be ready
|
||||
if not track.segments:
|
||||
await track.recv()
|
||||
headers = {
|
||||
'Content-Type': FORMAT_CONTENT_TYPE['hls']
|
||||
}
|
||||
return web.Response(body=renderer.render(
|
||||
track, utcnow()).encode("utf-8"), headers=headers)
|
||||
|
||||
|
||||
class HlsSegmentView(StreamView):
|
||||
"""Stream view to serve a MPEG2TS segment."""
|
||||
|
||||
url = r'/api/hls/{token:[a-f0-9]+}/segment/{sequence:\d+}.ts'
|
||||
name = 'api:stream:hls:segment'
|
||||
cors_allowed = True
|
||||
|
||||
async def handle(self, request, stream, sequence):
|
||||
"""Return mpegts segment."""
|
||||
track = stream.add_provider('hls')
|
||||
segment = track.get_segment(int(sequence))
|
||||
if not segment:
|
||||
return web.HTTPNotFound()
|
||||
headers = {
|
||||
'Content-Type': 'video/mp2t'
|
||||
}
|
||||
return web.Response(body=segment.segment.getvalue(), headers=headers)
|
||||
|
||||
|
||||
class M3U8Renderer:
|
||||
"""M3U8 Render Helper."""
|
||||
|
||||
def __init__(self, stream):
|
||||
"""Initialize renderer."""
|
||||
self.stream = stream
|
||||
|
||||
@staticmethod
|
||||
def render_preamble(track):
|
||||
"""Render preamble."""
|
||||
return [
|
||||
"#EXT-X-VERSION:3",
|
||||
"#EXT-X-TARGETDURATION:{}".format(track.target_duration),
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def render_playlist(track, start_time):
|
||||
"""Render playlist."""
|
||||
segments = track.segments
|
||||
|
||||
if not segments:
|
||||
return []
|
||||
|
||||
playlist = ["#EXT-X-MEDIA-SEQUENCE:{}".format(segments[0])]
|
||||
|
||||
for sequence in segments:
|
||||
segment = track.get_segment(sequence)
|
||||
playlist.extend([
|
||||
"#EXTINF:{:.04},".format(float(segment.duration)),
|
||||
"./segment/{}.ts".format(segment.sequence),
|
||||
])
|
||||
|
||||
return playlist
|
||||
|
||||
def render(self, track, start_time):
|
||||
"""Render M3U8 file."""
|
||||
lines = (
|
||||
["#EXTM3U"] +
|
||||
self.render_preamble(track) +
|
||||
self.render_playlist(track, start_time)
|
||||
)
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
|
||||
@PROVIDERS.register('hls')
|
||||
class HlsStreamOutput(StreamOutput):
|
||||
"""Represents HLS Output formats."""
|
||||
|
||||
@property
|
||||
def format(self) -> str:
|
||||
"""Return container format."""
|
||||
return 'mpegts'
|
||||
|
||||
@property
|
||||
def audio_codec(self) -> str:
|
||||
"""Return desired audio codec."""
|
||||
return 'aac'
|
||||
|
||||
@property
|
||||
def video_codec(self) -> str:
|
||||
"""Return desired video codec."""
|
||||
return 'h264'
|
||||
@@ -0,0 +1,142 @@
|
||||
"""Provides the worker thread needed for processing streams."""
|
||||
from fractions import Fraction
|
||||
import io
|
||||
import logging
|
||||
|
||||
from .const import AUDIO_SAMPLE_RATE
|
||||
from .core import Segment, StreamBuffer
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def generate_audio_frame():
|
||||
"""Generate a blank audio frame."""
|
||||
from av import AudioFrame
|
||||
audio_frame = AudioFrame(format='dbl', layout='mono', samples=1024)
|
||||
# audio_bytes = b''.join(b'\x00\x00\x00\x00\x00\x00\x00\x00'
|
||||
# for i in range(0, 1024))
|
||||
audio_bytes = b'\x00\x00\x00\x00\x00\x00\x00\x00' * 1024
|
||||
audio_frame.planes[0].update(audio_bytes)
|
||||
audio_frame.sample_rate = AUDIO_SAMPLE_RATE
|
||||
audio_frame.time_base = Fraction(1, AUDIO_SAMPLE_RATE)
|
||||
return audio_frame
|
||||
|
||||
|
||||
def create_stream_buffer(stream_output, video_stream, audio_frame):
|
||||
"""Create a new StreamBuffer."""
|
||||
import av
|
||||
a_packet = None
|
||||
segment = io.BytesIO()
|
||||
output = av.open(
|
||||
segment, mode='w', format=stream_output.format)
|
||||
vstream = output.add_stream(
|
||||
stream_output.video_codec, video_stream.rate)
|
||||
# Fix format
|
||||
vstream.codec_context.format = \
|
||||
video_stream.codec_context.format
|
||||
# Check if audio is requested
|
||||
astream = None
|
||||
if stream_output.audio_codec:
|
||||
astream = output.add_stream(
|
||||
stream_output.audio_codec, AUDIO_SAMPLE_RATE)
|
||||
# Need to do it multiple times for some reason
|
||||
while not a_packet:
|
||||
a_packets = astream.encode(audio_frame)
|
||||
if a_packets:
|
||||
a_packet = a_packets[0]
|
||||
return (a_packet, StreamBuffer(segment, output, vstream, astream))
|
||||
|
||||
|
||||
def stream_worker(hass, stream, quit_event):
|
||||
"""Handle consuming streams."""
|
||||
import av
|
||||
container = av.open(stream.source, options=stream.options)
|
||||
try:
|
||||
video_stream = container.streams.video[0]
|
||||
except (KeyError, IndexError):
|
||||
_LOGGER.error("Stream has no video")
|
||||
return
|
||||
|
||||
audio_frame = generate_audio_frame()
|
||||
|
||||
outputs = {}
|
||||
first_packet = True
|
||||
sequence = 1
|
||||
audio_packets = {}
|
||||
|
||||
while not quit_event.is_set():
|
||||
try:
|
||||
packet = next(container.demux(video_stream))
|
||||
if packet.dts is None:
|
||||
# If we get a "flushing" packet, the stream is done
|
||||
raise StopIteration
|
||||
except (av.AVError, StopIteration) as ex:
|
||||
# End of stream, clear listeners and stop thread
|
||||
for fmt, _ in outputs.items():
|
||||
hass.loop.call_soon_threadsafe(
|
||||
stream.outputs[fmt].put, None)
|
||||
_LOGGER.error("Error demuxing stream: %s", ex)
|
||||
break
|
||||
|
||||
# Reset segment on every keyframe
|
||||
if packet.is_keyframe:
|
||||
# Save segment to outputs
|
||||
segment_duration = (packet.pts * packet.time_base) / sequence
|
||||
for fmt, buffer in outputs.items():
|
||||
buffer.output.close()
|
||||
del audio_packets[buffer.astream]
|
||||
if stream.outputs.get(fmt):
|
||||
hass.loop.call_soon_threadsafe(
|
||||
stream.outputs[fmt].put, Segment(
|
||||
sequence, buffer.segment, segment_duration
|
||||
))
|
||||
|
||||
# Clear outputs and increment sequence
|
||||
outputs = {}
|
||||
if not first_packet:
|
||||
sequence += 1
|
||||
|
||||
# Initialize outputs
|
||||
for stream_output in stream.outputs.values():
|
||||
if video_stream.name != stream_output.video_codec:
|
||||
continue
|
||||
|
||||
a_packet, buffer = create_stream_buffer(
|
||||
stream_output, video_stream, audio_frame)
|
||||
audio_packets[buffer.astream] = a_packet
|
||||
outputs[stream_output.format] = buffer
|
||||
|
||||
# First video packet tends to have a weird dts/pts
|
||||
if first_packet:
|
||||
packet.dts = 0
|
||||
packet.pts = 0
|
||||
first_packet = False
|
||||
|
||||
# Store packets on each output
|
||||
for buffer in outputs.values():
|
||||
# Check if the format requires audio
|
||||
if audio_packets.get(buffer.astream):
|
||||
a_packet = audio_packets[buffer.astream]
|
||||
a_time_base = a_packet.time_base
|
||||
|
||||
# Determine video start timestamp and duration
|
||||
video_start = packet.pts * packet.time_base
|
||||
video_duration = packet.duration * packet.time_base
|
||||
|
||||
if packet.is_keyframe:
|
||||
# Set first audio packet in sequence to equal video pts
|
||||
a_packet.pts = int(video_start / a_time_base)
|
||||
a_packet.dts = int(video_start / a_time_base)
|
||||
|
||||
# Determine target end timestamp for audio
|
||||
target_pts = int((video_start + video_duration) / a_time_base)
|
||||
while a_packet.pts < target_pts:
|
||||
# Mux audio packet and adjust points until target hit
|
||||
buffer.output.mux(a_packet)
|
||||
a_packet.pts += a_packet.duration
|
||||
a_packet.dts += a_packet.duration
|
||||
audio_packets[buffer.astream] = a_packet
|
||||
|
||||
# Assign the video packet to the new stream & mux
|
||||
packet.stream = buffer.vstream
|
||||
buffer.output.mux(packet)
|
||||
Reference in new issue
Block a user