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
8 changes: 7 additions & 1 deletion livekit-agents/livekit/agents/utils/codecs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
# limitations under the License.

from .decoder import AudioStreamDecoder, StreamBuffer
from .encoder import AudioStreamEncoder, EncodedAudioData

__all__ = ["AudioStreamDecoder", "StreamBuffer"]
__all__ = [
"AudioStreamDecoder",
"StreamBuffer",
"AudioStreamEncoder",
"EncodedAudioData",
]

# Cleanup docs of unexported modules
_module = dir()
Expand Down
170 changes: 170 additions & 0 deletions livekit-agents/livekit/agents/utils/codecs/encoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
# Copyright 2026 LiveKit, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import io
from dataclasses import dataclass
from typing import Literal

import av
import av.audio
import av.container

from livekit import rtc


@dataclass
class EncodedAudioData:
"""Data returned by the encoder."""

data: bytes
"""The encoded audio data."""
num_samples: int
"""The number of samples in the encoded audio data,
useful if the receiver needs to buffer audio data based on duration without decoding."""


_CODEC_TABLE: dict[str, tuple[str, str]] = {
"opus": ("libopus", "ogg"),
"mp3": ("libmp3lame", "mp3"),
"pcm": ("pcm_s16le", "wav"),
}

_SUPPORTED_CODECS = Literal["opus", "mp3", "pcm"]


def _resolve_codec(codec: str) -> tuple[str, str]:
"""Return (av_encoder_name, container_format) for a public codec name."""
if codec not in _CODEC_TABLE:
raise ValueError(f"unsupported codec for streaming encode: {codec!r}")
return _CODEC_TABLE[codec]


class _CompactableBuffer(io.RawIOBase):
_COMPACT_THRESHOLD = 1 * 1024 * 1024 # reclaim consumed prefix once it exceeds 1MB

def __init__(self) -> None:
super().__init__()
self._buf = bytearray()
self._read_pos = 0

def writable(self) -> bool:
return True

def readable(self) -> bool:
return False

def seekable(self) -> bool:
"""disable back-patching container headers"""
return False

def write(self, b) -> int: # type: ignore[no-untyped-def]
self._buf.extend(b)
return len(b)

def drain(self) -> bytes:
if self._read_pos >= len(self._buf):
return b""
data = bytes(self._buf[self._read_pos :])
self._read_pos = len(self._buf)
if self._read_pos >= self._COMPACT_THRESHOLD:
self._buf.clear()
self._read_pos = 0
return data


class AudioStreamEncoder:
Copy link
Copy Markdown
Member

@theomonnom theomonnom Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should encode in another thread, like we do for our AudioDecoder

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this before, but I can see some difference here:

Decoder: we need a thread so that the blocking read() wait doesn't stall the event loop

Encoder: caller pushes data (calling encode() when we have a frame) → no blocking wait, no thread needed

I can create a threaded version and show some benchmarks.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are the results:

metric sync threaded
push mean 1,186 us 40 us
push p95 2,053 us 45 us
push max 4,303 us 728 us
first page 4.0 ms 10.0 ms
inter-page mean 990 ms 989 ms
inter-page median 990 ms 990 ms
pages / bytes 7 / 1520 7 / 1520

Threaded version has a 6ms delay for the first page, but all of them are pretty much invisible in real-time load (60ms input frame size, opus needs about 16 frames for a page)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

@theomonnom theomonnom Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the Opus encode is almost instantaneous? Tho what if you push more than 60ms? like if you push 500ms?
isn't it going to block? I understand we will push tiny frames for the barge-in model, but since this is a public utility, we still need to get the interface right

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sync version is still blocking 4ms sometimes, for the asyncio it's still not ideal (it accumulates with the user code and a lot of stuff inside our framework).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, reading this comment #5494 (comment)

Seems like we should close this PR then?

"""Encode PCM AudioFrames into a compressed audio byte stream."""
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.

def __init__(
self,
*,
codec: _SUPPORTED_CODECS = "opus",
sample_rate: int = 48000,
num_channels: int = 1,
bit_rate: int = 24000,
codec_options: dict[str, str] | None = None,
) -> None:
"""Create an encoder.

``codec_options`` is an optional mapping of libav private codec AVOptions
(e.g. ``{"application": "lowdelay", "frame_duration": "60",
"compression_level": "0", "vbr": "on"}`` for a low-latency libopus profile).
Values must be strings.
"""
self._sample_rate = sample_rate
self._num_channels = num_channels
self._layout = "mono" if num_channels == 1 else "stereo"

av_codec, container_format = _resolve_codec(codec)

self._output_buf = _CompactableBuffer()
self._container: av.container.OutputContainer = av.open(
self._output_buf,
mode="w",
format=container_format,
)
self._stream: av.audio.AudioStream = self._container.add_stream( # type: ignore[assignment]
av_codec,
rate=sample_rate,
layout=self._layout,
options=codec_options,
)
self._stream.bit_rate = bit_rate

self._closed = False

def push(self, frame: rtc.AudioFrame) -> EncodedAudioData:
"""Encode a PCM audio frame.

Returns any new encoded bytes produced by the muxer (may be empty when
the container hasn't flushed a full page yet). The very first call
includes container headers (e.g. OGG OpusHead / OpusTags) if not empty.
"""
if self._closed:
raise RuntimeError("encoder is closed")

av_frame = av.AudioFrame(
format="s16",
layout=self._layout,
samples=frame.samples_per_channel,
)
av_frame.rate = self._sample_rate
av_frame.planes[0].update(bytes(frame.data))

num_samples = 0
for packet in self._stream.encode(av_frame):
if packet.duration is not None:
num_samples += packet.duration
self._container.mux(packet)

return EncodedAudioData(data=self._output_buf.drain(), num_samples=num_samples)

def close(self) -> EncodedAudioData:
"""Finalize the container and return any remaining bytes (e.g. OGG EOS).

The encoder must not be used after calling this method.
"""
if self._closed:
return EncodedAudioData(data=b"", num_samples=0)

self._closed = True
num_samples = 0
for packet in self._stream.encode(None):
if packet.duration is not None:
num_samples += packet.duration
self._container.mux(packet)
self._container.close()
return EncodedAudioData(data=self._output_buf.drain(), num_samples=num_samples)
1 change: 1 addition & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ unit-tests:
tests/test_agent_session.py \
tests/test_aio.py \
tests/test_audio_decoder.py \
tests/test_audio_encoder.py \
tests/test_chat_ctx.py \
tests/test_config.py \
tests/test_connection_pool.py \
Expand Down
Loading
Loading