frames: use mixins for audio and image data

This commit is contained in:
Aleix Conchillo Flaqué
2024-12-02 14:49:28 -08:00
parent a6606a4040
commit 42658ecd92

View File

@@ -67,22 +67,42 @@ class ControlFrame(Frame):
pass pass
#
# Mixins
#
@dataclass
class AudioRawFrame:
"""A chunk of audio."""
audio: bytes
sample_rate: int
num_channels: int
num_frames: int = field(init=False)
@dataclass
class ImageRawFrame:
"""A raw image."""
image: bytes
size: Tuple[int, int]
format: str | None
# #
# Data frames. # Data frames.
# #
@dataclass @dataclass
class OutputAudioRawFrame(DataFrame): class OutputAudioRawFrame(DataFrame, AudioRawFrame):
"""A chunk of audio. Will be played by the output transport if the """A chunk of audio. Will be played by the output transport if the
transport's microphone has been enabled. transport's microphone has been enabled.
""" """
audio: bytes
sample_rate: int
num_channels: int
def __post_init__(self): def __post_init__(self):
super().__post_init__() super().__post_init__()
self.num_frames = int(len(self.audio) / (self.num_channels * 2)) self.num_frames = int(len(self.audio) / (self.num_channels * 2))
@@ -93,16 +113,12 @@ class OutputAudioRawFrame(DataFrame):
@dataclass @dataclass
class OutputImageRawFrame(DataFrame): class OutputImageRawFrame(DataFrame, ImageRawFrame):
"""An image that will be shown by the transport if the transport's camera is """An image that will be shown by the transport if the transport's camera is
enabled. enabled.
""" """
image: bytes
size: Tuple[int, int]
format: str | None
def __str__(self): def __str__(self):
pts = format_pts(self.pts) pts = format_pts(self.pts)
return f"{self.name}(pts: {pts}, size: {self.size}, format: {self.format})" return f"{self.name}(pts: {pts}, size: {self.size}, format: {self.format})"
@@ -474,13 +490,9 @@ class UserImageRequestFrame(SystemFrame):
@dataclass @dataclass
class InputAudioRawFrame(SystemFrame): class InputAudioRawFrame(SystemFrame, AudioRawFrame):
"""A chunk of audio usually coming from an input transport.""" """A chunk of audio usually coming from an input transport."""
audio: bytes
sample_rate: int
num_channels: int
def __post_init__(self): def __post_init__(self):
super().__post_init__() super().__post_init__()
self.num_frames = int(len(self.audio) / (self.num_channels * 2)) self.num_frames = int(len(self.audio) / (self.num_channels * 2))
@@ -491,13 +503,9 @@ class InputAudioRawFrame(SystemFrame):
@dataclass @dataclass
class InputImageRawFrame(SystemFrame): class InputImageRawFrame(SystemFrame, ImageRawFrame):
"""An image usually coming from an input transport.""" """An image usually coming from an input transport."""
image: bytes
size: Tuple[int, int]
format: str | None
def __str__(self): def __str__(self):
pts = format_pts(self.pts) pts = format_pts(self.pts)
return f"{self.name}(pts: {pts}, size: {self.size}, format: {self.format})" return f"{self.name}(pts: {pts}, size: {self.size}, format: {self.format})"