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