connectivity.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #include "rtc/rtc.hpp"
  9. #include <atomic>
  10. #include <chrono>
  11. #include <iostream>
  12. #include <memory>
  13. #include <thread>
  14. #define CUSTOM_MAX_MESSAGE_SIZE 1048576
  15. using namespace rtc;
  16. using namespace std;
  17. template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  18. void test_connectivity(bool signal_wrong_fingerprint) {
  19. InitLogger(LogLevel::Debug);
  20. Configuration config1;
  21. // STUN server example (not necessary to connect locally)
  22. config1.iceServers.emplace_back("stun:stun.l.google.com:19302");
  23. // Custom MTU example
  24. config1.mtu = 1500;
  25. // Custom max message size
  26. config1.maxMessageSize = CUSTOM_MAX_MESSAGE_SIZE;
  27. PeerConnection pc1(config1);
  28. Configuration config2;
  29. // STUN server example (not necessary to connect locally)
  30. config2.iceServers.emplace_back("stun:stun.l.google.com:19302");
  31. // Custom MTU example
  32. config2.mtu = 1500;
  33. // Custom max message size
  34. config2.maxMessageSize = CUSTOM_MAX_MESSAGE_SIZE;
  35. // Port range example
  36. config2.portRangeBegin = 5000;
  37. config2.portRangeEnd = 6000;
  38. PeerConnection pc2(config2);
  39. pc1.onLocalDescription([&pc2, signal_wrong_fingerprint](Description sdp) {
  40. cout << "Description 1: " << sdp << endl;
  41. if (signal_wrong_fingerprint) {
  42. auto f = sdp.fingerprint();
  43. if (f.has_value()) {
  44. auto s = f.value();
  45. auto& c = s[0];
  46. if (c == 'F' || c == 'f') c = '0'; else c++;
  47. sdp.setFingerprint(s);
  48. }
  49. }
  50. pc2.setRemoteDescription(string(sdp));
  51. });
  52. pc1.onLocalCandidate([&pc2](Candidate candidate) {
  53. cout << "Candidate 1: " << candidate << endl;
  54. pc2.addRemoteCandidate(string(candidate));
  55. });
  56. pc1.onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
  57. pc1.onIceStateChange([](PeerConnection::IceState state) {
  58. cout << "ICE state 1: " << state << endl;
  59. });
  60. pc1.onGatheringStateChange([](PeerConnection::GatheringState state) {
  61. cout << "Gathering state 1: " << state << endl;
  62. });
  63. pc1.onSignalingStateChange([](PeerConnection::SignalingState state) {
  64. cout << "Signaling state 1: " << state << endl;
  65. });
  66. pc2.onLocalDescription([&pc1](Description sdp) {
  67. cout << "Description 2: " << sdp << endl;
  68. pc1.setRemoteDescription(string(sdp));
  69. });
  70. pc2.onLocalCandidate([&pc1](Candidate candidate) {
  71. cout << "Candidate 2: " << candidate << endl;
  72. pc1.addRemoteCandidate(string(candidate));
  73. });
  74. pc2.onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
  75. pc2.onIceStateChange([](PeerConnection::IceState state) {
  76. cout << "ICE state 2: " << state << endl;
  77. });
  78. pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
  79. cout << "Gathering state 2: " << state << endl;
  80. });
  81. pc2.onSignalingStateChange([](PeerConnection::SignalingState state) {
  82. cout << "Signaling state 2: " << state << endl;
  83. });
  84. shared_ptr<DataChannel> dc2;
  85. pc2.onDataChannel([&dc2](shared_ptr<DataChannel> dc) {
  86. cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
  87. dc->onOpen([wdc = make_weak_ptr(dc)]() {
  88. if (auto dc = wdc.lock()) {
  89. cout << "DataChannel 2: Open" << endl;
  90. dc->send("Hello from 2");
  91. }
  92. });
  93. dc->onClosed([]() { cout << "DataChannel 2: Closed" << endl; });
  94. dc->onMessage([](variant<binary, string> message) {
  95. if (holds_alternative<string>(message)) {
  96. cout << "Message 2: " << get<string>(message) << endl;
  97. }
  98. });
  99. std::atomic_store(&dc2, dc);
  100. });
  101. auto dc1 = pc1.createDataChannel("test");
  102. if (dc1->id().has_value())
  103. throw std::runtime_error("DataChannel stream id assigned before connection");
  104. dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() {
  105. if (auto dc1 = wdc1.lock()) {
  106. cout << "DataChannel 1: Open" << endl;
  107. dc1->send("Hello from 1");
  108. }
  109. });
  110. dc1->onClosed([]() { cout << "DataChannel 1: Closed" << endl; });
  111. dc1->onMessage([](const variant<binary, string> &message) {
  112. if (holds_alternative<string>(message)) {
  113. cout << "Message 1: " << get<string>(message) << endl;
  114. }
  115. });
  116. // Wait a bit
  117. int attempts = 10;
  118. shared_ptr<DataChannel> adc2;
  119. while ((!(adc2 = std::atomic_load(&dc2)) || !adc2->isOpen() || !dc1->isOpen()) && attempts--)
  120. this_thread::sleep_for(1s);
  121. if (pc1.state() != PeerConnection::State::Connected ||
  122. pc2.state() != PeerConnection::State::Connected)
  123. throw runtime_error("PeerConnection is not connected");
  124. if ((pc1.iceState() != PeerConnection::IceState::Connected &&
  125. pc1.iceState() != PeerConnection::IceState::Completed) ||
  126. (pc2.iceState() != PeerConnection::IceState::Connected &&
  127. pc2.iceState() != PeerConnection::IceState::Completed))
  128. throw runtime_error("ICE is not connected");
  129. if (!adc2 || !adc2->isOpen() || !dc1->isOpen())
  130. throw runtime_error("DataChannel is not open");
  131. if (adc2->label() != "test")
  132. throw runtime_error("Wrong DataChannel label");
  133. if (dc1->maxMessageSize() != CUSTOM_MAX_MESSAGE_SIZE ||
  134. dc2->maxMessageSize() != CUSTOM_MAX_MESSAGE_SIZE)
  135. throw runtime_error("DataChannel max message size is incorrect");
  136. if (!dc1->id().has_value())
  137. throw runtime_error("DataChannel stream id is not assigned");
  138. if (dc1->id().value() != adc2->id().value())
  139. throw runtime_error("DataChannel stream ids do not match");
  140. if (auto addr = pc1.localAddress())
  141. cout << "Local address 1: " << *addr << endl;
  142. if (auto addr = pc1.remoteAddress())
  143. cout << "Remote address 1: " << *addr << endl;
  144. if (auto addr = pc2.localAddress())
  145. cout << "Local address 2: " << *addr << endl;
  146. if (auto addr = pc2.remoteAddress())
  147. cout << "Remote address 2: " << *addr << endl;
  148. Candidate local, remote;
  149. if (pc1.getSelectedCandidatePair(&local, &remote)) {
  150. cout << "Local candidate 1: " << local << endl;
  151. cout << "Remote candidate 1: " << remote << endl;
  152. }
  153. if (pc2.getSelectedCandidatePair(&local, &remote)) {
  154. cout << "Local candidate 2: " << local << endl;
  155. cout << "Remote candidate 2: " << remote << endl;
  156. }
  157. // Try to open a second data channel with another label
  158. shared_ptr<DataChannel> second2;
  159. pc2.onDataChannel([&second2](shared_ptr<DataChannel> dc) {
  160. cout << "Second DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
  161. dc->onOpen([wdc = make_weak_ptr(dc)]() {
  162. if (auto dc = wdc.lock())
  163. dc->send("Second hello from 2");
  164. });
  165. dc->onMessage([](variant<binary, string> message) {
  166. if (holds_alternative<string>(message)) {
  167. cout << "Second Message 2: " << get<string>(message) << endl;
  168. }
  169. });
  170. std::atomic_store(&second2, dc);
  171. });
  172. auto second1 = pc1.createDataChannel("second");
  173. if (!second1->id().has_value())
  174. throw std::runtime_error("Second DataChannel stream id is not assigned");
  175. second1->onOpen([wsecond1 = make_weak_ptr(second1)]() {
  176. if (auto second1 = wsecond1.lock()) {
  177. cout << "Second DataChannel 1: Open" << endl;
  178. second1->send("Second hello from 1");
  179. }
  180. });
  181. second1->onClosed([]() { cout << "Second DataChannel 1: Closed" << endl; });
  182. second1->onMessage([](const variant<binary, string> &message) {
  183. if (holds_alternative<string>(message)) {
  184. cout << "Second Message 1: " << get<string>(message) << endl;
  185. }
  186. });
  187. // Wait a bit
  188. attempts = 10;
  189. shared_ptr<DataChannel> asecond2;
  190. while (
  191. (!(asecond2 = std::atomic_load(&second2)) || !asecond2->isOpen() || !second1->isOpen()) &&
  192. attempts--)
  193. this_thread::sleep_for(1s);
  194. if (!asecond2 || !asecond2->isOpen() || !second1->isOpen())
  195. throw runtime_error("Second DataChannel is not open");
  196. if (asecond2->label() != "second")
  197. throw runtime_error("Wrong second DataChannel label");
  198. if (!second2->id().has_value() || !asecond2->id().has_value())
  199. throw runtime_error("Second DataChannel stream id is not assigned");
  200. if (second2->id().value() != asecond2->id().value())
  201. throw runtime_error("Second DataChannel stream ids do not match");
  202. // Delay close of peer 2 to check closing works properly
  203. pc1.close();
  204. this_thread::sleep_for(1s);
  205. pc2.close();
  206. this_thread::sleep_for(1s);
  207. cout << "Success" << endl;
  208. }