reliability.cpp 4.0 KB

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