Voice Client
Overview
VoiceClient adds voice to one RealtimeClient. The application keeps managing the connection and the rooms through the RealtimeClient, and the VoiceClient follows it.
Creating a VoiceClient
The constructor takes the RealtimeClient and, optionally, VoiceClientOptions:
C++
VoiceClientOptions options;
options.PlaybackDelayMs = 150;
VoiceClient voice(client, options);
A VoiceClient can be neither copied nor moved. Where it cannot live on the stack, hold it as a member or behind a std::unique_ptr.
Each RealtimeClient gets one VoiceClient. A second one would receive the same voice events and play every stream twice.
Create the VoiceClient before joining a room. It starts its streams when the RealtimeClient raises OnRoomJoined. One created inside a room stays silent until the next join.
VoiceClientOptions
PlaybackDelayMs is the amount of audio a remote voice buffers before playback starts, 100 ms by default. MaxPlaybackDelayMs is the amount of buffered audio above which the oldest audio is dropped, 400 ms by default.
The two delays are copied into the PlaybackBuffer of each remote voice as it is created. Their effect is covered in Playback Buffer.
The Service Loop
Call Service() every tick, on the thread that services the RealtimeClient, right after RealtimeClient::Service():
C++
while (running)
{
client.Service();
voice.Service();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
Service() sends the frames the encoder threads have queued since the last call, in one batch. The tick interval adds to the latency. 10 ms or less is recommended.
OnRemoteVoiceAdded and OnRemoteVoiceRemoved are raised from Service(). Their handlers run on the service thread.
Room Lifecycle
When the client joins a room, all local voices are announced to the players in it. Players who join later receive the announcement automatically.
A remote voice is added when its announcement arrives. It is removed when the sender destroys it or leaves the room, and when the local client leaves the room or disconnects.
Leaving a room does not remove the local voices. They stay with the application. Outside a room, pushed audio is discarded and nothing is sent. On the next join the voices are announced again without being recreated.
Frames that are encoded but not yet sent when the client leaves a room are dropped.
Object Lifetime and Shutdown Order
A LocalVoice is a std::unique_ptr, and destroying it removes the stream for every receiver. A RemoteVoice is a std::shared_ptr and stays valid for as long as the application holds it, for example on the audio thread after it has been removed.
Shut down in this order: stop the audio stream, destroy the local voices, destroy the VoiceClient, then the RealtimeClient. The VoiceClient must never outlive its RealtimeClient.
Statistics
GetStats() reads the counters live and returns them as VoiceStats:
| Field | Meaning |
|---|---|
FramesSent |
Encoded frames sent by the local voices. |
BytesSent |
Encoded bytes sent by the local voices. |
FramesReceived |
Frames received from remote voices. |
FramesLost |
Frames of remote voices that never arrived. |
Printing all four counters:
C++
const VoiceStats stats = voice.GetStats();
std::printf("sent %d frames (%d bytes), received %d, lost %d\n",
stats.FramesSent, stats.BytesSent, stats.FramesReceived, stats.FramesLost);
FramesReceived and FramesLost are totals since the VoiceClient was created. FramesSent and BytesSent sum over the local voices that exist right now: destroying a LocalVoice removes its counts, and a new one starts at 0. That includes recreating a voice to change its format or its input rate, for example after switching microphones. Connecting, joining, leaving and the LocalVoice setters reset nothing.
The counters show whether a stream is flowing and how much of it is lost. Connection-level numbers such as round-trip time come from the Realtime Core Statistics.
Reserved Event Codes
Voice uses the event codes 202 for stream control and 203 for audio frames, published as RealtimeCore::Voice::EventCode::VOICE_CONTROL and VOICE_FRAME in VoiceEventCode.h. Both are part of the Photon Voice wire protocol and the same in every Photon Voice client.
The game's own SendEvent() codes must avoid them. Reusing 202 or 203 feeds game payloads to the voice client and silently breaks audio in both directions. A compile-time check keeps a game event code clear of both:
C++
#include "RealtimeCore/Voice/VoiceEventCode.h"
constexpr uint8_t ChatMessageCode = 10;
static_assert(ChatMessageCode != RealtimeCore::Voice::EventCode::VOICE_CONTROL
&& ChatMessageCode != RealtimeCore::Voice::EventCode::VOICE_FRAME);
Additional Resources
Last updated on
Back to top