How to integrate part of WebRTC as a static / dynamic library with the existing C++ code?

Viewed 1156

From its official code site, I am able to download & install the WebRTC in Ubuntu. Now I intend to use its certain C++ APIs in our server for the purpose of creating P2P endpoints and encrypting / decrypting VoIP data. But there is no guideline on how to use the module as a static or dynamically linked library.

Question: How to link [preferably minimal] part of the WebRTC library in C++?
Note: I am using Qt platform.

2 Answers

Linking against Google's implementation of WebRTC is not trivial task because:

  1. It has its own 'toolchain' with compiler and stdlib (for example, for Ubuntu it uses clang and libc++). And that might be incompatible with your executable on ABI level. Also if you are using libstdc++ (AFAIK Qt does that), then you cannot have both libs linked in one unit.
  2. WebRTC uses BoringSSL and if your application uses OpenSSL then it's similar issue to previous one with std lib: you have conflicting symbols.

Solution 1

One of the solutions for this problem is to wrap WebRTC in dynamic library in which all WebRTC symbols are hidden. This lib must contain your implementation for working with offers/answers/candidates etc. And make pure C interface that wraps this functionality. Since only this interface would be exported from the lib, you won't have symbol conflicts and ABI incompatibility. To create such dynamic library the easiest way I've found is to modify some BUILD.gn files, add my implementation to WebRTC codebase and build it with GN, i.e. within whole WebRTC lib.

These steps describe example of adding customizations to GN build system:

  • add directory to root src dir (i.e. where root BUILD.gn is located)
  • create file BUILD.gn inside the new dir
  • in the root BUILD.gn file find section rtc_static_library("webrtc") and to its deps section add the name of your new dir

This is a template for your BUILD.gn file:

rtc_shared_library("name_of_your_dir") {
  deps = [
    "../api:create_peerconnection_factory",
    "../api:libjingle_peerconnection_api",
    "../api:media_stream_interface",
    "../api/audio_codecs:builtin_audio_decoder_factory",
    "../api/audio_codecs:builtin_audio_encoder_factory",
    "../api/video_codecs:builtin_video_decoder_factory",
    "../api/video:video_frame",
    "../media:rtc_audio_video",
    "../media:rtc_internal_video_codecs",
    "../media:rtc_media",
    "../media:rtc_media_base",
    "../modules/audio_device",
    "../pc:peerconnection",
    "../rtc_base",   ]

  # Add your source files here
  sources = [
    # "name_of_your_dir/header.h",
    # "name_of_your_dir/impl.cc"
  ]
}

In result after successful build you'll get lib_name_of_your_dir.so file in out dir. In this lib all symbols are hidden by default rules in WebRTC build system, so you need to export yours: to do this mark your methods/classes with __attribute__((visibility("default")))

Solution 2

Another solution is to compile WebRTC without included compiler and std lib, and that might be tricky because it requires you to go deep into GN build settings. But this approach has an advantage: it allows to use C++ interface instead of pure C, because the lib would be ABI compatible.

P.S. To understand how to work with signaling and create peer to peer connection you can refer to this example: https://webrtc.googlesource.com/src/+/refs/heads/main/examples/peerconnection

We have three platforms available for the webrtc.

1- WinRTC (used for making libraries for windows and Desktop apps)

2- Webrtc for Android app (Need Ubuntu for that)

3- Webrtc for IOS (Need MACOS for that)

For all of these platforms there are different mothods to setup the project. You are using the tutorial for using Webrtc for windows knows as winRTC.

In linux, You can only make the webrtc libs for the android app and the method is provided in this link.

https://github.com/mail2chromium/Compile_WebRTC_Library_For_Android

********************* Integration ******************

For integrating webrtc library in android app, You have to apply a flag named

implementation(name: 'library_Name', ext: 'Extension like .aar,.so,.a')

in your App level build.gradle So, now you will be able to call the Java function of webRTC like createPeerConnection or closePeerConnection directly.

Related