⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtpgsttransmitter.cpp

📁 jrtplib3.3.0 最新下载的jrtplib. 我现在正在用jrtplib2.7版本
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*  This class allows for jrtp to send GstBuffers. Allows for integration of RTP   into gstreamer.  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 "rtpgsttransmitter.h"#ifdef RTP_SUPPORT_GST#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"//GST_DEBUG_CATEGORY (gst_rtp_bin_debug);//#define GST_CAT_DEFAULT (gst_rtp_bin_debug)#define RTPGSTv4TRANS_RTPRECEIVEBUFFER							32768#define RTPGSTv4TRANS_RTCPRECEIVEBUFFER							32768#define RTPGSTv4TRANS_RTPTRANSMITBUFFER							32768#define RTPGSTv4TRANS_RTCPTRANSMITBUFFER						32768#define RTPGSTv4TRANS_MAXPACKSIZE							65535#define RTPGSTv4TRANS_IFREQBUFSIZE							8192//#define RTPGSTv4TRANS_IS_MCASTADDR(x)							(((x)&0xF0000000) == 0xE0000000)/*#define RTPGSTv4TRANS_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 RTPGSTv4Trans_GetHashIndex_IPv4Dest(const RTPIPv4Destination &d)				{ return d.GetIP_HBO()%RTPGSTv4TRANS_HASHSIZE; }	int RTPGSTv4Trans_GetHashIndex_u_int32_t(const u_int32_t &k)					{ return k%RTPGSTv4TRANS_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_THREADRTPGSTv4Transmitter::RTPGSTv4Transmitter(){	created = false;	init = false;}RTPGSTv4Transmitter::~RTPGSTv4Transmitter(){	Destroy();}int RTPGSTv4Transmitter::Init(bool tsafe){	if (init)		return ERR_RTP_GSTV4TRANS_ALREADYINIT;	#ifdef RTP_SUPPORT_THREAD	threadsafe = tsafe;	if (threadsafe)	{		int status;				status = mainmutex.Init();		if (status < 0)			return ERR_RTP_GSTV4TRANS_CANTINITMUTEX;		status = waitmutex.Init();		if (status < 0)			return ERR_RTP_GSTV4TRANS_CANTINITMUTEX;	}#else	if (tsafe)		return ERR_RTP_NOTHREADSUPPORT;#endif // RTP_SUPPORT_THREAD	init = true;	return 0;}int RTPGSTv4Transmitter::Create(size_t maximumpacketsize,const RTPTransmissionParams *transparams){	struct sockaddr_in addr;	int status;	if (!init)		return ERR_RTP_GSTV4TRANS_NOTINIT;		MAINMUTEX_LOCK	if (created)	{		MAINMUTEX_UNLOCK		return ERR_RTP_GSTV4TRANS_ALREADYCREATED;	}		// Obtain transmission parameters		if (transparams == 0)		params = new RTPGSTv4TransmissionParams;	else	{		if (transparams->GetTransmissionProtocol() != RTPTransmitter::IPv4GSTProto)			return ERR_RTP_GSTV4TRANS_ILLEGALPARAMETERS;		params = (RTPGSTv4TransmissionParams *)transparams;	}	// Check if portbase is even	//if (params->GetPortbase()%2 != 0)	//{	//	MAINMUTEX_UNLOCK	//	return ERR_RTP_GSTV4TRANS_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<u_int32_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 > RTPGSTv4TRANS_MAXPACKSIZE)	{		MAINMUTEX_UNLOCK		return ERR_RTP_GSTV4TRANS_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 RTPGSTv4Transmitter::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 *RTPGSTv4Transmitter::GetTransmissionInfo(){	if (!init)		return 0;	MAINMUTEX_LOCK	RTPTransmissionInfo *tinf = new RTPGSTv4TransmissionInfo(localIPs,             params->GetGstRTPSrc(), params->GetGstRTPSrc(), params);	MAINMUTEX_UNLOCK	return tinf;}int RTPGSTv4Transmitter::GetLocalHostName(u_int8_t *buffer,size_t *bufferlength){	if (!init)		return ERR_RTP_GSTV4TRANS_NOTINIT;	MAINMUTEX_LOCK	if (!created)	{		MAINMUTEX_UNLOCK		return ERR_RTP_GSTV4TRANS_NOTCREATED;	}	if (localhostname == 0)	{		if (localIPs.empty())		{			MAINMUTEX_UNLOCK			return ERR_RTP_GSTV4TRANS_NOLOCALIPS;		}				std::list<u_int32_t>::const_iterator it;		std::list<std::string> hostnames;			for (it = localIPs.begin() ; it != localIPs.end() ; it++)		{			struct hostent *he;			u_int8_t addr[4];			u_int32_t ip = (*it);				addr[0] = (u_int8_t)((ip>>24)&0xFF);			addr[1] = (u_int8_t)((ip>>16)&0xFF);			addr[2] = (u_int8_t)((ip>>8)&0xFF);			addr[3] = (u_int8_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 u_int8_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		{			u_int32_t ip;			int len;			char str[256];						it = localIPs.begin();			ip = (*it);						sprintf(str,"%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 u_int8_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 RTPGSTv4Transmitter::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<u_int32_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 RTPGSTv4Transmitter::Poll(){	if (!init)		return ERR_RTP_GSTV4TRANS_NOTINIT;	int status;		MAINMUTEX_LOCK	if (!created)	{		MAINMUTEX_UNLOCK		return ERR_RTP_GSTV4TRANS_NOTCREATED;	}	status = FakePoll(); // poll RTP socket    params->SetCurrentData(NULL);	MAINMUTEX_UNLOCK	return status;}int RTPGSTv4Transmitter::WaitForIncomingData(const RTPTime &delay,bool *dataavailable){/*	if (!init)		return ERR_RTP_GSTV4TRANS_NOTINIT;		MAINMUTEX_LOCK		fd_set fdset;	struct timeval tv;		if (!created)	{		MAINMUTEX_UNLOCK		return ERR_RTP_GSTV4TRANS_NOTCREATED;	}	if (waitingfordata)	{		MAINMUTEX_UNLOCK		return ERR_RTP_GSTV4TRANS_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_GSTV4TRANS_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_GSTV4TRANS_WAITNOTIMPLEMENTED;}int RTPGSTv4Transmitter::AbortWait(){/*	if (!init)		return ERR_RTP_GSTV4TRANS_NOTINIT;		MAINMUTEX_LOCK	if (!created)	{		MAINMUTEX_UNLOCK		return ERR_RTP_GSTV4TRANS_NOTCREATED;	}	if (!waitingfordata)	{		MAINMUTEX_UNLOCK		return ERR_RTP_GSTV4TRANS_NOTWAITING;	}	AbortWaitInternal();		MAINMUTEX_UNLOCK	return 0;*/	return 0;}int RTPGSTv4Transmitter::SendRTPData(const void *data,size_t len)	{	if (!init)		return ERR_RTP_GSTV4TRANS_NOTINIT;	MAINMUTEX_LOCK	    GstNetBuffer *out_buf;		if (!created)	{		MAINMUTEX_UNLOCK		return ERR_RTP_GSTV4TRANS_NOTCREATED;	}	if (len > maxpacksize)	{		MAINMUTEX_UNLOCK		return ERR_RTP_GSTV4TRANS_SPECIFIEDSIZETOOBIG;	}	destinations.GotoFirstElement();    // send to each destination	while (destinations.HasCurrentElement())	{        // create GstNetBuffer from data        out_buf = gst_netbuffer_new ();        GST_BUFFER_DATA (out_buf) = (guint8*)data;        GST_BUFFER_SIZE (out_buf) = len;        gst_netaddress_set_ip4_address (&out_buf->to, destinations.GetCurrentElement().GetIP_NBO(),                destinations.GetCurrentElement().GetRTPPort_NBO());        GST_DEBUG("Sending RTP packet to ip %d port %d", destinations.GetCurrentElement().GetIP_NBO(),                 destinations.GetCurrentElement().GetRTPPort_NBO());        // push data        gst_pad_push( params->GetGstRTPSrc(), GST_BUFFER(out_buf) );		destinations.GotoNextElement();	}		rtppackcount++;	MAINMUTEX_UNLOCK	return 0;}int RTPGSTv4Transmitter::SendRTCPData(const void *data,size_t len){	if (!init)		return ERR_RTP_GSTV4TRANS_NOTINIT;    // if it's NULL we dont want to transmit RTCPs    if (params->GetGstRTCPSrc() == NULL)    {        return 0;    }    	MAINMUTEX_LOCK    GstNetBuffer *out_buf;		if (!created)	{		MAINMUTEX_UNLOCK		return ERR_RTP_GSTV4TRANS_NOTCREATED;	}	if (len > maxpacksize)	{		MAINMUTEX_UNLOCK

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -