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. #include "rtc/rtc.hpp"
  20. #include <iostream>
  21. #include <memory>
  22. #include <vector>
  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. auto session = std::make_shared<rtc::RtcpReceivingSession>();
  51. track->setMediaHandler(session);
  52. const rtc::SSRC targetSSRC = 42;
  53. track->onMessage(
  54. [&receivers, targetSSRC](rtc::binary message) {
  55. // This is an RTP packet
  56. auto rtp = reinterpret_cast<rtc::RtpHeader *>(message.data());
  57. rtp->setSsrc(targetSSRC);
  58. for (auto pc : receivers) {
  59. if (pc->track != nullptr && pc->track->isOpen()) {
  60. pc->track->send(message);
  61. }
  62. }
  63. },
  64. nullptr);
  65. pc->setLocalDescription();
  66. // Set the sender's answer
  67. std::cout << "Please copy/paste the answer provided by the SENDER: " << std::endl;
  68. std::string sdp;
  69. std::getline(std::cin, sdp);
  70. std::cout << "Got answer" << sdp << std::endl;
  71. json j = json::parse(sdp);
  72. rtc::Description answer(j["sdp"].get<std::string>(), j["type"].get<std::string>());
  73. pc->setRemoteDescription(answer);
  74. // For each receiver
  75. while (true) {
  76. auto r = std::make_shared<Receiver>();
  77. r->conn = std::make_shared<rtc::PeerConnection>();
  78. r->conn->onStateChange([](rtc::PeerConnection::State state) {
  79. std::cout << "State: " << state << std::endl;
  80. });
  81. r->conn->onGatheringStateChange([r](rtc::PeerConnection::GatheringState state) {
  82. std::cout << "Gathering State: " << state << std::endl;
  83. if (state == rtc::PeerConnection::GatheringState::Complete) {
  84. auto description = r->conn->localDescription();
  85. json message = {{"type", description->typeString()},
  86. {"sdp", std::string(description.value())}};
  87. std::cout << "Please copy/paste this offer to the RECEIVER: " << message
  88. << std::endl;
  89. }
  90. });
  91. rtc::Description::Video media("video", rtc::Description::Direction::SendOnly);
  92. media.addH264Codec(96);
  93. media.setBitrate(3000);
  94. media.addSSRC(targetSSRC, "video-send");
  95. r->track = r->conn->addTrack(media);
  96. r->track->onOpen([session]() {
  97. session->requestKeyframe(); // So the receiver can start playing immediately
  98. });
  99. r->track->onMessage([](rtc::binary var) {}, nullptr);
  100. r->conn->setLocalDescription();
  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. r->conn->setRemoteDescription(answer);
  108. receivers.push_back(r);
  109. }
  110. } catch (const std::exception &e) {
  111. std::cerr << "Error: " << e.what() << std::endl;
  112. }
  113. }