rvrtplink.c

来自「h.248协议源码」· C语言 代码 · 共 246 行

C
246
字号
/******************************************************************************
Filename:    rvrtplink.c
Description: rtp termination handling
*******************************************************************************
                Copyright (c) 2001 RADVISION
*******************************************************************************
NOTICE:
This document contains information that is proprietary to RADVISION.
No part of this publication may be reproduced in any form whatsoever 
without written prior approval by RADVISION.

RADVISION reserves the right to revise this publication and make changes
without obligation to notify any person of such revisions or changes.
******************************************************************************/

#include "rvrtplink.h"

#ifdef RV_PERFORMANCETEST_ON
#define RV_MEGACOGATEWAY_DISABLERTP
#endif

static void rtpEventHandler(HRTPSESSION rtpSession, void *data)
{
	RvRtpLink *x = (RvRtpLink *)data;
	char buf[1000];
	rtpParam params;

	if(rtpRead(rtpSession, buf, sizeof(buf), &params) != 0)
		return;

	if(x->mode == RV_MDMSTREAMMODE_LOOPBACK)
	{
		if(x->remoteAddrValid)
			rtpWrite(x->rtpSession, buf, params.len, &params);
	}
	else if(x->mode == RV_MDMSTREAMMODE_SENDRECV || x->mode == RV_MDMSTREAMMODE_RECVONLY)
	{
		RvPtrListIter iter;
		for(iter = rvPtrListBegin(&x->connections);
			iter != rvPtrListEnd(&x->connections);
			iter = rvPtrListIterNext(iter))
		{
			RvRtpLink *link = (RvRtpLink *)rvPtrListIterData(iter);
			if(link->mode == RV_MDMSTREAMMODE_SENDRECV || link->mode == RV_MDMSTREAMMODE_SENDONLY)
			{
				if(link->remoteAddrValid)
					rtpWrite(link->rtpSession, buf, params.len, &params);
			}
		}
	}
}

RvRtpLink *rvRtpLinkConstruct(RvRtpLink *x, rtpEngine *engine)
{
#ifndef RV_MEGACOGATEWAY_DISABLERTP
	x->rtpSession = rtpOpen(0, 0, 0);
#endif
	rvPtrListConstruct(&x->connections, &rvDefaultAlloc);
	x->mode = RV_MDMSTREAMMODE_SENDRECV;
	x->remoteAddrValid = rvFalse;
#ifndef RV_MEGACOGATEWAY_DISABLERTP
	rtpSetEventHandlerEx(x->rtpSession, engine, rtpEventHandler, x);
#endif
	return x;
}

void rvRtpLinkDestruct(RvRtpLink *x)
{
#ifndef RV_MEGACOGATEWAY_DISABLERTP
	rtpClose(x->rtpSession);
#endif
	rvPtrListDestruct(&x->connections);
}

void rvRtpLinkSetRemoteAddress(RvRtpLink *x, const char *ip, RvInetPort port)
{
#ifndef RV_MEGACOGATEWAY_DISABLERTP
	RvSocketAddr remoteAddr;

	rvSocketAddrConstructInetByName(&remoteAddr, ip, port);
	rtpSetRemoteAddress(x->rtpSession, rvIpv4AddrToUint32(rvSocketAddrGetIpv4Addr(&remoteAddr)), port);
	rvSocketAddrDestruct(&remoteAddr);
#endif

	x->remoteAddrValid = port != 0;
}

void rvRtpLinkSetMode(RvRtpLink *x, RvMdmStreamMode mode)
{
	if(mode != RV_MDMSTREAMMODE_NOTSET)
		x->mode = mode;
}

RvInetPort rvRtpLinkGetLocalPort(RvRtpLink *x)
{
#ifdef RV_MEGACOGATEWAY_DISABLERTP
	return 41000;
#else
	return rtpGetPort(x->rtpSession);
#endif
}

void rvRtpLinkConnect(RvRtpLink *a, RvRtpLink *b, RvMdmStreamDirection direction)
{
	if(direction == RV_MDMSTREAMDIRECTION_BOTHWAYS || direction == RV_MDMSTREAMDIRECTION_SOURCE2TARGET)
		rvPtrListPushBack(&a->connections, b);
	if(direction == RV_MDMSTREAMDIRECTION_BOTHWAYS || direction == RV_MDMSTREAMDIRECTION_TARGET2SOURCE)
		rvPtrListPushBack(&b->connections, a);
}

void rvRtpLinkDisconnect(RvRtpLink *a, RvRtpLink *b)
{
	rvPtrListRemove(&a->connections, b);
	rvPtrListRemove(&b->connections, a);
}

const char *rvRtpLinkGetLocalIpAddr(void)
{
	static char localIpAddr[16] = "";
	
	if(*localIpAddr == 0)
	{
		RvHost localHost;
		rvHostConstructLocal(&localHost);
		rvHostGetAddrAsString(&localHost, 0, localIpAddr);
		rvHostDestruct(&localHost);
	}

	return localIpAddr;
}

