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.

14 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.

6 Likes

Continuing on my Zig’s learning journey, version 0.2.0 of WebRTC is out. The notable changes in this release are:

  • Add support for IPv6 and STUN/TURN candidates. More options for peers to connect together.
  • Add RTP mid header extension for identifying which rtp packets belongs to which stream.
  • Add support for RTCP generic nack and RTX. Generic nacks are a type of a feedback where the receiver signals to the sender which packets are lost and request retransmission either using the main stream used for sending or via RTX.
  • Switch from polling using std.Io.Queue to using callbacks via handler (define only which callbacks you’re interested in). As it turns out callbacks are more efficient in this case because the Io.Queue listener may be started in another thread/fiber which forces a wakeup per rtp packet.
  • Add a media engine to limit/register only codecs supported by the user.
  • Add more configuration options to the PeerConnection to control the above features.
1 Like

Btw, this is not the case with zio in the default mode. If you push to a queue and another task was waiting for it, the task will be resumed on the same thread where you pushed from, so no wakeup at all.

1 Like

Actually I made the benchmarks with zio and I tried a lot of stuff and I thought the queue was one of the issues.

And I discovered the real reason is that the UDP socket opened in one coroutine and then used for receiving data in another one forced a migration across executors and that was the biggest performance issue.

Anyway the zio library is really cool. Even with release safe the benchmark shows promising results.

Ah, yes, that can happen on epoll/kqueue. I plan to fix that soon.

Zigouat WebRTC v0.3.0 is released. This release brings big changes to the architecture, I moved from callbacks to a sans-io architecture, so it’s more of a whole rewrite of all the components (ICE, STUN, TURN, DTLS, SCTP and PeerConnection, …etc). The stack no longer does any kind of I/O, so the user is responsible for driving the sockets and timers.

The second big addition is complete support for data channels (ordered and unordered messages, reliable, partial reliability and stream reset).

At first I used usrsctp for the SCTP protocol which data channels use. Since I moved to a sans-io architecture, usrsctp is no longer a viable solution. The other choice is digging dcSctp out of libwebrtc, instead I created my own SCTP implementation is Zig.


Data channels benchmark

I stumbled upon this article about an improvement in webrc-rs sctp and a benchmark against pion. So I tried my solution against this stacks.

The stacks

Stack Version Bench source
Zigouat webrtc 0.3.0, zio 0.19.1, Zig 0.16.0 data-channels-flow-multi
rustrtc 0.4.10 LLM Generated
webrtc-rs 0.21.1 data-channels-flow-control-multi example
Pion v4.2.26, Go 1.26 From the article From 13 Mbps to beating Pion

The benchmark configuration

  • Messages: 1 KiB each, one message per SCTP DATA chunk.
  • Flow control: max buffered amount of 1 MiB, and bufferedamountlow event (threshold 512 KiB).
  • SCTP receive window: 1 MiB.
  • Network: one host candidate on 127.0.0.1 over UDP4, with no mDNS and no STUN or TURN.
  • Flow Control: controlled by FLOW_ORDERED=value, 1 means: ordered and reliable and 0 means unreliable with max_retransmits=0
  • Size: every scenario runs with 1 pair and with 10 pairs, with FLOW_WARMUP_MB=16 and FLOW_STOP_MB=128 per pair.
  • DTLS: All the stacks use AES-GCM as the ciphersuite for dtls

I/O differences

Webrtc-rs: UDP GSO/GRO
rustrtc: Sendmsg/recvmsg with draining (equivalent to Socket.receiveManyTimeout in Zig)
Zigouat: recvmmsg
Pion: Whatever golang is using

The scenarios:

The test machine is: AMD EPYC 7502P 32c/64t, 256 GB memory.
The values are averaged over 5 runs.

rustrtc is excluded from unordered scenarios because of a bug and pion is excluded from pinned scenarios.

The RESULTS

