Add source port parameter for RTPWriter

This is required if we want to support hole punching
This commit is contained in:
Aaro Altonen 2019-05-17 09:18:14 +03:00
parent d56b70beaf
commit 6ab00f1b5e
4 changed files with 66 additions and 4 deletions

View File

@ -62,6 +62,32 @@ RTPWriter *RTPContext::createWriter(std::string dstAddr, int dstPort, rtp_format
if ((writer = createWriter(dstAddr, dstPort)) == nullptr)
return nullptr;
conns_.insert(std::pair<int, RTPConnection *>(writer->getId(), writer));
writer->setPayloadType(fmt);
return writer;
}
RTPWriter *RTPContext::createWriter(std::string dstAddr, int dstPort, int srcPort)
{
RTPWriter *writer = new RTPWriter(dstAddr, dstPort, srcPort);
if (!writer) {
std::cerr << "Failed to create RTPWriter for " << dstAddr << ":" << dstPort << "!" << std::endl;
return nullptr;
}
conns_.insert(std::pair<int, RTPConnection *>(writer->getId(), writer));
return writer;
}
RTPWriter *RTPContext::createWriter(std::string dstAddr, int dstPort, int srcPort, rtp_format_t fmt)
{
RTPWriter *writer = nullptr;
if ((writer = createWriter(dstAddr, dstPort, srcPort)) == nullptr)
return nullptr;
conns_.insert(std::pair<int, RTPConnection *>(writer->getId(), writer));
writer->setPayloadType(fmt);
return writer;
}

View File

@ -18,11 +18,13 @@ public:
RTPReader *createReader(std::string srcAddr, int srcPort);
RTPReader *createReader(std::string srcAddr, int srcPort, rtp_format_t fmt);
/* Open connection for writing RTP packets to dstAddr:dstPort
/* Open connection for writing RTP packets to dstAddr:dstPort
*
* Packets can be sent by calling RTPWriter::pushFrame() */
RTPWriter *createWriter(std::string dstAddr, int dstPort);
RTPWriter *createWriter(std::string dstAddr, int dstPort, rtp_format_t fmt);
RTPWriter *createWriter(std::string dstAddr, int dstPort, int srcPort);
RTPWriter *createWriter(std::string dstAddr, int dstPort, int srcPort, rtp_format_t fmt);
RTPConnection *openConnection(std::string dstAddr, int dstPort, int srcPort);
int closeConnection(int id);

View File

@ -2,18 +2,26 @@
#include <cstring>
#include <iostream>
#include "writer.hh"
#include "debug.hh"
#include "rtp_opus.hh"
#include "rtp_hevc.hh"
#include "rtp_generic.hh"
#include "writer.hh"
RTPWriter::RTPWriter(std::string dstAddr, int dstPort):
RTPConnection(false),
dstAddr_(dstAddr),
dstPort_(dstPort)
dstPort_(dstPort),
srcPort_(0)
{
}
RTPWriter::RTPWriter(std::string dstAddr, int dstPort, int srcPort):
RTPWriter(dstAddr, dstPort)
{
srcPort_ = srcPort;
}
RTPWriter::~RTPWriter()
{
}
@ -21,10 +29,34 @@ RTPWriter::~RTPWriter()
int RTPWriter::start()
{
if ((socket_ = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("RTPConnection::open");
LOG_ERROR("Creating socket failed: %s", strerror(errno));
return RTP_SOCKET_ERROR;
}
/* if source port is not 0, writer should be bind to that port so that outgoing packet
* has a correct source port (important for hole punching purposes) */
if (srcPort_ != 0) {
int enable = 1;
if (setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
perror("setsockopt(SO_REUSEADDR) failed");
return RTP_GENERIC_ERROR;
}
LOG_DEBUG("Binding to port %d (source port)", srcPort_);
sockaddr_in addrIn_;
memset(&addrIn_, 0, sizeof(addrIn_));
addrIn_.sin_family = AF_INET;
addrIn_.sin_addr.s_addr = htonl(INADDR_ANY);
addrIn_.sin_port = htons(srcPort_);
if (bind(socket_, (struct sockaddr *) &addrIn_, sizeof(addrIn_)) < 0) {
LOG_ERROR("Binding failed: %s", strerror(errno));
return RTP_BIND_ERROR;
}
}
memset(&addrOut_, 0, sizeof(addrOut_));
addrOut_.sin_family = AF_INET;
inet_pton(AF_INET, dstAddr_.c_str(), &addrOut_.sin_addr);

View File

@ -8,6 +8,7 @@ class RTPWriter : public RTPConnection {
public:
RTPWriter(std::string dstAddr, int dstPort);
RTPWriter(std::string dstAddr, int dstPort, int srcPort);
~RTPWriter();
// open socket for sending frames
@ -24,5 +25,6 @@ public:
private:
std::string dstAddr_;
int dstPort_;
int srcPort_;
sockaddr_in addrOut_;
};