connectivity.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.onGatheringStateChange([](PeerConnection::GatheringState state) {
  58. cout << "Gathering state 1: " << state << endl;
  59. });
  60. pc1.onSignalingStateChange([](PeerConnection::SignalingState state) {
  61. cout << "Signaling state 1: " << state << endl;
  62. });
  63. pc2.onLocalDescription([&pc1](Description sdp) {
  64. cout << "Description 2: " << sdp << endl;
  65. pc1.setRemoteDescription(string(sdp));
  66. });
  67. pc2.onLocalCandidate([&pc1](Candidate candidate) {
  68. cout << "Candidate 2: " << candidate << endl;
  69. pc1.addRemoteCandidate(string(candidate));
  70. });
  71. pc2.onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
  72. pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
  73. cout << "Gathering state 2: " << state << endl;
  74. });
  75. pc2.onSignalingStateChange([](PeerConnection::SignalingState state) {
  76. cout << "Signaling state 2: " << state << endl;
  77. });
  78. shared_ptr<DataChannel> dc2;
  79. pc2.onDataChannel([&dc2](shared_ptr<DataChannel> dc) {
  80. cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
  81. dc->onOpen([wdc = make_weak_ptr(dc)]() {
  82. if (auto dc = wdc.lock()) {
  83. cout << "DataChannel 2: Open" << endl;
  84. dc->send("Hello from 2");
  85. }
  86. });
  87. dc->onClosed([]() { cout << "DataChannel 2: Closed" << endl; });
  88. dc->onMessage([](variant<binary, string> message) {
  89. if (holds_alternative<string>(message)) {
  90. cout << "Message 2: " << get<string>(message) << endl;
  91. }
  92. });
  93. std::atomic_store(&dc2, dc);
  94. });
  95. auto dc1 = pc1.createDataChannel("test");
  96. if (dc1->id().has_value())
  97. throw std::runtime_error("DataChannel stream id assigned before connection");
  98. dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() {
  99. if (auto dc1 = wdc1.lock()) {
  100. cout << "DataChannel 1: Open" << endl;
  101. dc1->send("Hello from 1");
  102. }
  103. });
  104. dc1->onClosed([]() { cout << "DataChannel 1: Closed" << endl; });
  105. dc1->onMessage([](const variant<binary, string> &message) {
  106. if (holds_alternative<string>(message)) {
  107. cout << "Message 1: " << get<string>(message) << endl;
  108. }
  109. });
  110. // Wait a bit
  111. int attempts = 10;
  112. shared_ptr<DataChannel> adc2;
  113. while ((!(adc2 = std::atomic_load(&dc2)) || !adc2->isOpen() || !dc1->isOpen()) && attempts--)
  114. this_thread::sleep_for(1s);
  115. if (pc1.state() != PeerConnection::State::Connected &&
  116. pc2.state() != PeerConnection::State::Connected)
  117. throw runtime_error("PeerConnection is not connected");
  118. if (!adc2 || !adc2->isOpen() || !dc1->isOpen())
  119. throw runtime_error("DataChannel is not open");
  120. if (adc2->label() != "test")
  121. throw runtime_error("Wrong DataChannel label");
  122. if (dc1->maxMessageSize() != CUSTOM_MAX_MESSAGE_SIZE ||
  123. dc2->maxMessageSize() != CUSTOM_MAX_MESSAGE_SIZE)
  124. throw runtime_error("DataChannel max message size is incorrect");
  125. if (!dc1->id().has_value())
  126. throw runtime_error("DataChannel stream id is not assigned");
  127. if (dc1->id().value() != adc2->id().value())
  128. throw runtime_error("DataChannel stream ids do not match");
  129. if (auto addr = pc1.localAddress())
  130. cout << "Local address 1: " << *addr << endl;
  131. if (auto addr = pc1.remoteAddress())
  132. cout << "Remote address 1: " << *addr << endl;
  133. if (auto addr = pc2.localAddress())
  134. cout << "Local address 2: " << *addr << endl;
  135. if (auto addr = pc2.remoteAddress())
  136. cout << "Remote address 2: " << *addr << endl;
  137. Candidate local, remote;
  138. if (pc1.getSelectedCandidatePair(&local, &remote)) {
  139. cout << "Local candidate 1: " << local << endl;
  140. cout << "Remote candidate 1: " << remote << endl;
  141. }
  142. if (pc2.getSelectedCandidatePair(&local, &remote)) {
  143. cout << "Local candidate 2: " << local << endl;
  144. cout << "Remote candidate 2: " << remote << endl;
  145. }
  146. // Try to open a second data channel with another label
  147. shared_ptr<DataChannel> second2;
  148. pc2.onDataChannel([&second2](shared_ptr<DataChannel> dc) {
  149. cout << "Second DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
  150. dc->onOpen([wdc = make_weak_ptr(dc)]() {
  151. if (auto dc = wdc.lock())
  152. dc->send("Second hello from 2");
  153. });
  154. dc->onMessage([](variant<binary, string> message) {
  155. if (holds_alternative<string>(message)) {
  156. cout << "Second Message 2: " << get<string>(message) << endl;
  157. }
  158. });
  159. std::atomic_store(&second2, dc);
  160. });
  161. auto second1 = pc1.createDataChannel("second");
  162. if (!second1->id().has_value())
  163. throw std::runtime_error("Second DataChannel stream id is not assigned");
  164. second1->onOpen([wsecond1 = make_weak_ptr(second1)]() {
  165. if (auto second1 = wsecond1.lock()) {
  166. cout << "Second DataChannel 1: Open" << endl;
  167. second1->send("Second hello from 1");
  168. }
  169. });
  170. second1->onClosed([]() { cout << "Second DataChannel 1: Closed" << endl; });
  171. second1->onMessage([](const variant<binary, string> &message) {
  172. if (holds_alternative<string>(message)) {
  173. cout << "Second Message 1: " << get<string>(message) << endl;
  174. }
  175. });
  176. // Wait a bit
  177. attempts = 10;
  178. shared_ptr<DataChannel> asecond2;
  179. while (
  180. (!(asecond2 = std::atomic_load(&second2)) || !asecond2->isOpen() || !second1->isOpen()) &&
  181. attempts--)
  182. this_thread::sleep_for(1s);
  183. if (!asecond2 || !asecond2->isOpen() || !second1->isOpen())
  184. throw runtime_error("Second DataChannel is not open");
  185. if (asecond2->label() != "second")
  186. throw runtime_error("Wrong second DataChannel label");
  187. if (!second2->id().has_value() || !asecond2->id().has_value())
  188. throw runtime_error("Second DataChannel stream id is not assigned");
  189. if (second2->id().value() != asecond2->id().value())
  190. throw runtime_error("Second DataChannel stream ids do not match");
  191. // Delay close of peer 2 to check closing works properly
  192. pc1.close();
  193. this_thread::sleep_for(1s);
  194. pc2.close();
  195. this_thread::sleep_for(1s);
  196. cout << "Success" << endl;
  197. }