3

I have been working with WebRtc Development for the Windows Platform. I want to develop webrtc based desktop application. I am doing it from scratch for learning and better understanding.

The normal process of WebRtc Library Compilation:

I have initially started with (Getting Started with WinRTC). I followed the normal compilation process. After that, I have tried multiple ways to generate project files for webrtc such as;

1.Default

gn gen --ide=vs2019 out/Default

2.Custom Flags

gn gen --ide=vs2019 out/Default --args="use_rtti=true is_clang=false rtc_build_tools=false rtc_include_tests=false rtc_build_examples=false"

3.Custom Flags

gn gen --ide=vs2019 out\Default --filters=//:webrtc "--args=is_debug=true use_lld=false is_clang=false rtc_include_tests=true rtc_build_tools=true rtc_win_video_capture_winrt=true target_cpu=\"x86\" target_os=\"win\" rtc_build_examples=true rtc_win_use_mf_h264=true enable_libaom=true rtc_enable_protobuf=true"

For the building process, I have Followed these methods:

With command line:

Run the following command to build the patched WebRTC from the command line.

ninja -C out\Default\x64

With Visual Studio 2019:

Open the generated Visual Studio solution with the following command:

devenv out\Default\x64\all.sln

I have tried almost all available combinations to generate build files and to build webrtc.lib static library. I have successfully managed to compile the static webrtc library webrtc.lib for both architectures;

  1. x64 (Default Arch) (For Debug as well as release)
  2. x86 (Custom Arch) (For Debug as well as release)

WebRtc Static Library Description

IMPORTANT:

I have successfully managed to run peerconnection_server.exe and peerconnection_client.exe on windows. These examples are successfully running on localhost.


Using VS2019:

After that, I created a new Console based project using VS2019 to consume generated binaries and followed these steps;

  1. Add include folders

Configuration Properties → C/C++ → General → Additional Include Directories and add the following paths:

c:\webrtc\src
c:\webrtc\src\out\Default\$(Configuration)\$(PlatformTarget)\gen
c:\webrtc\src\third_party\abseil-cpp
c:\webrtc\src\third_party\libyuv\include
  1. Preprocessor macros:

Click on Preprocessor → Preprocessor Definitions and add the following definitions:

USE_AURA=1;_HAS_EXCEPTIONS=0;__STD_C;_CRT_RAND_S;_CRT_SECURE_NO_DEPRECATE;_SCL_SECURE_NO_DEPRECATE;_ATL_NO_OPENGL;_WINDOWS;CERT_CHAIN_PARA_HAS_EXTRA_FIELDS;PSAPI_VERSION=2;WIN32;_SECURE_ATL;WINUWP;__WRL_NO_DEFAULT_LIB__;WINAPI_FAMILY=WINAPI_FAMILY_PC_APP;WIN10=_WIN32_WINNT_WIN10;WIN32_LEAN_AND_MEAN;NOMINMAX;_UNICODE;UNICODE;NTDDI_VERSION=NTDDI_WIN10_RS2;_WIN32_WINNT=0x0A00;WINVER=0x0A00;NDEBUG;NVALGRIND;DYNAMIC_ANNOTATIONS_ENABLED=0;WEBRTC_ENABLE_PROTOBUF=0;WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE;RTC_ENABLE_VP9;HAVE_SCTP;WEBRTC_LIBRARY_IMPL;WEBRTC_NON_STATIC_TRACE_EVENT_HANDLERS=0;WEBRTC_WIN;ABSL_ALLOCATOR_NOTHROW=1;HAVE_SCTP;WEBRTC_VIDEO_CAPTURE_WINRT
  1. Linker additional library path:

Click on Linker → General → Additional Library Directories and add the following path:

c:\webrtc\src\out\Default\$(Configuration)\$(PlatformTarget)\obj
  1. WebRTC library name:

Click on Input → Additional Dependencies and add the following filename:

webrtc.lib

Now, when I simply use this basic implementation such as;

#include <iostream>

#include "rtc_base/thread.h"
#include "rtc_base/logging.h"
#include "rtc_base/ssl_adapter.h"
#include "rtc_base/arraysize.h"
#include "rtc_base/net_helpers.h"
#include "rtc_base/string_utils.h"
#include "rtc_base/signal_thread.h"


int main(int argc, char** argv) {

    rtc::InitializeSSL();

    return 0;
}

