track.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "rtc/rtc.hpp"
  19. #include <atomic>
  20. #include <chrono>
  21. #include <iostream>
  22. #include <memory>
  23. #include <thread>
  24. using namespace rtc;
  25. using namespace std;
  26. template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  27. void test_track() {
  28. InitLogger(LogLevel::Debug);
  29. Configuration config1;
  30. // STUN server example
  31. // config1.iceServers.emplace_back("stun:stun.l.google.com:19302");
  32. PeerConnection pc1(config1);
  33. Configuration config2;
  34. // STUN server example
  35. // config2.iceServers.emplace_back("stun:stun.l.google.com:19302");
  36. // Port range example
  37. config2.portRangeBegin = 5000;
  38. config2.portRangeEnd = 6000;
  39. PeerConnection pc2(config2);
  40. pc1.onLocalDescription([&pc2](Description sdp) {
  41. cout << "Description 1: " << sdp << endl;
  42. pc2.setRemoteDescription(string(sdp));
  43. });
  44. pc1.onLocalCandidate([&pc2](Candidate candidate) {
  45. cout << "Candidate 1: " << candidate << endl;
  46. pc2.addRemoteCandidate(string(candidate));
  47. });
  48. pc1.onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
  49. pc1.onGatheringStateChange([](PeerConnection::GatheringState state) {
  50. cout << "Gathering state 1: " << state << endl;
  51. });
  52. pc2.onLocalDescription([&pc1](Description sdp) {
  53. cout << "Description 2: " << sdp << endl;
  54. pc1.setRemoteDescription(string(sdp));
  55. });
  56. pc2.onLocalCandidate([&pc1](Candidate candidate) {
  57. cout << "Candidate 2: " << candidate << endl;
  58. pc1.addRemoteCandidate(string(candidate));
  59. });
  60. pc2.onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
  61. pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
  62. cout << "Gathering state 2: " << state << endl;
  63. });
  64. shared_ptr<Track> t2;
  65. string newTrackMid;
  66. Description::Video media;
  67. pc2.onTrack([&t2, &newTrackMid](shared_ptr<Track> t) {
  68. string mid = t->mid();
  69. cout << "Track 2: Received track with mid \"" << mid << "\"" << endl;
  70. if (mid != newTrackMid) {
  71. cerr << "Wrong track mid" << endl;
  72. return;
  73. }
  74. t->onOpen([mid]() { cout << "Track 2: Track with mid \"" << mid << "\" is open" << endl; });
  75. t->onClosed(
  76. [mid]() { cout << "Track 2: Track with mid \"" << mid << "\" is closed" << endl; });
  77. std::atomic_store(&t2, t);
  78. });
  79. // Test opening a track
  80. newTrackMid = "test";
  81. media = Description::Video(newTrackMid, Description::Direction::SendOnly);
  82. media.addH264Codec(96);
  83. media.setBitrate(3000);
  84. media.addSSRC(1234, "video-send");
  85. auto t1 = pc1.addTrack(media);
  86. pc1.setLocalDescription();
  87. int attempts = 10;
  88. shared_ptr<Track> at2;
  89. while ((!(at2 = std::atomic_load(&t2)) || !at2->isOpen() || !t1->isOpen()) && attempts--)
  90. this_thread::sleep_for(1s);
  91. if (pc1.state() != PeerConnection::State::Connected &&
  92. pc2.state() != PeerConnection::State::Connected)
  93. throw runtime_error("PeerConnection is not connected");
  94. if (!at2 || !at2->isOpen() || !t1->isOpen())
  95. throw runtime_error("Track is not open");
  96. // Test renegotiation
  97. newTrackMid = "added";
  98. media = Description::Video(newTrackMid, Description::Direction::SendOnly);
  99. media.addH264Codec(96);
  100. media.setBitrate(3000);
  101. media.addSSRC(2468, "video-send");
  102. // NOTE: Overwriting the old shared_ptr for t1 will cause it's respective
  103. // track to be dropped (so it's SSRCs won't be on the description next time)
  104. t1 = pc1.addTrack(media);
  105. pc1.setLocalDescription();
  106. attempts = 10;
  107. t2.reset();
  108. while ((!(at2 = std::atomic_load(&t2)) || !at2->isOpen() || !t1->isOpen()) && attempts--)
  109. this_thread::sleep_for(1s);
  110. if (!at2 || !at2->isOpen() || !t1->isOpen())
  111. throw runtime_error("Renegotiated track is not open");
  112. // TODO: Test sending RTP packets in track
  113. // Delay close of peer 2 to check closing works properly
  114. pc1.close();
  115. this_thread::sleep_for(1s);
  116. pc2.close();
  117. this_thread::sleep_for(1s);
  118. if (!t1->isClosed() || !t2->isClosed())
  119. throw runtime_error("Track is not closed");
  120. cout << "Success" << endl;
  121. }