summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp36
-rw-r--r--src/protodefs/protos.proto24
-rw-r--r--src/rpcStub.cpp32
-rw-r--r--src/rpcStub.hpp15
-rw-r--r--src/rtcPeerHandler.cpp83
-rw-r--r--src/rtcPeerHandler.hpp32
-rw-r--r--src/rtcServer.hpp0
7 files changed, 222 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644

index 00000000..2fdeceee --- /dev/null +++ b/src/main.cpp
@@ -0,0 +1,36 @@ +// $$$$$$\ $$\ +// $$ __$$\ $$ | +// $$ / \__|$$$$$$\ $$$$$$$\ $$$$$$$\ $$$$$$$\ $$$$$$\ $$$$$$\ $$$$$$$ | +// $$$$\ $$ __$$\ $$ _____|$$ _____|$$ _____|$$ __$$\ $$ __$$\ $$ __$$ | +// $$ _| $$ / $$ |\$$$$$$\ \$$$$$$\ $$ / $$ / $$ |$$ | \__|$$ / $$ | +// $$ | $$ | $$ | \____$$\ \____$$\ $$ | $$ | $$ |$$ | $$ | $$ | +// $$ | \$$$$$$ |$$$$$$$ |$$$$$$$ |\$$$$$$$\ \$$$$$$ |$$ | \$$$$$$$ | +// \__| \______/ \_______/ \_______/ \_______| \______/ \__| \_______| +// +// +// +// $$\ $$$$$$\ +// \__| $$ __$$\ +// $$\ $$\ $$$$$$\ $$\ $$$$$$$\ $$$$$$\ $$ / \__| $$$$$$\ $$$$$$\ $$\ $$\ $$$$$$\ $$$$$$\ +// \$$\ $$ |$$ __$$\ $$ |$$ _____|$$ __$$\ \$$$$$$\ $$ __$$\ $$ __$$\\$$\ $$ |$$ __$$\ $$ __$$\ +// \$$\$$ / $$ / $$ |$$ |$$ / $$$$$$$$ | \____$$\ $$$$$$$$ |$$ | \__|\$$\$$ / $$$$$$$$ |$$ | \__| +// \$$$ / $$ | $$ |$$ |$$ | $$ ____|$$\ $$ |$$ ____|$$ | \$$$ / $$ ____|$$ | +// \$ / \$$$$$$ |$$ |\$$$$$$$\ \$$$$$$$\ \$$$$$$ |\$$$$$$$\ $$ | \$ / \$$$$$$$\ $$ | +// \_/ \______/ \__| \_______| \_______| \______/ \_______|\__| \_/ \_______|\__| +// +// +// + +#include "rtcPeerHandler.hpp" //Handle peer connection requests +#include "rpcStub.hpp" //Handle gRPC communications between the different fosscord elements + +int main(int argc, char **argv){ + + auto commsHandler = std::make_shared<rtcPeerHandler>(); + auto rpcHandler = std::unique_ptr<rpcStub>(new rpcStub(commsHandler, 8057)); + + std::cout << "Server created" << std::endl; + + //rpcHandler->server->Wait(); //blocking, this will need to be threaded + return 0; +} \ No newline at end of file diff --git a/src/protodefs/protos.proto b/src/protodefs/protos.proto new file mode 100644
index 00000000..11face5f --- /dev/null +++ b/src/protodefs/protos.proto
@@ -0,0 +1,24 @@ +syntax = "proto3"; + +package fosscordMedia; + +service fosscordInternals{ + rpc vRequest(voiceRequest) returns (voiceAnswer) {} +} + +message voiceRequest{ //OP1 from gw + uint64 userid = 1; + uint64 guildid = 2; + string ip=3; + uint32 port=4; + string protocol=5; + string rtcConnectionId=6; +} + +message voiceAnswer{//OP2 and OP4 to gw + string ip=1; + uint32 port=3; + repeated string modes=2; + int32 ssrc=4; + string audioCodec=5; +} \ No newline at end of file diff --git a/src/rpcStub.cpp b/src/rpcStub.cpp new file mode 100644
index 00000000..1633aab8 --- /dev/null +++ b/src/rpcStub.cpp
@@ -0,0 +1,32 @@ +#include "rpcStub.hpp" + +class fossCordInternalsImpl final : public fosscordMedia::fosscordInternals::Service { + std::shared_ptr<rtcPeerHandler> ph; + fossCordInternalsImpl(std::shared_ptr<rtcPeerHandler> handler){ + this->ph= handler; + } + grpc::Status vRequest(grpc::ServerContext* ctx, + const fosscordMedia::voiceRequest* req, + fosscordMedia::voiceAnswer* resp) override { + + this->ph->initiateConnection(req->ip(), req->port()); + return grpc::Status::OK; + } +}; + +rpcStub::rpcStub(std::shared_ptr<rtcPeerHandler> handler, int port) { + if (not port) { + port = 8057; + } + this->ph = handler; + + fossCordInternalsImpl* service; + grpc::ServerBuilder builder; + builder.AddListeningPort("0.0.0.0:" + std::to_string(port), + grpc::InsecureServerCredentials()); + builder.RegisterService(service); + + this->server = builder.BuildAndStart(); + + std::cout << "RPC stub listening on port " << port << std::endl; +} \ No newline at end of file diff --git a/src/rpcStub.hpp b/src/rpcStub.hpp new file mode 100644
index 00000000..d183cf3c --- /dev/null +++ b/src/rpcStub.hpp
@@ -0,0 +1,15 @@ +#include <grpc++/grpc++.h> +#include "protodefs/include/protos.grpc.pb.h" +#include "rtcPeerHandler.hpp" + +#ifndef RPCSTUB +#define RPCSTUB +class rpcStub{ + public: + rpcStub(std::shared_ptr<rtcPeerHandler> peerHandler, int port); + std::unique_ptr<grpc::Server> server; + + private: + std::shared_ptr<rtcPeerHandler> ph; +}; +#endif \ No newline at end of file diff --git a/src/rtcPeerHandler.cpp b/src/rtcPeerHandler.cpp new file mode 100644
index 00000000..9bfc6466 --- /dev/null +++ b/src/rtcPeerHandler.cpp
@@ -0,0 +1,83 @@ +#include "rtcPeerHandler.hpp" + +rtcPeerHandler::rtcPeerHandler() { + rtc::InitLogger(rtc::LogLevel::Verbose, NULL); +} + +void rtcPeerHandler::initiateConnection(std::string peerIP, int peerPort) { + // Socket connection between client and server + SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0); + sockaddr_in addr; + addr.sin_addr.s_addr = inet_addr(peerIP.c_str()); + addr.sin_port = htons(peerPort); + addr.sin_family = AF_INET; + + rtc::Configuration conf; + conf.enableIceTcp = false; + conf.disableAutoNegotiation = false; + + auto pc = std::make_shared<rtc::PeerConnection>(conf); + + rtc::Description::Audio media("audio", + rtc::Description::Direction::SendRecv); + media.addOpusCodec(96); + media.setBitrate(64); + + auto track = pc->addTrack(media); + + // auto session = std::make_shared<rtc::MediaHandler>(); + + // track->setMediaHandler(session); + + rtc::Reliability rtcRel; + rtcRel.unordered = true; + rtcRel.type = rtc::Reliability::Type::Timed; + rtcRel.rexmit = 500; + + rtc::DataChannelInit rtcConf; + rtcConf.reliability = rtcRel; + rtcConf.negotiated = false; + + pc->onStateChange([](rtc::PeerConnection::State state) { + std::cout << "State: " << state << std::endl; + if (state == rtc::PeerConnection::State::Disconnected || + state == rtc::PeerConnection::State::Failed || + state == rtc::PeerConnection::State::Closed) { + // remove disconnected client + } + }); + + pc->onGatheringStateChange([](rtc::PeerConnection::GatheringState state) { + std::cout << "Gathering State: " << state << std::endl; + }); + + /*std::tuple<rtc::Track*, rtc::RtcpSrReporter*> addAudio( + + const std::shared_ptr<rtc::PeerConnection> pc, + const uint8_t payloadType, const uint32_t ssrc, const std::string cname, + const std::string msid, const std::function<void(void)> onOpen) { + auto audio = Description::Audio(cname); + audio.addOpusCodec(payloadType); + audio.addSSRC(ssrc, cname, msid, cname); + auto track = pc->addTrack(audio); + // create RTP configuration + auto rtpConfig = make_shared<RtpPacketizationConfig>( + ssrc, cname, payloadType, OpusRtpPacketizer::defaultClockRate); + // create packetizer + auto packetizer = make_shared<OpusRtpPacketizer>(rtpConfig); + // create opus handler + auto opusHandler = make_shared<OpusPacketizationHandler>(packetizer); + + // add RTCP SR handler + auto srReporter = make_shared<RtcpSrReporter>(rtpConfig); + opusHandler->addToChain(srReporter); + + // set handler + track->setMediaHandler(opusHandler); + track->onOpen(onOpen); + auto trackData = make_shared<ClientTrackData>(track, srReporter); + return trackData; + }*/ + + pc->createDataChannel("Fosscord voice connection", rtcConf); +} diff --git a/src/rtcPeerHandler.hpp b/src/rtcPeerHandler.hpp new file mode 100644
index 00000000..3ba32a83 --- /dev/null +++ b/src/rtcPeerHandler.hpp
@@ -0,0 +1,32 @@ +#include "libdatachannel/rtc.hpp" +#include <iostream> +#include <memory> +#include "nlohmann/json.hpp" +#include <array> + +#ifdef _WIN32 +#include <winsock2.h> +#else +#include <arpa/inet.h> +typedef int SOCKET; +#endif + +using json = nlohmann::json; + +#ifndef RTCPEERHANDLER +#define RTCPEERHANDLER +class rtcPeerHandler{ +public: + rtcPeerHandler(); + void initiateConnection(std::string peerIP, int peerPort); + + struct client + { + std::shared_ptr<rtc::PeerConnection> pc; + std::shared_ptr<rtc::DataChannel> dc; + }; + +private: + std::map<SOCKET, client> clients; +}; +#endif \ No newline at end of file diff --git a/src/rtcServer.hpp b/src/rtcServer.hpp new file mode 100644
index 00000000..e69de29b --- /dev/null +++ b/src/rtcServer.hpp