123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- /**
- * Copyright (c) 2019 Paul-Louis Ageneau
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
- #include "rtc/rtc.hpp"
- #include <atomic>
- #include <chrono>
- #include <iostream>
- #include <memory>
- #include <thread>
- using namespace rtc;
- using namespace std;
- template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
- void test_connectivity() {
- InitLogger(LogLevel::Debug);
- Configuration config1;
- // STUN server example
- // config1.iceServers.emplace_back("stun:stun.l.google.com:19302");
- auto pc1 = std::make_shared<PeerConnection>(config1);
- Configuration config2;
- // STUN server example
- // config2.iceServers.emplace_back("stun:stun.l.google.com:19302");
- // Port range example
- config2.portRangeBegin = 5000;
- config2.portRangeEnd = 6000;
- auto pc2 = std::make_shared<PeerConnection>(config2);
- pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](Description sdp) {
- auto pc2 = wpc2.lock();
- if (!pc2)
- return;
- cout << "Description 1: " << sdp << endl;
- pc2->setRemoteDescription(string(sdp));
- });
- pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](Candidate candidate) {
- auto pc2 = wpc2.lock();
- if (!pc2)
- return;
- cout << "Candidate 1: " << candidate << endl;
- pc2->addRemoteCandidate(string(candidate));
- });
- pc1->onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
- pc1->onGatheringStateChange([](PeerConnection::GatheringState state) {
- cout << "Gathering state 1: " << state << endl;
- });
- pc1->onSignalingStateChange([](PeerConnection::SignalingState state) {
- cout << "Signaling state 1: " << state << endl;
- });
- pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](Description sdp) {
- auto pc1 = wpc1.lock();
- if (!pc1)
- return;
- cout << "Description 2: " << sdp << endl;
- pc1->setRemoteDescription(string(sdp));
- });
- pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](Candidate candidate) {
- auto pc1 = wpc1.lock();
- if (!pc1)
- return;
- cout << "Candidate 2: " << candidate << endl;
- pc1->addRemoteCandidate(string(candidate));
- });
- pc2->onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
- pc2->onGatheringStateChange([](PeerConnection::GatheringState state) {
- cout << "Gathering state 2: " << state << endl;
- });
- pc2->onSignalingStateChange([](PeerConnection::SignalingState state) {
- cout << "Signaling state 2: " << state << endl;
- });
- shared_ptr<DataChannel> dc2;
- pc2->onDataChannel([&dc2](shared_ptr<DataChannel> dc) {
- cout << "DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
- if (dc->label() != "test") {
- cerr << "Wrong DataChannel label" << endl;
- return;
- }
- dc->onMessage([](variant<binary, string> message) {
- if (holds_alternative<string>(message)) {
- cout << "Message 2: " << get<string>(message) << endl;
- }
- });
- dc->send("Hello from 2");
- std::atomic_store(&dc2, dc);
- });
- auto dc1 = pc1->createDataChannel("test");
- dc1->onOpen([wdc1 = make_weak_ptr(dc1)]() {
- auto dc1 = wdc1.lock();
- if (!dc1)
- return;
- cout << "DataChannel 1: Open" << endl;
- dc1->send("Hello from 1");
- });
- dc1->onMessage([](const variant<binary, string> &message) {
- if (holds_alternative<string>(message)) {
- cout << "Message 1: " << get<string>(message) << endl;
- }
- });
- // Wait a bit
- int attempts = 10;
- shared_ptr<DataChannel> adc2;
- while ((!(adc2 = std::atomic_load(&dc2)) || !adc2->isOpen() || !dc1->isOpen()) && attempts--)
- this_thread::sleep_for(1s);
- if (pc1->state() != PeerConnection::State::Connected &&
- pc2->state() != PeerConnection::State::Connected)
- throw runtime_error("PeerConnection is not connected");
- if (!adc2 || !adc2->isOpen() || !dc1->isOpen())
- throw runtime_error("DataChannel is not open");
- if (auto addr = pc1->localAddress())
- cout << "Local address 1: " << *addr << endl;
- if (auto addr = pc1->remoteAddress())
- cout << "Remote address 1: " << *addr << endl;
- if (auto addr = pc2->localAddress())
- cout << "Local address 2: " << *addr << endl;
- if (auto addr = pc2->remoteAddress())
- cout << "Remote address 2: " << *addr << endl;
- Candidate local, remote;
- if(pc1->getSelectedCandidatePair(&local, &remote)) {
- cout << "Local candidate 1: " << local << endl;
- cout << "Remote candidate 1: " << remote << endl;
- }
- if(pc2->getSelectedCandidatePair(&local, &remote)) {
- cout << "Local candidate 2: " << local << endl;
- cout << "Remote candidate 2: " << remote << endl;
- }
- // Try to open a second data channel with another label
- shared_ptr<DataChannel> second2;
- pc2->onDataChannel([&second2](shared_ptr<DataChannel> dc) {
- cout << "Second DataChannel 2: Received with label \"" << dc->label() << "\"" << endl;
- if (dc->label() != "second") {
- cerr << "Wrong second DataChannel label" << endl;
- return;
- }
- dc->onMessage([](variant<binary, string> message) {
- if (holds_alternative<string>(message)) {
- cout << "Second Message 2: " << get<string>(message) << endl;
- }
- });
- dc->send("Send hello from 2");
- std::atomic_store(&second2, dc);
- });
- auto second1 = pc1->createDataChannel("second");
- second1->onOpen([wsecond1 = make_weak_ptr(dc1)]() {
- auto second1 = wsecond1.lock();
- if (!second1)
- return;
- cout << "Second DataChannel 1: Open" << endl;
- second1->send("Second hello from 1");
- });
- dc1->onMessage([](const variant<binary, string> &message) {
- if (holds_alternative<string>(message)) {
- cout << "Second Message 1: " << get<string>(message) << endl;
- }
- });
- // Wait a bit
- attempts = 10;
- shared_ptr<DataChannel> asecond2;
- while (
- (!(asecond2 = std::atomic_load(&second2)) || !asecond2->isOpen() || !second1->isOpen()) &&
- attempts--)
- this_thread::sleep_for(1s);
- // Delay close of peer 2 to check closing works properly
- pc1->close();
- this_thread::sleep_for(1s);
- pc2->close();
- this_thread::sleep_for(1s);
- // You may call rtc::Cleanup() when finished to free static resources
- rtc::Cleanup();
- this_thread::sleep_for(2s);
- cout << "Success" << endl;
- }
|