1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-25 09:18:54 -05:00
sdrangel/qrtplib/rtppacketbuilder.cpp

283 lines
7.5 KiB
C++
Raw Normal View History

2018-02-26 19:35:16 -05:00
/*
2018-02-27 17:05:46 -05:00
This file is a part of JRTPLIB
Copyright (c) 1999-2017 Jori Liesenborgs
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
Contact: jori.liesenborgs@gmail.com
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
This library was developed at the Expertise Centre for Digital Media
(http://www.edm.uhasselt.be), a research center of the Hasselt University
(http://www.uhasselt.be). The library is based upon work done for
my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
*/
2018-02-26 19:35:16 -05:00
#include "rtppacketbuilder.h"
#include "rtperrors.h"
#include "rtppacket.h"
#include "rtpsources.h"
#include <time.h>
#include <stdlib.h>
namespace qrtplib
{
2018-02-27 17:05:46 -05:00
RTPPacketBuilder::RTPPacketBuilder(RTPRandom &r) :
2018-03-29 13:55:03 -04:00
rtprnd(r),
maxpacksize(0),
buffer(0),
packetlength(0),
lastwallclocktime(0, 0)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
init = false;
2018-03-29 13:55:03 -04:00
deftsset = false;
defptset = false;
defmarkset = false;
defaultmark = false;
defaulttimestampinc = 0;
ssrc = 0;
timestamp = 0;
seqnr = 0;
prevrtptimestamp = 0;
lastrtptimestamp = 0;
defaultpayloadtype = 0;
numcsrcs = 0;
numpayloadbytes = 0;
numpackets = 0;
2018-06-21 18:15:23 -04:00
memset((char *) csrcs, 0, RTP_MAXCSRCS*sizeof(uint32_t));
2018-02-27 17:05:46 -05:00
timeinit.Dummy();
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
//std::cout << (void *)(&rtprnd) << std::endl;
2018-02-26 19:35:16 -05:00
}
RTPPacketBuilder::~RTPPacketBuilder()
{
2018-02-27 17:05:46 -05:00
Destroy();
2018-02-26 19:35:16 -05:00
}
2018-03-29 13:55:03 -04:00
int RTPPacketBuilder::Init(unsigned int max)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
if (init)
return ERR_RTP_PACKBUILD_ALREADYINIT;
if (max <= 0)
return ERR_RTP_PACKBUILD_INVALIDMAXPACKETSIZE;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
maxpacksize = max;
buffer = new uint8_t[max];
packetlength = 0;
2018-03-29 13:55:03 -04:00
numpackets = 0;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
CreateNewSSRC();
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
deftsset = false;
defptset = false;
defmarkset = false;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
numcsrcs = 0;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
init = true;
return 0;
2018-02-26 19:35:16 -05:00
}
void RTPPacketBuilder::Destroy()
{
2018-02-27 17:05:46 -05:00
if (!init)
return;
delete[] buffer;
init = false;
2018-02-26 19:35:16 -05:00
}
2018-03-29 13:55:03 -04:00
int RTPPacketBuilder::SetMaximumPacketSize(unsigned int max)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
uint8_t *newbuf;
if (max <= 0)
return ERR_RTP_PACKBUILD_INVALIDMAXPACKETSIZE;
newbuf = new uint8_t[max];
delete[] buffer;
buffer = newbuf;
maxpacksize = max;
return 0;
2018-02-26 19:35:16 -05:00
}
int RTPPacketBuilder::AddCSRC(uint32_t csrc)
{
2018-02-27 17:05:46 -05:00
if (!init)
return ERR_RTP_PACKBUILD_NOTINIT;
if (numcsrcs >= RTP_MAXCSRCS)
return ERR_RTP_PACKBUILD_CSRCLISTFULL;
int i;
for (i = 0; i < numcsrcs; i++)
{
if (csrcs[i] == csrc)
return ERR_RTP_PACKBUILD_CSRCALREADYINLIST;
}
csrcs[numcsrcs] = csrc;
numcsrcs++;
return 0;
2018-02-26 19:35:16 -05:00
}
int RTPPacketBuilder::DeleteCSRC(uint32_t csrc)
{
2018-02-27 17:05:46 -05:00
if (!init)
return ERR_RTP_PACKBUILD_NOTINIT;
int i = 0;
bool found = false;
while (!found && i < numcsrcs)
{
if (csrcs[i] == csrc)
found = true;
else
i++;
}
if (!found)
return ERR_RTP_PACKBUILD_CSRCNOTINLIST;
// move the last csrc in the place of the deleted one
numcsrcs--;
if (numcsrcs > 0 && numcsrcs != i)
csrcs[i] = csrcs[numcsrcs];
return 0;
2018-02-26 19:35:16 -05:00
}
void RTPPacketBuilder::ClearCSRCList()
{
2018-02-27 17:05:46 -05:00
if (!init)
return;
numcsrcs = 0;
2018-02-26 19:35:16 -05:00
}
uint32_t RTPPacketBuilder::CreateNewSSRC()
{
2018-02-27 17:05:46 -05:00
ssrc = rtprnd.GetRandom32();
timestamp = rtprnd.GetRandom32();
seqnr = rtprnd.GetRandom16();
2018-03-29 13:55:03 -04:00
qDebug("RTPPacketBuilder::CreateNewSSRC: timestamp: %u", timestamp);
2018-02-27 17:05:46 -05:00
// p 38: the count SHOULD be reset if the sender changes its SSRC identifier
numpayloadbytes = 0;
numpackets = 0;
return ssrc;
2018-02-26 19:35:16 -05:00
}
uint32_t RTPPacketBuilder::CreateNewSSRC(RTPSources &sources)
{
2018-02-27 17:05:46 -05:00
bool found;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
do
{
ssrc = rtprnd.GetRandom32();
found = sources.GotEntry(ssrc);
} while (found);
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
timestamp = rtprnd.GetRandom32();
seqnr = rtprnd.GetRandom16();
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
// p 38: the count SHOULD be reset if the sender changes its SSRC identifier
numpayloadbytes = 0;
numpackets = 0;
return ssrc;
2018-02-26 19:35:16 -05:00
}
2018-03-29 13:55:03 -04:00
int RTPPacketBuilder::BuildPacket(const void *data, unsigned int len)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
if (!init)
return ERR_RTP_PACKBUILD_NOTINIT;
if (!defptset)
return ERR_RTP_PACKBUILD_DEFAULTPAYLOADTYPENOTSET;
if (!defmarkset)
return ERR_RTP_PACKBUILD_DEFAULTMARKNOTSET;
if (!deftsset)
return ERR_RTP_PACKBUILD_DEFAULTTSINCNOTSET;
return PrivateBuildPacket(data, len, defaultpayloadtype, defaultmark, defaulttimestampinc, false);
2018-02-26 19:35:16 -05:00
}
2018-03-29 13:55:03 -04:00
int RTPPacketBuilder::BuildPacket(const void *data, unsigned int len, uint8_t pt, bool mark, uint32_t timestampinc)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
if (!init)
return ERR_RTP_PACKBUILD_NOTINIT;
return PrivateBuildPacket(data, len, pt, mark, timestampinc, false);
2018-02-26 19:35:16 -05:00
}
2018-03-29 13:55:03 -04:00
int RTPPacketBuilder::BuildPacketEx(const void *data, unsigned int len, uint16_t hdrextID, const void *hdrextdata, unsigned int numhdrextwords)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
if (!init)
return ERR_RTP_PACKBUILD_NOTINIT;
if (!defptset)
return ERR_RTP_PACKBUILD_DEFAULTPAYLOADTYPENOTSET;
if (!defmarkset)
return ERR_RTP_PACKBUILD_DEFAULTMARKNOTSET;
if (!deftsset)
return ERR_RTP_PACKBUILD_DEFAULTTSINCNOTSET;
return PrivateBuildPacket(data, len, defaultpayloadtype, defaultmark, defaulttimestampinc, true, hdrextID, hdrextdata, numhdrextwords);
2018-02-26 19:35:16 -05:00
}
2018-03-29 13:55:03 -04:00
int RTPPacketBuilder::BuildPacketEx(const void *data, unsigned int len, uint8_t pt, bool mark, uint32_t timestampinc, uint16_t hdrextID, const void *hdrextdata, unsigned int numhdrextwords)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
if (!init)
return ERR_RTP_PACKBUILD_NOTINIT;
return PrivateBuildPacket(data, len, pt, mark, timestampinc, true, hdrextID, hdrextdata, numhdrextwords);
2018-02-26 19:35:16 -05:00
}
2018-03-29 13:55:03 -04:00
int RTPPacketBuilder::PrivateBuildPacket(const void *data, unsigned int len, uint8_t pt, bool mark, uint32_t timestampinc, bool gotextension, uint16_t hdrextID, const void *hdrextdata,
unsigned int numhdrextwords)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
RTPPacket p(pt, data, len, seqnr, timestamp, ssrc, mark, numcsrcs, csrcs, gotextension, hdrextID, (uint16_t) numhdrextwords, hdrextdata, buffer, maxpacksize);
int status = p.GetCreationError();
if (status < 0)
return status;
packetlength = p.GetPacketLength();
if (numpackets == 0) // first packet
{
lastwallclocktime = RTPTime::CurrentTime();
lastrtptimestamp = timestamp;
prevrtptimestamp = timestamp;
}
else if (timestamp != prevrtptimestamp)
{
lastwallclocktime = RTPTime::CurrentTime();
lastrtptimestamp = timestamp;
prevrtptimestamp = timestamp;
}
numpayloadbytes += (uint32_t) p.GetPayloadLength();
numpackets++;
timestamp += timestampinc;
seqnr++;
2018-03-29 13:55:03 -04:00
//qDebug("RTPPacketBuilder::PrivateBuildPacket: numpackets: %u timestamp: %u timestampinc: %u seqnr: %u", numpackets, timestamp, timestampinc, seqnr);
2018-02-27 17:05:46 -05:00
return 0;
2018-02-26 19:35:16 -05:00
}
} // end namespace