A pure Swift implementation of WebRTC data channels.
Built entirely from scratch — no C/C++ WebRTC library dependency. Implements the full protocol stack required for WebRTC Direct data channel communication:
UDP → STUN / ICE Lite → DTLS 1.2 → SCTP → Data Channels
- Swift 6.2+
- macOS 15+ / iOS 18+ / tvOS 18+ / watchOS 11+ / visionOS 2+
dependencies: [
.package(url: "https://github.com/1amageek/swift-webrtc.git", from: "0.0.1"),
]The library is split into independent modules:
| Module | Description | RFC |
|---|---|---|
| STUNCore | STUN message encoding/decoding, MESSAGE-INTEGRITY, FINGERPRINT | RFC 5389 |
| ICELite | ICE Lite agent for server-side connectivity checks | RFC 8445 |
| SCTPCore | SCTP association, chunk encoding/decoding, stream management | RFC 4960 |
| DataChannel | Data channel lifecycle, DCEP (open/ack) messages | RFC 8831 |
| WebRTC | Top-level API integrating all layers | — |
DTLS is provided by swift-tls.
import WebRTC
let endpoint = try WebRTCEndpoint.create()
print(endpoint.localFingerprint.sdpFormat)let connection = try endpoint.connect(
remoteFingerprint: remoteFingerprint,
sendHandler: { data in
// Send raw bytes over UDP
}
)
connection.setRemoteICECredentials(ufrag: remoteUfrag, password: remotePassword)
try connection.start()
let channel = try connection.openDataChannel(label: "data")
try connection.send(payload, on: channel.id)let listener = try endpoint.listen()
for await connection in listener.connections {
try connection.start()
for await channel in connection.incomingChannels {
print("Channel opened: \(channel.label)")
}
}- Transport-agnostic — Callers provide a
SendHandlerclosure and feed incoming bytes viareceive(_:). This allows integration with any UDP transport. - Sendable — All public types conform to
Sendable. Thread safety is achieved usingMutex<T>. - Modular — Each protocol layer is a standalone library that can be used independently.
Performance benchmarks are included under Tests/PerformanceTests/. Each module has a dedicated benchmark suite:
| Suite | Coverage |
|---|---|
SCTPBenchmarks |
CRC-32C, packet encode/decode, TSN tracking, fragment assembly |
STUNBenchmarks |
Message encode/decode, FINGERPRINT, MESSAGE-INTEGRITY |
ICEBenchmarks |
STUN request processing, credential generation, peer validation |
DataChannelBenchmarks |
DCEP encode/decode, channel open/lookup |
# All benchmarks (debug)
swift test --filter PerformanceTests
# All benchmarks (release — recommended for accurate numbers)
swift test -c release --filter PerformanceTests
# Single suite
swift test -c release --filter SCTPBenchmarksRelease mode is strongly recommended. Debug builds include bounds checks and disable compiler optimizations, resulting in measurements that do not reflect production performance.
| Operation | Throughput |
|---|---|
| CRC-32C (1500 B) | 2.7 GB/s |
| SCTP packet encode | 845K ops/s |
| SCTP packet decode | 1.5M ops/s |
| TSN gap block computation | 577K ops/s |
| STUN FINGERPRINT compute | 2.9M ops/s |
| Fragment assembly (multi-chunk) | 526K ops/s |
CRC-32C uses a slicing-by-8 lookup table algorithm. Checksum validation avoids packet-level copies by computing the CRC with the checksum field treated as zeros in-place.
MIT