Table of Contents

VAST.WebRTC Library

The VAST.WebRTC library provides WebRTC support for VASTreaming applications. It is based on the Google native WebRTC library and enables real-time peer-to-peer media streaming between browsers and servers, with built-in signaling, ICE negotiation, and optional media transcoding.

Overview

Feature Description
WebRTC Signaling Server Accept incoming WebRTC connections from browser clients and publishers
Standalone Signaling Server Room-based peer-to-peer signaling for direct browser-to-browser communication
WebRTC Client Receive WebRTC streams in .NET applications
Transcoding On-the-fly video and audio transcoding for codec compatibility
ICE Configuration STUN/TURN server configuration for NAT traversal
Transport Control Force UDP or TCP for media transport

Requirements

  • .NET: .NET 6.0 or later
  • Dependencies: VAST.Common
  • Native Library: Platform-specific WebRTC native library (configured via WebRtcGlobal)
  • Supported Platforms: Android, iOS, Windows, Debian

Because the library relies on a platform-specific native WebRTC library, it is currently available on the platforms listed above. For other platforms support, contact info@vastreaming.net.

Supported Codecs

Video

Codec Description
H.264 Primary codec for browser compatibility

Audio

Codec Description
Any Any audio codec supported by Google native WebRTC library

Signaling

WebRTC requires a signaling mechanism to exchange session descriptions (SDP offers/answers) and ICE candidates between peers. The library provides two signaling server implementations, both supporting HTTP long-polling and WebSocket transports.

Signaling Endpoints

Endpoint Method Description
/sign-in GET Register a peer with the signaling server
/wait GET Long-poll for incoming messages
/message GET/POST Send a message to another peer
/sign-out GET Disconnect from the signaling server

Signaling Transports

Transport Description
HTTP Long-Polling Legacy transport using sequential GET requests
WebSocket Persistent bidirectional connection for lower latency signaling

WebRTC Signaling Server

The WebRtcSignalingServer handles incoming WebRTC connections from browser clients and publishers, and manages media streaming through publishing points.

Browser clients connect to receive media from existing publishing points. Browser publishers can also stream media to the server; when a browser publishes via WebRTC, the StreamingServer automatically creates a new publishing point with a WebRtcPublisherSource (internal object) as the media source. This publishing point can then be consumed by other protocols (HLS, SRT, RTSP, etc.) like any other publishing point.

Note

WebRtcSignalingServer is not available as a standalone server. It operates as part of the VAST.Network.StreamingServer infrastructure and is instantiated by the StreamingServer automatically when enabled.

Integration with StreamingServer

When used as part of StreamingServer, the WebRTC signaling server shares the HTTP endpoints with other HTTP-based protocols:

var server = new VAST.Network.StreamingServer();

// Enable HTTP (required for WebRTC)
server.EnableHttp = true;

// Enable WebRTC
server.EnableWebRtc = true;
server.WebRtcServerParameters = new VAST.WebRTC.WebRtcServerParameters
{
    WebRtcPath = "/rtc",
    IceServers = "stun:stun.l.google.com:19302",
};

server.Start();

Server Parameters

Configure server behavior using WebRtcServerParameters:

Parameter Default Description
WebRtcPath null Path relative to the root HTTP server path for resolving WebRTC requests; null means the root path is used
IceServers null ICE server configuration string (e.g., stun:stun.l.google.com:19302); defaults to Google STUN server when not set
MediaTransport Auto Media transport protocol: Auto, ForceTcp, or ForceUdp
VideoTranscoding PassThrough Video transcoding mode: PassThrough, TranscodeIncompatible, ForceTranscode
VideoDecoderParameters null Video decoder configuration for transcoding
AudioDecoderParameters null Audio decoder configuration for transcoding
VideoEncoderParameters null Video encoder configuration for transcoding
AudioEncoderParameters null Audio encoder configuration for transcoding

Media Transport

Transport Description
Auto Automatically selects the best available transport protocol (default)
ForceTcp Forces TCP for media transport; useful in restrictive network environments
ForceUdp Forces UDP for media transport; lowest latency

Standalone Signaling Server

The WebRtcStandaloneSignalingServer provides room-based peer-to-peer signaling for direct browser-to-browser communication. Unlike the WebRtcSignalingServer, it does not process media on the server side; it only relays signaling messages between peers.

Usage

Create a standalone signaling server with its own HTTP server:

