Skip to main content
Version: 20 Mar 2024

AudioSink

Class that represents an audio sink used by the MLWebRTC API.

Inherits from:
Sink

Public Fields

CopyRawAudioDataToManagedMemory

Raw audio data received in the OnAudioDataAvailable_NativeCallbackThread delegate should be converted to float samples and copied from unmanaged to managed memory. Audio data received in OnAudioDataAvailable will always be available in managed memory. This is set to true by default. Disable this flag if you experience too many garbage collection invocations for MLAudio.Buffer.Samples & handle the audio data conversion from the unmanaged to managed and from Int to Float yourself.


public bool CopyRawAudioDataToManagedMemory { get; set; }


CurrentServiceStatus

Current status of the underlying audio service. Apps should use this status to determine whether its safe to set the audio attriutes like soundfield parameters and volume.


public ServiceStatus CurrentServiceStatus { get; set; }

TypeDescription
ServiceStatusStatus of the underlying Audio Service the buffers are submitted & soundfield attributes applied to.

Mode

Audio buffer notification mode for this AudioSink


public BufferNotifyMode Mode { get; set; }

TypeDescription
BufferNotifyModeBehavior of the sink if audio buffer data is provided to the app in a callback.

Public Methods

AudioSink Create

Creates an initialized AudioSink object.

public static AudioSink Create(
out MLResult result,
BufferNotifyMode mode =BufferNotifyMode.None
)

Parameters

TypeNameDescription
out MLResultresultThe MLResult object of the inner platform call(s).
BufferNotifyModemodeBehavior of the sink if audio buffer data is provided to the app in a callback.

Returns: An initialized AudioSink object.


override MLResult Destroy

Destroys this audio sink object.

public virtual override MLResult Destroy()

Returns: MLResult.Result will be MLResult.Code.Ok if destroying all handles was successful. MLResult.Result will be MLResult.Code.WebRTCResultInstanceNotCreated if MLWebRTC instance was not created. MLResult.Result will be MLResult.Code.WebRTCResultMismatchingHandle if an incorrect handle was sent.

Reimplements: Destroy


delegate void OnAudioDataAvailableDelegate

Delegate to provide audio buffer data received for this audio sink.

public delegate void OnAudioDataAvailableDelegate(
MLAudioOutput.Buffer buffer
)

Parameters

TypeNameDescription
MLAudioOutput.BufferbufferFormatFormat of the audio buffer
bufferAudio buffer

delegate void OnAudioServiceStatusChangedDelegate

Delegate to provide the current audio service status. Apps should use this status to determine whether its safe to set the audio attriutes like soundfield parameters and volume.

public delegate void OnAudioServiceStatusChangedDelegate(
ServiceStatus status
)

Parameters

TypeNameDescription
ServiceStatusstatusThe current status of the audio service

MLResult ResetPosition

Resets the world position of the audio sink for spatialized audio.

public MLResult ResetPosition()

Returns: MLResult.Result will be MLResult.Code.Ok if destroying all handles was successful. MLResult.Result will be MLResult.Code.WebRTCResultInstanceNotCreated if MLWebRTC instance was not created. MLResult.Result will be MLResult.Code.WebRTCResultMismatchingHandle if an incorrect handle was sent.


MLResult SetCacheSize

Sets the number of milliseconds of audio that should be cached in the buffers before dropping the packets. Dictates the audio latency when app recovers from lifecycle state transitions like standby & reality. Default is 200ms.

public MLResult SetCacheSize(
uint millisecondsToCache
)

Parameters

TypeNameDescription
uintsinkHandleThe handle to the audio sink.
millisecondsToCacheHow many milliseconds worth of audio to cache.

Returns: MLResult.Result will be MLResult.Code.Ok if destroying all handles was successful. MLResult.Result will be MLResult.Code.WebRTCResultInstanceNotCreated if MLWebRTC instance was not created. MLResult.Result will be MLResult.Code.WebRTCResultMismatchingHandle if an incorrect handle was sent.


MLResult SetDirectSendLevels

Sets the direct send levels of the audio sink for the specified channel.

public MLResult SetDirectSendLevels(
MLAudioOutput.SpatialSound.SendLevels sendLevels,
int channel =-1
)

Parameters