The program is flooded with two types of errors:

1. LNK2038  mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug'

and another one is

2. LNK2038  mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2'

You can also see as given; Here I have used webrtc.lib with Configuration (Release) & Platform (x64).

Release Configuration


Using Clion-2021.2.1 and CMAKE:

Here I have used webrtc.lib with Configuration (Release) & Platform (x86).

CMakeLists.txt is given as;

cmake_minimum_required(VERSION 3.20)
project(NewRtc)

set(CMAKE_CXX_STANDARD 14)


#set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")

include_directories(
        "c:/webrtc/src"
        "C:/webrtc/src/out/Default/x86/obj"
        "c:/webrtc/src/third_party/abseil-cpp"
        "c:/webrtc/src/third_party/libyuv/include"
)

# error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
# Solution:
#1. _ITERATOR_DEBUG_LEVEL = 0 // disabled (for release builds)
#2. _ITERATOR_DEBUG_LEVEL = 1 // enabled (if _SECURE_SCL is defined)
#3. _ITERATOR_DEBUG_LEVEL = 2 // enabled (for debug builds)

add_definitions(
        -D_ITERATOR_DEBUG_LEVEL=0
        -DUSE_AURA=1
        -D_HAS_EXCEPTIONS=0
        -D__STD_C
        -D_CRT_RAND_S
        -D_CRT_SECURE_NO_DEPRECATE
        -D_SCL_SECURE_NO_DEPRECATE
        -D_ATL_NO_OPENGL
        -D_WINDOWS
        -DCERT_CHAIN_PARA_HAS_EXTRA_FIELDS
        -DPSAPI_VERSION=2
        -DWIN32
        -D_SECURE_ATL
        -DWINUWP
        -D__WRL_NO_DEFAULT_LIB__
        -DWINAPI_FAMILY=WINAPI_FAMILY_PC_APP
        -DWIN10=_WIN32_WINNT_WIN10
        -DWIN32_LEAN_AND_MEAN
        -DNOMINMAX
        -D_UNICODE
        -DUNICODE
        -DNTDDI_VERSION=NTDDI_WIN10_RS2
        -D_WIN32_WINNT=0x0A00
        -DWINVER=0x0A00
        -DNDEBUG
        -DNVALGRIND
        -DDYNAMIC_ANNOTATIONS_ENABLED=0
        -DWEBRTC_ENABLE_PROTOBUF=0
        -DWEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE
        -DRTC_ENABLE_VP9
        -DHAVE_SCTP
        -DWEBRTC_LIBRARY_IMPL
        -DWEBRTC_NON_STATIC_TRACE_EVENT_HANDLERS=0
        -DWEBRTC_WIN
        -DABSL_ALLOCATOR_NOTHROW=1
        -DHAVE_SCTP
        -DWEBRTC_VIDEO_CAPTURE_WINRT)



#set(CMAKE_CXX_FLAGS_RELEASE "/MT")
#set(CMAKE_CXX_FLAGS_DEBUG "/MTd")

