1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-12 19:36:10 -05:00

RTP audio sink fix endianess problem

This commit is contained in:
f4exb 2018-01-31 02:03:23 +01:00
parent 70adea206c
commit 5cbfcccb85
2 changed files with 59 additions and 13 deletions

View File

@ -17,6 +17,7 @@
#include "rtpsink.h" #include "rtpsink.h"
#include "dsp/dsptypes.h" #include "dsp/dsptypes.h"
#include <algorithm>
RTPSink::RTPSink(const QString& address, uint16_t port, PayloadType payloadType) : RTPSink::RTPSink(const QString& address, uint16_t port, PayloadType payloadType) :
m_sampleRate(48000), m_sampleRate(48000),
@ -28,7 +29,10 @@ RTPSink::RTPSink(const QString& address, uint16_t port, PayloadType payloadType)
m_destport(port), m_destport(port),
m_mutex(QMutex::Recursive) m_mutex(QMutex::Recursive)
{ {
qDebug("RTPSink::RTPSink"); uint32_t endianTest32 = 1;
uint8_t *ptr = (uint8_t*) &endianTest32;
m_endianReverse = (*ptr == 1);
m_destip = inet_addr(address.toStdString().c_str()); m_destip = inet_addr(address.toStdString().c_str());
m_destip = ntohl(m_destip); m_destip = ntohl(m_destip);
@ -165,13 +169,17 @@ void RTPSink::addDestination(const QString& address, uint16_t port)
} }
} }
void RTPSink::write(uint8_t *sampleByte) void RTPSink::write(const uint8_t *sampleByte)
{ {
QMutexLocker locker(&m_mutex); QMutexLocker locker(&m_mutex);
if (m_sampleBufferIndex < m_packetSamples) if (m_sampleBufferIndex < m_packetSamples)
{ {
memcpy(&m_byteBuffer[m_sampleBufferIndex*m_sampleBytes], sampleByte, m_sampleBytes); writeNetBuf(&m_byteBuffer[m_sampleBufferIndex*m_sampleBytes],
sampleByte,
elemLength(m_payloadType),
m_sampleBytes,
m_endianReverse);
m_sampleBufferIndex++; m_sampleBufferIndex++;
} }
else else
@ -182,12 +190,12 @@ void RTPSink::write(uint8_t *sampleByte)
qCritical("RTPSink::write: cannot write packet: %s", jrtplib::RTPGetErrorString(status).c_str()); qCritical("RTPSink::write: cannot write packet: %s", jrtplib::RTPGetErrorString(status).c_str());
} }
memcpy(&m_byteBuffer[0], sampleByte, m_sampleBytes); writeNetBuf(&m_byteBuffer[0], sampleByte, elemLength(m_payloadType), m_sampleBytes, m_endianReverse);
m_sampleBufferIndex = 1; m_sampleBufferIndex = 1;
} }
} }
void RTPSink::write(uint8_t *samples, int nbSamples) void RTPSink::write(const uint8_t *samples, int nbSamples)
{ {
int samplesIndex = 0; int samplesIndex = 0;
QMutexLocker locker(&m_mutex); QMutexLocker locker(&m_mutex);
@ -195,10 +203,11 @@ void RTPSink::write(uint8_t *samples, int nbSamples)
// fill remainder of buffer and send it // fill remainder of buffer and send it
if (m_sampleBufferIndex + nbSamples > m_packetSamples) if (m_sampleBufferIndex + nbSamples > m_packetSamples)
{ {
memcpy(&m_byteBuffer[m_sampleBufferIndex*m_sampleBytes], writeNetBuf(&m_byteBuffer[m_sampleBufferIndex*m_sampleBytes],
&samples[samplesIndex*m_sampleBytes], &samples[samplesIndex*m_sampleBytes],
(m_packetSamples - m_sampleBufferIndex)*m_sampleBytes); elemLength(m_payloadType),
(m_packetSamples - m_sampleBufferIndex)*m_sampleBytes,
m_endianReverse);
m_rtpSession.SendPacket((const void *) m_byteBuffer, (std::size_t) m_bufferSize); m_rtpSession.SendPacket((const void *) m_byteBuffer, (std::size_t) m_bufferSize);
nbSamples -= (m_packetSamples - m_sampleBufferIndex); nbSamples -= (m_packetSamples - m_sampleBufferIndex);
m_sampleBufferIndex = 0; m_sampleBufferIndex = 0;
@ -207,14 +216,46 @@ void RTPSink::write(uint8_t *samples, int nbSamples)
// send complete packets // send complete packets
while (nbSamples > m_packetSamples) while (nbSamples > m_packetSamples)
{ {
m_rtpSession.SendPacket((const void *) &samples[samplesIndex*m_sampleBytes], (std::size_t) m_bufferSize); writeNetBuf(m_byteBuffer,
samples,
elemLength(m_payloadType),
m_bufferSize,
m_endianReverse);
m_rtpSession.SendPacket((const void *) m_byteBuffer, (std::size_t) m_bufferSize);
samplesIndex += m_packetSamples; samplesIndex += m_packetSamples;
nbSamples -= m_packetSamples; nbSamples -= m_packetSamples;
} }
// copy remainder of input to buffer // copy remainder of input to buffer
memcpy(&m_byteBuffer[m_sampleBufferIndex*m_sampleBytes], writeNetBuf(&m_byteBuffer[m_sampleBufferIndex*m_sampleBytes],
&samples[samplesIndex*m_sampleBytes], &samples[samplesIndex*m_sampleBytes],
nbSamples*m_sampleBytes); elemLength(m_payloadType),
nbSamples*m_sampleBytes,m_endianReverse);
}
void RTPSink::writeNetBuf(uint8_t *dest, const uint8_t *src, unsigned int elemLen, unsigned int bytesLen, bool endianReverse)
{
for (unsigned int i = 0; i < bytesLen; i += elemLen)
{
memcpy(&dest[i], &src[i], elemLen);
if (endianReverse) {
std::reverse(&dest[i], &dest[i+elemLen]);
}
}
}
unsigned int RTPSink::elemLength(PayloadType payloadType)
{
switch (payloadType)
{
case PayloadL16Stereo:
return sizeof(int16_t);
break;
case PayloadL16Mono:
default:
return sizeof(int16_t);
break;
}
} }

View File

@ -48,10 +48,14 @@ public:
void deleteDestination(const QString& address, uint16_t port); void deleteDestination(const QString& address, uint16_t port);
void addDestination(const QString& address, uint16_t port); void addDestination(const QString& address, uint16_t port);
void write(uint8_t *sampleByte); void write(const uint8_t *sampleByte);
void write(uint8_t *sampleByte, int nbSamples); void write(const uint8_t *sampleByte, int nbSamples);
protected: protected:
/** Reverse endianess in destination buffer */
static void writeNetBuf(uint8_t *dest, const uint8_t *src, unsigned int elemLen, unsigned int bytesLen, bool endianReverse);
static unsigned int elemLength(PayloadType payloadType);
PayloadType m_payloadType; PayloadType m_payloadType;
int m_sampleRate; int m_sampleRate;
int m_sampleBytes; int m_sampleBytes;
@ -64,6 +68,7 @@ protected:
jrtplib::RTPSession m_rtpSession; jrtplib::RTPSession m_rtpSession;
jrtplib::RTPSessionParams m_rtpSessionParams; jrtplib::RTPSessionParams m_rtpSessionParams;
jrtplib::RTPUDPv4TransmissionParams m_rtpTransmissionParams; jrtplib::RTPUDPv4TransmissionParams m_rtpTransmissionParams;
bool m_endianReverse;
QMutex m_mutex; QMutex m_mutex;
}; };