1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-08 01:26:01 -05:00
sdrangel/qrtplib/rtpsession.cpp

1465 lines
32 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 "rtpsession.h"
#include "rtperrors.h"
#include "rtpudpv4transmitter.h"
#include "rtptcptransmitter.h"
#include "rtpexternaltransmitter.h"
#include "rtpsessionparams.h"
#include "rtpdefines.h"
#include "rtprawpacket.h"
#include "rtppacket.h"
#include "rtptimeutilities.h"
#include "rtprandomrand48.h"
#include "rtprandomrands.h"
#include "rtprandomurandom.h"
#ifdef RTP_SUPPORT_SENDAPP
2018-02-27 17:05:46 -05:00
#include "rtcpcompoundpacket.h"
2018-02-26 19:35:16 -05:00
#endif // RTP_SUPPORT_SENDAPP
#include "rtpinternalutils.h"
#ifndef WIN32
2018-02-27 17:05:46 -05:00
#include <unistd.h>
#include <stdlib.h>
2018-02-26 19:35:16 -05:00
#else
2018-02-27 17:05:46 -05:00
#include <winbase.h>
2018-02-26 19:35:16 -05:00
#endif // WIN32
2018-02-26 19:43:41 -05:00
#define SOURCES_LOCK
#define SOURCES_UNLOCK
#define BUILDER_LOCK
#define BUILDER_UNLOCK
#define SCHED_LOCK
#define SCHED_UNLOCK
#define PACKSENT_LOCK
#define PACKSENT_UNLOCK
2018-02-26 19:35:16 -05:00
namespace qrtplib
{
2018-02-27 17:05:46 -05:00
RTPSession::RTPSession(RTPRandom *r) :
rtprnd(GetRandomNumberGenerator(r)), sources(*this), packetbuilder(*rtprnd), rtcpsched(sources, *rtprnd), rtcpbuilder(sources, packetbuilder)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
// We're not going to set these flags in Create, so that the constructor of a derived class
// can already change them
m_changeIncomingData = false;
m_changeOutgoingData = false;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
created = false;
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
}
RTPSession::~RTPSession()
{
2018-02-27 17:05:46 -05:00
Destroy();
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (deletertprnd)
delete rtprnd;
2018-02-26 19:35:16 -05:00
}
2018-02-27 17:05:46 -05:00
int RTPSession::Create(const RTPSessionParams &sessparams, const RTPTransmissionParams *transparams /* = 0 */, RTPTransmitter::TransmissionProtocol protocol)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
int status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (created)
return ERR_RTP_SESSION_ALREADYCREATED;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
usingpollthread = sessparams.IsUsingPollThread();
needthreadsafety = sessparams.NeedThreadSafety();
if (usingpollthread && !needthreadsafety)
return ERR_RTP_SESSION_THREADSAFETYCONFLICT;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
useSR_BYEifpossible = sessparams.GetSenderReportForBYE();
sentpackets = false;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
// Check max packet size
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if ((maxpacksize = sessparams.GetMaximumPacketSize()) < RTP_MINPACKETSIZE)
return ERR_RTP_SESSION_MAXPACKETSIZETOOSMALL;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
// Initialize the transmission component
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
rtptrans = 0;
switch (protocol)
{
case RTPTransmitter::IPv4UDPProto:
rtptrans = new RTPUDPv4Transmitter();
break;
case RTPTransmitter::ExternalProto:
rtptrans = new RTPExternalTransmitter();
break;
case RTPTransmitter::UserDefinedProto:
rtptrans = NewUserDefinedTransmitter();
if (rtptrans == 0)
return ERR_RTP_SESSION_USERDEFINEDTRANSMITTERNULL;
break;
case RTPTransmitter::TCPProto:
rtptrans = new RTPTCPTransmitter();
break;
default:
return ERR_RTP_SESSION_UNSUPPORTEDTRANSMISSIONPROTOCOL;
}
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (rtptrans == 0)
return ERR_RTP_OUTOFMEM;
if ((status = rtptrans->Init(needthreadsafety)) < 0)
{
delete rtptrans;
return status;
}
if ((status = rtptrans->Create(maxpacksize, transparams)) < 0)
{
delete rtptrans;
return status;
}
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
deletetransmitter = true;
return InternalCreate(sessparams);
2018-02-26 19:35:16 -05:00
}
2018-02-27 17:05:46 -05:00
int RTPSession::Create(const RTPSessionParams &sessparams, RTPTransmitter *transmitter)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
int status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (created)
return ERR_RTP_SESSION_ALREADYCREATED;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
usingpollthread = sessparams.IsUsingPollThread();
needthreadsafety = sessparams.NeedThreadSafety();
if (usingpollthread && !needthreadsafety)
return ERR_RTP_SESSION_THREADSAFETYCONFLICT;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
useSR_BYEifpossible = sessparams.GetSenderReportForBYE();
sentpackets = false;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
// Check max packet size
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if ((maxpacksize = sessparams.GetMaximumPacketSize()) < RTP_MINPACKETSIZE)
return ERR_RTP_SESSION_MAXPACKETSIZETOOSMALL;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
rtptrans = transmitter;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if ((status = rtptrans->SetMaximumPacketSize(maxpacksize)) < 0)
return status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
deletetransmitter = false;
return InternalCreate(sessparams);
2018-02-26 19:35:16 -05:00
}
int RTPSession::InternalCreate(const RTPSessionParams &sessparams)
{
2018-02-27 17:05:46 -05:00
int status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
// Initialize packet builder
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if ((status = packetbuilder.Init(maxpacksize)) < 0)
{
if (deletetransmitter)
delete rtptrans;
return status;
}
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (sessparams.GetUsePredefinedSSRC())
packetbuilder.AdjustSSRC(sessparams.GetPredefinedSSRC());
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
// Add our own ssrc to the source table
if ((status = sources.CreateOwnSSRC(packetbuilder.GetSSRC())) < 0)
{
packetbuilder.Destroy();
if (deletetransmitter)
delete rtptrans;
return status;
}
// Set the initial receive mode
if ((status = rtptrans->SetReceiveMode(sessparams.GetReceiveMode())) < 0)
{
packetbuilder.Destroy();
sources.Clear();
if (deletetransmitter)
delete rtptrans;
return status;
}
// Init the RTCP packet builder
double timestampunit = sessparams.GetOwnTimestampUnit();
uint8_t buf[1024];
size_t buflen = 1024;
std::string forcedcname = sessparams.GetCNAME();
if (forcedcname.length() == 0)
{
if ((status = CreateCNAME(buf, &buflen, sessparams.GetResolveLocalHostname())) < 0)
{
packetbuilder.Destroy();
sources.Clear();
if (deletetransmitter)
delete rtptrans;
return status;
}
}
else
{
RTP_STRNCPY((char * )buf, forcedcname.c_str(), buflen);
buf[buflen - 1] = 0;
buflen = strlen((char *) buf);
}
if ((status = rtcpbuilder.Init(maxpacksize, timestampunit, buf, buflen)) < 0)
{
packetbuilder.Destroy();
sources.Clear();
if (deletetransmitter)
delete rtptrans;
return status;
}
// Set scheduler parameters
rtcpsched.Reset();
rtcpsched.SetHeaderOverhead(rtptrans->GetHeaderOverhead());
RTCPSchedulerParams schedparams;
sessionbandwidth = sessparams.GetSessionBandwidth();
controlfragment = sessparams.GetControlTrafficFraction();
if ((status = schedparams.SetRTCPBandwidth(sessionbandwidth * controlfragment)) < 0)
{
if (deletetransmitter)
delete rtptrans;
packetbuilder.Destroy();
sources.Clear();
rtcpbuilder.Destroy();
return status;
}
if ((status = schedparams.SetSenderBandwidthFraction(sessparams.GetSenderControlBandwidthFraction())) < 0)
{
if (deletetransmitter)
delete rtptrans;
packetbuilder.Destroy();
sources.Clear();
rtcpbuilder.Destroy();
return status;
}
if ((status = schedparams.SetMinimumTransmissionInterval(sessparams.GetMinimumRTCPTransmissionInterval())) < 0)
{
if (deletetransmitter)
delete rtptrans;
packetbuilder.Destroy();
sources.Clear();
rtcpbuilder.Destroy();
return status;
}
schedparams.SetUseHalfAtStartup(sessparams.GetUseHalfRTCPIntervalAtStartup());
schedparams.SetRequestImmediateBYE(sessparams.GetRequestImmediateBYE());
rtcpsched.SetParameters(schedparams);
// copy other parameters
acceptownpackets = sessparams.AcceptOwnPackets();
membermultiplier = sessparams.GetSourceTimeoutMultiplier();
sendermultiplier = sessparams.GetSenderTimeoutMultiplier();
byemultiplier = sessparams.GetBYETimeoutMultiplier();
collisionmultiplier = sessparams.GetCollisionTimeoutMultiplier();
notemultiplier = sessparams.GetNoteTimeoutMultiplier();
// Do thread stuff if necessary
created = true;
return 0;
2018-02-26 19:35:16 -05:00
}
void RTPSession::Destroy()
{
2018-02-27 17:05:46 -05:00
if (!created)
return;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (deletetransmitter)
delete rtptrans;
packetbuilder.Destroy();
rtcpbuilder.Destroy();
rtcpsched.Reset();
collisionlist.Clear();
sources.Clear();
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
std::list<RTCPCompoundPacket *>::const_iterator it;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
for (it = byepackets.begin(); it != byepackets.end(); it++)
delete *it;
byepackets.clear();
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
created = false;
2018-02-26 19:35:16 -05:00
}
2018-02-27 17:05:46 -05:00
void RTPSession::BYEDestroy(const RTPTime &maxwaittime, const void *reason, size_t reasonlength)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
if (!created)
return;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
// first, stop the thread so we have full control over all components
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
RTPTime stoptime = RTPTime::CurrentTime();
stoptime += maxwaittime;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
// add bye packet to the list if we've sent data
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
RTCPCompoundPacket *pack;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (sentpackets)
{
int status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
reasonlength = (reasonlength > RTCP_BYE_MAXREASONLENGTH) ? RTCP_BYE_MAXREASONLENGTH : reasonlength;
status = rtcpbuilder.BuildBYEPacket(&pack, reason, reasonlength, useSR_BYEifpossible);
if (status >= 0)
{
byepackets.push_back(pack);
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (byepackets.size() == 1)
rtcpsched.ScheduleBYEPacket(pack->GetCompoundPacketLength());
}
}
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (!byepackets.empty())
{
bool done = false;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
while (!done)
{
RTPTime curtime = RTPTime::CurrentTime();
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (curtime >= stoptime)
done = true;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (rtcpsched.IsTime())
{
pack = *(byepackets.begin());
byepackets.pop_front();
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
SendRTCPData(pack->GetCompoundPacketData(), pack->GetCompoundPacketLength());
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
OnSendRTCPCompoundPacket(pack); // we'll place this after the actual send to avoid tampering
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
delete pack;
if (!byepackets.empty()) // more bye packets to send, schedule them
rtcpsched.ScheduleBYEPacket((*(byepackets.begin()))->GetCompoundPacketLength());
else
done = true;
}
if (!done)
RTPTime::Wait(RTPTime(0, 100000));
}
}
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (deletetransmitter)
delete rtptrans;
packetbuilder.Destroy();
rtcpbuilder.Destroy();
rtcpsched.Reset();
collisionlist.Clear();
sources.Clear();
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
// clear rest of bye packets
std::list<RTCPCompoundPacket *>::const_iterator it;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
for (it = byepackets.begin(); it != byepackets.end(); it++)
delete *it;
byepackets.clear();
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
created = false;
2018-02-26 19:35:16 -05:00
}
bool RTPSession::IsActive()
{
2018-02-27 17:05:46 -05:00
return created;
2018-02-26 19:35:16 -05:00
}
uint32_t RTPSession::GetLocalSSRC()
{
2018-02-27 17:05:46 -05:00
if (!created)
return 0;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
uint32_t ssrc;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
BUILDER_LOCK
ssrc = packetbuilder.GetSSRC();
BUILDER_UNLOCK
return ssrc;
2018-02-26 19:35:16 -05:00
}
int RTPSession::AddDestination(const RTPAddress &addr)
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
return rtptrans->AddDestination(addr);
2018-02-26 19:35:16 -05:00
}
int RTPSession::DeleteDestination(const RTPAddress &addr)
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
return rtptrans->DeleteDestination(addr);
2018-02-26 19:35:16 -05:00
}
void RTPSession::ClearDestinations()
{
2018-02-27 17:05:46 -05:00
if (!created)
return;
rtptrans->ClearDestinations();
2018-02-26 19:35:16 -05:00
}
bool RTPSession::SupportsMulticasting()
{
2018-02-27 17:05:46 -05:00
if (!created)
return false;
return rtptrans->SupportsMulticasting();
2018-02-26 19:35:16 -05:00
}
int RTPSession::JoinMulticastGroup(const RTPAddress &addr)
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
return rtptrans->JoinMulticastGroup(addr);
2018-02-26 19:35:16 -05:00
}
int RTPSession::LeaveMulticastGroup(const RTPAddress &addr)
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
return rtptrans->LeaveMulticastGroup(addr);
2018-02-26 19:35:16 -05:00
}
void RTPSession::LeaveAllMulticastGroups()
{
2018-02-27 17:05:46 -05:00
if (!created)
return;
rtptrans->LeaveAllMulticastGroups();
}
int RTPSession::SendPacket(const void *data, size_t len)
{
int status;
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
BUILDER_LOCK
if ((status = packetbuilder.BuildPacket(data, len)) < 0)
{
BUILDER_UNLOCK
return status;
}
if ((status = SendRTPData(packetbuilder.GetPacket(), packetbuilder.GetPacketLength())) < 0)
{
BUILDER_UNLOCK
return status;
}
BUILDER_UNLOCK
SOURCES_LOCK
sources.SentRTPPacket();
SOURCES_UNLOCK
PACKSENT_LOCK
sentpackets = true;
PACKSENT_UNLOCK
return 0;
}
int RTPSession::SendPacket(const void *data, size_t len, uint8_t pt, bool mark, uint32_t timestampinc)
{
int status;
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
BUILDER_LOCK
if ((status = packetbuilder.BuildPacket(data, len, pt, mark, timestampinc)) < 0)
{
BUILDER_UNLOCK
return status;
}
if ((status = SendRTPData(packetbuilder.GetPacket(), packetbuilder.GetPacketLength())) < 0)
{
BUILDER_UNLOCK
return status;
}
BUILDER_UNLOCK
SOURCES_LOCK
sources.SentRTPPacket();
SOURCES_UNLOCK
PACKSENT_LOCK
sentpackets = true;
PACKSENT_UNLOCK
return 0;
}
int RTPSession::SendPacketEx(const void *data, size_t len, uint16_t hdrextID, const void *hdrextdata, size_t numhdrextwords)
{
int status;
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
BUILDER_LOCK
if ((status = packetbuilder.BuildPacketEx(data, len, hdrextID, hdrextdata, numhdrextwords)) < 0)
{
BUILDER_UNLOCK
return status;
}
if ((status = SendRTPData(packetbuilder.GetPacket(), packetbuilder.GetPacketLength())) < 0)
{
BUILDER_UNLOCK
return status;
}
BUILDER_UNLOCK
SOURCES_LOCK
sources.SentRTPPacket();
SOURCES_UNLOCK
PACKSENT_LOCK
sentpackets = true;
PACKSENT_UNLOCK
return 0;
}
int RTPSession::SendPacketEx(const void *data, size_t len, uint8_t pt, bool mark, uint32_t timestampinc, uint16_t hdrextID, const void *hdrextdata, size_t numhdrextwords)
{
int status;
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
BUILDER_LOCK
if ((status = packetbuilder.BuildPacketEx(data, len, pt, mark, timestampinc, hdrextID, hdrextdata, numhdrextwords)) < 0)
{
BUILDER_UNLOCK
return status;
}
if ((status = SendRTPData(packetbuilder.GetPacket(), packetbuilder.GetPacketLength())) < 0)
{
BUILDER_UNLOCK
return status;
}
BUILDER_UNLOCK
SOURCES_LOCK
sources.SentRTPPacket();
SOURCES_UNLOCK
PACKSENT_LOCK
sentpackets = true;
PACKSENT_UNLOCK
return 0;
2018-02-26 19:35:16 -05:00
}
#ifdef RTP_SUPPORT_SENDAPP
int RTPSession::SendRTCPAPPPacket(uint8_t subtype, const uint8_t name[4], const void *appdata, size_t appdatalen)
{
2018-02-27 17:05:46 -05:00
int status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
BUILDER_LOCK
uint32_t ssrc = packetbuilder.GetSSRC();
BUILDER_UNLOCK
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
RTCPCompoundPacketBuilder pb;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
status = pb.InitBuild(maxpacksize);
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (status < 0)
return status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
//first packet in an rtcp compound packet should always be SR or RR
if ((status = pb.StartReceiverReport(ssrc)) < 0)
return status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
//add SDES packet with CNAME item
if ((status = pb.AddSDESSource(ssrc)) < 0)
return status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
BUILDER_LOCK
size_t owncnamelen = 0;
uint8_t *owncname = rtcpbuilder.GetLocalCNAME(&owncnamelen);
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if ((status = pb.AddSDESNormalItem(RTCPSDESPacket::CNAME, owncname, owncnamelen)) < 0)
{
BUILDER_UNLOCK
return status;
}
BUILDER_UNLOCK
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
//add our application specific packet
if ((status = pb.AddAPPPacket(subtype, ssrc, name, appdata, appdatalen)) < 0)
return status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if ((status = pb.EndBuild()) < 0)
return status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
//send packet
status = SendRTCPData(pb.GetCompoundPacketData(), pb.GetCompoundPacketLength());
if (status < 0)
return status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
PACKSENT_LOCK
sentpackets = true;
PACKSENT_UNLOCK
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
return pb.GetCompoundPacketLength();
2018-02-26 19:35:16 -05:00
}
#endif // RTP_SUPPORT_SENDAPP
int RTPSession::SendRawData(const void *data, size_t len, bool usertpchannel)
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
int status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (usertpchannel)
status = rtptrans->SendRTPData(data, len);
else
status = rtptrans->SendRTCPData(data, len);
return status;
2018-02-26 19:35:16 -05:00
}
int RTPSession::SetDefaultPayloadType(uint8_t pt)
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
int status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
BUILDER_LOCK
status = packetbuilder.SetDefaultPayloadType(pt);
BUILDER_UNLOCK
return status;
2018-02-26 19:35:16 -05:00
}
int RTPSession::SetDefaultMark(bool m)
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
int status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
BUILDER_LOCK
status = packetbuilder.SetDefaultMark(m);
BUILDER_UNLOCK
return status;
2018-02-26 19:35:16 -05:00
}
int RTPSession::SetDefaultTimestampIncrement(uint32_t timestampinc)
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
int status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
BUILDER_LOCK
status = packetbuilder.SetDefaultTimestampIncrement(timestampinc);
BUILDER_UNLOCK
return status;
2018-02-26 19:35:16 -05:00
}
int RTPSession::IncrementTimestamp(uint32_t inc)
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
int status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
BUILDER_LOCK
status = packetbuilder.IncrementTimestamp(inc);
BUILDER_UNLOCK
return status;
2018-02-26 19:35:16 -05:00
}
int RTPSession::IncrementTimestampDefault()
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
int status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
BUILDER_LOCK
status = packetbuilder.IncrementTimestampDefault();
BUILDER_UNLOCK
return status;
2018-02-26 19:35:16 -05:00
}
int RTPSession::SetPreTransmissionDelay(const RTPTime &delay)
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
int status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
BUILDER_LOCK
status = rtcpbuilder.SetPreTransmissionDelay(delay);
BUILDER_UNLOCK
return status;
2018-02-26 19:35:16 -05:00
}
RTPTransmissionInfo *RTPSession::GetTransmissionInfo()
{
2018-02-27 17:05:46 -05:00
if (!created)
return 0;
return rtptrans->GetTransmissionInfo();
2018-02-26 19:35:16 -05:00
}
void RTPSession::DeleteTransmissionInfo(RTPTransmissionInfo *inf)
{
2018-02-27 17:05:46 -05:00
if (!created)
return;
rtptrans->DeleteTransmissionInfo(inf);
2018-02-26 19:35:16 -05:00
}
int RTPSession::Poll()
{
2018-02-27 17:05:46 -05:00
int status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
if (usingpollthread)
return ERR_RTP_SESSION_USINGPOLLTHREAD;
if ((status = rtptrans->Poll()) < 0)
return status;
return ProcessPolledData();
2018-02-26 19:35:16 -05:00
}
2018-02-27 17:05:46 -05:00
int RTPSession::WaitForIncomingData(const RTPTime &delay, bool *dataavailable)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
if (usingpollthread)
return ERR_RTP_SESSION_USINGPOLLTHREAD;
return rtptrans->WaitForIncomingData(delay, dataavailable);
2018-02-26 19:35:16 -05:00
}
int RTPSession::AbortWait()
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
if (usingpollthread)
return ERR_RTP_SESSION_USINGPOLLTHREAD;
return rtptrans->AbortWait();
2018-02-26 19:35:16 -05:00
}
RTPTime RTPSession::GetRTCPDelay()
{
2018-02-27 17:05:46 -05:00
if (!created)
return RTPTime(0, 0);
if (usingpollthread)
return RTPTime(0, 0);
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
SOURCES_LOCK
SCHED_LOCK
RTPTime t = rtcpsched.GetTransmissionDelay();
SCHED_UNLOCK
SOURCES_UNLOCK
return t;
2018-02-26 19:35:16 -05:00
}
int RTPSession::BeginDataAccess()
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
SOURCES_LOCK
return 0;
2018-02-26 19:35:16 -05:00
}
bool RTPSession::GotoFirstSource()
{
2018-02-27 17:05:46 -05:00
if (!created)
return false;
return sources.GotoFirstSource();
2018-02-26 19:35:16 -05:00
}
bool RTPSession::GotoNextSource()
{
2018-02-27 17:05:46 -05:00
if (!created)
return false;
return sources.GotoNextSource();
2018-02-26 19:35:16 -05:00
}
bool RTPSession::GotoPreviousSource()
{
2018-02-27 17:05:46 -05:00
if (!created)
return false;
return sources.GotoPreviousSource();
2018-02-26 19:35:16 -05:00
}
bool RTPSession::GotoFirstSourceWithData()
{
2018-02-27 17:05:46 -05:00
if (!created)
return false;
return sources.GotoFirstSourceWithData();
2018-02-26 19:35:16 -05:00
}
bool RTPSession::GotoNextSourceWithData()
{
2018-02-27 17:05:46 -05:00
if (!created)
return false;
return sources.GotoNextSourceWithData();
2018-02-26 19:35:16 -05:00
}
bool RTPSession::GotoPreviousSourceWithData()
{
2018-02-27 17:05:46 -05:00
if (!created)
return false;
return sources.GotoPreviousSourceWithData();
2018-02-26 19:35:16 -05:00
}
RTPSourceData *RTPSession::GetCurrentSourceInfo()
{
2018-02-27 17:05:46 -05:00
if (!created)
return 0;
return sources.GetCurrentSourceInfo();
2018-02-26 19:35:16 -05:00
}
RTPSourceData *RTPSession::GetSourceInfo(uint32_t ssrc)
{
2018-02-27 17:05:46 -05:00
if (!created)
return 0;
return sources.GetSourceInfo(ssrc);
2018-02-26 19:35:16 -05:00
}
RTPPacket *RTPSession::GetNextPacket()
{
2018-02-27 17:05:46 -05:00
if (!created)
return 0;
return sources.GetNextPacket();
2018-02-26 19:35:16 -05:00
}
uint16_t RTPSession::GetNextSequenceNumber() const
{
return packetbuilder.GetSequenceNumber();
}
void RTPSession::DeletePacket(RTPPacket *p)
{
2018-02-27 17:05:46 -05:00
delete p;
2018-02-26 19:35:16 -05:00
}
int RTPSession::EndDataAccess()
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
SOURCES_UNLOCK
return 0;
2018-02-26 19:35:16 -05:00
}
int RTPSession::SetReceiveMode(RTPTransmitter::ReceiveMode m)
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
return rtptrans->SetReceiveMode(m);
2018-02-26 19:35:16 -05:00
}
int RTPSession::AddToIgnoreList(const RTPAddress &addr)
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
return rtptrans->AddToIgnoreList(addr);
2018-02-26 19:35:16 -05:00
}
int RTPSession::DeleteFromIgnoreList(const RTPAddress &addr)
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
return rtptrans->DeleteFromIgnoreList(addr);
2018-02-26 19:35:16 -05:00
}
void RTPSession::ClearIgnoreList()
{
2018-02-27 17:05:46 -05:00
if (!created)
return;
rtptrans->ClearIgnoreList();
2018-02-26 19:35:16 -05:00
}
int RTPSession::AddToAcceptList(const RTPAddress &addr)
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
return rtptrans->AddToAcceptList(addr);
2018-02-26 19:35:16 -05:00
}
int RTPSession::DeleteFromAcceptList(const RTPAddress &addr)
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
return rtptrans->DeleteFromAcceptList(addr);
2018-02-26 19:35:16 -05:00
}
void RTPSession::ClearAcceptList()
{
2018-02-27 17:05:46 -05:00
if (!created)
return;
rtptrans->ClearAcceptList();
2018-02-26 19:35:16 -05:00
}
int RTPSession::SetMaximumPacketSize(size_t s)
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
if (s < RTP_MINPACKETSIZE)
return ERR_RTP_SESSION_MAXPACKETSIZETOOSMALL;
int status;
if ((status = rtptrans->SetMaximumPacketSize(s)) < 0)
return status;
BUILDER_LOCK
if ((status = packetbuilder.SetMaximumPacketSize(s)) < 0)
{
BUILDER_UNLOCK
// restore previous max packet size
rtptrans->SetMaximumPacketSize(maxpacksize);
return status;
}
if ((status = rtcpbuilder.SetMaximumPacketSize(s)) < 0)
{
// restore previous max packet size
packetbuilder.SetMaximumPacketSize(maxpacksize);
BUILDER_UNLOCK
rtptrans->SetMaximumPacketSize(maxpacksize);
return status;
}
BUILDER_UNLOCK
maxpacksize = s;
return 0;
2018-02-26 19:35:16 -05:00
}
int RTPSession::SetSessionBandwidth(double bw)
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
int status;
SCHED_LOCK
RTCPSchedulerParams p = rtcpsched.GetParameters();
status = p.SetRTCPBandwidth(bw * controlfragment);
if (status >= 0)
{
rtcpsched.SetParameters(p);
sessionbandwidth = bw;
}
SCHED_UNLOCK
return status;
2018-02-26 19:35:16 -05:00
}
int RTPSession::SetTimestampUnit(double u)
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
int status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
BUILDER_LOCK
status = rtcpbuilder.SetTimestampUnit(u);
BUILDER_UNLOCK
return status;
2018-02-26 19:35:16 -05:00
}
void RTPSession::SetNameInterval(int count)
{
2018-02-27 17:05:46 -05:00
if (!created)
return;
BUILDER_LOCK
rtcpbuilder.SetNameInterval(count);
BUILDER_UNLOCK
2018-02-26 19:35:16 -05:00
}
void RTPSession::SetEMailInterval(int count)
{
2018-02-27 17:05:46 -05:00
if (!created)
return;
BUILDER_LOCK
rtcpbuilder.SetEMailInterval(count);
BUILDER_UNLOCK
2018-02-26 19:35:16 -05:00
}
void RTPSession::SetLocationInterval(int count)
{
2018-02-27 17:05:46 -05:00
if (!created)
return;
BUILDER_LOCK
rtcpbuilder.SetLocationInterval(count);
BUILDER_UNLOCK
2018-02-26 19:35:16 -05:00
}
void RTPSession::SetPhoneInterval(int count)
{
2018-02-27 17:05:46 -05:00
if (!created)
return;
BUILDER_LOCK
rtcpbuilder.SetPhoneInterval(count);
BUILDER_UNLOCK
2018-02-26 19:35:16 -05:00
}
void RTPSession::SetToolInterval(int count)
{
2018-02-27 17:05:46 -05:00
if (!created)
return;
BUILDER_LOCK
rtcpbuilder.SetToolInterval(count);
BUILDER_UNLOCK
2018-02-26 19:35:16 -05:00
}
void RTPSession::SetNoteInterval(int count)
{
2018-02-27 17:05:46 -05:00
if (!created)
return;
BUILDER_LOCK
rtcpbuilder.SetNoteInterval(count);
BUILDER_UNLOCK
2018-02-26 19:35:16 -05:00
}
2018-02-27 17:05:46 -05:00
int RTPSession::SetLocalName(const void *s, size_t len)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
int status;
BUILDER_LOCK
status = rtcpbuilder.SetLocalName(s, len);
BUILDER_UNLOCK
return status;
2018-02-26 19:35:16 -05:00
}
2018-02-27 17:05:46 -05:00
int RTPSession::SetLocalEMail(const void *s, size_t len)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
int status;
BUILDER_LOCK
status = rtcpbuilder.SetLocalEMail(s, len);
BUILDER_UNLOCK
return status;
2018-02-26 19:35:16 -05:00
}
2018-02-27 17:05:46 -05:00
int RTPSession::SetLocalLocation(const void *s, size_t len)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
int status;
BUILDER_LOCK
status = rtcpbuilder.SetLocalLocation(s, len);
BUILDER_UNLOCK
return status;
2018-02-26 19:35:16 -05:00
}
2018-02-27 17:05:46 -05:00
int RTPSession::SetLocalPhone(const void *s, size_t len)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
int status;
BUILDER_LOCK
status = rtcpbuilder.SetLocalPhone(s, len);
BUILDER_UNLOCK
return status;
2018-02-26 19:35:16 -05:00
}
2018-02-27 17:05:46 -05:00
int RTPSession::SetLocalTool(const void *s, size_t len)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
int status;
BUILDER_LOCK
status = rtcpbuilder.SetLocalTool(s, len);
BUILDER_UNLOCK
return status;
2018-02-26 19:35:16 -05:00
}
2018-02-27 17:05:46 -05:00
int RTPSession::SetLocalNote(const void *s, size_t len)
2018-02-26 19:35:16 -05:00
{
2018-02-27 17:05:46 -05:00
if (!created)
return ERR_RTP_SESSION_NOTCREATED;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
int status;
BUILDER_LOCK
status = rtcpbuilder.SetLocalNote(s, len);
BUILDER_UNLOCK
return status;
2018-02-26 19:35:16 -05:00
}
int RTPSession::ProcessPolledData()
{
2018-02-27 17:05:46 -05:00
RTPRawPacket *rawpack;
int status;
SOURCES_LOCK
while ((rawpack = rtptrans->GetNextPacket()) != 0)
{
if (m_changeIncomingData)
{
// Provide a way to change incoming data, for decryption for example
if (!OnChangeIncomingData(rawpack))
{
delete rawpack;
continue;
}
}
sources.ClearOwnCollisionFlag();
// since our sources instance also uses the scheduler (analysis of incoming packets)
// we'll lock it
SCHED_LOCK
if ((status = sources.ProcessRawPacket(rawpack, rtptrans, acceptownpackets)) < 0)
{
SCHED_UNLOCK
SOURCES_UNLOCK
delete rawpack;
return status;
}
SCHED_UNLOCK
if (sources.DetectedOwnCollision()) // collision handling!
{
bool created;
if ((status = collisionlist.UpdateAddress(rawpack->GetSenderAddress(), rawpack->GetReceiveTime(), &created)) < 0)
{
SOURCES_UNLOCK
delete rawpack;
return status;
}
if (created) // first time we've encountered this address, send bye packet and
{ // change our own SSRC
PACKSENT_LOCK
bool hassentpackets = sentpackets;
PACKSENT_UNLOCK
if (hassentpackets)
{
// Only send BYE packet if we've actually sent data using this
// SSRC
RTCPCompoundPacket *rtcpcomppack;
BUILDER_LOCK
if ((status = rtcpbuilder.BuildBYEPacket(&rtcpcomppack, 0, 0, useSR_BYEifpossible)) < 0)
{
BUILDER_UNLOCK
SOURCES_UNLOCK
delete rawpack;
return status;
}
BUILDER_UNLOCK
byepackets.push_back(rtcpcomppack);
if (byepackets.size() == 1) // was the first packet, schedule a BYE packet (otherwise there's already one scheduled)
{
SCHED_LOCK
rtcpsched.ScheduleBYEPacket(rtcpcomppack->GetCompoundPacketLength());
SCHED_UNLOCK
}
}
// bye packet is built and scheduled, now change our SSRC
// and reset the packet count in the transmitter
BUILDER_LOCK
uint32_t newssrc = packetbuilder.CreateNewSSRC(sources);
BUILDER_UNLOCK
PACKSENT_LOCK
sentpackets = false;
PACKSENT_UNLOCK
2018-02-26 19:35:16 -05:00
// remove old entry in source table and add new one
2018-02-27 17:05:46 -05:00
if ((status = sources.DeleteOwnSSRC()) < 0)
{
SOURCES_UNLOCK
delete rawpack;
return status;
}
if ((status = sources.CreateOwnSSRC(newssrc)) < 0)
{
SOURCES_UNLOCK
delete rawpack;
return status;
}
}
}
delete rawpack;
}
SCHED_LOCK
RTPTime d = rtcpsched.CalculateDeterministicInterval(false);
SCHED_UNLOCK
RTPTime t = RTPTime::CurrentTime();
double Td = d.GetDouble();
RTPTime sendertimeout = RTPTime(Td * sendermultiplier);
RTPTime generaltimeout = RTPTime(Td * membermultiplier);
RTPTime byetimeout = RTPTime(Td * byemultiplier);
RTPTime colltimeout = RTPTime(Td * collisionmultiplier);
RTPTime notetimeout = RTPTime(Td * notemultiplier);
sources.MultipleTimeouts(t, sendertimeout, byetimeout, generaltimeout, notetimeout);
collisionlist.Timeout(t, colltimeout);
// We'll check if it's time for RTCP stuff
SCHED_LOCK
bool istime = rtcpsched.IsTime();
SCHED_UNLOCK
if (istime)
{
RTCPCompoundPacket *pack;
// we'll check if there's a bye packet to send, or just a normal packet
if (byepackets.empty())
{
BUILDER_LOCK
if ((status = rtcpbuilder.BuildNextPacket(&pack)) < 0)
{
BUILDER_UNLOCK
SOURCES_UNLOCK
return status;
}
BUILDER_UNLOCK
if ((status = SendRTCPData(pack->GetCompoundPacketData(), pack->GetCompoundPacketLength())) < 0)
{
SOURCES_UNLOCK
delete pack;
return status;
}
PACKSENT_LOCK
sentpackets = true;
PACKSENT_UNLOCK
OnSendRTCPCompoundPacket(pack); // we'll place this after the actual send to avoid tampering
}
else
{
pack = *(byepackets.begin());
byepackets.pop_front();
if ((status = SendRTCPData(pack->GetCompoundPacketData(), pack->GetCompoundPacketLength())) < 0)
{
SOURCES_UNLOCK
delete pack;
return status;
}
PACKSENT_LOCK
sentpackets = true;
PACKSENT_UNLOCK
OnSendRTCPCompoundPacket(pack); // we'll place this after the actual send to avoid tampering
if (!byepackets.empty()) // more bye packets to send, schedule them
{
SCHED_LOCK
rtcpsched.ScheduleBYEPacket((*(byepackets.begin()))->GetCompoundPacketLength());
SCHED_UNLOCK
}
}
SCHED_LOCK
rtcpsched.AnalyseOutgoing(*pack);
SCHED_UNLOCK
delete pack;
}
SOURCES_UNLOCK
return 0;
}
int RTPSession::CreateCNAME(uint8_t *buffer, size_t *bufferlength, bool resolve)
2018-02-26 19:35:16 -05:00
{
#ifndef WIN32
2018-02-27 17:05:46 -05:00
bool gotlogin = true;
2018-02-26 19:35:16 -05:00
#ifdef RTP_SUPPORT_GETLOGINR
2018-02-27 17:05:46 -05:00
buffer[0] = 0;
if (getlogin_r((char *) buffer, *bufferlength) != 0)
gotlogin = false;
else
{
if (buffer[0] == 0)
gotlogin = false;
}
if (!gotlogin) // try regular getlogin
{
char *loginname = getlogin();
if (loginname == 0)
gotlogin = false;
else
strncpy((char *) buffer, loginname, *bufferlength);
}
2018-02-26 19:35:16 -05:00
#else
2018-02-27 17:05:46 -05:00
char *loginname = getlogin();
if (loginname == 0)
gotlogin = false;
else
strncpy((char *)buffer,loginname,*bufferlength);
2018-02-26 19:35:16 -05:00
#endif // RTP_SUPPORT_GETLOGINR
2018-02-27 17:05:46 -05:00
if (!gotlogin)
{
char *logname = getenv("LOGNAME");
if (logname == 0)
return ERR_RTP_SESSION_CANTGETLOGINNAME;
strncpy((char *) buffer, logname, *bufferlength);
}
2018-02-26 19:35:16 -05:00
#else // Win32 version
#ifndef _WIN32_WCE
2018-02-27 17:05:46 -05:00
DWORD len = *bufferlength;
if (!GetUserName((LPTSTR)buffer,&len))
RTP_STRNCPY((char *)buffer,"unknown",*bufferlength);
2018-02-26 19:35:16 -05:00
#else
2018-02-27 17:05:46 -05:00
RTP_STRNCPY((char *)buffer,"unknown",*bufferlength);
2018-02-26 19:35:16 -05:00
#endif // _WIN32_WCE
#endif // WIN32
2018-02-27 17:05:46 -05:00
buffer[*bufferlength - 1] = 0;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
size_t offset = strlen((const char *) buffer);
if (offset < (*bufferlength - 1))
buffer[offset] = (uint8_t) '@';
offset++;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
size_t buflen2 = *bufferlength - offset;
int status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (resolve)
{
if ((status = rtptrans->GetLocalHostName(buffer + offset, &buflen2)) < 0)
return status;
*bufferlength = buflen2 + offset;
}
else
{
char hostname[1024];
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
RTP_STRNCPY(hostname, "localhost", 1024); // just in case gethostname fails
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
gethostname(hostname, 1024);
RTP_STRNCPY((char * )(buffer + offset), hostname, buflen2);
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
*bufferlength = offset + strlen(hostname);
}
if (*bufferlength > RTCP_SDES_MAXITEMLENGTH)
*bufferlength = RTCP_SDES_MAXITEMLENGTH;
return 0;
2018-02-26 19:35:16 -05:00
}
RTPRandom *RTPSession::GetRandomNumberGenerator(RTPRandom *r)
{
2018-02-27 17:05:46 -05:00
RTPRandom *rnew = 0;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (r == 0)
{
rnew = RTPRandom::CreateDefaultRandomNumberGenerator();
deletertprnd = true;
}
else
{
rnew = r;
deletertprnd = false;
}
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
return rnew;
2018-02-26 19:35:16 -05:00
}
int RTPSession::SendRTPData(const void *data, size_t len)
{
2018-02-27 17:05:46 -05:00
if (!m_changeOutgoingData)
return rtptrans->SendRTPData(data, len);
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
void *pSendData = 0;
size_t sendLen = 0;
int status = 0;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
status = OnChangeRTPOrRTCPData(data, len, true, &pSendData, &sendLen);
if (status < 0)
return status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (pSendData)
{
status = rtptrans->SendRTPData(pSendData, sendLen);
OnSentRTPOrRTCPData(pSendData, sendLen, true);
}
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
return status;
2018-02-26 19:35:16 -05:00
}
int RTPSession::SendRTCPData(const void *data, size_t len)
{
2018-02-27 17:05:46 -05:00
if (!m_changeOutgoingData)
return rtptrans->SendRTCPData(data, len);
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
void *pSendData = 0;
size_t sendLen = 0;
int status = 0;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
status = OnChangeRTPOrRTCPData(data, len, false, &pSendData, &sendLen);
if (status < 0)
return status;
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
if (pSendData)
{
status = rtptrans->SendRTCPData(pSendData, sendLen);
OnSentRTPOrRTCPData(pSendData, sendLen, false);
}
2018-02-26 19:35:16 -05:00
2018-02-27 17:05:46 -05:00
return status;
2018-02-26 19:35:16 -05:00
}
} // end namespace