2
0

main.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * Copyright (c) 2019-2020 Paul-Louis Ageneau
  3. * Copyright (c) 2019 Murat Dogan
  4. * Copyright (c) 2020 Will Munn
  5. * Copyright (c) 2020 Lara Mackey
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "rtc/rtc.hpp"
  22. #include <nlohmann/json.hpp>
  23. #include <iostream>
  24. #include <memory>
  25. using namespace rtc;
  26. using namespace std;
  27. using namespace std::chrono_literals;
  28. using json = nlohmann::json;
  29. template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  30. int main(int argc, char **argv) {
  31. rtc::InitLogger(LogLevel::Warning);
  32. Configuration config;
  33. config.iceServers.emplace_back("stun.l.google.com:19302"); // change to your STUN server
  34. auto pc = std::make_shared<PeerConnection>(config);
  35. const std::string url = "ws://localhost:8000/";
  36. auto ws = std::make_shared<WebSocket>(url);
  37. ws->onOpen([]() {
  38. });
  39. ws->onMessage([wpc = make_weak_ptr(pc)](const std::variant<binary, string> &data) {
  40. auto pc = wpc.lock();
  41. if (pc && holds_alternative<string>(data)) {
  42. json message = json::parse(get<string>(data));
  43. if (auto it = message.find("type"); it != message.end()) {
  44. auto type = it->get<string>();
  45. if (type == "offer" || type == "answer") {
  46. auto str = message["description"].get<string>();
  47. pc->setRemoteDescription(Description(str, type));
  48. } else if (type == "candidate") {
  49. auto str = message["candidate"].get<string>();
  50. auto mid = message["mid"].get<string>();
  51. pc->addRemoteCandidate(Candidate(str, mid));
  52. }
  53. }
  54. }
  55. });
  56. ws->onClosed([]() {
  57. });
  58. pc->onLocalDescription([wws = make_weak_ptr(ws)](const Description &description) {
  59. json message = {{"type", description.typeString()},
  60. {"description", std::string(description)}};
  61. if (auto ws = wws.lock())
  62. ws->send(message.dump());
  63. });
  64. pc->onLocalCandidate([wws = make_weak_ptr(ws)](const Candidate &candidate) {
  65. json message = {
  66. {"type", "candidate"}, {"candidate", std::string(candidate)}, {"mid", candidate.mid()}};
  67. if (auto ws = wws.lock())
  68. ws->send(message.dump());
  69. });
  70. pc->onStateChange([wpc = make_weak_ptr(pc)](PeerConnection::State state) {
  71. cout << "State: " << state << endl;
  72. });
  73. pc->onGatheringStateChange(
  74. [](PeerConnection::GatheringState state) { cout << "Gathering State: " << state << endl; });
  75. auto dc = pc->createDataChannel("test"); // this is the offerer, so create a data channel
  76. dc->onOpen([&]() { cout << "DataChannel open: " << dc->label() << endl; });
  77. dc->onClosed([&]() { cout << "DataChannel closed: " << dc->label() << endl; });
  78. dc->onMessage([](const variant<binary, string> &message) {
  79. if (holds_alternative<string>(message)) {
  80. cout << "Received: " << get<string>(message) << endl;
  81. }
  82. });
  83. // TODO
  84. if (dc)
  85. dc->close();
  86. if (pc)
  87. pc->close();
  88. }