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

📄 rtp.c

📁 Radvision rtpRtcp协议栈 4.5.0.18 版本
💻 C
📖 第 1 页 / 共 3 页
字号:
/***********************************************************************        Copyright (c) 2002 RADVISION Ltd.************************************************************************NOTICE:This document contains information that is confidential and proprietaryto RADVISION Ltd.. No part of this document may be reproduced in anyform whatsoever without written prior approval by RADVISION Ltd..RADVISION Ltd. reserve the right to revise this publication and makechanges without obligation to notify any person of such revisions orchanges.***********************************************************************/#include "rvstdio.h"#include "rvmemory.h"#include "rvtimestamp.h"#include "rvrandomgenerator.h"#include "rvhost.h"#include "rvsocket.h"#include "rvselect.h"#include "rvcbase.h"#include "bitfield.h"#include "rtputil.h"#include "rtp.h"#include "rtcp.h"#ifdef __cplusplusextern "C" {#endif#define BUILD_VER_NUM(_max, _min, _dot1, _dot2) \    ((RvUint32)((_max << 24) + (_min << 16) + (_dot1 << 8) + _dot2))#define VERSION_STR    "4.2.0.0"#define VERSION_NUM    BUILD_VER_NUM(4, 2, 0, 0)#define RV_RTP_MAXIPS RvUint32Const(20)/* Internal RTP instance used. There a single such object */typedef struct{    RvSelectEngine*     selectEngine; /* Select engine used */    RvUint32            localIp; /* IP to bind to */    RvUint32            hostIPs[RV_RTP_MAXIPS]; /* Local host IPs */    RvUint32            timesInitialized; /* Times the RTP was initialized */    RvRandomGenerator   randomGenerator; /* Random numbers generator to use */} RvRtpInstance;#define logMgr NULL/* RTP instance to use */static RvRtpInstance rvRtpInstance;/* local functions */static RvBool isMyIP(RvUint32 ip);static void rtpEvent(        IN RvSelectEngine*  selectEngine,        IN RvSelectFd*      fd,        IN RvSelectEvents   selectEvent,        IN RvBool           error);                       /* == Basic RTP Functions == */RVAPIRvInt32 RVCALLCONV rtpInit(void){    RvStatus status;    status = RvCBaseInit();    if (status != RV_OK)        return status;    if (rvRtpInstance.timesInitialized == 0)    {        RvAddress addresses[RV_RTP_MAXIPS];        RvUint32 numAddrs = RV_RTP_MAXIPS;        RvUint32 i;        /* Find the list of host addresses we have */        status = RvHostLocalGetAddress(logMgr, &numAddrs, addresses);        if (status != RV_OK)        {            RvCBaseEnd();            return status;        }        for (i = 0; i < numAddrs; i++)            rvRtpInstance.hostIPs[i] = RvAddressIpv4GetIp(RvAddressGetIpv4(addresses + i));        while (i > 0)        {            i--;            RvAddressDestruct(addresses+i);        }        /* Create a random generator */        RvRandomGeneratorConstruct(&rvRtpInstance.randomGenerator,            (RvRandom)RvInt64ToRvUint32(RvInt64ShiftRight(RvTimestampGet(logMgr), 8)));    }    status = RvSelectConstruct(2048, 0, logMgr, &rvRtpInstance.selectEngine);    if (status != RV_OK)    {        RvCBaseEnd();        return status;    }    rvRtpInstance.timesInitialized++;    return RV_OK;}RVAPIRvInt32 RVCALLCONV rtpInitEx(RvUint32 ip){    RvInt32 rc;    if ((rc=rtpInit()) != RV_ERROR_UNKNOWN)        rvRtpInstance.localIp = ip;    return rc;}/************************************************************************************ * rtpSetLocalAddress * description: Set the local address to use for calls to rtpOpenXXX functions. *              This parameter overrides the value given in rtpInitEx() for all *              subsequent calls. * input: ip    - Local IP address to use * output: none. * return value: Non-negative value on success *               negative value on failure ***********************************************************************************/RVAPIint RVCALLCONV rtpSetLocalAddress(IN RvUint32 ip){    rvRtpInstance.localIp = ip;    return RV_OK;}RVAPIvoid RVCALLCONV rtpEnd(void){    rvRtpInstance.timesInitialized--;    if (rvRtpInstance.timesInitialized == 0)    {        RvRandomGeneratorDestruct(&rvRtpInstance.randomGenerator);    }    RvSelectDestruct(rvRtpInstance.selectEngine, 0);    RvCBaseEnd();}RVAPIint RVCALLCONV rtpGetAllocationSize(void){    return sizeof(rtpSession);}/************************************************************************************ * rtpOpenFrom * description: Opens an RTP session in the memory that the application allocated. * input: port        - The UDP port number to be used for the RTP session. *        ssrcPattern - Synchronization source Pattern value for the RTP session. *        ssrcMask    - Synchronization source Mask value for the RTP session. *        buffer      - Application allocated buffer with a value no less than the *                      value returned by the function rtpGetAllocationSize(). *        bufferSize  - size of the buffer. * output: none. * return value: If no error occurs, the function returns the handle for the opened RTP *               session. Otherwise, it returns NULL. ***********************************************************************************/RVAPIHRTPSESSION RVCALLCONV rtpOpenFrom(        IN  RvUint16    port,        IN  RvUint32    ssrcPattern,        IN  RvUint32    ssrcMask,        IN  void*       buffer,        IN  int         bufferSize){    rtpSession *s = (rtpSession *)buffer;    RvAddress localAddress;    RvRandom randomValue;    RvStatus res;    if (bufferSize < rtpGetAllocationSize())        return NULL;    memset(buffer, 0 , (RvSize_t)rtpGetAllocationSize());    /* Get a random value for the beginning sequence number */    RvRandomGeneratorGetValue(&rvRtpInstance.randomGenerator, &randomValue);    s->isAllocated    = RV_FALSE;    s->sSrcPattern    = ssrcPattern;    s->sSrcMask       = ssrcMask;    s->sequenceNumber = (RvUint16)randomValue;    RvAddressConstructIpv4(&localAddress, rvRtpInstance.localIp, port);    /* Open and bind the socket */    res = RvSocketConstruct(RV_ADDRESS_TYPE_IPV4, RvSocketProtocolUdp, logMgr, &s->socket);    if (res == RV_OK)    {        RvSocketSetBuffers(&s->socket, 8192, 8192, logMgr);        res = RvSocketSetBroadcast(&s->socket, RV_TRUE, logMgr);        if (res == RV_OK)            res = RvSocketSetBlocking(&s->socket, RV_TRUE, logMgr);        if (res == RV_OK)            res = RvSocketBind(&s->socket, &localAddress, NULL, logMgr);        if (res != RV_OK)            RvSocketDestruct(&s->socket, RV_FALSE, NULL, logMgr);    }    RvAddressDestruct(&localAddress);    if (res == RV_OK)    {        rtpRegenSSRC((HRTPSESSION)s);        return (HRTPSESSION)s;    }    else    {        return (HRTPSESSION)NULL;    }}/************************************************************************************ * rtpOpen * description: Opens an RTP session. The RTP Stack allocates an object and the *              memory needed for the RTP session. It also opens a socket and waits *              for packets. rtpOpen() also returns the handle of this session to *              the application. * input: port        - The UDP port number to be used for the RTP session. *        ssrcPattern - Synchronization source Pattern value for the RTP session. *        ssrcMask    - Synchronization source Mask value for the RTP session. * output: none. * return value: If no error occurs, the function returns the handle for the opened RTP *               session. Otherwise, it returns NULL. ***********************************************************************************/RVAPIHRTPSESSION RVCALLCONV rtpOpen(        IN  RvUint16    port,        IN  RvUint32    ssrcPattern,        IN  RvUint32    ssrcMask){    return rtpOpenEx(port, ssrcPattern, ssrcMask, NULL);}/************************************************************************************ * rtpOpenEx * description: Opens an RTP session and an associated RTCP session. * input: port        - The UDP port number to be used for the RTP session. *        ssrcPattern - Synchronization source Pattern value for the RTP session. *        ssrcMask    - Synchronization source Mask value for the RTP session. *        cname       - The unique name representing the source of the RTP data. * output: none. * return value: If no error occurs, the function returns the handle for the open *               RTP session. Otherwise, the function returns NULL. ***********************************************************************************/RVAPIHRTPSESSION RVCALLCONV rtpOpenEx(        IN  RvUint16    port,        IN  RvUint32    ssrcPattern,        IN  RvUint32    ssrcMask,        IN  char *      cname){    /* allocate rtp session.*/    rtpSession* s;    RvStatus res;    res = RvMemoryAlloc(NULL, (RvSize_t)rtpGetAllocationSize(), logMgr, (void**)&s);    if (res != RV_OK)        return NULL;    if ((rtpSession *)rtpOpenFrom(port, ssrcPattern, ssrcMask, (void*)s, rtpGetAllocationSize())==NULL)    {        RvMemoryFree(s, logMgr);        return NULL;    }    s->isAllocated=RV_TRUE;    if (cname)    {        /* Open a new rtcp session.The port for an RTCP session is always           (RTP port + 1).*/        s->hRTCP = rtcpOpen(s->sSrc, (RvUint16)((port)?port+1:port), cname);        if (s->hRTCP == NULL)        {            /* Bad RTCP - let's just kill the RTP and be done with it */            rtpClose((HRTPSESSION)s);            s = NULL;        }    }    return (HRTPSESSION)s;}/************************************************************************************ * rtpClose * description: Close RTP session. * input: hRTP - Handle of the RTP session. * output: none. * return value: If an error occurs, the function returns a negative value. *               If no error occurs, the function returns a non-negative value. ***********************************************************************************/RVAPIRvUint32 RVCALLCONV rtpClose(        IN  HRTPSESSION  hRTP){    rtpSession *s = (rtpSession *)hRTP;    if (s)    {        if (s->hRTCP)            rtcpClose(s->hRTCP);        if (s->eventHandler != NULL)        {            /* We're probably also in non-blocking mode */            RvSelectRemove(s->selectEngine, &s->selectFd);            RvFdDestruct(&s->selectFd);        }        else        {            /* Send UDP data through specified socket to the local host */            rtpResume(hRTP);        }        /* This function closes the specified IP socket and all the socket's connections.*/        RvSocketDestruct(&s->socket, RV_FALSE, NULL, logMgr);        if (s->remoteAddressSet)        {            RvAddressDestruct(&s->remoteAddress);        }        if (s->isAllocated)            RvMemoryFree(s, logMgr);    }

⌨️ 快捷键说明

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