main.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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::Debug);
  31. #ifdef _WIN32
  32. WSADATA wsaData;
  33. if (WSAStartup(MAKEWORD(2, 2), &wsaData))
  34. throw std::runtime_error("WSAStartup failed, error=" + std::to_string(WSAGetLastError()));
  35. #endif
  36. Configuration config;
  37. // config.iceServers.emplace_back("stun:stun.l.google.com:19302");
  38. // config.iceServers.emplace_back(IceServer("TURN_SERVER_URL", "PORT", "USERNAME", "PASSWORD",
  39. // IceServer::RelayType::TurnUdp)); // libnice only
  40. // config.enableIceTcp = true; // libnice only
  41. auto pc1 = std::make_shared<PeerConnection>(config);
  42. auto pc2 = std::make_shared<PeerConnection>(config);
  43. pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](const Description &sdp) {
  44. auto pc2 = wpc2.lock();
  45. if (!pc2)
  46. return;
  47. cout << "Description 1: " << sdp << endl;
  48. pc2->setRemoteDescription(sdp);
  49. });
  50. pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](const Candidate &candidate) {
  51. auto pc2 = wpc2.lock();
  52. if (!pc2)
  53. return;
  54. cout << "Candidate 1: " << candidate << endl;
  55. pc2->addRemoteCandidate(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. pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](const Description &sdp) {
  62. auto pc1 = wpc1.lock();
  63. if (!pc1)
  64. return;
  65. cout << "Description 2: " << sdp << endl;
  66. pc1->setRemoteDescription(sdp);
  67. });
  68. pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](const Candidate &candidate) {
  69. auto pc1 = wpc1.lock();
  70. if (!pc1)
  71. return;
  72. cout << "Candidate 2: " << candidate << endl;
  73. pc1->addRemoteCandidate(candidate);
  74. });
  75. pc2->onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
  76. pc2->onGatheringStateChange([](PeerConnection::GatheringState state) {
  77. cout << "Gathering state 2: " << state << endl;
  78. });
  79. shared_ptr<DataChannel> dc2;
  80. pc2->onDataChannel([&dc2](shared_ptr<DataChannel> dc) {
  81. cout << "Got a DataChannel with label: " << dc->label() << endl;
  82. dc2 = dc;
  83. dc2->onMessage([](const variant<binary, string> &message) {
  84. if (holds_alternative<string>(message)) {
  85. cout << "Received 2: " << get<string>(message) << endl;
  86. }
  87. });
  88. dc2->send("Hello from 2");
  89. });
  90. auto dc1 = pc1->createDataChannel("test");
  91. dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() {
  92. auto dc1 = wdc1.lock();
  93. if (!dc1)
  94. return;
  95. cout << "DataChannel open: " << dc1->label() << endl;
  96. dc1->send("Hello from 1");
  97. });
  98. dc1->onMessage([](const variant<binary, string> &message) {
  99. if (holds_alternative<string>(message)) {
  100. cout << "Received 1: " << get<string>(message) << endl;
  101. }
  102. });
  103. this_thread::sleep_for(3s);
  104. if (auto addr = pc1->localAddress())
  105. cout << "Local address 1: " << *addr << endl;
  106. if (auto addr = pc1->remoteAddress())
  107. cout << "Remote address 1: " << *addr << endl;
  108. if (auto addr = pc2->localAddress())
  109. cout << "Local address 2: " << *addr << endl;
  110. if (auto addr = pc2->remoteAddress())
  111. cout << "Remote address 2: " << *addr << endl;
  112. bool success;
  113. if ((success = dc1->isOpen() && dc2->isOpen())) {
  114. pc1->close();
  115. pc2->close();
  116. cout << "Success" << endl;
  117. } else {
  118. cout << "Failure" << endl;
  119. }
  120. #ifdef _WIN32
  121. WSACleanup();
  122. #endif
  123. return success ? 0 : 1;
  124. }