main.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <chrono>
  20. #include <iostream>
  21. #include <memory>
  22. #include <thread>
  23. #ifdef _WIN32
  24. #include <winsock2.h>
  25. #endif
  26. using namespace rtc;
  27. using namespace std;
  28. template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  29. int main(int argc, char **argv) {
  30. InitLogger(LogLevel::Warning);
  31. Configuration config;
  32. // config.iceServers.emplace_back("stun:stun.l.google.com:19302");
  33. // config.enableIceTcp = true;
  34. // TURN server example
  35. // IceServer turnServer("TURN_SERVER_URL", "PORT_NO", "USERNAME", "PASSWORD",
  36. // IceServer::RelayType::TurnUdp);
  37. // config.iceServers.push_back(turnServer);
  38. auto pc1 = std::make_shared<PeerConnection>(config);
  39. auto pc2 = std::make_shared<PeerConnection>(config);
  40. #ifdef _WIN32
  41. WSADATA wsaData;
  42. int iResult;
  43. // Initialize Winsock
  44. iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  45. if (iResult != 0) {
  46. std::string err("WSAStartup failed. Error:");
  47. err.append(WSAGetLastError() + "");
  48. std::cout << err;
  49. return -1;
  50. }
  51. #endif
  52. pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](const Description &sdp) {
  53. auto pc2 = wpc2.lock();
  54. if (!pc2)
  55. return;
  56. cout << "Description 1: " << sdp << endl;
  57. pc2->setRemoteDescription(sdp);
  58. });
  59. pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](const Candidate &candidate) {
  60. auto pc2 = wpc2.lock();
  61. if (!pc2)
  62. return;
  63. cout << "Candidate 1: " << candidate << endl;
  64. pc2->addRemoteCandidate(candidate);
  65. });
  66. pc1->onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
  67. pc1->onGatheringStateChange([](PeerConnection::GatheringState state) {
  68. cout << "Gathering state 1: " << state << endl;
  69. });
  70. pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](const Description &sdp) {
  71. auto pc1 = wpc1.lock();
  72. if (!pc1)
  73. return;
  74. cout << "Description 2: " << sdp << endl;
  75. pc1->setRemoteDescription(sdp);
  76. });
  77. pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](const Candidate &candidate) {
  78. auto pc1 = wpc1.lock();
  79. if (!pc1)
  80. return;
  81. cout << "Candidate 2: " << candidate << endl;
  82. pc1->addRemoteCandidate(candidate);
  83. });
  84. pc2->onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
  85. pc2->onGatheringStateChange([](PeerConnection::GatheringState state) {
  86. cout << "Gathering state 2: " << state << endl;
  87. });
  88. shared_ptr<DataChannel> dc2;
  89. pc2->onDataChannel([&dc2](shared_ptr<DataChannel> dc) {
  90. cout << "Got a DataChannel with label: " << dc->label() << endl;
  91. dc2 = dc;
  92. dc2->onMessage([](const variant<binary, string> &message) {
  93. if (holds_alternative<string>(message)) {
  94. cout << "Received 2: " << get<string>(message) << endl;
  95. }
  96. });
  97. dc2->send("Hello from 2");
  98. });
  99. auto dc1 = pc1->createDataChannel("test");
  100. dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() {
  101. auto dc1 = wdc1.lock();
  102. if (!dc1)
  103. return;
  104. cout << "DataChannel open: " << dc1->label() << endl;
  105. dc1->send("Hello from 1");
  106. });
  107. dc1->onMessage([](const variant<binary, string> &message) {
  108. if (holds_alternative<string>(message)) {
  109. cout << "Received 1: " << get<string>(message) << endl;
  110. }
  111. });
  112. this_thread::sleep_for(3s);
  113. if (auto addr = pc1->localAddress())
  114. cout << "Local address 1: " << *addr << endl;
  115. if (auto addr = pc1->remoteAddress())
  116. cout << "Remote address 1: " << *addr << endl;
  117. if (auto addr = pc2->localAddress())
  118. cout << "Local address 2: " << *addr << endl;
  119. if (auto addr = pc2->remoteAddress())
  120. cout << "Remote address 2: " << *addr << endl;
  121. if (dc1->isOpen() && dc2->isOpen()) {
  122. pc1->close();
  123. pc2->close();
  124. cout << "Success" << endl;
  125. #ifdef _WIN32
  126. WSACleanup();
  127. #endif
  128. return 0;
  129. } else {
  130. cout << "Failure" << endl;
  131. #ifdef _WIN32
  132. WSACleanup();
  133. #endif
  134. return 1;
  135. }
  136. }