Compare commits

...

63 Commits

Author SHA1 Message Date
679ecfc066 Bumped version to 0.10.5 2021-01-21 14:40:21 +01:00
4e3ea69073 Join thread pool at exit to prevent static destruction order issue 2021-01-21 12:08:57 +01:00
b79c886480 Force BUILD_SHARED_LIBS to OFF to build usrsctp as static 2021-01-20 20:27:01 +01:00
08da691911 Added RTC defines to usage requirement in Jamfile 2021-01-20 20:27:01 +01:00
754568506a Fixed implicit conversion of stream from unsigned int to uint16_t 2021-01-20 20:27:01 +01:00
7ac351d1b9 Fixed local variable shadowing 2021-01-20 20:27:01 +01:00
6d5cecbca1 Fixed usrsctp build in Jamfile 2021-01-20 20:27:01 +01:00
abec5fc219 Bumped version to 0.10.4 2020-11-29 19:15:54 +01:00
684b7ba925 Merge pull request #280 from paullouisageneau/default-data-mid
Change default data mid
2020-11-29 17:33:20 +01:00
d8515b6362 Some cleanup 2020-11-29 17:24:20 +01:00
62da885028 Changed default data mid to a number 2020-11-29 17:13:55 +01:00
ff2e83bbdc Call rtcAddRemoteCandidate() with NULL mid 2020-11-29 17:06:19 +01:00
8f9e8e718e Uncluttered client example to keep it simple 2020-11-29 17:02:01 +01:00
c6bee7b0d4 Added console logging in web example 2020-11-29 17:00:43 +01:00
4d93303be8 Updated libjuice to v0.6.2 2020-11-29 16:26:31 +01:00
1620ddfb03 Merge pull request #279 from paullouisageneau/fix-candidates-mid
Fix mid on local candidates
2020-11-29 16:25:50 +01:00
452b742adc Fixed mid on local candidates 2020-11-29 16:03:57 +01:00
244c834992 Cleanup and reformatting 2020-11-28 17:03:51 +01:00
ffe202a6a2 Merge pull request #278 from hhgyu/add-support-protocols
support Subprotocols
2020-11-28 13:18:06 +01:00
ea87e5ae09 Exposed send(data, size) on Channel 2020-11-27 21:20:24 +01:00
4259b4e968 fixed build failed for windows 2020-11-27 12:22:22 +09:00
6aff5dc5bc fixed build failed for macos 2020-11-27 12:11:16 +09:00
99bae7f830 support WebSocket Protocol RFC6455 2020-11-27 11:15:37 +09:00
7598d992dc Merge pull request #274 from stazio/newdesc
Exposed Additional rtc::Description::Media calls
2020-11-25 22:24:07 +01:00
6380038584 Merge pull request #277 from paullouisageneau/fix-websocket-case
Fix WebSocket Upgrade header check
2020-11-25 22:23:18 +01:00
6144bca0f7 Changed update header check to be case-insensitive 2020-11-25 20:46:02 +01:00
6ec129f8f8 Re-Formatting to use spaces 2020-11-23 20:37:29 -05:00
be394b7185 Cleaned up addVideoCodec 2020-11-23 20:33:26 -05:00
f008b5b447 Merge remote-tracking branch 'paulgit/master' into newdesc 2020-11-23 20:22:03 -05:00
5482912e18 Merge pull request #273 from stazio/fix_rtp_protect
Fixed a typo during the protection of RTP packets
2020-11-23 22:59:29 +01:00
fcc4eaf78b Fixed a typo during the protection of RTP packets 2020-11-23 15:55:36 -05:00
cca0742973 Added some more description options 2020-11-23 15:54:01 -05:00
495b389e05 Fixed signaling state callback in C API test 2020-11-21 18:05:43 +01:00
e83494df09 Bumped version to 0.10.3 2020-11-21 17:28:24 +01:00
7bf87c6989 Updated libjuice to 0.6.1 2020-11-21 17:27:51 +01:00
cb591de15f Added DiffServ QoS in features 2020-11-21 16:06:09 +01:00
eb4540e319 Merge pull request #271 from paullouisageneau/fix-dscp-rtcp
Send RTCP with the same DSCP as RTP
2020-11-21 16:04:44 +01:00
5d34439cb7 Added warning when track queue is full 2020-11-21 15:55:51 +01:00
b19e9077af Reformatting 2020-11-21 15:52:58 +01:00
4ff010b20b Added maybe_unused 2020-11-21 15:51:49 +01:00
442e50825c Changed warnings for media support 2020-11-21 15:47:51 +01:00
9f2801b7b9 Set the same DSCP for RTP and RTCP 2020-11-21 15:46:54 +01:00
3b0bf3a152 Updated libjuice 2020-11-21 15:30:14 +01:00
fe4a9ec453 Merge pull request #269 from paullouisageneau/update-libsrtp
Update libsrtp
2020-11-21 13:17:45 +01:00
dc1d14adf1 Merge pull request #270 from paullouisageneau/fix-dscp
Fix DSCP for tracks
2020-11-21 13:17:31 +01:00
14918c16e8 Fixed track DSCP 2020-11-21 12:43:34 +01:00
a023acfa33 Updated libsrtp 2020-11-21 12:43:00 +01:00
f098019c1f Merge pull request #268 from paullouisageneau/dscp
Differentiated Services support
2020-11-21 00:56:05 +01:00
a67ca9da9b Implemented support for DSCP 2020-11-21 00:45:18 +01:00
613ebf2564 Merge pull request #267 from paullouisageneau/windows-examples-dynamic
Link examples against dynamic library on Windows
2020-11-20 00:46:36 +01:00
dc6427770c Made winsock library dependency public 2020-11-20 00:29:39 +01:00
81e0a05a1a Added missing exports 2020-11-20 00:29:31 +01:00
9ea613f05f Also link tests against the dynamic library on Windows 2020-11-20 00:13:56 +01:00
eb4a764648 Disable MSVC warning 4251 in header rather than in CMakeLists 2020-11-20 00:04:53 +01:00
baf8a3adce Made examples also link against the dynamic library on Windows 2020-11-19 23:39:49 +01:00
d9aec59352 Fixed include 2020-11-19 23:29:44 +01:00
3ff5801512 Merge pull request #264 from hanseuljun/cpp-export
Exporting C++ API for Windows
2020-11-19 23:02:34 +01:00
fcc2577e11 Merge pull request #266 from supercairos/fix/msvc-w4-build-error-shadow-class-member
fix build warning on windows when built with MSVC /W4.
2020-11-19 23:00:10 +01:00
b4865f26e4 Move RTC_CPP_EXPORT from log.hpp to include.hpp, remove RTC_CPP_EXPORT from /src classes, and add RTC_CPP_EXPORT to Description. 2020-11-19 08:09:00 -08:00
fc6d5afdd9 fix build warning on windows when built with MSVC /W4.
Some arguments where conflicting with data member names.
2020-11-19 14:10:52 +01:00
7a49c0b88b Updated libjuice 2020-11-19 09:41:29 +01:00
679c0ccd2e Add more dllexports, leave RTC_CPP_EXPORT only in log.hpp, and ignore C4251 warning. 2020-11-18 14:55:17 -08:00
12098e7c41 Start exporting c++ API for windows. 2020-11-17 22:20:54 -08:00
48 changed files with 444 additions and 394 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.7) cmake_minimum_required(VERSION 3.7)
project(libdatachannel project(libdatachannel
VERSION 0.10.2 VERSION 0.10.5
LANGUAGES CXX) LANGUAGES CXX)
set(PROJECT_DESCRIPTION "WebRTC Data Channels Library") set(PROJECT_DESCRIPTION "WebRTC Data Channels Library")
@ -33,10 +33,11 @@ endif()
set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules) set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
set(BUILD_SHARED_LIBS OFF) # to force usrsctp to be built static
if(WIN32) if(WIN32)
add_definitions(-DWIN32_LEAN_AND_MEAN) add_definitions(-DWIN32_LEAN_AND_MEAN)
if (MSVC) if(MSVC)
add_definitions(-DNOMINMAX) add_definitions(-DNOMINMAX)
add_definitions(-D_CRT_SECURE_NO_WARNINGS) add_definitions(-D_CRT_SECURE_NO_WARNINGS)
add_definitions(-D_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING) add_definitions(-D_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING)
@ -163,8 +164,8 @@ target_link_libraries(datachannel-static PUBLIC Threads::Threads plog::plog)
target_link_libraries(datachannel-static PRIVATE Usrsctp::Usrsctp) target_link_libraries(datachannel-static PRIVATE Usrsctp::Usrsctp)
if(WIN32) if(WIN32)
target_link_libraries(datachannel PRIVATE ws2_32) # winsock2 target_link_libraries(datachannel PUBLIC ws2_32) # winsock2
target_link_libraries(datachannel-static PRIVATE ws2_32) # winsock2 target_link_libraries(datachannel-static PUBLIC ws2_32) # winsock2
endif() endif()
if(NO_MEDIA) if(NO_MEDIA)
@ -272,11 +273,7 @@ if(NOT NO_TESTS)
set_target_properties(datachannel-tests PROPERTIES OUTPUT_NAME tests) set_target_properties(datachannel-tests PROPERTIES OUTPUT_NAME tests)
endif() endif()
target_include_directories(datachannel-tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) target_include_directories(datachannel-tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
if(WIN32) target_link_libraries(datachannel-tests datachannel)
target_link_libraries(datachannel-tests datachannel-static) # DLL exports only the C API
else()
target_link_libraries(datachannel-tests datachannel)
endif()
# Benchmark # Benchmark
add_executable(datachannel-benchmark test/benchmark.cpp) add_executable(datachannel-benchmark test/benchmark.cpp)
@ -288,11 +285,7 @@ if(NOT NO_TESTS)
endif() endif()
target_compile_definitions(datachannel-benchmark PRIVATE BENCHMARK_MAIN=1) target_compile_definitions(datachannel-benchmark PRIVATE BENCHMARK_MAIN=1)
target_include_directories(datachannel-benchmark PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) target_include_directories(datachannel-benchmark PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
if(WIN32) target_link_libraries(datachannel-benchmark datachannel)
target_link_libraries(datachannel-benchmark datachannel-static) # DLL exports only the C API
else()
target_link_libraries(datachannel-benchmark datachannel)
endif()
endif() endif()
# Examples # Examples

View File

@ -15,9 +15,9 @@ lib libdatachannel
: # requirements : # requirements
<cxxstd>17 <cxxstd>17
<include>./include/rtc <include>./include/rtc
<define>USE_NICE=0
<define>RTC_ENABLE_MEDIA=0 <define>RTC_ENABLE_MEDIA=0
<define>RTC_ENABLE_WEBSOCKET=0 <define>RTC_ENABLE_WEBSOCKET=0
<define>USE_NICE=0
<toolset>msvc:<define>WIN32_LEAN_AND_MEAN <toolset>msvc:<define>WIN32_LEAN_AND_MEAN
<toolset>msvc:<define>NOMINMAX <toolset>msvc:<define>NOMINMAX
<toolset>msvc:<define>_CRT_SECURE_NO_WARNINGS <toolset>msvc:<define>_CRT_SECURE_NO_WARNINGS
@ -32,6 +32,8 @@ lib libdatachannel
<link>static <link>static
: # usage requirements : # usage requirements
<include>./include <include>./include
<define>RTC_ENABLE_MEDIA=0
<define>RTC_ENABLE_WEBSOCKET=0
<library>/libdatachannel//plog <library>/libdatachannel//plog
<toolset>gcc:<cxxflags>"-pthread -Wno-pedantic -Wno-unused-parameter -Wno-unused-variable" <toolset>gcc:<cxxflags>"-pthread -Wno-pedantic -Wno-unused-parameter -Wno-unused-variable"
<toolset>clang:<cxxflags>"-pthread -Wno-pedantic -Wno-unused-parameter -Wno-unused-variable" <toolset>clang:<cxxflags>"-pthread -Wno-pedantic -Wno-unused-parameter -Wno-unused-variable"
@ -94,7 +96,7 @@ rule make_libusrsctp ( targets * : sources * : properties * )
} }
actions make_libusrsctp actions make_libusrsctp
{ {
(cd $(CWD)/deps/usrsctp && mkdir -p $(BUILD_DIR) && cd $(BUILD_DIR) && cmake -DCMAKE_BUILD_TYPE=$(VARIANT) -DCMAKE_C_FLAGS="-fPIC -Wno-unknown-warning-option -Wno-format-truncation" .. && make -j2 usrsctp-static) (cd $(CWD)/deps/usrsctp && mkdir -p $(BUILD_DIR) && cd $(BUILD_DIR) && cmake -DCMAKE_BUILD_TYPE=$(VARIANT) -DCMAKE_C_FLAGS="-fPIC -Wno-unknown-warning-option -Wno-format-truncation" -Dsctp_build_shared_lib=0 -Dsctp_build_programs=0 .. && make -j2 usrsctp)
cp $(CWD)/deps/usrsctp/$(BUILD_DIR)/usrsctplib/libusrsctp.a $(<) cp $(CWD)/deps/usrsctp/$(BUILD_DIR)/usrsctplib/libusrsctp.a $(<)
} }
rule make_libusrsctp_msvc ( targets * : sources * : properties * ) rule make_libusrsctp_msvc ( targets * : sources * : properties * )
@ -109,7 +111,7 @@ actions make_libusrsctp_msvc
cd $(CWD)/deps/usrsctp cd $(CWD)/deps/usrsctp
mkdir $(BUILD_DIR) mkdir $(BUILD_DIR)
cd $(BUILD_DIR) cd $(BUILD_DIR)
cmake -G "Visual Studio 16 2019" .. cmake -G "Visual Studio 16 2019" -Dsctp_build_shared_lib=0 -Dsctp_build_programs=0 ..
msbuild usrsctplib.sln /property:Configuration=$(VARIANT) msbuild usrsctplib.sln /property:Configuration=$(VARIANT)
cd %OLDD% cd %OLDD%
cp $(CWD)/deps/usrsctp/$(BUILD_DIR)/usrsctplib/Release/usrsctp.lib $(<) cp $(CWD)/deps/usrsctp/$(BUILD_DIR)/usrsctplib/Release/usrsctp.lib $(<)

View File

