Refactor stream to create partial segments (#51282)

This commit is contained in:
uvjustin 2021-06-14 00:41:21 +08:00 committed by GitHub
parent 1adeb82930
commit 123e8f01a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 499 additions and 305 deletions

View file

@ -19,20 +19,37 @@ from .const import ATTR_STREAMS, DOMAIN
PROVIDERS = Registry()
@attr.s(slots=True)
class Part:
"""Represent a segment part."""
duration: float = attr.ib()
has_keyframe: bool = attr.ib()
data: bytes = attr.ib()
@attr.s(slots=True)
class Segment:
"""Represent a segment."""
sequence: int = attr.ib()
# the init of the mp4
init: bytes = attr.ib()
# the video data (moof + mddat)s of the mp4
moof_data: bytes = attr.ib()
duration: float = attr.ib()
sequence: int = attr.ib(default=0)
# the init of the mp4 the segment is based on
init: bytes = attr.ib(default=None)
duration: float = attr.ib(default=0)
# For detecting discontinuities across stream restarts
stream_id: int = attr.ib(default=0)
parts: list[Part] = attr.ib(factory=list)
start_time: datetime.datetime = attr.ib(factory=datetime.datetime.utcnow)
@property
def complete(self) -> bool:
"""Return whether the Segment is complete."""
return self.duration > 0
def get_bytes_without_init(self) -> bytes:
"""Return reconstructed data for entire segment as bytes."""
return b"".join([part.data for part in self.parts])
class IdleTimer:
"""Invoke a callback after an inactivity timeout.