📄 rtpsession.cpp
字号:
/* This file is a part of JRTPLIB Copyright (c) 1999-2006 Jori Liesenborgs Contact: jori@lumumba.uhasselt.be This library was developed at the "Expertisecentrum Digitale 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). 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: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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.*/#include "rtpsession.h"#include "rtperrors.h"#include "rtppollthread.h"#include "rtpudpv4transmitter.h"#include "rtpudpv6transmitter.h"#include "rtpsessionparams.h"#include "rtpdefines.h"#include "rtprawpacket.h"#include "rtppacket.h"#include "rtptimeutilities.h"#include "rtcpcompoundpacket.h"#ifndef WIN32 #include <unistd.h> #include <stdlib.h>#else #include <winbase.h>#endif // WIN32#ifdef RTPDEBUG #include <iostream>#endif // RTPDEBUG#include "rtpdebug.h"#ifdef RTP_SUPPORT_THREAD #define SOURCES_LOCK { if (usingpollthread) sourcesmutex.Lock(); } #define SOURCES_UNLOCK { if (usingpollthread) sourcesmutex.Unlock(); } #define BUILDER_LOCK { if (usingpollthread) buildermutex.Lock(); } #define BUILDER_UNLOCK { if (usingpollthread) buildermutex.Unlock(); } #define SCHED_LOCK { if (usingpollthread) schedmutex.Lock(); } #define SCHED_UNLOCK { if (usingpollthread) schedmutex.Unlock(); }#else #define SOURCES_LOCK #define SOURCES_UNLOCK #define BUILDER_LOCK #define BUILDER_UNLOCK #define SCHED_LOCK #define SCHED_UNLOCK#endif // RTP_SUPPORT_THREADRTPSession::RTPSession(RTPTransmitter::TransmissionProtocol proto /* = RTPTransmitter::IPv4UDPProto */ ) : protocol(proto),sources(*this),rtcpsched(sources),rtcpbuilder(sources,packetbuilder){ created = false;#if (defined(WIN32) || defined(_WIN32_WCE)) timeinit.Dummy();#endif // WIN32 || _WIN32_WCE}RTPSession::~RTPSession(){ Destroy();}int RTPSession::Create(const RTPSessionParams &sessparams,const RTPTransmissionParams *transparams /* = 0 */){ int status; if (created) return ERR_RTP_SESSION_ALREADYCREATED; usingpollthread = sessparams.IsUsingPollThread(); useSR_BYEifpossible = sessparams.GetSenderReportForBYE(); // Check max packet size if ((maxpacksize = sessparams.GetMaximumPacketSize()) < RTP_MINPACKETSIZE) return ERR_RTP_SESSION_MAXPACKETSIZETOOSMALL; // Initialize the transmission component rtptrans = 0; switch(protocol) { case RTPTransmitter::IPv4UDPProto: rtptrans = new RTPUDPv4Transmitter(); break;#ifdef RTP_SUPPORT_IPV6 case RTPTransmitter::IPv6UDPProto: rtptrans = new RTPUDPv6Transmitter(); break;#endif // RTP_SUPPORT_IPV6 case RTPTransmitter::UserDefinedProto: rtptrans = NewUserDefinedTransmitter(); if (rtptrans == 0) return ERR_RTP_SESSION_USERDEFINEDTRANSMITTERNULL; break; default: return ERR_RTP_SESSION_UNSUPPORTEDTRANSMISSIONPROTOCOL; } if (rtptrans == 0) return ERR_RTP_OUTOFMEM; if ((status = rtptrans->Init(usingpollthread)) < 0) { delete rtptrans; return status; } if ((status = rtptrans->Create(maxpacksize,transparams)) < 0) { delete rtptrans; return status; } // Initialize packet builder if ((status = packetbuilder.Init(maxpacksize)) < 0) { delete rtptrans; return status; }#ifdef RTP_SUPPORT_PROBATION // Set probation type sources.SetProbationType(sessparams.GetProbationType());#endif // RTP_SUPPORT_PROBATION // Add our own ssrc to the source table if ((status = sources.CreateOwnSSRC(packetbuilder.GetSSRC())) < 0) { packetbuilder.Destroy(); delete rtptrans; return status; } // Set the initial receive mode if ((status = rtptrans->SetReceiveMode(sessparams.GetReceiveMode())) < 0) { packetbuilder.Destroy(); sources.Clear(); delete rtptrans; return status; } // Init the RTCP packet builder double timestampunit = sessparams.GetOwnTimestampUnit(); uint8_t buf[1024]; size_t buflen = 1024; if ((status = CreateCNAME(buf,&buflen,sessparams.GetResolveLocalHostname())) < 0) { packetbuilder.Destroy(); sources.Clear(); delete rtptrans; return status; } if ((status = rtcpbuilder.Init(maxpacksize,timestampunit,buf,buflen)) < 0) { packetbuilder.Destroy(); sources.Clear(); 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) { delete rtptrans; packetbuilder.Destroy(); sources.Clear(); rtcpbuilder.Destroy(); return status; } if ((status = schedparams.SetSenderBandwidthFraction(sessparams.GetSenderControlBandwidthFraction())) < 0) { delete rtptrans; packetbuilder.Destroy(); sources.Clear(); rtcpbuilder.Destroy(); return status; } if ((status = schedparams.SetMinimumTransmissionInterval(sessparams.GetMinimumRTCPTransmissionInterval())) < 0) { 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 #ifdef RTP_SUPPORT_THREAD pollthread = 0; if (usingpollthread) { if (!sourcesmutex.IsInitialized()) { if (sourcesmutex.Init() < 0) { delete rtptrans; packetbuilder.Destroy(); sources.Clear(); rtcpbuilder.Destroy(); return ERR_RTP_SESSION_CANTINITMUTEX; } } if (!buildermutex.IsInitialized()) { if (buildermutex.Init() < 0) { delete rtptrans; packetbuilder.Destroy(); sources.Clear(); rtcpbuilder.Destroy(); return ERR_RTP_SESSION_CANTINITMUTEX; } } if (!schedmutex.IsInitialized()) { if (schedmutex.Init() < 0) { delete rtptrans; packetbuilder.Destroy(); sources.Clear(); rtcpbuilder.Destroy(); return ERR_RTP_SESSION_CANTINITMUTEX; } } pollthread = new RTPPollThread(*this,rtcpsched); if (pollthread == 0) { delete rtptrans; packetbuilder.Destroy(); sources.Clear(); rtcpbuilder.Destroy(); return ERR_RTP_OUTOFMEM; } if ((status = pollthread->Start(rtptrans)) < 0) { delete rtptrans; delete pollthread; packetbuilder.Destroy(); sources.Clear(); rtcpbuilder.Destroy(); return status; } }#endif // RTP_SUPPORT_THREAD created = true; return 0;}void RTPSession::Destroy(){ if (!created) return;#ifdef RTP_SUPPORT_THREAD if (pollthread) delete pollthread;#endif // RTP_SUPPORT_THREAD delete rtptrans; packetbuilder.Destroy(); rtcpbuilder.Destroy(); rtcpsched.Reset(); collisionlist.Clear(); sources.Clear(); std::list<RTCPCompoundPacket *>::const_iterator it; for (it = byepackets.begin() ; it != byepackets.end() ; it++) delete (*it); byepackets.clear(); created = false;}void RTPSession::BYEDestroy(const RTPTime &maxwaittime,const void *reason,size_t reasonlength){ if (!created) return; // first, stop the thread so we have full control over all components #ifdef RTP_SUPPORT_THREAD if (pollthread) delete pollthread;#endif // RTP_SUPPORT_THREAD RTPTime stoptime = RTPTime::CurrentTime(); stoptime += maxwaittime; // add bye packet to the list if we've sent data RTCPCompoundPacket *pack; if (rtptrans->GetNumRTPPacketsSent() != 0 || rtptrans->GetNumRTCPPacketsSent() != 0) { int status; reasonlength = (reasonlength>RTCP_BYE_MAXREASONLENGTH)?RTCP_BYE_MAXREASONLENGTH:reasonlength; status = rtcpbuilder.BuildBYEPacket(&pack,reason,reasonlength,useSR_BYEifpossible); if (status >= 0) { byepackets.push_back(pack); if (byepackets.size() == 1) rtcpsched.ScheduleBYEPacket(pack->GetCompoundPacketLength()); } } if (!byepackets.empty()) { bool done = false; while (!done) { RTPTime curtime = RTPTime::CurrentTime(); if (curtime >= stoptime) done = true; if (rtcpsched.IsTime()) { pack = *(byepackets.begin()); byepackets.pop_front(); rtptrans->SendRTCPData(pack->GetCompoundPacketData(),pack->GetCompoundPacketLength()); 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)); } } delete rtptrans; packetbuilder.Destroy(); rtcpbuilder.Destroy(); rtcpsched.Reset(); collisionlist.Clear(); sources.Clear(); // clear rest of bye packets std::list<RTCPCompoundPacket *>::const_iterator it; for (it = byepackets.begin() ; it != byepackets.end() ; it++) delete (*it); byepackets.clear(); created = false;}bool RTPSession::IsActive(){ return created;}uint32_t RTPSession::GetLocalSSRC(){ if (!created) return 0; uint32_t ssrc; BUILDER_LOCK ssrc = packetbuilder.GetSSRC(); BUILDER_UNLOCK return ssrc;}int RTPSession::AddDestination(const RTPAddress &addr){ if (!created) return ERR_RTP_SESSION_NOTCREATED; return rtptrans->AddDestination(addr);}int RTPSession::DeleteDestination(const RTPAddress &addr){ if (!created) return ERR_RTP_SESSION_NOTCREATED; return rtptrans->DeleteDestination(addr);}void RTPSession::ClearDestinations(){ if (!created) return; rtptrans->ClearDestinations();}bool RTPSession::SupportsMulticasting(){ if (!created) return false; return rtptrans->SupportsMulticasting();}int RTPSession::JoinMulticastGroup(const RTPAddress &addr){ if (!created) return ERR_RTP_SESSION_NOTCREATED; return rtptrans->JoinMulticastGroup(addr);}int RTPSession::LeaveMulticastGroup(const RTPAddress &addr){ if (!created) return ERR_RTP_SESSION_NOTCREATED; return rtptrans->LeaveMulticastGroup(addr);}void RTPSession::LeaveAllMulticastGroups(){ 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 = rtptrans->SendRTPData(packetbuilder.GetPacket(),packetbuilder.GetPacketLength())) < 0) { BUILDER_UNLOCK return status; } BUILDER_UNLOCK SOURCES_LOCK sources.SentRTPPacket(); SOURCES_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 = rtptrans->SendRTPData(packetbuilder.GetPacket(),packetbuilder.GetPacketLength())) < 0) { BUILDER_UNLOCK return status; } BUILDER_UNLOCK SOURCES_LOCK sources.SentRTPPacket(); SOURCES_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 = rtptrans->SendRTPData(packetbuilder.GetPacket(),packetbuilder.GetPacketLength())) < 0) { BUILDER_UNLOCK return status; } BUILDER_UNLOCK SOURCES_LOCK sources.SentRTPPacket(); SOURCES_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 = rtptrans->SendRTPData(packetbuilder.GetPacket(),packetbuilder.GetPacketLength())) < 0) { BUILDER_UNLOCK return status; } BUILDER_UNLOCK SOURCES_LOCK sources.SentRTPPacket(); SOURCES_UNLOCK return 0;}int RTPSession::SetDefaultPayloadType(uint8_t pt){ if (!created) return ERR_RTP_SESSION_NOTCREATED; int status; BUILDER_LOCK status = packetbuilder.SetDefaultPayloadType(pt); BUILDER_UNLOCK return status;}int RTPSession::SetDefaultMark(bool m){ if (!created) return ERR_RTP_SESSION_NOTCREATED; int status; BUILDER_LOCK status = packetbuilder.SetDefaultMark(m); BUILDER_UNLOCK return status;}int RTPSession::SetDefaultTimestampIncrement(uint32_t timestampinc){ if (!created) return ERR_RTP_SESSION_NOTCREATED; int status; BUILDER_LOCK status = packetbuilder.SetDefaultTimestampIncrement(timestampinc); BUILDER_UNLOCK return status;}int RTPSession::IncrementTimestamp(uint32_t inc){ if (!created) return ERR_RTP_SESSION_NOTCREATED; int status; BUILDER_LOCK status = packetbuilder.IncrementTimestamp(inc); BUILDER_UNLOCK return status;}int RTPSession::IncrementTimestampDefault(){ if (!created) return ERR_RTP_SESSION_NOTCREATED; int status; BUILDER_LOCK status = packetbuilder.IncrementTimestampDefault();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -