track.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 <atomic>
  10. #include <chrono>
  11. #include <iostream>
  12. #include <memory>
  13. #include <thread>
  14. using namespace rtc;
  15. using namespace std;
  16. template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  17. void test_track() {
  18. InitLogger(LogLevel::Debug);
  19. Configuration config1;
  20. // STUN server example
  21. // config1.iceServers.emplace_back("stun:stun.l.google.com:19302");
  22. PeerConnection pc1(config1);
  23. Configuration config2;
  24. // STUN server example
  25. // config2.iceServers.emplace_back("stun:stun.l.google.com:19302");
  26. // Port range example
  27. config2.portRangeBegin = 5000;
  28. config2.portRangeEnd = 6000;
  29. PeerConnection pc2(config2);
  30. pc1.onLocalDescription([&pc2](Description sdp) {
  31. cout << "Description 1: " << sdp << endl;
  32. pc2.setRemoteDescription(string(sdp));
  33. });
  34. pc1.onLocalCandidate([&pc2](Candidate candidate) {
  35. cout << "Candidate 1: " << candidate << endl;
  36. pc2.addRemoteCandidate(string(candidate));
  37. });
  38. pc1.onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
  39. pc1.onGatheringStateChange([](PeerConnection::GatheringState state) {
  40. cout << "Gathering state 1: " << state << endl;
  41. });
  42. pc2.onLocalDescription([&pc1](Description sdp) {
  43. cout << "Description 2: " << sdp << endl;
  44. pc1.setRemoteDescription(string(sdp));
  45. });
  46. pc2.onLocalCandidate([&pc1](Candidate candidate) {
  47. cout << "Candidate 2: " << candidate << endl;
  48. pc1.addRemoteCandidate(string(candidate));
  49. });
  50. pc2.onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
  51. pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
  52. cout << "Gathering state 2: " << state << endl;
  53. });
  54. shared_ptr<Track> t2;
  55. string newTrackMid;
  56. pc2.onTrack([&t2, &newTrackMid](shared_ptr<Track> t) {
  57. string mid = t->mid();
  58. cout << "Track 2: Received track with mid \"" << mid << "\"" << endl;
  59. if (mid != newTrackMid) {
  60. cerr << "Wrong track mid" << endl;
  61. return;
  62. }
  63. t->onOpen([mid]() { cout << "Track 2: Track with mid \"" << mid << "\" is open" << endl; });
  64. t->onClosed(
  65. [mid]() { cout << "Track 2: Track with mid \"" << mid << "\" is closed" << endl; });
  66. std::atomic_store(&t2, t);
  67. });
  68. // Test opening a track
  69. newTrackMid = "test";
  70. Description::Video media(newTrackMid, Description::Direction::SendOnly);
  71. media.addH264Codec(96);
  72. media.setBitrate(3000);
  73. media.addSSRC(1234, "video-send");
  74. const auto mediaSdp1 = string(media);
  75. const auto mediaSdp2 = string(Description::Media(mediaSdp1));
  76. if (mediaSdp2 != mediaSdp1) {
  77. cout << mediaSdp2 << endl;
  78. throw runtime_error("Media description parsing test failed");
  79. }
  80. auto t1 = pc1.addTrack(media);
  81. pc1.setLocalDescription();
  82. int attempts = 10;
  83. shared_ptr<Track> at2;
  84. while ((!(at2 = std::atomic_load(&t2)) || !at2->isOpen() || !t1->isOpen()) && attempts--)
  85. this_thread::sleep_for(1s);
  86. if (pc1.state() != PeerConnection::State::Connected ||
  87. pc2.state() != PeerConnection::State::Connected)
  88. throw runtime_error("PeerConnection is not connected");
  89. if (!at2 || !at2->isOpen() || !t1->isOpen())
  90. throw runtime_error("Track is not open");
  91. // Test renegotiation
  92. newTrackMid = "added";
  93. Description::Video media2(newTrackMid, Description::Direction::SendOnly);
  94. media2.addH264Codec(96);
  95. media2.setBitrate(3000);
  96. media2.addSSRC(2468, "video-send");
  97. // NOTE: Overwriting the old shared_ptr for t1 will cause it's respective
  98. // track to be dropped (so it's SSRCs won't be on the description next time)
  99. t1 = pc1.addTrack(media2);
  100. pc1.setLocalDescription();
  101. attempts = 10;
  102. t2.reset();
  103. while ((!(at2 = std::atomic_load(&t2)) || !at2->isOpen() || !t1->isOpen()) && attempts--)
  104. this_thread::sleep_for(1s);
  105. if (!at2 || !at2->isOpen() || !t1->isOpen())
  106. throw runtime_error("Renegotiated track is not open");
  107. // TODO: Test sending RTP packets in track
  108. // Delay close of peer 2 to check closing works properly
  109. pc1.close();
  110. this_thread::sleep_for(1s);
  111. pc2.close();
  112. this_thread::sleep_for(1s);
  113. if (!t1->isClosed() || !t2->isClosed())
  114. throw runtime_error("Track is not closed");
  115. cout << "Success" << endl;
  116. }