main.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  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. int main(int argc, char **argv) {
  26. rtc::Configuration config;
  27. config.iceServers.emplace_back("stun.l.google.com:19302");
  28. auto pc1 = std::make_shared<PeerConnection>(config);
  29. auto pc2 = std::make_shared<PeerConnection>(config);
  30. pc1->onLocalDescription([pc2](const Description &sdp) {
  31. cout << "Description 1: " << sdp << endl;
  32. pc2->setRemoteDescription(sdp);
  33. });
  34. pc1->onLocalCandidate([pc2](const optional<Candidate> &candidate) {
  35. if (candidate) {
  36. cout << "Candidate 1: " << *candidate << endl;
  37. pc2->addRemoteCandidate(*candidate);
  38. }
  39. });
  40. pc2->onLocalDescription([pc1](const Description &sdp) {
  41. cout << "Description 2: " << sdp << endl;
  42. pc1->setRemoteDescription(sdp);
  43. });
  44. pc2->onLocalCandidate([pc1](const optional<Candidate> &candidate) {
  45. if (candidate) {
  46. cout << "Candidate 2: " << *candidate << endl;
  47. pc1->addRemoteCandidate(*candidate);
  48. }
  49. });
  50. shared_ptr<DataChannel> dc2;
  51. pc2->onDataChannel([&dc2](shared_ptr<DataChannel> dc) {
  52. cout << "Got a DataChannel with label: " << dc->label() << endl;
  53. dc2 = dc;
  54. dc2->send("Hello world!");
  55. });
  56. auto dc1 = pc1->createDataChannel("test");
  57. dc1->onOpen([dc1]() {
  58. cout << "DataChannel open: " << dc1->label() << endl;
  59. });
  60. dc1->onMessage([](const variant<binary, string> &message) {
  61. if (holds_alternative<string>(message)) {
  62. cout << "Received: " << get<string>(message) << endl;
  63. }
  64. });
  65. this_thread::sleep_for(10s);
  66. }