-
Notifications
You must be signed in to change notification settings - Fork 108
Add frame processor support for audio streams #533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
60ce71f
0d378f2
d786dbd
cb8745c
67802ee
6837888
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| from .audio_frame import AudioFrame | ||
| from .participant import Participant | ||
| from .track import Track | ||
| from .frame_processor import FrameProcessor | ||
|
|
||
|
|
||
| @dataclass | ||
|
|
@@ -62,7 +63,7 @@ def __init__( | |
| sample_rate: int = 48000, | ||
| num_channels: int = 1, | ||
| frame_size_ms: int | None = None, | ||
| noise_cancellation: Optional[NoiseCancellationOptions] = None, | ||
| noise_cancellation: Optional[NoiseCancellationOptions | FrameProcessor[AudioFrame]] = None, | ||
| **kwargs, | ||
| ) -> None: | ||
| """Initialize an `AudioStream` instance. | ||
|
|
@@ -76,8 +77,8 @@ def __init__( | |
| sample_rate (int, optional): The sample rate for the audio stream in Hz. | ||
| Defaults to 48000. | ||
| num_channels (int, optional): The number of audio channels. Defaults to 1. | ||
| noise_cancellation (Optional[NoiseCancellationOptions], optional): | ||
| If noise cancellation is used, pass a `NoiseCancellationOptions` instance | ||
| noise_cancellation (Optional[NoiseCancellationOptions | FrameProcessor[AudioFrame]], optional): | ||
| If noise cancellation is used, pass a `NoiseCancellationOptions` or `FrameProcessor[AudioFrame]` instance | ||
| created by the noise cancellation module. | ||
|
|
||
| Example: | ||
|
|
@@ -105,9 +106,12 @@ def __init__( | |
|
|
||
| self._audio_filter_module = None | ||
| self._audio_filter_options = None | ||
| if noise_cancellation is not None: | ||
| if isinstance(noise_cancellation, NoiseCancellationOptions): | ||
| self._audio_filter_module = noise_cancellation.module_id | ||
| self._audio_filter_options = noise_cancellation.options | ||
| elif isinstance(noise_cancellation, FrameProcessor): | ||
| self._processor = noise_cancellation | ||
|
|
||
| self._task = self._loop.create_task(self._run()) | ||
| self._task.add_done_callback(task_done_logger) | ||
|
|
||
|
|
@@ -132,7 +136,7 @@ def from_participant( | |
| sample_rate: int = 48000, | ||
| num_channels: int = 1, | ||
| frame_size_ms: int | None = None, | ||
| noise_cancellation: Optional[NoiseCancellationOptions] = None, | ||
| noise_cancellation: Optional[NoiseCancellationOptions | FrameProcessor[AudioFrame]] = None, | ||
| ) -> AudioStream: | ||
| """Create an `AudioStream` from a participant's audio track. | ||
|
|
||
|
|
@@ -182,7 +186,7 @@ def from_track( | |
| sample_rate: int = 48000, | ||
| num_channels: int = 1, | ||
| frame_size_ms: int | None = None, | ||
| noise_cancellation: Optional[NoiseCancellationOptions] = None, | ||
| noise_cancellation: Optional[NoiseCancellationOptions | FrameProcessor[AudioFrame]] = None, | ||
| ) -> AudioStream: | ||
| """Create an `AudioStream` from an existing audio track. | ||
|
|
||
|
|
@@ -268,6 +272,8 @@ async def _run(self): | |
| if audio_event.HasField("frame_received"): | ||
| owned_buffer_info = audio_event.frame_received.frame | ||
| frame = AudioFrame._from_owned_info(owned_buffer_info) | ||
| if self._processor is not None: | ||
| frame = self._processor._process(frame) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. neat.. this is clean |
||
| event = AudioFrameEvent(frame) | ||
| self._queue.put(event) | ||
| elif audio_event.HasField("eos"): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| from abc import ABC, abstractmethod | ||
| from typing import Generic, TypeVar, Union | ||
| from .audio_frame import AudioFrame | ||
| from .video_frame import VideoFrame | ||
|
|
||
|
|
||
| T = TypeVar("T", bound=Union[AudioFrame, VideoFrame]) | ||
|
|
||
|
|
||
| class FrameProcessor(Generic[T], ABC): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (non-blocking): This might be over abstracting, but we could make this more generalizable by defining an AuthenticatedFrameProcessor interface which inherits from a more general FrameProcessor one.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true, we can always go the other way around later with an |
||
| @property | ||
| @abstractmethod | ||
| def is_enabled(self) -> bool: ... | ||
|
|
||
| @abstractmethod | ||
| def set_enabled(self, enable: bool): ... | ||
|
|
||
| @abstractmethod | ||
| def _update_stream_info( | ||
| self, | ||
| *, | ||
| room_name: str, | ||
| participant_identity: str, | ||
| publication_sid: str, | ||
| ): ... | ||
|
|
||
| @abstractmethod | ||
| def _update_credentials(self, *, token: str, url: str): ... | ||
|
|
||
| @abstractmethod | ||
| def _process(self, frame: T) -> T: ... | ||
|
|
||
| @abstractmethod | ||
| def _close(self): ... | ||
|
Comment on lines
+19
to
+34
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need those methods (e.g:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking this interface could be used by anybody, more like a general purpose API
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the underlying Rust implementation would need to make calls to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
yeah, that would be good. The methods defined right now is the minimal API we need to make authenticated processors work without tying them too deeply to the RTC package. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have another field name than
noise_cancellation?Like just
processors?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I went with reusing this for now is that the first official processor we'll have will also be noise cancellation, so I thought it might be confusing otherwise.
I think we can still transition away (deprecate) noise_cancellation lateron in favour of something more generic