main.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * libdatachannel client example
  3. * Copyright (c) 2020 Staz Modrzynski
  4. * Copyright (c) 2020 Paul-Louis Ageneau
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  20. #include "rtc/rtc.hpp"
  21. #include <iostream>
  22. #include <memory>
  23. #include <nlohmann/json.hpp>
  24. using nlohmann::json;
  25. class Sender {
  26. rtc::PeerConnection conn;
  27. };
  28. struct Receiver {
  29. std::shared_ptr<rtc::PeerConnection> conn;
  30. std::shared_ptr<rtc::Track> track;
  31. };
  32. int main() {
  33. std::vector<std::shared_ptr<Receiver>> receivers;
  34. try {
  35. rtc::InitLogger(rtc::LogLevel::Info);
  36. auto pc = std::make_shared<rtc::PeerConnection>();
  37. pc->onStateChange(
  38. [](rtc::PeerConnection::State state) { std::cout << "State: " << state << std::endl; });
  39. pc->onGatheringStateChange([pc](rtc::PeerConnection::GatheringState state) {
  40. std::cout << "Gathering State: " << state << std::endl;
  41. if (state == rtc::PeerConnection::GatheringState::Complete) {
  42. auto description = pc->localDescription();
  43. json message = {{"type", description->typeString()},
  44. {"sdp", std::string(description.value())}};
  45. std::cout << "Please copy/paste this offer to the SENDER: " << message << std::endl;
  46. }
  47. });
  48. rtc::Description::Video media("video", rtc::Description::Direction::RecvOnly);
  49. media.addH264Codec(96);
  50. media.setBitrate(3000); // Request 3Mbps (Browsers do not encode more than 2.5MBps from a webcam)
  51. auto track = pc->addTrack(media);
  52. pc->setLocalDescription();
  53. auto session = std::make_shared<rtc::RtcpSession>();
  54. track->setRtcpHandler(session);
  55. const rtc::SSRC targetSSRC = 15;
  56. track->onMessage(
  57. [&receivers](rtc::binary message) {
  58. // This is an RTP packet
  59. auto rtp = (rtc::RTP*) message.data();
  60. rtp->ssrc = htonl(targetSSRC);
  61. for (auto pc : receivers) {
  62. if (pc->track != nullptr && pc->track->isOpen()) {
  63. pc->track->send(message);
  64. }
  65. }
  66. },
  67. nullptr);
  68. // Set the SENDERS Answer
  69. {
  70. std::cout << "Please copy/paste the answer provided by the SENDER: " << std::endl;
  71. std::string sdp;
  72. std::getline(std::cin, sdp);
  73. std::cout << "Got answer" << sdp << std::endl;
  74. json j = json::parse(sdp);
  75. rtc::Description answer(j["sdp"].get<std::string>(), j["type"].get<std::string>());
  76. pc->setRemoteDescription(answer);
  77. }
  78. // For each receiver
  79. while (true) {
  80. auto pc = std::make_shared<Receiver>();
  81. pc->conn = std::make_shared<rtc::PeerConnection>();
  82. pc->conn->onStateChange(
  83. [](rtc::PeerConnection::State state) { std::cout << "State: " << state << std::endl; });
  84. pc->conn->onGatheringStateChange([pc](rtc::PeerConnection::GatheringState state) {
  85. std::cout << "Gathering State: " << state << std::endl;
  86. if (state == rtc::PeerConnection::GatheringState::Complete) {
  87. auto description = pc->conn->localDescription();
  88. json message = {{"type", description->typeString()},
  89. {"sdp", std::string(description.value())}};
  90. std::cout << "Please copy/paste this offer to the RECEIVER: " << message << std::endl;
  91. }
  92. });
  93. rtc::Description::Video media("video", rtc::Description::Direction::SendOnly);
  94. media.addH264Codec(96);
  95. media.setBitrate(
  96. 3000); // Request 3Mbps (Browsers do not encode more than 2.5MBps from a webcam)
  97. media.addSSRC(targetSSRC, "video-send");
  98. pc->track = pc->conn->addTrack(media);
  99. pc->conn->setLocalDescription();
  100. auto session = std::make_shared<rtc::RtcpSession>();
  101. pc->track->setRtcpHandler(session);
  102. pc->track->onMessage([](rtc::binary var){}, nullptr);
  103. std::cout << "Please copy/paste the answer provided by the RECEIVER: " << std::endl;
  104. std::string sdp;
  105. std::getline(std::cin, sdp);
  106. std::cout << "Got answer" << sdp << std::endl;
  107. json j = json::parse(sdp);
  108. rtc::Description answer(j["sdp"].get<std::string>(), j["type"].get<std::string>());
  109. pc->conn->setRemoteDescription(answer);
  110. receivers.push_back(pc);
  111. }
  112. } catch (const std::exception &e) {
  113. std::cerr << "Error: " << e.what() << std::endl;
  114. }
  115. }