Create master playlist for cast (#40483)
Co-authored-by: Jason Hunter <hunterjm@gmail.com>
This commit is contained in:
1 parent
e320c3b735
commit
9a32e28574
3 files changed
+188
-51
No files matched your search
@@ -1,4 +1,5 @@
|
||||
"""Provide functionality to stream HLS."""
|
||||
import io
|
||||
from typing import Callable
|
||||
|
||||
from aiohttp import web
|
||||
@@ -7,7 +8,7 @@ from homeassistant.core import callback
|
||||
|
||||
from .const import FORMAT_CONTENT_TYPE
|
||||
from .core import PROVIDERS, StreamOutput, StreamView
|
||||
from .fmp4utils import get_init, get_m4s
|
||||
from .fmp4utils import get_codec_string, get_init, get_m4s
|
||||
|
||||
|
||||
@callback
|
||||
@@ -16,7 +17,43 @@ def async_setup_hls(hass):
|
||||
hass.http.register_view(HlsPlaylistView())
|
||||
hass.http.register_view(HlsSegmentView())
|
||||
hass.http.register_view(HlsInitView())
|
||||
return "/api/hls/{}/playlist.m3u8"
|
||||
hass.http.register_view(HlsMasterPlaylistView())
|
||||
return "/api/hls/{}/master_playlist.m3u8"
|
||||
|
||||
|
||||
class HlsMasterPlaylistView(StreamView):
|
||||
"""Stream view used only for Chromecast compatibility."""
|
||||
|
||||
url = r"/api/hls/{token:[a-f0-9]+}/master_playlist.m3u8"
|
||||
name = "api:stream:hls:master_playlist"
|
||||
cors_allowed = True
|
||||
|
||||
@staticmethod
|
||||
def render(track):
|
||||
"""Render M3U8 file."""
|
||||
# Need to calculate max bandwidth as input_container.bit_rate doesn't seem to work
|
||||
# Calculate file size / duration and use a multiplier to account for variation
|
||||
segment = track.get_segment(track.segments[-1])
|
||||
bandwidth = round(
|
||||
segment.segment.seek(0, io.SEEK_END) * 8 / segment.duration * 3
|
||||
)
|
||||
codecs = get_codec_string(segment.segment)
|
||||
lines = [
|
||||
"#EXTM3U",
|
||||
f'#EXT-X-STREAM-INF:BANDWIDTH={bandwidth},CODECS="{codecs}"',
|
||||
"playlist.m3u8",
|
||||
]
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
async def handle(self, request, stream, sequence):
|
||||
"""Return m3u8 playlist."""
|
||||
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=self.render(track).encode("utf-8"), headers=headers)
|
||||
|
||||
|
||||
class HlsPlaylistView(StreamView):
|
||||
@@ -26,18 +63,50 @@ class HlsPlaylistView(StreamView):
|
||||
name = "api:stream:hls:playlist"
|
||||
cors_allowed = True
|
||||
|
||||
@staticmethod
|
||||
def render_preamble(track):
|
||||
"""Render preamble."""
|
||||
return [
|
||||
"#EXT-X-VERSION:7",
|
||||
f"#EXT-X-TARGETDURATION:{track.target_duration}",
|
||||
'#EXT-X-MAP:URI="init.mp4"',
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def render_playlist(track):
|
||||
"""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:{:.04f},".format(float(segment.duration)),
|
||||
f"./segment/{segment.sequence}.m4s",
|
||||
]
|
||||
)
|
||||
|
||||
return playlist
|
||||
|
||||
def render(self, track):
|
||||
"""Render M3U8 file."""
|
||||
lines = ["#EXTM3U"] + self.render_preamble(track) + self.render_playlist(track)
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
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).encode("utf-8"), headers=headers
|
||||
)
|
||||
return web.Response(body=self.render(track).encode("utf-8"), headers=headers)
|
||||
|
||||
|
||||
class HlsInitView(StreamView):
|
||||
@@ -77,49 +146,6 @@ class HlsSegmentView(StreamView):
|
||||
)
|
||||
|
||||
|
||||
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:7",
|
||||
f"#EXT-X-TARGETDURATION:{track.target_duration}",
|
||||
'#EXT-X-MAP:URI="init.mp4"',
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def render_playlist(track):
|
||||
"""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:{:.04f},".format(float(segment.duration)),
|
||||
f"./segment/{segment.sequence}.m4s",
|
||||
]
|
||||
)
|
||||
|
||||
return playlist
|
||||
|
||||
def render(self, track):
|
||||
"""Render M3U8 file."""
|
||||
lines = ["#EXTM3U"] + self.render_preamble(track) + self.render_playlist(track)
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
|
||||
@PROVIDERS.register("hls")
|
||||
class HlsStreamOutput(StreamOutput):
|
||||
"""Represents HLS Output formats."""
|
||||
@@ -137,7 +163,7 @@ class HlsStreamOutput(StreamOutput):
|
||||
@property
|
||||
def audio_codecs(self) -> str:
|
||||
"""Return desired audio codecs."""
|
||||
return {"aac", "ac3", "mp3"}
|
||||
return {"aac", "mp3"}
|
||||
|
||||
@property
|
||||
def video_codecs(self) -> tuple:
|
||||
|
||||
Reference in new issue
Block a user