Remote Voices

Overview

A RemoteVoice is one incoming stream: one voice of one player. The VoiceClient creates it and hands it to the application once, as a std::shared_ptr in OnRemoteVoiceAdded.

There is no callback per frame. A decoder thread writes the decoded audio into the voice's PlaybackBuffer, and the application's audio callback reads it out at the pace of the output device.

Remote Voice Added and Removed

Both events are raised from VoiceClient::Service():

C++

RealtimeCore::Common::SubscriptionBag subscriptions;

subscriptions += voice.OnRemoteVoiceAdded.Subscribe([&](const std::shared_ptr<RemoteVoice>& remote) {
    StartPlaying(remote); // application code
});
subscriptions += voice.OnRemoteVoiceRemoved.Subscribe([&](const std::shared_ptr<RemoteVoice>& remote) {
    StopPlaying(remote); // application code
});

When a voice is added, the application hands it to the audio thread for playback. When it is removed, playback stops. The std::shared_ptr keeps the voice valid until the application lets go of it.

All remote voices are removed when the local client leaves the room or disconnects.

Remote Voice Info

GetInfo() returns the stream's RemoteVoiceInfo:

Field Meaning
PlayerNumber The sender's player number in the room.
VoiceId The sender's id for this voice.
SamplingRate Sampling rate of the stream, in Hz.
Channels Number of channels.
FrameDurationMs Length of one encoded frame, in milliseconds.
Bitrate The sender's target bit rate, in bits per second.
UserData The sender's LocalVoiceOptions::UserData; null when absent or not decodable.

PlayerNumber connects the stream to a player in the room, for example for a speaking indicator or for positional audio.

Listing Remote Voices

GetRemoteVoices() returns a copy of the current list and belongs on the service thread. It helps when UI is set up late. The two events remain the primary way to track voices.

Playback Buffer

Every remote voice owns a PlaybackBuffer. The decoder thread writes into it and the audio callback reads from it, and both sides are thread-safe.

Playback Delay

The buffer starts returning audio once PlaybackDelayMs of it is buffered, 100 ms by default. A higher delay plays more smoothly on a jittery network but adds latency. The value is set in VoiceClientOptions.

Underruns and Overruns

When the buffer runs empty, Read() pads the output with silence and the buffer goes back to buffering until the delay is reached again.

When more than VoiceClientOptions::MaxPlaybackDelayMs piles up, 400 ms by default, for example after a network stall, the oldest audio is dropped down to the playback delay. This keeps the latency bounded.

Between the two limits nothing catches up gradually. A backlog below MaxPlaybackDelayMs plays at normal speed and stays as added latency until the buffer runs empty.

Reading Audio

Read() always fills the whole span and returns how many samples came from the stream. The rest is silence. The samples are interleaved, at the sender's rate and channel count. Reading one voice into a scratch buffer, for one device callback that asks for frameCount frames:

C++

std::vector<float> scratch(frameCount * remote->GetInfo().Channels);
const std::size_t fromStream = remote->GetPlaybackBuffer().Read(scratch);

GetBufferedSamples() reports the buffered amount for diagnostics, and Clear() discards everything buffered.

Use One Stream Format

Read() returns audio in the sender's format, and nothing converts it on the way out. The recommendation is for all clients to send the same format, 48 kHz mono, with the output device running at it.

Otherwise the application resamples each voice itself, or ignores the streams it cannot play, as the Voice Quick Start does.

Additional Resources

Last updated on

Back to top