|
| 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