Skip to content

junlon2006/libwebrtc

Repository files navigation

libwebrtc - Pure C WebRTC Library

A lightweight, cross-platform WebRTC implementation in pure C.

Features

  • Pure C99 implementation - No C++ dependencies
  • Cross-platform - Linux, macOS, Windows, FreeBSD
  • Full protocol stack:
    • ICE/STUN/TURN for connectivity
    • DTLS for key exchange
    • SRTP for media encryption
    • SCTP for data channels
    • SDP parsing and generation
  • Platform abstraction layer for portability
  • Minimal dependencies - Only standard C library
  • Simple API design - Easy to integrate

Architecture

libwebrtc/
├── include/          # Public API headers
│   ├── webrtc.h              # Main API
│   ├── webrtc_platform.h     # Platform abstraction
│   ├── webrtc_ice.h          # ICE protocol
│   ├── webrtc_stun.h         # STUN protocol
│   ├── webrtc_sdp.h          # SDP parser
│   ├── webrtc_dtls.h         # DTLS security
│   ├── webrtc_srtp.h         # SRTP encryption
│   └── webrtc_sctp.h         # SCTP transport
├── src/
│   ├── api/          # PeerConnection API
│   ├── platform/     # Platform implementations
│   ├── net/          # Network layer (STUN)
│   └── transport/    # Transport protocols (ICE/DTLS/SRTP/SCTP/SDP)
├── examples/         # Example applications
└── tests/            # Unit tests

See ARCHITECTURE.md for detailed design documentation.

Building

Linux/macOS

# Using CMake
mkdir build && cd build
cmake ..
make

# Or using Make directly
make

Windows

mkdir build && cd build
cmake -G "Visual Studio 16 2019" ..
cmake --build .

Quick Start

#include "webrtc.h"

static void on_ice_candidate(webrtc_peer_connection_t *pc, 
                             const webrtc_ice_candidate_t *candidate, 
                             void *userdata) {
    printf("ICE Candidate: %s\n", candidate->candidate);
}

static void on_data_channel(webrtc_peer_connection_t *pc, 
                           webrtc_data_channel_t *channel, 
                           void *userdata) {
    printf("Data Channel Created\n");
}

int main(void) {
    webrtc_init();
    
    // Configure ICE servers
    webrtc_config_t config = {0};
    const char *ice_servers[] = {"stun:stun.l.google.com:19302"};
    config.ice_servers = (char**)ice_servers;
    config.ice_server_count = 1;
    
    // Set up callbacks
    webrtc_callbacks_t callbacks = {
        .on_ice_candidate = on_ice_candidate,
        .on_data_channel = on_data_channel
    };
    
    // Create peer connection
    webrtc_peer_connection_t *pc = webrtc_create_peer_connection(
        &config, &callbacks, NULL);
    
    // Create data channel
    webrtc_data_channel_t *channel = webrtc_create_data_channel(pc, "test");
    
    // Create and set offer
    webrtc_session_description_t *offer;
    webrtc_create_offer(pc, &offer);
    webrtc_set_local_description(pc, offer);
    
    // Send data
    const char *msg = "Hello WebRTC!";
    webrtc_data_channel_send(channel, (const uint8_t*)msg, strlen(msg));
    
    // Cleanup
    webrtc_data_channel_close(channel);
    webrtc_destroy_peer_connection(pc);
    webrtc_cleanup();
    
    return 0;
}

Examples

Simple Example

./example_simple

DataChannel Example

./example_datachannel

Integration Test

./example_integration

API Overview

Initialization

  • webrtc_init() - Initialize library
  • webrtc_cleanup() - Cleanup resources

PeerConnection

  • webrtc_create_peer_connection() - Create peer connection
  • webrtc_destroy_peer_connection() - Destroy peer connection
  • webrtc_create_offer() - Create SDP offer
  • webrtc_create_answer() - Create SDP answer
  • webrtc_set_local_description() - Set local SDP
  • webrtc_set_remote_description() - Set remote SDP
  • webrtc_add_ice_candidate() - Add ICE candidate

DataChannel

  • webrtc_create_data_channel() - Create data channel
  • webrtc_data_channel_send() - Send data
  • webrtc_data_channel_close() - Close channel

ICE/STUN

  • ice_agent_create() - Create ICE agent
  • ice_agent_gather_candidates() - Gather candidates
  • stun_send_request() - Send STUN binding request

SDP

  • sdp_parse() - Parse SDP string
  • sdp_generate() - Generate SDP string
  • sdp_add_media() - Add media section

DTLS/SRTP

  • dtls_context_create() - Create DTLS context
  • dtls_generate_fingerprint() - Generate fingerprint
  • srtp_protect() - Encrypt RTP packet
  • srtp_unprotect() - Decrypt SRTP packet

SCTP

  • sctp_context_create() - Create SCTP context
  • sctp_send() - Send SCTP data
  • sctp_receive() - Receive SCTP data

Platform Abstraction

The library abstracts platform-specific functionality:

  • Threads: webrtc_thread_*
  • Mutexes: webrtc_mutex_*
  • Sockets: webrtc_socket_*
  • Time: webrtc_time_ms(), webrtc_sleep_ms()
  • Memory: webrtc_malloc(), webrtc_free()

Protocol Support

Protocol Status Description
ICE ✅ Implemented Interactive Connectivity Establishment
STUN ✅ Implemented Session Traversal Utilities for NAT
TURN 🚧 Partial Traversal Using Relays around NAT
DTLS ✅ Implemented Datagram Transport Layer Security
SRTP ✅ Implemented Secure Real-time Transport Protocol
SCTP ✅ Implemented Stream Control Transmission Protocol
SDP ✅ Implemented Session Description Protocol

Roadmap

  • Platform abstraction layer
  • ICE/STUN implementation
  • DTLS key exchange
  • SRTP encryption
  • SCTP data channels
  • SDP parsing/generation
  • Full TURN relay support
  • OpenSSL/BoringSSL integration
  • Media codec support (Opus, VP8, H.264)
  • Bandwidth estimation
  • Congestion control

License

MIT License - see LICENSE file for details.

Contributing

Contributions welcome! Please:

  1. Follow the existing code style (C99)
  2. Add tests for new features
  3. Update documentation
  4. Submit pull requests

References

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors