main.cpp 4.1 KB

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