static RvBool rvSdpMsgIsPayloadSupported(const RvSdpMsg *msg, int payload)
{
	size_t i;
	for(i=0; i<rvSdpMsgGetNumOfMediaDescr(msg); ++i)
	{
		RvSdpMediaDescr *media = rvSdpMsgGetMediaDescr(msg, i);
		size_t j;
		for(j=0; j<rvSdpMediaDescrGetNumOfPayloads(media); ++j)
		{
			if(rvSdpMediaDescrGetPayloadNumber(media, j) == payload)
			{
				return rvTrue;
			}
		}
	}
	return rvFalse;
}

static RvBool rvRtpLinkProcessMedia(RvMdmTerm *term,
	RvMdmMediaStream *media, RvMdmMediaStreamDescr *streamDescr, OUT RvMdmError *mdmError)
{
	RvRtpLink *rtpLink = (RvRtpLink *)rvMdmTermGetUserData(term);
	RvSdpMsgList *localSdpList = rvMdmMediaStreamDescrGetLocalDescr(streamDescr);
	RvSdpMsgList *remoteSdpList = rvMdmMediaStreamDescrGetRemoteDescr(streamDescr);
	RvMdmStreamMode mode = rvMdmMediaStreamDescrGetMode(streamDescr); /* do i care? */

	if(mode != RV_MDMSTREAMMODE_NOTSET)
		rvRtpLinkSetMode(rtpLink, mode);

	if(localSdpList != NULL)
	{
		while(rvSdpMsgListGetSize(localSdpList))
		{
			RvSdpMsg *msg = rvSdpMsgListGetElement(localSdpList, 0);
			if(rvSdpMsgIsPayloadSupported(msg, 0))
			{
				RvSdpMediaDescr *media;
				RvSdpConnection *connection = rvSdpMsgGetConnection(msg);
				
				rvSdpMsgClearMediaDescr(msg);
				media = rvSdpMsgAddMediaDescr(msg, RV_SDPMEDIATYPE_AUDIO, rvRtpLinkGetLocalPort(rtpLink), RV_SDPPROTOCOL_RTP);
				rvSdpMediaDescrAddPayloadNumber(media, 0);
				
				if(connection != NULL)
					rvSdpConnectionSetAddress(connection, rvRtpLinkGetLocalIpAddr());
				else
					rvSdpMsgSetConnection(msg, RV_SDPNETTYPE_IN, RV_SDPADDRTYPE_IP4, rvRtpLinkGetLocalIpAddr());
				
				rvMdmMediaStreamDescrReportLocalDescr(streamDescr);

				while(rvSdpMsgListGetSize(localSdpList) > 1)
					rvSdpMsgListRemoveElement(localSdpList, 1);			

				break;
			}
			else
			{
				rvSdpMsgListRemoveElement(localSdpList, 0);
			}
		}
	}

	if(remoteSdpList != NULL)
	{
		size_t i;
	
		for(i=0; i<rvSdpMsgListGetSize(remoteSdpList); i++)
		{
			RvSdpMsg *msg = rvSdpMsgListGetElement(remoteSdpList, i);
			if(rvSdpMsgGetNumOfMediaDescr(msg))
			{
				const RvSdpMediaDescr *media = rvSdpMsgGetMediaDescr(msg, 0);
				const RvSdpConnection *connection = rvSdpMediaDescrGetConnection(media);
				
				if(connection == NULL)
					connection = rvSdpMsgGetConnection(msg);
				
				if(connection != NULL)
				{
					const char *address = rvSdpConnectionGetAddress(connection);
					RvInetPort port = rvSdpMediaDescrGetPort(media);
					rvRtpLinkSetRemoteAddress(rtpLink, address, port);
					break; /* not handling multiple SDP messages yet */
				}
			}
		}
	}
	
	return rvTrue;
}

static RvBool rvRtpLinkCreateMedia(RvMdmTerm *term,
	RvMdmMediaStream *media, RvMdmMediaStreamDescr *streamDescr, OUT RvMdmError *mdmError)
{
	return rvRtpLinkProcessMedia(term, media, streamDescr, mdmError);
}

static RvBool rvRtpLinkModifyMedia(RvMdmTerm *term,
	RvMdmMediaStream *media, RvMdmMediaStreamDescr *streamDescr, RvMdmError *mdmError)
{
	return rvRtpLinkProcessMedia(term, media, streamDescr, mdmError);
}

static RvBool rvRtpLinkDestroyMedia(RvMdmTerm *term, RvMdmMediaStream *media, OUT RvMdmError *mdmError)
{
	return rvTrue;
}

void rvRtpLinkInitTermClass(RvMdmTermClass *rtpClass)
{
	rvMdmTermClassRegisterCreateMediaCB(rtpClass, rvRtpLinkCreateMedia);
	rvMdmTermClassRegisterModifyMediaCB(rtpClass, rvRtpLinkModifyMedia);
	rvMdmTermClassRegisterDestroyMediaCB(rtpClass, rvRtpLinkDestroyMedia);
}

⌨️ 快捷键说明

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