WebRTC implementation in zig

I started learning zig a few months ago and decided to create my own live media server. I tried first implementing WebRTC in zig.

WebRTC is a collection of protocols that provides peer to peer real time communication.

Since the underlying protocols may be used elsewhere, so I divided the whole multimedia pipeline into several repos.

The only protocols that are not implemented in zig are SCTP (not supported yet) and DTLS (I’m using mbedtls for now)

The implementation uses blocking code style (polling events in while loop) instead of the callbacks as defined in the WebRTC API Spec.

I included an example app where I stream an mp4 file to a browser that can be found in the repo. And the only supported codec is H264.

Any feeedback or suggestion is welcome.

Now that I’m done here, it’s time to go back and read some RFCs.

Supported Zig versions

0.16.0

AI / LLM usage disclosure

AI/LLM is not used for the code. It’s only used to generate some tests.

12 Likes

Your agent code helped me to solve several Io issues

Kudos

1 Like

Version 0.1.0 is out

The first version of the WebRTC library is now available.

This implementation tries to follow the WebRTC spec. However callbacks is a poor choice for the std.Io in Zig. For this, instead of using callbacks, we use event polling using std.Io.Queue.

The included features in this version are:

Offer/answer and SDP generation and parsing

The base for all negotiation, this is the part used for negotiating what kind of medias, codecs, extension headers and other parameters to use for both peers. This also the trickiest part to get right. Currently we support negotiating codecs and media types.

Media types

The supported video codecs are : VP8 and H264
The supported audio codecs are: Opus

ICE

This is the protocol used for establishing a connection between two peers (even behind NAT). Currently we only support IPv4 and local candidates. Support for IPv6, STUN and TURN will follow in the next releases.

DTLS

DTLS used for establishing a secure channel between the peers and used also for encrypting data from data channels (not supported yet). Media data are not encrypted by DTLS.
We use mbedtls for this (A Zig implementation is in the roadmap too).

SRTP

DTLS used for exchanging secrets and SRTP is used for encrypting media data. The supported ciphers for now are:
AES_128_CM_SHA1_80
AES_128_CM_SHA1_32

RTCP Feedback

This are used for exchanging information about the quality of the streams between peers. The supported feedback are: Sender report and PLI (which is used to tell the sender to send a new keyframe for video streams).

For usage check the examples in the repo.

3 Likes