main.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. struct Receiver {
  26. std::shared_ptr<rtc::PeerConnection> conn;
  27. std::shared_ptr<rtc::Track> track;
  28. };
  29. int main() {
  30. std::vector<std::shared_ptr<Receiver>> receivers;
  31. try {
  32. rtc::InitLogger(rtc::LogLevel::Info);
  33. auto pc = std::make_shared<rtc::PeerConnection>();
  34. pc->onStateChange(
  35. [](rtc::PeerConnection::State state) { std::cout << "State: " << state << std::endl; });
  36. pc->onGatheringStateChange([pc](rtc::PeerConnection::GatheringState state) {
  37. std::cout << "Gathering State: " << state << std::endl;
  38. if (state == rtc::PeerConnection::GatheringState::Complete) {
  39. auto description = pc->localDescription();
  40. json message = {{"type", description->typeString()},
  41. {"sdp", std::string(description.value())}};
  42. std::cout << "Please copy/paste this offer to the SENDER: " << message << std::endl;
  43. }
  44. });
  45. rtc::Description::Video media("video", rtc::Description::Direction::RecvOnly);
  46. media.addH264Codec(96);
  47. media.setBitrate(
  48. 3000); // Request 3Mbps (Browsers do not encode more than 2.5MBps from a webcam)
  49. auto track = pc->addTrack(media);
  50. pc->setLocalDescription();
  51. auto session = std::make_shared<rtc::RtcpReceivingSession>();
  52. track->setRtcpHandler(session);
  53. const rtc::SSRC targetSSRC = 4;
  54. track->onMessage(
  55. [&receivers, targetSSRC](rtc::binary message) {
  56. // This is an RTP packet
  57. auto rtp = (rtc::RTP *)message.data();
  58. rtp->setSsrc(targetSSRC);
  59. for (auto pc : receivers) {
  60. if (pc->track != nullptr && pc->track->isOpen()) {
  61. pc->track->send(message);
  62. }
  63. }
  64. },
  65. nullptr);
  66. // Set the SENDERS Answer
  67. {
  68. std::cout << "Please copy/paste the answer provided by the SENDER: " << std::endl;
  69. std::string sdp;
  70. std::getline(std::cin, sdp);
  71. std::cout << "Got answer" << sdp << std::endl;
  72. json j = json::parse(sdp);
  73. rtc::Description answer(j["sdp"].get<std::string>(), j["type"].get<std::string>());
  74. pc->setRemoteDescription(answer);
  75. }
  76. // For each receiver
  77. while (true) {
  78. auto pc = std::make_shared<Receiver>();
  79. pc->conn = std::make_shared<rtc::PeerConnection>();
  80. pc->conn->onStateChange([](rtc::PeerConnection::State state) {
  81. std::cout << "State: " << state << std::endl;
  82. });
  83. pc->conn->onGatheringStateChange([pc](rtc::PeerConnection::GatheringState state) {
  84. std::cout << "Gathering State: " << state << std::endl;
  85. if (state == rtc::PeerConnection::GatheringState::Complete) {
  86. auto description = pc->conn->localDescription();
  87. json message = {{"type", description->typeString()},
  88. {"sdp", std::string(description.value())}};
  89. std::cout << "Please copy/paste this offer to the RECEIVER: " << message
  90. << 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. pc->track->onMessage([](rtc::binary var) {}, nullptr);
  101. std::cout << "Please copy/paste the answer provided by the RECEIVER: " << std::endl;
  102. std::string sdp;
  103. std::getline(std::cin, sdp);
  104. std::cout << "Got answer" << sdp << std::endl;
  105. json j = json::parse(sdp);
  106. rtc::Description answer(j["sdp"].get<std::string>(), j["type"].get<std::string>());
  107. pc->conn->setRemoteDescription(answer);
  108. receivers.push_back(pc);
  109. }
  110. } catch (const std::exception &e) {
  111. std::cerr << "Error: " << e.what() << std::endl;
  112. }
  113. }