-
-
Notifications
You must be signed in to change notification settings - Fork 377
/
Copy pathanswerer.cpp
154 lines (137 loc) · 4 KB
/
answerer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Copyright (c) 2019 Paul-Louis Ageneau
* Copyright (c) 2019 Murat Dogan
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "rtc/rtc.hpp"
#include <chrono>
#include <iostream>
#include <memory>
#include <thread>
using namespace std::chrono_literals;
using std::shared_ptr;
using std::weak_ptr;
template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
int main(int argc, char **argv) {
rtc::InitLogger(rtc::LogLevel::Warning);
rtc::Configuration config;
// config.iceServers.emplace_back("stun.l.google.com:19302");
auto pc = std::make_shared<rtc::PeerConnection>(config);
pc->onLocalDescription([](rtc::Description description) {
std::cout << "Local Description (Paste this to the other peer):" << std::endl;
std::cout << std::string(description) << std::endl;
});
pc->onLocalCandidate([](rtc::Candidate candidate) {
std::cout << "Local Candidate (Paste this to the other peer after the local description):"
<< std::endl;
std::cout << std::string(candidate) << std::endl << std::endl;
});
pc->onStateChange([](rtc::PeerConnection::State state) {
std::cout << "[State: " << state << "]" << std::endl;
});
pc->onGatheringStateChange([](rtc::PeerConnection::GatheringState state) {
std::cout << "[Gathering State: " << state << "]" << std::endl;
});
shared_ptr<rtc::DataChannel> dc;
pc->onDataChannel([&](shared_ptr<rtc::DataChannel> _dc) {
std::cout << "[Got a DataChannel with label: " << _dc->label() << "]" << std::endl;
dc = _dc;
dc->onClosed(
[&]() { std::cout << "[DataChannel closed: " << dc->label() << "]" << std::endl; });
dc->onMessage([](auto data) {
if (std::holds_alternative<std::string>(data)) {
std::cout << "[Received message: " << std::get<std::string>(data) << "]"
<< std::endl;
}
});
});
bool exit = false;
while (!exit) {
std::cout
<< std::endl
<< "**********************************************************************************"
"*****"
<< std::endl
<< "* 0: Exit /"
<< " 1: Enter remote description /"
<< " 2: Enter remote candidate /"
<< " 3: Send message /"
<< " 4: Print Connection Info *" << std::endl
<< "[Command]: ";
int command = -1;
std::cin >> command;
std::cin.ignore();
switch (command) {
case 0: {
exit = true;
break;
}
case 1: {
// Parse Description
std::cout << "[Description]: ";
std::string sdp, line;
while (getline(std::cin, line) && !line.empty()) {
sdp += line;
sdp += "\r\n";
}
std::cout << sdp;
pc->setRemoteDescription(sdp);
break;
}
case 2: {
// Parse Candidate
std::cout << "[Candidate]: ";
std::string candidate;
getline(std::cin, candidate);
pc->addRemoteCandidate(candidate);
break;
}
case 3: {
// Send Message
if (!dc || !dc->isOpen()) {
std::cout << "** Channel is not Open ** ";
break;
}
std::cout << "[Message]: ";
std::string message;
getline(std::cin, message);
dc->send(message);
break;
}
case 4: {
// Connection Info
if (!dc || !dc->isOpen()) {
std::cout << "** Channel is not Open ** ";
break;
}
rtc::Candidate local, remote;
std::optional<std::chrono::milliseconds> rtt = pc->rtt();
if (pc->getSelectedCandidatePair(&local, &remote)) {
std::cout << "Local: " << local << std::endl;
std::cout << "Remote: " << remote << std::endl;
std::cout << "Bytes Sent:" << pc->bytesSent()
<< " / Bytes Received:" << pc->bytesReceived() << " / Round-Trip Time:";
if (rtt.has_value())
std::cout << rtt.value().count();
else
std::cout << "null";
std::cout << " ms";
} else {
std::cout << "Could not get Candidate Pair Info" << std::endl;
}
break;
}
default: {
std::cout << "** Invalid Command ** " << std::endl;
break;
}
}
}
if (dc)
dc->close();
if (pc)
pc->close();
}