Use dataclasses instead of attrs in stream (#101148)

This commit is contained in:
uvjustin 2023-09-30 15:46:30 +08:00 committed by GitHub
parent e6c9a82b5f
commit d40a08958d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 25 deletions

View File

@ -4,14 +4,13 @@ from __future__ import annotations
import asyncio import asyncio
from collections import deque from collections import deque
from collections.abc import Callable, Coroutine, Iterable from collections.abc import Callable, Coroutine, Iterable
from dataclasses import dataclass from dataclasses import dataclass, field
import datetime import datetime
from enum import IntEnum from enum import IntEnum
import logging import logging
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any
from aiohttp import web from aiohttp import web
import attr
import numpy as np import numpy as np
from homeassistant.components.http.view import HomeAssistantView from homeassistant.components.http.view import HomeAssistantView
@ -51,15 +50,15 @@ class Orientation(IntEnum):
ROTATE_RIGHT = 8 ROTATE_RIGHT = 8
@attr.s(slots=True) @dataclass(slots=True)
class StreamSettings: class StreamSettings:
"""Stream settings.""" """Stream settings."""
ll_hls: bool = attr.ib() ll_hls: bool
min_segment_duration: float = attr.ib() min_segment_duration: float
part_target_duration: float = attr.ib() part_target_duration: float
hls_advance_part_limit: int = attr.ib() hls_advance_part_limit: int
hls_part_timeout: float = attr.ib() hls_part_timeout: float
STREAM_SETTINGS_NON_LL_HLS = StreamSettings( STREAM_SETTINGS_NON_LL_HLS = StreamSettings(
@ -81,29 +80,29 @@ class Part:
data: bytes data: bytes
@attr.s(slots=True) @dataclass(slots=True)
class Segment: class Segment:
"""Represent a segment.""" """Represent a segment."""
sequence: int = attr.ib() sequence: int
# the init of the mp4 the segment is based on # the init of the mp4 the segment is based on
init: bytes = attr.ib() init: bytes
# For detecting discontinuities across stream restarts # For detecting discontinuities across stream restarts
stream_id: int = attr.ib() stream_id: int
start_time: datetime.datetime = attr.ib() start_time: datetime.datetime
_stream_outputs: Iterable[StreamOutput] = attr.ib() _stream_outputs: Iterable[StreamOutput]
duration: float = attr.ib(default=0) duration: float = 0
parts: list[Part] = attr.ib(factory=list) parts: list[Part] = field(default_factory=list)
# Store text of this segment's hls playlist for reuse # Store text of this segment's hls playlist for reuse
# Use list[str] for easy appends # Use list[str] for easy appends
hls_playlist_template: list[str] = attr.ib(factory=list) hls_playlist_template: list[str] = field(default_factory=list)
hls_playlist_parts: list[str] = attr.ib(factory=list) hls_playlist_parts: list[str] = field(default_factory=list)
# Number of playlist parts rendered so far # Number of playlist parts rendered so far
hls_num_parts_rendered: int = attr.ib(default=0) hls_num_parts_rendered: int = 0
# Set to true when all the parts are rendered # Set to true when all the parts are rendered
hls_playlist_complete: bool = attr.ib(default=False) hls_playlist_complete: bool = False
def __attrs_post_init__(self) -> None: def __post_init__(self) -> None:
"""Run after init.""" """Run after init."""
for output in self._stream_outputs: for output in self._stream_outputs:
output.put(self) output.put(self)

View File

@ -4,13 +4,13 @@ from __future__ import annotations
from collections import defaultdict, deque from collections import defaultdict, deque
from collections.abc import Callable, Generator, Iterator, Mapping from collections.abc import Callable, Generator, Iterator, Mapping
import contextlib import contextlib
from dataclasses import fields
import datetime import datetime
from io import SEEK_END, BytesIO from io import SEEK_END, BytesIO
import logging import logging
from threading import Event from threading import Event
from typing import Any, Self, cast from typing import Any, Self, cast
import attr
import av import av
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -283,7 +283,7 @@ class StreamMuxer:
init=read_init(self._memory_file), init=read_init(self._memory_file),
# Fetch the latest StreamOutputs, which may have changed since the # Fetch the latest StreamOutputs, which may have changed since the
# worker started. # worker started.
stream_outputs=self._stream_state.outputs, _stream_outputs=self._stream_state.outputs,
start_time=self._start_time, start_time=self._start_time,
) )
self._memory_file_pos = self._memory_file.tell() self._memory_file_pos = self._memory_file.tell()
@ -537,7 +537,7 @@ def stream_worker(
audio_stream = None audio_stream = None
# Disable ll-hls for hls inputs # Disable ll-hls for hls inputs
if container.format.name == "hls": if container.format.name == "hls":
for field in attr.fields(StreamSettings): for field in fields(StreamSettings):
setattr( setattr(
stream_settings, stream_settings,
field.name, field.name,

View File

@ -24,7 +24,7 @@ DefaultSegment = partial(
init=None, init=None,
stream_id=0, stream_id=0,
start_time=FAKE_TIME, start_time=FAKE_TIME,
stream_outputs=[], _stream_outputs=[],
) )
AUDIO_SAMPLE_RATE = 8000 AUDIO_SAMPLE_RATE = 8000