connectivity.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "rtc/rtc.hpp"
  19. #include <atomic>
  20. #include <chrono>
  21. #include <iostream>
  22. #include <memory>
  23. #include <thread>
  24. #define CUSTOM_MAX_MESSAGE_SIZE 1048576
  25. using namespace rtc;
  26. using namespace std;
  27. template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  28. void test_connectivity() {
  29. InitLogger(LogLevel::Debug);
  30. Configuration config1;
  31. // STUN server example (not necessary to connect locally)
  32. config1.iceServers.emplace_back("stun:stun.l.google.com:19302");
  33. // Custom MTU example
  34. config1.mtu = 1500;
  35. // Custom max message size
  36. config1.maxMessageSize = CUSTOM_MAX_MESSAGE_SIZE;
  37. PeerConnection pc1(config1);
  38. Configuration config2;
  39. // STUN server example (not necessary to connect locally)
  40. config2.iceServers.emplace_back("stun:stun.l.google.com:19302");
  41. // Custom MTU example
  42. config2.mtu = 1500;
  43. // Custom max message size
  44. config2.maxMessageSize = CUSTOM_MAX_MESSAGE_SIZE;
  45. // Port range example
  46. config2.portRangeBegin = 5000;
  47. config2.portRangeEnd = 6000;
  48. PeerConnection pc2(config2);
  49. pc1.onLocalDescription([&pc2](Description sdp) {
  50. cout << "Description 1: " << sdp << endl;
  51. pc2.setRemoteDescription(string(sdp));
  52. });
  53. pc1.onLocalCandidate([&pc2](Candidate candidate) {
  54. cout << "Candidate 1: " << candidate << endl;
  55. pc2.addRemoteCandidate(string(candidate));
  56. });
  57. pc1.onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
  58. pc1.onGatheringStateChange([](PeerConnection::GatheringState state) {
  59. cout << "Gathering state 1: " << state << endl;
  60. });
  61. pc1.onSignalingStateChange([](PeerConnection::SignalingState state) {
  62. cout << "Signaling state 1: " << state << endl;
  63. });
  64. pc2.onLocalDescription([&pc1](Description sdp) {
  65. cout << "Description 2: " << sdp << endl;
  66. pc1.setRemoteDescription(string(sdp));
  67. });
  68. pc2.onLocalCandidate([&pc1](Candidate candidate) {
  69. cout << "Candidate 2: " << candidate << endl;
  70. pc1.addRemoteCandidate(string(candidate));
  71. });
  72. pc2.onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
  73. pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
  74. cout << "Gathering state 2: " << state << endl;
  75. });
  76. pc2.onSignalingStateChange([](PeerConnection::SignalingState state) {
  77. cout << "Signaling state 2: " << state << endl;
  78. });
  79. shared_ptr<DataChannel> dc2;
  80. pc2.onDataChannel([&dc2](shared_ptr<DataChannel> dc) {
  81. cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
  82. dc->onOpen([wdc = make_weak_ptr(dc)]() {
  83. if (auto dc = wdc.lock()) {
  84. cout << "DataChannel 2: Open" << endl;
  85. dc->send("Hello from 2");
  86. }
  87. });
  88. dc->onClosed([]() { cout << "DataChannel 2: Closed" << endl; });
  89. dc->onMessage([](variant<binary, string> message) {
  90. if (holds_alternative<string>(message)) {
  91. cout << "Message 2: " << get<string>(message) << endl;
  92. }
  93. });
  94. std::atomic_store(&dc2, dc);
  95. });
  96. auto dc1 = pc1.createDataChannel("test");
  97. if (dc1->id().has_value())
  98. throw std::runtime_error("DataChannel stream id assigned before connection");
  99. dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() {
  100. if (auto dc1 = wdc1.lock()) {
  101. cout << "DataChannel 1: Open" << endl;
  102. dc1->send("Hello from 1");
  103. }
  104. });
  105. dc1->onClosed([]() { cout << "DataChannel 1: Closed" << endl; });
  106. dc1->onMessage([](const variant<binary, string> &message) {
  107. if (holds_alternative<string>(message)) {
  108. cout << "Message 1: " << get<string>(message) << endl;
  109. }
  110. });
  111. // Wait a bit
  112. int attempts = 10;
  113. shared_ptr<DataChannel> adc2;
  114. while ((!(adc2 = std::atomic_load(&dc2)) || !adc2->isOpen() || !dc1->isOpen()) && attempts--)
  115. this_thread::sleep_for(1s);
  116. if (pc1.state() != PeerConnection::State::Connected &&
  117. pc2.state() != PeerConnection::State::Connected)
  118. throw runtime_error("PeerConnection is not connected");
  119. if (!adc2 || !adc2->isOpen() || !dc1->isOpen())
  120. throw runtime_error("DataChannel is not open");
  121. if (adc2->label() != "test")
  122. throw runtime_error("Wrong DataChannel label");
  123. if (dc1->maxMessageSize() != CUSTOM_MAX_MESSAGE_SIZE ||
  124. dc2->maxMessageSize() != CUSTOM_MAX_MESSAGE_SIZE)
  125. throw runtime_error("DataChannel max message size is incorrect");
  126. if (!dc1->id().has_value())
  127. throw runtime_error("DataChannel stream id is not assigned");
  128. if (dc1->id().value() != adc2->id().value())
  129. throw runtime_error("DataChannel stream ids do not match");
  130. if (auto addr = pc1.localAddress())
  131. cout << "Local address 1: " << *addr << endl;
  132. if (auto addr = pc1.remoteAddress())
  133. cout << "Remote address 1: " << *addr << endl;
  134. if (auto addr = pc2.localAddress())
  135. cout << "Local address 2: " << *addr << endl;
  136. if (auto addr = pc2.remoteAddress())
  137. cout << "Remote address 2: " << *addr << endl;
  138. Candidate local, remote;
  139. if (pc1.getSelectedCandidatePair(&local, &remote)) {
  140. cout << "Local candidate 1: " << local << endl;
  141. cout << "Remote candidate 1: " << remote << endl;
  142. }
  143. if (pc2.getSelectedCandidatePair(&local, &remote)) {
  144. cout << "Local candidate 2: " << local << endl;
  145. cout << "Remote candidate 2: " << remote << endl;
  146. }
  147. // Try to open a second data channel with another label
  148. shared_ptr<DataChannel> second2;
  149. pc2.onDataChannel([&second2](shared_ptr<DataChannel> dc) {
  150. cout << "Second DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
  151. dc->onOpen([wdc = make_weak_ptr(dc)]() {
  152. if (auto dc = wdc.lock())
  153. dc->send("Second hello from 2");
  154. });
  155. dc->onMessage([](variant<binary, string> message) {
  156. if (holds_alternative<string>(message)) {
  157. cout << "Second Message 2: " << get<string>(message) << endl;
  158. }
  159. });
  160. std::atomic_store(&second2, dc);
  161. });
  162. auto second1 = pc1.createDataChannel("second");
  163. if (!second1->id().has_value())
  164. throw std::runtime_error("Second DataChannel stream id is not assigned");
  165. second1->onOpen([wsecond1 = make_weak_ptr(second1)]() {
  166. if (auto second1 = wsecond1.lock()) {
  167. cout << "Second DataChannel 1: Open" << endl;
  168. second1->send("Second hello from 1");
  169. }
  170. });
  171. second1->onClosed([]() { cout << "Second DataChannel 1: Closed" << endl; });
  172. second1->onMessage([](const variant<binary, string> &message) {
  173. if (holds_alternative<string>(message)) {
  174. cout << "Second Message 1: " << get<string>(message) << endl;
  175. }
  176. });
  177. // Wait a bit
  178. attempts = 10;
  179. shared_ptr<DataChannel> asecond2;
  180. while (
  181. (!(asecond2 = std::atomic_load(&second2)) || !asecond2->isOpen() || !second1->isOpen()) &&
  182. attempts--)
  183. this_thread::sleep_for(1s);
  184. if (!asecond2 || !asecond2->isOpen() || !second1->isOpen())
  185. throw runtime_error("Second DataChannel is not open");
  186. if (asecond2->label() != "second")
  187. throw runtime_error("Wrong second DataChannel label");
  188. if (!second2->id().has_value() || !asecond2->id().has_value())
  189. throw runtime_error("Second DataChannel stream id is not assigned");
  190. if (second2->id().value() != asecond2->id().value())
  191. throw runtime_error("Second DataChannel stream ids do not match");
  192. // Delay close of peer 2 to check closing works properly
  193. pc1.close();
  194. this_thread::sleep_for(1s);
  195. pc2.close();
  196. this_thread::sleep_for(1s);
  197. cout << "Success" << endl;
  198. }