TypeNameDescription
MLAudioOutput.SpatialSound.SendLevelssendLevelsThe send levels of the audio sink.
intchannelChannel to set this spatialization property for. Passing -1 sets it for all available channels.

Returns: MLResult.Result will be MLResult.Code.Ok if destroying all handles was successful. MLResult.Result will be MLResult.Code.WebRTCResultInstanceNotCreated if MLWebRTC instance was not created. MLResult.Result will be MLResult.Code.WebRTCResultMismatchingHandle if an incorrect handle was sent. MLResult.Result will be MLResult.Code.InvalidParam if an invalid parameter was passed.


MLResult SetDistanceProperties

Sets the distance properties of the audio sink for the specified channel.

public MLResult SetDistanceProperties(
MLAudioOutput.SpatialSound.DistanceProperties distanceProperties,
int channel =-1
)

Parameters

TypeNameDescription
MLAudioOutput.SpatialSound.DistancePropertiesdistancePropertiesThe distance properties of the audio sink.
intchannelChannel to set this spatialization property for. Passing -1 sets it for all available channels.

Returns: MLResult.Result will be MLResult.Code.Ok if destroying all handles was successful. MLResult.Result will be MLResult.Code.WebRTCResultInstanceNotCreated if MLWebRTC instance was not created. MLResult.Result will be MLResult.Code.WebRTCResultMismatchingHandle if an incorrect handle was sent. MLResult.Result will be MLResult.Code.InvalidParam if an invalid parameter was passed.


MLResult SetOrientation

Sets the world orientation of the audio sink for the specified channel.

public MLResult SetOrientation(
Quaternion orientation,
int channel =-1
)

Parameters

TypeNameDescription
QuaternionorientationThe orientation of the audio sink.
intchannelChannel to set this spatialization property for. Passing -1 sets it for all available channels.

Returns: MLResult.Result will be MLResult.Code.Ok if destroying all handles was successful. MLResult.Result will be MLResult.Code.WebRTCResultInstanceNotCreated if MLWebRTC instance was not created. MLResult.Result will be MLResult.Code.WebRTCResultMismatchingHandle if an incorrect handle was sent. MLResult.Result will be MLResult.Code.InvalidParam if an invalid parameter was passed.


MLResult SetPosition

Sets the world position of the audio sink for the specified channel.

public MLResult SetPosition(
Vector3 position,
int channel =-1
)

Parameters

TypeNameDescription
Vector3positionThe position to set the audio sink to.
intchannelChannel to set this spatialization property for. Passing -1 sets it for all available channels.

Returns: MLResult.Result will be MLResult.Code.Ok if destroying all handles was successful. MLResult.Result will be MLResult.Code.WebRTCResultInstanceNotCreated if MLWebRTC instance was not created. MLResult.Result will be MLResult.Code.WebRTCResultMismatchingHandle if an incorrect handle was sent. MLResult.Result will be MLResult.Code.InvalidParam if an invalid parameter was passed.


MLResult SetRadiationProperties

Sets the radiation properties of the audio sink for the specified channel.

public MLResult SetRadiationProperties(
MLAudioOutput.SpatialSound.RadiationProperties radiationProperties,
int channel =-1
)

Parameters

TypeNameDescription
MLAudioOutput.SpatialSound.RadiationPropertiesradiationPropertiesThe radiation properties of the audio sink.
intchannelChannel to set this spatialization property for. Passing -1 sets it for all available channels.

Returns: MLResult.Result will be MLResult.Code.Ok if destroying all handles was successful. MLResult.Result will be MLResult.Code.WebRTCResultInstanceNotCreated if MLWebRTC instance was not created. MLResult.Result will be MLResult.Code.WebRTCResultMismatchingHandle if an incorrect handle was sent. MLResult.Result will be MLResult.Code.InvalidParam if an invalid parameter was passed.


MLResult SetRoomSendLevels

Sets the room send levels of the audio sink for the specified channel.

public MLResult SetRoomSendLevels(
MLAudioOutput.SpatialSound.SendLevels sendLevels,
int channel =-1
)

Parameters

TypeNameDescription
MLAudioOutput.SpatialSound.SendLevelssendLevelsThe send levels of the audio sink.
intchannelChannel to set this spatialization property for. Passing -1 sets it for all available channels.

