answerer.cpp 3.9 KB

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