Skip to content

Support explicit audio codec selection before publishing an audio track #2281

Open

Description

@huangyouyi

Could maintainers confirm the public contract below and whether it is feasible in the JS SDK alone? If official protocol or SFU changes are required, please identify the companion versions. We would consume official releases through public APIs, without maintaining JS or SFU forks.

Motivation and current API

Any application should be able to select a fixed Opus, PCMA or PCMU primary codec before publishing a local audio track. Ordinary browser applications may need this independently of embedded devices. When neither track options nor room publishing defaults specify a codec, browser-to-browser calls without an embedded device should retain normal default publishing and negotiation: each browser can publish without waiting for the other participant's audio publication.

On the check date, the official latest release resolves to v2.24.1. Its public TrackPublishDefaults and TrackPublishOptions reference pages show audio parameters and red, but no audioCodec selector. RoomOptions.publishDefaults uses TrackPublishDefaults.

The installed 2.24.0 package is used for the static reproduction below; 2.24.1 was checked through official release/reference pages, not installed or compiled. In the 2.24.0 source, AudioCodec derives from audioCodecs, whose values are opus and red. That existing type does not already express the three requested primary codecs; RED must be treated separately.

Proposed public contract

Please consider optional audioCodec alongside videoCodec in TrackPublishDefaults, inherited by TrackPublishOptions. Reuse RoomOptions.publishDefaults and the existing per-track override mechanism. Candidate values follow the lowercase style: opus, pcma, pcmu. The final type representation is for maintainers to decide; no new policy or additional mode options are requested. This is a proposal, not an available API.

  • Omitting the option at both levels retains existing default negotiation and independent publishing, including browser-to-browser calls without a device. Selecting a codec does not make the SDK wait for remote audio tracks.
  • An explicit choice applies to the primary codec from the first media RTP packet. The SDK must not first send a default codec or silently substitute another one. Publication metadata alone is insufficient evidence of this behavior.
  • The selection survives SDK-managed republishing and full reconnection without application access to internals. Runtime codec switching is outside this request.
  • Keep the existing red option separate from the primary codec. Please define valid combinations with RED, stereo and other audio options; distinguish a RED RTP wrapper from its primary codec when validating the result.
  • If the browser/server cannot honor an explicit choice, surface an identifiable publishing failure. Define ownership and cleanup for SDK publishing resources and application-supplied capture tracks, including failure and cancellation, so callers can release their resources without stopping a reused track.
  • Document supported browsers and minimum official SDK/protocol/SFU versions. First-packet codec behavior, republishing and full reconnection require runtime validation by the eventual implementation; they have not been tested here.

One embedded use case selects the local codec in application code from a target device's current microphone publication MIME. Device identity, MIME interpretation, waiting and business-session state remain application responsibilities. A best-effort application can handle an audio failure while continuing unrelated work in the room; this is not a request for SDK knowledge of that application lifecycle.

Minimal public-type reproduction

Environment actually checked: Node 22.22.3, TypeScript 5.9.3, official livekit-client 2.24.0. An isolated temporary directory contained only these two files and a node_modules/livekit-client symlink to the already installed package. Both files import types only; no SDK runtime, browser, server or media device was started.

baseline.mts:

import type { TrackPublishOptions } from 'livekit-client';

const options: TrackPublishOptions = { red: false };

candidate.mts:

import type { TrackPublishOptions } from 'livekit-client';

const options: TrackPublishOptions = { red: false, audioCodec: 'pcma' };

With tsc referring to TypeScript 5.9.3, the equivalent commands are:

tsc --noEmit --skipLibCheck --module NodeNext --moduleResolution NodeNext --target ES2022 --lib ES2022,DOM baseline.mts
tsc --noEmit --skipLibCheck --module NodeNext --moduleResolution NodeNext --target ES2022 --lib ES2022,DOM candidate.mts

The baseline exits 0 with no diagnostics. The candidate exits 2 with this single diagnostic:

candidate.mts(3,52): error TS2353: Object literal may only specify known properties, and 'audioCodec' does not exist in type 'TrackPublishOptions'.

This demonstrates the installed 2.24.0 public-type gap only. It does not establish whether G.711 can be negotiated at runtime, inspect actual RTP, or reproduce a 2.24.1 compiler result. No packages were installed or updated; the temporary directory was removed after the check.

Implementation scope to confirm

Adding a field alone is not assumed sufficient to guarantee the requested behavior. Please first confirm the design and whether the SDK can enforce it with the current official signaling and SFU, or whether coordinated official changes are necessary. The application needs one supported publishing path and will not use internal engine/sender/transceiver access, SDK hooks, SDP rewriting, transcoding or an additional application-created PeerConnection as workarounds.

Activity

  1. 1egoman commented on Nov 16, 2026

    @1egoman
    Contributor

    I think this is fairly niche, but I think makes sense - for the esp32 sdk some embedded devices, OPUS encoding is too computationally expensive so G.711 (PCMA/PCMU) is published instead. And as a converse, this means that if the browser wanted to send audio to an ESP32, it also needs a way to signal that it should encode it as non OPUS.

    Suggestions

    I think as part of this it would be worth deprecating the red flag outright though, and moving over to audioCodec globally for selecting any codec value. AudioCodec (ie, this) could then become something like 'opus' | 'red' | 'pcmu' | 'pcma'.

    And I think since some audio capture settings are opus specific today (dtx, stereo, audioPreset), so maybe it makes sense to make TrackPublishDefaults["audioCodec"] a discriminated union with an entry for each? ie, maybe something like:

    type OpusParams = {
      dtx?: boolean;
      stereo?: boolean;      // replaces forceStereo
      maxBitrate?: number;   // the bitrate half of audioPreset
    };
    
    export type AudioCodecOptions =
      | ({ codec: 'opus' } & OpusParams)
      | ({ codec: 'red' } & OpusParams) // RED wraps Opus, so Opus params apply
      | { codec: 'pcmu' }
      | { codec: 'pcma' };
    
    export interface TrackPublishDefaults {
      audioCodec?: AudioCodec | AudioCodecOptions; // 'pcmu' or { codec: 'opus', dtx: false }
      /** @deprecated use audioCodec: { codec: 'opus' | 'red', dtx } */
      dtx?: boolean;
      /** @deprecated use audioCodec: 'red' | 'opus' */
      red?: boolean;
      // ...
    }
  2. 1egoman commented on Nov 17, 2026

    @1egoman
    Contributor

    @huangyouyi Was this something you were interested in working on? If not I think it is fairly straightforward and I can aim to make a PR in the next week or so.

  3. ladvoc commented on Nov 17, 2026

    @ladvoc
    Contributor

    Hi @huangyouyi, you mentioned "embedded devices"; are you are specifically working with the LiveKit ESP32 SDK?

  4. huangyouyi commented on Nov 18, 2026

    @huangyouyi
    Author

    @ladvoc Yes. Our firmware is based on the LiveKit ESP32 SDK v0.3.12. We upgraded to ESP-IDF 6.0.2 and esp_capture 1.1.2, with local SDK adaptations for the updated media stack.

    @1egoman Thanks for the invitation. I’d be happy to contribute the PR. Your proposed audioCodec API works for us: accept a codec name or an options object, use 'red' for Opus with RED, and deprecate the separate red flag.

    Our app can determine which codec to request from the device’s microphone track. What we need from the SDK is a way to select that codec before publishing, while preserving the existing behavior when audioCodec is omitted. If that matches your intent, I’ll proceed on that basis.

  5. 1egoman commented on Nov 18, 2026

    @1egoman
    Contributor

    Yep, I think that matches the intent. Looking forward to taking a look at the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions