1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-22 16:08:39 -05:00

RTP support: fixed RTPSink (1)

This commit is contained in:
f4exb 2018-02-20 14:50:21 +01:00
parent 9a3832a14f
commit ec262caa33
2 changed files with 25 additions and 2 deletions

View File

@ -18,6 +18,7 @@
#include "rtpsink.h"
#include "dsp/dsptypes.h"
#include <algorithm>
#include <sys/socket.h>
RTPSink::RTPSink(const QString& address, uint16_t port, PayloadType payloadType) :
m_sampleRate(48000),
@ -29,6 +30,19 @@ RTPSink::RTPSink(const QString& address, uint16_t port, PayloadType payloadType)
m_destport(port),
m_mutex(QMutex::Recursive)
{
// Here we use JRTPLIB in a bit funny way since we do not want the socket to bind because we are only sending
// data to a remote party and we don't want to waste a port on the local machine for each possible connection that may not be used.
// Therefore we create a socket and assign it through the SetUseExistingSockets method of the RTPUDPv4TransmissionParams object
// By doing this the socket is left unbound but sending RTP packets with the library is still possible. Other functions may
// not work but we don't care
m_rtpsock = socket(PF_INET,SOCK_DGRAM,0);
if (m_rtpsock < 0) {
qCritical("RTPSink::RTPSink: cannot allocate socket");
m_valid = false;
}
uint32_t endianTest32 = 1;
uint8_t *ptr = (uint8_t*) &endianTest32;
m_endianReverse = (*ptr == 1);
@ -37,25 +51,31 @@ RTPSink::RTPSink(const QString& address, uint16_t port, PayloadType payloadType)
m_destip = ntohl(m_destip);
m_rtpSessionParams.SetOwnTimestampUnit(1.0 / (double) m_sampleRate);
int status = m_rtpSession.Create(m_rtpSessionParams);
m_rtpTransmissionParams.SetUseExistingSockets(m_rtpsock, m_rtpsock);
int status = m_rtpSession.Create(m_rtpSessionParams, &m_rtpTransmissionParams);
if (status < 0) {
qCritical("RTPSink::RTPSink: cannot create session: %s", jrtplib::RTPGetErrorString(status).c_str());
m_valid = false;
} else {
qDebug("RTPSink::RTPSink: created session: %s", jrtplib::RTPGetErrorString(status).c_str());
}
setPayloadType(payloadType);
m_valid = true;
}
RTPSink::~RTPSink()
{
jrtplib::RTPTime delay = jrtplib::RTPTime(10.0);
m_rtpSession.BYEDestroy(delay, "Time's up", 9);
m_rtpSession.sDestroy(delay, "Time's up", 9);
if (m_byteBuffer) {
delete[] m_byteBuffer;
}
close(m_rtpsock);
}
void RTPSink::setPayloadType(PayloadType payloadType)

View File

@ -42,6 +42,7 @@ public:
RTPSink(const QString& address, uint16_t port, PayloadType payloadType = PayloadL16Mono);
~RTPSink();
bool isValid() const { return m_valid; }
void setPayloadType(PayloadType payloadType);
void setDestination(const QString& address, uint16_t port);
@ -56,6 +57,8 @@ protected:
static void writeNetBuf(uint8_t *dest, const uint8_t *src, unsigned int elemLen, unsigned int bytesLen, bool endianReverse);
static unsigned int elemLength(PayloadType payloadType);
int m_rtpsock;
bool m_valid;
PayloadType m_payloadType;
int m_sampleRate;
int m_sampleBytes;