diff --git a/backend/fastrtc/tracks.py b/backend/fastrtc/tracks.py index 5bddbd6..5742e4f 100644 --- a/backend/fastrtc/tracks.py +++ b/backend/fastrtc/tracks.py @@ -35,6 +35,7 @@ from numpy import typing as npt from fastrtc.utils import ( AdditionalOutputs, DataChannel, + WebRTCError, create_message, current_channel, player_worker_decode, @@ -459,6 +460,11 @@ class AudioCallback(AudioStreamTrack): if isinstance(self.event_handler, AsyncHandler): callable = self.event_handler.emit start_up = self.event_handler.start_up() + if not inspect.isawaitable(start_up): + raise WebRTCError( + "In AsyncStreamHandler, start_up must be a coroutine (async def)" + ) + else: callable = functools.partial( loop.run_in_executor, None, self.event_handler_emit diff --git a/docs/userguide/audio.md b/docs/userguide/audio.md index 547ae88..e426e77 100644 --- a/docs/userguide/audio.md +++ b/docs/userguide/audio.md @@ -151,30 +151,30 @@ Here is aa simple example of using `AsyncStreamHandler`: === "Code" ``` py - - from fastrtc import AsyncStreamHandler, wait_for_item + from fastrtc import AsyncStreamHandler, wait_for_item, Stream import asyncio + import numpy as np class AsyncEchoHandler(AsyncStreamHandler): - """Handler for the Gemini API""" - + """Simple Async Echo Handler""" + def __init__(self) -> None: - super().__init__() + super().__init__(input_sample_rate=24000) self.queue = asyncio.Queue() - async def receive(self, frame: tuple[int, np.ndarray]) -> None: - self.queue.put(frame) + async def receive(self, frame: tuple[int, np.ndarray]) -> None: + await self.queue.put(frame) - async def emit(self) -> None: # (2) + async def emit(self) -> None: return await wait_for_item(self.queue) - + def copy(self): return AsyncEchoHandler() - - async def shutdown(self): # (3) + + async def shutdown(self): pass - - def start_up(self) -> None: # (4) + + async def start_up(self) -> None: pass ``` diff --git a/pyproject.toml b/pyproject.toml index 973497a..01be0db 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,7 +8,7 @@ build-backend = "hatchling.build" [project] name = "fastrtc" -version = "0.0.9" +version = "0.0.10" description = "The realtime communication library for Python" readme = "README.md" license = "apache-2.0"