update demo webrtc

This commit is contained in:
phamvannhat 2025-11-21 23:36:45 +07:00
parent 7ace38178b
commit 6ea15dce1a
8 changed files with 242 additions and 0 deletions

View File

@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.10)
project(send_number_sample)
set(CMAKE_CXX_STANDARD 17)
include_directories($ENV{HOME}/armv7/libdatachannel/include)
link_directories($ENV{HOME}/armv7/libdatachannel/lib)
link_directories($ENV{HOME}/armv7/openssl/lib)
add_executable(send_number main.cpp)
target_link_libraries(send_number datachannel ssl crypto)

View File

@ -0,0 +1,9 @@
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_PROCESSOR arm)
SET(CMAKE_C_COMPILER arm-rockchip830-linux-uclibcgnueabihf-gcc)
SET(CMAKE_CXX_COMPILER arm-rockchip830-linux-uclibcgnueabihf-g++)
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

View File

@ -0,0 +1,89 @@
#include <rtc/rtc.hpp>
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
int main() {
// 1. Khởi tạo logger
rtc::InitLogger(rtc::LogLevel::Info);
// 2. Khởi tạo PeerConnection
rtc::Configuration config;
auto pc = std::make_shared<rtc::PeerConnection>(config);
// 3. Khởi tạo DataChannel
auto dc = pc->createDataChannel("test");
// 4. Thiết lập callback DataChannel
dc->onMessage([](rtc::message_variant data) {
if (std::holds_alternative<std::string>(data)) {
std::cout << "Received: " << std::get<std::string>(data) << std::endl;
}
});
dc->onOpen([dc]() {
std::cout << "DataChannel open → sending number 1\n";
dc->send("1");
});
// Sử dụng GatheringState như đã sửa ở lần trước
pc->onGatheringStateChange([](rtc::PeerConnection::GatheringState state) {
if (state == rtc::PeerConnection::GatheringState::Complete) {
std::cout << "ICE Gathering Complete.\n";
}
});
// 5. SỬA LỖI: Khởi tạo rtc::LocalDescriptionInit
try {
rtc::Description offer = pc->createOffer();
std::cout << "\n===== LOCAL SDP (OFFER) =====\n";
std::cout << std::string(offer) << std::endl;
std::cout << "=============================\n\n";
// SỬA: Thay đổi cú pháp khởi tạo để tránh Most Vexing Parse.
// Sử dụng Uniform Initialization (dấu ngoặc nhọn {}) hoặc cú pháp khởi tạo bằng (=).
rtc::LocalDescriptionInit init{std::string(offer)}; // Khởi tạo đồng nhất (Uniform Initialization)
// Gọi hàm với hai đối số Type và đối tượng Init
pc->setLocalDescription(offer.type(), init);
std::cout << "Local description set.\n";
} catch (const std::exception& e) {
std::cerr << "Error creating or setting local description: " << e.what() << "\n";
}
// 6. Nhận Remote SDP
std::cout << "Paste remote SDP (ANSWER) and press Enter:\n";
std::string remote;
std::string remoteLine;
// Đọc nhiều dòng cho SDP
while (std::getline(std::cin, remoteLine) && !remoteLine.empty()) {
remote += remoteLine + "\n";
}
if (!remote.empty()) {
// 7. setRemoteDescription (đã sửa ở lần trước)
try {
// Khởi tạo rtc::Description với type là "answer"
rtc::Description remoteDesc(remote, "answer");
// Chỉ truyền đối tượng rtc::Description hoàn chỉnh.
pc->setRemoteDescription(remoteDesc);
std::cout << "Remote description set.\n";
} catch (const std::exception& e) {
std::cerr << "Error setting remote description: " << e.what() << "\n";
}
} else {
std::cerr << "Error: Remote SDP is empty.\n";
}
// 8. Vòng lặp giữ chương trình chạy
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}

View File

