Table of Contents

Class AudioSource

Namespace
LiveKit.Rtc
Assembly
LivekitRtc.dll

Represents a real-time audio source with an internal audio queue. The AudioSource class allows you to push audio frames into a real-time audio source, managing an internal queue of audio data.

public class AudioSource : IDisposable
Inheritance
AudioSource
Implements
Inherited Members

Constructors

AudioSource(int, int, int)

Initializes a new instance of the audio source.

public AudioSource(int sampleRate, int numChannels, int queueSizeMs = 1000)

Parameters

sampleRate int

The sample rate of the audio source in Hz.

numChannels int

The number of audio channels.

queueSizeMs int

The buffer size of the audio queue in milliseconds. Defaults to 1000 ms.

Properties

NumChannels

Gets the number of audio channels.

public int NumChannels { get; }

Property Value

int

QueuedDuration

Gets the current duration (in seconds) of audio data queued for playback.

public double QueuedDuration { get; }

Property Value

double

SampleRate

Gets the sample rate of the audio source in Hz.

public int SampleRate { get; }

Property Value

int

Methods

CaptureFrame(AudioFrame)

Captures an AudioFrame and blocks the calling thread until the native source accepts it, which can take a while when the queue is full. Prefer CaptureFrameAsync(AudioFrame, CancellationToken) in asynchronous code.

public void CaptureFrame(AudioFrame frame)

Parameters

frame AudioFrame

The audio frame to capture and queue.

CaptureFrameAsync(AudioFrame, CancellationToken)

Captures an AudioFrame and queues it for playback.

public Task CaptureFrameAsync(AudioFrame frame, CancellationToken cancellationToken = default)

Parameters

frame AudioFrame

The audio frame to capture and queue.

cancellationToken CancellationToken

Optional cancellation token.

Returns

Task

A task that completes once the native source has accepted the frame.

ClearQueue()

Clears the internal audio queue, discarding all buffered audio data.

public void ClearQueue()

CreateTrack(string)

Creates a local audio track from this audio source.

public LocalAudioTrack CreateTrack(string name = "audio")

Parameters

name string

The name of the track.

Returns

LocalAudioTrack

A new LocalAudioTrack.

Dispose()

Releases the native source. Queued audio is dropped and pending WaitForPlayoutAsync(CancellationToken) calls complete.

public void Dispose()

WaitForPlayoutAsync(CancellationToken)

Waits for the audio source to finish playing out all audio data.

public Task WaitForPlayoutAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Cancels this wait only; the queued audio keeps playing.

Returns

Task

A task that completes when all queued audio has been played out.