var server = new VAST.WebRTC.WebRtcStandaloneSignalingServer(
    httpPort: 8080,
    httpsPort: 8443,
    path: "/",
    parameters: new VAST.WebRTC.WebRtcServerParameters
    {
        IceServers = "stun:stun.l.google.com:19302",
    });

server.Start();

Or attach to an existing HTTP server:

var server = new VAST.WebRTC.WebRtcStandaloneSignalingServer(
    httpServer: existingHttpServer,
    path: "/signaling",
    parameters: new VAST.WebRTC.WebRtcServerParameters
    {
        IceServers = "stun:stun.l.google.com:19302",
    });

server.Start();

Authorization

The Authorize event is fired when a new peer connects, allowing custom authorization logic:

server.Authorize += (sender, e) =>
{
    // e.InboundUri - the request URI
    // e.RoomId - the room the peer is joining
    // e.EndPoint - the peer's network endpoint
    // Set e.Accept = false to reject the connection
    e.Accept = IsAuthorized(e.RoomId, e.EndPoint);
};

Rooms

Peers are organized into rooms. Each room is identified by a room query parameter in the sign-in request. Peers within the same room can exchange signaling messages with each other.

http://server:8080/sign-in?room=my-room

WebRTC Client

The WebRtcClientSource class receives WebRTC streams in .NET applications. It implements INetworkSource.

Usage

var source = new VAST.WebRTC.WebRtcClientSource();
source.IceServers = "stun:stun.l.google.com:19302";

source.NewStream += (sender, e) =>
{
    Console.WriteLine($"Stream {e.StreamIndex}: {e.MediaType}");
};

source.NewSample += (sender, e) =>
{
    ProcessSample(e.Sample);
};

source.StateChanged += (sender, state) =>
{
    if (state == MediaState.Opened)
    {
        source.Start();
    }
};

source.Open();

Properties

Property Default Description
IceServers stun:stun.l.google.com:19302 ICE server configuration string
IsVideoExpected Whether a video stream is expected
IsAudioExpected Whether an audio stream is expected
Signalling Signaling interface for sending messages to the remote peer
OurPeerId Local peer identifier
RemotePeerId Remote peer identifier

Third-Party Signaling Servers

WebRtcClientSource can be used with third-party signaling servers (e.g., custom WebSocket-based servers) by providing a custom implementation of the IWebRtcSignalling interface.

The IWebRtcSignalling interface has a single method:

void SendToPeer(object caller, long peerId, string data);

To integrate with a third-party signaling server:

  1. Implement IWebRtcSignalling to send signaling messages (SDP offers/answers and ICE candidates) to the remote peer via your signaling server.
  2. Assign the implementation to Signalling.
  3. When a signaling message is received from the remote peer, call ProcessPeerMessage(long, string) to deliver it to the WebRTC session.
var source = new VAST.WebRTC.WebRtcClientSource();
source.Signalling = new MySignallingImplementation();
source.OurPeerId = ourId;
source.RemotePeerId = remoteId;

// When a message arrives from the signaling server:
source.ProcessPeerMessage(remoteId, incomingMessage);

For a complete working example, see the MAUI WebRTC Demo Application.

Global Configuration

Configure library-wide settings using WebRtcGlobal:

Setting Default Description
DynamicLibrary32BitPath null File path to the 32-bit native WebRTC dynamic library
DynamicLibrary64BitPath null File path to the 64-bit native WebRTC dynamic library

Troubleshooting

Common Issues

Issue Cause Solution
ICE connection failed STUN/TURN server unreachable or NAT restrictions Verify ICE server configuration; add a TURN server for symmetric NAT
No video/audio Codec mismatch between peers Enable transcoding via VideoTranscoding parameter
Connection timeout Signaling messages not reaching peer Check firewall rules for HTTP/WebSocket and UDP ports
Native library not found Missing platform-specific WebRTC DLL Set WebRtcGlobal.DynamicLibrary64BitPath to the correct path
Peer not found Peer disconnected or timed out Peers are removed after 30 seconds of inactivity

Network Requirements

Protocol Port Description
TCP 80/443 HTTP/HTTPS for signaling
UDP Dynamic WebRTC media transport (ICE-negotiated ports)
UDP 3478/19302 STUN server (for NAT traversal)
TCP/UDP 3478 TURN server (for relayed media, if configured)

See Also