Skip to content

Commit 2ea2bda

Browse files
committed
Add separate offer and answer testclients
This is to test the newly implemented GenerateOffer API.
1 parent dd72a61 commit 2ea2bda

3 files changed

Lines changed: 229 additions & 3 deletions

File tree

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
add_executable(testclient
1+
add_executable(testclientoffer
22
json/json.h
33
json/json-forwards.h
44
easywsclient.cpp
55
easywsclient.hpp
66
jsoncpp.cpp
7-
testclient.cpp
7+
testclient_offer.cpp
88
WebSocketWrapper.cpp
99
WebSocketWrapper.hpp)
10+
target_link_libraries(testclientoffer rtcdcpp)
1011

11-
target_link_libraries(testclient rtcdcpp)
12+
add_executable(testclientanswer
13+
json/json.h
14+
json/json-forwards.h
15+
easywsclient.cpp
16+
easywsclient.hpp
17+
jsoncpp.cpp
18+
testclient_answer.cpp
19+
WebSocketWrapper.cpp
20+
WebSocketWrapper.hpp)
21+
target_link_libraries(testclientanswer rtcdcpp)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* Simple WebRTC test client.
3+
*/
4+
5+
#include "WebSocketWrapper.hpp"
6+
#include "json/json.h"
7+
8+
#include <rtcdcpp/PeerConnection.hpp>
9+
#include <rtcdcpp/Logging.hpp>
10+
11+
#include <iostream>
12+
13+
using namespace rtcdcpp;
14+
15+
int main(void) {
16+
#ifndef SPDLOG_DISABLED
17+
auto console_sink = std::make_shared<spdlog::sinks::ansicolor_sink>(spdlog::sinks::stdout_sink_mt::instance());
18+
spdlog::create("rtcdcpp.PeerConnection", console_sink);
19+
spdlog::create("rtcdcpp.SCTP", console_sink);
20+
spdlog::create("rtcdcpp.Nice", console_sink);
21+
spdlog::create("rtcdcpp.DTLS", console_sink);
22+
spdlog::set_level(spdlog::level::debug);
23+
#endif
24+
25+
WebSocketWrapper ws("ws://localhost:5000/channel/test");
26+
std::shared_ptr<PeerConnection> pc;
27+
std::shared_ptr<DataChannel> dc;
28+
29+
if (!ws.Initialize()) {
30+
std::cout << "WebSocket connection failed\n";
31+
return 0;
32+
}
33+
34+
RTCConfiguration config;
35+
config.ice_servers.emplace_back(RTCIceServer{"stun3.l.google.com", 19302});
36+
37+
bool running = true;
38+
39+
ChunkQueue messages;
40+
41+
std::function<void(std::string)> onMessage = [&messages](std::string msg) {
42+
messages.push(std::shared_ptr<Chunk>(new Chunk((const void *)msg.c_str(), msg.length())));
43+
};
44+
45+
std::function<void(PeerConnection::IceCandidate)> onLocalIceCandidate = [&ws](PeerConnection::IceCandidate candidate) {
46+
Json::Value jsonCandidate;
47+
jsonCandidate["type"] = "candidate";
48+
jsonCandidate["msg"]["candidate"] = candidate.candidate;
49+
jsonCandidate["msg"]["sdpMid"] = candidate.sdpMid;
50+
jsonCandidate["msg"]["sdpMLineIndex"] = candidate.sdpMLineIndex;
51+
52+
Json::StreamWriterBuilder wBuilder;
53+
ws.Send(Json::writeString(wBuilder, jsonCandidate));
54+
};
55+
56+
std::function<void(std::shared_ptr<DataChannel> channel)> onDataChannel = [&dc](std::shared_ptr<DataChannel> channel) {
57+
std::cout << "Hey cool, got a data channel\n";
58+
dc = channel;
59+
dc->SendString("Hello from native code");
60+
//dc->Close();
61+
};
62+
63+
ws.SetOnMessage(onMessage);
64+
ws.Start();
65+
ws.Send("{\"type\": \"client_connected\", \"msg\": {}}");
66+
67+
Json::Reader reader;
68+
Json::StreamWriterBuilder msgBuilder;
69+
70+
while (running) {
71+
ChunkPtr cur_msg = messages.wait_and_pop();
72+
std::string msg((const char *)cur_msg->Data(), cur_msg->Length());
73+
std::cout << msg << "\n";
74+
Json::Value root;
75+
if (reader.parse(msg, root)) {
76+
std::cout << "Got msg of type: " << root["type"] << "\n";
77+
if (root["type"] == "offer") {
78+
std::cout << "Time to get the rtc party started\n";
79+
pc = std::make_shared<PeerConnection>(config, onLocalIceCandidate, onDataChannel);
80+
81+
pc->ParseOffer(root["msg"]["sdp"].asString());
82+
Json::Value answer;
83+
answer["type"] = "answer";
84+
answer["msg"]["sdp"] = pc->GenerateAnswer();
85+
answer["msg"]["type"] = "answer";
86+
87+
std::cout << "Sending Answer: " << answer << "\n";
88+
ws.Send(Json::writeString(msgBuilder, answer));
89+
} else if (root["type"] == "candidate") {
90+
pc->SetRemoteIceCandidate("a=" + root["msg"]["candidate"].asString());
91+
}
92+
} else {
93+
std::cout << "Json parse failed"
94+
<< "\n";
95+
}
96+
}
97+
98+
ws.Close();
99+
100+
return 0;
101+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* Simple WebRTC test client.
3+
* Cpp to JavaScript
4+
*/
5+
6+
#include "WebSocketWrapper.hpp"
7+
#include "json/json.h"
8+
9+
#include <rtcdcpp/PeerConnection.hpp>
10+
#include <rtcdcpp/Logging.hpp>
11+
12+
#include <iostream>
13+
14+
using namespace rtcdcpp;
15+
16+
void OnStrMsg(std::string s){
17+
std::cout << s << "\n" ;
18+
}
19+
int main(void) {
20+
#ifndef SPDLOG_DISABLED
21+
auto console_sink = std::make_shared<spdlog::sinks::ansicolor_sink>(spdlog::sinks::stdout_sink_mt::instance());
22+
spdlog::create("rtcdcpp.PeerConnection", console_sink);
23+
spdlog::create("rtcdcpp.SCTP", console_sink);
24+
spdlog::create("rtcdcpp.Nice", console_sink);
25+
spdlog::create("rtcdcpp.DTLS", console_sink);
26+
spdlog::set_level(spdlog::level::debug);
27+
#endif
28+
29+
WebSocketWrapper ws("ws://localhost:5000/channel/test");
30+
std::shared_ptr<PeerConnection> pc;
31+
std::shared_ptr<DataChannel> dc;
32+
33+
if (!ws.Initialize()) {
34+
std::cout << "WebSocket connection failed\n";
35+
return 0;
36+
}
37+
38+
RTCConfiguration config;
39+
config.ice_servers.emplace_back(RTCIceServer{"stun3.l.google.com", 19302});
40+
41+
bool running = true;
42+
43+
ChunkQueue messages;
44+
45+
std::function<void(std::string)> onMessage = [&messages](std::string msg) {
46+
messages.push(std::shared_ptr<Chunk>(new Chunk((const void *)msg.c_str(), msg.length())));
47+
};
48+
49+
std::function<void(PeerConnection::IceCandidate)> onLocalIceCandidate = [&ws](PeerConnection::IceCandidate candidate) {
50+
Json::Value jsonCandidate;
51+
jsonCandidate["type"] = "candidate";
52+
jsonCandidate["msg"]["candidate"] = candidate.candidate;
53+
jsonCandidate["msg"]["sdpMid"] = candidate.sdpMid;
54+
jsonCandidate["msg"]["sdpMLineIndex"] = candidate.sdpMLineIndex;
55+
56+
Json::StreamWriterBuilder wBuilder;
57+
ws.Send(Json::writeString(wBuilder, jsonCandidate));
58+
};
59+
60+
std::function<void(std::shared_ptr<DataChannel> channel)> onDataChannel = [&dc](std::shared_ptr<DataChannel> channel) {
61+
std::cout << "Hey cool, got a data channel\n";
62+
dc = channel;
63+
dc->SendString("Hello from native code");
64+
dc->SetOnStringMsgCallback(OnStrMsg);
65+
// dc->Close();
66+
};
67+
68+
ws.SetOnMessage(onMessage);
69+
ws.Start();
70+
ws.Send("{\"type\": \"client_connected\", \"msg\": {}}");
71+
72+
Json::Reader reader;
73+
Json::StreamWriterBuilder msgBuilder;
74+
Json::Value jsonOffer;
75+
pc = std::make_shared<PeerConnection>(config, onLocalIceCandidate, onDataChannel);
76+
pc->CreateDataChannel("testchannel","");
77+
std::string offer = pc->GenerateOffer();
78+
79+
jsonOffer["type"] = "offer";
80+
std::cout << "offer" << offer ;
81+
jsonOffer["msg"]["sdp"] = offer;
82+
Json::StreamWriterBuilder wBuilder;
83+
ws.Send(Json::writeString(wBuilder, jsonOffer));
84+
while (running) {
85+
ChunkPtr cur_msg = messages.wait_and_pop();
86+
std::string msg((const char *)cur_msg->Data(), cur_msg->Length());
87+
//std::cout << msg << "\n";
88+
Json::Value root;
89+
if (reader.parse(msg, root)) {
90+
std::cout << "Got msg of type: " << root["type"] << "\n";
91+
if (root["type"] == "answer") {
92+
std::cout << "Time to get the rtc party started\n";
93+
// pc = std::make_shared<PeerConnection>(config, onLocalIceCandidate, onDataChannel);
94+
std::cout << "remote answer sdp as string : " << root["msg"]["sdp"].asString()<< "\n";
95+
pc->ParseOffer(root["msg"]["sdp"].asString());
96+
// Json::Value answer;
97+
// answer["type"] = "answer";
98+
// answer["msg"]["sdp"] = pc->GenerateAnswer();
99+
// answer["msg"]["type"] = "answer";
100+
101+
// std::cout << "Sending Answer: " << answer << "\n";
102+
// ws.Send(Json::writeString(msgBuilder, answer));
103+
} else if (root["type"] == "candidate") {
104+
pc->SetRemoteIceCandidate("a=" + root["msg"]["candidate"].asString());
105+
}
106+
} else {
107+
std::cout << "Json parse failed"
108+
<< "\n";
109+
}
110+
}
111+
112+
ws.Close();
113+
114+
return 0;
115+
}

0 commit comments

Comments
 (0)