answerer.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. using namespace rtc;
  23. using namespace std;
  24. template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  25. int main(int argc, char **argv) {
  26. // InitLogger(LogLevel::Debug);
  27. Configuration config;
  28. // config.iceServers.emplace_back("stun.l.google.com:19302");
  29. // config.enableIceTcp = true;
  30. // TURN Server example
  31. // IceServer turnServer("TURN_SERVER_URL", "PORT_NO", "USERNAME", "PASSWORD",
  32. // IceServer::RelayType::TurnUdp);
  33. // config.iceServers.push_back(turnServer);
  34. auto pc = std::make_shared<PeerConnection>(config);
  35. pc->onLocalDescription([](const Description &sdp) {
  36. std::string s(sdp);
  37. std::replace(s.begin(), s.end(), '\n', static_cast<char>(94));
  38. cout << "Local Description (Paste this to other peer):" << endl << s << endl << endl;
  39. });
  40. pc->onLocalCandidate([](const Candidate &candidate) {
  41. cout << "Local Candidate (Paste this to other peer):" << endl << candidate << endl << endl;
  42. });
  43. pc->onStateChange(
  44. [](PeerConnection::State state) { cout << "[ State: " << state << " ]" << endl; });
  45. pc->onGatheringStateChange([](PeerConnection::GatheringState state) {
  46. cout << "[ Gathering State: " << state << " ]" << endl;
  47. });
  48. shared_ptr<DataChannel> dc = nullptr;
  49. pc->onDataChannel([&](shared_ptr<DataChannel> _dc) {
  50. cout << "[ Got a DataChannel with label: " << _dc->label() << " ]" << endl;
  51. dc = _dc;
  52. dc->onClosed([&]() { cout << "[ DataChannel closed: " << dc->label() << " ]" << endl; });
  53. dc->onMessage([](const variant<binary, string> &message) {
  54. if (holds_alternative<string>(message)) {
  55. cout << "[ Received: " << get<string>(message) << " ]" << endl;
  56. }
  57. });
  58. });
  59. bool exit = false;
  60. while (!exit) {
  61. cout << endl
  62. << endl
  63. << "*************************************************************************" << endl
  64. << "* 0: Exit /"
  65. << " 1: Enter Description /"
  66. << " 2: Enter Candidate /"
  67. << " 3: Send Message *" << endl
  68. << " [Command]: ";
  69. int command;
  70. std::string sdp, candidate, message;
  71. const char *a;
  72. std::unique_ptr<Candidate> candidatePtr;
  73. std::unique_ptr<Description> descPtr;
  74. cin >> command;
  75. switch (command) {
  76. case 0:
  77. exit = true;
  78. break;
  79. case 1:
  80. // Parse Description
  81. cout << "[SDP]: ";
  82. sdp = "";
  83. while (sdp.length() == 0)
  84. getline(cin, sdp);
  85. std::replace(sdp.begin(), sdp.end(), static_cast<char>(94), '\n');
  86. descPtr = std::make_unique<Description>(sdp, Description::Type::Offer,
  87. Description::Role::Passive);
  88. pc->setRemoteDescription(*descPtr);
  89. break;
  90. case 2:
  91. // Parse Candidate
  92. cout << "[Candidate]: ";
  93. candidate = "";
  94. while (candidate.length() == 0)
  95. getline(cin, candidate);
  96. candidatePtr = std::make_unique<Candidate>(candidate);
  97. pc->addRemoteCandidate(*candidatePtr);
  98. break;
  99. case 3:
  100. // Send Message
  101. if (!dc || !dc->isOpen()) {
  102. cout << "** Channel is not Open ** ";
  103. break;
  104. }
  105. cout << "[Message]: ";
  106. message = "";
  107. while (message.length() == 0)
  108. getline(cin, message);
  109. dc->send(message);
  110. break;
  111. default:
  112. cout << "** Invalid Command ** ";
  113. break;
  114. }
  115. }
  116. if (dc)
  117. dc->close();
  118. if (pc)
  119. pc->close();
  120. }