main.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * libdatachannel media sender 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 <cstddef>
  12. #include <iostream>
  13. #include <memory>
  14. #include <stdexcept>
  15. #include <utility>
  16. #include <nlohmann/json.hpp>
  17. #ifdef _WIN32
  18. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  19. #include <winsock2.h>
  20. #else
  21. #include <arpa/inet.h>
  22. #include <netinet/in.h>
  23. #include <sys/socket.h>
  24. typedef int SOCKET;
  25. #endif
  26. using nlohmann::json;
  27. const int BUFFER_SIZE = 2048;
  28. int main() {
  29. try {
  30. rtc::InitLogger(rtc::LogLevel::Debug);
  31. auto pc = std::make_shared<rtc::PeerConnection>();
  32. pc->onStateChange(
  33. [](rtc::PeerConnection::State state) { std::cout << "State: " << state << std::endl; });
  34. pc->onGatheringStateChange([pc](rtc::PeerConnection::GatheringState state) {
  35. std::cout << "Gathering State: " << state << std::endl;
  36. if (state == rtc::PeerConnection::GatheringState::Complete) {
  37. auto description = pc->localDescription();
  38. json message = {{"type", description->typeString()},
  39. {"sdp", std::string(description.value())}};
  40. std::cout << message << std::endl;
  41. }
  42. });
  43. SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
  44. struct sockaddr_in addr = {};
  45. addr.sin_family = AF_INET;
  46. addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  47. addr.sin_port = htons(6000);
  48. if (bind(sock, reinterpret_cast<const sockaddr *>(&addr), sizeof(addr)) < 0)
  49. throw std::runtime_error("Failed to bind UDP socket on 127.0.0.1:6000");
  50. int rcvBufSize = 212992;
  51. setsockopt(sock, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<const char *>(&rcvBufSize),
  52. sizeof(rcvBufSize));
  53. const rtc::SSRC ssrc = 42;
  54. rtc::Description::Video media("video", rtc::Description::Direction::SendOnly);
  55. media.addH264Codec(96); // Must match the payload type of the external h264 RTP stream
  56. media.addSSRC(ssrc, "video-send");
  57. auto track = pc->addTrack(media);
  58. pc->setLocalDescription();
  59. std::cout << "RTP video stream expected on localhost:6000" << std::endl;
  60. std::cout << "Please copy/paste the answer provided by the browser: " << std::endl;
  61. std::string sdp;
  62. std::getline(std::cin, sdp);
  63. json j = json::parse(sdp);
  64. rtc::Description answer(j["sdp"].get<std::string>(), j["type"].get<std::string>());
  65. pc->setRemoteDescription(answer);
  66. // Receive from UDP
  67. char buffer[BUFFER_SIZE];
  68. int len;
  69. while ((len = recv(sock, buffer, BUFFER_SIZE, 0)) >= 0) {
  70. if (len < sizeof(rtc::RtpHeader) || !track->isOpen())
  71. continue;
  72. auto rtp = reinterpret_cast<rtc::RtpHeader *>(buffer);
  73. rtp->setSsrc(ssrc);
  74. track->send(reinterpret_cast<const std::byte *>(buffer), len);
  75. }
  76. } catch (const std::exception &e) {
  77. std::cerr << "Error: " << e.what() << std::endl;
  78. }
  79. }