main.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. template <class T>
  26. weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  27. int main(int argc, char **argv) {
  28. rtc::Configuration config;
  29. // config.iceServers.emplace_back("stun.l.google.com:19302");
  30. auto pc1 = std::make_shared<PeerConnection>(config);
  31. auto pc2 = std::make_shared<PeerConnection>(config);
  32. pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](const Description &sdp) {
  33. auto pc2 = wpc2.lock();
  34. if (!pc2) return;
  35. cout << "Description 1: " << sdp << endl;
  36. pc2->setRemoteDescription(sdp);
  37. });
  38. pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](const Candidate &candidate) {
  39. auto pc2 = wpc2.lock();
  40. if (!pc2) return;
  41. cout << "Candidate 1: " << candidate << endl;
  42. pc2->addRemoteCandidate(candidate);
  43. });
  44. pc1->onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
  45. pc1->onGatheringStateChange([](PeerConnection::GatheringState state) {
  46. cout << "Gathering state 1: " << state << endl;
  47. });
  48. pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](const Description &sdp) {
  49. auto pc1 = wpc1.lock();
  50. if (!pc1) return;
  51. cout << "Description 2: " << sdp << endl;
  52. pc1->setRemoteDescription(sdp);
  53. });
  54. pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](const Candidate &candidate) {
  55. auto pc1 = wpc1.lock();
  56. if (!pc1) return;
  57. cout << "Candidate 2: " << candidate << endl;
  58. pc1->addRemoteCandidate(candidate);
  59. });
  60. pc2->onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
  61. pc2->onGatheringStateChange([](PeerConnection::GatheringState state) {
  62. cout << "Gathering state 2: " << state << endl;
  63. });
  64. shared_ptr<DataChannel> dc2;
  65. pc2->onDataChannel([&dc2](shared_ptr<DataChannel> dc) {
  66. cout << "Got a DataChannel with label: " << dc->label() << endl;
  67. dc2 = dc;
  68. dc2->onMessage([](const variant<binary, string> &message) {
  69. if (holds_alternative<string>(message)) {
  70. cout << "Received 2: " << get<string>(message) << endl;
  71. }
  72. });
  73. dc2->send("Hello from 2");
  74. });
  75. auto dc1 = pc1->createDataChannel("test");
  76. dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() {
  77. auto dc1 = wdc1.lock();
  78. if (!dc1) return;
  79. cout << "DataChannel open: " << dc1->label() << endl;
  80. dc1->send("Hello from 1");
  81. });
  82. dc1->onMessage([](const variant<binary, string> &message) {
  83. if (holds_alternative<string>(message)) {
  84. cout << "Received 1: " << get<string>(message) << endl;
  85. }
  86. });
  87. this_thread::sleep_for(3s);
  88. }