-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
275 lines (252 loc) · 9.75 KB
/
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#ifdef WEBRTC_WIN
#include <rtc_base/win32_socket_init.h>
#endif
#include <api/audio_codecs/builtin_audio_decoder_factory.h>
#include <api/audio_codecs/builtin_audio_encoder_factory.h>
#include <api/create_peerconnection_factory.h>
#include <api/peer_connection_interface.h>
#include <api/video_codecs/builtin_video_decoder_factory.h>
#include <api/video_codecs/builtin_video_encoder_factory.h>
#include <rtc_base/ssl_adapter.h>
class GetStatsCallback : public webrtc::RTCStatsCollectorCallback {
public:
GetStatsCallback() {}
~GetStatsCallback() {}
void OnStatsDelivered(
const rtc::scoped_refptr<const webrtc::RTCStatsReport>& report) override {
std::cout << report.get()->ToJson() << std::endl;
}
protected:
void AddRef() const override {}
rtc::RefCountReleaseStatus Release() const override {
return rtc::RefCountReleaseStatus::kDroppedLastRef;
}
};
class CreateSDPCallback : public webrtc::CreateSessionDescriptionObserver {
private:
std::function<void(webrtc::SessionDescriptionInterface*)> success;
std::function<void(const std::string&)> failure;
public:
CreateSDPCallback(std::function<void(webrtc::SessionDescriptionInterface*)> s,
std::function<void(const std::string&)> f)
: success(s), failure(f) {}
void OnSuccess(webrtc::SessionDescriptionInterface* desc) {
std::cout << __LINE__ << " " << __FUNCTION__ << std::endl;
if (success) {
success(desc);
}
}
void OnFailure(webrtc::RTCError error) {
std::cout << error.message() << std::endl;
}
};
class DummySetSessionDescriptionObserver
: public webrtc::SetSessionDescriptionObserver {
public:
static DummySetSessionDescriptionObserver* Create() {
return new rtc::RefCountedObject<DummySetSessionDescriptionObserver>();
}
virtual void OnSuccess() {
std::cout << __LINE__ << " " << __FUNCTION__ << std::endl;
}
virtual void OnFailure(webrtc::RTCError error) {
std::cout << error.message() << std::endl;
}
protected:
DummySetSessionDescriptionObserver() {}
~DummySetSessionDescriptionObserver() {}
};
class PeerConnectionCallback : public webrtc::PeerConnectionObserver {
private:
std::function<void(rtc::scoped_refptr<webrtc::MediaStreamInterface>)>
onAddStream;
std::function<void(const webrtc::IceCandidateInterface*)> onIceCandidate;
rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc;
std::vector<std::string> RTCSignalingState = {"stable",
"have-local-offer",
"have-remote-offer",
"have-local-pranswer",
"have-remote-pranswer",
"closed"};
std::vector<std::string> RTCIceGatheringState = {"new", "gathering",
"complete"};
std::vector<std::string> RTCPeerConnectionState = {
"new", "connecting", "connected", "disconnected", "failed", "closed"};
std::vector<std::string> RTCIceConnectionState = {
"new", "checking", "connected", "completed",
"disconnected", "failed", "closed"};
public:
PeerConnectionCallback() : onAddStream(nullptr), onIceCandidate(nullptr) {}
virtual ~PeerConnectionCallback() {}
void SetOnAddStream(
std::function<void(rtc::scoped_refptr<webrtc::MediaStreamInterface>)>
addStream) {
onAddStream = addStream;
}
void SetPeerConnection(
rtc::scoped_refptr<webrtc::PeerConnectionInterface> p) {
pc = p;
}
protected:
void OnSignalingChange(
webrtc::PeerConnectionInterface::SignalingState new_state) override {
std::cout << __FUNCTION__ << " " << this->RTCSignalingState[new_state]
<< std::endl;
}
void OnDataChannel(
rtc::scoped_refptr<webrtc::DataChannelInterface> channel) override {}
void OnRenegotiationNeeded() override {
std::cout << __FUNCTION__ << " !!!!!!!!!!!!!!!!" << std::endl;
}
void OnIceConnectionChange(
webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
std::cout << __FUNCTION__ << " " << this->RTCIceConnectionState[new_state]
<< std::endl;
}
void OnIceGatheringChange(
webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
std::cout << __FUNCTION__ << " " << this->RTCIceGatheringState[new_state]
<< std::endl;
if (new_state == 2) {
std::string sdp;
pc->local_description()->ToString(&sdp);
std::cout
<< "\n\n"
<< "3 -------------------- send server answer ---------------------"
<< std::endl;
std::cout << sdp << std::endl;
std::cout
<< "3 -------------------- send server answer ---------------------"
<< std::endl;
}
}
void OnIceConnectionReceivingChange(bool receiving) override {
std::cout << __FUNCTION__ << " " << receiving << std::endl;
}
void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
std::cout << __FUNCTION__ << std::endl;
}
void OnRemoveStream(
rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override {
std::cout << __FUNCTION__ << " " << stream->id() << std::endl;
}
void OnAddStream(
rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override {
std::cout << __FUNCTION__ << " " << stream->id() << std::endl;
onAddStream(stream);
}
};
rtc::scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
webrtc::PeerConnectionObserver* observer) {
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
peer_connection_factory = webrtc::CreatePeerConnectionFactory(
nullptr, nullptr, nullptr, nullptr,
webrtc::CreateBuiltinAudioEncoderFactory(),
webrtc::CreateBuiltinAudioDecoderFactory(),
webrtc::CreateBuiltinVideoEncoderFactory(),
webrtc::CreateBuiltinVideoDecoderFactory(), nullptr, nullptr);
if (!peer_connection_factory.get()) {
std::cout << "Failed to initialize PeerConnectionFactory" << std::endl;
return nullptr;
}
webrtc::PeerConnectionInterface::RTCConfiguration config;
webrtc::PeerConnectionInterface::IceServers servers;
webrtc::PeerConnectionInterface::IceServer ice_server;
ice_server.uri = "stun:stun.l.google.com:19302";
servers.push_back(ice_server);
config.servers = servers;
config.enable_dtls_srtp = true;
config.sdp_semantics = webrtc::SdpSemantics::kPlanB;
return peer_connection_factory->CreatePeerConnection(config, nullptr, nullptr,
observer);
}
class LoopBack {
public:
LoopBack() {
#ifdef WEBRTC_WIN
rtc::WinsockInitializer winsock_initializer;
#endif
rtc::InitializeSSL();
get_stats_callback = new GetStatsCallback();
auto peer_connection_callback = new PeerConnectionCallback();
peer_connection = CreatePeerConnection(peer_connection_callback);
peer_connection_callback->SetPeerConnection(peer_connection);
{ // receive offer
std::string sdp;
{
std::cout << "\n\n"
<< "1. input browser offer" << std::endl;
std::string input;
do {
std::getline(std::cin, input);
if (input != "") {
sdp += input + '\n';
}
} while (input != "");
std::cout << "1. input received" << std::endl;
}
webrtc::SdpParseError error;
webrtc::SessionDescriptionInterface* session_description(
webrtc::CreateSessionDescription("offer", sdp, &error));
if (!session_description) {
std::cout << "Can't parse received session description message. "
<< "SdpParseError was: " << error.description << std::endl;
return;
}
std::cout << " Received session description" << std::endl;
peer_connection_callback->SetOnAddStream(
[&](rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
peer_connection->AddStream(stream);
});
std::cout << "set remote description start." << std::endl;
peer_connection->SetRemoteDescription(
DummySetSessionDescriptionObserver::Create(), session_description);
std::cout << "set remote description end." << std::endl;
}
if (peer_connection->remote_description()->type() == "offer") {
// create answer
webrtc::PeerConnectionInterface::RTCOfferAnswerOptions options;
options.offer_to_receive_audio = webrtc::PeerConnectionInterface::
RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
options.offer_to_receive_video = webrtc::PeerConnectionInterface::
RTCOfferAnswerOptions::kOfferToReceiveMediaTrue;
peer_connection->CreateAnswer(
new rtc::RefCountedObject<CreateSDPCallback>(
[&](webrtc::SessionDescriptionInterface* desc) {
std::cout << "create answer callback start." << std::endl;
peer_connection->SetLocalDescription(
DummySetSessionDescriptionObserver::Create(), desc);
std::cout << "set set local description" << std::endl;
},
nullptr),
options);
}
is_start = true;
get_stats_thread = std::thread(&LoopBack::GetStatsThread, this);
(rtc::Thread::Current())->Run();
rtc::CleanupSSL();
}
~LoopBack() {
is_start = false;
get_stats_thread.join();
}
void GetStatsThread() {
while (is_start) {
peer_connection->GetStats(get_stats_callback);
std::this_thread::sleep_for(std::chrono::seconds(10));
}
}
private:
bool is_start = false;
rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection;
GetStatsCallback* get_stats_callback;
std::thread get_stats_thread;
};
int main() {
LoopBack lb;
return 0;
}