-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
409 lines (337 loc) · 13.9 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include <iostream>
#include <memory>
#include <mutex>
#include <queue>
#include <string>
#include <thread>
// 環境に合わせてマクロ定義が必要
//#define WEBRTC_ANDROID 1
//#define WEBRTC_IOS 1
//#define WEBRTC_LINUX 1
//#define WEBRTC_MAC 1
//#define WEBRTC_POSIX 1
//#define WEBRTC_WIN 1
// WebRTC関連のヘッダ
#include <api/audio_codecs/builtin_audio_decoder_factory.h>
#include <api/audio_codecs/builtin_audio_encoder_factory.h>
#include <api/video_codecs/builtin_video_decoder_factory.h>
#include <api/video_codecs/builtin_video_encoder_factory.h>
#include <api/peerconnectioninterface.h>
#include <rtc_base/flags.h>
#include <rtc_base/physicalsocketserver.h>
#include <rtc_base/ssladapter.h>
#include <rtc_base/thread.h>
// picojsonはコピペ用データ構造を作るために使う
#include "picojson/picojson.h"
class Connection {
public:
rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection;
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel;
std::string sdp_type;
picojson::array ice_array;
// Offer/Answerの作成が成功したら、LocalDescriptionとして設定 & 相手に渡す文字列として表示
void onSuccessCSD(webrtc::SessionDescriptionInterface* desc) {
peer_connection->SetLocalDescription(ssdo, desc);
std::string sdp;
desc->ToString(&sdp);
std::cout << sdp_type << " SDP:begin" << std::endl << sdp << sdp_type << " SDP:end" << std::endl;
}
// ICEを取得したら、表示用JSON配列の末尾に追加
void onIceCandidate(const webrtc::IceCandidateInterface* candidate) {
picojson::object ice;
std::string candidate_str;
candidate->ToString(&candidate_str);
ice.insert(std::make_pair("candidate", picojson::value(candidate_str)));
ice.insert(std::make_pair("sdpMid", picojson::value(candidate->sdp_mid())));
ice.insert(std::make_pair("sdpMLineIndex", picojson::value(static_cast<double>(candidate->sdp_mline_index()))));
ice_array.push_back(picojson::value(ice));
}
class PCO : public webrtc::PeerConnectionObserver {
private:
Connection& parent;
public:
PCO(Connection& parent) : parent(parent) {
}
void OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState new_state) override {
std::cout << std::this_thread::get_id() << ":"
<< "PeerConnectionObserver::SignalingChange(" << new_state << ")" << std::endl;
};
void OnAddStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override {
std::cout << std::this_thread::get_id() << ":"
<< "PeerConnectionObserver::AddStream" << std::endl;
};
void OnRemoveStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) override {
std::cout << std::this_thread::get_id() << ":"
<< "PeerConnectionObserver::RemoveStream" << std::endl;
};
void OnDataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) override {
std::cout << std::this_thread::get_id() << ":"
<< "PeerConnectionObserver::DataChannel(" << data_channel
<< ", " << parent.data_channel.get() << ")" << std::endl;
// Answer送信側は、onDataChannelでDataChannelの接続を受け付ける
parent.data_channel = data_channel;
parent.data_channel->RegisterObserver(&parent.dco);
};
void OnRenegotiationNeeded() override {
std::cout << std::this_thread::get_id() << ":"
<< "PeerConnectionObserver::RenegotiationNeeded" << std::endl;
};
void OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state) override {
std::cout << std::this_thread::get_id() << ":"
<< "PeerConnectionObserver::IceConnectionChange(" << new_state << ")" << std::endl;
};
void OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state) override {
std::cout << std::this_thread::get_id() << ":"
<< "PeerConnectionObserver::IceGatheringChange(" << new_state << ")" << std::endl;
};
void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override {
std::cout << std::this_thread::get_id() << ":"
<< "PeerConnectionObserver::IceCandidate" << std::endl;
parent.onIceCandidate(candidate);
};
};
class DCO : public webrtc::DataChannelObserver {
private:
Connection& parent;
public:
DCO(Connection& parent) : parent(parent) {
}
// 接続状況が変化した時に発火する。切断は発火タイミングで値を確認して検知可能
void OnStateChange() override {
std::cout << std::this_thread::get_id() << ":"
<< "DataChannelObserver::StateChange" << std::endl;
};
// メッセージ受信
void OnMessage(const webrtc::DataBuffer& buffer) override {
std::cout << std::this_thread::get_id() << ":"
<< "DataChannelObserver::Message" << std::endl;
std::cout << std::string(buffer.data.data<char>(), buffer.data.size()) << std::endl;
};
void OnBufferedAmountChange(uint64_t previous_amount) override {
std::cout << std::this_thread::get_id() << ":"
<< "DataChannelObserver::BufferedAmountChange(" << previous_amount << ")" << std::endl;
};
};
class CSDO : public webrtc::CreateSessionDescriptionObserver {
private:
Connection& parent;
public:
CSDO(Connection& parent) : parent(parent) {
}
void OnSuccess(webrtc::SessionDescriptionInterface* desc) override {
std::cout << std::this_thread::get_id() << ":"
<< "CreateSessionDescriptionObserver::OnSuccess" << std::endl;
parent.onSuccessCSD(desc);
};
void OnFailure(const std::string& error) override {
std::cout << std::this_thread::get_id() << ":"
<< "CreateSessionDescriptionObserver::OnFailure" << std::endl << error << std::endl;
};
};
class SSDO : public webrtc::SetSessionDescriptionObserver {
private:
Connection& parent;
public:
SSDO(Connection& parent) : parent(parent) {
}
void OnSuccess() override {
std::cout << std::this_thread::get_id() << ":"
<< "SetSessionDescriptionObserver::OnSuccess" << std::endl;
};
void OnFailure(const std::string& error) override {
std::cout << std::this_thread::get_id() << ":"
<< "SetSessionDescriptionObserver::OnFailure" << std::endl << error << std::endl;
};
};
PCO pco;
DCO dco;
rtc::scoped_refptr<CSDO> csdo;
rtc::scoped_refptr<SSDO> ssdo;
Connection() :
pco(*this),
dco(*this),
csdo(new rtc::RefCountedObject<CSDO>(*this)),
ssdo(new rtc::RefCountedObject<SSDO>(*this)) {
}
};
std::unique_ptr<rtc::Thread> network_thread;
std::unique_ptr<rtc::Thread> worker_thread;
std::unique_ptr<rtc::Thread> signaling_thread;
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> peer_connection_factory;
webrtc::PeerConnectionInterface::RTCConfiguration configuration;
Connection connection;
void cmd_sdp1() {
connection.peer_connection = peer_connection_factory->CreatePeerConnection(configuration, nullptr, nullptr, &connection.pco);
webrtc::DataChannelInit config;
// DataChannelの設定
connection.data_channel = connection.peer_connection->CreateDataChannel("data_channel", &config);
connection.data_channel->RegisterObserver(&connection.dco);
if (connection.peer_connection.get() == nullptr) {
peer_connection_factory = nullptr;
std::cout << "Error on CreatePeerConnection." << std::endl;
return;
}
connection.sdp_type = "Offer"; // 表示用の文字列、webrtcの動作には関係ない
connection.peer_connection->CreateOffer(connection.csdo,
webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
}
void cmd_sdp2(const std::string& parameter) {
connection.peer_connection = peer_connection_factory->CreatePeerConnection(configuration, nullptr, nullptr, &connection.pco);
if (connection.peer_connection.get() == nullptr) {
peer_connection_factory = nullptr;
std::cout << "Error on CreatePeerConnection." << std::endl;
return;
}
webrtc::SdpParseError error;
webrtc::SessionDescriptionInterface* session_description(
webrtc::CreateSessionDescription("offer", parameter, &error));
if (session_description == nullptr) {
std::cout << "Error on CreateSessionDescription." << std::endl
<< error.line << std::endl
<< error.description << std::endl;
std::cout << "Offer SDP:begin" << std::endl << parameter << std::endl << "Offer SDP:end" << std::endl;
}
connection.peer_connection->SetRemoteDescription(connection.ssdo, session_description);
connection.sdp_type = "Answer"; // 表示用の文字列、webrtcの動作には関係ない
connection.peer_connection->CreateAnswer(connection.csdo,
webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
}
void cmd_sdp3(const std::string& parameter) {
webrtc::SdpParseError error;
webrtc::SessionDescriptionInterface* session_description(
webrtc::CreateSessionDescription("answer", parameter, &error));
if (session_description == nullptr) {
std::cout << "Error on CreateSessionDescription." << std::endl
<< error.line << std::endl
<< error.description << std::endl;
std::cout << "Answer SDP:begin" << std::endl << parameter << std::endl << "Answer SDP:end" << std::endl;
}
connection.peer_connection->SetRemoteDescription(connection.ssdo, session_description);
}
void cmd_ice1() {
std::cout << picojson::value(connection.ice_array).serialize(true) << std::endl;
connection.ice_array.clear();
}
void cmd_ice2(const std::string& parameter) {
picojson::value v;
std::string err = picojson::parse(v, parameter);
if (!err.empty()) {
std::cout << "Error on parse json : " << err << std::endl;
return;
}
webrtc::SdpParseError err_sdp;
for (auto& ice_it : v.get<picojson::array>()) {
picojson::object& ice_json = ice_it.get<picojson::object>();
webrtc::IceCandidateInterface* ice =
CreateIceCandidate(ice_json.at("sdpMid").get<std::string>(),
static_cast<int>(ice_json.at("sdpMLineIndex").get<double>()),
ice_json.at("candidate").get<std::string>(),
&err_sdp);
if (!err_sdp.line.empty() && !err_sdp.description.empty()) {
std::cout << "Error on CreateIceCandidate" << std::endl
<< err_sdp.line << std::endl
<< err_sdp.description << std::endl;
return;
}
connection.peer_connection->AddIceCandidate(ice);
}
}
void cmd_send(const std::string& parameter) {
webrtc::DataBuffer buffer(rtc::CopyOnWriteBuffer(parameter.c_str(), parameter.size()), true);
std::cout << "Send(" << connection.data_channel->state() << ")" << std::endl;
connection.data_channel->Send(buffer);
}
void cmd_quit() {
// スレッドを活かしながらCloseしないと、別スレッドからのイベント待ちになり終了できなくなる
connection.peer_connection->Close();
connection.peer_connection = nullptr;
connection.data_channel = nullptr;
peer_connection_factory = nullptr;
// リソースを開放したらスレッドを止めてOK
network_thread->Stop();
worker_thread->Stop();
signaling_thread->Stop();
}
int main(int argc, char* argv[]) {
// 第三引数にtrueを指定すると、WebRTC関連の引数をargvから削除してくれるらしい
rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
rtc::FlagList::Print(nullptr, false);
std::cout << std::this_thread::get_id() << ":"
<< "Main thread" << std::endl;
// GoogleのSTUNサーバを利用
webrtc::PeerConnectionInterface::IceServer ice_server;
ice_server.uri = "stun:stun.l.google.com:19302";
configuration.servers.push_back(ice_server);
rtc::InitializeSSL();
network_thread = rtc::Thread::CreateWithSocketServer();
network_thread->Start();
worker_thread = rtc::Thread::Create();
worker_thread->Start();
signaling_thread = rtc::Thread::Create();
signaling_thread->Start();
peer_connection_factory = webrtc::CreatePeerConnectionFactory(
network_thread.get(),
worker_thread.get(),
signaling_thread.get(),
nullptr /* default_adm */,
webrtc::CreateBuiltinAudioEncoderFactory(),
webrtc::CreateBuiltinAudioDecoderFactory(),
webrtc::CreateBuiltinVideoEncoderFactory(),
webrtc::CreateBuiltinVideoDecoderFactory(),
nullptr /* audio_mixer */,
nullptr /* audio_processing */);
if (peer_connection_factory.get() == nullptr) {
std::cout << "Error on CreatePeerConnectionFactory." << std::endl;
return EXIT_FAILURE;
}
std::string line;
std::string command;
std::string parameter;
bool is_cmd_mode = true;
while (std::getline(std::cin, line)) {
if (is_cmd_mode) {
if (line == "") {
continue;
} else if (line == "sdp1") {
cmd_sdp1();
} else if (line == "sdp2") {
command = "sdp2";
is_cmd_mode = false;
} else if (line == "sdp3") {
command = "sdp3";
is_cmd_mode = false;
} else if (line == "ice1") {
cmd_ice1();
} else if (line == "ice2") {
command = "ice2";
is_cmd_mode = false;
} else if (line == "send") {
command = "send";
is_cmd_mode = false;
} else if (line == "quit") {
cmd_quit();
break;
} else {
std::cout << "?" << line << std::endl;
}
} else {
if (line == ";") {
if (command == "sdp2") {
cmd_sdp2(parameter);
} else if (command == "sdp3") {
cmd_sdp3(parameter);
} else if (command == "ice2") {
cmd_ice2(parameter);
} else if (command == "send") {
cmd_send(parameter);
}
parameter = "";
is_cmd_mode = true;
} else {
parameter += line + "\n";
}
}
}
rtc::CleanupSSL();
return 0;
}