Kaynağa Gözat

Merge pull request #3 from paullouisageneau/master

Update to tip.
Erik Cota-Robles 4 yıl önce
ebeveyn
işleme
cdde6ad36e
48 değiştirilmiş dosya ile 2725 ekleme ve 535 silme
  1. 2 0
      .github/FUNDING.yml
  2. 1 1
      .github/workflows/build-openssl.yml
  3. 16 7
      CMakeLists.txt
  4. 11 8
      README.md
  5. 1 1
      deps/libjuice
  6. 2 0
      examples/README.md
  7. 3 3
      examples/client/main.cpp
  8. 3 3
      examples/copy-paste/answerer.cpp
  9. 3 3
      examples/copy-paste/offerer.cpp
  10. 14 0
      examples/media/CMakeLists.txt
  11. 19 0
      examples/media/README.md
  12. 97 0
      examples/media/main.cpp
  13. 45 0
      examples/media/main.html
  14. 29 12
      examples/web/package-lock.json
  15. 1 1
      examples/web/package.json
  16. 10 9
      include/rtc/channel.hpp
  17. 2 2
      include/rtc/datachannel.hpp
  18. 146 28
      include/rtc/description.hpp
  19. 4 0
      include/rtc/include.hpp
  20. 14 18
      include/rtc/message.hpp
  21. 29 21
      include/rtc/peerconnection.hpp
  22. 6 0
      include/rtc/queue.hpp
  23. 17 8
      include/rtc/rtc.h
  24. 59 0
      include/rtc/rtcp.hpp
  25. 82 0
      include/rtc/track.hpp
  26. 2 2
      include/rtc/websocket.hpp
  27. 168 56
      src/capi.cpp
  28. 7 9
      src/channel.cpp
  29. 10 29
      src/datachannel.cpp
  30. 543 116
      src/description.cpp
  31. 21 9
      src/dtlssrtptransport.cpp
  32. 0 1
      src/dtlstransport.cpp
  33. 8 6
      src/icetransport.cpp
  34. 60 0
      src/message.cpp
  35. 252 85
      src/peerconnection.cpp
  36. 426 0
      src/rtcp.cpp
  37. 25 2
      src/sctptransport.cpp
  38. 31 25
      src/tcptransport.cpp
  39. 1 2
      src/tls.cpp
  40. 148 0
      src/track.cpp
  41. 7 23
      src/websocket.cpp
  42. 22 23
      test/benchmark.cpp
  43. 6 6
      test/capi_connectivity.cpp
  44. 198 0
      test/capi_track.cpp
  45. 13 9
      test/connectivity.cpp
  46. 25 5
      test/main.cpp
  47. 134 0
      test/track.cpp
  48. 2 2
      test/websocket.cpp

+ 2 - 0
.github/FUNDING.yml

@@ -1 +1,3 @@
+github: ['paullouisageneau']
 custom: ['https://paypal.me/paullouisageneau']
+

+ 1 - 1
.github/workflows/build-openssl.yml

@@ -26,7 +26,7 @@ jobs:
     steps:
     - uses: actions/checkout@v2
     - name: install packages
-      run: brew reinstall [email protected]
+      run: HOMEBREW_NO_INSTALL_CLEANUP=1 brew reinstall [email protected]
     - name: submodules
       run: git submodule update --init --recursive
     - name: cmake

+ 16 - 7
CMakeLists.txt

@@ -1,13 +1,12 @@
 cmake_minimum_required(VERSION 3.7)
 project(libdatachannel
 	DESCRIPTION "WebRTC Data Channels Library"
-	VERSION 0.8.0
+	VERSION 0.9.0
 	LANGUAGES CXX)
 
 # Options
 option(USE_GNUTLS "Use GnuTLS instead of OpenSSL" OFF)
 option(USE_NICE "Use libnice instead of libjuice" OFF)
-option(USE_SRTP "Enable SRTP for media support" OFF)
 option(NO_WEBSOCKET "Disable WebSocket support" OFF)
 option(NO_EXAMPLES "Disable examples" OFF)
 option(NO_TESTS "Disable tests build" OFF)