@ -27,8 +27,9 @@ Features:
- Full IPv6 support - Full IPv6 support
- Trickle ICE ([draft-ietf-ice-trickle-21](https://tools.ietf.org/html/draft-ietf-ice-trickle-21)) - Trickle ICE ([draft-ietf-ice-trickle-21](https://tools.ietf.org/html/draft-ietf-ice-trickle-21))
- JSEP compatible ([draft-ietf-rtcweb-jsep-26](https://tools.ietf.org/html/draft-ietf-rtcweb-jsep-26)) - JSEP compatible ([draft-ietf-rtcweb-jsep-26](https://tools.ietf.org/html/draft-ietf-rtcweb-jsep-26))
- SRTP and SRTCP key derivation from DTLS ([RFC5764](https://tools.ietf.org/html/rfc5764))
- Multicast DNS candidates ([draft-ietf-rtcweb-mdns-ice-candidates-04](https://tools.ietf.org/html/draft-ietf-rtcweb-mdns-ice-candidates-04)) - Multicast DNS candidates ([draft-ietf-rtcweb-mdns-ice-candidates-04](https://tools.ietf.org/html/draft-ietf-rtcweb-mdns-ice-candidates-04))
- SRTP and SRTCP key derivation from DTLS ([RFC5764](https://tools.ietf.org/html/rfc5764))
- Differentiated Services QoS ([draft-ietf-tsvwg-rtcweb-qos-18](https://tools.ietf.org/html/draft-ietf-tsvwg-rtcweb-qos-18))
- TURN relaying ([RFC5766](https://tools.ietf.org/html/rfc5766)) with [libnice](https://github.com/libnice/libnice) as ICE backend - TURN relaying ([RFC5766](https://tools.ietf.org/html/rfc5766)) with [libnice](https://github.com/libnice/libnice) as ICE backend
Note only SDP BUNDLE mode is supported for media multiplexing ([draft-ietf-mmusic-sdp-bundle-negotiation-54](https://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-54)). The behavior is equivalent to the JSEP bundle-only policy: the library always negociates one unique network component, where SRTP media streams are multiplexed with SRTCP control packets ([RFC5761](https://tools.ietf.org/html/rfc5761)) and SCTP/DTLS data traffic ([RFC5764](https://tools.ietf.org/html/rfc5764)). Note only SDP BUNDLE mode is supported for media multiplexing ([draft-ietf-mmusic-sdp-bundle-negotiation-54](https://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-54)). The behavior is equivalent to the JSEP bundle-only policy: the library always negociates one unique network component, where SRTP media streams are multiplexed with SRTCP control packets ([RFC5761](https://tools.ietf.org/html/rfc5761)) and SCTP/DTLS data traffic ([RFC5764](https://tools.ietf.org/html/rfc5764)).

2
deps/libjuice vendored

2
deps/libsrtp vendored

View File

@ -9,14 +9,9 @@ target_compile_definitions(datachannel-client PUBLIC STATIC_GETOPT)
else() else()
add_executable(datachannel-client main.cpp parse_cl.cpp parse_cl.h) add_executable(datachannel-client main.cpp parse_cl.cpp parse_cl.h)
endif() endif()
set_target_properties(datachannel-client PROPERTIES set_target_properties(datachannel-client PROPERTIES
CXX_STANDARD 17 CXX_STANDARD 17
OUTPUT_NAME client) OUTPUT_NAME client)
if(WIN32)
target_link_libraries(datachannel-client datachannel-static) # DLL exports only the C API
else()
target_link_libraries(datachannel-client datachannel)
endif()
target_link_libraries(datachannel-client datachannel nlohmann_json) target_link_libraries(datachannel-client datachannel nlohmann_json)

View File

@ -23,17 +23,18 @@
#include "rtc/rtc.hpp" #include "rtc/rtc.hpp"
#include "parse_cl.h"
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <algorithm> #include <algorithm>
#include <future>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <random> #include <random>
#include <thread>
#include <future>
#include <stdexcept> #include <stdexcept>
#include <thread>
#include <unordered_map> #include <unordered_map>
#include "parse_cl.h"
using namespace rtc; using namespace rtc;
using namespace std; using namespace std;
@ -47,25 +48,23 @@ unordered_map<string, shared_ptr<PeerConnection>> peerConnectionMap;
unordered_map<string, shared_ptr<DataChannel>> dataChannelMap; unordered_map<string, shared_ptr<DataChannel>> dataChannelMap;
string localId; string localId;
bool echoDataChannelMessages = false;
shared_ptr<PeerConnection> createPeerConnection(const Configuration &config, shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
weak_ptr<WebSocket> wws, string id); weak_ptr<WebSocket> wws, string id);
void printReceived(bool echoed, string id, string type, size_t length);
string randomId(size_t length); string randomId(size_t length);
int main(int argc, char **argv) try { int main(int argc, char **argv) try {
auto params = std::make_unique<Cmdline>(argc, argv); auto params = std::make_unique<Cmdline>(argc, argv);
rtc::InitLogger(LogLevel::Debug); rtc::InitLogger(LogLevel::Info);
Configuration config; Configuration config;
string stunServer = ""; string stunServer = "";
if (params->noStun()) { if (params->noStun()) {
cout << "No STUN server is configured. Only local hosts and public IP addresses supported." << endl; cout << "No STUN server is configured. Only local hosts and public IP addresses supported."
<< endl;
} else { } else {
if (params->stunServer().substr(0,5).compare("stun:") != 0) { if (params->stunServer().substr(0, 5).compare("stun:") != 0) {
stunServer = "stun:"; stunServer = "stun:";
} }
stunServer += params->stunServer() + ":" + to_string(params->stunPort()); stunServer += params->stunServer() + ":" + to_string(params->stunPort());
@ -76,27 +75,23 @@ int main(int argc, char **argv) try {
localId = randomId(4); localId = randomId(4);
cout << "The local ID is: " << localId << endl; cout << "The local ID is: " << localId << endl;
echoDataChannelMessages = params->echoDataChannelMessages();
cout << "Received data channel messages will be "
<< (echoDataChannelMessages ? "echoed back to sender" : "printed to stdout") << endl;
auto ws = make_shared<WebSocket>(); auto ws = make_shared<WebSocket>();
std::promise<void> wsPromise; std::promise<void> wsPromise;
auto wsFuture = wsPromise.get_future(); auto wsFuture = wsPromise.get_future();
ws->onOpen([&wsPromise]() { ws->onOpen([&wsPromise]() {
cout << "WebSocket connected, signaling ready" << endl; cout << "WebSocket connected, signaling ready" << endl;
wsPromise.set_value(); wsPromise.set_value();
}); });
ws->onError([&wsPromise](string s) { ws->onError([&wsPromise](string s) {
cout << "WebSocket error" << endl; cout << "WebSocket error" << endl;
wsPromise.set_exception(std::make_exception_ptr(std::runtime_error(s))); wsPromise.set_exception(std::make_exception_ptr(std::runtime_error(s)));
}); });
ws->onClosed([]() { cout << "WebSocket closed" << endl; }); ws->onClosed([]() { cout << "WebSocket closed" << endl; });
ws->onMessage([&](variant<binary, string> data) { ws->onMessage([&](variant<binary, string> data) {
if (!holds_alternative<string>(data)) if (!holds_alternative<string>(data))
return; return;
@ -134,14 +129,14 @@ int main(int argc, char **argv) try {
}); });
string wsPrefix = ""; string wsPrefix = "";
if (params->webSocketServer().substr(0,5).compare("ws://") != 0) { if (params->webSocketServer().substr(0, 5).compare("ws://") != 0) {
wsPrefix = "ws://"; wsPrefix = "ws://";
} }
const string url = wsPrefix + params->webSocketServer() + ":" + const string url = wsPrefix + params->webSocketServer() + ":" +
to_string(params->webSocketPort()) + "/" + localId; to_string(params->webSocketPort()) + "/" + localId;
cout << "Url is " << url << endl; cout << "Url is " << url << endl;
ws->open(url); ws->open(url);
cout << "Waiting for signaling to be connected..." << endl; cout << "Waiting for signaling to be connected..." << endl;
wsFuture.get(); wsFuture.get();
@ -171,20 +166,12 @@ int main(int argc, char **argv) try {
dc->onClosed([id]() { cout << "DataChannel from " << id << " closed" << endl; }); dc->onClosed([id]() { cout << "DataChannel from " << id << " closed" << endl; });
dc->onMessage([id, wdc = make_weak_ptr(dc)](const variant<binary, string> &message) { dc->onMessage([id, wdc = make_weak_ptr(dc)](variant<binary, string> data) {
static bool firstMessage = true; if (holds_alternative<string>(data))
if (holds_alternative<string>(message) && (!echoDataChannelMessages || firstMessage)) { cout << "Message from " << id << " received: " << get<string>(data) << endl;
cout << "Message from " << id << " received: " << get<string>(message) << endl; else
firstMessage = false; cout << "Binary message from " << id
} else if (echoDataChannelMessages) { << " received, size=" << get<binary>(data).size() << endl;
bool echoed = false;
if (auto dc = wdc.lock()) {
dc->send(message);
echoed = true;
}
printReceived(echoed, id, (holds_alternative<string>(message) ? "text" : "binary"),
get<string>(message).length());
}
}); });
dataChannelMap.emplace(id, dc); dataChannelMap.emplace(id, dc);
@ -237,20 +224,12 @@ shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
dc->onClosed([id]() { cout << "DataChannel from " << id << " closed" << endl; }); dc->onClosed([id]() { cout << "DataChannel from " << id << " closed" << endl; });
dc->onMessage([id, wdc = make_weak_ptr(dc)](const variant<binary, string> &message) { dc->onMessage([id, wdc = make_weak_ptr(dc)](variant<binary, string> data) {
static bool firstMessage = true; if (holds_alternative<string>(data))
if (holds_alternative<string>(message) && (!echoDataChannelMessages || firstMessage)) { cout << "Message from " << id << " received: " << get<string>(data) << endl;
cout << "Message from " << id << " received: " << get<string>(message) << endl; else
firstMessage = false; cout << "Binary message from " << id
} else if (echoDataChannelMessages) { << " received, size=" << get<binary>(data).size() << endl;
bool echoed = false;
if (auto dc = wdc.lock()) {
dc->send(message);
echoed = true;
}
printReceived(echoed, id, (holds_alternative<string>(message) ? "text" : "binary"),
get<string>(message).length());
}
}); });
dc->send("Hello from " + localId); dc->send("Hello from " + localId);
@ -262,20 +241,6 @@ shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
return pc; return pc;
}; };
// Helper function to print received pings
void printReceived(bool echoed, string id, string type, size_t length) {
static long count = 0;
static long freq = 100;
if (!(++count%freq)) {
cout << "Received " << count << " pings in total from " << id << ", most recent of type "
<< type << " and " << (echoed ? "" : "un") << "successfully echoed most recent ping of size "
<< length << " back to " << id << endl;
if (count >= (freq * 10) && freq < 1000000) {
freq *= 10;
}
}
}
// Helper function to generate a random ID // Helper function to generate a random ID
string randomId(size_t length) { string randomId(size_t length) {
static const string characters( static const string characters(

View File

@ -43,47 +43,39 @@ Cmdline::Cmdline (int argc, char *argv[]) // ISO C++17 not allowed: throw (std::
static struct option long_options[] = static struct option long_options[] =
{ {
{"echo", no_argument, NULL, 'e'},
{"noStun", no_argument, NULL, 'n'}, {"noStun", no_argument, NULL, 'n'},
{"stunServer", required_argument, NULL, 's'}, {"stunServer", required_argument, NULL, 's'},
{"stunPort", required_argument, NULL, 't'}, {"stunPort", required_argument, NULL, 't'},
{"webSocketServer", required_argument, NULL, 'w'}, {"webSocketServer", required_argument, NULL, 'w'},
{"webSocketPort", required_argument, NULL, 'x'}, {"webSocketPort", required_argument, NULL, 'x'},
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
}; };
_program_name += argv[0]; _program_name += argv[0];
/* default values */ /* default values */
_e = false;
_n = false; _n = false;
_s = "stun.l.google.com"; _s = "stun.l.google.com";
_t = 19302; _t = 19302;
_w = "localhost"; _w = "localhost";
_x = 8000; _x = 8000;
_h = false; _h = false;
_v = false;
optind = 0; optind = 0;
while ((c = getopt_long (argc, argv, "s:t:w:x:enhv", long_options, &optind)) != - 1) while ((c = getopt_long (argc, argv, "s:t:w:x:enhv", long_options, &optind)) != - 1)
{ {
switch (c) switch (c)
{ {
case 'e': case 'n':
_e = true;
break;
case 'n':
_n = true; _n = true;
break; break;
case 's': case 's':
_s = optarg; _s = optarg;
break; break;
case 't': case 't':
_t = atoi (optarg); _t = atoi (optarg);
if (_t < 0) if (_t < 0)
{ {
@ -99,11 +91,11 @@ Cmdline::Cmdline (int argc, char *argv[]) // ISO C++17 not allowed: throw (std::
} }
break; break;
case 'w': case 'w':
_w = optarg; _w = optarg;
break; break;
case 'x': case 'x':
_x = atoi (optarg); _x = atoi (optarg);
if (_x < 0) if (_x < 0)
{ {
@ -119,16 +111,11 @@ Cmdline::Cmdline (int argc, char *argv[]) // ISO C++17 not allowed: throw (std::
} }
break; break;
case 'h': case 'h':
_h = true; _h = true;
this->usage (EXIT_SUCCESS); this->usage (EXIT_SUCCESS);
break; break;
case 'v':
_v = true;
this->version (EXIT_SUCCESS);
break;
default: default:
this->usage (EXIT_FAILURE); this->usage (EXIT_FAILURE);
@ -155,8 +142,6 @@ void Cmdline::usage (int status)
std::cout << "\ std::cout << "\
usage: " << _program_name << " [ -enstwxhv ] \n\ usage: " << _program_name << " [ -enstwxhv ] \n\
libdatachannel client implementing WebRTC Data Channels with WebSocket signaling\n\ libdatachannel client implementing WebRTC Data Channels with WebSocket signaling\n\
[ -e ] [ --echo ] (type=FLAG)\n\
Echo data channel messages back to sender rather than putting to stdout.\n\
[ -n ] [ --noStun ] (type=FLAG)\n\ [ -n ] [ --noStun ] (type=FLAG)\n\
Do NOT use a stun server (overrides -s and -t).\n\ Do NOT use a stun server (overrides -s and -t).\n\
[ -s ] [ --stunServer ] (type=STRING, default=stun.l.google.com)\n\ [ -s ] [ --stunServer ] (type=STRING, default=stun.l.google.com)\n\
@ -168,15 +153,8 @@ libdatachannel client implementing WebRTC Data Channels with WebSocket signaling
[ -x ] [ --webSocketPort ] (type=INTEGER, range=0...65535, default=8000)\n\ [ -x ] [ --webSocketPort ] (type=INTEGER, range=0...65535, default=8000)\n\
Web socket server port.\n\ Web socket server port.\n\
[ -h ] [ --help ] (type=FLAG)\n\ [ -h ] [ --help ] (type=FLAG)\n\
Display this help and exit.\n\ Display this help and exit.\n";
[ -v ] [ --version ] (type=FLAG)\n\
Output version information and exit.\n";
} }
exit (status); exit (status);
} }
void Cmdline::version (int status)
{
std::cout << _program_name << " v0.5\n";
exit (status);
}

View File

@ -34,14 +34,12 @@ class Cmdline
{ {
private: private:
/* parameters */ /* parameters */
bool _e;
bool _n; bool _n;
std::string _s; std::string _s;
int _t; int _t;
std::string _w; std::string _w;
int _x; int _x;
bool _h; bool _h;
bool _v;
/* other stuff to keep track of */ /* other stuff to keep track of */
std::string _program_name; std::string _program_name;
@ -55,20 +53,15 @@ public:
/* usage function */ /* usage function */
void usage (int status); void usage (int status);
/* version function */
void version (int status);
/* return next (non-option) parameter */ /* return next (non-option) parameter */
int next_param () { return _optind; } int next_param () { return _optind; }
bool echoDataChannelMessages () const { return _e; }
bool noStun () const { return _n; } bool noStun () const { return _n; }
std::string stunServer () const { return _s; } std::string stunServer () const { return _s; }
int stunPort () const { return _t; } int stunPort () const { return _t; }
std::string webSocketServer () const { return _w; } std::string webSocketServer () const { return _w; }
int webSocketPort () const { return _x; } int webSocketPort () const { return _x; }
bool h () const { return _h; } bool h () const { return _h; }
bool v () const { return _v; }
}; };
#endif #endif

View File

@ -137,7 +137,7 @@ int main(int argc, char **argv) {
size_t candidate_size = 0; size_t candidate_size = 0;
if (getline(&candidate, &candidate_size, stdin)) { if (getline(&candidate, &candidate_size, stdin)) {
rtcAddRemoteCandidate(peer->pc, candidate, "0"); rtcAddRemoteCandidate(peer->pc, candidate, NULL);
free(candidate); free(candidate);
} else { } else {

View File

@ -140,7 +140,7 @@ int main(int argc, char **argv) {
char *candidate = NULL; char *candidate = NULL;
size_t candidate_size = 0; size_t candidate_size = 0;
if (getline(&candidate, &candidate_size, stdin)) { if (getline(&candidate, &candidate_size, stdin)) {
rtcAddRemoteCandidate(peer->pc, candidate, "0"); rtcAddRemoteCandidate(peer->pc, candidate, NULL);
free(candidate); free(candidate);
} else { } else {

View File

@ -4,19 +4,11 @@ add_executable(datachannel-copy-paste-offerer offerer.cpp)
set_target_properties(datachannel-copy-paste-offerer PROPERTIES set_target_properties(datachannel-copy-paste-offerer PROPERTIES
CXX_STANDARD 17 CXX_STANDARD 17
OUTPUT_NAME offerer) OUTPUT_NAME offerer)
if(WIN32) target_link_libraries(datachannel-copy-paste-offerer datachannel)
target_link_libraries(datachannel-copy-paste-offerer datachannel-static) # DLL exports only the C API
else()
target_link_libraries(datachannel-copy-paste-offerer datachannel)
endif()
add_executable(datachannel-copy-paste-answerer answerer.cpp) add_executable(datachannel-copy-paste-answerer answerer.cpp)
set_target_properties(datachannel-copy-paste-answerer PROPERTIES set_target_properties(datachannel-copy-paste-answerer PROPERTIES
CXX_STANDARD 17 CXX_STANDARD 17
OUTPUT_NAME answerer) OUTPUT_NAME answerer)
if(WIN32) target_link_libraries(datachannel-copy-paste-answerer datachannel)
target_link_libraries(datachannel-copy-paste-answerer datachannel-static) # DLL exports only the C API
else()
target_link_libraries(datachannel-copy-paste-answerer datachannel)
endif()

View File

@ -4,11 +4,5 @@ add_executable(datachannel-media main.cpp)
set_target_properties(datachannel-media PROPERTIES set_target_properties(datachannel-media PROPERTIES
CXX_STANDARD 17 CXX_STANDARD 17
OUTPUT_NAME media) OUTPUT_NAME media)
target_link_libraries(datachannel-media datachannel nlohmann_json)
if(WIN32)
target_link_libraries(datachannel-media datachannel-static) # DLL exports only the C API
else()
target_link_libraries(datachannel-media datachannel)
endif()
target_link_libraries(datachannel-media datachannel nlohmann_json)

View File

@ -4,12 +4,5 @@ add_executable(datachannel-sfu-media main.cpp)
set_target_properties(datachannel-sfu-media PROPERTIES set_target_properties(datachannel-sfu-media PROPERTIES
CXX_STANDARD 17 CXX_STANDARD 17
OUTPUT_NAME sfu-media) OUTPUT_NAME sfu-media)
if(WIN32)
target_link_libraries(datachannel-sfu-media datachannel-static) # DLL exports only the C API
else()
target_link_libraries(datachannel-sfu-media datachannel)
endif()
target_link_libraries(datachannel-sfu-media datachannel nlohmann_json) target_link_libraries(datachannel-sfu-media datachannel nlohmann_json)

View File

@ -59,6 +59,7 @@ function openSignaling(url) {
ws.onmessage = (e) => { ws.onmessage = (e) => {
if(typeof(e.data) != 'string') return; if(typeof(e.data) != 'string') return;
const message = JSON.parse(e.data); const message = JSON.parse(e.data);
console.log(message);
const { id, type } = message; const { id, type } = message;
let pc = peerConnectionMap[id]; let pc = peerConnectionMap[id];

View File

@ -25,7 +25,7 @@
namespace rtc { namespace rtc {
class Candidate { class RTC_CPP_EXPORT Candidate {
public: public:
enum class Family { Unresolved, Ipv4, Ipv6 }; enum class Family { Unresolved, Ipv4, Ipv6 };
enum class Type { Unknown, Host, ServerReflexive, PeerReflexive, Relayed }; enum class Type { Unknown, Host, ServerReflexive, PeerReflexive, Relayed };
@ -76,8 +76,8 @@ private:
} // namespace rtc } // namespace rtc
std::ostream &operator<<(std::ostream &out, const rtc::Candidate &candidate); RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, const rtc::Candidate &candidate);
std::ostream &operator<<(std::ostream &out, const rtc::Candidate::Type &type); RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, const rtc::Candidate::Type &type);
std::ostream &operator<<(std::ostream &out, const rtc::Candidate::TransportType &transportType); RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, const rtc::Candidate::TransportType &transportType);
#endif #endif

View File

@ -28,13 +28,14 @@
namespace rtc { namespace rtc {
class Channel { class RTC_CPP_EXPORT Channel {
public: public:
Channel() = default; Channel() = default;
virtual ~Channel() = default; virtual ~Channel() = default;
virtual void close() = 0; virtual void close() = 0;
virtual bool send(message_variant data) = 0; // returns false if buffered virtual bool send(message_variant data) = 0; // returns false if buffered
virtual bool send(const byte *data, size_t size) = 0;
virtual bool isOpen() const = 0; virtual bool isOpen() const = 0;
virtual bool isClosed() const = 0; virtual bool isClosed() const = 0;

View File

@ -26,7 +26,7 @@
namespace rtc { namespace rtc {
struct IceServer { struct RTC_CPP_EXPORT IceServer {
enum class Type { Stun, Turn }; enum class Type { Stun, Turn };
enum class RelayType { TurnUdp, TurnTcp, TurnTls }; enum class RelayType { TurnUdp, TurnTcp, TurnTls };
@ -51,7 +51,7 @@ struct IceServer {
RelayType relayType; RelayType relayType;
}; };
struct ProxyServer { struct RTC_CPP_EXPORT ProxyServer {
enum class Type { None = 0, Socks5, Http, Last = Http }; enum class Type { None = 0, Socks5, Http, Last = Http };
ProxyServer(Type type_, string ip_, uint16_t port_, string username_ = "", ProxyServer(Type type_, string ip_, uint16_t port_, string username_ = "",
@ -64,7 +64,7 @@ struct ProxyServer {
string password; string password;
}; };
struct Configuration { struct RTC_CPP_EXPORT Configuration {
std::vector<IceServer> iceServers; std::vector<IceServer> iceServers;
std::optional<ProxyServer> proxyServer; std::optional<ProxyServer> proxyServer;
bool enableIceTcp = false; bool enableIceTcp = false;

View File

@ -36,7 +36,7 @@ namespace rtc {
class SctpTransport; class SctpTransport;
class PeerConnection; class PeerConnection;
class DataChannel : public std::enable_shared_from_this<DataChannel>, public Channel { class RTC_CPP_EXPORT DataChannel : public std::enable_shared_from_this<DataChannel>, public Channel {
public: public:
DataChannel(std::weak_ptr<PeerConnection> pc, uint16_t stream, string label, string protocol, DataChannel(std::weak_ptr<PeerConnection> pc, uint16_t stream, string label, string protocol,
Reliability reliability); Reliability reliability);
@ -50,7 +50,7 @@ public:
void close(void) override; void close(void) override;
bool send(message_variant data) override; bool send(message_variant data) override;
bool send(const byte *data, size_t size); bool send(const byte *data, size_t size) override;
template <typename Buffer> bool sendBuffer(const Buffer &buf); template <typename Buffer> bool sendBuffer(const Buffer &buf);
template <typename Iterator> bool sendBuffer(Iterator first, Iterator last); template <typename Iterator> bool sendBuffer(Iterator first, Iterator last);
@ -87,7 +87,7 @@ private:
friend class PeerConnection; friend class PeerConnection;
}; };
class NegociatedDataChannel final : public DataChannel { class RTC_CPP_EXPORT NegociatedDataChannel final : public DataChannel {
public: public:
NegociatedDataChannel(std::weak_ptr<PeerConnection> pc, uint16_t stream, string label, NegociatedDataChannel(std::weak_ptr<PeerConnection> pc, uint16_t stream, string label,
string protocol, Reliability reliability); string protocol, Reliability reliability);

View File

@ -32,7 +32,16 @@
namespace rtc { namespace rtc {
class Description { const string DEFAULT_AUDIO_PROFILE =
"minptime=10;maxaveragebitrate=96000;stereo=1;sprop-stereo=1;useinbandfec=1";
// Use Constrained Baseline profile Level 4.2 (necessary for Firefox)
// https://developer.mozilla.org/en-US/docs/Web/Media/Formats/WebRTC_codecs#Supported_video_codecs
// TODO: Should be 42E0 but 42C0 appears to be more compatible. Investigate this.
const string DEFAULT_VIDEO_PROFILE =
"profile-level-id=42e01f;packetization-mode=1;level-asymmetry-allowed=1";
class RTC_CPP_EXPORT Description {
public: public:
enum class Type { Unspec, Offer, Answer, Pranswer, Rollback }; enum class Type { Unspec, Offer, Answer, Pranswer, Rollback };
enum class Role { ActPass, Passive, Active }; enum class Role { ActPass, Passive, Active };
@ -63,7 +72,7 @@ public:
string generateSdp(string_view eol) const; string generateSdp(string_view eol) const;
string generateApplicationSdp(string_view eol) const; string generateApplicationSdp(string_view eol) const;
class Entry { class RTC_CPP_EXPORT Entry {
public: public:
virtual ~Entry() = default; virtual ~Entry() = default;
@ -95,7 +104,7 @@ public:
Direction mDirection; Direction mDirection;
}; };
struct Application : public Entry { struct RTC_CPP_EXPORT Application : public Entry {
public: public:
Application(string mid = "data"); Application(string mid = "data");
virtual ~Application() = default; virtual ~Application() = default;
@ -120,7 +129,7 @@ public:
}; };
// Media (non-data) // Media (non-data)
class Media : public Entry { class RTC_CPP_EXPORT Media : public Entry {
public: public:
Media(const string &sdp); Media(const string &sdp);
Media(const string &mline, string mid, Direction dir = Direction::SendOnly); Media(const string &mline, string mid, Direction dir = Direction::SendOnly);
@ -131,9 +140,10 @@ public:
void removeFormat(const string &fmt); void removeFormat(const string &fmt);
void addSSRC(uint32_t ssrc, std::string name); void addSSRC(uint32_t ssrc, std::optional<string> name,
void addSSRC(uint32_t ssrc); std::optional<string> msid = nullopt);
void replaceSSRC(uint32_t oldSSRC, uint32_t ssrc, string name); void replaceSSRC(uint32_t oldSSRC, uint32_t ssrc, std::optional<string> name,
std::optional<string> msid = nullopt);
bool hasSSRC(uint32_t ssrc); bool hasSSRC(uint32_t ssrc);
std::vector<uint32_t> getSSRCs(); std::vector<uint32_t> getSSRCs();
@ -142,6 +152,9 @@ public:
bool hasPayloadType(int payloadType) const; bool hasPayloadType(int payloadType) const;
void addRTXCodec(unsigned int payloadType, unsigned int originalPayloadType,
unsigned int clockRate);
virtual void parseSdpLine(string_view line) override; virtual void parseSdpLine(string_view line) override;
struct RTPMap { struct RTPMap {
@ -150,7 +163,7 @@ public:
void removeFB(const string &string); void removeFB(const string &string);
void addFB(const string &string); void addFB(const string &string);
void addAttribute(std::string attr) { fmtps.emplace_back(attr); } void addAttribute(string attr) { fmtps.emplace_back(std::move(attr)); }
int pt; int pt;
string format; string format;
@ -181,21 +194,27 @@ public:
public: public:
void addRTPMap(const RTPMap &map); void addRTPMap(const RTPMap &map);
void removeSSRC(uint32_t oldSSRC);
}; };
class Audio : public Media { class RTC_CPP_EXPORT Audio : public Media {
public: public:
Audio(string mid = "audio", Direction dir = Direction::SendOnly); Audio(string mid = "audio", Direction dir = Direction::SendOnly);
void addAudioCodec(int payloadType, const string &codec); void addAudioCodec(int payloadType, string codec,
std::optional<string> profile = DEFAULT_AUDIO_PROFILE);
void addOpusCodec(int payloadType); void addOpusCodec(int payloadType);
}; };
class Video : public Media { class RTC_CPP_EXPORT Video : public Media {
public: public:
Video(string mid = "video", Direction dir = Direction::SendOnly); Video(string mid = "video", Direction dir = Direction::SendOnly);
void addVideoCodec(int payloadType, const string &codec); void addVideoCodec(int payloadType, string codec,
std::optional<string> profile = DEFAULT_VIDEO_PROFILE);
void addH264Codec(int payloadType); void addH264Codec(int payloadType);
void addVP8Codec(int payloadType); void addVP8Codec(int payloadType);
void addVP9Codec(int payloadType); void addVP9Codec(int payloadType);
@ -245,8 +264,8 @@ private:
} // namespace rtc } // namespace rtc
std::ostream &operator<<(std::ostream &out, const rtc::Description &description); RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, const rtc::Description &description);
std::ostream &operator<<(std::ostream &out, rtc::Description::Type type); RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::Description::Type type);
std::ostream &operator<<(std::ostream &out, rtc::Description::Role role); RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::Description::Role role);
#endif #endif

View File

@ -28,9 +28,15 @@
#endif #endif
#ifdef _WIN32 #ifdef _WIN32
#define RTC_CPP_EXPORT __declspec(dllexport)
#ifndef _WIN32_WINNT #ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0602 // Windows 8 #define _WIN32_WINNT 0x0602 // Windows 8
#endif #endif
#ifdef _MSC_VER
#pragma warning(disable:4251) // disable "X needs to have dll-interface..."
#endif
#else
#define RTC_CPP_EXPORT
#endif #endif
#include "log.hpp" #include "log.hpp"

View File

@ -27,7 +27,7 @@ namespace rtc {
using init_token = std::shared_ptr<void>; using init_token = std::shared_ptr<void>;
class Init { class RTC_CPP_EXPORT Init {
public: public:
static init_token Token(); static init_token Token();
static void Preload(); static void Preload();

View File

@ -35,6 +35,8 @@
#pragma warning(pop) #pragma warning(pop)
#endif #endif
#include "include.hpp"
namespace rtc { namespace rtc {
enum class LogLevel { // Don't change, it must match plog severity enum class LogLevel { // Don't change, it must match plog severity
@ -47,8 +49,8 @@ enum class LogLevel { // Don't change, it must match plog severity
Verbose = 6 Verbose = 6
}; };
void InitLogger(LogLevel level); RTC_CPP_EXPORT void InitLogger(LogLevel level);
void InitLogger(plog::Severity severity, plog::IAppender *appender = nullptr); RTC_CPP_EXPORT void InitLogger(plog::Severity severity, plog::IAppender *appender = nullptr);
} // namespace rtc } // namespace rtc
#endif #endif

View File

@ -29,7 +29,7 @@
namespace rtc { namespace rtc {
struct Message : binary { struct RTC_CPP_EXPORT Message : binary {
enum Type { Binary, String, Control, Reset }; enum Type { Binary, String, Control, Reset };
Message(const Message &message) = default; Message(const Message &message) = default;
@ -42,7 +42,8 @@ struct Message : binary {
Message(binary &&data, Type type_ = Binary) : binary(std::move(data)), type(type_) {} Message(binary &&data, Type type_ = Binary) : binary(std::move(data)), type(type_) {}
Type type; Type type;
unsigned int stream = 0; unsigned int stream = 0; // Stream id (SCTP stream or SSRC)
unsigned int dscp = 0; // Differentiated Services Code Point
std::shared_ptr<Reliability> reliability; std::shared_ptr<Reliability> reliability;
}; };

View File

@ -50,14 +50,14 @@ class SctpTransport;
using certificate_ptr = std::shared_ptr<Certificate>; using certificate_ptr = std::shared_ptr<Certificate>;
using future_certificate_ptr = std::shared_future<certificate_ptr>; using future_certificate_ptr = std::shared_future<certificate_ptr>;
struct DataChannelInit { struct RTC_CPP_EXPORT DataChannelInit {
Reliability reliability = {}; Reliability reliability = {};
bool negotiated = false; bool negotiated = false;
std::optional<uint16_t> id = nullopt; std::optional<uint16_t> id = nullopt;
string protocol = ""; string protocol = "";
}; };
class PeerConnection final : public std::enable_shared_from_this<PeerConnection> { class RTC_CPP_EXPORT PeerConnection final : public std::enable_shared_from_this<PeerConnection> {
public: public:
enum class State : int { enum class State : int {
New = RTC_NEW, New = RTC_NEW,
@ -157,6 +157,8 @@ private:
void processLocalCandidate(Candidate candidate); void processLocalCandidate(Candidate candidate);
void processRemoteDescription(Description description); void processRemoteDescription(Description description);
void processRemoteCandidate(Candidate candidate); void processRemoteCandidate(Candidate candidate);
string localBundleMid() const;
void triggerDataChannel(std::weak_ptr<DataChannel> weakDataChannel); void triggerDataChannel(std::weak_ptr<DataChannel> weakDataChannel);
void triggerTrack(std::shared_ptr<Track> track); void triggerTrack(std::shared_ptr<Track> track);
bool changeState(State state); bool changeState(State state);
@ -203,8 +205,8 @@ private:
} // namespace rtc } // namespace rtc
std::ostream &operator<<(std::ostream &out, rtc::PeerConnection::State state); RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::PeerConnection::State state);
std::ostream &operator<<(std::ostream &out, rtc::PeerConnection::GatheringState state); RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::PeerConnection::GatheringState state);
std::ostream &operator<<(std::ostream &out, rtc::PeerConnection::SignalingState state); RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, rtc::PeerConnection::SignalingState state);
#endif #endif

View File

@ -29,7 +29,7 @@
namespace rtc { namespace rtc {
class RtcpHandler { class RTC_CPP_EXPORT RtcpHandler {
protected: protected:
/** /**
* Use this callback when trying to send custom data (such as RTCP) to the client. * Use this callback when trying to send custom data (such as RTCP) to the client.
@ -64,7 +64,7 @@ public:
class Track; class Track;
// An RtcpSession can be plugged into a Track to handle the whole RTCP session // An RtcpSession can be plugged into a Track to handle the whole RTCP session
class RtcpReceivingSession : public RtcpHandler { class RTC_CPP_EXPORT RtcpReceivingSession : public RtcpHandler {
public: public:
rtc::message_ptr incoming(rtc::message_ptr ptr) override; rtc::message_ptr incoming(rtc::message_ptr ptr) override;
rtc::message_ptr outgoing(rtc::message_ptr ptr) override; rtc::message_ptr outgoing(rtc::message_ptr ptr) override;

View File

@ -20,7 +20,7 @@
#ifndef RTC_RTP_HPP #ifndef RTC_RTP_HPP
#define RTC_RTP_HPP #define RTC_RTP_HPP
#include <rtc/log.hpp> #include "log.hpp"
#include <cmath> #include <cmath>
@ -66,28 +66,31 @@ public:
inline uint32_t ssrc() const { return ntohl(_ssrc); } inline uint32_t ssrc() const { return ntohl(_ssrc); }
inline size_t getSize() const { inline size_t getSize() const {
return ((char *)&csrc) - ((char *)this) + sizeof(SSRC) * csrcCount(); return reinterpret_cast<const char *>(&csrc) - reinterpret_cast<const char *>(this) +
sizeof(SSRC) * csrcCount();
} }
char *getBody() const { return ((char *)&csrc) + sizeof(SSRC) * csrcCount(); } [[nodiscard]] char *getBody() {
return reinterpret_cast<char *>(&csrc) + sizeof(SSRC) * csrcCount();
}
[[nodiscard]] const char *getBody() const {
return reinterpret_cast<const char *>(&csrc) + sizeof(SSRC) * csrcCount();
}
inline void setSeqNumber(uint16_t newSeqNo) { _seqNumber = htons(newSeqNo); } inline void setSeqNumber(uint16_t newSeqNo) { _seqNumber = htons(newSeqNo); }
inline void setPayloadType(uint8_t newPayloadType) { inline void setPayloadType(uint8_t newPayloadType) {
_payloadType = (_payloadType & 0b10000000u) | (0b01111111u & newPayloadType); _payloadType = (_payloadType & 0b10000000u) | (0b01111111u & newPayloadType);
} }
inline void setSsrc(uint32_t ssrc) { _ssrc = htonl(ssrc); } inline void setSsrc(uint32_t in_ssrc) { _ssrc = htonl(in_ssrc); }
void setTimestamp(uint32_t i) { _timestamp = htonl(i); } void setTimestamp(uint32_t i) { _timestamp = htonl(i); }
void log() { void log() {
PLOG_VERBOSE << "RTP V: " << (int) version() PLOG_VERBOSE << "RTP V: " << (int)version() << " P: " << (padding() ? "P" : " ")
<< " P: " << (padding() ? "P" : " ") << " X: " << (extension() ? "X" : " ") << " CC: " << (int)csrcCount()
<< " X: " << (extension() ? "X" : " ") << " M: " << (marker() ? "M" : " ") << " PT: " << (int)payloadType()
<< " CC: " << (int) csrcCount() << " SEQNO: " << seqNumber() << " TS: " << timestamp();
<< " M: " << (marker() ? "M" : " ")
<< " PT: " << (int) payloadType()
<< " SEQNO: " << seqNumber()
<< " TS: " << timestamp();
} }
}; };
@ -103,13 +106,13 @@ private:
uint32_t _delaySinceLastReport; uint32_t _delaySinceLastReport;
public: public:
inline void preparePacket(SSRC ssrc, [[maybe_unused]] unsigned int packetsLost, inline void preparePacket(SSRC in_ssrc, [[maybe_unused]] unsigned int packetsLost,
[[maybe_unused]] unsigned int totalPackets, uint16_t highestSeqNo, [[maybe_unused]] unsigned int totalPackets, uint16_t highestSeqNo,
uint16_t seqNoCycles, uint32_t jitter, uint64_t lastSR_NTP, uint16_t seqNoCycles, uint32_t jitter, uint64_t lastSR_NTP,
uint64_t lastSR_DELAY) { uint64_t lastSR_DELAY) {
setSeqNo(highestSeqNo, seqNoCycles); setSeqNo(highestSeqNo, seqNoCycles);
setJitter(jitter); setJitter(jitter);
setSSRC(ssrc); setSSRC(in_ssrc);
// Middle 32 bits of NTP Timestamp // Middle 32 bits of NTP Timestamp
// this->lastReport = lastSR_NTP >> 16u; // this->lastReport = lastSR_NTP >> 16u;
@ -117,22 +120,23 @@ public:
setDelaySinceSR(uint32_t(lastSR_DELAY)); setDelaySinceSR(uint32_t(lastSR_DELAY));
// The delay, expressed in units of 1/65536 seconds // The delay, expressed in units of 1/65536 seconds
// this->delaySinceLastReport = lastSR_DELAY; // this->delaySinceLastReport = lastSR_DELAY;
} }
inline void setSSRC(SSRC ssrc) { this->ssrc = htonl(ssrc); } inline void setSSRC(SSRC in_ssrc) { this->ssrc = htonl(in_ssrc); }
inline SSRC getSSRC() const { return ntohl(ssrc); } [[nodiscard]] inline SSRC getSSRC() const { return ntohl(ssrc); }
inline void setPacketsLost([[maybe_unused]] unsigned int packetsLost, inline void setPacketsLost([[maybe_unused]] unsigned int packetsLost,
[[maybe_unused]] unsigned int totalPackets) { [[maybe_unused]] unsigned int totalPackets) {
// TODO Implement loss percentages. // TODO Implement loss percentages.
_fractionLostAndPacketsLost = 0; _fractionLostAndPacketsLost = 0;
} }
inline unsigned int getLossPercentage() const {
[[nodiscard]] inline unsigned int getLossPercentage() const {
// TODO Implement loss percentages. // TODO Implement loss percentages.
return 0; return 0;
} }
inline unsigned int getPacketLostCount() const { [[nodiscard]] inline unsigned int getPacketLostCount() const {
// TODO Implement total packets lost. // TODO Implement total packets lost.
return 0; return 0;
} }
@ -149,13 +153,13 @@ public:
inline void setJitter(uint32_t jitter) { _jitter = htonl(jitter); } inline void setJitter(uint32_t jitter) { _jitter = htonl(jitter); }
inline void setNTPOfSR(uint64_t ntp) { _lastReport = htonll(ntp >> 16u); } inline void setNTPOfSR(uint64_t ntp) { _lastReport = htonll(ntp >> 16u); }
inline uint32_t getNTPOfSR() const { return ntohl(_lastReport) << 16u; } [[nodiscard]] inline uint32_t getNTPOfSR() const { return ntohl(_lastReport) << 16u; }
inline void setDelaySinceSR(uint32_t sr) { inline void setDelaySinceSR(uint32_t sr) {
// The delay, expressed in units of 1/65536 seconds // The delay, expressed in units of 1/65536 seconds
_delaySinceLastReport = htonl(sr); _delaySinceLastReport = htonl(sr);
} }
inline uint32_t getDelaySinceSR() const { return ntohl(_delaySinceLastReport); } [[nodiscard]] inline uint32_t getDelaySinceSR() const { return ntohl(_delaySinceLastReport); }
inline void log() const { inline void log() const {
PLOG_VERBOSE << "RTCP report block: " PLOG_VERBOSE << "RTCP report block: "
@ -199,9 +203,9 @@ public:
inline void log() const { inline void log() const {
PLOG_VERBOSE << "RTCP header: " PLOG_VERBOSE << "RTCP header: "
<< "version=" << unsigned(version()) << ", padding=" << padding() << "version=" << unsigned(version()) << ", padding=" << padding()
<< ", reportCount=" << unsigned(reportCount()) << ", reportCount=" << unsigned(reportCount())
<< ", payloadType=" << unsigned(payloadType()) << ", length=" << length(); << ", payloadType=" << unsigned(payloadType()) << ", length=" << length();
} }
}; };
@ -246,8 +250,10 @@ public:
this->_senderSSRC = htonl(senderSSRC); this->_senderSSRC = htonl(senderSSRC);
} }
inline RTCP_ReportBlock *getReportBlock(int num) { return &_reportBlocks + num; } [[nodiscard]] inline RTCP_ReportBlock *getReportBlock(int num) { return &_reportBlocks + num; }
inline const RTCP_ReportBlock *getReportBlock(int num) const { return &_reportBlocks + num; } [[nodiscard]] inline const RTCP_ReportBlock *getReportBlock(int num) const {
return &_reportBlocks + num;
}
[[nodiscard]] inline size_t getSize() const { [[nodiscard]] inline size_t getSize() const {
// "length" in packet is one less than the number of 32 bit words in the packet. // "length" in packet is one less than the number of 32 bit words in the packet.
@ -284,8 +290,10 @@ private:
RTCP_ReportBlock _reportBlocks; RTCP_ReportBlock _reportBlocks;
public: public:
inline RTCP_ReportBlock *getReportBlock(int num) { return &_reportBlocks + num; } [[nodiscard]] inline RTCP_ReportBlock *getReportBlock(int num) { return &_reportBlocks + num; }
inline const RTCP_ReportBlock *getReportBlock(int num) const { return &_reportBlocks + num; } [[nodiscard]] inline const RTCP_ReportBlock *getReportBlock(int num) const {
return &_reportBlocks + num;
}
inline SSRC senderSSRC() const { return ntohl(_senderSSRC); } inline SSRC senderSSRC() const { return ntohl(_senderSSRC); }
inline void setSenderSSRC(SSRC ssrc) { this->_senderSSRC = htonl(ssrc); } inline void setSenderSSRC(SSRC ssrc) { this->_senderSSRC = htonl(ssrc); }
@ -337,7 +345,7 @@ struct RTCP_REMB {
return sizeof(uint32_t) * (1 + header.header.length()); return sizeof(uint32_t) * (1 + header.header.length());
} }
void preparePacket(SSRC senderSSRC, unsigned int numSSRC, unsigned int bitrate) { void preparePacket(SSRC senderSSRC, unsigned int numSSRC, unsigned int in_bitrate) {
// Report Count becomes the format here. // Report Count becomes the format here.
header.header.prepareHeader(206, 15, 0); header.header.prepareHeader(206, 15, 0);
@ -352,21 +360,21 @@ struct RTCP_REMB {
id[2] = 'M'; id[2] = 'M';
id[3] = 'B'; id[3] = 'B';
setBitrate(numSSRC, bitrate); setBitrate(numSSRC, in_bitrate);
} }
void setBitrate(unsigned int numSSRC, unsigned int bitrate) { void setBitrate(unsigned int numSSRC, unsigned int in_bitrate) {
unsigned int exp = 0; unsigned int exp = 0;
while (bitrate > pow(2, 18) - 1) { while (in_bitrate > pow(2, 18) - 1) {
exp++; exp++;
bitrate /= 2; in_bitrate /= 2;
} }
// "length" in packet is one less than the number of 32 bit words in the packet. // "length" in packet is one less than the number of 32 bit words in the packet.
header.header.setLength( header.header.setLength(
uint16_t((offsetof(RTCP_REMB, ssrc) / sizeof(uint32_t)) - 1 + numSSRC)); uint16_t((offsetof(RTCP_REMB, ssrc) / sizeof(uint32_t)) - 1 + numSSRC));
this->bitrate = htonl((numSSRC << (32u - 8u)) | (exp << (32u - 8u - 6u)) | bitrate); this->bitrate = htonl((numSSRC << (32u - 8u)) | (exp << (32u - 8u - 6u)) | in_bitrate);
} }
void setSsrc(int iterator, SSRC newSssrc) { ssrc[iterator] = htonl(newSssrc); } void setSsrc(int iterator, SSRC newSssrc) { ssrc[iterator] = htonl(newSssrc); }
@ -428,7 +436,7 @@ public:
public: public:
void preparePacket(SSRC ssrc, unsigned int discreteSeqNoCount) { void preparePacket(SSRC ssrc, unsigned int discreteSeqNoCount) {
header.header.prepareHeader(205, 1, 2 + discreteSeqNoCount); header.header.prepareHeader(205, 1, 2 + uint16_t(discreteSeqNoCount));
header.setMediaSourceSSRC(ssrc); header.setMediaSourceSSRC(ssrc);
header.setPacketSenderSSRC(ssrc); header.setPacketSenderSSRC(ssrc);
} }
@ -481,11 +489,15 @@ public:
return ntohs(*(uint16_t *)(header.getBody())); return ntohs(*(uint16_t *)(header.getBody()));
} }
char *getBody() { return header.getBody() + sizeof(uint16_t); } [[nodiscard]] char *getBody() { return header.getBody() + sizeof(uint16_t); }
size_t getBodySize(size_t totalSize) { return totalSize - ((char *)getBody() - (char *)this); } [[nodiscard]] const char *getBody() const { return header.getBody() + sizeof(uint16_t); }
RTP &getHeader() { return header; } [[nodiscard]] size_t getBodySize(size_t totalSize) {
return totalSize - (getBody() - reinterpret_cast<char *>(this));
}
[[nodiscard]] RTP &getHeader() { return header; }
size_t normalizePacket(size_t totalSize, SSRC originalSSRC, uint8_t originalPayloadType) { size_t normalizePacket(size_t totalSize, SSRC originalSSRC, uint8_t originalPayloadType) {
header.setSeqNumber(getOriginalSeqNo()); header.setSeqNumber(getOriginalSeqNo());

View File

@ -35,7 +35,7 @@ namespace rtc {
class DtlsSrtpTransport; class DtlsSrtpTransport;
#endif #endif
class Track final : public std::enable_shared_from_this<Track>, public Channel { class RTC_CPP_EXPORT Track final : public std::enable_shared_from_this<Track>, public Channel {
public: public:
Track(Description::Media description); Track(Description::Media description);
~Track() = default; ~Track() = default;
@ -47,7 +47,7 @@ public:
void close(void) override; void close(void) override;
bool send(message_variant data) override; bool send(message_variant data) override;
bool send(const byte *data, size_t size); bool send(const byte *data, size_t size) override;
bool isOpen(void) const override; bool isOpen(void) const override;
bool isClosed(void) const override; bool isClosed(void) const override;
@ -70,8 +70,8 @@ private:
std::weak_ptr<DtlsSrtpTransport> mDtlsSrtpTransport; std::weak_ptr<DtlsSrtpTransport> mDtlsSrtpTransport;
#endif #endif
bool outgoing(message_ptr message);
void incoming(message_ptr message); void incoming(message_ptr message);
bool outgoing(message_ptr message);
Description::Media mMediaDescription; Description::Media mMediaDescription;
std::atomic<bool> mIsClosed = false; std::atomic<bool> mIsClosed = false;

View File

@ -38,7 +38,7 @@ class TcpTransport;
class TlsTransport; class TlsTransport;
class WsTransport; class WsTransport;
class WebSocket final : public Channel, public std::enable_shared_from_this<WebSocket> { class RTC_CPP_EXPORT WebSocket final : public Channel, public std::enable_shared_from_this<WebSocket> {
public: public:
enum class State : int { enum class State : int {
Connecting = 0, Connecting = 0,
@ -49,6 +49,7 @@ public:
struct Configuration { struct Configuration {
bool disableTlsVerification = false; // if true, don't verify the TLS certificate bool disableTlsVerification = false; // if true, don't verify the TLS certificate
std::vector<string> protocols;
}; };
WebSocket(std::optional<Configuration> config = nullopt); WebSocket(std::optional<Configuration> config = nullopt);
@ -59,6 +60,7 @@ public:
void open(const string &url); void open(const string &url);
void close() override; void close() override;
bool send(const message_variant data) override; bool send(const message_variant data) override;
bool send(const byte *data, size_t size) override;
bool isOpen() const override; bool isOpen() const override;
bool isClosed() const override; bool isClosed() const override;

View File

@ -117,9 +117,9 @@ void Candidate::parse(string candidate) {
mTransportType = TransportType::Udp; mTransportType = TransportType::Udp;
} else if (transport == "TCP" || transport == "tcp") { } else if (transport == "TCP" || transport == "tcp") {
// Peek tail to find TCP type // Peek tail to find TCP type
std::istringstream iss(mTail); std::istringstream tiss(mTail);
string tcptype_, tcptype; string tcptype_, tcptype;
if (iss >> tcptype_ >> tcptype && tcptype_ == "tcptype") { if (tiss >> tcptype_ >> tcptype && tcptype_ == "tcptype") {
if (auto it = TcpTypeMap.find(tcptype); it != TcpTypeMap.end()) if (auto it = TcpTypeMap.find(tcptype); it != TcpTypeMap.end())
mTransportType = it->second; mTransportType = it->second;
else else

View File

@ -29,13 +29,13 @@
#include <unordered_map> #include <unordered_map>
using std::shared_ptr; using std::shared_ptr;
using std::size_t;
using std::string;
using std::string_view;
using std::chrono::system_clock; using std::chrono::system_clock;
namespace { namespace {
using std::string;
using std::string_view;
inline bool match_prefix(string_view str, string_view prefix) { inline bool match_prefix(string_view str, string_view prefix) {
return str.size() >= prefix.size() && return str.size() >= prefix.size() &&
std::mismatch(prefix.begin(), prefix.end(), str.begin()).first == prefix.end(); std::mismatch(prefix.begin(), prefix.end(), str.begin()).first == prefix.end();
@ -483,8 +483,7 @@ string Description::Entry::generateSdpLines(string_view eol) const {
} }
for (const auto &attr : mAttributes) { for (const auto &attr : mAttributes) {
if (attr.find("extmap") == std::string::npos && if (attr.find("extmap") == string::npos && attr.find("rtcp-rsize") == string::npos)
attr.find("rtcp-rsize") == std::string::npos)
sdp << "a=" << attr << eol; sdp << "a=" << attr << eol;
} }
@ -519,12 +518,21 @@ Description::Entry::removeAttribute(std::vector<string>::iterator it) {
return mAttributes.erase(it); return mAttributes.erase(it);
} }
void Description::Media::addSSRC(uint32_t ssrc, std::string name) { void Description::Media::addSSRC(uint32_t ssrc, std::optional<string> name,
mAttributes.emplace_back("ssrc:" + std::to_string(ssrc) + " cname:" + name); std::optional<string> msid) {
if (name)
mAttributes.emplace_back("ssrc:" + std::to_string(ssrc) + " cname:" + *name);
else
mAttributes.emplace_back("ssrc:" + std::to_string(ssrc));
if (msid)
mAttributes.emplace_back("ssrc:" + std::to_string(ssrc) + " msid:" + *msid + " " + *msid);
mSsrcs.emplace_back(ssrc); mSsrcs.emplace_back(ssrc);
} }
void Description::Media::replaceSSRC(uint32_t oldSSRC, uint32_t ssrc, std::string name) { void Description::Media::replaceSSRC(uint32_t oldSSRC, uint32_t ssrc, std::optional<string> name,
std::optional<string> msid) {
auto it = mAttributes.begin(); auto it = mAttributes.begin();
while (it != mAttributes.end()) { while (it != mAttributes.end()) {
if (it->find("ssrc:" + std::to_string(oldSSRC)) == 0) { if (it->find("ssrc:" + std::to_string(oldSSRC)) == 0) {
@ -532,11 +540,17 @@ void Description::Media::replaceSSRC(uint32_t oldSSRC, uint32_t ssrc, std::strin
} else } else
it++; it++;
} }
mAttributes.emplace_back("ssrc:" + std::to_string(ssrc) + " cname:" + name); addSSRC(ssrc, std::move(name), std::move(msid));
} }
void Description::Media::addSSRC(uint32_t ssrc) { void Description::Media::removeSSRC(uint32_t oldSSRC) {
mAttributes.emplace_back("ssrc:" + std::to_string(ssrc)); auto it = mAttributes.begin();
while (it != mAttributes.end()) {
if (it->find("ssrc:" + std::to_string(oldSSRC)) == 0) {
it = mAttributes.erase(it);
} else
it++;
}
} }
bool Description::Media::hasSSRC(uint32_t ssrc) { bool Description::Media::hasSSRC(uint32_t ssrc) {
@ -689,32 +703,17 @@ void Description::Media::removeFormat(const string &fmt) {
} }
} }
void Description::Video::addVideoCodec(int payloadType, const string &codec) { void Description::Video::addVideoCodec(int payloadType, string codec,
std::optional<string> profile) {
RTPMap map(std::to_string(payloadType) + ' ' + codec + "/90000"); RTPMap map(std::to_string(payloadType) + ' ' + codec + "/90000");
map.addFB("nack"); map.addFB("nack");
map.addFB("nack pli"); map.addFB("nack pli");
// map.addFB("nack fir"); // map.addFB("ccm fir");
map.addFB("goog-remb"); map.addFB("goog-remb");
if (codec == "H264") { if (profile)
// Use Constrained Baseline profile Level 4.2 (necessary for Firefox) map.fmtps.emplace_back(*profile);
// https://developer.mozilla.org/en-US/docs/Web/Media/Formats/WebRTC_codecs#Supported_video_codecs
// TODO: Should be 42E0 but 42C0 appears to be more compatible. Investigate this.
map.fmtps.emplace_back(
"profile-level-id=4de01f;packetization-mode=1;level-asymmetry-allowed=1");
// Because certain Android devices don't like me, let us just negotiate some random
{
RTPMap map(std::to_string(payloadType + 1) + ' ' + codec + "/90000");
map.addFB("nack");
map.addFB("nack pli");
// map.addFB("nack fir");
map.addFB("goog-remb");
addRTPMap(map);
}
}
addRTPMap(map); addRTPMap(map);
// // RTX Packets
/* TODO /* TODO
* TIL that Firefox does not properly support the negotiation of RTX! It works, but doesn't * TIL that Firefox does not properly support the negotiation of RTX! It works, but doesn't
* negotiate the SSRC so we have no idea what SSRC is RTX going to be. Three solutions: One) we * negotiate the SSRC so we have no idea what SSRC is RTX going to be. Three solutions: One) we
@ -722,24 +721,34 @@ void Description::Video::addVideoCodec(int payloadType, const string &codec) {
* rebuild the original packet before we send it distribute it to each track. Three) we complain * rebuild the original packet before we send it distribute it to each track. Three) we complain
* to mozilla. This one probably won't do much. * to mozilla. This one probably won't do much.
*/ */
// RTPMap rtx(std::to_string(payloadType+1) + " rtx/90000"); // RTX Packets
// // TODO rtx-time is how long can a request be stashed for before needing to resend it. // RTPMap rtx(std::to_string(payloadType+1) + " rtx/90000");
// Needs to be parameterized rtx.addAttribute("apt=" + std::to_string(payloadType) + // // TODO rtx-time is how long can a request be stashed for before needing to resend it.
// ";rtx-time=3000"); addRTPMap(rtx); // Needs to be parameterized rtx.addAttribute("apt=" + std::to_string(payloadType) +
// ";rtx-time=3000"); addRTPMap(rtx);
} }
void Description::Audio::addAudioCodec(int payloadType, const string &codec) { void Description::Audio::addAudioCodec(int payloadType, string codec,
std::optional<string> profile) {
// TODO This 48000/2 should be parameterized // TODO This 48000/2 should be parameterized
RTPMap map(std::to_string(payloadType) + ' ' + codec + "/48000/2"); RTPMap map(std::to_string(payloadType) + ' ' + codec + "/48000/2");
map.fmtps.emplace_back("maxaveragebitrate=96000; stereo=1; sprop-stereo=1; useinbandfec=1"); if (profile)
map.fmtps.emplace_back(*profile);
addRTPMap(map);
}
void Description::Media::addRTXCodec(unsigned int payloadType, unsigned int originalPayloadType,
unsigned int clockRate) {
RTPMap map(std::to_string(payloadType) + " RTX/" + std::to_string(clockRate));
map.fmtps.emplace_back("apt=" + std::to_string(originalPayloadType));
addRTPMap(map); addRTPMap(map);
} }
void Description::Video::addH264Codec(int pt) { addVideoCodec(pt, "H264"); } void Description::Video::addH264Codec(int pt) { addVideoCodec(pt, "H264"); }
void Description::Video::addVP8Codec(int payloadType) { addVideoCodec(payloadType, "VP8"); } void Description::Video::addVP8Codec(int payloadType) { addVideoCodec(payloadType, "VP8", nullopt); }
void Description::Video::addVP9Codec(int payloadType) { addVideoCodec(payloadType, "VP9"); } void Description::Video::addVP9Codec(int payloadType) { addVideoCodec(payloadType, "VP9", nullopt); }
void Description::Media::setBitrate(int bitrate) { mBas = bitrate; } void Description::Media::setBitrate(int bitrate) { mBas = bitrate; }
@ -808,7 +817,7 @@ void Description::Media::parseSdpLine(string_view line) {
} else if (key == "rtcp-mux") { } else if (key == "rtcp-mux") {
// always added // always added
} else if (key == "ssrc") { } else if (key == "ssrc") {
mSsrcs.emplace_back(std::stoul((std::string)value)); mSsrcs.emplace_back(std::stoul(string(value)));
} else { } else {
Entry::parseSdpLine(line); Entry::parseSdpLine(line);
} }
@ -828,7 +837,7 @@ std::vector<uint32_t> Description::Media::getSSRCs() {
for (auto &val : mAttributes) { for (auto &val : mAttributes) {
PLOG_DEBUG << val; PLOG_DEBUG << val;
if (val.find("ssrc:") == 0) { if (val.find("ssrc:") == 0) {
vec.emplace_back(std::stoul((std::string)val.substr(5, val.find(" ")))); vec.emplace_back(std::stoul(string(val.substr(5, val.find(" ")))));
} }
} }
return vec; return vec;
@ -852,7 +861,7 @@ Description::Media::RTPMap::RTPMap(string_view mline) { setMLine(mline); }
void Description::Media::RTPMap::removeFB(const string &str) { void Description::Media::RTPMap::removeFB(const string &str) {
auto it = rtcpFbs.begin(); auto it = rtcpFbs.begin();
while (it != rtcpFbs.end()) { while (it != rtcpFbs.end()) {
if (it->find(str) != std::string::npos) { if (it->find(str) != string::npos) {
it = rtcpFbs.erase(it); it = rtcpFbs.erase(it);
} else } else
it++; it++;

View File

@ -110,7 +110,7 @@ bool DtlsSrtpTransport::sendMedia(message_ptr message) {
if (err == srtp_err_status_replay_fail) if (err == srtp_err_status_replay_fail)
throw std::runtime_error("SRTCP packet is a replay"); throw std::runtime_error("SRTCP packet is a replay");
else if (err == srtp_err_status_no_ctx) { else if (err == srtp_err_status_no_ctx) {
auto ssrc = ((RTCP_SR *)message->data())->senderSSRC(); auto ssrc = reinterpret_cast<RTCP_SR *>(message->data())->senderSSRC();
PLOG_INFO << "Adding SSRC to SRTCP: " << ssrc; PLOG_INFO << "Adding SSRC to SRTCP: " << ssrc;
addSSRC(ssrc); addSSRC(ssrc);
if ((err = srtp_protect_rtcp(mSrtpOut, message->data(), &size))) if ((err = srtp_protect_rtcp(mSrtpOut, message->data(), &size)))
@ -127,11 +127,11 @@ bool DtlsSrtpTransport::sendMedia(message_ptr message) {
if (err == srtp_err_status_replay_fail) if (err == srtp_err_status_replay_fail)
throw std::runtime_error("Outgoing SRTP packet is a replay"); throw std::runtime_error("Outgoing SRTP packet is a replay");
else if (err == srtp_err_status_no_ctx) { else if (err == srtp_err_status_no_ctx) {
auto ssrc = ((RTP *)message->data())->ssrc(); auto ssrc = reinterpret_cast<RTP *>(message->data())->ssrc();
PLOG_INFO << "Adding SSRC to RTP: " << ssrc; PLOG_INFO << "Adding SSRC to RTP: " << ssrc;
addSSRC(ssrc); addSSRC(ssrc);
if ((err = srtp_protect_rtcp(mSrtpOut, message->data(), &size))) if ((err = srtp_protect(mSrtpOut, message->data(), &size)))
throw std::runtime_error("SRTCP protect error, status=" + throw std::runtime_error("SRTP protect error, status=" +
to_string(static_cast<int>(err))); to_string(static_cast<int>(err)));
} else } else
throw std::runtime_error("SRTP protect error, status=" + throw std::runtime_error("SRTP protect error, status=" +
@ -141,7 +141,14 @@ bool DtlsSrtpTransport::sendMedia(message_ptr message) {
} }
message->resize(size); message->resize(size);
return outgoing(message);
if (message->dscp == 0) { // Track might override the value
// Set recommended medium-priority DSCP value
// See https://tools.ietf.org/html/draft-ietf-tsvwg-rtcweb-qos-18
message->dscp = 36; // AF42: Assured Forwarding class 4, medium drop probability
}
return Transport::outgoing(message); // bypass DTLS DSCP marking
} }
void DtlsSrtpTransport::incoming(message_ptr message) { void DtlsSrtpTransport::incoming(message_ptr message) {
@ -189,7 +196,7 @@ void DtlsSrtpTransport::incoming(message_ptr message) {
else if (err == srtp_err_status_auth_fail) else if (err == srtp_err_status_auth_fail)
PLOG_WARNING << "Incoming SRTCP packet failed authentication check"; PLOG_WARNING << "Incoming SRTCP packet failed authentication check";
else if (err == srtp_err_status_no_ctx) { else if (err == srtp_err_status_no_ctx) {
auto ssrc = ((RTCP_SR *)message->data())->senderSSRC(); auto ssrc = reinterpret_cast<RTCP_SR *>(message->data())->senderSSRC();
PLOG_INFO << "Adding SSRC to RTCP: " << ssrc; PLOG_INFO << "Adding SSRC to RTCP: " << ssrc;
addSSRC(ssrc); addSSRC(ssrc);
if ((err = srtp_unprotect_rtcp(mSrtpIn, message->data(), &size))) if ((err = srtp_unprotect_rtcp(mSrtpIn, message->data(), &size)))
@ -203,8 +210,7 @@ void DtlsSrtpTransport::incoming(message_ptr message) {
} }
PLOG_VERBOSE << "Unprotected SRTCP packet, size=" << size; PLOG_VERBOSE << "Unprotected SRTCP packet, size=" << size;
message->type = Message::Type::Control; message->type = Message::Type::Control;
auto rtp = (RTCP_SR *)message->data(); message->stream = reinterpret_cast<RTCP_SR *>(message->data())->senderSSRC();
message->stream = rtp->senderSSRC();
} else { } else {
PLOG_VERBOSE << "Incoming SRTP packet, size=" << size; PLOG_VERBOSE << "Incoming SRTP packet, size=" << size;
if (srtp_err_status_t err = srtp_unprotect(mSrtpIn, message->data(), &size)) { if (srtp_err_status_t err = srtp_unprotect(mSrtpIn, message->data(), &size)) {
@ -213,21 +219,20 @@ void DtlsSrtpTransport::incoming(message_ptr message) {
else if (err == srtp_err_status_auth_fail) else if (err == srtp_err_status_auth_fail)
PLOG_WARNING << "Incoming SRTP packet failed authentication check"; PLOG_WARNING << "Incoming SRTP packet failed authentication check";
else if (err == srtp_err_status_no_ctx) { else if (err == srtp_err_status_no_ctx) {
auto ssrc = ((RTP *)message->data())->ssrc(); auto ssrc = reinterpret_cast<RTP *>(message->data())->ssrc();
PLOG_INFO << "Adding SSRC to RTP: " << ssrc; PLOG_INFO << "Adding SSRC to RTP: " << ssrc;
addSSRC(ssrc); addSSRC(ssrc);
if ((err = srtp_unprotect(mSrtpIn, message->data(), &size))) if ((err = srtp_unprotect(mSrtpIn, message->data(), &size)))
throw std::runtime_error("SRTCP unprotect error, status=" + throw std::runtime_error("SRTP unprotect error, status=" +
to_string(static_cast<int>(err))); to_string(static_cast<int>(err)));
} else } else
PLOG_WARNING << "SRTP unprotect error, status=" << err PLOG_WARNING << "SRTP unprotect error, status=" << err
<< " SSRC=" << ((RTP *)message->data())->ssrc(); << " SSRC=" << reinterpret_cast<RTP *>(message->data())->ssrc();
return; return;
} }
PLOG_VERBOSE << "Unprotected SRTP packet, size=" << size; PLOG_VERBOSE << "Unprotected SRTP packet, size=" << size;
message->type = Message::Type::Binary; message->type = Message::Type::Binary;
auto rtp = (RTP *)message->data(); message->stream = reinterpret_cast<RTP *>(message->data())->ssrc();
message->stream = rtp->ssrc();
} }
message->resize(size); message->resize(size);

View File

@ -53,7 +53,7 @@ DtlsTransport::DtlsTransport(shared_ptr<IceTransport> lower, certificate_ptr cer
verifier_callback verifierCallback, state_callback stateChangeCallback) verifier_callback verifierCallback, state_callback stateChangeCallback)
: Transport(lower, std::move(stateChangeCallback)), mCertificate(certificate), : Transport(lower, std::move(stateChangeCallback)), mCertificate(certificate),
mVerifierCallback(std::move(verifierCallback)), mVerifierCallback(std::move(verifierCallback)),
mIsClient(lower->role() == Description::Role::Active) { mIsClient(lower->role() == Description::Role::Active), mCurrentDscp(0) {
PLOG_DEBUG << "Initializing DTLS transport (GnuTLS)"; PLOG_DEBUG << "Initializing DTLS transport (GnuTLS)";
@ -122,6 +122,7 @@ bool DtlsTransport::send(message_ptr message) {
PLOG_VERBOSE << "Send size=" << message->size(); PLOG_VERBOSE << "Send size=" << message->size();
mCurrentDscp = message->dscp;
ssize_t ret; ssize_t ret;
do { do {
ret = gnutls_record_send(mSession, message->data(), message->size()); ret = gnutls_record_send(mSession, message->data(), message->size());
@ -143,6 +144,13 @@ void DtlsTransport::incoming(message_ptr message) {
mIncomingQueue.push(message); mIncomingQueue.push(message);
} }
bool DtlsTransport::outgoing(message_ptr message) {
if (message->dscp == 0)
message->dscp = mCurrentDscp;
return Transport::outgoing(std::move(message));
}
void DtlsTransport::postHandshake() { void DtlsTransport::postHandshake() {
// Dummy // Dummy
} }
@ -309,7 +317,7 @@ DtlsTransport::DtlsTransport(shared_ptr<IceTransport> lower, shared_ptr<Certific
verifier_callback verifierCallback, state_callback stateChangeCallback) verifier_callback verifierCallback, state_callback stateChangeCallback)
: Transport(lower, std::move(stateChangeCallback)), mCertificate(certificate), : Transport(lower, std::move(stateChangeCallback)), mCertificate(certificate),
mVerifierCallback(std::move(verifierCallback)), mVerifierCallback(std::move(verifierCallback)),
mIsClient(lower->role() == Description::Role::Active) { mIsClient(lower->role() == Description::Role::Active), mCurrentDscp(0) {
PLOG_DEBUG << "Initializing DTLS transport (OpenSSL)"; PLOG_DEBUG << "Initializing DTLS transport (OpenSSL)";
try { try {
@ -405,6 +413,7 @@ bool DtlsTransport::send(message_ptr message) {
PLOG_VERBOSE << "Send size=" << message->size(); PLOG_VERBOSE << "Send size=" << message->size();
mCurrentDscp = message->dscp;
int ret = SSL_write(mSsl, message->data(), int(message->size())); int ret = SSL_write(mSsl, message->data(), int(message->size()));
return openssl::check(mSsl, ret); return openssl::check(mSsl, ret);
} }
@ -419,6 +428,13 @@ void DtlsTransport::incoming(message_ptr message) {
mIncomingQueue.push(message); mIncomingQueue.push(message);
} }
bool DtlsTransport::outgoing(message_ptr message) {
if (message->dscp == 0)
message->dscp = mCurrentDscp;
return Transport::outgoing(std::move(message));
}
void DtlsTransport::postHandshake() { void DtlsTransport::postHandshake() {
// Dummy // Dummy
} }

View File

@ -53,6 +53,7 @@ public:
protected: protected:
virtual void incoming(message_ptr message) override; virtual void incoming(message_ptr message) override;
virtual bool outgoing(message_ptr message) override;
virtual void postHandshake(); virtual void postHandshake();
void runRecvLoop(); void runRecvLoop();
@ -62,6 +63,7 @@ protected:
Queue<message_ptr> mIncomingQueue; Queue<message_ptr> mIncomingQueue;
std::thread mRecvThread; std::thread mRecvThread;
std::atomic<unsigned int> mCurrentDscp;
#if USE_GNUTLS #if USE_GNUTLS
gnutls_session_t mSession; gnutls_session_t mSession;

View File

@ -168,7 +168,9 @@ bool IceTransport::addRemoteCandidate(const Candidate &candidate) {
return juice_add_remote_candidate(mAgent.get(), string(candidate).c_str()) >= 0; return juice_add_remote_candidate(mAgent.get(), string(candidate).c_str()) >= 0;
} }
void IceTransport::gatherLocalCandidates() { void IceTransport::gatherLocalCandidates(string mid) {
mMid = std::move(mid);
// Change state now as candidates calls can be synchronous // Change state now as candidates calls can be synchronous
changeGatheringState(GatheringState::InProgress); changeGatheringState(GatheringState::InProgress);
@ -222,8 +224,10 @@ bool IceTransport::send(message_ptr message) {
} }
bool IceTransport::outgoing(message_ptr message) { bool IceTransport::outgoing(message_ptr message) {
return juice_send(mAgent.get(), reinterpret_cast<const char *>(message->data()), // Explicit Congestion Notification takes the least-significant 2 bits of the DS field
message->size()) >= 0; int ds = int(message->dscp << 2);
return juice_send_diffserv(mAgent.get(), reinterpret_cast<const char *>(message->data()),
message->size(), ds) >= 0;
} }
void IceTransport::changeGatheringState(GatheringState state) { void IceTransport::changeGatheringState(GatheringState state) {
@ -330,7 +334,7 @@ IceTransport::IceTransport(const Configuration &config, candidate_callback candi
mMid("0"), mGatheringState(GatheringState::New), mMid("0"), mGatheringState(GatheringState::New),
mCandidateCallback(std::move(candidateCallback)), mCandidateCallback(std::move(candidateCallback)),
mGatheringStateChangeCallback(std::move(gatheringStateChangeCallback)), mGatheringStateChangeCallback(std::move(gatheringStateChangeCallback)),
mNiceAgent(nullptr, nullptr), mMainLoop(nullptr, nullptr) { mNiceAgent(nullptr, nullptr), mMainLoop(nullptr, nullptr), mOutgoingDscp(0) {
PLOG_DEBUG << "Initializing ICE transport (libnice)"; PLOG_DEBUG << "Initializing ICE transport (libnice)";
@ -580,7 +584,9 @@ bool IceTransport::addRemoteCandidate(const Candidate &candidate) {
return ret > 0; return ret > 0;
} }
void IceTransport::gatherLocalCandidates() { void IceTransport::gatherLocalCandidates(string mid) {
mMid = std::move(mid);
// Change state now as candidates calls can be synchronous // Change state now as candidates calls can be synchronous
changeGatheringState(GatheringState::InProgress); changeGatheringState(GatheringState::InProgress);
@ -617,6 +623,13 @@ bool IceTransport::send(message_ptr message) {
} }
bool IceTransport::outgoing(message_ptr message) { bool IceTransport::outgoing(message_ptr message) {
std::lock_guard lock(mOutgoingMutex);
if (mOutgoingDscp != message->dscp) {
mOutgoingDscp = message->dscp;
// Explicit Congestion Notification takes the least-significant 2 bits of the DS field
int ds = int(message->dscp << 2);
nice_agent_set_stream_tos(mNiceAgent.get(), mStreamId, ds); // ToS is the legacy name for DS
}
return nice_agent_send(mNiceAgent.get(), mStreamId, 1, message->size(), return nice_agent_send(mNiceAgent.get(), mStreamId, 1, message->size(),
reinterpret_cast<const char *>(message->data())) >= 0; reinterpret_cast<const char *>(message->data())) >= 0;
} }

View File

@ -35,6 +35,7 @@
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#include <mutex>
namespace rtc { namespace rtc {
@ -55,7 +56,7 @@ public:
Description getLocalDescription(Description::Type type) const; Description getLocalDescription(Description::Type type) const;
void setRemoteDescription(const Description &description); void setRemoteDescription(const Description &description);
bool addRemoteCandidate(const Candidate &candidate); bool addRemoteCandidate(const Candidate &candidate);
void gatherLocalCandidates(); void gatherLocalCandidates(string mid);
std::optional<string> getLocalAddress() const; std::optional<string> getLocalAddress() const;
std::optional<string> getRemoteAddress() const; std::optional<string> getRemoteAddress() const;
@ -99,6 +100,8 @@ private:
std::unique_ptr<GMainLoop, void (*)(GMainLoop *)> mMainLoop; std::unique_ptr<GMainLoop, void (*)(GMainLoop *)> mMainLoop;
std::thread mMainLoopThread; std::thread mMainLoopThread;
guint mTimeoutId = 0; guint mTimeoutId = 0;
std::mutex mOutgoingMutex;
unsigned int mOutgoingDscp;
static string AddressToString(const NiceAddress &addr); static string AddressToString(const NiceAddress &addr);

View File

@ -189,13 +189,14 @@ void PeerConnection::setLocalDescription(Description::Type type) {
auto iceTransport = initIceTransport(); auto iceTransport = initIceTransport();
Description localDescription = iceTransport->getLocalDescription(type); Description local = iceTransport->getLocalDescription(type);
processLocalDescription(std::move(localDescription)); processLocalDescription(std::move(local));
changeSignalingState(newSignalingState); changeSignalingState(newSignalingState);
if (mGatheringState == GatheringState::New) if (mGatheringState == GatheringState::New) {
iceTransport->gatherLocalCandidates(); iceTransport->gatherLocalCandidates(localBundleMid());
}
} }
void PeerConnection::setRemoteDescription(Description description) { void PeerConnection::setRemoteDescription(Description description) {
@ -369,7 +370,7 @@ void PeerConnection::onSignalingStateChange(std::function<void(SignalingState st
std::shared_ptr<Track> PeerConnection::addTrack(Description::Media description) { std::shared_ptr<Track> PeerConnection::addTrack(Description::Media description) {
#if !RTC_ENABLE_MEDIA #if !RTC_ENABLE_MEDIA
if (mTracks.empty()) { if (mTracks.empty()) {
PLOG_WARNING << "Tracks will be inative (not compiled with SRTP support)"; PLOG_WARNING << "Tracks will be inative (not compiled with media support)";
} }
#endif #endif
@ -503,7 +504,7 @@ shared_ptr<DtlsTransport> PeerConnection::initDtlsTransport() {
lower, certificate, verifierCallback, lower, certificate, verifierCallback,
std::bind(&PeerConnection::forwardMedia, this, _1), stateChangeCallback); std::bind(&PeerConnection::forwardMedia, this, _1), stateChangeCallback);
#else #else
PLOG_WARNING << "Ignoring media support (not compiled with SRTP support)"; PLOG_WARNING << "Ignoring media support (not compiled with media support)";
#endif #endif
} }
@ -648,10 +649,10 @@ void PeerConnection::forwardMessage(message_ptr message) {
stream % 2 == remoteParity) { stream % 2 == remoteParity) {
channel = std::make_shared<NegociatedDataChannel>(shared_from_this(), sctpTransport, channel = std::make_shared<NegociatedDataChannel>(shared_from_this(), sctpTransport,
message->stream); stream);
channel->onOpen(weak_bind(&PeerConnection::triggerDataChannel, this, channel->onOpen(weak_bind(&PeerConnection::triggerDataChannel, this,
weak_ptr<DataChannel>{channel})); weak_ptr<DataChannel>{channel}));
mDataChannels.emplace(message->stream, channel); mDataChannels.emplace(stream, channel);
} else { } else {
// Invalid, close the DataChannel // Invalid, close the DataChannel
sctpTransport->closeStream(message->stream); sctpTransport->closeStream(message->stream);
@ -872,7 +873,7 @@ void PeerConnection::incomingTrack(Description::Media description) {
std::unique_lock lock(mTracksMutex); // we are going to emplace std::unique_lock lock(mTracksMutex); // we are going to emplace
#if !RTC_ENABLE_MEDIA #if !RTC_ENABLE_MEDIA
if (mTracks.empty()) { if (mTracks.empty()) {
PLOG_WARNING << "Tracks will be inative (not compiled with SRTP support)"; PLOG_WARNING << "Tracks will be inative (not compiled with media support)";
} }
#endif #endif
if (mTracks.find(description.mid()) == mTracks.end()) { if (mTracks.find(description.mid()) == mTracks.end()) {
@ -1013,7 +1014,10 @@ void PeerConnection::processLocalDescription(Description description) {
if (!description.hasApplication()) { if (!description.hasApplication()) {
std::shared_lock lock(mDataChannelsMutex); std::shared_lock lock(mDataChannelsMutex);
if (!mDataChannels.empty()) { if (!mDataChannels.empty()) {
Description::Application app("data"); unsigned int m = 0;
while (description.hasMid(std::to_string(m)))
++m;
Description::Application app(std::to_string(m));
app.setSctpPort(DEFAULT_SCTP_PORT); app.setSctpPort(DEFAULT_SCTP_PORT);
app.setMaxMessageSize(LOCAL_MAX_MESSAGE_SIZE); app.setMaxMessageSize(LOCAL_MAX_MESSAGE_SIZE);
@ -1133,7 +1137,7 @@ void PeerConnection::processRemoteCandidate(Candidate candidate) {
} else { } else {
// We might need a lookup, do it asynchronously // We might need a lookup, do it asynchronously
// We don't use the thread pool because we have no control on the timeout // We don't use the thread pool because we have no control on the timeout
if (auto iceTransport = std::atomic_load(&mIceTransport)) { if ((iceTransport = std::atomic_load(&mIceTransport))) {
weak_ptr<IceTransport> weakIceTransport{iceTransport}; weak_ptr<IceTransport> weakIceTransport{iceTransport};
std::thread t([weakIceTransport, candidate = std::move(candidate)]() mutable { std::thread t([weakIceTransport, candidate = std::move(candidate)]() mutable {
if (candidate.resolve(Candidate::ResolveMode::Lookup)) if (candidate.resolve(Candidate::ResolveMode::Lookup))
@ -1145,6 +1149,11 @@ void PeerConnection::processRemoteCandidate(Candidate candidate) {
} }
} }
string PeerConnection::localBundleMid() const {
std::lock_guard lock(mLocalDescriptionMutex);
return mLocalDescription ? mLocalDescription->bundleMid() : "0";
}
void PeerConnection::triggerDataChannel(weak_ptr<DataChannel> weakDataChannel) { void PeerConnection::triggerDataChannel(weak_ptr<DataChannel> weakDataChannel) {
auto dataChannel = weakDataChannel.lock(); auto dataChannel = weakDataChannel.lock();
if (!dataChannel) if (!dataChannel)

View File

@ -126,7 +126,7 @@ bool RtcpReceivingSession::requestKeyframe() {
void RtcpReceivingSession::pushPLI() { void RtcpReceivingSession::pushPLI() {
auto msg = rtc::make_message(rtc::RTCP_PLI::size(), rtc::Message::Type::Control); auto msg = rtc::make_message(rtc::RTCP_PLI::size(), rtc::Message::Type::Control);
auto *pli = (rtc::RTCP_PLI *)msg->data(); auto *pli = reinterpret_cast<rtc::RTCP_PLI *>(msg->data());
pli->preparePacket(mSsrc); pli->preparePacket(mSsrc);
send(msg); send(msg);
} }

View File

@ -325,6 +325,13 @@ void SctpTransport::incoming(message_ptr message) {
usrsctp_conninput(this, message->data(), message->size(), 0); usrsctp_conninput(this, message->data(), message->size(), 0);
} }
bool SctpTransport::outgoing(message_ptr message) {
// Set recommended medium-priority DSCP value
// See https://tools.ietf.org/html/draft-ietf-tsvwg-rtcweb-qos-18
message->dscp = 10; // AF11: Assured Forwarding class 1, low drop probability
return Transport::outgoing(std::move(message));
}
void SctpTransport::doRecv() { void SctpTransport::doRecv() {
std::lock_guard lock(mRecvMutex); std::lock_guard lock(mRecvMutex);
--mPendingRecvCount; --mPendingRecvCount;
@ -554,6 +561,7 @@ int SctpTransport::handleWrite(byte *data, size_t len, uint8_t /*tos*/, uint8_t
std::unique_lock lock(mWriteMutex); std::unique_lock lock(mWriteMutex);
PLOG_VERBOSE << "Handle write, len=" << len; PLOG_VERBOSE << "Handle write, len=" << len;
auto message = make_message(data, data + len);
if (!outgoing(make_message(data, data + len))) if (!outgoing(make_message(data, data + len)))
return -1; return -1;

View File

@ -77,6 +77,7 @@ private:
void shutdown(); void shutdown();
void close(); void close();
void incoming(message_ptr message) override; void incoming(message_ptr message) override;
bool outgoing(message_ptr message) override;
void doRecv(); void doRecv();
bool trySendQueue(); bool trySendQueue();

View File

@ -18,15 +18,26 @@
#include "threadpool.hpp" #include "threadpool.hpp"
#include <cstdlib>
namespace {
void joinThreadPoolInstance() {
rtc::ThreadPool::Instance().join();
}
}
namespace rtc { namespace rtc {
ThreadPool &ThreadPool::Instance() { ThreadPool &ThreadPool::Instance() {
// Init handles joining on cleanup
static ThreadPool *instance = new ThreadPool; static ThreadPool *instance = new ThreadPool;
return *instance; return *instance;
} }
ThreadPool::~ThreadPool() { join(); } ThreadPool::ThreadPool() {
std::atexit(joinThreadPoolInstance);
}
ThreadPool::~ThreadPool() {}
int ThreadPool::count() const { int ThreadPool::count() const {
std::unique_lock lock(mWorkersMutex); std::unique_lock lock(mWorkersMutex);

View File

@ -56,7 +56,7 @@ public:
auto enqueue(F &&f, Args &&... args) -> invoke_future_t<F, Args...>; auto enqueue(F &&f, Args &&... args) -> invoke_future_t<F, Args...>;
protected: protected:
ThreadPool() = default; ThreadPool();
~ThreadPool(); ~ThreadPool();
std::function<void()> dequeue(); // returns null function if joining std::function<void()> dequeue(); // returns null function if joining

View File

@ -45,12 +45,30 @@ void Track::close() {
setRtcpHandler(nullptr); setRtcpHandler(nullptr);
} }
bool Track::send(message_variant data) { return outgoing(make_message(std::move(data))); } bool Track::send(message_variant data) {
if (mIsClosed)
throw std::runtime_error("Track is closed");
bool Track::send(const byte *data, size_t size) { auto direction = mMediaDescription.direction();
return outgoing(std::make_shared<Message>(data, data + size, Message::Binary)); if ((direction == Description::Direction::RecvOnly ||
direction == Description::Direction::Inactive)) {
PLOG_WARNING << "Track media direction does not allow transmission, dropping";
return false;
}
auto message = make_message(std::move(data));
if (mRtcpHandler) {
message = mRtcpHandler->outgoing(message);
if (!message)
return false;
}
return outgoing(std::move(message));
} }
bool Track::send(const byte *data, size_t size) { return send(binary(data, data + size)); }
std::optional<message_variant> Track::receive() { std::optional<message_variant> Track::receive() {
if (auto next = mRecvQueue.tryPop()) if (auto next = mRecvQueue.tryPop())
return to_variant(std::move(**next)); return to_variant(std::move(**next));
@ -88,50 +106,10 @@ void Track::open(shared_ptr<DtlsSrtpTransport> transport) {
} }
#endif #endif
bool Track::outgoing(message_ptr message) {
if (mRtcpHandler) {
message = mRtcpHandler->outgoing(message);
if (!message)
return false;
}
auto direction = mMediaDescription.direction();
if ((direction == Description::Direction::RecvOnly ||
direction == Description::Direction::Inactive) &&
message->type != Message::Control) {
PLOG_WARNING << "Track media direction does not allow transmission, dropping";
return false;
}
if (mIsClosed)
throw std::runtime_error("Track is closed");
if (message->size() > maxMessageSize())
throw std::runtime_error("Message size exceeds limit");
#if RTC_ENABLE_MEDIA
auto transport = mDtlsSrtpTransport.lock();
if (!transport)
throw std::runtime_error("Track transport is not open");
return transport->sendMedia(message);
#else
PLOG_WARNING << "Ignoring track send (not compiled with SRTP support)";
return false;
#endif
}
void Track::incoming(message_ptr message) { void Track::incoming(message_ptr message) {
if (!message) if (!message)
return; return;
if (mRtcpHandler) {
message = mRtcpHandler->incoming(message);
if (!message)
return;
}
auto direction = mMediaDescription.direction(); auto direction = mMediaDescription.direction();
if ((direction == Description::Direction::SendOnly || if ((direction == Description::Direction::SendOnly ||
direction == Description::Direction::Inactive) && direction == Description::Direction::Inactive) &&
@ -140,30 +118,46 @@ void Track::incoming(message_ptr message) {
return; return;
} }
if (mRtcpHandler) {
message = mRtcpHandler->incoming(message);
if (!message)
return;
}
// Tail drop if queue is full // Tail drop if queue is full
if (mRecvQueue.full()) if (mRecvQueue.full()) {
PLOG_WARNING << "Track incoming queue is full, dropping";
return; return;
}
mRecvQueue.push(message); mRecvQueue.push(message);
triggerAvailable(mRecvQueue.size()); triggerAvailable(mRecvQueue.size());
} }
bool Track::outgoing([[maybe_unused]] message_ptr message) {
#if RTC_ENABLE_MEDIA
auto transport = mDtlsSrtpTransport.lock();
if (!transport)
throw std::runtime_error("Track transport is not open");
// Set recommended medium-priority DSCP value
// See https://tools.ietf.org/html/draft-ietf-tsvwg-rtcweb-qos-18
if (mMediaDescription.type() == "audio")
message->dscp = 46; // EF: Expedited Forwarding
else
message->dscp = 36; // AF42: Assured Forwarding class 4, medium drop probability
return transport->sendMedia(message);
#else
PLOG_WARNING << "Ignoring track send (not compiled with media support)";
return false;
#endif
}
void Track::setRtcpHandler(std::shared_ptr<RtcpHandler> handler) { void Track::setRtcpHandler(std::shared_ptr<RtcpHandler> handler) {
mRtcpHandler = std::move(handler); mRtcpHandler = std::move(handler);
if (mRtcpHandler) { if (mRtcpHandler)
mRtcpHandler->onOutgoing([&]([[maybe_unused]] const rtc::message_ptr &message) { mRtcpHandler->onOutgoing(std::bind(&Track::outgoing, this, std::placeholders::_1));
#if RTC_ENABLE_MEDIA
auto transport = mDtlsSrtpTransport.lock();
if (!transport)
throw std::runtime_error("Track transport is not open");
return transport->sendMedia(message);
#else
PLOG_WARNING << "Ignoring track send (not compiled with SRTP support)";
return false;
#endif
});
}
} }
bool Track::requestKeyframe() { bool Track::requestKeyframe() {

View File

@ -28,8 +28,6 @@
namespace rtc { namespace rtc {
using namespace std::placeholders;
class Transport { class Transport {
public: public:
enum class State { Disconnected, Connecting, Connected, Completed, Failed }; enum class State { Disconnected, Connecting, Connected, Completed, Failed };
@ -57,7 +55,7 @@ public:
void registerIncoming() { void registerIncoming() {
if (mLower) { if (mLower) {
PLOG_VERBOSE << "Registering incoming callback"; PLOG_VERBOSE << "Registering incoming callback";
mLower->onRecv(std::bind(&Transport::incoming, this, _1)); mLower->onRecv(std::bind(&Transport::incoming, this, std::placeholders::_1));
} }
} }

View File

@ -36,6 +36,7 @@
namespace rtc { namespace rtc {
using std::shared_ptr; using std::shared_ptr;
using namespace std::placeholders;
WebSocket::WebSocket(std::optional<Configuration> config) WebSocket::WebSocket(std::optional<Configuration> config)
: mConfig(config ? std::move(*config) : Configuration()), : mConfig(config ? std::move(*config) : Configuration()),
@ -117,6 +118,10 @@ void WebSocket::remoteClose() {
bool WebSocket::send(message_variant data) { return outgoing(make_message(std::move(data))); } bool WebSocket::send(message_variant data) { return outgoing(make_message(std::move(data))); }
bool WebSocket::send(const byte *data, size_t size) {
return outgoing(make_message(data, data + size));
}
bool WebSocket::isOpen() const { return mState == State::Open; } bool WebSocket::isOpen() const { return mState == State::Open; }
bool WebSocket::isClosed() const { return mState == State::Closed; } bool WebSocket::isClosed() const { return mState == State::Closed; }
@ -286,8 +291,14 @@ shared_ptr<WsTransport> WebSocket::initWsTransport() {
shared_ptr<Transport> lower = std::atomic_load(&mTlsTransport); shared_ptr<Transport> lower = std::atomic_load(&mTlsTransport);
if (!lower) if (!lower)
lower = std::atomic_load(&mTcpTransport); lower = std::atomic_load(&mTcpTransport);
WsTransport::Configuration wsConfig = {};
wsConfig.host = mHost;
wsConfig.path = mPath;
wsConfig.protocols = mConfig.protocols;
auto transport = std::make_shared<WsTransport>( auto transport = std::make_shared<WsTransport>(
lower, mHost, mPath, weak_bind(&WebSocket::incoming, this, _1), lower, wsConfig, weak_bind(&WebSocket::incoming, this, _1),
[this, weak_this = weak_from_this()](State state) { [this, weak_this = weak_from_this()](State state) {
auto shared_this = weak_this.lock(); auto shared_this = weak_this.lock();
if (!shared_this) if (!shared_this)

View File

@ -24,8 +24,10 @@
#if RTC_ENABLE_WEBSOCKET #if RTC_ENABLE_WEBSOCKET
#include <chrono> #include <chrono>
#include <iterator>
#include <list> #include <list>
#include <map> #include <map>
#include <numeric>
#include <random> #include <random>
#include <regex> #include <regex>
@ -52,17 +54,17 @@ using std::to_string;
using random_bytes_engine = using random_bytes_engine =
std::independent_bits_engine<std::default_random_engine, CHAR_BIT, unsigned short>; std::independent_bits_engine<std::default_random_engine, CHAR_BIT, unsigned short>;
WsTransport::WsTransport(std::shared_ptr<Transport> lower, string host, string path, WsTransport::WsTransport(std::shared_ptr<Transport> lower, Configuration config,
message_callback recvCallback, state_callback stateCallback) message_callback recvCallback, state_callback stateCallback)
: Transport(lower, std::move(stateCallback)), mHost(std::move(host)), mPath(std::move(path)) { : Transport(lower, std::move(stateCallback)), mConfig(std::move(config)) {
onRecv(recvCallback); onRecv(recvCallback);
PLOG_DEBUG << "Initializing WebSocket transport"; PLOG_DEBUG << "Initializing WebSocket transport";
if (mHost.empty()) if (mConfig.host.empty())
throw std::invalid_argument("WebSocket HTTP host cannot be empty"); throw std::invalid_argument("WebSocket HTTP host cannot be empty");
if (mPath.empty()) if (mConfig.path.empty())
throw std::invalid_argument("WebSocket HTTP path cannot be empty"); throw std::invalid_argument("WebSocket HTTP path cannot be empty");
} }
@ -153,7 +155,7 @@ void WsTransport::close() {
} }
bool WsTransport::sendHttpRequest() { bool WsTransport::sendHttpRequest() {
PLOG_DEBUG << "Sending WebSocket HTTP request for path " << mPath; PLOG_DEBUG << "Sending WebSocket HTTP request for path " << mConfig.path;
changeState(State::Connecting); changeState(State::Connecting);
auto seed = static_cast<unsigned int>(system_clock::now().time_since_epoch().count()); auto seed = static_cast<unsigned int>(system_clock::now().time_since_epoch().count());
@ -163,18 +165,27 @@ bool WsTransport::sendHttpRequest() {
auto k = reinterpret_cast<uint8_t *>(key.data()); auto k = reinterpret_cast<uint8_t *>(key.data());
std::generate(k, k + key.size(), [&]() { return uint8_t(generator()); }); std::generate(k, k + key.size(), [&]() { return uint8_t(generator()); });
const string request = "GET " + mPath + string appendHeader = "";
if (mConfig.protocols.size() > 0) {
appendHeader +=
"Sec-WebSocket-Protocol: " +
std::accumulate(mConfig.protocols.begin(), mConfig.protocols.end(), string(),
[](const string &a, const string &b) -> string {
return a + (a.length() > 0 ? "," : "") + b;
}) +
"\r\n";
}
const string request = "GET " + mConfig.path +
" HTTP/1.1\r\n" " HTTP/1.1\r\n"
"Host: " + "Host: " +
mHost + mConfig.host +
"\r\n" "\r\n"
"Connection: Upgrade\r\n" "Connection: Upgrade\r\n"
"Upgrade: websocket\r\n" "Upgrade: websocket\r\n"
"Sec-WebSocket-Version: 13\r\n" "Sec-WebSocket-Version: 13\r\n"
"Sec-WebSocket-Key: " + "Sec-WebSocket-Key: " +
to_base64(key) + to_base64(key) + "\r\n" + std::move(appendHeader) + "\r\n";
"\r\n"
"\r\n";
auto data = reinterpret_cast<const byte *>(request.data()); auto data = reinterpret_cast<const byte *>(request.data());
auto size = request.size(); auto size = request.size();
@ -227,8 +238,14 @@ size_t WsTransport::readHttpResponse(const byte *buffer, size_t size) {
} }
auto h = headers.find("upgrade"); auto h = headers.find("upgrade");
if (h == headers.end() || h->second != "websocket") if (h == headers.end())
throw std::runtime_error("WebSocket update header missing or mismatching"); throw std::runtime_error("WebSocket update header missing");
string upgrade;
std::transform(h->second.begin(), h->second.end(), std::back_inserter(upgrade),
[](char c) { return std::tolower(c); });
if (upgrade != "websocket")
throw std::runtime_error("WebSocket update header mismatching: " + h->second);
h = headers.find("sec-websocket-accept"); h = headers.find("sec-websocket-accept");
if (h == headers.end()) if (h == headers.end())

View File

@ -31,7 +31,13 @@ class TlsTransport;
class WsTransport : public Transport { class WsTransport : public Transport {
public: public:
WsTransport(std::shared_ptr<Transport> lower, string host, string path, struct Configuration {
string host;
string path = "/";
std::vector<string> protocols;
};
WsTransport(std::shared_ptr<Transport> lower, Configuration config,
message_callback recvCallback, state_callback stateCallback); message_callback recvCallback, state_callback stateCallback);
~WsTransport(); ~WsTransport();
@ -68,12 +74,12 @@ private:
void recvFrame(const Frame &frame); void recvFrame(const Frame &frame);
bool sendFrame(const Frame &frame); bool sendFrame(const Frame &frame);
const string mHost; const Configuration mConfig;
const string mPath;
binary mBuffer; binary mBuffer;
binary mPartial; binary mPartial;
Opcode mPartialOpcode; Opcode mPartialOpcode;
}; };
} // namespace rtc } // namespace rtc

View File

@ -161,6 +161,7 @@ static Peer *createPeer(const rtcConfiguration *config) {
rtcSetLocalCandidateCallback(peer->pc, candidateCallback); rtcSetLocalCandidateCallback(peer->pc, candidateCallback);
rtcSetStateChangeCallback(peer->pc, stateChangeCallback); rtcSetStateChangeCallback(peer->pc, stateChangeCallback);
rtcSetGatheringStateChangeCallback(peer->pc, gatheringStateCallback); rtcSetGatheringStateChangeCallback(peer->pc, gatheringStateCallback);
rtcSetSignalingStateChangeCallback(peer->pc, signalingStateCallback);
return peer; return peer;
} }
@ -226,12 +227,6 @@ int test_capi_connectivity_main() {
goto error; goto error;
} }
if (peer1->signalingState != RTC_SIGNALING_STABLE ||
peer2->signalingState != RTC_SIGNALING_STABLE) {
fprintf(stderr, "Signaling state is not stable\n");
goto error;
}
if (!peer1->connected || !peer2->connected) { if (!peer1->connected || !peer2->connected) {
fprintf(stderr, "DataChannel is not connected\n"); fprintf(stderr, "DataChannel is not connected\n");
goto error; goto error;