sipsorcery-org / sipsorcery

A WebRTC, SIP and VoIP library for C# and .NET. Designed for real-time communications apps.
https://sipsorcery-org.github.io/sipsorcery
Other
1.42k stars 432 forks source link

Switching audio input and output device #960

Open mail2mhossain opened 1 year ago

mail2mhossain commented 1 year ago

We are trying to switch audio source (SDL2AudioSource) on a live MediaSteamTrack in an open and connected RTCPeerConnection.

We are trying to switch audio endpoint (SDL2AudioEndPoint) on a live and connected RTCPeerConnection.

What we are doing:

  1. Closing old source and end point _audioSource.OnAudioSourceEncodedSample -= _peerConnection.SendAudio; _peerConnection.OnRtpPacketReceived -= _peerConnection_OnRtpPacketReceived; _peerConnection.removeTrack(_audioInputTrack); await CloseAudioDevices();

  2. Initializing new source and end point with the changed device name

_audioSource = new SDL2AudioSource(NewAudioInputName, _audioEncoder, (uint)_frameSize); _audioEndPoint = new SDL2AudioEndPoint (NewAudioOutputName, _audioEncoder);

_audioInputTrack = new MediaStreamTrack( _audioSource.GetAudioSourceFormats(), MediaStreamStatusEnum.SendRecv);

_peerConnection.addTrack(_audioInputTrack);

_audioSource.OnAudioSourceEncodedSample += _peerConnection.SendAudio; _peerConnection.OnRtpPacketReceived += _peerConnection_OnRtpPacketReceived;

_peerConnection.OnAudioFormatsNegotiated += (audioFormats) => { _audioSource.SetAudioSourceFormat(audioFormats.First());
_audioEndPoint.SetAudioSinkFormat(audioFormats.First()); };

await StartAudioVideoDevice();

BUT cannot get audio.

Is there anything wrong?

ChristopheI commented 1 year ago

Event OnAudioFormatsNegotiated will be raised only if a renegociation is done. Which seems not the case in your case. You want to change a device (for example an headset with another one) not the codec used between peers. Also you don't have to add or remove track in the PeerConnection,

You have to store the AudioFormat currently used. Open the new device using the current AudioFormat Init / Start the new device Use this new device instead of the older one. Close the older one