@@ -49,12 +48,15 @@ set(LIBDATACHANNEL_SOURCES
 	${CMAKE_CURRENT_SOURCE_DIR}/src/icetransport.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/init.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/log.cpp
+	${CMAKE_CURRENT_SOURCE_DIR}/src/message.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/peerconnection.cpp
-	${CMAKE_CURRENT_SOURCE_DIR}/src/rtc.cpp
+	${CMAKE_CURRENT_SOURCE_DIR}/src/rtcp.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/sctptransport.cpp
-	${CMAKE_CURRENT_SOURCE_DIR}/src/tls.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/threadpool.cpp
+	${CMAKE_CURRENT_SOURCE_DIR}/src/tls.cpp
+	${CMAKE_CURRENT_SOURCE_DIR}/src/track.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/processor.cpp
+	${CMAKE_CURRENT_SOURCE_DIR}/src/capi.cpp
 )
 
 set(LIBDATACHANNEL_WEBSOCKET_SOURCES
@@ -73,6 +75,7 @@ set(LIBDATACHANNEL_HEADERS
 	${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/configuration.hpp
 	${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/datachannel.hpp
 	${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/description.hpp
+	${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtcp.hpp
 	${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/include.hpp
 	${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/init.hpp
 	${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/log.hpp
@@ -82,13 +85,16 @@ set(LIBDATACHANNEL_HEADERS
 	${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/reliability.hpp
 	${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtc.h
 	${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtc.hpp
+	${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/track.hpp
 	${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/websocket.hpp
 )
 
 set(TESTS_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/test/main.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test/connectivity.cpp
-    ${CMAKE_CURRENT_SOURCE_DIR}/test/capi.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/test/track.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/test/capi_connectivity.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/test/capi_track.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test/websocket.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/test/benchmark.cpp
 )
@@ -155,8 +161,8 @@ if(WIN32)
 	target_link_libraries(datachannel-static PRIVATE wsock32 ws2_32) # winsock2
 endif()
 
-if(USE_SRTP)
-	find_package(SRTP REQUIRED)
+find_package(SRTP)
+if(SRTP_FOUND)
 	if(NOT TARGET SRTP::SRTP)
 		add_library(SRTP::SRTP UNKNOWN IMPORTED)
 		set_target_properties(SRTP::SRTP PROPERTIES
@@ -164,11 +170,13 @@ if(USE_SRTP)
 			IMPORTED_LINK_INTERFACE_LANGUAGES C
 			IMPORTED_LOCATION ${SRTP_LIBRARIES})
 	endif()
+	message(STATUS "LibSRTP found, compiling with media transport")
 	target_compile_definitions(datachannel PUBLIC RTC_ENABLE_MEDIA=1)
 	target_compile_definitions(datachannel-static PUBLIC RTC_ENABLE_MEDIA=1)
 	target_link_libraries(datachannel PRIVATE SRTP::SRTP)
 	target_link_libraries(datachannel-static PRIVATE SRTP::SRTP)
 else()
+	message(STATUS "LibSRTP NOT found, compiling WITHOUT media transport")
 	target_compile_definitions(datachannel PUBLIC RTC_ENABLE_MEDIA=0)
 	target_compile_definitions(datachannel-static PUBLIC RTC_ENABLE_MEDIA=0)
 endif()
@@ -264,6 +272,7 @@ if(NOT NO_EXAMPLES)
 	set(JSON_BuildTests OFF CACHE INTERNAL "")
 	add_subdirectory(deps/json)
 	add_subdirectory(examples/client)
+	add_subdirectory(examples/media)
 	add_subdirectory(examples/copy-paste)
 	add_subdirectory(examples/copy-paste-capi)
 endif()

+ 11 - 8
README.md

@@ -1,6 +1,6 @@
 # libdatachannel - C/C++ WebRTC Data Channels
 
-libdatachannel is a standalone implementation of WebRTC Data Channels and WebSockets in C++17 with C bindings for POSIX platforms (including GNU/Linux, Android, and Apple macOS) and Microsoft Windows. It enables direct connectivity between native applications and web browsers without the pain of importing the entire WebRTC stack. The interface consists of simplified versions of the JavaScript WebRTC and WebSocket APIs present in browsers, in order to ease the design of cross-environment applications.
+libdatachannel is a standalone implementation of WebRTC Data Channels, WebRTC Media Transport, and WebSockets in C++17 with C bindings for POSIX platforms (including GNU/Linux, Android, and Apple macOS) and Microsoft Windows. It enables direct connectivity between native applications and web browsers without the pain of importing the entire WebRTC stack. The interface consists of simplified versions of the JavaScript WebRTC and WebSocket APIs present in browsers, in order to ease the design of cross-environment applications.
 It can be compiled with multiple backends:
 - The security layer can be provided through [OpenSSL](https://www.openssl.org/) or [GnuTLS](https://www.gnutls.org/).
 - The connectivity for WebRTC can be provided through my ad-hoc ICE library [libjuice](https://github.com/paullouisageneau/libjuice) as submodule or through [libnice](https://github.com/libnice/libnice).
@@ -13,12 +13,13 @@ Licensed under LGPLv2, see [LICENSE](https://github.com/paullouisageneau/libdata
 
 The library aims at implementing the following communication protocols:
 
-### WebRTC Data Channel
+### WebRTC Data Channels and Media Transport
 
 The WebRTC stack has been tested to be compatible with Firefox and Chromium.
 
 Protocol stack:
 - SCTP-based Data Channels ([draft-ietf-rtcweb-data-channel-13](https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13))
+- SRTP-based Media Transport ([draft-ietf-rtcweb-rtp-usage-26](https://tools.ietf.org/html/draft-ietf-rtcweb-rtp-usage-26)) with [libSRTP](https://github.com/cisco/libsrtp)
 - DTLS/UDP ([RFC7350](https://tools.ietf.org/html/rfc7350) and [RFC8261](https://tools.ietf.org/html/rfc8261))
 - ICE ([RFC8445](https://tools.ietf.org/html/rfc8445)) with STUN ([RFC5389](https://tools.ietf.org/html/rfc5389))
 
@@ -26,9 +27,11 @@ Features:
 - Full IPv6 support
 - 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))
+- 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))
 - TURN relaying ([RFC5766](https://tools.ietf.org/html/rfc5766)) with [libnice](https://github.com/libnice/libnice) as ICE backend
-- SRTP media transport ([RFC3711](https://tools.ietf.org/html/rfc3711)) with [libSRTP](https://github.com/cisco/libsrtp)
+
+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)).
 
 ### WebSocket
 
@@ -104,7 +107,7 @@ $ make USE_GNUTLS=1 USE_NICE=0
 
 See [examples](https://github.com/paullouisageneau/libdatachannel/blob/master/examples/) for a complete usage example with signaling server (under GPLv2).
 
-Additionnaly, you might want to have a look at the [C API](https://github.com/paullouisageneau/libdatachannel/blob/dev/include/rtc/rtc.h).
+Additionnaly, you might want to have a look at the [C API](https://github.com/paullouisageneau/libdatachannel/blob/master/include/rtc/rtc.h).
 
 ### Signal a PeerConnection
 
@@ -118,12 +121,12 @@ config.iceServers.emplace_back("mystunserver.org:3478");
 
 auto pc = make_shared<rtc::PeerConnection>(config);
 
-pc->onLocalDescription([](const rtc::Description &sdp) {
+pc->onLocalDescription([](rtc::Description sdp) {
     // Send the SDP to the remote peer
     MY_SEND_DESCRIPTION_TO_REMOTE(string(sdp));
 });
 
-pc->onLocalCandidate([](const rtc::Candidate &candidate) {
+pc->onLocalCandidate([](rtc::Candidate candidate) {
     // Send the candidate to the remote peer
     MY_SEND_CANDIDATE_TO_REMOTE(candidate.candidate(), candidate.mid());
 });
@@ -159,7 +162,7 @@ dc->onOpen([]() {
     cout << "Open" << endl;
 });
 
-dc->onMessage([](const variant<binary, string> &message) {
+dc->onMessage([](variant<binary, string> message) {
     if (holds_alternative<string>(message)) {
         cout << "Received: " << get<string>(message) << endl;
     }
@@ -186,7 +189,7 @@ ws->onOpen([]() {
 	cout << "WebSocket open" << endl;
 });
 
-ws->onMessage([](const variant<binary, string> &message) {
+ws->onMessage([](variant<binary, string> message) {
     if (holds_alternative<string>(message)) {
         cout << "WebSocket received: " << get<string>(message) << endl;
     }

+ 1 - 1
deps/libjuice

@@ -1 +1 @@
-Subproject commit 0234e89d83d121103aaa8d2cf7183dc0f580d86c
+Subproject commit ddc30648908181f6e6222a2fd648d35c2c28b724

+ 2 - 0
examples/README.md

@@ -7,6 +7,8 @@ This directory contains different WebRTC clients and compatible WebSocket + JSON
 - [signaling-server-python](signaling-server-python) contains a similar signaling server in Python
 - [signaling-server-rust](signaling-server-rust) contains a similar signaling server in Rust (see [lerouxrgd/datachannel-rs](https://github.com/lerouxrgd/datachannel-rs) for Rust wrappers)
 
+- [media](media) is a copy/paste demo to send the webcam from your browser into gstreamer.
+
 Additionally, it contains two debugging tools for libdatachannel with copy-pasting as signaling:
 - [copy-paste](copy-paste) using the C++ API
 - [copy-paste-capi](copy-paste-capi) using the C API

+ 3 - 3
examples/client/main.cpp

@@ -65,7 +65,7 @@ int main(int argc, char **argv) {
 
 	ws->onError([](const string &error) { cout << "WebSocket failed: " << error << endl; });
 
-	ws->onMessage([&](const variant<binary, string> &data) {
+	ws->onMessage([&](variant<binary, string> data) {
 		if (!holds_alternative<string>(data))
 			return;
 
@@ -166,7 +166,7 @@ shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
 	pc->onGatheringStateChange(
 	    [](PeerConnection::GatheringState state) { cout << "Gathering State: " << state << endl; });
 
-	pc->onLocalDescription([wws, id](const Description &description) {
+	pc->onLocalDescription([wws, id](Description description) {
 		json message = {
 		    {"id", id}, {"type", description.typeString()}, {"description", string(description)}};
 
@@ -174,7 +174,7 @@ shared_ptr<PeerConnection> createPeerConnection(const Configuration &config,
 			ws->send(message.dump());
 	});
 
-	pc->onLocalCandidate([wws, id](const Candidate &candidate) {
+	pc->onLocalCandidate([wws, id](Candidate candidate) {
 		json message = {{"id", id},
 		                {"type", "candidate"},
 		                {"candidate", string(candidate)},

+ 3 - 3
examples/copy-paste/answerer.cpp

@@ -36,12 +36,12 @@ int main(int argc, char **argv) {
 
 	auto pc = std::make_shared<PeerConnection>(config);
 
-	pc->onLocalDescription([](const Description &description) {
+	pc->onLocalDescription([](Description description) {
 		cout << "Local Description (Paste this to the other peer):" << endl;
 		cout << string(description) << endl;
 	});
 
-	pc->onLocalCandidate([](const Candidate &candidate) {
+	pc->onLocalCandidate([](Candidate candidate) {
 		cout << "Local Candidate (Paste this to the other peer after the local description):"
 		     << endl;
 		cout << string(candidate) << endl << endl;
@@ -60,7 +60,7 @@ int main(int argc, char **argv) {
 
 		dc->onClosed([&]() { cout << "[DataChannel closed: " << dc->label() << "]" << endl; });
 
-		dc->onMessage([](const variant<binary, string> &message) {
+		dc->onMessage([](variant<binary, string> message) {
 			if (holds_alternative<string>(message)) {
 				cout << "[Received message: " << get<string>(message) << "]" << endl;
 			}

+ 3 - 3
examples/copy-paste/offerer.cpp

@@ -36,12 +36,12 @@ int main(int argc, char **argv) {
 
 	auto pc = std::make_shared<PeerConnection>(config);
 
-	pc->onLocalDescription([](const Description &description) {
+	pc->onLocalDescription([](Description description) {
 		cout << "Local Description (Paste this to the other peer):" << endl;
 		cout << string(description) << endl;
 	});
 
-	pc->onLocalCandidate([](const Candidate &candidate) {
+	pc->onLocalCandidate([](Candidate candidate) {
 		cout << "Local Candidate (Paste this to the other peer after the local description):"
 		     << endl;
 		cout << string(candidate) << endl << endl;
@@ -60,7 +60,7 @@ int main(int argc, char **argv) {
 
 	dc->onClosed([&]() { cout << "[DataChannel closed: " << dc->label() << "]" << endl; });
 
-	dc->onMessage([](const variant<binary, string> &message) {
+	dc->onMessage([](variant<binary, string> message) {
 		if (holds_alternative<string>(message)) {
 			cout << "[Received: " << get<string>(message) << "]" << endl;
 		}

+ 14 - 0
examples/media/CMakeLists.txt

@@ -0,0 +1,14 @@
+cmake_minimum_required(VERSION 3.7)
+
+add_executable(datachannel-media main.cpp)
+set_target_properties(datachannel-media PROPERTIES
+        CXX_STANDARD 17
+        OUTPUT_NAME media)
+
+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)

+ 19 - 0
examples/media/README.md

@@ -0,0 +1,19 @@
+# Example Webcam from Browser to Port 5000
+This is an example copy/paste demo to send your webcam from your browser and out port 5000 through the demo application.
+
+## How to use
+Open main.html in your browser (you must open it either as HTTPS or as a domain of http://localhost).
+
+Start the application and copy it's offer into the text box of the web page.
+
+Copy the answer of the webpage back into the application.
+
+You will now see RTP traffic on `localhost:5000` of the computer that the application is running on.
+
+Use the following gstreamer demo pipeline to display the traffic
+(you might need to wave your hand in front of your camera to force an I-frame).
+
+```
+$ gst-launch-1.0 udpsrc address=127.0.0.1 port=5000 caps="application/x-rtp" ! queue ! rtph264depay ! video/x-h264,stream-format=byte-stream ! queue ! avdec_h264 ! queue ! autovideosink
+```
+

+ 97 - 0
examples/media/main.cpp

@@ -0,0 +1,97 @@
+/*
+ * libdatachannel client example
+ * Copyright (c) 2020 Staz M
+ * Copyright (c) 2020 Paul-Louis Ageneau
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define _WINSOCK_DEPRECATED_NO_WARNINGS
+
+#include "rtc/rtc.hpp"
+
+#include <iostream>
+#include <memory>
+#include <utility>
+
+#include <nlohmann/json.hpp>
+
+#ifdef _WIN32
+#include <winsock2.h>
+#else
+#include <arpa/inet.h>
+typedef int SOCKET;
+#endif
+
+using nlohmann::json;
+
+int main() {
+	try {
+		rtc::InitLogger(rtc::LogLevel::Debug);
+		auto pc = std::make_shared<rtc::PeerConnection>();
+
+		pc->onStateChange(
+		    [](rtc::PeerConnection::State state) { std::cout << "State: " << state << std::endl; });
+
+		pc->onGatheringStateChange([pc](rtc::PeerConnection::GatheringState state) {
+			std::cout << "Gathering State: " << state << std::endl;
+			if (state == rtc::PeerConnection::GatheringState::Complete) {
+				auto description = pc->localDescription();
+				json message = {{"type", description->typeString()},
+				                {"sdp", std::string(description.value())}};
+				std::cout << message << std::endl;
+			}
+		});
+
+		SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
+		sockaddr_in addr;
+		addr.sin_addr.s_addr = inet_addr("127.0.0.1");
+		addr.sin_port = htons(5000);
+		addr.sin_family = AF_INET;
+
+		rtc::Description::Video media("video", rtc::Description::Direction::RecvOnly);
+		media.addH264Codec(96);
+		media.setBitrate(
+		    3000); // Request 3Mbps (Browsers do not encode more than 2.5MBps from a webcam)
+
+		auto track = pc->addTrack(media);
+
+		auto session = std::make_shared<rtc::RtcpSession>();
+		track->setRtcpHandler(session);
+
+		track->onMessage(
+		    [session, sock, addr](rtc::binary message) {
+			    // This is an RTP packet
+			    sendto(sock, reinterpret_cast<const char *>(message.data()), message.size(), 0,
+			           reinterpret_cast<const struct sockaddr *>(&addr), sizeof(addr));
+		    },
+		    nullptr);
+
+		pc->setLocalDescription();
+
+		std::cout << "Expect RTP video traffic on localhost:5000" << std::endl;
+		std::cout << "Please copy/paste the answer provided by the browser: " << std::endl;
+		std::string sdp;
+		std::getline(std::cin, sdp);
+		std::cout << "Got answer" << sdp << std::endl;
+		json j = json::parse(sdp);
+		rtc::Description answer(j["sdp"].get<std::string>(), j["type"].get<std::string>());
+		pc->setRemoteDescription(answer);
+		std::cout << "Press any key to exit." << std::endl;
+		std::cin >> sdp;
+
+	} catch (const std::exception &e) {
+		std::cerr << "Error: " << e.what() << std::endl;
+	}
+}

+ 45 - 0
examples/media/main.html

@@ -0,0 +1,45 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>libdatachannel media example</title>
+</head>
+<body>
+
+<p>Please enter the offer provided to you by the application: </p>
+<textarea cols="50" rows="50"></textarea>
+<button>Submit</button>
+
+<script>
+    document.querySelector('button').addEventListener('click',  async () => {
+        let offer = JSON.parse(document.querySelector('textarea').value);
+        rtc = new RTCPeerConnection({
+            // Recommended for libdatachannel
+            bundlePolicy: "max-bundle",
+        });
+
+        rtc.onicegatheringstatechange = (state) => {
+            if (rtc.iceGatheringState === 'complete') {
+                // We only want to provide an answer once all of our candidates have been added to the SDP.
+                let answer = rtc.localDescription;
+                document.querySelector('textarea').value = JSON.stringify({"type": answer.type, sdp: answer.sdp});
+                document.querySelector('p').value = 'Please paste the answer in the application.';
+                alert('Please paste the answer in the application.');
+            }
+        }
+        await rtc.setRemoteDescription(offer);
+
+        let media = await navigator.mediaDevices.getUserMedia({
+            video: {
+                width: 1280,
+                height: 720
+            }
+        });
+        media.getTracks().forEach(track => rtc.addTrack(track, media));
+        let answer = await rtc.createAnswer();
+        await rtc.setLocalDescription(answer);
+    })
+</script>
+
+</body>
+</html>

+ 29 - 12
examples/web/package-lock.json

@@ -4,6 +4,14 @@
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
+    "bufferutil": {
+      "version": "4.0.1",
+      "resolved": "https://registry.npmjs.org/bufferutil/-/bufferutil-4.0.1.tgz",
+      "integrity": "sha512-xowrxvpxojqkagPcWRQVXZl0YXhRhAtBEIq3VoER1NH5Mw1n1o0ojdspp+GS2J//2gCVyrzQDApQ4unGF+QOoA==",
+      "requires": {
+        "node-gyp-build": "~3.7.0"
+      }
+    },
     "d": {
       "version": "1.0.1",
       "resolved": "https://registry.npmjs.org/d/-/d-1.0.1.tgz",
@@ -59,9 +67,9 @@
       },
       "dependencies": {
         "type": {
-          "version": "2.0.0",
-          "resolved": "https://registry.npmjs.org/type/-/type-2.0.0.tgz",
-          "integrity": "sha512-KBt58xCHry4Cejnc2ISQAF7QY+ORngsWfxezO68+12hKV6lQY8P/psIkcbjeHWn7MqcgciWJyCCevFMJdIXpow=="
+          "version": "2.1.0",
+          "resolved": "https://registry.npmjs.org/type/-/type-2.1.0.tgz",
+          "integrity": "sha512-G9absDWvhAWCV2gmF1zKud3OyC61nZDwWvBL2DApaVFogI07CprggiQAOOjvp2NRjYWFzPyu7vwtDrQFq8jeSA=="
         }
       }
     },
@@ -75,16 +83,16 @@
       "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
       "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
     },
-    "nan": {
-      "version": "2.14.1",
-      "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.1.tgz",
-      "integrity": "sha512-isWHgVjnFjh2x2yuJ/tj3JbwoHu3UC2dX5G/88Cm24yB6YopVgxvBObDY7n5xW6ExmFhJpSEQqFPvq9zaXc8Jw=="
-    },
     "next-tick": {
       "version": "1.0.0",
       "resolved": "https://registry.npmjs.org/next-tick/-/next-tick-1.0.0.tgz",
       "integrity": "sha1-yobR/ogoFpsBICCOPchCS524NCw="
     },
+    "node-gyp-build": {
+      "version": "3.7.0",
+      "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-3.7.0.tgz",
+      "integrity": "sha512-L/Eg02Epx6Si2NXmedx+Okg+4UHqmaf3TNcxd50SF9NQGcJaON3AtU++kax69XV7YWz4tUspqZSAsVofhFKG2w=="
+    },
     "type": {
       "version": "1.2.0",
       "resolved": "https://registry.npmjs.org/type/-/type-1.2.0.tgz",
@@ -98,15 +106,24 @@
         "is-typedarray": "^1.0.0"
       }
     },
+    "utf-8-validate": {
+      "version": "5.0.2",
+      "resolved": "https://registry.npmjs.org/utf-8-validate/-/utf-8-validate-5.0.2.tgz",
+      "integrity": "sha512-SwV++i2gTD5qh2XqaPzBnNX88N6HdyhQrNNRykvcS0QKvItV9u3vPEJr+X5Hhfb1JC0r0e1alL0iB09rY8+nmw==",
+      "requires": {
+        "node-gyp-build": "~3.7.0"
+      }
+    },
     "websocket": {
-      "version": "1.0.31",
-      "resolved": "https://registry.npmjs.org/websocket/-/websocket-1.0.31.tgz",
-      "integrity": "sha512-VAouplvGKPiKFDTeCCO65vYHsyay8DqoBSlzIO3fayrfOgU94lQN5a1uWVnFrMLceTJw/+fQXR5PGbUVRaHshQ==",
+      "version": "1.0.32",
+      "resolved": "https://registry.npmjs.org/websocket/-/websocket-1.0.32.tgz",
+      "integrity": "sha512-i4yhcllSP4wrpoPMU2N0TQ/q0O94LRG/eUQjEAamRltjQ1oT1PFFKOG4i877OlJgCG8rw6LrrowJp+TYCEWF7Q==",
       "requires": {
+        "bufferutil": "^4.0.1",
         "debug": "^2.2.0",
         "es5-ext": "^0.10.50",
-        "nan": "^2.14.0",
         "typedarray-to-buffer": "^3.1.5",
+        "utf-8-validate": "^5.0.2",
         "yaeti": "^0.0.6"
       }
     },

+ 1 - 1
examples/web/package.json

@@ -18,6 +18,6 @@
   },
   "homepage": "https://github.com/paullouisageneau/libdatachannel#readme",
   "dependencies": {
-    "websocket": "^1.0.31"
+    "websocket": "^1.0.32"
   }
 }

+ 10 - 9
include/rtc/channel.hpp

@@ -20,6 +20,7 @@
 #define RTC_CHANNEL_H
 
 #include "include.hpp"
+#include "message.hpp"
 
 #include <atomic>
 #include <functional>
@@ -33,7 +34,7 @@ public:
 	virtual ~Channel() = default;
 
 	virtual void close() = 0;
-	virtual bool send(const std::variant<binary, string> &data) = 0; // returns false if buffered
+	virtual bool send(message_variant data) = 0; // returns false if buffered
 
 	virtual bool isOpen() const = 0;
 	virtual bool isClosed() const = 0;
@@ -42,24 +43,24 @@ public:
 
 	void onOpen(std::function<void()> callback);
 	void onClosed(std::function<void()> callback);
-	void onError(std::function<void(const string &error)> callback);
+	void onError(std::function<void(string error)> callback);
 
-	void onMessage(std::function<void(const std::variant<binary, string> &data)> callback);
-	void onMessage(std::function<void(const binary &data)> binaryCallback,
-	               std::function<void(const string &data)> stringCallback);
+	void onMessage(std::function<void(message_variant data)> callback);
+	void onMessage(std::function<void(binary data)> binaryCallback,
+	               std::function<void(string data)> stringCallback);
 
 	void onBufferedAmountLow(std::function<void()> callback);
 	void setBufferedAmountLowThreshold(size_t amount);
 
 	// Extended API
-	virtual std::optional<std::variant<binary, string>> receive() = 0; // only if onMessage unset
+	virtual std::optional<message_variant> receive() = 0; // only if onMessage unset
 	virtual size_t availableAmount() const; // total size available to receive
 	void onAvailable(std::function<void()> callback);
 
 protected:
 	virtual void triggerOpen();
 	virtual void triggerClosed();
-	virtual void triggerError(const string &error);
+	virtual void triggerError(string error);
 	virtual void triggerAvailable(size_t count);
 	virtual void triggerBufferedAmount(size_t amount);
 
@@ -68,8 +69,8 @@ protected:
 private:
 	synchronized_callback<> mOpenCallback;
 	synchronized_callback<> mClosedCallback;
-	synchronized_callback<const string &> mErrorCallback;
-	synchronized_callback<const std::variant<binary, string> &> mMessageCallback;
+	synchronized_callback<string> mErrorCallback;
+	synchronized_callback<message_variant> mMessageCallback;
 	synchronized_callback<> mAvailableCallback;
 	synchronized_callback<> mBufferedAmountLowCallback;
 

+ 2 - 2
include/rtc/datachannel.hpp

@@ -50,7 +50,7 @@ public:
 	Reliability reliability() const;
 
 	void close(void) override;
-	bool send(const std::variant<binary, string> &data) override;
+	bool send(message_variant data) override;
 	bool send(const byte *data, size_t size);
 	template <typename Buffer> bool sendBuffer(const Buffer &buf);
 	template <typename Iterator> bool sendBuffer(Iterator first, Iterator last);
@@ -61,7 +61,7 @@ public:
 
 	// Extended API
 	size_t availableAmount() const override;
-	std::optional<std::variant<binary, string>> receive() override;
+	std::optional<message_variant> receive() override;
 
 private:
 	void remoteClose();

+ 146 - 28
include/rtc/description.hpp

@@ -1,5 +1,6 @@
 /**
- * Copyright (c) 2019 Paul-Louis Ageneau
+ * Copyright (c) 2019-2020 Paul-Louis Ageneau
+ * Copyright (c) 2020 Staz M
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -24,7 +25,9 @@
 
 #include <iostream>
 #include <map>
+#include <memory>
 #include <optional>
+#include <variant>
 #include <vector>
 
 namespace rtc {
@@ -33,6 +36,7 @@ class Description {
 public:
 	enum class Type { Unspec = 0, Offer = 1, Answer = 2 };
 	enum class Role { ActPass = 0, Passive = 1, Active = 2 };
+	enum class Direction { SendOnly, RecvOnly, SendRecv, Inactive, Unknown };
 
 	Description(const string &sdp, const string &typeString = "");
 	Description(const string &sdp, Type type);
@@ -42,52 +46,166 @@ public:
 	string typeString() const;
 	Role role() const;
 	string roleString() const;
-	string dataMid() const;
+	string bundleMid() const;
 	std::optional<string> fingerprint() const;
-	std::optional<uint16_t> sctpPort() const;
-	std::optional<size_t> maxMessageSize() const;
 	bool ended() const;
 
 	void hintType(Type type);
-	void setDataMid(string mid);
 	void setFingerprint(string fingerprint);
-	void setSctpPort(uint16_t port);
-	void setMaxMessageSize(size_t size);
 
 	void addCandidate(Candidate candidate);
 	void endCandidates();
 	std::vector<Candidate> extractCandidates();
 
-	bool hasMedia() const;
-	void addMedia(const Description &source);
-
 	operator string() const;
-	string generateSdp(const string &eol) const;
+	string generateSdp(string_view eol) const;
+	string generateApplicationSdp(string_view eol) const;
+
+	class Entry {
+	public:
+		virtual ~Entry() = default;
+
+		virtual string type() const { return mType; }
+		virtual string description() const { return mDescription; }
+		virtual string mid() const { return mMid; }
+		Direction direction() const { return mDirection; }
+		void setDirection(Direction dir);
+
+		operator string() const;
+		string generateSdp(string_view eol) const;
+
+		virtual void parseSdpLine(string_view line);
+
+	protected:
+		Entry(const string &mline, string mid, Direction dir = Direction::Unknown);
+		virtual string generateSdpLines(string_view eol) const;
+
+		std::vector<string> mAttributes;
+
+	private:
+		string mType;
+		string mDescription;
+		string mMid;
+		Direction mDirection;
+	};
+
+	struct Application : public Entry {
+	public:
+		Application(string mid = "data");
+		Application(const Application &other) = default;
+		Application(Application &&other) = default;
+
+		string description() const override;
+		Application reciprocate() const;
+
+		void setSctpPort(uint16_t port) { mSctpPort = port; }
+		void hintSctpPort(uint16_t port) { mSctpPort = mSctpPort.value_or(port); }
+		void setMaxMessageSize(size_t size) { mMaxMessageSize = size; }
+
+		std::optional<uint16_t> sctpPort() const { return mSctpPort; }
+		std::optional<size_t> maxMessageSize() const { return mMaxMessageSize; }
+
+		virtual void parseSdpLine(string_view line) override;
+
+	private:
+		virtual string generateSdpLines(string_view eol) const override;
+
+		std::optional<uint16_t> mSctpPort;
+		std::optional<size_t> mMaxMessageSize;
+	};
+
+	// Media (non-data)
+	class Media : public Entry {
+	public:
+		Media(const string &sdp);
+		Media(const string &mline, string mid, Direction dir = Direction::SendOnly);
+		Media(const Media &other) = default;
+		Media(Media &&other) = default;
+		virtual ~Media() = default;
+
+		string description() const override;
+		Media reciprocate() const;
+
+		void removeFormat(const string &fmt);
+
+		void addVideoCodec(int payloadType, const string &codec);
+		void addH264Codec(int payloadType);
+		void addVP8Codec(int payloadType);
+		void addVP9Codec(int payloadType);
+
+		void setBitrate(int bitrate);
+		int getBitrate() const;
+
+		bool hasPayloadType(int payloadType) const;
+
+		virtual void parseSdpLine(string_view line) override;
+
+	private:
+		virtual string generateSdpLines(string_view eol) const override;
+
+		int mBas = -1;
+
+		struct RTPMap {
+			RTPMap(string_view mline);
+
+			void removeFB(const string &string);
+			void addFB(const string &string);
+
+			int pt;
+			string format;
+			int clockRate;
+			string encParams;
+
+			std::vector<string> rtcpFbs;
+			std::vector<string> fmtps;
+		};
+
+		Media::RTPMap &getFormat(int fmt);
+		Media::RTPMap &getFormat(const string &fmt);
+
+		std::map<int, RTPMap> mRtpMap;
+	};
+
+	class Audio : public Media {
+	public:
+		Audio(string mid = "audio", Direction dir = Direction::SendOnly);
+	};
+
+	class Video : public Media {
+	public:
+		Video(string mid = "video", Direction dir = Direction::SendOnly);
+	};
+
+	bool hasApplication() const;
+	bool hasAudioOrVideo() const;
+
+	int addMedia(Media media);
+	int addMedia(Application application);
+	int addApplication(string mid = "data");
+	int addVideo(string mid = "video", Direction dir = Direction::SendOnly);
+	int addAudio(string mid = "audio", Direction dir = Direction::SendOnly);
+
+	std::variant<Media *, Application *> media(int index);
+	std::variant<const Media *, const Application *> media(int index) const;
+	int mediaCount() const;
+
+	Application *application();
 
 private:
+	std::shared_ptr<Entry> createEntry(string mline, string mid, Direction dir);
+	void removeApplication();
+
 	Type mType;
+
+	// Session-level attributes
 	Role mRole;
 	string mSessionId;
 	string mIceUfrag, mIcePwd;
 	std::optional<string> mFingerprint;
 
-	// Data
-	struct Data {
-		string mid;
-		std::optional<uint16_t> sctpPort;
-		std::optional<size_t> maxMessageSize;
-	};
-	Data mData;
-
-	// Media (non-data)
-	struct Media {
-		Media(const string &mline);
-		string type;
-		string description;
-		string mid;
-		std::vector<string> attributes;
-	};
-	std::map<int, Media> mMedia; // by m-line index
+	// Entries
+	std::vector<std::shared_ptr<Entry>> mEntries;
+	std::shared_ptr<Application> mApplication;
 
 	// Candidates
 	std::vector<Candidate> mCandidates;

+ 4 - 0
include/rtc/include.hpp

@@ -41,12 +41,14 @@
 #include <mutex>
 #include <optional>
 #include <string>
+#include <string_view>
 #include <vector>
 
 namespace rtc {
 
 using std::byte;
 using std::string;
+using std::string_view;
 using binary = std::vector<byte>;
 
 using std::nullopt;
@@ -64,6 +66,8 @@ const uint16_t DEFAULT_SCTP_PORT = 5000; // SCTP port to use by default
 const size_t DEFAULT_MAX_MESSAGE_SIZE = 65536;    // Remote max message size if not specified in SDP
 const size_t LOCAL_MAX_MESSAGE_SIZE = 256 * 1024; // Local max message size
 
+const size_t RECV_QUEUE_LIMIT = 1024 * 1024; // Max per-channel queue size
+
 const int THREADPOOL_SIZE = 4; // Number of threads in the global thread pool
 
 // overloaded helper

+ 14 - 18
include/rtc/message.hpp

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2019 Paul-Louis Ageneau
+ * Copyright (c) 2019-2020 Paul-Louis Ageneau
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -24,6 +24,8 @@
 
 #include <functional>
 #include <memory>
+#include <optional>
+#include <variant>
 
 namespace rtc {
 
@@ -46,8 +48,9 @@ struct Message : binary {
 
 using message_ptr = std::shared_ptr<Message>;
 using message_callback = std::function<void(message_ptr message)>;
+using message_variant = std::variant<binary, string>;
 
-constexpr auto message_size_func = [](const message_ptr &m) -> size_t {
+inline size_t message_size_func(const message_ptr &m) {
 	return m->type == Message::Binary || m->type == Message::String ? m->size() : 0;
 };
 
@@ -61,23 +64,16 @@ message_ptr make_message(Iterator begin, Iterator end, Message::Type type = Mess
 	return message;
 }
 
-inline message_ptr make_message(size_t size, Message::Type type = Message::Binary,
-                                unsigned int stream = 0,
-                                std::shared_ptr<Reliability> reliability = nullptr) {
-	auto message = std::make_shared<Message>(size, type);
-	message->stream = stream;
-	message->reliability = reliability;
-	return message;
-}
+message_ptr make_message(size_t size, Message::Type type = Message::Binary, unsigned int stream = 0,
+                         std::shared_ptr<Reliability> reliability = nullptr);
 
-inline message_ptr make_message(binary &&data, Message::Type type = Message::Binary,
-                                unsigned int stream = 0,
-                                std::shared_ptr<Reliability> reliability = nullptr) {
-	auto message = std::make_shared<Message>(std::move(data), type);
-	message->stream = stream;
-	message->reliability = reliability;
-	return message;
-}
+message_ptr make_message(binary &&data, Message::Type type = Message::Binary,
+                         unsigned int stream = 0,
+                         std::shared_ptr<Reliability> reliability = nullptr);
+
+message_ptr make_message(message_variant data);
+
+message_variant to_variant(Message &&message);
 
 } // namespace rtc
 

+ 29 - 21
include/rtc/peerconnection.hpp

@@ -28,6 +28,7 @@
 #include "message.hpp"
 #include "reliability.hpp"
 #include "rtc.hpp"
+#include "track.hpp"
 
 #include <atomic>
 #include <functional>
@@ -79,18 +80,22 @@ public:
 	std::optional<Description> remoteDescription() const;
 	std::optional<string> localAddress() const;
 	std::optional<string> remoteAddress() const;
+	bool hasMedia() const;
 
-	void setLocalDescription(std::optional<Description> mediaDescription = nullopt);
-	void setRemoteDescription(Description description,
-	                          std::optional<Description> mediaDescription = nullopt);
+	void setLocalDescription();
+	void setRemoteDescription(Description description);
 	void addRemoteCandidate(Candidate candidate);
 
-	std::shared_ptr<DataChannel> createDataChannel(const string &label, const string &protocol = "",
-	                                               const Reliability &reliability = {});
+	std::shared_ptr<DataChannel> addDataChannel(string label, string protocol = "",
+	                                            Reliability reliability = {});
+
+	// Equivalent to calling addDataChannel() and setLocalDescription()
+	std::shared_ptr<DataChannel> createDataChannel(string label, string protocol = "",
+	                                               Reliability reliability = {});
 
 	void onDataChannel(std::function<void(std::shared_ptr<DataChannel> dataChannel)> callback);
-	void onLocalDescription(std::function<void(const Description &description)> callback);
-	void onLocalCandidate(std::function<void(const Candidate &candidate)> callback);
+	void onLocalDescription(std::function<void(Description description)> callback);
+	void onLocalCandidate(std::function<void(Candidate candidate)> callback);
 	void onStateChange(std::function<void(State state)> callback);
 	void onGatheringStateChange(std::function<void(GatheringState state)> callback);
 
@@ -100,12 +105,9 @@ public:
 	size_t bytesReceived();
 	std::optional<std::chrono::milliseconds> rtt();
 
-	// Media
-	bool hasMedia() const;
-	void sendMedia(const binary &packet);
-	void sendMedia(const byte *packet, size_t size);
-
-	void onMedia(std::function<void(const binary &packet)> callback);
+	// Track media support requires compiling with libSRTP
+	std::shared_ptr<Track> addTrack(Description::Media description);
+	void onTrack(std::function<void(std::shared_ptr<Track> track)> callback);
 
 	// libnice only
 	bool getSelectedCandidatePair(CandidateInfo *local, CandidateInfo *remote);
@@ -122,18 +124,21 @@ private:
 	void forwardMedia(message_ptr message);
 	void forwardBufferedAmount(uint16_t stream, size_t amount);
 
-	std::shared_ptr<DataChannel> emplaceDataChannel(Description::Role role, const string &label,
-	                                                const string &protocol,
-	                                                const Reliability &reliability);
+	std::shared_ptr<DataChannel> emplaceDataChannel(Description::Role role, string label,
+	                                                string protocol, Reliability reliability);
 	std::shared_ptr<DataChannel> findDataChannel(uint16_t stream);
 	void iterateDataChannels(std::function<void(std::shared_ptr<DataChannel> channel)> func);
 	void openDataChannels();
 	void closeDataChannels();
 	void remoteCloseDataChannels();
 
+	void incomingTrack(Description::Media description);
+	void openTracks();
+
 	void processLocalDescription(Description description);
 	void processLocalCandidate(Candidate candidate);
 	void triggerDataChannel(std::weak_ptr<DataChannel> weakDataChannel);
+	void triggerTrack(std::shared_ptr<Track> track);
 	bool changeState(State state);
 	bool changeGatheringState(GatheringState state);
 
@@ -153,18 +158,21 @@ private:
 	std::shared_ptr<DtlsTransport> mDtlsTransport;
 	std::shared_ptr<SctpTransport> mSctpTransport;
 
-	std::unordered_map<unsigned int, std::weak_ptr<DataChannel>> mDataChannels;
-	std::shared_mutex mDataChannelsMutex;
+	std::unordered_map<unsigned int, std::weak_ptr<DataChannel>> mDataChannels; // by stream ID
+	std::unordered_map<string, std::weak_ptr<Track>> mTracks;                   // by mid
+	std::shared_mutex mDataChannelsMutex, mTracksMutex;
+
+	std::unordered_map<unsigned int, string> mMidFromPayloadType; // cache
 
 	std::atomic<State> mState;
 	std::atomic<GatheringState> mGatheringState;
 
 	synchronized_callback<std::shared_ptr<DataChannel>> mDataChannelCallback;
-	synchronized_callback<const Description &> mLocalDescriptionCallback;
-	synchronized_callback<const Candidate &> mLocalCandidateCallback;
+	synchronized_callback<Description> mLocalDescriptionCallback;
+	synchronized_callback<Candidate> mLocalCandidateCallback;
 	synchronized_callback<State> mStateChangeCallback;
 	synchronized_callback<GatheringState> mGatheringStateChangeCallback;
-	synchronized_callback<const binary &> mMediaCallback;
+	synchronized_callback<std::shared_ptr<Track>> mTrackCallback;
 };
 
 } // namespace rtc

+ 6 - 0
include/rtc/queue.hpp

@@ -39,6 +39,7 @@ public:
 
 	void stop();
 	bool empty() const;
+	bool full() const;
 	size_t size() const;   // elements
 	size_t amount() const; // amount
 	void push(T element);
@@ -80,6 +81,11 @@ template <typename T> bool Queue<T>::empty() const {
 	return mQueue.empty();
 }
 
+template <typename T> bool Queue<T>::full() const {
+	std::lock_guard lock(mMutex);
+	return mQueue.size() >= mLimit;
+}
+
 template <typename T> size_t Queue<T>::size() const {
 	std::lock_guard lock(mMutex);
 	return mQueue.size();

+ 17 - 8
include/rtc/rtc.h

@@ -29,10 +29,6 @@ extern "C" {
 #define RTC_EXPORT
 #endif
 
-#ifndef RTC_ENABLE_MEDIA
-#define RTC_ENABLE_MEDIA 1
-#endif
-
 #ifndef RTC_ENABLE_WEBSOCKET
 #define RTC_ENABLE_WEBSOCKET 1
 #endif
@@ -86,11 +82,12 @@ typedef struct {
 } rtcReliability;
 
 typedef void (*rtcLogCallbackFunc)(rtcLogLevel level, const char *message);
-typedef void (*rtcDataChannelCallbackFunc)(int dc, void *ptr);
 typedef void (*rtcDescriptionCallbackFunc)(const char *sdp, const char *type, void *ptr);
 typedef void (*rtcCandidateCallbackFunc)(const char *cand, const char *mid, void *ptr);
 typedef void (*rtcStateChangeCallbackFunc)(rtcState state, void *ptr);
 typedef void (*rtcGatheringStateCallbackFunc)(rtcGatheringState state, void *ptr);
+typedef void (*rtcDataChannelCallbackFunc)(int dc, void *ptr);
+typedef void (*rtcTrackCallbackFunc)(int tr, void *ptr);
 typedef void (*rtcOpenCallbackFunc)(void *ptr);
 typedef void (*rtcClosedCallbackFunc)(void *ptr);
 typedef void (*rtcErrorCallbackFunc)(const char *error, void *ptr);
@@ -108,12 +105,12 @@ RTC_EXPORT void rtcSetUserPointer(int id, void *ptr);
 RTC_EXPORT int rtcCreatePeerConnection(const rtcConfiguration *config); // returns pc id
 RTC_EXPORT int rtcDeletePeerConnection(int pc);
 
-RTC_EXPORT int rtcSetDataChannelCallback(int pc, rtcDataChannelCallbackFunc cb);
 RTC_EXPORT int rtcSetLocalDescriptionCallback(int pc, rtcDescriptionCallbackFunc cb);
 RTC_EXPORT int rtcSetLocalCandidateCallback(int pc, rtcCandidateCallbackFunc cb);
 RTC_EXPORT int rtcSetStateChangeCallback(int pc, rtcStateChangeCallbackFunc cb);
 RTC_EXPORT int rtcSetGatheringStateChangeCallback(int pc, rtcGatheringStateCallbackFunc cb);
 
+RTC_EXPORT int rtcSetLocalDescription(int pc);
 RTC_EXPORT int rtcSetRemoteDescription(int pc, const char *sdp, const char *type);
 RTC_EXPORT int rtcAddRemoteCandidate(int pc, const char *cand, const char *mid);
 
@@ -121,6 +118,11 @@ RTC_EXPORT int rtcGetLocalAddress(int pc, char *buffer, int size);
 RTC_EXPORT int rtcGetRemoteAddress(int pc, char *buffer, int size);
 
 // DataChannel
+RTC_EXPORT int rtcSetDataChannelCallback(int pc, rtcDataChannelCallbackFunc cb);
+RTC_EXPORT int rtcAddDataChannel(int pc, const char *label); // returns dc id
+RTC_EXPORT int rtcAddDataChannelExt(int pc, const char *label, const char *protocol,
+                                    const rtcReliability *reliability); // returns dc id
+// Equivalent to calling rtcAddDataChannel() and rtcSetLocalDescription()
 RTC_EXPORT int rtcCreateDataChannel(int pc, const char *label); // returns dc id
 RTC_EXPORT int rtcCreateDataChannelExt(int pc, const char *label, const char *protocol,
                                        const rtcReliability *reliability); // returns dc id
@@ -130,6 +132,13 @@ RTC_EXPORT int rtcGetDataChannelLabel(int dc, char *buffer, int size);
 RTC_EXPORT int rtcGetDataChannelProtocol(int dc, char *buffer, int size);
 RTC_EXPORT int rtcGetDataChannelReliability(int dc, rtcReliability *reliability);
 
+// Track
+RTC_EXPORT int rtcSetTrackCallback(int pc, rtcTrackCallbackFunc cb);
+RTC_EXPORT int rtcAddTrack(int pc, const char *mediaDescriptionSdp); // returns tr id
+RTC_EXPORT int rtcDeleteTrack(int tr);
+
+RTC_EXPORT int rtcGetTrackDescription(int tr, char *buffer, int size);
+
 // WebSocket
 #if RTC_ENABLE_WEBSOCKET
 typedef struct {
@@ -141,7 +150,7 @@ RTC_EXPORT int rtcCreateWebSocketEx(const char *url, const rtcWsConfiguration *c
 RTC_EXPORT int rtcDeleteWebsocket(int ws);
 #endif
 
-// DataChannel and WebSocket common API
+// DataChannel, Track, and WebSocket common API
 RTC_EXPORT int rtcSetOpenCallback(int id, rtcOpenCallbackFunc cb);
 RTC_EXPORT int rtcSetClosedCallback(int id, rtcClosedCallbackFunc cb);
 RTC_EXPORT int rtcSetErrorCallback(int id, rtcErrorCallbackFunc cb);
@@ -152,7 +161,7 @@ RTC_EXPORT int rtcGetBufferedAmount(int id); // total size buffered to send
 RTC_EXPORT int rtcSetBufferedAmountLowThreshold(int id, int amount);
 RTC_EXPORT int rtcSetBufferedAmountLowCallback(int id, rtcBufferedAmountLowCallbackFunc cb);
 
-// DataChannel and WebSocket common extended API
+// DataChannel, Track, and WebSocket common extended API
 RTC_EXPORT int rtcGetAvailableAmount(int id); // total size available to receive
 RTC_EXPORT int rtcSetAvailableCallback(int id, rtcAvailableCallbackFunc cb);
 RTC_EXPORT int rtcReceiveMessage(int id, char *buffer, int *size);

+ 59 - 0
include/rtc/rtcp.hpp

@@ -0,0 +1,59 @@
+/**
+ * Copyright (c) 2020 Staz M
+ * Copyright (c) 2020 Paul-Louis Ageneau
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef RTC_RTCP_H
+#define RTC_RTCP_H
+
+#include "include.hpp"
+#include "log.hpp"
+#include "message.hpp"
+
+namespace rtc {
+
+typedef uint32_t SSRC;
+
+class RtcpHandler {
+public:
+	virtual void onOutgoing(std::function<void(rtc::message_ptr)> cb) = 0;
+	virtual std::optional<rtc::message_ptr> incoming(rtc::message_ptr ptr) = 0;
+};
+
+// An RtcpSession can be plugged into a Track to handle the whole RTCP session
+class RtcpSession : public RtcpHandler {
+public:
+	void onOutgoing(std::function<void(rtc::message_ptr)> cb) override;
+
+	std::optional<rtc::message_ptr> incoming(rtc::message_ptr ptr) override;
+	void requestBitrate(unsigned int newBitrate);
+
+private:
+	void pushREMB(unsigned int bitrate);
+	void pushRR(unsigned int lastSR_delay);
+	void tx(message_ptr msg);
+
+	unsigned int mRequestedBitrate = 0;
+	synchronized_callback<rtc::message_ptr> mTxCallback;
+	SSRC mSsrc = 0;
+	uint32_t mGreatestSeqNo = 0;
+	uint64_t mSyncRTPTS, mSyncNTPTS;
+};
+
+} // namespace rtc
+
+#endif // RTC_RTCP_H

+ 82 - 0
include/rtc/track.hpp

@@ -0,0 +1,82 @@
+/**
+ * Copyright (c) 2020 Paul-Louis Ageneau
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef RTC_TRACK_H
+#define RTC_TRACK_H
+
+#include "channel.hpp"
+#include "description.hpp"
+#include "include.hpp"
+#include "message.hpp"
+#include "queue.hpp"
+#include "rtcp.hpp"
+
+#include <atomic>
+#include <variant>
+
+namespace rtc {
+
+#if RTC_ENABLE_MEDIA
+class DtlsSrtpTransport;
+#endif
+
+class Track final : public std::enable_shared_from_this<Track>, public Channel {
+public:
+	Track(Description::Media description);
+	~Track() = default;
+
+	string mid() const;
+	Description::Media description() const;
+
+	void close(void) override;
+	bool send(message_variant data) override;
+	bool send(const byte *data, size_t size);
+
+	bool isOpen(void) const override;
+	bool isClosed(void) const override;
+	size_t maxMessageSize() const override;
+
+	// Extended API
+	size_t availableAmount() const override;
+	std::optional<message_variant> receive() override;
+
+	// RTCP handler
+	void setRtcpHandler(std::shared_ptr<RtcpHandler> handler);
+
+private:
+#if RTC_ENABLE_MEDIA
+	void open(std::shared_ptr<DtlsSrtpTransport> transport);
+	std::weak_ptr<DtlsSrtpTransport> mDtlsSrtpTransport;
+#endif
+
+	bool outgoing(message_ptr message);
+	void incoming(message_ptr message);
+
+	Description::Media mMediaDescription;
+	std::atomic<bool> mIsClosed = false;
+
+	Queue<message_ptr> mRecvQueue;
+	std::shared_ptr<RtcpHandler> mRtcpHandler;
+
+	friend class PeerConnection;
+};
+
+} // namespace rtc
+
+#endif
+

+ 2 - 2
include/rtc/websocket.hpp

@@ -58,14 +58,14 @@ public:
 
 	void open(const string &url);
 	void close() override;
-	bool send(const std::variant<binary, string> &data) override;
+	bool send(const message_variant data) override;
 
 	bool isOpen() const override;
 	bool isClosed() const override;
 	size_t maxMessageSize() const override;
 
 	// Extended API
-	std::optional<std::variant<binary, string>> receive() override;
+	std::optional<message_variant> receive() override;
 	size_t availableAmount() const override; // total size available to receive
 
 private:

+ 168 - 56
src/rtc.cpp → src/capi.cpp

@@ -50,6 +50,7 @@ namespace {
 
 std::unordered_map<int, shared_ptr<PeerConnection>> peerConnectionMap;
 std::unordered_map<int, shared_ptr<DataChannel>> dataChannelMap;
+std::unordered_map<int, shared_ptr<Track>> trackMap;
 #if RTC_ENABLE_WEBSOCKET
 std::unordered_map<int, shared_ptr<WebSocket>> webSocketMap;
 #endif
@@ -84,6 +85,14 @@ shared_ptr<DataChannel> getDataChannel(int id) {
 		throw std::invalid_argument("DataChannel ID does not exist");
 }
 
+shared_ptr<Track> getTrack(int id) {
+	std::lock_guard lock(mutex);
+	if (auto it = trackMap.find(id); it != trackMap.end())
+		return it->second;
+	else
+		throw std::invalid_argument("Track ID does not exist");
+}
+
 int emplacePeerConnection(shared_ptr<PeerConnection> ptr) {
 	std::lock_guard lock(mutex);
 	int pc = ++lastId;
@@ -100,6 +109,14 @@ int emplaceDataChannel(shared_ptr<DataChannel> ptr) {
 	return dc;
 }
 
+int emplaceTrack(shared_ptr<Track> ptr) {
+	std::lock_guard lock(mutex);
+	int tr = ++lastId;
+	trackMap.emplace(std::make_pair(tr, ptr));
+	userPointerMap.emplace(std::make_pair(tr, nullptr));
+	return tr;
+}
+
 void erasePeerConnection(int pc) {
 	std::lock_guard lock(mutex);
 	if (peerConnectionMap.erase(pc) == 0)
@@ -114,6 +131,13 @@ void eraseDataChannel(int dc) {
 	userPointerMap.erase(dc);
 }
 
+void eraseTrack(int tr) {
+	std::lock_guard lock(mutex);
+	if (trackMap.erase(tr) == 0)
+		throw std::invalid_argument("Track ID does not exist");
+	userPointerMap.erase(tr);
+}
+
 #if RTC_ENABLE_WEBSOCKET
 shared_ptr<WebSocket> getWebSocket(int id) {
 	std::lock_guard lock(mutex);
@@ -143,11 +167,13 @@ shared_ptr<Channel> getChannel(int id) {
 	std::lock_guard lock(mutex);
 	if (auto it = dataChannelMap.find(id); it != dataChannelMap.end())
 		return it->second;
+	if (auto it = trackMap.find(id); it != trackMap.end())
+		return it->second;
 #if RTC_ENABLE_WEBSOCKET
 	if (auto it = webSocketMap.find(id); it != webSocketMap.end())
 		return it->second;
 #endif
-	throw std::invalid_argument("DataChannel or WebSocket ID does not exist");
+	throw std::invalid_argument("DataChannel, Track, or WebSocket ID does not exist");
 }
 
 template <typename F> int wrap(F func) {
@@ -242,12 +268,12 @@ int rtcDeletePeerConnection(int pc) {
 	});
 }
 
-int rtcCreateDataChannel(int pc, const char *label) {
-	return rtcCreateDataChannelExt(pc, label, nullptr, nullptr);
+int rtcAddDataChannel(int pc, const char *label) {
+	return rtcAddDataChannelExt(pc, label, nullptr, nullptr);
 }
 
-int rtcCreateDataChannelExt(int pc, const char *label, const char *protocol,
-                            const rtcReliability *reliability) {
+int rtcAddDataChannelExt(int pc, const char *label, const char *protocol,
+                         const rtcReliability *reliability) {
 	return WRAP({
 		Reliability r = {};
 		if (reliability) {
@@ -265,7 +291,7 @@ int rtcCreateDataChannelExt(int pc, const char *label, const char *protocol,
 			}
 		}
 		auto peerConnection = getPeerConnection(pc);
-		int dc = emplaceDataChannel(peerConnection->createDataChannel(
+		int dc = emplaceDataChannel(peerConnection->addDataChannel(
 		    string(label ? label : ""), string(protocol ? protocol : ""), r));
 		if (auto ptr = getUserPointer(pc))
 			rtcSetUserPointer(dc, *ptr);
@@ -273,6 +299,17 @@ int rtcCreateDataChannelExt(int pc, const char *label, const char *protocol,
 	});
 }
 
+int rtcCreateDataChannel(int pc, const char *label) {
+	return rtcCreateDataChannelExt(pc, label, nullptr, nullptr);
+}
+
+int rtcCreateDataChannelExt(int pc, const char *label, const char *protocol,
+                            const rtcReliability *reliability) {
+	int dc = rtcAddDataChannelExt(pc, label, protocol, reliability);
+	rtcSetLocalDescription(pc);
+	return dc;
+}
+
 int rtcDeleteDataChannel(int dc) {
 	return WRAP({
 		auto dataChannel = getDataChannel(dc);
@@ -287,6 +324,51 @@ int rtcDeleteDataChannel(int dc) {
 	});
 }
 
+int rtcAddTrack(int pc, const char *mediaDescriptionSdp) {
+	if (!mediaDescriptionSdp)
+		throw std::invalid_argument("Unexpected null pointer for track media description");
+
+	auto peerConnection = getPeerConnection(pc);
+	Description::Media media{string(mediaDescriptionSdp)};
+	int tr = emplaceTrack(peerConnection->addTrack(std::move(media)));
+	if (auto ptr = getUserPointer(pc))
+		rtcSetUserPointer(tr, *ptr);
+	return tr;
+}
+
+int rtcDeleteTrack(int tr) {
+	return WRAP({
+		auto track = getTrack(tr);
+		track->onOpen(nullptr);
+		track->onClosed(nullptr);
+		track->onError(nullptr);
+		track->onMessage(nullptr);
+		track->onBufferedAmountLow(nullptr);
+		track->onAvailable(nullptr);
+
+		eraseTrack(tr);
+	});
+}
+
+int rtcGetTrackDescription(int tr, char *buffer, int size) {
+	return WRAP({
+		auto track = getTrack(tr);
+
+		if (size <= 0)
+			return 0;
+
+		if (!buffer)
+			throw std::invalid_argument("Unexpected null pointer for buffer");
+
+		string description(track->description());
+		const char *data = description.data();
+		size = std::min(size - 1, int(description.size()));
+		std::copy(data, data + size, buffer);
+		buffer[size] = '\0';
+		return int(size + 1);
+	});
+}
+
 #if RTC_ENABLE_WEBSOCKET
 int rtcCreateWebSocket(const char *url) {
 	return WRAP({
@@ -321,27 +403,11 @@ int rtcDeleteWebsocket(int ws) {
 }
 #endif
 
-int rtcSetDataChannelCallback(int pc, rtcDataChannelCallbackFunc cb) {
-	return WRAP({
-		auto peerConnection = getPeerConnection(pc);
-		if (cb)
-			peerConnection->onDataChannel([pc, cb](std::shared_ptr<DataChannel> dataChannel) {
-				int dc = emplaceDataChannel(dataChannel);
-				if (auto ptr = getUserPointer(pc)) {
-					rtcSetUserPointer(dc, *ptr);
-					cb(dc, *ptr);
-				}
-			});
-		else
-			peerConnection->onDataChannel(nullptr);
-	});
-}
-
 int rtcSetLocalDescriptionCallback(int pc, rtcDescriptionCallbackFunc cb) {
 	return WRAP({
 		auto peerConnection = getPeerConnection(pc);
 		if (cb)
-			peerConnection->onLocalDescription([pc, cb](const Description &desc) {
+			peerConnection->onLocalDescription([pc, cb](Description desc) {
 				if (auto ptr = getUserPointer(pc))
 					cb(string(desc).c_str(), desc.typeString().c_str(), *ptr);
 			});
@@ -354,7 +420,7 @@ int rtcSetLocalCandidateCallback(int pc, rtcCandidateCallbackFunc cb) {
 	return WRAP({
 		auto peerConnection = getPeerConnection(pc);
 		if (cb)
-			peerConnection->onLocalCandidate([pc, cb](const Candidate &cand) {
+			peerConnection->onLocalCandidate([pc, cb](Candidate cand) {
 				if (auto ptr = getUserPointer(pc))
 					cb(cand.candidate().c_str(), cand.mid().c_str(), *ptr);
 			});
@@ -389,12 +455,51 @@ int rtcSetGatheringStateChangeCallback(int pc, rtcGatheringStateCallbackFunc cb)
 	});
 }
 
+int rtcSetDataChannelCallback(int pc, rtcDataChannelCallbackFunc cb) {
+	return WRAP({
+		auto peerConnection = getPeerConnection(pc);
+		if (cb)
+			peerConnection->onDataChannel([pc, cb](std::shared_ptr<DataChannel> dataChannel) {
+				int dc = emplaceDataChannel(dataChannel);
+				if (auto ptr = getUserPointer(pc)) {
+					rtcSetUserPointer(dc, *ptr);
+					cb(dc, *ptr);
+				}
+			});
+		else
+			peerConnection->onDataChannel(nullptr);
+	});
+}
+
+int rtcSetTrackCallback(int pc, rtcTrackCallbackFunc cb) {
+	return WRAP({
+		auto peerConnection = getPeerConnection(pc);
+		if (cb)
+			peerConnection->onTrack([pc, cb](std::shared_ptr<Track> track) {
+				int tr = emplaceTrack(track);
+				if (auto ptr = getUserPointer(pc)) {
+					rtcSetUserPointer(tr, *ptr);
+					cb(tr, *ptr);
+				}
+			});
+		else
+			peerConnection->onTrack(nullptr);
+	});
+}
+
+int rtcSetLocalDescription(int pc) {
+	return WRAP({
+		auto peerConnection = getPeerConnection(pc);
+		peerConnection->setLocalDescription();
+	});
+}
+
 int rtcSetRemoteDescription(int pc, const char *sdp, const char *type) {
 	return WRAP({
 		auto peerConnection = getPeerConnection(pc);
 
 		if (!sdp)
-			throw std::invalid_argument("Unexpected null pointer");
+			throw std::invalid_argument("Unexpected null pointer for remote description");
 
 		peerConnection->setRemoteDescription({string(sdp), type ? string(type) : ""});
 	});
@@ -405,7 +510,7 @@ int rtcAddRemoteCandidate(int pc, const char *cand, const char *mid) {
 		auto peerConnection = getPeerConnection(pc);
 
 		if (!cand)
-			throw std::invalid_argument("Unexpected null pointer");
+			throw std::invalid_argument("Unexpected null pointer for remote candidate");
 
 		peerConnection->addRemoteCandidate({string(cand), mid ? string(mid) : ""});
 	});
@@ -415,12 +520,12 @@ int rtcGetLocalAddress(int pc, char *buffer, int size) {
 	return WRAP({
 		auto peerConnection = getPeerConnection(pc);
 
-		if (!buffer)
-			throw std::invalid_argument("Unexpected null pointer");
-
 		if (size <= 0)
 			return 0;
 
+		if (!buffer)
+			throw std::invalid_argument("Unexpected null pointer for buffer");
+
 		if (auto addr = peerConnection->localAddress()) {
 			const char *data = addr->data();
 			size = std::min(size - 1, int(addr->size()));
@@ -435,12 +540,12 @@ int rtcGetRemoteAddress(int pc, char *buffer, int size) {
 	return WRAP({
 		auto peerConnection = getPeerConnection(pc);
 
-		if (!buffer)
-			throw std::invalid_argument("Unexpected null pointer");
-
 		if (size <= 0)
 			return 0;
 
+		if (!buffer)
+			throw std::invalid_argument("Unexpected null pointer for buffer");
+
 		if (auto addr = peerConnection->remoteAddress()) {
 			const char *data = addr->data();
 			size = std::min(size - 1, int(addr->size()));
@@ -455,12 +560,12 @@ int rtcGetDataChannelLabel(int dc, char *buffer, int size) {
 	return WRAP({
 		auto dataChannel = getDataChannel(dc);
 
-		if (!buffer)
-			throw std::invalid_argument("Unexpected null pointer");
-
 		if (size <= 0)
 			return 0;
 
+		if (!buffer)
+			throw std::invalid_argument("Unexpected null pointer for buffer");
+
 		string label = dataChannel->label();
 		const char *data = label.data();
 		size = std::min(size - 1, int(label.size()));
@@ -474,12 +579,12 @@ int rtcGetDataChannelProtocol(int dc, char *buffer, int size) {
 	return WRAP({
 		auto dataChannel = getDataChannel(dc);
 
-		if (!buffer)
-			throw std::invalid_argument("Unexpected null pointer");
-
 		if (size <= 0)
 			return 0;
 
+		if (!buffer)
+			throw std::invalid_argument("Unexpected null pointer for buffer");
+
 		string protocol = dataChannel->protocol();
 		const char *data = protocol.data();
 		size = std::min(size - 1, int(protocol.size()));
@@ -494,7 +599,7 @@ int rtcGetDataChannelReliability(int dc, rtcReliability *reliability) {
 		auto dataChannel = getDataChannel(dc);
 
 		if (!reliability)
-			throw std::invalid_argument("Unexpected null pointer");
+			throw std::invalid_argument("Unexpected null pointer for reliability");
 
 		Reliability r = dataChannel->reliability();
 		std::memset(reliability, 0, sizeof(*reliability));
@@ -542,7 +647,7 @@ int rtcSetErrorCallback(int id, rtcErrorCallbackFunc cb) {
 	return WRAP({
 		auto channel = getChannel(id);
 		if (cb)
-			channel->onError([id, cb](const string &error) {
+			channel->onError([id, cb](string error) {
 				if (auto ptr = getUserPointer(id))
 					cb(error.c_str(), *ptr);
 			});
@@ -556,11 +661,11 @@ int rtcSetMessageCallback(int id, rtcMessageCallbackFunc cb) {
 		auto channel = getChannel(id);
 		if (cb)
 			channel->onMessage(
-			    [id, cb](const binary &b) {
+			    [id, cb](binary b) {
 				    if (auto ptr = getUserPointer(id))
 					    cb(reinterpret_cast<const char *>(b.data()), int(b.size()), *ptr);
 			    },
-			    [id, cb](const string &s) {
+			    [id, cb](string s) {
 				    if (auto ptr = getUserPointer(id))
 					    cb(s.c_str(), -int(s.size() + 1), *ptr);
 			    });
@@ -573,8 +678,8 @@ int rtcSendMessage(int id, const char *data, int size) {
 	return WRAP({
 		auto channel = getChannel(id);
 
-		if (!data)
-			throw std::invalid_argument("Unexpected null pointer");
+		if (!data && size != 0)
+			throw std::invalid_argument("Unexpected null pointer for data");
 
 		if (size >= 0) {
 			auto b = reinterpret_cast<const byte *>(data);
@@ -637,25 +742,32 @@ int rtcReceiveMessage(int id, char *buffer, int *size) {
 	return WRAP({
 		auto channel = getChannel(id);
 
-		if (!buffer || !size)
-			throw std::invalid_argument("Unexpected null pointer");
+		if (!size)
+			throw std::invalid_argument("Unexpected null pointer for size");
+
+		if (!buffer && *size != 0)
+			throw std::invalid_argument("Unexpected null pointer for buffer");
 
 		if (auto message = channel->receive())
 			return std::visit( //
 			    overloaded{    //
-			               [&](const binary &b) {
-				               *size = std::min(*size, int(b.size()));
-				               auto data = reinterpret_cast<const char *>(b.data());
-				               std::copy(data, data + *size, buffer);
+			               [&](binary b) {
+				               if (*size > 0) {
+					               *size = std::min(*size, int(b.size()));
+					               auto data = reinterpret_cast<const char *>(b.data());
+					               std::copy(data, data + *size, buffer);
+				               }
 				               return 1;
 			               },
-			               [&](const string &s) {
-				               int len = std::min(*size - 1, int(s.size()));
-				               if (len >= 0) {
-					               std::copy(s.data(), s.data() + len, buffer);
-					               buffer[len] = '\0';
+			               [&](string s) {
+				               if (*size > 0) {
+					               int len = std::min(*size - 1, int(s.size()));
+					               if (len >= 0) {
+						               std::copy(s.data(), s.data() + len, buffer);
+						               buffer[len] = '\0';
+					               }
+					               *size = -(len + 1);
 				               }
-				               *size = -(len + 1);
 				               return 1;
 			               }},
 			    *message);

+ 7 - 9
src/channel.cpp

@@ -34,11 +34,9 @@ void Channel::onClosed(std::function<void()> callback) {
 	mClosedCallback = callback;
 }
 
-void Channel::onError(std::function<void(const string &error)> callback) {
-	mErrorCallback = callback;
-}
+void Channel::onError(std::function<void(string error)> callback) { mErrorCallback = callback; }
 
-void Channel::onMessage(std::function<void(const std::variant<binary, string> &data)> callback) {
+void Channel::onMessage(std::function<void(message_variant data)> callback) {
 	mMessageCallback = callback;
 
 	// Pass pending messages
@@ -46,10 +44,10 @@ void Channel::onMessage(std::function<void(const std::variant<binary, string> &d
 		mMessageCallback(*message);
 }
 
-void Channel::onMessage(std::function<void(const binary &data)> binaryCallback,
-                        std::function<void(const string &data)> stringCallback) {
-	onMessage([binaryCallback, stringCallback](const std::variant<binary, string> &data) {
-		std::visit(overloaded{binaryCallback, stringCallback}, data);
+void Channel::onMessage(std::function<void(binary data)> binaryCallback,
+                        std::function<void(string data)> stringCallback) {
+	onMessage([binaryCallback, stringCallback](std::variant<binary, string> data) {
+		std::visit(overloaded{binaryCallback, stringCallback}, std::move(data));
 	});
 }
 
@@ -67,7 +65,7 @@ void Channel::triggerOpen() { mOpenCallback(); }
 
 void Channel::triggerClosed() { mClosedCallback(); }
 
-void Channel::triggerError(const string &error) { mErrorCallback(error); }
+void Channel::triggerError(string error) { mErrorCallback(error); }
 
 void Channel::triggerAvailable(size_t count) {
 	if (count == 1)

+ 10 - 29
src/datachannel.cpp

@@ -72,8 +72,6 @@ struct CloseMessage {
 };
 #pragma pack(pop)
 
-const size_t RECV_QUEUE_LIMIT = 1024 * 1024; // 1 MiB
-
 DataChannel::DataChannel(weak_ptr<PeerConnection> pc, unsigned int stream, string label,
                          string protocol, Reliability reliability)
     : mPeerConnection(pc), mStream(stream), mLabel(std::move(label)),
@@ -117,39 +115,21 @@ void DataChannel::remoteClose() {
 	mSctpTransport.reset();
 }
 
-bool DataChannel::send(const std::variant<binary, string> &data) {
-	return std::visit(
-	    [&](const auto &d) {
-		    using T = std::decay_t<decltype(d)>;
-		    constexpr auto type = std::is_same_v<T, string> ? Message::String : Message::Binary;
-		    auto *b = reinterpret_cast<const byte *>(d.data());
-		    return outgoing(std::make_shared<Message>(b, b + d.size(), type));
-	    },
-	    data);
-}
+bool DataChannel::send(message_variant data) { return outgoing(make_message(std::move(data))); }
 
 bool DataChannel::send(const byte *data, size_t size) {
 	return outgoing(std::make_shared<Message>(data, data + size, Message::Binary));
 }
 
-std::optional<std::variant<binary, string>> DataChannel::receive() {
+std::optional<message_variant> DataChannel::receive() {
 	while (!mRecvQueue.empty()) {
 		auto message = *mRecvQueue.pop();
-		switch (message->type) {
-		case Message::Control: {
+		if (message->type == Message::Control) {
 			auto raw = reinterpret_cast<const uint8_t *>(message->data());
-			if (raw[0] == MESSAGE_CLOSE)
+			if (!message->empty() && raw[0] == MESSAGE_CLOSE)
 				remoteClose();
-			break;
-		}
-		case Message::String:
-			return std::make_optional(
-			    string(reinterpret_cast<const char *>(message->data()), message->size()));
-		case Message::Binary:
-			return std::make_optional(std::move(*message));
-		default:
-			// Ignore
-			break;
+		} else {
+			return to_variant(std::move(*message));
 		}
 	}
 
@@ -164,8 +144,9 @@ size_t DataChannel::maxMessageSize() const {
 	size_t remoteMax = DEFAULT_MAX_MESSAGE_SIZE;
 	if (auto pc = mPeerConnection.lock())
 		if (auto description = pc->remoteDescription())
-			if (auto maxMessageSize = description->maxMessageSize())
-				remoteMax = *maxMessageSize > 0 ? *maxMessageSize : LOCAL_MAX_MESSAGE_SIZE;
+			if (auto *application = description->application())
+				if (auto maxMessageSize = application->maxMessageSize())
+					remoteMax = *maxMessageSize > 0 ? *maxMessageSize : LOCAL_MAX_MESSAGE_SIZE;
 
 	return std::min(remoteMax, LOCAL_MAX_MESSAGE_SIZE);
 }
@@ -223,7 +204,7 @@ bool DataChannel::outgoing(message_ptr message) {
 
 	auto transport = mSctpTransport.lock();
 	if (!transport)
-		throw std::runtime_error("DataChannel has no transport");
+		throw std::runtime_error("DataChannel transport is not open");
 
 	// Before the ACK has been received on a DataChannel, all messages must be sent ordered
 	message->reliability = mIsOpen ? mReliability : nullptr;

+ 543 - 116
src/description.cpp

@@ -1,5 +1,6 @@
 /**
- * Copyright (c) 2019 Paul-Louis Ageneau
+ * Copyright (c) 2019-2020 Paul-Louis Ageneau
+ * Copyright (c) 2020 Staz M
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -19,18 +20,22 @@
 #include "description.hpp"
 
 #include <algorithm>
+#include <array>
 #include <cctype>
 #include <chrono>
+#include <iostream>
 #include <random>
 #include <sstream>
 
+using std::shared_ptr;
 using std::size_t;
 using std::string;
+using std::string_view;
 using std::chrono::system_clock;
 
 namespace {
 
-inline bool match_prefix(const string &str, const string &prefix) {
+inline bool match_prefix(string_view str, string_view prefix) {
 	return str.size() >= prefix.size() &&
 	       std::mismatch(prefix.begin(), prefix.end(), str.begin()).first == prefix.end();
 }
@@ -41,6 +46,21 @@ inline void trim_end(string &str) {
 	    str.end());
 }
 
+inline std::pair<string_view, string_view> parse_pair(string_view attr) {
+	string_view key, value;
+	if (size_t separator = attr.find(':'); separator != string::npos) {
+		key = attr.substr(0, separator);
+		value = attr.substr(separator + 1);
+	} else {
+		key = attr;
+	}
+	return std::make_pair(std::move(key), std::move(value));
+}
+
+template <typename T> T to_integer(string_view s) {
+	return std::is_signed<T>::value ? T(std::stol(string(s))) : T(std::stoul(string(s)));
+}
+
 } // namespace
 
 namespace rtc {
@@ -52,7 +72,6 @@ Description::Description(const string &sdp, Type type) : Description(sdp, type,
 
 Description::Description(const string &sdp, Type type, Role role)
     : mType(Type::Unspec), mRole(role) {
-	mData.mid = "data";
 	hintType(type);
 
 	auto seed = static_cast<unsigned int>(system_clock::now().time_since_epoch().count());
@@ -61,50 +80,25 @@ Description::Description(const string &sdp, Type type, Role role)
 	mSessionId = std::to_string(uniform(generator));
 
 	std::istringstream ss(sdp);
-	std::optional<Media> currentMedia;
+	std::shared_ptr<Entry> current;
 
-	int mlineIndex = 0;
-	bool finished;
-	do {
-		string line;
-		finished = !std::getline(ss, line) && line.empty();
+	int index = -1;
+	string line;
+	while (std::getline(ss, line) || !line.empty()) {
 		trim_end(line);
 
 		// Media description line (aka m-line)
-		if (finished || match_prefix(line, "m=")) {
-			if (currentMedia) {
-				if (!currentMedia->mid.empty()) {
-					if (currentMedia->type == "application")
-						mData.mid = currentMedia->mid;
-					else
-						mMedia.emplace(mlineIndex, std::move(*currentMedia));
-
-					++mlineIndex;
-
-				} else if (line.find(" ICE/SDP") != string::npos) {
-					PLOG_WARNING << "SDP \"m=\" line has no corresponding mid, ignoring";
-				}
-			}
-			if (!finished)
-				currentMedia.emplace(Media(line.substr(2)));
+		if (match_prefix(line, "m=")) {
+			++index;
+			string mline = line.substr(2);
+			current = createEntry(std::move(mline), std::to_string(index), Direction::Unknown);
 
 			// Attribute line
 		} else if (match_prefix(line, "a=")) {
 			string attr = line.substr(2);
+			auto [key, value] = parse_pair(attr);
 
-			string key, value;
-			if (size_t separator = attr.find(':'); separator != string::npos) {
-				key = attr.substr(0, separator);
-				value = attr.substr(separator + 1);
-			} else {
-				key = attr;
-			}
-
-			if (key == "mid") {
-				if (currentMedia)
-					currentMedia->mid = value;
-
-			} else if (key == "setup") {
+			if (key == "setup") {
 				if (value == "active")
 					mRole = Role::Active;
 				else if (value == "passive")
@@ -125,19 +119,18 @@ Description::Description(const string &sdp, Type type, Role role)
 				mIceUfrag = value;
 			} else if (key == "ice-pwd") {
 				mIcePwd = value;
-			} else if (key == "sctp-port") {
-				mData.sctpPort = uint16_t(std::stoul(value));
-			} else if (key == "max-message-size") {
-				mData.maxMessageSize = size_t(std::stoul(value));
 			} else if (key == "candidate") {
-				addCandidate(Candidate(attr, currentMedia ? currentMedia->mid : mData.mid));
+				addCandidate(Candidate(attr, bundleMid()));
 			} else if (key == "end-of-candidates") {
 				mEnded = true;
-			} else if (currentMedia) {
-				currentMedia->attributes.emplace_back(line.substr(2));
+			} else if (current) {
+				current->parseSdpLine(std::move(line));
 			}
+
+		} else if (current) {
+			current->parseSdpLine(std::move(line));
 		}
-	} while (!finished);
+	};
 }
 
 Description::Type Description::type() const { return mType; }
@@ -148,14 +141,13 @@ Description::Role Description::role() const { return mRole; }
 
 string Description::roleString() const { return roleToString(mRole); }
 
-string Description::dataMid() const { return mData.mid; }
+string Description::bundleMid() const {
+	// Get the mid of the first media
+	return !mEntries.empty() ? mEntries[0]->mid() : "0";
+}
 
 std::optional<string> Description::fingerprint() const { return mFingerprint; }
 
-std::optional<uint16_t> Description::sctpPort() const { return mData.sctpPort; }
-
-std::optional<size_t> Description::maxMessageSize() const { return mData.maxMessageSize; }
-
 bool Description::ended() const { return mEnded; }
 
 void Description::hintType(Type type) {
@@ -166,16 +158,10 @@ void Description::hintType(Type type) {
 	}
 }
 
-void Description::setDataMid(string mid) { mData.mid = mid; }
-
 void Description::setFingerprint(string fingerprint) {
 	mFingerprint.emplace(std::move(fingerprint));
 }
 
-void Description::setSctpPort(uint16_t port) { mData.sctpPort.emplace(port); }
-
-void Description::setMaxMessageSize(size_t size) { mData.maxMessageSize.emplace(size); }
-
 void Description::addCandidate(Candidate candidate) {
 	mCandidates.emplace_back(std::move(candidate));
 }
@@ -189,19 +175,9 @@ std::vector<Candidate> Description::extractCandidates() {
 	return result;
 }
 
-bool Description::hasMedia() const { return !mMedia.empty(); }
-
-void Description::addMedia(const Description &source) {
-	for (auto p : source.mMedia)
-		mMedia.emplace(p);
-}
-
 Description::operator string() const { return generateSdp("\r\n"); }
 
-string Description::generateSdp(const string &eol) const {
-	if (!mFingerprint)
-		throw std::logic_error("Fingerprint must be set to generate an SDP string");
-
+string Description::generateSdp(string_view eol) const {
 	std::ostringstream sdp;
 
 	// Header
@@ -214,59 +190,70 @@ string Description::generateSdp(const string &eol) const {
 	// see Negotiating Media Multiplexing Using the Session Description Protocol
 	// https://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-54
 	sdp << "a=group:BUNDLE";
-	for (int i = 0; i < int(mMedia.size() + 1); ++i)
-		if (auto it = mMedia.find(i); it != mMedia.end())
-			sdp << ' ' << it->second.mid;
-		else
-			sdp << ' ' << mData.mid;
+	for (const auto &entry : mEntries)
+		sdp << ' ' << entry->mid();
 	sdp << eol;
 
-	sdp << "a=msid-semantic: WMS" << eol;
+	// Lip-sync
+	std::ostringstream lsGroup;
+	for (const auto &entry : mEntries)
+		if (entry != mApplication)
+			lsGroup << ' ' << entry->mid();
 
-	// Non-data media
-	if (!mMedia.empty()) {
-		// Lip-sync
-		sdp << "a=group:LS";
-		for (const auto &p : mMedia)
-			sdp << " " << p.second.mid;
-		sdp << eol;
-	}
+	if (!lsGroup.str().empty())
+		sdp << "a=group:LS" << lsGroup.str() << eol;
+
+	// Session-level attributes
+	sdp << "a=msid-semantic:WMS *" << eol;
+	sdp << "a=setup:" << roleToString(mRole) << eol;
+	sdp << "a=ice-ufrag:" << mIceUfrag << eol;
+	sdp << "a=ice-pwd:" << mIcePwd << eol;
 
-	// Descriptions and attributes
-	for (int i = 0; i < int(mMedia.size() + 1); ++i) {
-		if (auto it = mMedia.find(i); it != mMedia.end()) {
-			// Non-data media
-			const auto &media = it->second;
-			sdp << "m=" << media.type << ' ' << 0 << ' ' << media.description << eol;
-			sdp << "c=IN IP4 0.0.0.0" << eol;
-			sdp << "a=bundle-only" << eol;
-			sdp << "a=mid:" << media.mid << eol;
-			for (const auto &attr : media.attributes)
-				sdp << "a=" << attr << eol;
+	if (!mEnded)
+		sdp << "a=ice-options:trickle" << eol;
 
-		} else {
-			// Data
-			const string description = "UDP/DTLS/SCTP webrtc-datachannel";
-			sdp << "m=application" << ' ' << (!mMedia.empty() ? 0 : 9) << ' ' << description << eol;
-			sdp << "c=IN IP4 0.0.0.0" << eol;
-			if (!mMedia.empty())
-				sdp << "a=bundle-only" << eol;
-			sdp << "a=mid:" << mData.mid << eol;
-			if (mData.sctpPort)
-				sdp << "a=sctp-port:" << *mData.sctpPort << eol;
-			if (mData.maxMessageSize)
-				sdp << "a=max-message-size:" << *mData.maxMessageSize << eol;
+	if (mFingerprint)
+		sdp << "a=fingerprint:sha-256 " << *mFingerprint << eol;
+
+	// Entries
+	bool first = true;
+	for (const auto &entry : mEntries) {
+		sdp << entry->generateSdp(eol);
+
+		if (std::exchange(first, false)) {
+			// Candidates
+			for (const auto &candidate : mCandidates)
+				sdp << string(candidate) << eol;
+
+			if (mEnded)
+				sdp << "a=end-of-candidates" << eol;
 		}
 	}
 
-	// Common
-	if (!mEnded)
-		sdp << "a=ice-options:trickle" << eol;
+	return sdp.str();
+}
+
+string Description::generateApplicationSdp(string_view eol) const {
+	std::ostringstream sdp;
+
+	// Header
+	sdp << "v=0" << eol;
+	sdp << "o=- " << mSessionId << " 0 IN IP4 127.0.0.1" << eol;
+	sdp << "s=-" << eol;
+	sdp << "t=0 0" << eol;
+
+	// Application
+	auto app = mApplication ? mApplication : std::make_shared<Application>();
+	sdp << app->generateSdp(eol);
 
+	// Session-level attributes
+	sdp << "a=msid-semantic:WMS *" << eol;
+	sdp << "a=setup:" << roleToString(mRole) << eol;
 	sdp << "a=ice-ufrag:" << mIceUfrag << eol;
 	sdp << "a=ice-pwd:" << mIcePwd << eol;
-	sdp << "a=setup:" << roleToString(mRole) << eol;
-	sdp << "a=tls-id:1" << eol;
+
+	if (!mEnded)
+		sdp << "a=ice-options:trickle" << eol;
 
 	if (mFingerprint)
 		sdp << "a=fingerprint:sha-256 " << *mFingerprint << eol;
@@ -274,20 +261,461 @@ string Description::generateSdp(const string &eol) const {
 	// Candidates
 	for (const auto &candidate : mCandidates)
 		sdp << string(candidate) << eol;
+
 	if (mEnded)
 		sdp << "a=end-of-candidates" << eol;
 
 	return sdp.str();
 }
 
-Description::Media::Media(const string &mline) {
+shared_ptr<Description::Entry> Description::createEntry(string mline, string mid, Direction dir) {
+	string type = mline.substr(0, mline.find(' '));
+	if (type == "application") {
+		removeApplication();
+		mApplication = std::make_shared<Application>(std::move(mid));
+		mEntries.emplace_back(mApplication);
+		return mApplication;
+	} else {
+		auto media = std::make_shared<Media>(std::move(mline), std::move(mid), dir);
+		mEntries.emplace_back(media);
+		return media;
+	}
+}
+
+void Description::removeApplication() {
+	if (!mApplication)
+		return;
+
+	auto it = std::find(mEntries.begin(), mEntries.end(), mApplication);
+	if (it != mEntries.end())
+		mEntries.erase(it);
+
+	mApplication.reset();
+}
+
+bool Description::hasApplication() const { return mApplication != nullptr; }
+
+bool Description::hasAudioOrVideo() const {
+	for (auto entry : mEntries)
+		if (entry != mApplication)
+			return true;
+
+	return false;
+}
+
+int Description::addMedia(Media media) {
+	mEntries.emplace_back(std::make_shared<Media>(std::move(media)));
+	return int(mEntries.size()) - 1;
+}
+
+int Description::addMedia(Application application) {
+	removeApplication();
+	mApplication = std::make_shared<Application>(std::move(application));
+	mEntries.emplace_back(mApplication);
+	return int(mEntries.size()) - 1;
+}
+
+int Description::addApplication(string mid) { return addMedia(Application(std::move(mid))); }
+
+Description::Application *Description::application() { return mApplication.get(); }
+
+int Description::addVideo(string mid, Direction dir) {
+	return addMedia(Video(std::move(mid), dir));
+}
+
+int Description::addAudio(string mid, Direction dir) {
+	return addMedia(Audio(std::move(mid), dir));
+}
+
+std::variant<Description::Media *, Description::Application *> Description::media(int index) {
+	if (index < 0 || index >= int(mEntries.size()))
+		throw std::out_of_range("Media index out of range");
+
+	const auto &entry = mEntries[index];
+	if (entry == mApplication) {
+		auto result = dynamic_cast<Application *>(entry.get());
+		if (!result)
+			throw std::logic_error("Bad type of application in description");
+		return result;
+	} else {
+		auto result = dynamic_cast<Media *>(entry.get());
+		if (!result)
+			throw std::logic_error("Bad type of media in description");
+		return result;
+	}
+}
+
+std::variant<const Description::Media *, const Description::Application *>
+Description::media(int index) const {
+	if (index < 0 || index >= int(mEntries.size()))
+		throw std::out_of_range("Media index out of range");
+
+	const auto &entry = mEntries[index];
+	if (entry == mApplication) {
+		auto result = dynamic_cast<Application *>(entry.get());
+		if (!result)
+			throw std::logic_error("Bad type of application in description");
+		return result;
+	} else {
+		auto result = dynamic_cast<Media *>(entry.get());
+		if (!result)
+			throw std::logic_error("Bad type of media in description");
+		return result;
+	}
+}
+
+int Description::mediaCount() const { return int(mEntries.size()); }
+
+Description::Entry::Entry(const string &mline, string mid, Direction dir)
+    : mMid(std::move(mid)), mDirection(dir) {
+
+	unsigned int port;
+	std::istringstream ss(mline);
+	ss >> mType;
+	ss >> port; // ignored
+	ss >> mDescription;
+}
+
+void Description::Entry::setDirection(Direction dir) { mDirection = dir; }
+
+Description::Entry::operator string() const { return generateSdp("\r\n"); }
+
+string Description::Entry::generateSdp(string_view eol) const {
+	std::ostringstream sdp;
+	// Port 9 is the discard protocol
+	sdp << "m=" << type() << ' ' << 9 << ' ' << description() << eol;
+	sdp << "c=IN IP4 0.0.0.0" << eol;
+	sdp << generateSdpLines(eol);
+
+	return sdp.str();
+}
+
+string Description::Entry::generateSdpLines(string_view eol) const {
+	std::ostringstream sdp;
+	sdp << "a=bundle-only" << eol;
+	sdp << "a=mid:" << mMid << eol;
+
+	switch (mDirection) {
+	case Direction::SendOnly:
+		sdp << "a=sendonly" << eol;
+		break;
+	case Direction::RecvOnly:
+		sdp << "a=recvonly" << eol;
+		break;
+	case Direction::SendRecv:
+		sdp << "a=sendrecv" << eol;
+		break;
+	case Direction::Inactive:
+		sdp << "a=inactive" << eol;
+		break;
+	default:
+		// Ignore
+		break;
+	}
+
+	for (const auto &attr : mAttributes)
+		sdp << "a=" << attr << eol;
+
+	return sdp.str();
+}
+
+void Description::Entry::parseSdpLine(string_view line) {
+	if (match_prefix(line, "a=")) {
+		string_view attr = line.substr(2);
+		auto [key, value] = parse_pair(attr);
+
+		if (key == "mid")
+			mMid = value;
+		else if (attr == "sendonly")
+			mDirection = Direction::SendOnly;
+		else if (attr == "recvonly")
+			mDirection = Direction::RecvOnly;
+		else if (key == "sendrecv")
+			mDirection = Direction::SendRecv;
+		else if (key == "inactive")
+			mDirection = Direction::Inactive;
+		else if (key == "bundle-only") {
+			// always added
+		} else
+			mAttributes.emplace_back(line.substr(2));
+	}
+}
+
+Description::Application::Application(string mid)
+    : Entry("application 9 UDP/DTLS/SCTP", std::move(mid), Direction::SendRecv) {}
+
+string Description::Application::description() const {
+	return Entry::description() + " webrtc-datachannel";
+}
+
+Description::Application Description::Application::reciprocate() const {
+	Application reciprocated(*this);
+
+	reciprocated.mMaxMessageSize.reset();
+
+	return reciprocated;
+}
+
+string Description::Application::generateSdpLines(string_view eol) const {
+	std::ostringstream sdp;
+	sdp << Entry::generateSdpLines(eol);
+
+	if (mSctpPort)
+		sdp << "a=sctp-port:" << *mSctpPort << eol;
+
+	if (mMaxMessageSize)
+		sdp << "a=max-message-size:" << *mMaxMessageSize << eol;
+
+	return sdp.str();
+}
+
+void Description::Application::parseSdpLine(string_view line) {
+	if (match_prefix(line, "a=")) {
+		string_view attr = line.substr(2);
+		auto [key, value] = parse_pair(attr);
+
+		if (key == "sctp-port") {
+			mSctpPort = to_integer<uint16_t>(value);
+		} else if (key == "max-message-size") {
+			mMaxMessageSize = to_integer<size_t>(value);
+		} else {
+			Entry::parseSdpLine(line);
+		}
+	} else {
+		Entry::parseSdpLine(line);
+	}
+}
+
+Description::Media::Media(const string &sdp) : Entry(sdp, "", Direction::Unknown) {
+	std::istringstream ss(sdp);
+	string line;
+	while (std::getline(ss, line) || !line.empty()) {
+		trim_end(line);
+		parseSdpLine(line);
+	}
+
+	if (mid().empty())
+		throw std::invalid_argument("Missing mid in media SDP");
+}
+
+Description::Media::Media(const string &mline, string mid, Direction dir)
+    : Entry(mline, std::move(mid), dir) {
+}
+
+string Description::Media::description() const {
+	std::ostringstream desc;
+	desc << Entry::description();
+	for (auto it = mRtpMap.begin(); it != mRtpMap.end(); ++it)
+		desc << ' ' << it->first;
+
+	return desc.str();
+}
+
+Description::Media Description::Media::reciprocate() const {
+	Media reciprocated(*this);
+
+	// Invert direction
+	switch (direction()) {
+	case Direction::RecvOnly:
+		reciprocated.setDirection(Direction::SendOnly);
+		break;
+	case Direction::SendOnly:
+		reciprocated.setDirection(Direction::RecvOnly);
+		break;
+	default:
+		// We are good
+		break;
+	}
+
+	return reciprocated;
+}
+
+Description::Media::RTPMap &Description::Media::getFormat(int fmt) {
+	auto it = mRtpMap.find(fmt);
+	if (it != mRtpMap.end())
+		return it->second;
+
+	throw std::invalid_argument("m-line index is out of bounds");
+}
+
+Description::Media::RTPMap &Description::Media::getFormat(const string &fmt) {
+	for (auto it = mRtpMap.begin(); it != mRtpMap.end(); ++it)
+		if (it->second.format == fmt)
+			return it->second;
+
+	throw std::invalid_argument("format was not found");
+}
+
+void Description::Media::removeFormat(const string &fmt) {
+	auto it = mRtpMap.begin();
+	std::vector<int> remed;
+
+	// Remove the actual formats
+	while (it != mRtpMap.end()) {
+		if (it->second.format == fmt) {
+			remed.emplace_back(it->first);
+			it = mRtpMap.erase(it);
+		} else {
+			it++;
+		}
+	}
+
+	// Remove any other rtpmaps that depend on the formats we just removed
+	it = mRtpMap.begin();
+	while (it != mRtpMap.end()) {
+		auto it2 = it->second.fmtps.begin();
+		bool rem = false;
+		while (it2 != it->second.fmtps.end()) {
+			if (it2->find("apt=") == 0) {
+				for (auto remid : remed) {
+					if (it2->find(std::to_string(remid)) != string::npos) {
+						std::cout << *it2 << ' ' << remid << std::endl;
+						it = mRtpMap.erase(it);
+						rem = true;
+						break;
+					}
+				}
+				break;
+			}
+			it2++;
+		}
+		if (!rem)
+			it++;
+	}
+}
+
+void Description::Media::addVideoCodec(int payloadType, const string &codec) {
+	RTPMap map(std::to_string(payloadType) + ' ' + codec + "/90000");
+	map.addFB("nack");
+	map.addFB("goog-remb");
+	if (codec == "H264") {
+		// 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.
+		map.fmtps.emplace_back("profile-level-id=42E02A;level-asymmetry-allowed=1");
+	}
+	mRtpMap.emplace(map.pt, map);
+}
+
+void Description::Media::addH264Codec(int pt) { addVideoCodec(pt, "H264"); }
+
+void Description::Media::addVP8Codec(int payloadType) { addVideoCodec(payloadType, "VP8"); }
+
+void Description::Media::addVP9Codec(int payloadType) { addVideoCodec(payloadType, "VP9"); }
+
+void Description::Media::setBitrate(int bitrate) { mBas = bitrate; }
+
+int Description::Media::getBitrate() const { return mBas; }
+
+bool Description::Media::hasPayloadType(int payloadType) const {
+	return mRtpMap.find(payloadType) != mRtpMap.end();
+}
+
+string Description::Media::generateSdpLines(string_view eol) const {
+	std::ostringstream sdp;
+	if (mBas >= 0)
+		sdp << "b=AS:" << mBas << eol;
+
+	sdp << Entry::generateSdpLines(eol);
+	sdp << "a=rtcp-mux" << eol;
+
+	for (auto it = mRtpMap.begin(); it != mRtpMap.end(); ++it) {
+		auto &map = it->second;
+
+		// Create the a=rtpmap
+		sdp << "a=rtpmap:" << map.pt << ' ' << map.format << '/' << map.clockRate;
+		if (!map.encParams.empty())
+			sdp << '/' << map.encParams;
+		sdp << eol;
+
+		for (const auto &val : map.rtcpFbs)
+			sdp << "a=rtcp-fb:" << map.pt << ' ' << val << eol;
+		for (const auto &val : map.fmtps)
+			sdp << "a=fmtp:" << map.pt << ' ' << val << eol;
+	}
+
+	return sdp.str();
+}
+
+void Description::Media::parseSdpLine(string_view line) {
+	if (match_prefix(line, "a=")) {
+		string_view attr = line.substr(2);
+		auto [key, value] = parse_pair(attr);
+
+		if (key == "rtpmap") {
+			Description::Media::RTPMap map(value);
+			int pt = map.pt;
+			mRtpMap.emplace(pt, std::move(map));
+		} else if (key == "rtcp-fb") {
+			size_t p = value.find(' ');
+			int pt = to_integer<int>(value.substr(0, p));
+			auto it = mRtpMap.find(pt);
+			if (it == mRtpMap.end()) {
+				PLOG_WARNING << "rtcp-fb applied before the corresponding rtpmap, ignoring";
+			} else {
+				it->second.rtcpFbs.emplace_back(value.substr(p + 1));
+			}
+		} else if (key == "fmtp") {
+			size_t p = value.find(' ');
+			int pt = to_integer<int>(value.substr(0, p));
+			auto it = mRtpMap.find(pt);
+			if (it == mRtpMap.end()) {
+				PLOG_WARNING << "fmtp applied before the corresponding rtpmap, ignoring";
+			} else {
+				it->second.fmtps.emplace_back(value.substr(p + 1));
+			}
+		} else if (key == "rtcp-mux") {
+			// always added
+		} else {
+			Entry::parseSdpLine(line);
+		}
+	} else if (match_prefix(line, "b=AS")) {
+		mBas = to_integer<int>(line.substr(line.find(':') + 1));
+	} else {
+		Entry::parseSdpLine(line);
+	}
+}
+
+Description::Media::RTPMap::RTPMap(string_view mline) {
 	size_t p = mline.find(' ');
-	this->type = mline.substr(0, p);
-	if (p != string::npos)
-		if (size_t q = mline.find(' ', p + 1); q != string::npos)
-			this->description = mline.substr(q + 1);
+
+	this->pt = to_integer<int>(mline.substr(0, p));
+
+	string_view line = mline.substr(p + 1);
+	size_t spl = line.find('/');
+	this->format = line.substr(0, spl);
+
+	line = line.substr(spl + 1);
+	spl = line.find('/');
+	if (spl == string::npos) {
+		spl = line.find(' ');
+	}
+	if (spl == string::npos)
+		this->clockRate = to_integer<int>(line);
+	else {
+		this->clockRate = to_integer<int>(line.substr(0, spl));
+		this->encParams = line.substr(spl);
+	}
+}
+
+void Description::Media::RTPMap::removeFB(const string &str) {
+	auto it = rtcpFbs.begin();
+	while (it != rtcpFbs.end()) {
+		if (it->find(str) != std::string::npos) {
+			it = rtcpFbs.erase(it);
+		} else
+			it++;
+	}
 }
 
+void Description::Media::RTPMap::addFB(const string &str) { rtcpFbs.emplace_back(str); }
+
+Description::Audio::Audio(string mid, Direction dir)
+    : Media("audio 9 UDP/TLS/RTP/SAVPF", std::move(mid), dir) {}
+
+Description::Video::Video(string mid, Direction dir)
+    : Media("video 9 UDP/TLS/RTP/SAVPF", std::move(mid), dir) {}
+
 Description::Type Description::stringToType(const string &typeString) {
 	if (typeString == "offer")
 		return Type::Offer;
@@ -324,4 +752,3 @@ string Description::roleToString(Role role) {
 std::ostream &operator<<(std::ostream &out, const rtc::Description &description) {
 	return out << std::string(description);
 }
-

+ 21 - 9
src/dtlssrtptransport.cpp

@@ -52,7 +52,7 @@ DtlsSrtpTransport::DtlsSrtpTransport(std::shared_ptr<IceTransport> lower,
 #else
 	PLOG_DEBUG << "Setting SRTP profile (OpenSSL)";
 	// returns 0 on success, 1 on error
-	if (SSL_set_tlsext_use_srtp(mSsl, "SRTP_AES128_CM_SHA1_80"), "Failed to set SRTP profile")
+	if (SSL_set_tlsext_use_srtp(mSsl, "SRTP_AES128_CM_SHA1_80"))
 		throw std::runtime_error("Failed to set SRTP profile: " +
 		                         openssl::error_string(ERR_get_error()));
 #endif
@@ -86,7 +86,8 @@ bool DtlsSrtpTransport::sendMedia(message_ptr message) {
 	PLOG_VERBOSE << "Send size=" << size;
 
 	// The RTP header has a minimum size of 12 bytes
-	if (size < 12)
+	// An RTCP packet can have a minimum size of 8 bytes
+	if (size < 8)
 		throw std::runtime_error("RTP/RTCP packet too short");
 
 	// srtp_protect() and srtp_protect_rtcp() assume that they can write SRTP_MAX_TRAILER_LEN (for
@@ -98,6 +99,7 @@ bool DtlsSrtpTransport::sendMedia(message_ptr message) {
 	             << unsigned(value2);
 
 	// RFC 5761 Multiplexing RTP and RTCP 4. Distinguishable RTP and RTCP Packets
+	// https://tools.ietf.org/html/rfc5761#section-4
 	// It is RECOMMENDED to follow the guidelines in the RTP/AVP profile for the choice of RTP
 	// payload type values, with the additional restriction that payload type values in the
 	// range 64-95 MUST NOT be used. Specifically, dynamic RTP payload types SHOULD be chosen in
@@ -124,8 +126,8 @@ bool DtlsSrtpTransport::sendMedia(message_ptr message) {
 	}
 
 	message->resize(size);
-	outgoing(message);
-	return true;
+	return outgoing(message);
+//	return DtlsTransport::send(message);
 }
 
 void DtlsSrtpTransport::incoming(message_ptr message) {
@@ -140,6 +142,7 @@ void DtlsSrtpTransport::incoming(message_ptr message) {
 		return;
 
 	// RFC 5764 5.1.2. Reception
+	// https://tools.ietf.org/html/rfc5764#section-5.1.2
 	// The process for demultiplexing a packet is as follows. The receiver looks at the first byte
 	// of the packet. [...] If the value is in between 128 and 191 (inclusive), then the packet is
 	// RTP (or RTCP [...]). If the value is between 20 and 63 (inclusive), the packet is DTLS.
@@ -153,7 +156,8 @@ void DtlsSrtpTransport::incoming(message_ptr message) {
 
 	} else if (value1 >= 128 && value1 <= 191) {
 		// The RTP header has a minimum size of 12 bytes
-		if (size < 12) {
+		// An RTCP packet can have a minimum size of 8 bytes
+		if (size < 8) {
 			PLOG_WARNING << "Incoming SRTP/SRTCP packet too short, size=" << size;
 			return;
 		}
@@ -168,21 +172,29 @@ void DtlsSrtpTransport::incoming(message_ptr message) {
 			if (srtp_err_status_t err = srtp_unprotect_rtcp(mSrtpIn, message->data(), &size)) {
 				if (err == srtp_err_status_replay_fail)
 					PLOG_WARNING << "Incoming SRTCP packet is a replay";
+				else if (err == srtp_err_status_auth_fail)
+					PLOG_WARNING << "Incoming SRTCP packet failed authentication check";
 				else
 					PLOG_WARNING << "SRTCP unprotect error, status=" << err;
 				return;
 			}
 			PLOG_VERBOSE << "Unprotected SRTCP packet, size=" << size;
+			message->type = Message::Type::Control;
+			message->stream = to_integer<uint8_t>(*(message->begin() + 1)); // Payload Type
 		} else {
 			PLOG_VERBOSE << "Incoming SRTP packet, size=" << size;
 			if (srtp_err_status_t err = srtp_unprotect(mSrtpIn, message->data(), &size)) {
 				if (err == srtp_err_status_replay_fail)
 					PLOG_WARNING << "Incoming SRTP packet is a replay";
+				else if (err == srtp_err_status_auth_fail)
+					PLOG_WARNING << "Incoming SRTP packet failed authentication check";
 				else
 					PLOG_WARNING << "SRTP unprotect error, status=" << err;
 				return;
 			}
 			PLOG_VERBOSE << "Unprotected SRTP packet, size=" << size;
+			message->type = Message::Type::Binary;
+			message->stream = value2; // Payload Type
 		}
 
 		message->resize(size);
@@ -238,11 +250,11 @@ void DtlsSrtpTransport::postHandshake() {
 		throw std::runtime_error("Failed to derive SRTP keys: " +
 		                         openssl::error_string(ERR_get_error()));
 
+	// Order is client key, server key, client salt, and server salt
 	clientKey = material;
-	clientSalt = clientKey + SRTP_AES_128_KEY_LEN;
-
-	serverKey = material + SRTP_AES_ICM_128_KEY_LEN_WSALT;
-	serverSalt = serverKey + SRTP_AES_128_KEY_LEN;
+	serverKey = clientKey + SRTP_AES_128_KEY_LEN;
+	clientSalt = serverKey + SRTP_AES_128_KEY_LEN;
+	serverSalt = clientSalt + SRTP_SALT_LEN;
 #endif
 
 	unsigned char clientSessionKey[SRTP_AES_ICM_128_KEY_LEN_WSALT];

+ 0 - 1
src/dtlstransport.cpp

@@ -149,7 +149,6 @@ void DtlsTransport::postHandshake() {
 
 void DtlsTransport::runRecvLoop() {
 	const size_t maxMtu = 4096;
-
 	// Handshake loop
 	try {
 		changeState(State::Connecting);

+ 8 - 6
src/icetransport.cpp

@@ -123,9 +123,10 @@ Description IceTransport::getLocalDescription(Description::Type type) const {
 void IceTransport::setRemoteDescription(const Description &description) {
 	mRole = description.role() == Description::Role::Active ? Description::Role::Passive
 	                                                        : Description::Role::Active;
-	mMid = description.dataMid();
-	if (juice_set_remote_description(mAgent.get(), string(description).c_str()) < 0)
-		throw std::runtime_error("Failed to parse remote SDP");
+	mMid = description.bundleMid();
+	if (juice_set_remote_description(mAgent.get(),
+	                                 description.generateApplicationSdp("\r\n").c_str()) < 0)
+		throw std::runtime_error("Failed to parse ICE settings from remote SDP");
 }
 
 bool IceTransport::addRemoteCandidate(const Candidate &candidate) {
@@ -483,12 +484,13 @@ Description IceTransport::getLocalDescription(Description::Type type) const {
 void IceTransport::setRemoteDescription(const Description &description) {
 	mRole = description.role() == Description::Role::Active ? Description::Role::Passive
 	                                                        : Description::Role::Active;
-	mMid = description.dataMid();
+	mMid = description.bundleMid();
 	mTrickleTimeout = !description.ended() ? 30s : 0s;
 
 	// Warning: libnice expects "\n" as end of line
-	if (nice_agent_parse_remote_sdp(mNiceAgent.get(), description.generateSdp("\n").c_str()) < 0)
-		throw std::runtime_error("Failed to parse remote SDP");
+	if (nice_agent_parse_remote_sdp(mNiceAgent.get(),
+	                                description.generateApplicationSdp("\n").c_str()) < 0)
+		throw std::runtime_error("Failed to parse ICE settings from remote SDP");
 }
 
 bool IceTransport::addRemoteCandidate(const Candidate &candidate) {

+ 60 - 0
src/message.cpp

@@ -0,0 +1,60 @@
+/**
+ * Copyright (c) 2019-2020 Paul-Louis Ageneau
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "message.hpp"
+
+namespace rtc {
+
+message_ptr make_message(size_t size, Message::Type type, unsigned int stream,
+                         std::shared_ptr<Reliability> reliability) {
+	auto message = std::make_shared<Message>(size, type);
+	message->stream = stream;
+	message->reliability = reliability;
+	return message;
+}
+
+message_ptr make_message(binary &&data, Message::Type type, unsigned int stream,
+                         std::shared_ptr<Reliability> reliability) {
+	auto message = std::make_shared<Message>(std::move(data), type);
+	message->stream = stream;
+	message->reliability = reliability;
+	return message;
+}
+
+message_ptr make_message(message_variant data) {
+	return std::visit( //
+	    overloaded{
+	        [&](binary data) { return make_message(std::move(data), Message::Binary); },
+	        [&](string data) {
+		        auto b = reinterpret_cast<const byte *>(data.data());
+		        return make_message(b, b + data.size(), Message::String);
+	        },
+	    },
+	    std::move(data));
+}
+
+message_variant to_variant(Message &&message) {
+	switch (message.type) {
+	case Message::String:
+		return string(reinterpret_cast<const char *>(message.data()), message.size());
+	default:
+		return std::move(message);
+	}
+}
+
+} // namespace rtc

+ 252 - 85
src/peerconnection.cpp

@@ -30,6 +30,7 @@
 #include "dtlssrtptransport.hpp"
 #endif
 
+#include <iomanip>
 #include <thread>
 
 namespace rtc {
@@ -81,27 +82,44 @@ std::optional<Description> PeerConnection::remoteDescription() const {
 	return mRemoteDescription;
 }
 
-void PeerConnection::setLocalDescription(std::optional<Description> mediaDescription) {
+void PeerConnection::setLocalDescription() {
 	PLOG_VERBOSE << "Setting local description";
 
-	if (std::atomic_load(&mIceTransport))
-		throw std::logic_error("Local description is already set");
+	if (std::atomic_load(&mIceTransport)) {
+		PLOG_DEBUG << "Local description is already set";
+	}
 
 	// RFC 5763: The endpoint that is the offerer MUST use the setup attribute value of
 	// setup:actpass.
 	// See https://tools.ietf.org/html/rfc5763#section-5
 	auto iceTransport = initIceTransport(Description::Role::ActPass);
 	Description localDescription = iceTransport->getLocalDescription(Description::Type::Offer);
-	if (mediaDescription)
-		localDescription.addMedia(*mediaDescription);
 	processLocalDescription(localDescription);
 	iceTransport->gatherLocalCandidates();
 }
 
-void PeerConnection::setRemoteDescription(Description description,
-                                          std::optional<Description> mediaDescription) {
+void PeerConnection::setRemoteDescription(Description description) {
 	PLOG_VERBOSE << "Setting remote description: " << string(description);
 
+	if (description.mediaCount() == 0)
+		throw std::invalid_argument("Remote description has no media line");
+
+	int activeMediaCount = 0;
+	for (int i = 0; i < description.mediaCount(); ++i)
+		std::visit( // reciprocate each media
+		    rtc::overloaded{[&](Description::Application *) { ++activeMediaCount; },
+		                    [&](Description::Media *media) {
+			                    if (media->direction() != Description::Direction::Inactive)
+				                    ++activeMediaCount;
+		                    }},
+		    description.media(i));
+
+	if (activeMediaCount == 0)
+		throw std::invalid_argument("Remote description has no active media");
+
+	if (!description.fingerprint())
+		throw std::invalid_argument("Remote description has no fingerprint");
+
 	description.hintType(localDescription() ? Description::Type::Answer : Description::Type::Offer);
 	auto type = description.type();
 	auto remoteCandidates = description.extractCandidates(); // Candidates will be added at the end
@@ -119,8 +137,6 @@ void PeerConnection::setRemoteDescription(Description description,
 	if (type == Description::Type::Offer) {
 		// This is an offer and we are the answerer.
 		Description localDescription = iceTransport->getLocalDescription(Description::Type::Answer);
-		if (mediaDescription)
-			localDescription.addMedia(*mediaDescription);
 		processLocalDescription(localDescription);
 		iceTransport->gatherLocalCandidates();
 	} else {
@@ -145,7 +161,7 @@ void PeerConnection::setRemoteDescription(Description description,
 
 	for (const auto &candidate : remoteCandidates)
 		addRemoteCandidate(candidate);
-}
+	}
 
 void PeerConnection::addRemoteCandidate(Candidate candidate) {
 	PLOG_VERBOSE << "Adding remote candidate: " << string(candidate);
@@ -182,9 +198,13 @@ std::optional<string> PeerConnection::remoteAddress() const {
 	return iceTransport ? iceTransport->getRemoteAddress() : nullopt;
 }
 
-shared_ptr<DataChannel> PeerConnection::createDataChannel(const string &label,
-                                                          const string &protocol,
-                                                          const Reliability &reliability) {
+shared_ptr<DataChannel> PeerConnection::addDataChannel(string label, string protocol,
+                                                       Reliability reliability) {
+	if (auto local = localDescription(); local && !local->hasApplication()) {
+		PLOG_ERROR << "The PeerConnection was negociated without DataChannel support.";
+		throw std::runtime_error("No DataChannel support on the PeerConnection");
+	}
+
 	// RFC 5763: The answerer MUST use either a setup attribute value of setup:active or
 	// setup:passive. [...] Thus, setup:active is RECOMMENDED.
 	// See https://tools.ietf.org/html/rfc5763#section-5
@@ -192,20 +212,20 @@ shared_ptr<DataChannel> PeerConnection::createDataChannel(const string &label,
 	auto iceTransport = std::atomic_load(&mIceTransport);
 	auto role = iceTransport ? iceTransport->role() : Description::Role::Passive;
 
-	auto channel = emplaceDataChannel(role, label, protocol, reliability);
+	auto channel =
+	    emplaceDataChannel(role, std::move(label), std::move(protocol), std::move(reliability));
+
+	if (auto transport = std::atomic_load(&mSctpTransport))
+		if (transport->state() == SctpTransport::State::Connected)
+			channel->open(transport);
 
-	if (!iceTransport) {
-		// RFC 5763: The endpoint that is the offerer MUST use the setup attribute value of
-		// setup:actpass.
-		// See https://tools.ietf.org/html/rfc5763#section-5
-		iceTransport = initIceTransport(Description::Role::ActPass);
-		processLocalDescription(iceTransport->getLocalDescription(Description::Type::Offer));
-		iceTransport->gatherLocalCandidates();
-	} else {
-		if (auto transport = std::atomic_load(&mSctpTransport))
-			if (transport->state() == SctpTransport::State::Connected)
-				channel->open(transport);
-	}
+	return channel;
+}
+
+shared_ptr<DataChannel> PeerConnection::createDataChannel(string label, string protocol,
+                                                          Reliability reliability) {
+	auto channel = addDataChannel(label, protocol, reliability);
+	setLocalDescription();
 	return channel;
 }
 
@@ -214,12 +234,11 @@ void PeerConnection::onDataChannel(
 	mDataChannelCallback = callback;
 }
 
-void PeerConnection::onLocalDescription(
-    std::function<void(const Description &description)> callback) {
+void PeerConnection::onLocalDescription(std::function<void(Description description)> callback) {
 	mLocalDescriptionCallback = callback;
 }
 
-void PeerConnection::onLocalCandidate(std::function<void(const Candidate &candidate)> callback) {
+void PeerConnection::onLocalCandidate(std::function<void(Candidate candidate)> callback) {
 	mLocalCandidateCallback = callback;
 }
 
@@ -233,35 +252,29 @@ void PeerConnection::onGatheringStateChange(std::function<void(GatheringState st
 
 bool PeerConnection::hasMedia() const {
 	auto local = localDescription();
-	auto remote = remoteDescription();
-	return (local && local->hasMedia()) || (remote && remote->hasMedia());
+	return local && local->hasAudioOrVideo();
 }
 
-void PeerConnection::sendMedia(const binary &packet) {
-	outgoingMedia(make_message(packet.begin(), packet.end(), Message::Binary));
-}
+std::shared_ptr<Track> PeerConnection::addTrack(Description::Media description) {
+	if (localDescription())
+		throw std::logic_error("Tracks must be created before local description");
 
-void PeerConnection::sendMedia(const byte *packet, size_t size) {
-	outgoingMedia(make_message(packet, packet + size, Message::Binary));
-}
+	if (auto it = mTracks.find(description.mid()); it != mTracks.end())
+		if (auto track = it->second.lock())
+			return track;
 
-void PeerConnection::onMedia(std::function<void(const binary &packet)> callback) {
-	mMediaCallback = callback;
+#if !RTC_ENABLE_MEDIA
+	if (mTracks.empty()) {
+		PLOG_WARNING << "Tracks will be inative (not compiled with SRTP support)";
+	}
+#endif
+	auto track = std::make_shared<Track>(std::move(description));
+	mTracks.emplace(std::make_pair(track->mid(), track));
+	return track;
 }
 
-void PeerConnection::outgoingMedia([[maybe_unused]] message_ptr message) {
-	if (!hasMedia())
-		throw std::runtime_error("PeerConnection has no media support");
-
-#if RTC_ENABLE_MEDIA
-	auto transport = std::atomic_load(&mDtlsTransport);
-	if (!transport)
-		throw std::runtime_error("PeerConnection is not open");
-
-	std::dynamic_pointer_cast<DtlsSrtpTransport>(transport)->sendMedia(message);
-#else
-	PLOG_WARNING << "Ignoring sent media (not compiled with SRTP support)";
-#endif
+void PeerConnection::onTrack(std::function<void(std::shared_ptr<Track>)> callback) {
+	mTrackCallback = callback;
 }
 
 shared_ptr<IceTransport> PeerConnection::initIceTransport(Description::Role role) {
@@ -342,7 +355,12 @@ shared_ptr<DtlsTransport> PeerConnection::initDtlsTransport() {
 
 			switch (state) {
 			case DtlsTransport::State::Connected:
-				initSctpTransport();
+				if (auto local = localDescription(); local && local->hasApplication())
+					initSctpTransport();
+				else
+					changeState(State::Connected);
+
+				openTracks();
 				break;
 			case DtlsTransport::State::Failed:
 				changeState(State::Failed);
@@ -396,7 +414,11 @@ shared_ptr<SctpTransport> PeerConnection::initSctpTransport() {
 		if (auto transport = std::atomic_load(&mSctpTransport))
 			return transport;
 
-		uint16_t sctpPort = remoteDescription()->sctpPort().value_or(DEFAULT_SCTP_PORT);
+		auto remote = remoteDescription();
+		if (!remote || !remote->application())
+			throw std::logic_error("Initializing SCTP transport without application description");
+
+		uint16_t sctpPort = remote->application()->sctpPort().value_or(DEFAULT_SCTP_PORT);
 		auto lower = std::atomic_load(&mDtlsTransport);
 		auto transport = std::make_shared<SctpTransport>(
 		    lower, sctpPort, weak_bind(&PeerConnection::forwardMessage, this, _1),
@@ -413,13 +435,7 @@ shared_ptr<SctpTransport> PeerConnection::initSctpTransport() {
 			    case SctpTransport::State::Failed:
 				    LOG_WARNING << "SCTP transport failed";
 				    remoteCloseDataChannels();
-#if RTC_ENABLE_MEDIA
-				    // Ignore SCTP failure if media is present
-				    if (!hasMedia())
-					    changeState(State::Failed);
-#else
 				    changeState(State::Failed);
-#endif
 				    break;
 			    case SctpTransport::State::Disconnected:
 				    remoteCloseDataChannels();
@@ -525,8 +541,56 @@ void PeerConnection::forwardMessage(message_ptr message) {
 }
 
 void PeerConnection::forwardMedia(message_ptr message) {
-	if (message)
-		mMediaCallback(*message);
+	if (!message)
+		return;
+
+	if (message->type == Message::Type::Control) {
+		std::shared_lock lock(mTracksMutex); // read-only
+		for (auto it = mTracks.begin(); it != mTracks.end(); ++it)
+			if (auto track = it->second.lock())
+				return track->incoming(message);
+
+		PLOG_WARNING << "No track available to receive control, dropping";
+		return;
+	}
+
+	unsigned int payloadType = message->stream;
+	std::optional<string> mid;
+	if (auto it = mMidFromPayloadType.find(payloadType); it != mMidFromPayloadType.end()) {
+		mid = it->second;
+	} else {
+		std::lock_guard lock(mLocalDescriptionMutex);
+		if (!mLocalDescription)
+			return;
+
+		for (int i = 0; i < mLocalDescription->mediaCount(); ++i) {
+			if (auto found = std::visit(
+			        rtc::overloaded{[&](Description::Application *) -> std::optional<string> {
+				                        return std::nullopt;
+			                        },
+			                        [&](Description::Media *media) -> std::optional<string> {
+				                        return media->hasPayloadType(payloadType)
+				                                   ? std::make_optional(media->mid())
+				                                   : nullopt;
+			                        }},
+			        mLocalDescription->media(i))) {
+
+				mMidFromPayloadType.emplace(payloadType, *found);
+				mid = *found;
+				break;
+			}
+		}
+	}
+
+	if (!mid) {
+		PLOG_WARNING << "Track not found for payload type " << payloadType << ", dropping";
+		return;
+	}
+
+	std::shared_lock lock(mTracksMutex); // read-only
+	if (auto it = mTracks.find(*mid); it != mTracks.end())
+		if (auto track = it->second.lock())
+			track->incoming(message);
 }
 
 void PeerConnection::forwardBufferedAmount(uint16_t stream, size_t amount) {
@@ -534,10 +598,9 @@ void PeerConnection::forwardBufferedAmount(uint16_t stream, size_t amount) {
 		channel->triggerBufferedAmount(amount);
 }
 
-shared_ptr<DataChannel> PeerConnection::emplaceDataChannel(Description::Role role,
-                                                           const string &label,
-                                                           const string &protocol,
-                                                           const Reliability &reliability) {
+shared_ptr<DataChannel> PeerConnection::emplaceDataChannel(Description::Role role, string label,
+                                                           string protocol,
+                                                           Reliability reliability) {
 	// The active side must use streams with even identifiers, whereas the passive side must use
 	// streams with odd identifiers.
 	// See https://tools.ietf.org/html/draft-ietf-rtcweb-data-protocol-09#section-6
@@ -548,8 +611,8 @@ shared_ptr<DataChannel> PeerConnection::emplaceDataChannel(Description::Role rol
 		if (stream >= 65535)
 			throw std::runtime_error("Too many DataChannels");
 	}
-	auto channel =
-	    std::make_shared<DataChannel>(shared_from_this(), stream, label, protocol, reliability);
+	auto channel = std::make_shared<DataChannel>(shared_from_this(), stream, std::move(label),
+	                                             std::move(protocol), std::move(reliability));
 	mDataChannels.emplace(std::make_pair(stream, channel));
 	return channel;
 }
@@ -606,28 +669,126 @@ void PeerConnection::remoteCloseDataChannels() {
 	iterateDataChannels([&](shared_ptr<DataChannel> channel) { channel->remoteClose(); });
 }
 
+void PeerConnection::incomingTrack(Description::Media description) {
+	std::unique_lock lock(mTracksMutex); // we are going to emplace
+#if !RTC_ENABLE_MEDIA
+	if (mTracks.empty()) {
+		PLOG_WARNING << "Tracks will be inative (not compiled with SRTP support)";
+	}
+#endif
+	if (mTracks.find(description.mid()) == mTracks.end()) {
+		auto track = std::make_shared<Track>(std::move(description));
+		mTracks.emplace(std::make_pair(track->mid(), track));
+		triggerTrack(std::move(track));
+	}
+}
+
+void PeerConnection::openTracks() {
+#if RTC_ENABLE_MEDIA
+	if (auto transport = std::atomic_load(&mDtlsTransport)) {
+		auto srtpTransport = std::reinterpret_pointer_cast<DtlsSrtpTransport>(transport);
+		std::shared_lock lock(mTracksMutex); // read-only
+		for (auto it = mTracks.begin(); it != mTracks.end(); ++it)
+			if (auto track = it->second.lock())
+				track->open(srtpTransport);
+	}
+#endif
+}
+
+
 void PeerConnection::processLocalDescription(Description description) {
-	std::optional<uint16_t> remoteSctpPort;
-	std::optional<string> remoteDataMid;
+	int activeMediaCount = 0;
+
 	if (auto remote = remoteDescription()) {
-		remoteDataMid = remote->dataMid();
-	    remoteSctpPort = remote->sctpPort();
-	}
+		// Reciprocate remote description
+		for (int i = 0; i < remote->mediaCount(); ++i)
+			std::visit( // reciprocate each media
+			    rtc::overloaded{
+			        [&](Description::Application *app) {
+				        auto reciprocated = app->reciprocate();
+				        reciprocated.hintSctpPort(DEFAULT_SCTP_PORT);
+				        reciprocated.setMaxMessageSize(LOCAL_MAX_MESSAGE_SIZE);
+				        ++activeMediaCount;
+
+				        PLOG_DEBUG << "Reciprocating application in local description, mid=\""
+				                   << reciprocated.mid() << "\"";
+
+				        description.addMedia(std::move(reciprocated));
+			        },
+			        [&](Description::Media *media) {
+				        auto reciprocated = media->reciprocate();
+#if RTC_ENABLE_MEDIA
+				        if (reciprocated.direction() != Description::Direction::Inactive)
+					        ++activeMediaCount;
+#else
+				        // No media support, mark as inactive
+				        reciprocated.setDirection(Description::Direction::Inactive);
+#endif
+				        incomingTrack(reciprocated);
 
-	auto certificate = mCertificate.get(); // wait for certificate if not ready
+				        PLOG_DEBUG
+				            << "Reciprocating media in local description, mid=\""
+				            << reciprocated.mid() << "\", active=" << std::boolalpha
+				            << (reciprocated.direction() != Description::Direction::Inactive);
 
-	{
-		std::lock_guard lock(mLocalDescriptionMutex);
-		mLocalDescription.emplace(std::move(description));
-		if (remoteDataMid)
-			mLocalDescription->setDataMid(*remoteDataMid);
+				        description.addMedia(std::move(reciprocated));
+			        },
+			    },
+			    remote->media(i));
+	} else {
+		// Add application for data channels
+		{
+			std::shared_lock lock(mDataChannelsMutex);
+			if (!mDataChannels.empty()) {
+				Description::Application app("data");
+				app.setSctpPort(DEFAULT_SCTP_PORT);
+				app.setMaxMessageSize(LOCAL_MAX_MESSAGE_SIZE);
+				++activeMediaCount;
+
+				PLOG_DEBUG << "Adding application to local description, mid=\"" << app.mid()
+				           << "\"";
+
+				description.addMedia(std::move(app));
+			}
+		}
+
+		// Add media for local tracks
+		{
+			std::shared_lock lock(mTracksMutex);
+			for (auto it = mTracks.begin(); it != mTracks.end(); ++it) {
+				if (auto track = it->second.lock()) {
+					auto media = track->description();
+#if RTC_ENABLE_MEDIA
+					if (media.direction() != Description::Direction::Inactive)
+						++activeMediaCount;
+#else
+					// No media support, mark as inactive
+					media.setDirection(Description::Direction::Inactive);
+#endif
+					PLOG_DEBUG << "Adding media to local description, mid=\"" << media.mid()
+					           << "\", active=" << std::boolalpha
+					           << (media.direction() != Description::Direction::Inactive);
 
-		mLocalDescription->setFingerprint(certificate->fingerprint());
-		mLocalDescription->setSctpPort(remoteSctpPort.value_or(DEFAULT_SCTP_PORT));
-		mLocalDescription->setMaxMessageSize(LOCAL_MAX_MESSAGE_SIZE);
+					description.addMedia(std::move(media));
+				}
+			}
+		}
 	}
 
-	mProcessor->enqueue([this]() { mLocalDescriptionCallback(*mLocalDescription); });
+	// There must be at least one active media to negociate
+	if (activeMediaCount == 0)
+		throw std::runtime_error("Nothing to negociate");
+
+	// Set local fingerprint (wait for certificate if necessary)
+	description.setFingerprint(mCertificate.get()->fingerprint());
+
+	std::lock_guard lock(mLocalDescriptionMutex);
+	mLocalDescription.emplace(std::move(description));
+
+	mProcessor->enqueue([this, description = *mLocalDescription]() {
+		PLOG_VERBOSE << "Issuing local description: " << description;
+		mLocalDescriptionCallback(std::move(description));
+	});
 }
 
 void PeerConnection::processLocalCandidate(Candidate candidate) {
@@ -637,8 +798,10 @@ void PeerConnection::processLocalCandidate(Candidate candidate) {
 
 	mLocalDescription->addCandidate(candidate);
 
-	mProcessor->enqueue(
-	    [this, candidate = std::move(candidate)]() { mLocalCandidateCallback(candidate); });
+	mProcessor->enqueue([this, candidate = std::move(candidate)]() {
+		PLOG_VERBOSE << "Issuing local candidate: " << candidate;
+		mLocalCandidateCallback(std::move(candidate));
+	});
 }
 
 void PeerConnection::triggerDataChannel(weak_ptr<DataChannel> weakDataChannel) {
@@ -650,6 +813,10 @@ void PeerConnection::triggerDataChannel(weak_ptr<DataChannel> weakDataChannel) {
 	    [this, dataChannel = std::move(dataChannel)]() { mDataChannelCallback(dataChannel); });
 }
 
+void PeerConnection::triggerTrack(std::shared_ptr<Track> track) {
+	mProcessor->enqueue([this, track = std::move(track)]() { mTrackCallback(track); });
+}
+
 bool PeerConnection::changeState(State state) {
 	State current;
 	do {

+ 426 - 0
src/rtcp.cpp

@@ -0,0 +1,426 @@
+/**
+ * Copyright (c) 2020 Staz M
+ * Copyright (c) 2020 Paul-Louis Ageneau
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "rtcp.hpp"
+
+#include <cmath>
+#include <utility>
+
+#ifdef _WIN32
+#include <winsock2.h>
+#else
+#include <arpa/inet.h>
+#endif
+
+#ifndef htonll
+#define htonll(x)                                                                                  \
+	((uint64_t)htonl(((uint64_t)(x)&0xFFFFFFFF) << 32) | (uint64_t)htonl((uint64_t)(x) >> 32))
+#endif
+#ifndef ntohll
+#define ntohll(x) htonll(x)
+#endif
+
+namespace rtc {
+
+#pragma pack(push, 1)
+
+struct RTP {
+private:
+	uint8_t _first;
+	uint8_t _payloadType;
+	uint16_t _seqNumber;
+	uint32_t _timestamp;
+
+public:
+	SSRC ssrc;
+	SSRC csrc[16];
+
+	inline uint8_t version() const { return _first >> 6; }
+	inline bool padding() const { return (_first >> 5) & 0x01; }
+	inline uint8_t csrcCount() const { return _first & 0x0F; }
+	inline uint8_t payloadType() const { return _payloadType; }
+	inline uint16_t seqNumber() const { return ntohs(_seqNumber); }
+	inline uint32_t timestamp() const { return ntohl(_timestamp); }
+};
+
+struct RTCP_ReportBlock {
+	SSRC ssrc;
+
+private:
+	uint32_t _fractionLostAndPacketsLost; // fraction lost is 8-bit, packets lost is 24-bit
+	uint16_t _seqNoCycles;
+	uint16_t _highestSeqNo;
+	uint32_t _jitter;
+	uint32_t _lastReport;
+	uint32_t _delaySinceLastReport;
+
+public:
+	inline void preparePacket(SSRC ssrc, [[maybe_unused]] unsigned int packetsLost,
+	                          [[maybe_unused]] unsigned int totalPackets, uint16_t highestSeqNo,
+	                          uint16_t seqNoCycles, uint32_t jitter, uint64_t lastSR_NTP,
+	                          uint64_t lastSR_DELAY) {
+		setSeqNo(highestSeqNo, seqNoCycles);
+		setJitter(jitter);
+		setSSRC(ssrc);
+
+		// Middle 32 bits of NTP Timestamp
+		//		  this->lastReport = lastSR_NTP >> 16u;
+		setNTPOfSR(uint32_t(lastSR_NTP));
+		setDelaySinceSR(uint32_t(lastSR_DELAY));
+
+		// The delay, expressed in units of 1/65536 seconds
+		//		  this->delaySinceLastReport = lastSR_DELAY;
+	}
+
+	inline void setSSRC(SSRC ssrc) { this->ssrc = htonl(ssrc); }
+	inline SSRC getSSRC() const { return ntohl(ssrc); }
+
+	inline void setPacketsLost([[maybe_unused]] unsigned int packetsLost,
+	                           [[maybe_unused]] unsigned int totalPackets) {
+		// TODO Implement loss percentages.
+		_fractionLostAndPacketsLost = 0;
+	}
+	inline unsigned int getLossPercentage() const {
+		// TODO Implement loss percentages.
+		return 0;
+	}
+	inline unsigned int getPacketLostCount() const {
+		// TODO Implement total packets lost.
+		return 0;
+	}
+
+	inline uint16_t seqNoCycles() const { return ntohs(_seqNoCycles); }
+	inline uint16_t highestSeqNo() const { return ntohs(_highestSeqNo); }
+	inline uint32_t jitter() const { return ntohl(_jitter); }
+
+	inline void setSeqNo(uint16_t highestSeqNo, uint16_t seqNoCycles) {
+		_highestSeqNo = htons(highestSeqNo);
+		_seqNoCycles = htons(seqNoCycles);
+	}
+
+	inline void setJitter(uint32_t jitter) { _jitter = htonl(jitter); }
+
+	inline void setNTPOfSR(uint32_t ntp) { _lastReport = htonl(ntp >> 16u); }
+	inline uint32_t getNTPOfSR() const { return ntohl(_lastReport) << 16u; }
+
+	inline void setDelaySinceSR(uint32_t sr) {
+		// The delay, expressed in units of 1/65536 seconds
+		_delaySinceLastReport = htonl(sr);
+	}
+	inline uint32_t getDelaySinceSR() const { return ntohl(_delaySinceLastReport); }
+
+	inline void log() const {
+		PLOG_DEBUG << "RTCP report block: "
+		           << "ssrc="
+		           << ntohl(ssrc)
+		           // TODO: Implement these reports
+		           //	<< ", fractionLost=" << fractionLost
+		           //	<< ", packetsLost=" << packetsLost
+		           << ", highestSeqNo=" << highestSeqNo() << ", seqNoCycles=" << seqNoCycles()
+		           << ", jitter=" << jitter() << ", lastSR=" << getNTPOfSR()
+		           << ", lastSRDelay=" << getDelaySinceSR();
+	}
+};
+
+struct RTCP_HEADER {
+private:
+	uint8_t _first;
+	uint8_t _payloadType;
+	uint16_t _length;
+
+public:
+	inline uint8_t version() const { return _first >> 6; }
+	inline bool padding() const { return (_first >> 5) & 0x01; }
+	inline uint8_t reportCount() const { return _first & 0x0F; }
+	inline uint8_t payloadType() const { return _payloadType; }
+	inline uint16_t length() const { return ntohs(_length); }
+
+	inline void setPayloadType(uint8_t type) { _payloadType = type; }
+	inline void setReportCount(uint8_t count) { _first = (_first & 0xF0) | (count & 0x0F); }
+	inline void setLength(uint16_t length) { _length = htons(length); }
+
+	inline void prepareHeader(uint8_t payloadType, uint8_t reportCount, uint16_t length) {
+		_first = 0x02 << 6; // version 2, no padding
+		setReportCount(reportCount);
+		setPayloadType(payloadType);
+		setLength(length);
+	}
+
+	inline void log() const {
+		PLOG_DEBUG << "RTCP header: "
+		           << "version=" << unsigned(version()) << ", padding=" << padding()
+		           << ", reportCount=" << unsigned(reportCount())
+		           << ", payloadType=" << unsigned(payloadType()) << ", length=" << length();
+	}
+};
+
+struct RTCP_SR {
+	RTCP_HEADER header;
+	SSRC senderSSRC;
+
+private:
+	uint64_t _ntpTimestamp;
+	uint32_t _rtpTimestamp;
+	uint32_t _packetCount;
+	uint32_t _octetCount;
+
+	RTCP_ReportBlock _reportBlocks;
+
+public:
+	inline void preparePacket(SSRC senderSSRC, uint8_t reportCount) {
+		unsigned int length =
+		    ((sizeof(header) + 24 + reportCount * sizeof(RTCP_ReportBlock)) / 4) - 1;
+		header.prepareHeader(200, reportCount, uint16_t(length));
+		this->senderSSRC = htonl(senderSSRC);
+	}
+
+	inline RTCP_ReportBlock *getReportBlock(int num) { return &_reportBlocks + num; }
+	inline const RTCP_ReportBlock *getReportBlock(int num) const { return &_reportBlocks + num; }
+
+	[[nodiscard]] inline size_t getSize() const {
+		// "length" in packet is one less than the number of 32 bit words in the packet.
+		return sizeof(uint32_t) * (1 + size_t(header.length()));
+	}
+
+	inline uint32_t ntpTimestamp() const { return ntohll(_ntpTimestamp); }
+	inline uint32_t rtpTimestamp() const { return ntohl(_rtpTimestamp); }
+	inline uint32_t packetCount() const { return ntohl(_packetCount); }
+	inline uint32_t octetCount() const { return ntohl(_octetCount); }
+
+	inline void setNtpTimestamp(uint32_t ts) { _ntpTimestamp = htonll(ts); }
+	inline void setRtpTimestamp(uint32_t ts) { _rtpTimestamp = htonl(ts); }
+
+	inline void log() const {
+		header.log();
+		PLOG_DEBUG << "RTCP SR: "
+		           << " SSRC=" << ntohl(senderSSRC) << ", NTP_TS=" << ntpTimestamp()
+		           << ", RTP_TS=" << rtpTimestamp() << ", packetCount=" << packetCount()
+		           << ", octetCount=" << octetCount();
+
+		for (unsigned i = 0; i < unsigned(header.reportCount()); i++) {
+			getReportBlock(i)->log();
+		}
+	}
+};
+
+struct RTCP_RR {
+	RTCP_HEADER header;
+	SSRC senderSSRC;
+
+private:
+	RTCP_ReportBlock _reportBlocks;
+
+public:
+	inline RTCP_ReportBlock *getReportBlock(int num) { return &_reportBlocks + num; }
+	inline const RTCP_ReportBlock *getReportBlock(int num) const { return &_reportBlocks + num; }
+
+	inline SSRC getSenderSSRC() const { return ntohl(senderSSRC); }
+	inline void setSenderSSRC(SSRC ssrc) { this->senderSSRC = htonl(ssrc); }
+
+	[[nodiscard]] inline size_t getSize() const {
+		// "length" in packet is one less than the number of 32 bit words in the packet.
+		return sizeof(uint32_t) * (1 + size_t(header.length()));
+	}
+
+	inline void preparePacket(SSRC senderSSRC, uint8_t reportCount) {
+		// "length" in packet is one less than the number of 32 bit words in the packet.
+		size_t length = (sizeWithReportBlocks(reportCount) / 4) - 1;
+		header.prepareHeader(201, reportCount, uint16_t(length));
+		this->senderSSRC = htonl(senderSSRC);
+	}
+
+	inline static size_t sizeWithReportBlocks(uint8_t reportCount) {
+		return sizeof(header) + 4 + size_t(reportCount) * sizeof(RTCP_ReportBlock);
+	}
+
+	inline void log() const {
+		header.log();
+		PLOG_DEBUG << "RTCP RR: "
+		           << " SSRC=" << ntohl(senderSSRC);
+
+		for (unsigned i = 0; i < unsigned(header.reportCount()); i++) {
+			getReportBlock(i)->log();
+		}
+	}
+};
+
+struct RTCP_REMB {
+	RTCP_HEADER header;
+	SSRC senderSSRC;
+	SSRC mediaSourceSSRC;
+
+	// Unique identifier
+	const char id[4] = {'R', 'E', 'M', 'B'};
+
+	// Num SSRC, Br Exp, Br Mantissa (bit mask)
+	uint32_t bitrate;
+
+	SSRC ssrc[1];
+
+	[[nodiscard]] inline size_t getSize() const {
+		// "length" in packet is one less than the number of 32 bit words in the packet.
+		return sizeof(uint32_t) * (1 + size_t(header.length()));
+	}
+
+	inline void preparePacket(SSRC senderSSRC, unsigned int numSSRC, unsigned int bitrate) {
+		// Report Count becomes the format here.
+		header.prepareHeader(206, 15, 0);
+
+		// Always zero.
+		mediaSourceSSRC = 0;
+
+		this->senderSSRC = htonl(senderSSRC);
+		setBitrate(numSSRC, bitrate);
+	}
+
+	inline void setBitrate(unsigned int numSSRC, unsigned int bitrate) {
+		unsigned int exp = 0;
+		while (bitrate > pow(2, 18) - 1) {
+			exp++;
+			bitrate /= 2;
+		}
+
+		// "length" in packet is one less than the number of 32 bit words in the packet.
+		header.setLength(uint16_t(((sizeof(header) + 4 * 2 + 4 + 4) / 4) - 1 + numSSRC));
+
+		this->bitrate = htonl((numSSRC << (32u - 8u)) | (exp << (32u - 8u - 6u)) | bitrate);
+	}
+
+	// TODO Make this work
+	//	  uint64_t getBitrate() const{
+	//		  uint32_t ntohed = ntohl(this->bitrate);
+	//		  uint64_t bitrate = ntohed & (unsigned int)(pow(2, 18)-1);
+	//		  unsigned int exp = ntohed & ((unsigned int)( (pow(2, 6)-1)) << (32u-8u-6u));
+	//		  return bitrate * pow(2,exp);
+	//	  }
+	//
+	//	  uint8_t getNumSSRCS() const {
+	//		  return ntohl(this->bitrate) & (((unsigned int) pow(2,8)-1) << (32u-8u));
+	//	  }
+
+	inline void setSSRC(uint8_t iterator, SSRC ssrc) { this->ssrc[iterator] = htonl(ssrc); }
+
+	inline void log() const {
+		header.log();
+		PLOG_DEBUG << "RTCP REMB: "
+		           << " SSRC=" << ntohl(senderSSRC);
+	}
+
+	static unsigned int sizeWithSSRCs(int numSSRC) {
+		return (sizeof(header) + 4 * 2 + 4 + 4) + sizeof(SSRC) * numSSRC;
+	}
+};
+
+#pragma pack(pop)
+
+void RtcpSession::onOutgoing(std::function<void(rtc::message_ptr)> cb) { mTxCallback = cb; }
+
+std::optional<rtc::message_ptr> RtcpSession::incoming(rtc::message_ptr ptr) {
+	if (ptr->type == rtc::Message::Type::Binary) {
+		auto rtp = reinterpret_cast<const RTP *>(ptr->data());
+
+		// https://tools.ietf.org/html/rfc3550#appendix-A.1
+		if (rtp->version() != 2) {
+			PLOG_WARNING << "RTP packet is not version 2";
+
+			return std::nullopt;
+		}
+		if (rtp->payloadType() == 201 || rtp->payloadType() == 200) {
+			PLOG_WARNING << "RTP packet has a payload type indicating RR/SR";
+
+			return std::nullopt;
+		}
+
+		// TODO Implement the padding bit
+		if (rtp->padding()) {
+			PLOG_WARNING << "Padding processing not implemented";
+		}
+
+		mSsrc = ntohl(rtp->ssrc);
+
+		uint32_t seqNo = rtp->seqNumber();
+		// uint32_t rtpTS = rtp->getTS();
+
+		if (mGreatestSeqNo < seqNo)
+			mGreatestSeqNo = seqNo;
+
+		return ptr;
+	}
+
+	assert(ptr->type == rtc::Message::Type::Control);
+	auto rr = reinterpret_cast<const RTCP_RR *>(ptr->data());
+	if (rr->header.payloadType() == 201) {
+		// RR
+		mSsrc = rr->getSenderSSRC();
+		rr->log();
+	} else if (rr->header.payloadType() == 200) {
+		// SR
+		mSsrc = rr->getSenderSSRC();
+		auto sr = reinterpret_cast<const RTCP_SR *>(ptr->data());
+		mSyncRTPTS = sr->rtpTimestamp();
+		mSyncNTPTS = sr->ntpTimestamp();
+		sr->log();
+
+		// TODO For the time being, we will send RR's/REMB's when we get an SR
+		pushRR(0);
+		if (mRequestedBitrate > 0)
+			pushREMB(mRequestedBitrate);
+	}
+	return std::nullopt;
+}
+
+void RtcpSession::requestBitrate(unsigned int newBitrate) {
+	mRequestedBitrate = newBitrate;
+
+	PLOG_DEBUG << "[GOOG-REMB] Requesting bitrate: " << newBitrate << std::endl;
+	pushREMB(newBitrate);
+}
+
+void RtcpSession::pushREMB(unsigned int bitrate) {
+	rtc::message_ptr msg =
+	    rtc::make_message(RTCP_REMB::sizeWithSSRCs(1), rtc::Message::Type::Control);
+	auto remb = reinterpret_cast<RTCP_REMB *>(msg->data());
+	remb->preparePacket(mSsrc, 1, bitrate);
+	remb->setSSRC(0, mSsrc);
+	remb->log();
+
+	tx(msg);
+}
+
+void RtcpSession::pushRR(unsigned int lastSR_delay) {
+	auto msg = rtc::make_message(RTCP_RR::sizeWithReportBlocks(1), rtc::Message::Type::Control);
+	auto rr = reinterpret_cast<RTCP_RR *>(msg->data());
+	rr->preparePacket(mSsrc, 1);
+	rr->getReportBlock(0)->preparePacket(mSsrc, 0, 0, mGreatestSeqNo, 0, 0, mSyncNTPTS,
+	                                     lastSR_delay);
+	rr->log();
+
+	tx(msg);
+}
+
+void RtcpSession::tx(message_ptr msg) {
+	try {
+		mTxCallback(msg);
+	} catch (const std::exception &e) {
+		LOG_DEBUG << "RTCP tx failed: " << e.what();
+	}
+}
+
+} // namespace rtc
+

+ 25 - 2
src/sctptransport.cpp

@@ -600,7 +600,7 @@ void SctpTransport::processNotification(const union sctp_notification *notify, s
 	}
 
 	auto type = notify->sn_header.sn_type;
-	PLOG_VERBOSE << "Process notification, type=" << type;
+	PLOG_VERBOSE << "Processing notification, type=" << type;
 
 	switch (type) {
 	case SCTP_ASSOC_CHANGE: {
@@ -622,7 +622,8 @@ void SctpTransport::processNotification(const union sctp_notification *notify, s
 	}
 
 	case SCTP_SENDER_DRY_EVENT: {
-		// It not should be necessary since the send callback should have been called already,
+		PLOG_VERBOSE << "SCTP dry event";
+		// It should not be necessary since the send callback should have been called already,
 		// but to be sure, let's try to send now.
 		safeFlush();
 		break;
@@ -633,6 +634,28 @@ void SctpTransport::processNotification(const union sctp_notification *notify, s
 		const int count = (reset_event.strreset_length - sizeof(reset_event)) / sizeof(uint16_t);
 		const uint16_t flags = reset_event.strreset_flags;
 
+		IF_PLOG(plog::verbose) {
+			std::ostringstream desc;
+			desc << "flags=";
+			if (flags & SCTP_STREAM_RESET_OUTGOING_SSN && flags & SCTP_STREAM_RESET_INCOMING_SSN)
+				desc << "outgoing|incoming";
+			else if (flags & SCTP_STREAM_RESET_OUTGOING_SSN)
+				desc << "outgoing";
+			else if (flags & SCTP_STREAM_RESET_INCOMING_SSN)
+				desc << "incoming";
+			else
+				desc << "0";
+
+			desc << ", streams=[";
+			for (int i = 0; i < count; ++i) {
+				uint16_t streamId = reset_event.strreset_stream_list[i];
+				desc << (i != 0 ? "," : "") << streamId;
+			}
+			desc << "]";
+
+			PLOG_VERBOSE << "SCTP reset event, " << desc.str();
+		}
+
 		if (flags & SCTP_STREAM_RESET_OUTGOING_SSN) {
 			for (int i = 0; i < count; ++i) {
 				uint16_t streamId = reset_event.strreset_stream_list[i];

+ 31 - 25
src/tcptransport.cpp

@@ -212,36 +212,42 @@ void TcpTransport::connect(const sockaddr *addr, socklen_t addrlen) {
 			throw std::runtime_error(msg.str());
 		}
 
-		fd_set writefds;
-		FD_ZERO(&writefds);
-		FD_SET(mSock, &writefds);
-		struct timeval tv;
-		tv.tv_sec = 10; // TODO
-		tv.tv_usec = 0;
-		ret = ::select(SOCKET_TO_INT(mSock) + 1, NULL, &writefds, NULL, &tv);
-
-		if (ret < 0)
-			throw std::runtime_error("Failed to wait for socket connection");
+		while (true) {
+			fd_set writefds;
+			FD_ZERO(&writefds);
+			FD_SET(mSock, &writefds);
+			struct timeval tv;
+			tv.tv_sec = 10; // TODO: Make the timeout configurable
+			tv.tv_usec = 0;
+			ret = ::select(SOCKET_TO_INT(mSock) + 1, NULL, &writefds, NULL, &tv);
 
-		if (ret == 0) {
-			std::ostringstream msg;
-			msg << "TCP connection to " << node << ":" << serv << " timed out";
-			throw std::runtime_error(msg.str());
-		}
+			if (ret < 0) {
+				if (sockerrno == SEINTR || sockerrno == SEAGAIN) // interrupted
+					continue;
+				else
+					throw std::runtime_error("Failed to wait for socket connection");
+			}
 
-		int error = 0;
-		socklen_t errorlen = sizeof(error);
-		if (::getsockopt(mSock, SOL_SOCKET, SO_ERROR, (char *)&error, &errorlen) != 0)
-			throw std::runtime_error("Failed to get socket error code");
+			if (ret == 0) {
+				std::ostringstream msg;
+				msg << "TCP connection to " << node << ":" << serv << " timed out";
+				throw std::runtime_error(msg.str());
+			}
 
-		if (error != 0) {
-			std::ostringstream msg;
-			msg << "TCP connection to " << node << ":" << serv << " failed, errno=" << error;
-			throw std::runtime_error(msg.str());
-		}
+			int error = 0;
+			socklen_t errorlen = sizeof(error);
+			if (::getsockopt(mSock, SOL_SOCKET, SO_ERROR, (char *)&error, &errorlen) != 0)
+				throw std::runtime_error("Failed to get socket error code");
 
-		PLOG_DEBUG << "TCP connection to " << node << ":" << serv << " succeeded";
+			if (error != 0) {
+				std::ostringstream msg;
+				msg << "TCP connection to " << node << ":" << serv << " failed, errno=" << error;
+				throw std::runtime_error(msg.str());
+			}
 
+			PLOG_DEBUG << "TCP connection to " << node << ":" << serv << " succeeded";
+			break;
+		}
 	} catch (...) {
 		if (mSock != INVALID_SOCKET) {
 			::closesocket(mSock);

+ 1 - 2
src/tls.cpp

@@ -85,10 +85,9 @@ void init() {
 	static bool done = false;
 
 	std::lock_guard lock(mutex);
-	if (!done) {
+	if (!std::exchange(done, true)) {
 		OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, nullptr);
 		OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, nullptr);
-		done = true;
 	}
 }
 

+ 148 - 0
src/track.cpp

@@ -0,0 +1,148 @@
+/**
+ * Copyright (c) 2020 Paul-Louis Ageneau
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "track.hpp"
+#include "dtlssrtptransport.hpp"
+#include "include.hpp"
+
+namespace rtc {
+
+using std::shared_ptr;
+using std::weak_ptr;
+
+Track::Track(Description::Media description)
+    : mMediaDescription(std::move(description)), mRecvQueue(RECV_QUEUE_LIMIT, message_size_func) {}
+
+string Track::mid() const { return mMediaDescription.mid(); }
+
+Description::Media Track::description() const { return mMediaDescription; }
+
+void Track::close() {
+	mIsClosed = true;
+	resetCallbacks();
+	setRtcpHandler(nullptr);
+}
+
+bool Track::send(message_variant data) { return outgoing(make_message(std::move(data))); }
+
+bool Track::send(const byte *data, size_t size) {
+	return outgoing(std::make_shared<Message>(data, data + size, Message::Binary));
+}
+
+std::optional<message_variant> Track::receive() {
+	if (!mRecvQueue.empty())
+		return to_variant(std::move(**mRecvQueue.pop()));
+
+	return nullopt;
+}
+
+bool Track::isOpen(void) const {
+#if RTC_ENABLE_MEDIA
+	return !mIsClosed && mDtlsSrtpTransport.lock();
+#else
+	return !mIsClosed;
+#endif
+}
+
+bool Track::isClosed(void) const { return mIsClosed; }
+
+size_t Track::maxMessageSize() const {
+	return 65535 - 12 - 4; // SRTP/UDP
+}
+
+size_t Track::availableAmount() const {
+	return mRecvQueue.amount();
+}
+
+#if RTC_ENABLE_MEDIA
+void Track::open(shared_ptr<DtlsSrtpTransport> transport) {
+	mDtlsSrtpTransport = transport;
+	triggerOpen();
+}
+#endif
+
+bool Track::outgoing(message_ptr message) {
+	auto direction = mMediaDescription.direction();
+	if (direction == Description::Direction::RecvOnly ||
+	    direction == Description::Direction::Inactive)
+		throw std::runtime_error("Track media direction does not allow sending");
+
+	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) {
+	if (!message)
+		return;
+
+	if (mRtcpHandler) {
+		auto opt = mRtcpHandler->incoming(message);
+		if (!opt)
+			return;
+
+		message = *opt;
+	}
+
+	auto direction = mMediaDescription.direction();
+	if ((direction == Description::Direction::SendOnly ||
+	     direction == Description::Direction::Inactive) &&
+	    message->type != Message::Control) {
+		PLOG_WARNING << "Track media direction does not allow reception, dropping";
+	}
+
+	// Tail drop if queue is full
+	if (mRecvQueue.full())
+		return;
+
+	mRecvQueue.push(message);
+	triggerAvailable(mRecvQueue.size());
+}
+
+void Track::setRtcpHandler(std::shared_ptr<RtcpHandler> handler) {
+	if (mRtcpHandler)
+		mRtcpHandler->onOutgoing(nullptr);
+
+	mRtcpHandler = std::move(handler);
+	if (mRtcpHandler) {
+		mRtcpHandler->onOutgoing([&]([[maybe_unused]] message_ptr message) {
+#if RTC_ENABLE_MEDIA
+			if (auto transport = mDtlsSrtpTransport.lock())
+				transport->sendMedia(message);
+#else
+			PLOG_WARNING << "Ignoring RTCP send (not compiled with SRTP support)";
+#endif
+		});
+	}
+}
+
+} // namespace rtc
+

+ 7 - 23
src/websocket.cpp

@@ -38,7 +38,8 @@ namespace rtc {
 using std::shared_ptr;
 
 WebSocket::WebSocket(std::optional<Configuration> config)
-    : mConfig(config ? std::move(*config) : Configuration()) {
+    : mConfig(config ? std::move(*config) : Configuration()),
+      mRecvQueue(RECV_QUEUE_LIMIT, message_size_func) {
 	PLOG_VERBOSE << "Creating WebSocket";
 }
 
@@ -100,16 +101,7 @@ void WebSocket::remoteClose() {
 	}
 }
 
-bool WebSocket::send(const std::variant<binary, string> &data) {
-	return std::visit(
-	    [&](const auto &d) {
-		    using T = std::decay_t<decltype(d)>;
-		    constexpr auto type = std::is_same_v<T, string> ? Message::String : Message::Binary;
-		    auto *b = reinterpret_cast<const byte *>(d.data());
-		    return outgoing(std::make_shared<Message>(b, b + d.size(), type));
-	    },
-	    data);
-}
+bool WebSocket::send(message_variant data) { return outgoing(make_message(std::move(data))); }
 
 bool WebSocket::isOpen() const { return mState == State::Open; }
 
@@ -117,19 +109,11 @@ bool WebSocket::isClosed() const { return mState == State::Closed; }
 
 size_t WebSocket::maxMessageSize() const { return DEFAULT_MAX_MESSAGE_SIZE; }
 
-std::optional<std::variant<binary, string>> WebSocket::receive() {
+std::optional<message_variant> WebSocket::receive() {
 	while (!mRecvQueue.empty()) {
 		auto message = *mRecvQueue.pop();
-		switch (message->type) {
-		case Message::String:
-			return std::make_optional(
-			    string(reinterpret_cast<const char *>(message->data()), message->size()));
-		case Message::Binary:
-			return std::make_optional(std::move(*message));
-		default:
-			// Ignore
-			break;
-		}
+		if (message->type != Message::Control)
+			return to_variant(std::move(*message));
 	}
 	return nullopt;
 }
@@ -337,6 +321,6 @@ void WebSocket::closeTransports() {
 	});
 }
 
-} // namespace rtc
+	} // namespace rtc
 
 #endif

+ 22 - 23
test/benchmark.cpp

@@ -48,20 +48,20 @@ size_t benchmark(milliseconds duration) {
 
 	auto pc2 = std::make_shared<PeerConnection>(config2);
 
-	pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](const Description &sdp) {
+	pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](Description sdp) {
 		auto pc2 = wpc2.lock();
 		if (!pc2)
 			return;
 		cout << "Description 1: " << sdp << endl;
-		pc2->setRemoteDescription(sdp);
+		pc2->setRemoteDescription(std::move(sdp));
 	});
 
-	pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](const Candidate &candidate) {
+	pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](Candidate candidate) {
 		auto pc2 = wpc2.lock();
 		if (!pc2)
 			return;
 		cout << "Candidate 1: " << candidate << endl;
-		pc2->addRemoteCandidate(candidate);
+		pc2->addRemoteCandidate(std::move(candidate));
 	});
 
 	pc1->onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
@@ -69,20 +69,20 @@ size_t benchmark(milliseconds duration) {
 		cout << "Gathering state 1: " << state << endl;
 	});
 
-	pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](const Description &sdp) {
+	pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](Description sdp) {
 		auto pc1 = wpc1.lock();
 		if (!pc1)
 			return;
 		cout << "Description 2: " << sdp << endl;
-		pc1->setRemoteDescription(sdp);
+		pc1->setRemoteDescription(std::move(sdp));
 	});
 
-	pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](const Candidate &candidate) {
+	pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](Candidate candidate) {
 		auto pc1 = wpc1.lock();
 		if (!pc1)
 			return;
 		cout << "Candidate 2: " << candidate << endl;
-		pc1->addRemoteCandidate(candidate);
+		pc1->addRemoteCandidate(std::move(candidate));
 	});
 
 	pc2->onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
@@ -99,21 +99,20 @@ size_t benchmark(milliseconds duration) {
 	steady_clock::time_point startTime, openTime, receivedTime, endTime;
 
 	shared_ptr<DataChannel> dc2;
-	pc2->onDataChannel(
-	    [&dc2, &receivedSize, &receivedTime](shared_ptr<DataChannel> dc) {
-		    dc->onMessage([&receivedTime, &receivedSize](const variant<binary, string> &message) {
-			    if (holds_alternative<binary>(message)) {
-				    const auto &bin = get<binary>(message);
-				    if (receivedSize == 0)
-					    receivedTime = steady_clock::now();
-				    receivedSize += bin.size();
-			    }
-		    });
-
-		    dc->onClosed([]() { cout << "DataChannel closed." << endl; });
-
-		    std::atomic_store(&dc2, dc);
-	    });
+	pc2->onDataChannel([&dc2, &receivedSize, &receivedTime](shared_ptr<DataChannel> dc) {
+		dc->onMessage([&receivedTime, &receivedSize](variant<binary, string> message) {
+			if (holds_alternative<binary>(message)) {
+				const auto &bin = get<binary>(message);
+				if (receivedSize == 0)
+					receivedTime = steady_clock::now();
+				receivedSize += bin.size();
+			}
+		});
+
+		dc->onClosed([]() { cout << "DataChannel closed." << endl; });
+
+		std::atomic_store(&dc2, dc);
+	});
 
 	startTime = steady_clock::now();
 	auto dc1 = pc1->createDataChannel("benchmark");

+ 6 - 6
test/capi.cpp → test/capi_connectivity.cpp

@@ -37,8 +37,8 @@ typedef struct {
 	bool connected;
 } Peer;
 
-Peer *peer1 = NULL;
-Peer *peer2 = NULL;
+static Peer *peer1 = NULL;
+static Peer *peer2 = NULL;
 
 static void descriptionCallback(const char *sdp, const char *type, void *ptr) {
 	Peer *peer = (Peer *)ptr;
@@ -132,7 +132,7 @@ static void deletePeer(Peer *peer) {
 	}
 }
 
-int test_capi_main() {
+int test_capi_connectivity_main() {
 	int attempts;
 
 	rtcInitLogger(RTC_LOG_DEBUG, nullptr);
@@ -170,7 +170,7 @@ int test_capi_main() {
 	rtcSetMessageCallback(peer1->dc, messageCallback);
 
 	attempts = 10;
-	while (!peer2->connected && !peer1->connected && attempts--)
+	while ((!peer2->connected || !peer1->connected) && attempts--)
 		sleep(1);
 
 	if (peer1->state != RTC_CONNECTED || peer2->state != RTC_CONNECTED) {
@@ -213,7 +213,7 @@ error:
 
 #include <stdexcept>
 
-void test_capi() {
-	if (test_capi_main())
+void test_capi_connectivity() {
+	if (test_capi_connectivity_main())
 		throw std::runtime_error("Connection failed");
 }

+ 198 - 0
test/capi_track.cpp

@@ -0,0 +1,198 @@
+/**
+ * Copyright (c) 2020 Paul-Louis Ageneau
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <rtc/rtc.h>
+
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+
+#ifdef _WIN32
+#include <windows.h>
+static void sleep(unsigned int secs) { Sleep(secs * 1000); }
+#else
+#include <unistd.h> // for sleep
+#endif
+
+typedef struct {
+	rtcState state;
+	rtcGatheringState gatheringState;
+	int pc;
+	int tr;
+	bool connected;
+} Peer;
+
+static Peer *peer1 = NULL;
+static Peer *peer2 = NULL;
+
+static const char *mediaDescription = "video 9 UDP/TLS/RTP/SAVPF\r\n"
+                                      "a=mid:video\r\n";
+
+static void descriptionCallback(const char *sdp, const char *type, void *ptr) {
+	Peer *peer = (Peer *)ptr;
+	printf("Description %d:\n%s\n", peer == peer1 ? 1 : 2, sdp);
+	Peer *other = peer == peer1 ? peer2 : peer1;
+	rtcSetRemoteDescription(other->pc, sdp, type);
+}
+
+static void candidateCallback(const char *cand, const char *mid, void *ptr) {
+	Peer *peer = (Peer *)ptr;
+	printf("Candidate %d: %s\n", peer == peer1 ? 1 : 2, cand);
+	Peer *other = peer == peer1 ? peer2 : peer1;
+	rtcAddRemoteCandidate(other->pc, cand, mid);
+}
+
+static void stateChangeCallback(rtcState state, void *ptr) {
+	Peer *peer = (Peer *)ptr;
+	peer->state = state;
+	printf("State %d: %d\n", peer == peer1 ? 1 : 2, (int)state);
+}
+
+static void gatheringStateCallback(rtcGatheringState state, void *ptr) {
+	Peer *peer = (Peer *)ptr;
+	peer->gatheringState = state;
+	printf("Gathering state %d: %d\n", peer == peer1 ? 1 : 2, (int)state);
+}
+
+static void openCallback(void *ptr) {
+	Peer *peer = (Peer *)ptr;
+	peer->connected = true;
+	printf("Track %d: Open\n", peer == peer1 ? 1 : 2);
+}
+
+static void closedCallback(void *ptr) {
+	Peer *peer = (Peer *)ptr;
+	peer->connected = false;
+}
+
+static void trackCallback(int tr, void *ptr) {
+	Peer *peer = (Peer *)ptr;
+	peer->tr = tr;
+	peer->connected = true;
+	rtcSetClosedCallback(tr, closedCallback);
+
+	char buffer[1024];
+	if (rtcGetTrackDescription(tr, buffer, 1024) >= 0)
+		printf("Track %d: Received with media description: \n%s\n", peer == peer1 ? 1 : 2, buffer);
+}
+
+static Peer *createPeer(const rtcConfiguration *config) {
+	Peer *peer = (Peer *)malloc(sizeof(Peer));
+	if (!peer)
+		return nullptr;
+	memset(peer, 0, sizeof(Peer));
+
+	// Create peer connection
+	peer->pc = rtcCreatePeerConnection(config);
+	rtcSetUserPointer(peer->pc, peer);
+	rtcSetTrackCallback(peer->pc, trackCallback);
+	rtcSetLocalDescriptionCallback(peer->pc, descriptionCallback);
+	rtcSetLocalCandidateCallback(peer->pc, candidateCallback);
+	rtcSetStateChangeCallback(peer->pc, stateChangeCallback);
+	rtcSetGatheringStateChangeCallback(peer->pc, gatheringStateCallback);
+
+	return peer;
+}
+
+static void deletePeer(Peer *peer) {
+	if (peer) {
+		if (peer->tr)
+			rtcDeleteTrack(peer->tr);
+		if (peer->pc)
+			rtcDeletePeerConnection(peer->pc);
+		free(peer);
+	}
+}
+
+int test_capi_track_main() {
+	int attempts;
+
+	rtcInitLogger(RTC_LOG_DEBUG, nullptr);
+
+	// Create peer 1
+	rtcConfiguration config1;
+	memset(&config1, 0, sizeof(config1));
+	// STUN server example
+	// const char *iceServers[1] = {"stun:stun.l.google.com:19302"};
+	// config1.iceServers = iceServers;
+	// config1.iceServersCount = 1;
+
+	peer1 = createPeer(&config1);
+	if (!peer1)
+		goto error;
+
+	// Create peer 2
+	rtcConfiguration config2;
+	memset(&config2, 0, sizeof(config2));
+	// STUN server example
+	// config2.iceServers = iceServers;
+	// config2.iceServersCount = 1;
+	// Port range example
+	config2.portRangeBegin = 5000;
+	config2.portRangeEnd = 6000;
+
+	peer2 = createPeer(&config2);
+	if (!peer2)
+		goto error;
+
+	// Peer 1: Create track
+	peer1->tr = rtcAddTrack(peer1->pc, mediaDescription);
+	rtcSetOpenCallback(peer1->tr, openCallback);
+	rtcSetClosedCallback(peer1->tr, closedCallback);
+
+	// Initiate the handshake
+	rtcSetLocalDescription(peer1->pc);
+
+	attempts = 10;
+	while ((!peer2->connected || !peer1->connected) && attempts--)
+		sleep(1);
+
+	if (peer1->state != RTC_CONNECTED || peer2->state != RTC_CONNECTED) {
+		fprintf(stderr, "PeerConnection is not connected\n");
+		goto error;
+	}
+
+	if (!peer1->connected || !peer2->connected) {
+		fprintf(stderr, "Track is not connected\n");
+		goto error;
+	}
+
+	deletePeer(peer1);
+	sleep(1);
+	deletePeer(peer2);
+	sleep(1);
+
+	// You may call rtcCleanup() when finished to free static resources
+	rtcCleanup();
+	sleep(2);
+
+	printf("Success\n");
+	return 0;
+
+error:
+	deletePeer(peer1);
+	deletePeer(peer2);
+	return -1;
+}
+
+#include <stdexcept>
+
+void test_capi_track() {
+	if (test_capi_track_main())
+		throw std::runtime_error("Connection failed");
+}

+ 13 - 9
test/connectivity.cpp

@@ -47,20 +47,20 @@ void test_connectivity() {
 
 	auto pc2 = std::make_shared<PeerConnection>(config2);
 
-	pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](const Description &sdp) {
+	pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](Description sdp) {
 		auto pc2 = wpc2.lock();
 		if (!pc2)
 			return;
 		cout << "Description 1: " << sdp << endl;
-		pc2->setRemoteDescription(sdp);
+		pc2->setRemoteDescription(string(sdp));
 	});
 
-	pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](const Candidate &candidate) {
+	pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](Candidate candidate) {
 		auto pc2 = wpc2.lock();
 		if (!pc2)
 			return;
 		cout << "Candidate 1: " << candidate << endl;
-		pc2->addRemoteCandidate(candidate);
+		pc2->addRemoteCandidate(string(candidate));
 	});
 
 	pc1->onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
@@ -69,20 +69,20 @@ void test_connectivity() {
 		cout << "Gathering state 1: " << state << endl;
 	});
 
-	pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](const Description &sdp) {
+	pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](Description sdp) {
 		auto pc1 = wpc1.lock();
 		if (!pc1)
 			return;
 		cout << "Description 2: " << sdp << endl;
-		pc1->setRemoteDescription(sdp);
+		pc1->setRemoteDescription(string(sdp));
 	});
 
-	pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](const Candidate &candidate) {
+	pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](Candidate candidate) {
 		auto pc1 = wpc1.lock();
 		if (!pc1)
 			return;
 		cout << "Candidate 2: " << candidate << endl;
-		pc1->addRemoteCandidate(candidate);
+		pc1->addRemoteCandidate(string(candidate));
 	});
 
 	pc2->onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
@@ -94,8 +94,12 @@ void test_connectivity() {
 	shared_ptr<DataChannel> dc2;
 	pc2->onDataChannel([&dc2](shared_ptr<DataChannel> dc) {
 		cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
+		if (dc->label() != "test") {
+			cerr << "Wrong DataChannel label" << endl;
+			return;
+		}
 
-		dc->onMessage([](const variant<binary, string> &message) {
+		dc->onMessage([](variant<binary, string> message) {
 			if (holds_alternative<string>(message)) {
 				cout << "Message 2: " << get<string>(message) << endl;
 			}

+ 25 - 5
test/main.cpp

@@ -24,7 +24,9 @@ using namespace std;
 using namespace chrono_literals;
 
 void test_connectivity();
-void test_capi();
+void test_track();
+void test_capi_connectivity();
+void test_capi_track();
 void test_websocket();
 size_t benchmark(chrono::milliseconds duration);
 
@@ -49,13 +51,31 @@ int main(int argc, char **argv) {
 		return -1;
 	}
 	try {
-		cout << endl << "*** Running WebRTC C API test..." << endl;
-		test_capi();
-		cout << "*** Finished WebRTC C API test" << endl;
+		cout << endl << "*** Running WebRTC C API connectivity test..." << endl;
+		test_capi_connectivity();
+		cout << "*** Finished WebRTC C API connectivity test" << endl;
 	} catch (const exception &e) {
-		cerr << "WebRTC C API test failed: " << e.what() << endl;
+		cerr << "WebRTC C API connectivity test failed: " << e.what() << endl;
 		return -1;
 	}
+#if RTC_ENABLE_MEDIA
+	try {
+		cout << endl << "*** Running WebRTC Track test..." << endl;
+		test_track();
+		cout << "*** Finished WebRTC Track test" << endl;
+	} catch (const exception &e) {
+		cerr << "WebRTC Track test failed: " << e.what() << endl;
+		return -1;
+	}
+	try {
+		cout << endl << "*** Running WebRTC C API track test..." << endl;
+		test_capi_track();
+		cout << "*** Finished WebRTC C API track test" << endl;
+	} catch (const exception &e) {
+		cerr << "WebRTC C API track test failed: " << e.what() << endl;
+		return -1;
+	}
+#endif
 #if RTC_ENABLE_WEBSOCKET
 	try {
 		cout << endl << "*** Running WebSocket test..." << endl;

+ 134 - 0
test/track.cpp

@@ -0,0 +1,134 @@
+/**
+ * Copyright (c) 2019 Paul-Louis Ageneau
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "rtc/rtc.hpp"
+
+#include <atomic>
+#include <chrono>
+#include <iostream>
+#include <memory>
+#include <thread>
+
+using namespace rtc;
+using namespace std;
+
+template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
+
+void test_track() {
+	InitLogger(LogLevel::Debug);
+
+	Configuration config1;
+	// STUN server example
+	// config1.iceServers.emplace_back("stun:stun.l.google.com:19302");
+
+	auto pc1 = std::make_shared<PeerConnection>(config1);
+
+	Configuration config2;
+	// STUN server example
+	// config2.iceServers.emplace_back("stun:stun.l.google.com:19302");
+	// Port range example
+	config2.portRangeBegin = 5000;
+	config2.portRangeEnd = 6000;
+
+	auto pc2 = std::make_shared<PeerConnection>(config2);
+
+	pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](Description sdp) {
+		auto pc2 = wpc2.lock();
+		if (!pc2)
+			return;
+		cout << "Description 1: " << sdp << endl;
+		pc2->setRemoteDescription(string(sdp));
+	});
+
+	pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](Candidate candidate) {
+		auto pc2 = wpc2.lock();
+		if (!pc2)
+			return;
+		cout << "Candidate 1: " << candidate << endl;
+		pc2->addRemoteCandidate(string(candidate));
+	});
+
+	pc1->onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
+
+	pc1->onGatheringStateChange([](PeerConnection::GatheringState state) {
+		cout << "Gathering state 1: " << state << endl;
+	});
+
+	pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](Description sdp) {
+		auto pc1 = wpc1.lock();
+		if (!pc1)
+			return;
+		cout << "Description 2: " << sdp << endl;
+		pc1->setRemoteDescription(string(sdp));
+	});
+
+	pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](Candidate candidate) {
+		auto pc1 = wpc1.lock();
+		if (!pc1)
+			return;
+		cout << "Candidate 2: " << candidate << endl;
+		pc1->addRemoteCandidate(string(candidate));
+	});
+
+	pc2->onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
+
+	pc2->onGatheringStateChange([](PeerConnection::GatheringState state) {
+		cout << "Gathering state 2: " << state << endl;
+	});
+
+	shared_ptr<Track> t2;
+	pc2->onTrack([&t2](shared_ptr<Track> t) {
+		cout << "Track 2: Received with mid \"" << t->mid() << "\"" << endl;
+		if (t->mid() != "test") {
+			cerr << "Wrong track mid" << endl;
+			return;
+		}
+
+		std::atomic_store(&t2, t);
+	});
+
+	auto t1 = pc1->addTrack(Description::Video("test"));
+
+	pc1->setLocalDescription();
+
+	int attempts = 10;
+	shared_ptr<Track> at2;
+	while ((!(at2 = std::atomic_load(&t2)) || !at2->isOpen() || !t1->isOpen()) && attempts--)
+		this_thread::sleep_for(1s);
+
+	if (pc1->state() != PeerConnection::State::Connected &&
+	    pc2->state() != PeerConnection::State::Connected)
+		throw runtime_error("PeerConnection is not connected");
+
+	if (!at2 || !at2->isOpen() || !t1->isOpen())
+		throw runtime_error("Track is not open");
+
+	// TODO: Test sending RTP packets in track
+
+	// Delay close of peer 2 to check closing works properly
+	pc1->close();
+	this_thread::sleep_for(1s);
+	pc2->close();
+	this_thread::sleep_for(1s);
+
+	// You may call rtc::Cleanup() when finished to free static resources
+	rtc::Cleanup();
+	this_thread::sleep_for(2s);
+
+	cout << "Success" << endl;
+}

+ 2 - 2
test/websocket.cpp

@@ -53,9 +53,9 @@ void test_websocket() {
 	ws->onClosed([]() { cout << "WebSocket: Closed" << endl; });
 
 	std::atomic<bool> received = false;
-	ws->onMessage([&received, &myMessage](const variant<binary, string> &message) {
+	ws->onMessage([&received, &myMessage](variant<binary, string> message) {
 		if (holds_alternative<string>(message)) {
-			string str = get<string>(message);
+			string str = std::move(get<string>(message));
 			if((received = (str == myMessage)))
 				cout << "WebSocket: Received expected message" << endl;
 			else