Returns: MLResult.Result will be MLResult.Code.Ok if destroying all handles was successful. MLResult.Result will be MLResult.Code.WebRTCResultInstanceNotCreated if MLWebRTC instance was not created. MLResult.Result will be MLResult.Code.WebRTCResultMismatchingHandle if an incorrect handle was sent. MLResult.Result will be MLResult.Code.InvalidParam if an invalid parameter was passed.


MLResult SetStream

Sets the stream of the audio sink.

public MLResult SetStream(
MediaStream stream
)

Parameters

TypeNameDescription
MediaStreamstreamThe stream to use.

Returns: MLResult.Result will be MLResult.Code.Ok if destroying all handles was successful. MLResult.Result will be MLResult.Code.WebRTCResultInstanceNotCreated if MLWebRTC instance was not created. MLResult.Result will be MLResult.Code.WebRTCResultMismatchingHandle if an incorrect handle was sent.


MLResult SetVolume

Sets the volume of the audio sink for the specified channel. The range of the volume is 0 to 8, with 0 for silence, 1 for unity gain, and 8 for 8x gain.

public MLResult SetVolume(
float volume
)

Parameters

TypeNameDescription
floatvolumeThe volume of the audio sink.

Returns: MLResult.Result will be MLResult.Code.Ok if destroying all handles was successful. MLResult.Result will be MLResult.Code.WebRTCResultInstanceNotCreated if MLWebRTC instance was not created. MLResult.Result will be MLResult.Code.WebRTCResultMismatchingHandle if an incorrect handle was sent. MLResult.Result will be MLResult.Code.InvalidParam if an invalid parameter was passed.


Protected Methods

SetTrack

Sets the track of the audio sink.

protected virtual override MLResult SetTrack(
MLWebRTC.MediaStream.Track track
)

Parameters

TypeNameDescription
MLWebRTC.MediaStream.TracktrackThe track to use.

Returns: MLResult.Result will be MLResult.Code.Ok if destroying all handles was successful. MLResult.Result will be MLResult.Code.WebRTCResultInstanceNotCreated if MLWebRTC instance was not created. MLResult.Result will be MLResult.Code.WebRTCResultMismatchingHandle if an incorrect handle was sent.

Reimplements: SetTrack


Public Events

OnAudioDataAvailable

Delegate invoked on the main thread to provide the audio buffer data for this sink. MLAudio.Buffer.NativeDataPtr is NOT valid for this delegate. Use the managed MLAudio.Buffer.Samples array instead.

public OnAudioDataAvailableDelegate OnAudioDataAvailable()

OnAudioDataAvailable_NativeCallbackThread

Delegate invoked on the native callback thread to provide the audio buffer data for this sink. MLAudio.Buffer.NativeDataPtr is valid for this delegate.

public OnAudioDataAvailableDelegate OnAudioDataAvailable_NativeCallbackThread()

OnAudioServiceStatusChanged

Delegate invoked on the main thread to notify the app about the current status of the underlying audio service. Apps should use this tatus to determine whether its safe to set the audio attriutes like soundfield parameters and volume.

public OnAudioServiceStatusChangedDelegate OnAudioServiceStatusChanged()

Public Enums

BufferNotifyMode

Behavior of the sink if audio buffer data is provided to the app in a callback.

EnumeratorValueDescription
NoneDon't provide app with audio buffers, consume them in the underlying lib and play directly on the device.
NotifyAndPlayProvide the app with audio buffers in a callback, AND also play the audio directly on the device. Ths is useful where apps want to use the buffers to calculate something (like RMS) but not take the responsibility of actually playing the audio.
NotifyOnlyProvide the app with audio buffers in a callback. The underlyin library will NOT play the audio. It is the app's responsibility to submit the audio buffers to the device.

ServiceStatus

Status of the underlying Audio Service the buffers are submitted & soundfield attributes applied to.

EnumeratorValueDescription
InitialThe audio service has been initialized but not ready to accept updates to any attributes (soundfield or volume).
StartedThe audio service has started. It is now safe to update the audio attributes (soundfield and volume).
FailedAudio service has failed. This will cause disruption in audio playback. Audio attributes (soundfield or volume) should not be set after receiving this status update.
StoppedAudio service has stopped. Audio attributes (soundfield or volume) should not be set after receiving this status update.
UnknownAudio service status is unknown. Audio attributes (soundfield or volume) should not be set after receiving this status update.