track.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #include "rtc/rtc.hpp"
  9. #include "rtc/rtp.hpp"
  10. #include <atomic>
  11. #include <cstring>
  12. #include <future>
  13. #include <iostream>
  14. #include <memory>
  15. #include <mutex>
  16. #include <thread>
  17. using namespace rtc;
  18. using namespace std;
  19. static const auto RtpHeaderSize = 12;
  20. template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  21. void test_track() {
  22. InitLogger(LogLevel::Debug);
  23. Configuration config1;
  24. // STUN server example
  25. // config1.iceServers.emplace_back("stun:stun.l.google.com:19302");
  26. PeerConnection pc1(config1);
  27. Configuration config2;
  28. // STUN server example
  29. // config2.iceServers.emplace_back("stun:stun.l.google.com:19302");
  30. // Port range example
  31. config2.portRangeBegin = 5000;
  32. config2.portRangeEnd = 6000;
  33. PeerConnection pc2(config2);
  34. pc1.onLocalDescription([&pc2](Description sdp) {
  35. cout << "Description 1: " << sdp << endl;
  36. pc2.setRemoteDescription(string(sdp));
  37. });
  38. pc1.onLocalCandidate([&pc2](Candidate candidate) {
  39. cout << "Candidate 1: " << candidate << endl;
  40. pc2.addRemoteCandidate(string(candidate));
  41. });
  42. pc1.onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
  43. pc1.onGatheringStateChange([](PeerConnection::GatheringState state) {
  44. cout << "Gathering state 1: " << state << endl;
  45. });
  46. pc2.onLocalDescription([&pc1](Description sdp) {
  47. cout << "Description 2: " << sdp << endl;
  48. pc1.setRemoteDescription(string(sdp));
  49. });
  50. pc2.onLocalCandidate([&pc1](Candidate candidate) {
  51. cout << "Candidate 2: " << candidate << endl;
  52. pc1.addRemoteCandidate(string(candidate));
  53. });
  54. pc2.onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
  55. pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
  56. cout << "Gathering state 2: " << state << endl;
  57. });
  58. shared_ptr<Track> t2;
  59. string newTrackMid;
  60. std::promise<rtc::binary> recvRtpPromise;
  61. pc2.onTrack([&t2, &newTrackMid, &recvRtpPromise](shared_ptr<Track> t) {
  62. string mid = t->mid();
  63. cout << "Track 2: Received track with mid \"" << mid << "\"" << endl;
  64. if (mid != newTrackMid) {
  65. cerr << "Wrong track mid" << endl;
  66. return;
  67. }
  68. t->onOpen([mid]() { cout << "Track 2: Track with mid \"" << mid << "\" is open" << endl; });
  69. t->onClosed(
  70. [mid]() { cout << "Track 2: Track with mid \"" << mid << "\" is closed" << endl; });
  71. t->onMessage(
  72. [&recvRtpPromise](rtc::binary message) {
  73. // This is an RTP packet
  74. recvRtpPromise.set_value(message);
  75. },
  76. nullptr);
  77. std::atomic_store(&t2, t);
  78. });
  79. // Test opening a track
  80. newTrackMid = "test";
  81. Description::Video media(newTrackMid, Description::Direction::SendOnly);
  82. media.addH264Codec(96);
  83. media.setBitrate(3000);
  84. media.addSSRC(1234, "video-send");
  85. const auto mediaSdp1 = string(media);
  86. const auto mediaSdp2 = string(Description::Media(mediaSdp1));
  87. if (mediaSdp2 != mediaSdp1) {
  88. cout << mediaSdp2 << endl;
  89. throw runtime_error("Media description parsing test failed");
  90. }
  91. auto t1 = pc1.addTrack(media);
  92. pc1.setLocalDescription();
  93. int attempts = 10;
  94. shared_ptr<Track> at2;
  95. while ((!(at2 = std::atomic_load(&t2)) || !at2->isOpen() || !t1->isOpen()) && attempts--)
  96. this_thread::sleep_for(1s);
  97. if (pc1.state() != PeerConnection::State::Connected ||
  98. pc2.state() != PeerConnection::State::Connected)
  99. throw runtime_error("PeerConnection is not connected");
  100. if (!at2 || !at2->isOpen() || !t1->isOpen())
  101. throw runtime_error("Track is not open");
  102. // Test renegotiation
  103. newTrackMid = "added";
  104. Description::Video media2(newTrackMid, Description::Direction::SendOnly);
  105. media2.addH264Codec(96);
  106. media2.setBitrate(3000);
  107. media2.addSSRC(2468, "video-send");
  108. // NOTE: Overwriting the old shared_ptr for t1 will cause it's respective
  109. // track to be dropped (so it's SSRCs won't be on the description next time)
  110. t1 = pc1.addTrack(media2);
  111. pc1.setLocalDescription();
  112. attempts = 10;
  113. t2.reset();
  114. while ((!(at2 = std::atomic_load(&t2)) || !at2->isOpen() || !t1->isOpen()) && attempts--)
  115. this_thread::sleep_for(1s);
  116. if (!at2 || !at2->isOpen() || !t1->isOpen())
  117. throw runtime_error("Renegotiated track is not open");
  118. std::vector<std::byte> payload = {std::byte{0}, std::byte{1}, std::byte{2}, std::byte{3}};
  119. std::vector<std::byte> rtpRaw(RtpHeaderSize + payload.size());
  120. auto *rtp = (RtpHeader *)rtpRaw.data();
  121. rtp->setPayloadType(96);
  122. rtp->setSeqNumber(1);
  123. rtp->setTimestamp(3000);
  124. rtp->setSsrc(2468);
  125. rtp->preparePacket();
  126. std::memcpy(rtpRaw.data() + RtpHeaderSize, payload.data(), payload.size());
  127. if (!t1->send(rtpRaw.data(), rtpRaw.size())) {
  128. throw runtime_error("Couldn't send RTP packet");
  129. }
  130. // wait for an RTP packet to be received
  131. auto future = recvRtpPromise.get_future();
  132. if (future.wait_for(5s) == std::future_status::timeout) {
  133. throw runtime_error("Didn't receive RTP packet on pc2");
  134. }
  135. auto receivedRtpRaw = future.get();
  136. if (receivedRtpRaw.empty()) {
  137. throw runtime_error("Didn't receive RTP packet on pc2");
  138. }
  139. if (receivedRtpRaw.size() != rtpRaw.size() ||
  140. memcmp(receivedRtpRaw.data(), rtpRaw.data(), rtpRaw.size()) != 0) {
  141. throw runtime_error("Received RTP packet is different than the packet that was sent");
  142. }
  143. // Delay close of peer 2 to check closing works properly
  144. pc1.close();
  145. this_thread::sleep_for(1s);
  146. pc2.close();
  147. this_thread::sleep_for(1s);
  148. if (!t1->isClosed() || !t2->isClosed())
  149. throw runtime_error("Track is not closed");
  150. cout << "Success" << endl;
  151. }