reliability.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 "test.hpp"
  10. #include <atomic>
  11. #include <chrono>
  12. #include <iostream>
  13. #include <memory>
  14. #include <thread>
  15. using namespace rtc;
  16. using namespace std;
  17. TestResult test_reliability() {
  18. InitLogger(LogLevel::Debug);
  19. Configuration config1;
  20. // STUN server example (not necessary to connect locally)
  21. config1.iceServers.emplace_back("stun:stun.l.google.com:19302");
  22. PeerConnection pc1(config1);
  23. Configuration config2;
  24. // STUN server example (not necessary to connect locally)
  25. config2.iceServers.emplace_back("stun:stun.l.google.com:19302");
  26. PeerConnection pc2(config2);
  27. pc1.onLocalDescription([&pc2](Description sdp) {
  28. cout << "Description 1: " << sdp << endl;
  29. pc2.setRemoteDescription(string(sdp));
  30. });
  31. pc1.onLocalCandidate([&pc2](Candidate candidate) {
  32. cout << "Candidate 1: " << candidate << endl;
  33. pc2.addRemoteCandidate(string(candidate));
  34. });
  35. pc2.onLocalDescription([&pc1](Description sdp) {
  36. cout << "Description 2: " << sdp << endl;
  37. pc1.setRemoteDescription(string(sdp));
  38. });
  39. pc2.onLocalCandidate([&pc1](Candidate candidate) {
  40. cout << "Candidate 2: " << candidate << endl;
  41. pc1.addRemoteCandidate(string(candidate));
  42. });
  43. Reliability reliableOrdered;
  44. auto dcReliableOrdered = pc1.createDataChannel("reliable_ordered", {reliableOrdered});
  45. Reliability reliableUnordered;
  46. reliableUnordered.unordered = true;
  47. auto dcReliableUnordered = pc1.createDataChannel("reliable_unordered", {reliableUnordered});
  48. Reliability unreliableMaxPacketLifeTime;
  49. unreliableMaxPacketLifeTime.unordered = true;
  50. unreliableMaxPacketLifeTime.maxPacketLifeTime = 222ms;
  51. auto dcUnreliableMaxPacketLifeTime =
  52. pc1.createDataChannel("unreliable_maxpacketlifetime", {unreliableMaxPacketLifeTime});
  53. Reliability unreliableMaxRetransmits;
  54. unreliableMaxRetransmits.unordered = true;
  55. unreliableMaxRetransmits.maxRetransmits = 2;
  56. auto dcUnreliableMaxRetransmits =
  57. pc1.createDataChannel("unreliable_maxretransmits", {unreliableMaxRetransmits});
  58. std::atomic<int> count = 0;
  59. std::atomic<bool> failed = false;
  60. pc2.onDataChannel([&count, &failed](shared_ptr<DataChannel> dc) {
  61. cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
  62. auto label = dc->label();
  63. auto reliability = dc->reliability();
  64. try {
  65. if (label == "reliable_ordered") {
  66. if (reliability.unordered != false || reliability.maxPacketLifeTime ||
  67. reliability.maxRetransmits)
  68. throw std::runtime_error("Expected reliable ordered");
  69. } else if (label == "reliable_unordered") {
  70. if (reliability.unordered != true || reliability.maxPacketLifeTime ||
  71. reliability.maxRetransmits)
  72. throw std::runtime_error("Expected reliable unordered");
  73. } else if (label == "unreliable_maxpacketlifetime") {
  74. if (!reliability.maxPacketLifeTime || *reliability.maxPacketLifeTime != 222ms ||
  75. reliability.maxRetransmits)
  76. throw std::runtime_error("Expected maxPacketLifeTime to be set");
  77. } else if (label == "unreliable_maxretransmits") {
  78. if (reliability.maxPacketLifeTime || !reliability.maxRetransmits ||
  79. *reliability.maxRetransmits != 2)
  80. throw std::runtime_error("Expected maxRetransmits to be set");
  81. } else
  82. throw std::runtime_error("Unexpected label: " + label);
  83. } catch (const std::exception &e) {
  84. cerr << "Error: " << e.what();
  85. failed = true;
  86. return;
  87. }
  88. ++count;
  89. });
  90. // Wait a bit
  91. int attempts = 10;
  92. shared_ptr<DataChannel> adc2;
  93. while (count != 4 && !failed && attempts--)
  94. this_thread::sleep_for(1s);
  95. if (pc1.state() != PeerConnection::State::Connected ||
  96. pc2.state() != PeerConnection::State::Connected)
  97. return TestResult(false, "PeerConnection is not connected");
  98. if (failed)
  99. return TestResult(false, "Incorrect reliability settings");
  100. if (count != 4)
  101. return TestResult(false, "Some DataChannels are not open");
  102. pc1.close();
  103. return TestResult(true);
  104. }