How to play sound from WebRTC peer connection?

We use WebRTC package in my unity project to add voicechat. We get my audio as AudioStreamTrack from the peer connection. For now we used audioSource.SetTrack(myAudioStreamTrack); //an extension method which is available when using Unity.WebRTC namespace.

But if we want to use FMOD for Unity, can we play AudioStreamTrack sound? And can we get and send the input from the microphone?

Thanks for your help

Hi,

FMOD doesn’t support WebRTC, but if you can retrieve the audio buffer of an incoming audio stream track and access the raw audio data, you’ll be able to stream it into an FMOD Sound for playback. You can either do this in an FMOD_SOUND_PCMREAD_CALLBACK, or by getting a pointer to the sound’s buffer with Sound.lock, and copying your raw audio data to it with Sound.unlock. Note that the second method requires you to manually schedule copying audio data yourself, as you won’t be told when to give it data.

I’d recommend taking a look at the user_created_sound.cpp example in the FMOD Core API examples (found in the FMOD Studio API install - ./api/core/examples/) as a reference on how to use FMOD_SOUND_PCMREAD_CALLBACK, though note that with C# you’ll want to use Marshal.Copy() instead of manually iterating through the data to copy it.

1 Like