2
0

connectivity.cpp 7.2 KB

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