offerer.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. auto dc = pc->createDataChannel("test"); // this is the offerer, so create a data channel
  48. dc->onOpen([&]() { std::cout << "[DataChannel open: " << dc->label() << "]" << std::endl; });
  49. dc->onClosed(
  50. [&]() { std::cout << "[DataChannel closed: " << dc->label() << "]" << std::endl; });
  51. dc->onMessage([](auto data) {
  52. if (std::holds_alternative<std::string>(data)) {
  53. std::cout << "[Received: " << std::get<std::string>(data) << "]" << std::endl;
  54. }
  55. });
  56. std::this_thread::sleep_for(1s);
  57. bool exit = false;
  58. while (!exit) {
  59. std::cout
  60. << std::endl
  61. << "**********************************************************************************"
  62. "*****"
  63. << std::endl
  64. << "* 0: Exit /"
  65. << " 1: Enter remote description /"
  66. << " 2: Enter remote candidate /"
  67. << " 3: Send message /"
  68. << " 4: Print Connection Info *" << std::endl
  69. << "[Command]: ";
  70. int command = -1;
  71. std::cin >> command;
  72. std::cin.ignore();
  73. switch (command) {
  74. case 0: {
  75. exit = true;
  76. break;
  77. }
  78. case 1: {
  79. // Parse Description
  80. std::cout << "[Description]: ";
  81. std::string sdp, line;
  82. while (getline(std::cin, line) && !line.empty()) {
  83. sdp += line;
  84. sdp += "\r\n";
  85. }
  86. pc->setRemoteDescription(sdp);
  87. break;
  88. }
  89. case 2: {
  90. // Parse Candidate
  91. std::cout << "[Candidate]: ";
  92. std::string candidate;
  93. getline(std::cin, candidate);
  94. pc->addRemoteCandidate(candidate);
  95. break;
  96. }
  97. case 3: {
  98. // Send Message
  99. if (!dc->isOpen()) {
  100. std::cout << "** Channel is not Open ** ";
  101. break;
  102. }
  103. std::cout << "[Message]: ";
  104. std::string message;
  105. getline(std::cin, message);
  106. dc->send(message);
  107. break;
  108. }
  109. case 4: {
  110. // Connection Info
  111. if (!dc || !dc->isOpen()) {
  112. std::cout << "** Channel is not Open ** ";
  113. break;
  114. }
  115. rtc::Candidate local, remote;
  116. std::optional<std::chrono::milliseconds> rtt = pc->rtt();
  117. if (pc->getSelectedCandidatePair(&local, &remote)) {
  118. std::cout << "Local: " << local << std::endl;
  119. std::cout << "Remote: " << remote << std::endl;
  120. std::cout << "Bytes Sent:" << pc->bytesSent()
  121. << " / Bytes Received:" << pc->bytesReceived() << " / Round-Trip Time:";
  122. if (rtt.has_value())
  123. std::cout << rtt.value().count();
  124. else
  125. std::cout << "null";
  126. std::cout << " ms";
  127. } else {
  128. std::cout << "Could not get Candidate Pair Info" << std::endl;
  129. }
  130. break;
  131. }
  132. default: {
  133. std::cout << "** Invalid Command **" << std::endl;
  134. break;
  135. }
  136. }
  137. }
  138. if (dc)
  139. dc->close();
  140. if (pc)
  141. pc->close();
  142. }