main.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * libdatachannel media 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 <utility>
  23. #include <nlohmann/json.hpp>
  24. #ifdef _WIN32
  25. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  26. #include <winsock2.h>
  27. #else
  28. #include <arpa/inet.h>
  29. #include <netinet/in.h>
  30. #include <sys/socket.h>
  31. typedef int SOCKET;
  32. #endif
  33. using nlohmann::json;
  34. int main() {
  35. try {
  36. rtc::InitLogger(rtc::LogLevel::Debug);
  37. auto pc = std::make_shared<rtc::PeerConnection>();
  38. pc->onStateChange(
  39. [](rtc::PeerConnection::State state) { std::cout << "State: " << state << std::endl; });
  40. pc->onGatheringStateChange([pc](rtc::PeerConnection::GatheringState state) {
  41. std::cout << "Gathering State: " << state << std::endl;
  42. if (state == rtc::PeerConnection::GatheringState::Complete) {
  43. auto description = pc->localDescription();
  44. json message = {{"type", description->typeString()},
  45. {"sdp", std::string(description.value())}};
  46. std::cout << message << std::endl;
  47. }
  48. });
  49. SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
  50. sockaddr_in addr = {};
  51. addr.sin_family = AF_INET;
  52. addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  53. addr.sin_port = htons(5000);
  54. rtc::Description::Video media("video", rtc::Description::Direction::RecvOnly);
  55. media.addH264Codec(96);
  56. media.setBitrate(
  57. 3000); // Request 3Mbps (Browsers do not encode more than 2.5MBps from a webcam)
  58. auto track = pc->addTrack(media);
  59. auto session = std::make_shared<rtc::RtcpReceivingSession>();
  60. track->setMediaHandler(session);
  61. track->onMessage(
  62. [session, sock, addr](rtc::binary message) {
  63. // This is an RTP packet
  64. sendto(sock, reinterpret_cast<const char *>(message.data()), int(message.size()), 0,
  65. reinterpret_cast<const struct sockaddr *>(&addr), sizeof(addr));
  66. },
  67. nullptr);
  68. pc->setLocalDescription();
  69. std::cout << "Expect RTP video traffic on localhost:5000" << std::endl;
  70. std::cout << "Please copy/paste the answer provided by the browser: " << std::endl;
  71. std::string sdp;
  72. std::getline(std::cin, sdp);
  73. std::cout << "Got answer" << sdp << std::endl;
  74. json j = json::parse(sdp);
  75. rtc::Description answer(j["sdp"].get<std::string>(), j["type"].get<std::string>());
  76. pc->setRemoteDescription(answer);
  77. std::cout << "Press any key to exit." << std::endl;
  78. std::cin >> sdp;
  79. } catch (const std::exception &e) {
  80. std::cerr << "Error: " << e.what() << std::endl;
  81. }
  82. }