Table of Contents

Class AudioMixer

Namespace
LiveKit.Rtc
Assembly
LivekitRtc.dll

Mixes multiple audio streams into a single output stream. Implements IAsyncEnumerable for modern async iteration patterns.

public class AudioMixer : IAsyncDisposable, IDisposable, IAsyncEnumerable<AudioFrame>
Inheritance
AudioMixer
Implements
Inherited Members

Constructors

AudioMixer(uint, uint, int, int, int)

Initializes a new AudioMixer.

public AudioMixer(uint sampleRate, uint numChannels, int blockSize = 0, int streamTimeoutMs = 100, int capacity = 100)

Parameters

sampleRate uint

The audio sample rate in Hz.

numChannels uint

The number of audio channels.

blockSize int

The size of the audio block (in samples) for mixing. If 0, defaults to sampleRate/10.

streamTimeoutMs int

The maximum wait time in milliseconds for each stream to provide audio data.

capacity int

The maximum number of mixed frames to store in the output channel.

Properties

ChunkSize

Gets the chunk size in samples.

public int ChunkSize { get; }

Property Value

int

NumChannels

Gets the number of channels.

public uint NumChannels { get; }

Property Value

uint

SampleRate

Gets the sample rate.

public uint SampleRate { get; }

Property Value

uint

StreamCount

Gets the current number of active streams.

public int StreamCount { get; }

Property Value

int

Methods

AddStream(IAudioStreamSource)

Adds an audio stream to the mixer.

public void AddStream(IAudioStreamSource stream)

Parameters

stream IAudioStreamSource

The audio stream to add.

Dispose()

Stops mixing. Pending reads return null and later reads throw ObjectDisposedException. The added streams are not disposed.

public void Dispose()

DisposeAsync()

Stops mixing and waits for the mixing loop to exit. Pending reads return null and later reads throw ObjectDisposedException. The added streams are not disposed.

public ValueTask DisposeAsync()

Returns

ValueTask

A task that completes once the mixing loop has stopped.

EndInput()

Signals that no more streams will be added. Existing streams will still be processed until exhausted.

public void EndInput()

GetAsyncEnumerator(CancellationToken)

Returns an async enumerator for iterating over mixed audio frames.

public IAsyncEnumerator<AudioFrame> GetAsyncEnumerator(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Cancellation token.

Returns

IAsyncEnumerator<AudioFrame>

An async enumerator of audio frames.

ReadAsync(CancellationToken)

Reads the next mixed audio frame.

public ValueTask<AudioFrame?> ReadAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask<AudioFrame>

The next mixed audio frame, or null if the mixer is complete.

RemoveStream(IAudioStreamSource)

Removes an audio stream from the mixer.

public void RemoveStream(IAudioStreamSource stream)

Parameters

stream IAudioStreamSource

The audio stream to remove.

TryRead(out AudioFrame?)

Tries to read the next mixed audio frame without blocking.

public bool TryRead(out AudioFrame? frame)

Parameters

frame AudioFrame

The frame if available.

Returns

bool

True if a frame was available.