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