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