answerer.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau, Murat Dogan
  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 std::chrono_literals;
  24. using std::shared_ptr;
  25. using std::weak_ptr;
  26. template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  27. int main(int argc, char **argv) {
  28. rtc::InitLogger(rtc::LogLevel::Warning);
  29. rtc::Configuration config;
  30. // config.iceServers.emplace_back("stun.l.google.com:19302");
  31. auto pc = std::make_shared<rtc::PeerConnection>(config);
  32. pc->onLocalDescription([](rtc::Description description) {
  33. std::cout << "Local Description (Paste this to the other peer):" << std::endl;
  34. std::cout << std::string(description) << std::endl;
  35. });
  36. pc->onLocalCandidate([](rtc::Candidate candidate) {
  37. std::cout << "Local Candidate (Paste this to the other peer after the local description):"
  38. << std::endl;
  39. std::cout << std::string(candidate) << std::endl << std::endl;
  40. });
  41. pc->onStateChange([](rtc::PeerConnection::State state) {
  42. std::cout << "[State: " << state << "]" << std::endl;
  43. });
  44. pc->onGatheringStateChange([](rtc::PeerConnection::GatheringState state) {
  45. std::cout << "[Gathering State: " << state << "]" << std::endl;
  46. });
  47. shared_ptr<rtc::DataChannel> dc;
  48. pc->onDataChannel([&](shared_ptr<rtc::DataChannel> _dc) {
  49. std::cout << "[Got a DataChannel with label: " << _dc->label() << "]" << std::endl;
  50. dc = _dc;
  51. dc->onClosed(
  52. [&]() { std::cout << "[DataChannel closed: " << dc->label() << "]" << std::endl; });
  53. dc->onMessage([](auto data) {
  54. if (std::holds_alternative<std::string>(data)) {
  55. std::cout << "[Received message: " << std::get<std::string>(data) << "]"
  56. << std::endl;
  57. }
  58. });
  59. });
  60. bool exit = false;
  61. while (!exit) {
  62. std::cout
  63. << std::endl
  64. << "**********************************************************************************"
  65. "*****"
  66. << std::endl
  67. << "* 0: Exit /"
  68. << " 1: Enter remote description /"
  69. << " 2: Enter remote candidate /"
  70. << " 3: Send message /"
  71. << " 4: Print Connection Info *" << std::endl
  72. << "[Command]: ";
  73. int command = -1;
  74. std::cin >> command;
  75. std::cin.ignore();
  76. switch (command) {
  77. case 0: {
  78. exit = true;
  79. break;
  80. }
  81. case 1: {
  82. // Parse Description
  83. std::cout << "[Description]: ";
  84. std::string sdp, line;
  85. while (getline(std::cin, line) && !line.empty()) {
  86. sdp += line;
  87. sdp += "\r\n";
  88. }
  89. std::cout << sdp;
  90. pc->setRemoteDescription(sdp);
  91. break;
  92. }
  93. case 2: {
  94. // Parse Candidate
  95. std::cout << "[Candidate]: ";
  96. std::string candidate;
  97. getline(std::cin, candidate);
  98. pc->addRemoteCandidate(candidate);
  99. break;
  100. }
  101. case 3: {
  102. // Send Message
  103. if (!dc || !dc->isOpen()) {
  104. std::cout << "** Channel is not Open ** ";
  105. break;
  106. }
  107. std::cout << "[Message]: ";
  108. std::string message;
  109. getline(std::cin, message);
  110. dc->send(message);
  111. break;
  112. }
  113. case 4: {
  114. // Connection Info
  115. if (!dc || !dc->isOpen()) {
  116. std::cout << "** Channel is not Open ** ";
  117. break;
  118. }
  119. rtc::Candidate local, remote;
  120. std::optional<std::chrono::milliseconds> rtt = pc->rtt();
  121. if (pc->getSelectedCandidatePair(&local, &remote)) {
  122. std::cout << "Local: " << local << std::endl;
  123. std::cout << "Remote: " << remote << std::endl;
  124. std::cout << "Bytes Sent:" << pc->bytesSent()
  125. << " / Bytes Received:" << pc->bytesReceived() << " / Round-Trip Time:";
  126. if (rtt.has_value())
  127. std::cout << rtt.value().count();
  128. else
  129. std::cout << "null";
  130. std::cout << " ms";
  131. } else {
  132. std::cout << "Could not get Candidate Pair Info" << std::endl;
  133. }
  134. break;
  135. }
  136. default: {
  137. std::cout << "** Invalid Command ** " << std::endl;
  138. break;
  139. }
  140. }
  141. }
  142. if (dc)
  143. dc->close();
  144. if (pc)
  145. pc->close();
  146. }