answerer.cpp 3.5 KB

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