main.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * libdatachannel media receiver 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 <utility>
  14. #include <nlohmann/json.hpp>
  15. #ifdef _WIN32
  16. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  17. #include <winsock2.h>
  18. #else
  19. #include <arpa/inet.h>
  20. #include <netinet/in.h>
  21. #include <sys/socket.h>
  22. typedef int SOCKET;
  23. #endif
  24. using nlohmann::json;
  25. int main() {
  26. try {
  27. rtc::InitLogger(rtc::LogLevel::Debug);
  28. auto pc = std::make_shared<rtc::PeerConnection>();
  29. pc->onStateChange(
  30. [](rtc::PeerConnection::State state) { std::cout << "State: " << state << std::endl; });
  31. pc->onGatheringStateChange([pc](rtc::PeerConnection::GatheringState state) {
  32. std::cout << "Gathering State: " << state << std::endl;
  33. if (state == rtc::PeerConnection::GatheringState::Complete) {
  34. auto description = pc->localDescription();
  35. json message = {{"type", description->typeString()},
  36. {"sdp", std::string(description.value())}};
  37. std::cout << message << std::endl;
  38. }
  39. });
  40. SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
  41. sockaddr_in addr = {};
  42. addr.sin_family = AF_INET;
  43. addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  44. addr.sin_port = htons(5000);
  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. track->onMessage(
  53. [session, sock, addr](rtc::binary message) {
  54. // This is an RTP packet
  55. sendto(sock, reinterpret_cast<const char *>(message.data()), int(message.size()), 0,
  56. reinterpret_cast<const struct sockaddr *>(&addr), sizeof(addr));
  57. },
  58. nullptr);
  59. pc->setLocalDescription();
  60. std::cout << "Expect RTP video traffic on localhost:5000" << std::endl;
  61. std::cout << "Please copy/paste the answer provided by the browser: " << std::endl;
  62. std::string sdp;
  63. std::getline(std::cin, sdp);
  64. std::cout << "Got answer" << sdp << std::endl;
  65. json j = json::parse(sdp);
  66. rtc::Description answer(j["sdp"].get<std::string>(), j["type"].get<std::string>());
  67. pc->setRemoteDescription(answer);
  68. std::cout << "Press any key to exit." << std::endl;
  69. char dummy;
  70. std::cin >> dummy;
  71. } catch (const std::exception &e) {
  72. std::cerr << "Error: " << e.what() << std::endl;
  73. }
  74. }