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
trackTrackThe audio track to stream from.
sampleRateuintThe sample rate in Hz (default 48000).
numChannelsuintThe number of channels (default 1).
frameSizeMsuint?Optional frame size in milliseconds.
capacityintThe capacity of the internal channel (0 for unbounded).
noiseCancellationNoiseCancellationOptionsOptional noise cancellation options.
frameProcessorFrameProcessor<AudioFrame>Optional frame processor for custom audio processing.
Properties
NumChannels
Gets the number of channels.
public uint NumChannels { get; }
Property Value
SampleRate
Gets the sample rate.
public uint SampleRate { get; }
Property Value
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
participantParticipantThe participant.
trackSourceTrackSourceThe track source type.
sampleRateuintThe sample rate in Hz (default 48000).
numChannelsuintThe number of channels (default 1).
frameSizeMsuint?Optional frame size in milliseconds.
capacityintThe capacity of the internal channel.
noiseCancellationNoiseCancellationOptionsOptional noise cancellation options.
frameProcessorFrameProcessor<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
trackTrackThe track.
sampleRateuintThe sample rate in Hz (default 48000).
numChannelsuintThe number of channels (default 1).
frameSizeMsuint?Optional frame size in milliseconds.
capacityintThe capacity of the internal channel.
noiseCancellationNoiseCancellationOptionsOptional noise cancellation options.
frameProcessorFrameProcessor<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
cancellationTokenCancellationTokenCancellation 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
cancellationTokenCancellationTokenCancellation 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
frameEventAudioFrameEventThe frame event if available.
Returns
- bool
True if a frame was available.