📄 rtpfaketransmitter.cpp
字号:
/* This class allows for jrtp to process packets without sending them out anywhere. The incoming messages are handed in to jrtp through the TransmissionParams and can be retreived from jrtp through the normal polling mecanisms. The outgoing RTP/RTCP packets are given to jrtp through the normal session->SendPacket() and those packets are handed back out to the client through a callback function (packet_ready_cb). example usage : Allows for integration of RTP into gstreamer. THIS HAS NOT BEEN TESTED WITH THREADS SO DON'T TRY Copyright (c) 2005 Philippe Khalaf <burger@speedy.org> This file is a part of JRTPLIB Copyright (c) 1999-2004 Jori Liesenborgs Contact: jori@lumumba.luc.ac.be This library was developed at the "Expertisecentrum Digitale Media" (http://www.edm.luc.ac.be), a research center of the "Limburgs Universitair Centrum" (http://www.luc.ac.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 "rtpfaketransmitter.h"#include "rtprawpacket.h"#include "rtpipv4address.h"#include "rtptimeutilities.h"#include <stdio.h>#include <net/if.h>#include <string.h>#include <netdb.h>#include <unistd.h>#ifdef RTP_HAVE_SYS_FILIO#include <sys/filio.h>#endif // RTP_HAVE_SYS_FILIO#define RTPIOCTL ioctl#ifdef RTPDEBUG #include <iostream>#endif // RTPDEBUG#include "rtpdebug.h"#define RTPFAKETRANS_MAXPACKSIZE 65535#define RTPFAKETRANS_IFREQBUFSIZE 8192//#define RTPFAKETRANS_IS_MCASTADDR(x) (((x)&0xF0000000) == 0xE0000000)/*#define RTPFAKETRANS_MCASTMEMBERSHIP(socket,type,mcastip,status) {\ struct ip_mreq mreq;\ \ mreq.imr_multiaddr.s_addr = htonl(mcastip);\ mreq.imr_interface.s_addr = htonl(bindIP);\ status = setsockopt(socket,IPPROTO_IP,type,(const char *)&mreq,sizeof(struct ip_mreq));\ }*/#ifndef RTP_SUPPORT_INLINETEMPLATEPARAM int RTPFakeTrans_GetHashIndex_IPv4Dest(const RTPIPv4Destination &d) { return d.GetIP_HBO()%RTPFAKETRANS_HASHSIZE; } int RTPFakeTrans_GetHashIndex_uint32_t(const uint32_t &k) { return k%RTPFAKETRANS_HASHSIZE; }#endif // !RTP_SUPPORT_INLINETEMPLATEPARAM #ifdef RTP_SUPPORT_THREAD #define MAINMUTEX_LOCK { if (threadsafe) mainmutex.Lock(); } #define MAINMUTEX_UNLOCK { if (threadsafe) mainmutex.Unlock(); } #define WAITMUTEX_LOCK { if (threadsafe) waitmutex.Lock(); } #define WAITMUTEX_UNLOCK { if (threadsafe) waitmutex.Unlock(); }#else #define MAINMUTEX_LOCK #define MAINMUTEX_UNLOCK #define WAITMUTEX_LOCK #define WAITMUTEX_UNLOCK#endif // RTP_SUPPORT_THREADRTPFakeTransmitter::RTPFakeTransmitter(){ created = false; init = false;}RTPFakeTransmitter::~RTPFakeTransmitter(){ Destroy();}int RTPFakeTransmitter::Init(bool tsafe){ if (init) return ERR_RTP_FAKETRANS_ALREADYINIT; // bomb out if trying to use threads if (tsafe) return ERR_RTP_NOTHREADSUPPORT; #ifdef RTP_SUPPORT_THREAD threadsafe = tsafe; if (threadsafe) { int status; status = mainmutex.Init(); if (status < 0) return ERR_RTP_FAKETRANS_CANTINITMUTEX; status = waitmutex.Init(); if (status < 0) return ERR_RTP_FAKETRANS_CANTINITMUTEX; }#else if (tsafe) return ERR_RTP_NOTHREADSUPPORT;#endif // RTP_SUPPORT_THREAD init = true; return 0;}int RTPFakeTransmitter::Create(size_t maximumpacketsize,const RTPTransmissionParams *transparams){ struct sockaddr_in addr; int status; if (!init) return ERR_RTP_FAKETRANS_NOTINIT; MAINMUTEX_LOCK if (created) { MAINMUTEX_UNLOCK return ERR_RTP_FAKETRANS_ALREADYCREATED; } // Obtain transmission parameters if (transparams == 0) params = new RTPFakeTransmissionParams; else { if (transparams->GetTransmissionProtocol() != RTPTransmitter::UserDefinedProto) return ERR_RTP_FAKETRANS_ILLEGALPARAMETERS; params = (RTPFakeTransmissionParams *)transparams; } // Check if portbase is even //if (params->GetPortbase()%2 != 0) //{ // MAINMUTEX_UNLOCK // return ERR_RTP_FAKETRANS_PORTBASENOTEVEN; //} // Try to obtain local IP addresses localIPs = params->GetLocalIPList(); if (localIPs.empty()) // User did not provide list of local IP addresses, calculate them { int status; if ((status = CreateLocalIPList()) < 0) { MAINMUTEX_UNLOCK return status; }#ifdef RTPDEBUG std::cout << "Found these local IP addresses:" << std::endl; std::list<uint32_t>::const_iterator it; for (it = localIPs.begin() ; it != localIPs.end() ; it++) { RTPIPv4Address a(*it); std::cout << a.GetAddressString() << std::endl; }#endif // RTPDEBUG }//#ifdef RTP_SUPPORT_IPV4MULTICAST// if (SetMulticastTTL(params->GetMulticastTTL()))// supportsmulticasting = true;// else// supportsmulticasting = false;//#else // no multicast support enabled supportsmulticasting = false;//#endif // RTP_SUPPORT_IPV4MULTICAST if (maximumpacketsize > RTPFAKETRANS_MAXPACKSIZE) { MAINMUTEX_UNLOCK return ERR_RTP_FAKETRANS_SPECIFIEDSIZETOOBIG; } maxpacksize = maximumpacketsize; portbase = params->GetPortbase(); multicastTTL = params->GetMulticastTTL(); receivemode = RTPTransmitter::AcceptAll; localhostname = 0; localhostnamelength = 0; rtppackcount = 0; rtcppackcount = 0; waitingfordata = false; created = true; MAINMUTEX_UNLOCK return 0;}void RTPFakeTransmitter::Destroy(){ if (!init) return; MAINMUTEX_LOCK if (!created) { MAINMUTEX_UNLOCK; return; } if (localhostname) { delete [] localhostname; localhostname = 0; localhostnamelength = 0; } destinations.Clear();#ifdef RTP_SUPPORT_IPV4MULTICAST// multicastgroups.Clear();#endif // RTP_SUPPORT_IPV4MULTICAST FlushPackets(); ClearAcceptIgnoreInfo(); localIPs.clear(); created = false; delete params; MAINMUTEX_UNLOCK}RTPTransmissionInfo *RTPFakeTransmitter::GetTransmissionInfo(){ if (!init) return 0; MAINMUTEX_LOCK RTPTransmissionInfo *tinf = new RTPFakeTransmissionInfo(localIPs, params); MAINMUTEX_UNLOCK return tinf;}int RTPFakeTransmitter::GetLocalHostName(uint8_t *buffer,size_t *bufferlength){ if (!init) return ERR_RTP_FAKETRANS_NOTINIT; MAINMUTEX_LOCK if (!created) { MAINMUTEX_UNLOCK return ERR_RTP_FAKETRANS_NOTCREATED; } if (localhostname == 0) { if (localIPs.empty()) { MAINMUTEX_UNLOCK return ERR_RTP_FAKETRANS_NOLOCALIPS; } std::list<uint32_t>::const_iterator it; std::list<std::string> hostnames; for (it = localIPs.begin() ; it != localIPs.end() ; it++) { struct hostent *he; uint8_t addr[4]; uint32_t ip = (*it); addr[0] = (uint8_t)((ip>>24)&0xFF); addr[1] = (uint8_t)((ip>>16)&0xFF); addr[2] = (uint8_t)((ip>>8)&0xFF); addr[3] = (uint8_t)(ip&0xFF); he = gethostbyaddr((char *)addr,4,AF_INET); if (he != 0) { std::string hname = std::string(he->h_name); hostnames.push_back(hname); } } bool found = false; if (!hostnames.empty()) // try to select the most appropriate hostname { std::list<std::string>::const_iterator it; for (it = hostnames.begin() ; !found && it != hostnames.end() ; it++) { if ((*it).find('.') != std::string::npos) { found = true; localhostnamelength = (*it).length(); localhostname = new uint8_t [localhostnamelength+1]; if (localhostname == 0) { MAINMUTEX_UNLOCK return ERR_RTP_OUTOFMEM; } memcpy(localhostname,(*it).c_str(),localhostnamelength); localhostname[localhostnamelength] = 0; } } } if (!found) // use an IP address { uint32_t ip; int len; char str[16]; it = localIPs.begin(); ip = (*it); snprintf(str,16,"%d.%d.%d.%d",(int)((ip>>24)&0xFF),(int)((ip>>16)&0xFF),(int)((ip>>8)&0xFF),(int)(ip&0xFF)); len = strlen(str); localhostnamelength = len; localhostname = new uint8_t [localhostnamelength + 1]; if (localhostname == 0) { MAINMUTEX_UNLOCK return ERR_RTP_OUTOFMEM; } memcpy(localhostname,str,localhostnamelength); localhostname[localhostnamelength] = 0; } } if ((*bufferlength) < localhostnamelength) { *bufferlength = localhostnamelength; // tell the application the required size of the buffer MAINMUTEX_UNLOCK return ERR_RTP_TRANS_BUFFERLENGTHTOOSMALL; } memcpy(buffer,localhostname,localhostnamelength); *bufferlength = localhostnamelength; MAINMUTEX_UNLOCK return 0;}bool RTPFakeTransmitter::ComesFromThisTransmitter(const RTPAddress *addr){ if (!init) return false; if (addr == 0) return false; MAINMUTEX_LOCK bool v; if (created && addr->GetAddressType() == RTPAddress::IPv4Address) { const RTPIPv4Address *addr2 = (const RTPIPv4Address *)addr; bool found = false; std::list<uint32_t>::const_iterator it; it = localIPs.begin(); while (!found && it != localIPs.end()) { if (addr2->GetIP() == *it) found = true; else ++it; } if (!found) v = false; else { if (addr2->GetPort() == params->GetPortbase()) // check for RTP port v = true; else if (addr2->GetPort() == (params->GetPortbase()+1)) // check for RTCP port v = true; else v = false; } } else v = false; MAINMUTEX_UNLOCK return v;}int RTPFakeTransmitter::Poll(){ if (!init) return ERR_RTP_FAKETRANS_NOTINIT; int status; MAINMUTEX_LOCK if (!created) { MAINMUTEX_UNLOCK return ERR_RTP_FAKETRANS_NOTCREATED; } status = FakePoll(); // poll RTP socket params->SetCurrentData(NULL); MAINMUTEX_UNLOCK return status;}int RTPFakeTransmitter::WaitForIncomingData(const RTPTime &delay,bool *dataavailable){/* if (!init) return ERR_RTP_FAKETRANS_NOTINIT; MAINMUTEX_LOCK fd_set fdset; struct timeval tv; if (!created) { MAINMUTEX_UNLOCK return ERR_RTP_FAKETRANS_NOTCREATED; } if (waitingfordata) { MAINMUTEX_UNLOCK return ERR_RTP_FAKETRANS_ALREADYWAITING; } FD_ZERO(&fdset); FD_SET(rtpsock,&fdset); FD_SET(rtcpsock,&fdset); FD_SET(abortdesc[0],&fdset); tv.tv_sec = delay.GetSeconds(); tv.tv_usec = delay.GetMicroSeconds(); waitingfordata = true; WAITMUTEX_LOCK MAINMUTEX_UNLOCK if (select(FD_SETSIZE,&fdset,0,0,&tv) < 0) { MAINMUTEX_LOCK waitingfordata = false; MAINMUTEX_UNLOCK WAITMUTEX_UNLOCK return ERR_RTP_FAKETRANS_ERRORINSELECT; } MAINMUTEX_LOCK waitingfordata = false; if (!created) // destroy called { MAINMUTEX_UNLOCK; WAITMUTEX_UNLOCK return 0; } // if aborted, read from abort buffer if (FD_ISSET(abortdesc[0],&fdset)) {#ifdef WIN32 char buf[1]; recv(abortdesc[0],buf,1,0);#else unsigned char buf[1]; read(abortdesc[0],buf,1);#endif // WIN32 } MAINMUTEX_UNLOCK WAITMUTEX_UNLOCK return 0;*/ return ERR_RTP_FAKETRANS_WAITNOTIMPLEMENTED;}int RTPFakeTransmitter::AbortWait(){/* if (!init) return ERR_RTP_FAKETRANS_NOTINIT; MAINMUTEX_LOCK if (!created) { MAINMUTEX_UNLOCK return ERR_RTP_FAKETRANS_NOTCREATED; } if (!waitingfordata) { MAINMUTEX_UNLOCK return ERR_RTP_FAKETRANS_NOTWAITING; } AbortWaitInternal(); MAINMUTEX_UNLOCK return 0;*/ return 0;}int RTPFakeTransmitter::SendRTPData(const void *data,size_t len) { if (!init) return ERR_RTP_FAKETRANS_NOTINIT; MAINMUTEX_LOCK if (!created) { MAINMUTEX_UNLOCK return ERR_RTP_FAKETRANS_NOTCREATED; } if (len > maxpacksize) { MAINMUTEX_UNLOCK return ERR_RTP_FAKETRANS_SPECIFIEDSIZETOOBIG; } destinations.GotoFirstElement(); // send to each destination while (destinations.HasCurrentElement()) { (*params->GetPacketReadyCB())((uint8_t*)data, len, destinations.GetCurrentElement().GetIP_NBO(), destinations.GetCurrentElement().GetRTPPort_NBO(), 1); destinations.GotoNextElement(); } rtppackcount++; MAINMUTEX_UNLOCK return 0;}int RTPFakeTransmitter::SendRTCPData(const void *data,size_t len){ if (!init) return ERR_RTP_FAKETRANS_NOTINIT; MAINMUTEX_LOCK if (!created) { MAINMUTEX_UNLOCK return ERR_RTP_FAKETRANS_NOTCREATED; } if (len > maxpacksize) { MAINMUTEX_UNLOCK return ERR_RTP_FAKETRANS_SPECIFIEDSIZETOOBIG;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -