Skip to content

WebRTC Server

sepfy edited this page Sep 15, 2024 · 2 revisions

How to Connect to a WebRTC Server

libpeer supports the WHIP/WHEP protocol to connect to WebRTC servers, but it only works over HTTPS. To connect to a server, modify the http_url in the service_config and use the peer_signaling_whip_connect function to establish the connection. This guide will provide step-by-step instructions for connecting to different WebRTC servers.

Examples:

  1. ZLMediaKit Example

    • ZLMediaKit supports two WebRTC streaming methods: a proprietary protocol and the standard WHIP/WHEP protocol. Since libpeer only supports WHIP/WHEP, please use the WHIP/WHEP method to connect to ZLMediaKit.

    Steps:

    1. Install ZLMediaKit: Follow the ZLMediaKit installation guide to set up the media server on your system.

    2. Run ZLMediaKit and Enable HTTPS:

      • Execute ZLMediaKit and ensure that HTTPS is enabled in the configuration for secure WebRTC signaling.
    3. Modify libpeer's service_config:

      • Using the generic/sample configuration as an example, modify the service_config in libpeer.
      • Since ZLMediaKit does not send STUN packets to maintain the connection, you will need to disable the KEEP ALIVE check in libpeer.
    4. Run libpeer and Stream Video via Web Browser:

      • Execute libpeer and access the ZLMediaKit WebRTC demo page.
      • Press the Play button on the demo page to start pulling the video stream, and you should be able to see the live video.
diff --git a/examples/generic/main.c b/examples/generic/main.c
index a49d57e..bc6f825 100644
--- a/examples/generic/main.c
+++ b/examples/generic/main.c
@@ -68,11 +68,6 @@ int main(int argc, char* argv[]) {
   pthread_t peer_singaling_thread;
   pthread_t peer_connection_thread;

-  if (argc < 2) {
-    printf("Usage: %s <device_id>\n", argv[0]);
-    return -1;
-  }
-
   signal(SIGINT, signal_handler);

   PeerConfiguration config = {
@@ -92,13 +87,12 @@ int main(int argc, char* argv[]) {
   peer_connection_oniceconnectionstatechange(g_pc, onconnectionstatechange);
   peer_connection_ondatachannel(g_pc, onmessage, onopen, onclose);

-  service_config.client_id = argv[1];
   service_config.pc = g_pc;
+  service_config.http_url = "127.0.0.1/index/api/whip?app=live&stream=test";
   peer_signaling_set_config(&service_config);
-  peer_signaling_join_channel();
+  peer_signaling_whip_connect();

   pthread_create(&peer_connection_thread, NULL, peer_connection_task, NULL);
-  pthread_create(&peer_singaling_thread, NULL, peer_singaling_task, NULL);

   reader_init();

diff --git a/src/config.h b/src/config.h
index 7b7cdf9..95ebc9b 100644
--- a/src/config.h
+++ b/src/config.h
@@ -17,7 +17,7 @@
 #endif

 #define AUDIO_LATENCY 20  // ms
-#define KEEPALIVE_CONNCHECK 10000
+#define KEEPALIVE_CONNCHECK 0
 #define CONFIG_IPV6 0
Clone this wiki locally