streamSuccessAudioSwitch

Manages switching to a new audio stream, updating the audio producer, local streams, and UI state as necessary.

This function handles the transition to a new audio stream by performing several key actions:

  1. Audio Device Check: Checks if the audio device has changed. If so, it closes the current audio producer, updates the audio device ID, and prepares the new audio stream for transmission.

  2. Local Stream Update: Updates localStream and localStreamAudio with the new audio track.

  3. Transport Handling: Creates a new audio send transport if one does not exist; otherwise, it connects to the existing transport with the updated audio parameters.

  4. UI and Event Handling: Updates UI elements based on user level, screen lock status, and if video is already on.

Workflow:

  1. Extract Audio Track: Gets the audio track and device ID from the new stream

  2. Device Change Detection: Compares new device ID with current device ID

  3. Producer Management: Closes existing producer if device changed

  4. Socket Emission: Pauses audio on the server side

  5. Stream Updates: Updates local audio streams

  6. Transport Handling: Creates or connects transport as needed

  7. Pause Handling: Pauses producer if audio was paused

  8. UI Refresh: Updates UI if needed based on user level

Parameters

options

Options containing the new audio stream and parameters

Example:

val options = StreamSuccessAudioSwitchOptions(
stream = newAudioStream,
parameters = streamSuccessAudioSwitchParams,
audioConstraints = mapOf("echoCancellation" to true)
)

streamSuccessAudioSwitch(options)

Note:

Matches Flutter's stream_success_audio_switch.dart implementation.