track.cpp 5.6 KB

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