Add H.265 support to stream component (#38125)
* Add H.265 support to stream component * Change find_box to generator * Move fmp4 utilities to fmp4utils.py * Add minimum segments and segment durations * Remove MIN_SEGMENTS * Fix when container_options is None * Fix missing num_segments and update tests * Remove unnecessary mock attribute * Fix Segment construction in test_recorder_save * fix recorder with lookback Co-authored-by: Jason Hunter <hunterjm@gmail.com>
This commit is contained in:
1 parent
d0a59e28ac
commit
5355fcaba8
11 files changed
+169
-51
No files matched your search
@@ -6,6 +6,7 @@ from homeassistant.util.dt import utcnow
|
||||
|
||||
from .const import FORMAT_CONTENT_TYPE
|
||||
from .core import PROVIDERS, StreamOutput, StreamView
|
||||
from .fmp4utils import get_init, get_m4s
|
||||
|
||||
|
||||
@callback
|
||||
@@ -13,6 +14,7 @@ def async_setup_hls(hass):
|
||||
"""Set up api endpoints."""
|
||||
hass.http.register_view(HlsPlaylistView())
|
||||
hass.http.register_view(HlsSegmentView())
|
||||
hass.http.register_view(HlsInitView())
|
||||
return "/api/hls/{}/playlist.m3u8"
|
||||
|
||||
|
||||
@@ -37,21 +39,41 @@ class HlsPlaylistView(StreamView):
|
||||
)
|
||||
|
||||
|
||||
class HlsSegmentView(StreamView):
|
||||
"""Stream view to serve a MPEG2TS segment."""
|
||||
class HlsInitView(StreamView):
|
||||
"""Stream view to serve HLS init.mp4."""
|
||||
|
||||
url = r"/api/hls/{token:[a-f0-9]+}/segment/{sequence:\d+}.ts"
|
||||
url = r"/api/hls/{token:[a-f0-9]+}/init.mp4"
|
||||
name = "api:stream:hls:init"
|
||||
cors_allowed = True
|
||||
|
||||
async def handle(self, request, stream, sequence):
|
||||
"""Return init.mp4."""
|
||||
track = stream.add_provider("hls")
|
||||
segments = track.get_segment()
|
||||
if not segments:
|
||||
return web.HTTPNotFound()
|
||||
headers = {"Content-Type": "video/mp4"}
|
||||
return web.Response(body=get_init(segments[0].segment), headers=headers)
|
||||
|
||||
|
||||
class HlsSegmentView(StreamView):
|
||||
"""Stream view to serve a HLS fmp4 segment."""
|
||||
|
||||
url = r"/api/hls/{token:[a-f0-9]+}/segment/{sequence:\d+}.m4s"
|
||||
name = "api:stream:hls:segment"
|
||||
cors_allowed = True
|
||||
|
||||
async def handle(self, request, stream, sequence):
|
||||
"""Return mpegts segment."""
|
||||
"""Return fmp4 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)
|
||||
headers = {"Content-Type": "video/iso.segment"}
|
||||
return web.Response(
|
||||
body=get_m4s(segment.segment, segment.start_pts, int(sequence)),
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
|
||||
class M3U8Renderer:
|
||||
@@ -64,7 +86,12 @@ class M3U8Renderer:
|
||||
@staticmethod
|
||||
def render_preamble(track):
|
||||
"""Render preamble."""
|
||||
return ["#EXT-X-VERSION:3", f"#EXT-X-TARGETDURATION:{track.target_duration}"]
|
||||
return [
|
||||
"#EXT-X-VERSION:7",
|
||||
f"#EXT-X-TARGETDURATION:{track.target_duration}",
|
||||
'#EXT-X-MAP:URI="init.mp4"',
|
||||
"#EXT-X-INDEPENDENT-SEGMENTS",
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def render_playlist(track, start_time):
|
||||
@@ -81,7 +108,7 @@ class M3U8Renderer:
|
||||
playlist.extend(
|
||||
[
|
||||
"#EXTINF:{:.04f},".format(float(segment.duration)),
|
||||
f"./segment/{segment.sequence}.ts",
|
||||
f"./segment/{segment.sequence}.m4s",
|
||||
]
|
||||
)
|
||||
|
||||
@@ -109,7 +136,7 @@ class HlsStreamOutput(StreamOutput):
|
||||
@property
|
||||
def format(self) -> str:
|
||||
"""Return container format."""
|
||||
return "mpegts"
|
||||
return "mp4"
|
||||
|
||||
@property
|
||||
def audio_codec(self) -> str:
|
||||
@@ -117,6 +144,11 @@ class HlsStreamOutput(StreamOutput):
|
||||
return "aac"
|
||||
|
||||
@property
|
||||
def video_codec(self) -> str:
|
||||
"""Return desired video codec."""
|
||||
return "h264"
|
||||
def video_codecs(self) -> tuple:
|
||||
"""Return desired video codecs."""
|
||||
return {"hevc", "h264"}
|
||||
|
||||
@property
|
||||
def container_options(self) -> dict:
|
||||
"""Return container options."""
|
||||
return {"movflags": "frag_custom+empty_moov+default_base_moof"}
|
||||
Reference in new issue
Block a user