Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/source-close-off-loop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-rtc": patch
---

`AudioSource.aclose()` and `VideoSource.aclose()` now dispose the native handle in a worker thread, so they no longer block the event loop, and both sources gain a synchronous `close()`. `AudioSource.close()`, `clear_queue()` and `asyncio.run(source.aclose())` can be called from another thread: a pending `wait_for_playout()` is released on the source's own loop, and audio captured after a `clear_queue()` keeps its own playout tracking. `wait_for_playout()` no longer returns early after the source moves to a new event loop. Capturing a frame after close is now a no-op.
155 changes: 123 additions & 32 deletions livekit-rtc/livekit/rtc/audio_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,40 @@

from __future__ import annotations

import time
import asyncio
import threading
import time
from typing import Any, Callable

from ._ffi_client import FfiHandle, FfiClient
from ._proto import audio_frame_pb2 as proto_audio_frame
from ._proto import ffi_pb2 as proto_ffi
from .audio_frame import AudioFrame


def _call_on_loop(
loop: asyncio.AbstractEventLoop | None, callback: Callable[..., None], *args: Any
) -> None:
"""Run `callback` on `loop` if it is running on another thread, else run it here."""
try:
running_loop = asyncio.get_running_loop()
except RuntimeError:
running_loop = None

if loop is not None and loop.is_running() and running_loop is not loop:
loop.call_soon_threadsafe(callback, *args)
else:
callback(*args)


def _finish_waiter(fut: asyncio.Future[None], handle: asyncio.TimerHandle | None) -> None:
"""Cancel a detached playout timer and release the waiters of its future."""
if handle is not None:
handle.cancel()
if not fut.done():
fut.set_result(None)


class AudioSource:
"""
Represents a real-time audio source with an internal audio queue.
Expand Down Expand Up @@ -53,7 +78,12 @@ def __init__(
"""
self._sample_rate = sample_rate
self._num_channels = num_channels
self._loop = loop or asyncio.get_event_loop()
self._loop: asyncio.AbstractEventLoop | None = loop
if self._loop is None:
try:
self._loop = asyncio.get_running_loop()
except RuntimeError:
self._loop = None