set(-Dwebrtc.lib)
add_executable(NewRtc main.cpp)
set_property(TARGET NewRtc PROPERTY
        MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
target_link_libraries(NewRtc

        PRIVATE "C:/webrtc/src/out/Default/x86/obj/webrtc.lib"

        )

But when I simply build the project, this error comes up for every implementation of WebRtc. Here you can see:

Clion Error Description

Please assist me that how can I simply use webrtc library in any project on windows suing VS2019 or Clion. I am trying to solve these problems, I have tried multiple solutions over stack overflow and other communities using cmake or adding flags inside project properties such as;

I have tried my best to explain the complete solution and associated problem so that someone might help me accordingly.

Muhammad Usman Bashir
  • 1,441
  • 2
  • 14
  • 43
  • The error is you are mixing debug and release. Are there separate .lib files for debug and release? If there is not you may be limited to use release only. – drescherjm Sep 18 '21 at 15:24
  • Yes Sir, I have placed the libraries in different directories. I have developed for `x86` inside `out\Default\x86` and `x64` inside `out\Default\x64`. – Muhammad Usman Bashir Sep 18 '21 at 15:27
  • There should be Debug and Release .lib files for both x86 and x64 meaning 4 total lib files. – drescherjm Sep 18 '21 at 15:28
  • To me the error messages are odd. One suggests a conflict between dynamic runtime and static. The other suggests mixing release and debug or an improper setting of iterator debug level while building the library for the debug configuration. – drescherjm Sep 18 '21 at 15:29
  • Yes exactly. Let say for now I am looking for these settings: Configuration `(Release)` & Platform `(x64)`. Means I have only one lib in these settings. VS2019 also has the same settings. I have updated the VS2019 IDE with a Project screenshot. Can you Please re-visit: https://i.stack.imgur.com/G02ic.png – Muhammad Usman Bashir Sep 18 '21 at 15:39
  • For Clion: I am using `webrtc.lib` with Configuration (Release) & Platform (x86). Clion toolChains has also been set on these settings. – Muhammad Usman Bashir Sep 18 '21 at 15:42
  • My guess is the `webrtc.lib` that is built the x64 folder is a debug library and not release and you can't use the debug library in the release configuration. – drescherjm Sep 18 '21 at 15:45
  • @drescherjm I have repeated the steps and compiled `webrtc.lib` using Configuration `release` and Platform `x64`. Then, I again created a new VS2019 console-based project. I have changed the compiler setting to Release and x64 as well. Added required library paths in the project setting in `C/C++` and `Linker`. After that, I have built the project after cleaning it. Now the errors are minimized to half and this error is coming: `LNK2038 mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MD_DynamicRelease'`. Can you please assist me further from here? – Muhammad Usman Bashir Sep 18 '21 at 16:45
  • You have to change the runtime setting of the demo project you are compiling to match the library. – drescherjm Sep 18 '21 at 21:35
  • Actually, what I have observed is that I am compiling and using `webrtc.lib` accurately. But as I am working with `executables` i.e. console-based projects, so I am calling all the relevant webrtc functionality inside the `Entry-Point` or `main` function which is causing consistent trouble. So, linking the `static library` with configuration type: `Application (.exe)` is making me trouble. I have visited this solution: https://stackoverflow.com/a/16948470/10413749 – Muhammad Usman Bashir Sep 18 '21 at 22:46

1 Answers1

4

Well Guys, After spending almost 12 hours of research, I have made my day. I have exactly figured out what was wrong with my development setup in (VS2019/Clion) of using static webrtc.lib in executable projects.

I must thank Mr.@drescherjm for his support on my question. It's really appreciated it.

All things were in a positive direction from library compilation to its usage in VS2019/Clion projects. Actually, webrtc development in .netCore projects required some useful libraries for the normal use of webrtc.lib in your projects.

Here is the list of libraries that you must use during development such as;


For Windows:

Required Modules for building WebRtc projects for Windows as follows;

WS2_32 secur32.lib winmm.lib dmoguids.lib wmcodecdspuuid.lib msdmo.lib Strmiids.lib

For Linux:

Required Modules for building WebRtc projects for Linux as follows;

Secur32 Msdmo Winmm Dmoguids wmcodecdspuuid

These are the important libraries as webrtc peer-to-peer communication mostly comes up with the extensive implementation of socket programming in C/C++ for both Linux as well as Windows Operating Systems.

How to use these libraries in projects:

  • using CmakeLists.txt
  • using #pragma keyword

For CMakeLists.txt:

You can simply include these modules in target_link_libraries() in which you are also linking webrtc.lib such as;

add_executable(NewRtc main.cpp)

set_property(TARGET NewRtc PROPERTY
        MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

target_link_libraries(NewRtc

    PRIVATE "{$LIB_PATH}/webrtc.lib"

    # Required Modules for building projects
    WS2_32 secur32.lib winmm.lib dmoguids.lib wmcodecdspuuid.lib msdmo.lib Strmiids.lib

    )

For #Pragma Keyword:

You can directly include all the required libraries using the #pragma keyword inside your executable files (in my case main.cpp) such as;

# pragma comment(lib, "webrtc.lib")
# pragma comment(lib,"WS2_32")
# pragma comment(lib, "secur32.lib")
# pragma comment(lib, "winmm.lib")
# pragma comment(lib, "dmoguids.lib")
# pragma comment(lib, "wmcodecdspuuid.lib")
# pragma comment(lib, "msdmo.lib")
# pragma comment(lib, "Strmiids.lib")

Both procedures will 100% serve the same purpose. I hope it would help someone.

Muhammad Usman Bashir
  • 1,441
  • 2
  • 14
  • 43