offerer.cpp 3.9 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. // Add TURN Server Example
  31. // IceServer turnServer("TURN_SERVER_URL", "PORT_NO", "USERNAME", "PASSWORD",
  32. // IceServer::RelayType::TurnTls);
  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. auto dc = pc->createDataChannel("test");
  49. dc->onOpen([&]() { cout << "[ DataChannel open: " << dc->label() << " ]" << endl; });
  50. dc->onClosed([&]() { cout << "[ DataChannel closed: " << dc->label() << " ]" << endl; });
  51. dc->onMessage([](const variant<binary, string> &message) {
  52. if (holds_alternative<string>(message)) {
  53. cout << "[ Received: " << get<string>(message) << " ]" << endl;
  54. }
  55. });
  56. bool exit = false;
  57. while (!exit) {
  58. cout << endl
  59. << endl
  60. << "*************************************************************************" << endl
  61. << "* 0: Exit /"
  62. << " 1: Enter Description /"
  63. << " 2: Enter Candidate /"
  64. << " 3: Send Message *" << endl
  65. << " [Command]: ";
  66. int command;
  67. std::string sdp, candidate, message;
  68. const char *a;
  69. std::unique_ptr<Candidate> candidatePtr;
  70. std::unique_ptr<Description> descPtr;
  71. cin >> command;
  72. switch (command) {
  73. case 0:
  74. exit = true;
  75. break;
  76. case 1:
  77. // Parse Description
  78. cout << "[SDP]: ";
  79. sdp = "";
  80. while (sdp.length() == 0)
  81. getline(cin, sdp);
  82. std::replace(sdp.begin(), sdp.end(), static_cast<char>(94), '\n');
  83. descPtr = std::make_unique<Description>(sdp);
  84. pc->setRemoteDescription(*descPtr);
  85. break;
  86. case 2:
  87. // Parse Candidate
  88. cout << "[Candidate]: ";
  89. candidate = "";
  90. while (candidate.length() == 0)
  91. getline(cin, candidate);
  92. candidatePtr = std::make_unique<Candidate>(candidate);
  93. pc->addRemoteCandidate(*candidatePtr);
  94. break;
  95. case 3:
  96. // Send Message
  97. if (!dc->isOpen()) {
  98. cout << "** Channel is not Open ** ";
  99. break;
  100. }
  101. cout << "[Message]: ";
  102. message = "";
  103. while (message.length() == 0)
  104. getline(cin, message);
  105. dc->send(message);
  106. if (dc)
  107. dc->close();
  108. if (pc)
  109. pc->close();
  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. }