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
sampleRateuintThe audio sample rate in Hz.
numChannelsuintThe number of audio channels.
blockSizeintThe size of the audio block (in samples) for mixing. If 0, defaults to sampleRate/10.
streamTimeoutMsintThe maximum wait time in milliseconds for each stream to provide audio data.
capacityintThe 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
NumChannels
Gets the number of channels.
public uint NumChannels { get; }
Property Value
SampleRate
Gets the sample rate.
public uint SampleRate { get; }
Property Value
StreamCount
Gets the current number of active streams.
public int StreamCount { get; }
Property Value
Methods
AddStream(IAudioStreamSource)
Adds an audio stream to the mixer.
public void AddStream(IAudioStreamSource stream)
Parameters
streamIAudioStreamSourceThe 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
cancellationTokenCancellationTokenCancellation 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
cancellationTokenCancellationTokenCancellation 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
streamIAudioStreamSourceThe audio stream to remove.
TryRead(out AudioFrame?)
Tries to read the next mixed audio frame without blocking.
public bool TryRead(out AudioFrame? frame)
Parameters
frameAudioFrameThe frame if available.
Returns
- bool
True if a frame was available.