req = proto_ffi.FfiRequest()
req.new_audio_source.type = proto_audio_frame.AudioSourceType.AUDIO_SOURCE_NATIVE
Expand All @@ -67,6 +97,8 @@ def __init__(

self._last_capture = 0.0
self._q_size = 0.0
self._capture_gen = 0
self._lock = threading.Lock()
self._join_handle: asyncio.TimerHandle | None = None
self._join_fut: asyncio.Future[None] | None = None

Expand All @@ -83,7 +115,8 @@ def num_channels(self) -> int:
@property
def queued_duration(self) -> float:
"""The current duration (in seconds) of audio data queued for playback."""
return max(self._q_size - time.monotonic() + self._last_capture, 0.0)
with self._lock:
return max(self._q_size - time.monotonic() + self._last_capture, 0.0)

def clear_queue(self) -> None:
"""
Expand All @@ -96,8 +129,23 @@ def clear_queue(self) -> None:
"""
req = proto_ffi.FfiRequest()
req.clear_audio_buffer.source_handle = self._ffi_handle.handle
_ = FfiClient.instance.request(req)
self._release_waiter()
# The native clear shares the lock with capture_frame()'s submission, so a capture
# is ordered entirely before the clear or entirely after it. The cleared audio's
# future and timer are detached here, so a later capture starts with fresh ones.
with self._lock:
_ = FfiClient.instance.request(req)
self._last_capture = 0.0
self._q_size = 0.0
fut, handle = self._join_fut, self._join_handle
self._join_fut = None
self._join_handle = None
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
Resolved
if fut is not None:
_call_on_loop(fut.get_loop(), _finish_waiter, fut, handle)

def _get_loop(self) -> asyncio.AbstractEventLoop:
if self._loop is None or self._loop.is_closed():
self._loop = asyncio.get_running_loop()
return self._loop

async def capture_frame(self, frame: AudioFrame) -> None:
"""
Expand All @@ -116,21 +164,10 @@ async def capture_frame(self, frame: AudioFrame) -> None:
Exception: If there is an error during frame capture.
"""

if frame.samples_per_channel == 0 or self._ffi_handle.disposed:
if frame.samples_per_channel == 0 or getattr(self._ffi_handle, "disposed", False):
return

now = time.monotonic()
elapsed = 0.0 if self._last_capture == 0.0 else now - self._last_capture
self._q_size += frame.samples_per_channel / self.sample_rate - elapsed
self._last_capture = now

if self._join_handle:
self._join_handle.cancel()

if self._join_fut is None:
self._join_fut = self._loop.create_future()

self._join_handle = self._loop.call_later(self._q_size, self._release_waiter)
loop = self._get_loop()

req = proto_ffi.FfiRequest()
req.capture_audio_frame.source_handle = self._ffi_handle.handle
Expand All @@ -140,14 +177,42 @@ async def capture_frame(self, frame: AudioFrame) -> None:
# callback is let through, and wait_for drops the ones of other captures.
async_id: int | None = None
queue = FfiClient.instance.queue.subscribe(
loop=self._loop,
loop=loop,
filter_fn=lambda e: (
e.WhichOneof("message") == "capture_audio_frame"
and (async_id is None or e.capture_audio_frame.async_id == async_id)
),
)
try:
resp = FfiClient.instance.request(req)
# The playout state update and the native submission happen under one lock,
# shared with clear_queue(), so a clear never lands between the two.
with self._lock:
self._capture_gen += 1
current_gen = self._capture_gen

now = time.monotonic()
elapsed = 0.0 if self._last_capture == 0.0 else now - self._last_capture
# Clamp the remaining backlog: if the timer that resets it never ran (its
# loop closed), the expired time must not shorten this frame.
self._q_size = (
max(self._q_size - elapsed, 0.0) + frame.samples_per_channel / self.sample_rate
)
self._last_capture = now
q_size = self._q_size

if self._join_handle:
self._join_handle.cancel()

if (
self._join_fut is None
or self._join_fut.get_loop() is not loop
or self._join_fut.get_loop().is_closed()
):
self._join_fut = loop.create_future()

self._join_handle = loop.call_later(q_size, self._release_waiter, current_gen)

resp = FfiClient.instance.request(req)
async_id = resp.capture_audio_frame.async_id
cb: proto_ffi.FfiEvent = await queue.wait_for(
lambda e: e.capture_audio_frame.async_id == resp.capture_audio_frame.async_id
Expand All @@ -167,25 +232,51 @@ async def wait_for_playout(self) -> None:
audio queue is empty.
"""

if self._join_fut is None:
# One read: clear_queue() on another thread may detach the future at any point,
# and it still releases the detached one.
fut = self._join_fut
if fut is None:
return

await asyncio.shield(self._join_fut)
await asyncio.shield(fut)

def _release_waiter(self) -> None:
if self._join_fut is None:
return # could be None when clear_queue is called
def _release_waiter(self, gen: int | None = None) -> None:
with self._lock:
if gen is not None and gen != self._capture_gen:
return

if not self._join_fut.done():
self._join_fut.set_result(None)
if self._join_handle is not None:
self._join_handle.cancel()
self._join_handle = None

self._last_capture = 0.0
self._q_size = 0.0
self._join_fut = None
if self._join_fut is None:
return # could be None when clear_queue is called

async def aclose(self) -> None:
"""Close the audio source
if not self._join_fut.done():
self._join_fut.set_result(None)

self._last_capture = 0.0
self._q_size = 0.0
self._join_fut = None

def _release_waiter_threadsafe(self) -> None:
_call_on_loop(self._loop, self._release_waiter)

def close(self) -> None:
"""Close the audio source.

This method cleans up resources associated with the audio source.
"""
self._release_waiter_threadsafe()
self._ffi_handle.dispose()

async def aclose(self) -> None:
"""Close the audio source without blocking the event loop.

This method cleans up resources associated with the audio source.
"""
# aclose() may run on another thread's loop (asyncio.run(source.aclose()), see
# #832); the playout waiter belongs to the source's loop and is released there.
self._release_waiter_threadsafe()
if not getattr(self._ffi_handle, "disposed", False):
await asyncio.to_thread(self._ffi_handle.dispose)
19 changes: 18 additions & 1 deletion livekit-rtc/livekit/rtc/video_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

from __future__ import annotations

import asyncio

from ._ffi_client import FfiHandle, FfiClient
from ._proto import ffi_pb2 as proto_ffi
from ._proto import video_frame_pb2 as proto_video
Expand Down Expand Up @@ -61,6 +63,9 @@ def capture_frame(
metadata: proto_video.FrameMetadata | None = None,
) -> None:
"""Capture a frame, optionally attaching frame metadata."""
if getattr(self._ffi_handle, "disposed", False):
return

req = proto_ffi.FfiRequest()
req.capture_video_frame.source_handle = self._ffi_handle.handle
req.capture_video_frame.buffer.CopyFrom(frame._proto_info())
Expand All @@ -70,5 +75,17 @@ def capture_frame(
req.capture_video_frame.metadata.CopyFrom(metadata)
FfiClient.instance.request(req)

async def aclose(self) -> None:
def close(self) -> None:
"""Close the video source.

This method cleans up resources associated with the video source.
"""
self._ffi_handle.dispose()

async def aclose(self) -> None:
"""Close the video source without blocking the event loop.

This method cleans up resources associated with the video source.
"""
if not getattr(self._ffi_handle, "disposed", False):
await asyncio.to_thread(self._ffi_handle.dispose)
9 changes: 7 additions & 2 deletions livekit-rtc/tests/test_ffi_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@
_COUNT_ARRAY_TYPES_IN_GARBAGE = """
import ctypes
import gc
import os
import sys

from livekit import rtc

gc.collect()
gc.set_debug(gc.DEBUG_SAVEALL)
for _ in range(50):
for _ in range(10):
rtc.AudioProcessingModule()
gc.collect()
print(sum(isinstance(o, type(ctypes.Array)) for o in gc.garbage))
count = sum(isinstance(o, type(ctypes.Array)) for o in gc.garbage)
sys.stdout.write(f"{count}\\n")
sys.stdout.flush()
os._exit(0)
"""


Expand Down
Loading
Loading