main.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * libdatachannel client example
  3. * Copyright (c) 2020 Staz Modrzynski
  4. * Copyright (c) 2020 Paul-Louis Ageneau
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  9. */
  10. #include "rtc/rtc.hpp"
  11. #include <iostream>
  12. #include <memory>
  13. #include <vector>
  14. #include <nlohmann/json.hpp>
  15. using nlohmann::json;
  16. struct Receiver {
  17. std::shared_ptr<rtc::PeerConnection> conn;
  18. std::shared_ptr<rtc::Track> track;
  19. };
  20. int main() {
  21. std::vector<std::shared_ptr<Receiver>> receivers;
  22. try {
  23. rtc::InitLogger(rtc::LogLevel::Info);
  24. auto pc = std::make_shared<rtc::PeerConnection>();
  25. pc->onStateChange(
  26. [](rtc::PeerConnection::State state) { std::cout << "State: " << state << std::endl; });
  27. pc->onGatheringStateChange([pc](rtc::PeerConnection::GatheringState state) {
  28. std::cout << "Gathering State: " << state << std::endl;
  29. if (state == rtc::PeerConnection::GatheringState::Complete) {
  30. auto description = pc->localDescription();
  31. json message = {{"type", description->typeString()},
  32. {"sdp", std::string(description.value())}};
  33. std::cout << "Please copy/paste this offer to the SENDER: " << message << std::endl;
  34. }
  35. });
  36. rtc::Description::Video media("video", rtc::Description::Direction::RecvOnly);
  37. media.addH264Codec(96);
  38. media.setBitrate(
  39. 3000); // Request 3Mbps (Browsers do not encode more than 2.5MBps from a webcam)
  40. auto track = pc->addTrack(media);
  41. track->setMediaHandler(std::make_shared<rtc::RtcpReceivingSession>());
  42. const rtc::SSRC targetSSRC = 42;
  43. track->onMessage(
  44. [&receivers, targetSSRC](rtc::binary message) {
  45. // This is an RTP packet
  46. auto rtp = reinterpret_cast<rtc::RtpHeader *>(message.data());
  47. rtp->setSsrc(targetSSRC);
  48. for (auto pc : receivers) {
  49. if (pc->track != nullptr && pc->track->isOpen()) {
  50. pc->track->send(message);
  51. }
  52. }
  53. },
  54. nullptr);
  55. pc->setLocalDescription();
  56. // Set the sender's answer
  57. std::cout << "Please copy/paste the answer provided by the SENDER: " << std::endl;
  58. std::string sdp;
  59. std::getline(std::cin, sdp);
  60. std::cout << "Got answer" << sdp << std::endl;
  61. json j = json::parse(sdp);
  62. rtc::Description answer(j["sdp"].get<std::string>(), j["type"].get<std::string>());
  63. pc->setRemoteDescription(answer);
  64. // For each receiver
  65. while (true) {
  66. auto r = std::make_shared<Receiver>();
  67. r->conn = std::make_shared<rtc::PeerConnection>();
  68. r->conn->onStateChange([](rtc::PeerConnection::State state) {
  69. std::cout << "State: " << state << std::endl;
  70. });
  71. r->conn->onGatheringStateChange([r](rtc::PeerConnection::GatheringState state) {
  72. std::cout << "Gathering State: " << state << std::endl;
  73. if (state == rtc::PeerConnection::GatheringState::Complete) {
  74. auto description = r->conn->localDescription();
  75. json message = {{"type", description->typeString()},
  76. {"sdp", std::string(description.value())}};
  77. std::cout << "Please copy/paste this offer to the RECEIVER: " << message
  78. << std::endl;
  79. }
  80. });
  81. rtc::Description::Video media("video", rtc::Description::Direction::SendOnly);
  82. media.addH264Codec(96);
  83. media.setBitrate(3000);
  84. media.addSSRC(targetSSRC, "video-send");
  85. r->track = r->conn->addTrack(media);
  86. r->track->onOpen([r]() {
  87. r->track->requestKeyframe(); // So the receiver can start playing immediately
  88. });
  89. r->track->onMessage([](rtc::binary var) {}, nullptr);
  90. r->conn->setLocalDescription();
  91. std::cout << "Please copy/paste the answer provided by the RECEIVER: " << std::endl;
  92. std::string sdp;
  93. std::getline(std::cin, sdp);
  94. std::cout << "Got answer" << sdp << std::endl;
  95. json j = json::parse(sdp);
  96. rtc::Description answer(j["sdp"].get<std::string>(), j["type"].get<std::string>());
  97. r->conn->setRemoteDescription(answer);
  98. receivers.push_back(r);
  99. }
  100. } catch (const std::exception &e) {
  101. std::cerr << "Error: " << e.what() << std::endl;
  102. }
  103. }