2021-05-21 11:13:17 +02:00
|
|
|
#include "rtcPeerHandler.hpp"
|
|
|
|
|
2021-05-21 15:48:56 +02:00
|
|
|
rtcPeerHandler::rtcPeerHandler() {
|
|
|
|
rtc::InitLogger(rtc::LogLevel::Verbose, NULL);
|
|
|
|
}
|
2021-05-21 11:13:17 +02:00
|
|
|
|
2021-05-21 15:48:56 +02:00
|
|
|
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;
|
2021-05-21 11:13:17 +02:00
|
|
|
|
2021-05-21 15:48:56 +02:00
|
|
|
rtc::Configuration conf;
|
|
|
|
conf.enableIceTcp = false;
|
|
|
|
conf.disableAutoNegotiation = false;
|
2021-05-21 11:13:17 +02:00
|
|
|
|
2021-05-21 15:48:56 +02:00
|
|
|
auto pc = std::make_shared<rtc::PeerConnection>(conf);
|
2021-05-21 11:13:17 +02:00
|
|
|
|
2021-05-21 15:48:56 +02:00
|
|
|
rtc::Description::Audio media("audio",
|
|
|
|
rtc::Description::Direction::SendRecv);
|
|
|
|
media.addOpusCodec(96);
|
|
|
|
media.setBitrate(64);
|
2021-05-21 11:13:17 +02:00
|
|
|
|
2021-05-21 15:48:56 +02:00
|
|
|
auto track = pc->addTrack(media);
|
2021-05-21 11:13:17 +02:00
|
|
|
|
2021-05-21 15:48:56 +02:00
|
|
|
// auto session = std::make_shared<rtc::MediaHandler>();
|
2021-05-21 11:13:17 +02:00
|
|
|
|
2021-05-21 15:48:56 +02:00
|
|
|
// track->setMediaHandler(session);
|
2021-05-21 11:13:17 +02:00
|
|
|
|
2021-05-21 15:48:56 +02:00
|
|
|
rtc::Reliability rtcRel;
|
|
|
|
rtcRel.unordered = true;
|
|
|
|
rtcRel.type = rtc::Reliability::Type::Timed;
|
|
|
|
rtcRel.rexmit = 500;
|
2021-05-21 11:13:17 +02:00
|
|
|
|
2021-05-21 15:48:56 +02:00
|
|
|
rtc::DataChannelInit rtcConf;
|
|
|
|
rtcConf.reliability = rtcRel;
|
|
|
|
rtcConf.negotiated = false;
|
2021-05-21 11:13:17 +02:00
|
|
|
|
2021-05-21 15:48:56 +02:00
|
|
|
pc->onStateChange([](rtc::PeerConnection::State state) {
|
2021-05-21 11:13:17 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-05-21 15:48:56 +02:00
|
|
|
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);
|
2021-05-21 11:13:17 +02:00
|
|
|
|
2021-05-21 15:48:56 +02:00
|
|
|
// set handler
|
|
|
|
track->setMediaHandler(opusHandler);
|
|
|
|
track->onOpen(onOpen);
|
|
|
|
auto trackData = make_shared<ClientTrackData>(track, srReporter);
|
|
|
|
return trackData;
|
|
|
|
}*/
|
2021-05-21 11:13:17 +02:00
|
|
|
|
2021-05-21 15:48:56 +02:00
|
|
|
pc->createDataChannel("Fosscord voice connection", rtcConf);
|
2021-05-21 11:13:17 +02:00
|
|
|
}
|