WebRTC Connection
WebRTC connections are used to receive the returned audio and video data from the NavTalk API. After processing your audio input via WebSocket, the digital human’s audio and video responses are delivered through WebRTC for optimal streaming performance.In the new unified API, WebRTC signaling messages (offer, answer, ICE candidates) are sent through the same WebSocket connection used for real-time API communication. You no longer need a separate WebRTC WebSocket connection.
Handle WebRTC Signaling Through Unified WebSocket
In the new unified API, WebRTC signaling messages are sent and received through the same WebSocket connection used for real-time API communication. You need to handle WebRTC events in your message handler:
Important: WebRTC signaling is now integrated into the unified WebSocket connection. You no longer need to:
- Create a separate WebRTC WebSocket connection
- Use
sessionIdasuserIdquery parameter - Send
{ type: 'create', targetSessionId: ... }message
Handle WebRTC Offer
When you receive an offer from the server, create a peer connection, set the remote description, create an answer, and send it back:
Handle ICE Candidates
Exchange ICE candidates to establish the optimal network path for media streaming:
Video Stream Display
The video stream is automatically handled by the
ontrack event handler set up in Step 2 (Handle WebRTC Offer). When the WebRTC connection is established and media tracks are received, the ontrack event fires and displays the video in your HTML video element.Important points:- The
ontrackhandler is set up inside thehandleOfferfunction (Step 2), right after creating the RTCPeerConnection - This ensures the handler is ready before any tracks arrive
- The video element ID should match what you use in your HTML:
<video id="character-video" autoplay playsinline></video>
- Server sends WebRTC offer →
handleOfferis called handleOffercreates RTCPeerConnection and sets upontrackhandler- When media tracks arrive →
ontrackevent fires automatically - Video is displayed in the HTML video element
The
ontrack event handler is set up once when creating the peer connection. You don’t need to set it up again elsewhere. All incoming audio/video tracks will automatically trigger this handler.Optional: Handle Renegotiation (Advanced)
In some advanced scenarios, you may need to handle WebRTC renegotiation. This happens when the connection needs to be re-established (e.g., after adding/removing media tracks, changing codecs, etc.). In such cases, you may need to send an offer:
When is renegotiation needed?
- Typically, the server sends the initial offer, and you respond with an answer
- Renegotiation is only needed in advanced scenarios, such as:
- Dynamically adding/removing media tracks
- Changing codec preferences
- Recovering from connection failures
- In most cases, you won’t need to handle
onnegotiationneededor callsendOfferMessage