Skip to content
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

Add close function to Audio|Video Source #336

Merged
merged 5 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/video-stream/audio_wave.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ async def main(room: rtc.Room, room_name: str):
finally:
audio_task.cancel()
await av_sync.aclose()
await audio_source.aclose()
await video_source.aclose()


if __name__ == "__main__":
Expand Down
2 changes: 2 additions & 0 deletions examples/video-stream/video_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ async def _push_frames(
finally:
await streamer.aclose()
await av_sync.aclose()
await audio_source.aclose()
await video_source.aclose()


if __name__ == "__main__":
Expand Down
9 changes: 8 additions & 1 deletion livekit-rtc/livekit/rtc/audio_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ async def capture_frame(self, frame: AudioFrame) -> None:
Exception: If there is an error during frame capture.
"""

if frame.samples_per_channel == 0:
if frame.samples_per_channel == 0 or self._ffi_handle.disposed:
return

now = time.monotonic()
Expand Down Expand Up @@ -175,3 +175,10 @@ def _release_waiter(self) -> None:
self._last_capture = 0.0
self._q_size = 0.0
self._join_fut = None

async def aclose(self) -> None:
"""Close the audio source

This method cleans up resources associated with the audio source.
"""
self._ffi_handle.dispose()
3 changes: 3 additions & 0 deletions livekit-rtc/livekit/rtc/video_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ def capture_frame(
req.capture_video_frame.rotation = rotation
req.capture_video_frame.timestamp_us = timestamp_us
FfiClient.instance.request(req)

async def aclose(self) -> None:
self._ffi_handle.dispose()
Loading