answerer.cpp 4.5 KB

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