Scenario Stack Throughput (Mibit/s) Wall time (s) Peak RSS (MB)
1 pair, work stealing, ordered Zig 676 (608–773) 2.00 9.1 (9.6)
rustrtc 527 (485–582) 2.22 24.5 (25.4)
webrtc-rs 679 (414–814) 2.39 25.4 (26.6)
Pion 223 (221–227) 5.14 28.6 (29.0)
1 pair, work stealing, unordered Zig 705 (679–770) 1.92 9.4 (9.9)
webrtc-rs 702 (661–749) 2.28 24.5 (27.0)
Pion 220 (218–223) 5.19 28.4 (28.9)
1 pair, pinned, ordered Zig 1,198 (1,158–1,235) 1.25 7.6 (8.2)
rustrtc 853 (847–864) 1.38 10.9 (11.1)
webrtc-rs 1,310 (872–1,640) 1.60 17.5 (19.4)
1 pair, pinned, unordered Zig 1,203 (1,166–1,229) 1.24 7.6 (8.0)
webrtc-rs 1,523 (1,223–1,717) 1.42 15.9 (17.5)
10 pairs, work stealing, ordered Zig 5,554 (5,321–5,653) 2.37 46.9 (51.4)
rustrtc 5,136 (5,050–5,246) 2.39 87.7 (91.9)
webrtc-rs 5,600 (5,083–5,963) 3.62 385.4 (1,217.6)
Pion 1,570 (1,551–1,597) 7.46 116.8 (125.8)
10 pairs, work stealing, unordered Zig 5,546 (5,483–5,640) 2.37 48.6 (52.9)
webrtc-rs 5,774 (5,622–5,844) 3.10 208.3 (329.6)
Pion 1,538 (1,528–1,543) 7.61 114.2 (118.1)
10 pairs, pinned, ordered Zig 10,324 (10,189–10,400) 1.51 24.7 (24.9)
rustrtc 8,508 (8,331–8,584) 1.50 28.2 (29.0)
webrtc-rs 10,835 (9,464–12,914) 3.68 280.1 (384.1)
10 pairs, pinned, unordered Zig 9,945 (9,745–10,294) 1.61 24.7 (25.1)
webrtc-rs 12,324 (11,619–13,118) 2.41 157.7 (186.3)

webrtc-rs wins in terms of throughput (however GRO/GSO played a big part here).
Zig wins memory wise.

If we do the same tests with random messages between 800-1024 bytes we have the following results:

Scenario Stack Throughput (Mibit/s) Wall time (s) Peak RSS (MB)
1 pair, work stealing, ordered Zig 695 (604–761) 1.96 10.4 (10.9)
rustrtc 455 (427–492) 2.56 25.5 (26.3)
webrtc-rs 541 (518–563) 2.81 115.3 (141.0)
Pion 199 (197–202) 5.74 28.5 (29.1)
1 pair, work stealing, unordered Zig 679 (613–746) 1.99 10.7 (11.4)
webrtc-rs 559 (547–583) 2.74 102.8 (119.8)
Pion 202 (194–204) 5.69 28.7 (29.4)
1 pair, pinned, ordered Zig 1,088 (1,013–1,125) 1.35 8.3 (8.5)
rustrtc 783 (776–796) 1.49 11.1 (11.4)
webrtc-rs 776 (760–796) 2.14 45.2 (50.0)
1 pair, pinned, unordered Zig 1,053 (931–1,114) 1.38 8.3 (8.5)
webrtc-rs 789 (773–802) 2.14 44.5 (54.9)
10 pairs, work stealing, ordered Zig 5,005 (4,936–5,126) 2.59 69.1 (70.5)
rustrtc 4,793 (4,731–4,872) 2.55 108.7 (111.4)
webrtc-rs 5,726 (5,698–5,797) 2.87 948.5 (982.2)
Pion 1,561 (1,552–1,573) 7.50 114.9 (118.2)
10 pairs, work stealing, unordered Zig 5,000 (4,919–5,093) 2.60 69.6 (71.4)
webrtc-rs 5,685 (5,676–5,702) 2.86 944.8 (973.1)
Pion 1,549 (1,542–1,565) 7.55 117.6 (126.7)
10 pairs, pinned, ordered Zig 9,246 (8,987–9,411) 1.62 27.3 (28.8)
rustrtc 7,806 (7,778–7,835) 1.59 28.9 (31.1)
webrtc-rs 7,700 (7,630–7,771) 2.32 455.9 (500.7)
10 pairs, pinned, unordered Zig 9,201 (9,070–9,446) 1.67 27.1 (28.9)
webrtc-rs 7,710 (7,640–7,797) 2.31 434.1 (481.0)
3 Likes