answerer.cpp 4.2 KB

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