@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 3.10)
project(webrtc_demo CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# Include & lib ca libdatachannel
include_directories(/usr/local/include)
link_directories(/usr/local/lib)
# Module .so
add_library(webrtc_module SHARED
src/module.cpp
)
target_link_libraries(webrtc_module
datachannel
ssl
crypto
sctp
pthread
)
# Main app
add_executable(webrtc_app
src/main.cpp
)
target_link_libraries(webrtc_app
dl
)

View File

@ -0,0 +1,28 @@
#include <dlfcn.h>
#include <iostream>
#include <string>
int main() {
std::cout << "[MAIN] Loading WebRTC module...\n";
void* handle = dlopen("./libwebrtc_module.so", RTLD_LAZY);
if (!handle) {
std::cerr << "Cannot load module: " << dlerror() << std::endl;
return 1;
}
// Lấy function start_webrtc
typedef void (*start_func_t)();
start_func_t start_webrtc = (start_func_t)dlsym(handle, "start_webrtc");
char* error;
if ((error = dlerror()) != nullptr) {
std::cerr << "Cannot find start_webrtc: " << error << std::endl;
return 1;
}
std::cout << "[MAIN] Starting WebRTC...\n";
start_webrtc(); // chạy module
dlclose(handle);
return 0;
}

View File

@ -0,0 +1,65 @@
#include <rtc/rtc.hpp>
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
extern "C" {
// Hàm khởi động module WebRTC
void start_webrtc() {
rtc::InitLogger(rtc::LogLevel::Info);
rtc::Configuration config;
// Nếu dùng Tailscale, không cần STUN/TURN
auto pc = std::make_shared<rtc::PeerConnection>(config);
auto dc = pc->createDataChannel("echo");
// Khi DataChannel mở
dc->onOpen([dc]() {
std::cout << "[MODULE] DataChannel opened\n";
});
// Nhận message từ DataChannel
dc->onMessage([dc](rtc::message_variant message) {
std::visit([dc](auto&& msg){
using T = std::decay_t<decltype(msg)>;
if constexpr (std::is_same_v<T,std::string>) {
std::cout << "[MODULE] Received (string): " << msg << std::endl;
dc->send("Echo: " + msg);
} else if constexpr (std::is_same_v<T,rtc::binary>) {
std::string s(reinterpret_cast<const char*>(msg.data()), msg.size());
std::cout << "[MODULE] Received (binary): " << s << std::endl;
dc->send("Echo: " + s);
}
}, message);
});
// Khi SDP local được tạo
pc->onLocalDescription([](rtc::Description desc) {
std::cout << "----- MODULE SDP -----\n";
std::cout << std::string(desc) << "\n"; // <--- sửa ở đây
std::cout << "----------------------\n";
});
// Nhập SDP remote từ main app (hoặc file)
std::cout << "[MODULE] Enter remote SDP (end with empty line):\n";
std::string remoteLine;
std::string remoteSDP;
while (true) {
std::getline(std::cin, remoteLine);
if (remoteLine.empty()) break;
remoteSDP += remoteLine + "\n";
}
pc->setRemoteDescription(remoteSDP);
std::cout << "[MODULE] Running...\n";
// Giữ module chạy, không CPU 100%
while (true) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
} // extern "C"

View File

@ -54,6 +54,8 @@ CONFIG_INET_TABLE_PERTURB_ORDER=8
# CONFIG_INET_DIAG is not set
CONFIG_IPV6=m
# CONFIG_IPV6_SIT is not set
CONFIG_IP_SCTP=m
CONFIG_SCTP_DEFAULT_COOKIE_HMAC_SHA1=y
CONFIG_DEVTMPFS=y
CONFIG_DEVTMPFS_MOUNT=y
# CONFIG_ALLOW_DEV_COREDUMP is not set
@ -336,7 +338,13 @@ CONFIG_NFS_V3_ACL=y
CONFIG_NFS_V4=y
CONFIG_NLS_CODEPAGE_437=y
CONFIG_NLS_ISO8859_1=y
CONFIG_CRYPTO_CCM=y
CONFIG_CRYPTO_GCM=y
CONFIG_CRYPTO_SHA1=y
CONFIG_CRYPTO_SHA256=y
CONFIG_CRYPTO_AES=y
CONFIG_CRYPTO_ZSTD=y
CONFIG_CRYPTO_USER_API_AEAD=y
# CONFIG_CRYPTO_HW is not set
# CONFIG_XZ_DEC_X86 is not set
# CONFIG_XZ_DEC_POWERPC is not set