answerer.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #include <thread>
  23. #ifdef _WIN32
  24. #include <winsock2.h>
  25. #endif
  26. using namespace rtc;
  27. using namespace std;
  28. template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  29. int main(int argc, char **argv) {
  30. InitLogger(LogLevel::Debug);
  31. #ifdef _WIN32
  32. WSADATA wsaData;
  33. if (WSAStartup(MAKEWORD(2, 2), &wsaData))
  34. throw std::runtime_error("WSAStartup failed, error=" + std::to_string(WSAGetLastError()));
  35. #endif
  36. Configuration config;
  37. // config.iceServers.emplace_back("stun.l.google.com:19302");
  38. auto pc = std::make_shared<PeerConnection>(config);
  39. pc->onLocalDescription([](const Description &description) {
  40. cout << "Local Description (Paste this to the other peer):" << endl;
  41. cout << string(description) << endl;
  42. });
  43. pc->onLocalCandidate([](const Candidate &candidate) {
  44. cout << "Local Candidate (Paste this to the other peer after the local description):"
  45. << endl;
  46. cout << string(candidate) << endl << endl;
  47. });
  48. pc->onStateChange(
  49. [](PeerConnection::State state) { cout << "[State: " << state << "]" << endl; });
  50. pc->onGatheringStateChange([](PeerConnection::GatheringState state) {
  51. cout << "[Gathering State: " << state << "]" << endl;
  52. });
  53. shared_ptr<DataChannel> dc = nullptr;
  54. pc->onDataChannel([&](shared_ptr<DataChannel> _dc) {
  55. cout << "[Got a DataChannel with label: " << _dc->label() << "]" << endl;
  56. dc = _dc;
  57. dc->onClosed([&]() { cout << "[DataChannel closed: " << dc->label() << "]" << endl; });
  58. dc->onMessage([](const variant<binary, string> &message) {
  59. if (holds_alternative<string>(message)) {
  60. cout << "[Received message: " << get<string>(message) << "]" << endl;
  61. }
  62. });
  63. });
  64. bool exit = false;
  65. while (!exit) {
  66. cout << endl
  67. << "**********************************************************************************"
  68. "*****"
  69. << endl
  70. << "* 0: Exit /"
  71. << " 1: Enter remote description /"
  72. << " 2: Enter remote candidate /"
  73. << " 3: Send message *" << endl
  74. << "[Command]: ";
  75. int command = -1;
  76. cin >> command;
  77. cin.ignore();
  78. switch (command) {
  79. case 0: {
  80. exit = true;
  81. break;
  82. }
  83. case 1: {
  84. // Parse Description
  85. cout << "[Description]: ";
  86. string sdp, line;
  87. while (getline(cin, line) && !line.empty()) {
  88. sdp += line;
  89. sdp += "\r\n";
  90. }
  91. std::cout << sdp;
  92. pc->setRemoteDescription(sdp);
  93. break;
  94. }
  95. case 2: {
  96. // Parse Candidate
  97. cout << "[Candidate]: ";
  98. string candidate;
  99. getline(cin, candidate);
  100. pc->addRemoteCandidate(candidate);
  101. break;
  102. }
  103. case 3: {
  104. // Send Message
  105. if (!dc || !dc->isOpen()) {
  106. cout << "** Channel is not Open ** ";
  107. break;
  108. }
  109. cout << "[Message]: ";
  110. string message;
  111. getline(cin, message);
  112. dc->send(message);
  113. break;
  114. }
  115. default: {
  116. cout << "** Invalid Command ** ";
  117. break;
  118. }
  119. }
  120. }
  121. if (dc)
  122. dc->close();
  123. if (pc)
  124. pc->close();
  125. #ifdef _WIN32
  126. WSACleanup();
  127. #endif
  128. }