Skip to content

Commit 201b04d

Browse files
authored
Add close function to Audio|Video Source (#336)
1 parent 1738a10 commit 201b04d

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

examples/video-stream/audio_wave.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ async def main(room: rtc.Room, room_name: str):
296296
finally:
297297
audio_task.cancel()
298298
await av_sync.aclose()
299+
await audio_source.aclose()
300+
await video_source.aclose()
299301

300302

301303
if __name__ == "__main__":

examples/video-stream/video_play.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ async def _push_frames(
181181
finally:
182182
await streamer.aclose()
183183
await av_sync.aclose()
184+
await audio_source.aclose()
185+
await video_source.aclose()
184186

185187

186188
if __name__ == "__main__":

livekit-rtc/livekit/rtc/audio_source.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ async def capture_frame(self, frame: AudioFrame) -> None:
118118
Exception: If there is an error during frame capture.
119119
"""
120120

121-
if frame.samples_per_channel == 0:
121+
if frame.samples_per_channel == 0 or self._ffi_handle.disposed:
122122
return
123123

124124
now = time.monotonic()
@@ -175,3 +175,10 @@ def _release_waiter(self) -> None:
175175
self._last_capture = 0.0
176176
self._q_size = 0.0
177177
self._join_fut = None
178+
179+
async def aclose(self) -> None:
180+
"""Close the audio source
181+
182+
This method cleans up resources associated with the audio source.
183+
"""
184+
self._ffi_handle.dispose()

livekit-rtc/livekit/rtc/video_source.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@ def capture_frame(
4242
req.capture_video_frame.rotation = rotation
4343
req.capture_video_frame.timestamp_us = timestamp_us
4444
FfiClient.instance.request(req)
45+
46+
async def aclose(self) -> None:
47+
self._ffi_handle.dispose()

0 commit comments

Comments
 (0)