offerer.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. auto dc = pc->createDataChannel("test");
  65. dc->onOpen([&]() { cout << "[ DataChannel open: " << dc->label() << " ]" << endl; });
  66. dc->onClosed([&]() { cout << "[ DataChannel closed: " << dc->label() << " ]" << endl; });
  67. dc->onMessage([](const variant<binary, string> &message) {
  68. if (holds_alternative<string>(message)) {
  69. cout << "[ Received: " << get<string>(message) << " ]" << endl;
  70. }
  71. });
  72. bool exit = false;
  73. while (!exit) {
  74. cout << endl
  75. << endl
  76. << "*************************************************************************" << endl
  77. << "* 0: Exit /"
  78. << " 1: Enter Description /"
  79. << " 2: Enter Candidate /"
  80. << " 3: Send Message *" << endl
  81. << " [Command]: ";
  82. int command;
  83. std::string sdp, candidate, message;
  84. const char *a;
  85. std::unique_ptr<Candidate> candidatePtr;
  86. std::unique_ptr<Description> descPtr;
  87. cin >> command;
  88. switch (command) {
  89. case 0:
  90. exit = true;
  91. break;
  92. case 1:
  93. // Parse Description
  94. cout << "[SDP]: ";
  95. sdp = "";
  96. while (sdp.length() == 0)
  97. getline(cin, sdp);
  98. std::replace(sdp.begin(), sdp.end(), static_cast<char>(94), '\n');
  99. std::replace(sdp.begin(), sdp.end(), static_cast<char>(95), '\r');
  100. descPtr = std::make_unique<Description>(sdp);
  101. pc->setRemoteDescription(*descPtr);
  102. break;
  103. case 2:
  104. // Parse Candidate
  105. cout << "[Candidate]: ";
  106. candidate = "";
  107. while (candidate.length() == 0)
  108. getline(cin, candidate);
  109. candidatePtr = std::make_unique<Candidate>(candidate);
  110. pc->addRemoteCandidate(*candidatePtr);
  111. break;
  112. case 3:
  113. // Send Message
  114. if (!dc->isOpen()) {
  115. cout << "** Channel is not Open ** ";
  116. break;
  117. }
  118. cout << "[Message]: ";
  119. message = "";
  120. while (message.length() == 0)
  121. getline(cin, message);
  122. dc->send(message);
  123. break;
  124. default:
  125. cout << "** Invalid Command ** ";
  126. break;
  127. }
  128. }
  129. if (dc)
  130. dc->close();
  131. if (pc)
  132. pc->close();
  133. #ifdef _WIN32
  134. WSACleanup();
  135. #endif
  136. }