Linking against Google's implementation of WebRTC is not trivial task because:
- 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.
- 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