answerer.cpp 4.0 KB

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