Table of Contents

Class AudioStream

Namespace
LiveKit.Rtc
Assembly
LivekitRtc.dll

An audio stream for receiving audio frames from a remote track or participant. Implements IAsyncEnumerable for modern async iteration patterns.

public class AudioStream : IAsyncDisposable, IDisposable, IAsyncEnumerable<AudioFrameEvent>
Inheritance
AudioStream
Implements
Inherited Members

Constructors

AudioStream(Track, uint, uint, uint?, int, NoiseCancellationOptions?, FrameProcessor<AudioFrame>?)

Initializes a new AudioStream from a track.

public AudioStream(Track track, uint sampleRate = 48000, uint numChannels = 1, uint? frameSizeMs = null, int capacity = 0, NoiseCancellationOptions? noiseCancellation = null, FrameProcessor<AudioFrame>? frameProcessor = null)

Parameters

track Track

The audio track to stream from.

sampleRate uint

The sample rate in Hz (default 48000).

numChannels uint

The number of channels (default 1).

frameSizeMs uint?

Optional frame size in milliseconds.

capacity int

The capacity of the internal channel (0 for unbounded).

noiseCancellation NoiseCancellationOptions

Optional noise cancellation options.

frameProcessor FrameProcessor<AudioFrame>

Optional frame processor for custom audio processing.

Properties

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

Methods

Dispose()

Stops the stream, closes the frame processor and releases the native stream. Pending reads return null and later reads throw ObjectDisposedException.

public void Dispose()

DisposeAsync()

Stops the stream, closes the frame processor and releases the native stream. Pending reads return null and later reads throw ObjectDisposedException.

public ValueTask DisposeAsync()

Returns

ValueTask

A task that completes once the stream is released.

FromParticipant(Participant, TrackSource, uint, uint, uint?, int, NoiseCancellationOptions?, FrameProcessor<AudioFrame>?)

Creates an AudioStream from a participant's track source.

public static AudioStream FromParticipant(Participant participant, TrackSource trackSource, uint sampleRate = 48000, uint numChannels = 1, uint? frameSizeMs = null, int capacity = 0, NoiseCancellationOptions? noiseCancellation = null, FrameProcessor<AudioFrame>? frameProcessor = null)

Parameters

participant Participant

The participant.

trackSource TrackSource

The track source type.

sampleRate uint

The sample rate in Hz (default 48000).

numChannels uint

The number of channels (default 1).

frameSizeMs uint?

Optional frame size in milliseconds.

capacity int

The capacity of the internal channel.

noiseCancellation NoiseCancellationOptions

Optional noise cancellation options.

frameProcessor FrameProcessor<AudioFrame>

Optional frame processor for custom audio processing.

Returns

AudioStream

A new AudioStream instance.

FromTrack(Track, uint, uint, uint?, int, NoiseCancellationOptions?, FrameProcessor<AudioFrame>?)

Creates an AudioStream from an existing track.

public static AudioStream FromTrack(Track track, uint sampleRate = 48000, uint numChannels = 1, uint? frameSizeMs = null, int capacity = 0, NoiseCancellationOptions? noiseCancellation = null, FrameProcessor<AudioFrame>? frameProcessor = null)

Parameters

track Track

The track.

sampleRate uint

The sample rate in Hz (default 48000).

numChannels uint

The number of channels (default 1).

frameSizeMs uint?

Optional frame size in milliseconds.

capacity int

The capacity of the internal channel.

noiseCancellation NoiseCancellationOptions

Optional noise cancellation options.

frameProcessor FrameProcessor<AudioFrame>

Optional frame processor for custom audio processing.

Returns

AudioStream

A new AudioStream instance.

GetAsyncEnumerator(CancellationToken)

Returns an async enumerator for iterating over audio frames.

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

Parameters

cancellationToken CancellationToken

Cancellation token.

Returns

IAsyncEnumerator<AudioFrameEvent>

An async enumerator of audio frame events.

ReadAsync(CancellationToken)

Reads the next audio frame event from the stream.

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

Parameters

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask<AudioFrameEvent?>

The next audio frame event, or null if the stream has ended.

TryRead(out AudioFrameEvent)

Tries to read the next audio frame event without blocking.

public bool TryRead(out AudioFrameEvent frameEvent)

Parameters

frameEvent AudioFrameEvent

The frame event if available.

Returns

bool

True if a frame was available.