main.cpp 2.2 KB

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