1.创建cmakelists.txt
# CMake 最低版本号要求
cmake_minimum_required (VERSION 3.12)
# 设置指定的C++编译器版本是必须的,如果不设置,或者为OFF,则指定版本不可用时,会使用上一版本。
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# 指定为C++11 版本
set(CMAKE_CXX_STANDARD 11)
add_definitions(-DRTC_DESKTOP_DEVICE)
# 项目信息
project(TestLibwebrtc)
link_directories(./)
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS)
# 查找当前目录下的所有头文件
include_directories(./)
include_directories(./include/)
# 指定生成目标
#add_executable(Test ${DIR_SRCS})
add_executable(Test main.cc)
target_link_libraries(Test libwebrtc.dylib)
2.main.cc
#include <iostream>
#include <string>
#include <string.h>
#include <list>
#include <map>
#include <memory>
#include <mutex>
#include <chrono>
#include <thread>
#include "libwebrtc.h"
#include "rtc_audio_device.h"
#include "rtc_desktop_device.h"
#include "rtc_dtmf_sender.h"
#include "rtc_media_stream.h"
#include "rtc_media_track.h"
#include "rtc_mediaconstraints.h"
#include "rtc_peerconnection.h"
#include "rtc_peerconnection_factory.h"
#include "rtc_video_device.h"
#include "rtc_desktop_capturer.h"
#include "rtc_desktop_media_list.h"
using namespace std;
using namespace libwebrtc;
int main() {
cout<<"------test webrtc start------"<<endl;
LibWebRTC::Initialize();
scoped_refptr<RTCPeerConnectionFactory> factory_;
scoped_refptr<RTCAudioDevice> audio_device_;
scoped_refptr<RTCVideoDevice> video_device_;
scoped_refptr<RTCDesktopDevice> desktop_device_;
factory_ = LibWebRTC::CreateRTCPeerConnectionFactory();
audio_device_ = factory_->GetAudioDevice();
video_device_ = factory_->GetVideoDevice();
desktop_device_ = factory_->GetDesktopDevice();
//DesktopType desktop_type = DesktopType::kScreen;
DesktopType desktop_type = DesktopType::kWindow;
scoped_refptr<RTCDesktopMediaList> source_list;
source_list = desktop_device_->GetDesktopMediaList(desktop_type);
source_list->UpdateSourceList();
int count = source_list->GetSourceCount();
for (int j = 0; j < count; j++) {
cout<<"------source list:------"<<source_list->GetSource(j)->name().c_string()<<endl;
}
LibWebRTC::Terminate();
std::this_thread::sleep_for(std::chrono::seconds(1));
cout<<"test webrtc end"<<endl;
return 0;
}
3.vscode配置launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "/Users/works/electron/libwebrtc_build/webrtc/test/build/Test",
"args": [],
"cwd": "${workspaceFolder}",
"sourceMap": {
"./":"/Users/works/electron/libwebrtc_build/webrtc/src/libwebrtc/",
"../":"/Users/works/electron/libwebrtc_build/webrtc/src/"
}
}
]
}