rtputil.c

来自「基于h323协议的软phone」· C语言 代码 · 共 275 行

C
275
字号
/***********************************************************************
        Copyright (c) 2002 RADVISION Ltd.
************************************************************************
NOTICE:
This document contains information that is confidential and proprietary
to RADVISION Ltd.. No part of this document may be reproduced in any
form whatsoever without written prior approval by RADVISION Ltd..

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

#include "rvtypes.h"
#include "rvlock.h"
#include "rvmemory.h"
#include "rvthreadinstance.h"
#include "rtputil.h"


/* The thread index of the select engine */
static RvUint32 rtpSelectEngineIndex;

/* The thread index of the timer queue used by RTCP */
static RvUint32 rtpTimerQIndex;


static RvStatus rtpDeleteSelectEngine(
    IN  const RvChar*   objectName,
    IN  void*           context,
    IN  void*           objectPtr)
{
    RvSelectEngine* selectEngine = (RvSelectEngine *)objectPtr;
    RV_UNUSED_ARG(objectName);
    RV_UNUSED_ARG(context);

    RvSelectDestruct(selectEngine);
    return RvMemoryFree(selectEngine);
}


static RvStatus rtpCreateSelectEngine(
    IN    const RvChar*             objectName,
    IN    void*                     context,
    IN    RvSize_t                  amountNeeded,
    INOUT void**                    objectPtr,
    OUT   RvSize_t*                 amountAllocated,
    OUT   ThreadInstanceDeleteFunc* deleteFunc)
{
    RvSelectEngine* selectEngine;
    RvStatus status;

    RV_UNUSED_ARG(objectName);
    RV_UNUSED_ARG(context);
    RV_UNUSED_ARG(amountNeeded);

    status = RvMemoryAlloc(NULL, (void**)&selectEngine, sizeof(RvSelectEngine));
    if (status != RV_OK)
        return status;

    status = RvSelectConstruct(selectEngine, 2048);
    if (status != RV_OK)
    {
        RvMemoryFree(selectEngine);
        return status;
    }

    *objectPtr = selectEngine;
    *amountAllocated = 0;
    *deleteFunc = rtpDeleteSelectEngine;

    return RV_OK;
}


static RvStatus rtpDeleteTimerQ(
    IN  const RvChar*   objectName,
    IN  void*           context,
    IN  void*           objectPtr)
{
    RvTimerQueue* timerQ = (RvTimerQueue *)objectPtr;
    RV_UNUSED_ARG(objectName);
    RV_UNUSED_ARG(context);

    RvTimerQueueDestruct(timerQ);
    return RvMemoryFree(timerQ);
}


static RvStatus rtpCreateTimerQ(
    IN    const RvChar*             objectName,
    IN    void*                     context,
    IN    RvSize_t                  amountNeeded,
    INOUT void**                    objectPtr,
    OUT   RvSize_t*                 amountAllocated,
    OUT   ThreadInstanceDeleteFunc* deleteFunc)
{
    RvTimerQueue* timerQ;
    RvStatus status;

    RV_UNUSED_ARG(objectName);
    RV_UNUSED_ARG(context);

    if (*objectPtr != NULL)
    {
        /* We're adding more timers */
        timerQ = (RvTimerQueue *)(*objectPtr);

        *amountAllocated = RvTimerQueueAddSize(timerQ, amountNeeded);
    }
    else
    {
        /* New allocation of a timer queue */
        status = RvMemoryAlloc(NULL, (void**)&timerQ, sizeof(RvTimerQueue));
        if (status != RV_OK)
            return status;

        status = RvTimerQueueConstruct(timerQ, RV_TIMER_QTYPE_FIXED, amountNeeded, 0, 0, 0,
            100, NULL);
        if (status != RV_OK)
            return status;

        *objectPtr = timerQ;
        *amountAllocated = RvTimerQueueGetSize(timerQ);
        *deleteFunc = rtpDeleteTimerQ;
    }

    return RV_OK;
}





/************************************************************************
 * RvRtpInitSelectEngine
 * purpose: Initialize a select engine if necessary or just give the
 *          allocated one.
 * input  : None
 * output : Select engine pointer
 * return : RV_OK on success, negative value otherwise
 ************************************************************************/
RvStatus RvRtpInitSelectEngine(
    OUT RvSelectEngine**   selectEngine)
{
    RvStatus status;

    *selectEngine = NULL;

    status = ThreadInstanceInitObject(RV_THREAD_INSTANCE_SELECT, rtpCreateSelectEngine, NULL, 0,
        (void**)selectEngine, NULL, &rtpSelectEngineIndex);

    return status;
}


/************************************************************************
 * RvRtpEndSelectEngine
 * purpose: Deinitialize aselect engine if necessary or just decrease its
 *          reference count by one.
 * input  : None
 * output : None
 * return : RV_OK on success, negative value otherwise
 ************************************************************************/
RvStatus RvRtpEndSelectEngine(void)
{
    RvStatus status;

    status = ThreadInstanceEndObject(rtpSelectEngineIndex, 0, NULL);

    return status;
}


/************************************************************************
 * RvRtpGetSelectEngine
 * purpose: Get the select engine used by the current thread
 * input  : defSelectEngine - Default select engine to use if none available
 * output : None
 * return : Select engine on success, NULL if not initialized in this thread.
 ************************************************************************/
RvSelectEngine* RvRtpGetSelectEngine(IN RvSelectEngine* defSelectEngine)
{
    RvSelectEngine* selectEngine;
    RvStatus status;

    status = ThreadInstanceGetObject(rtpSelectEngineIndex, (void**)&selectEngine);

    if (status == RV_OK)
        return selectEngine;

    return defSelectEngine;
}


/************************************************************************
 * RvRtpInitTimerQueue
 * purpose: Initialize a timer queue if necessary or just reallocate its
 *          size
 * input  : None
 * output : Timer queue pointer
 * return : RV_OK on success, negative value otherwise
 ************************************************************************/
RvStatus RvRtpInitTimerQueue(
    OUT RvTimerQueue**   timerQ)
{
    RvStatus status;

    *timerQ = NULL;

    status = ThreadInstanceInitObject(RV_THREAD_INSTANCE_TIMERS, rtpCreateTimerQ, NULL, 1024,
        (void**)timerQ, NULL, &rtpTimerQIndex);

    return status;
}


/************************************************************************
 * RvRtpEndTimerQueue
 * purpose: Deinitialize a timer queue if necessary.
 * input  : None
 * output : None
 * return : RV_OK on success, negative value otherwise
 ************************************************************************/
RvStatus RvRtpEndTimerQueue(void)
{
    RvStatus status;

    status = ThreadInstanceEndObject(rtpTimerQIndex, 0, NULL);

    return status;
}


/************************************************************************
 * RvRtpGetTimerQueue
 * purpose: Get the timer queue used by the current thread
 * input  : defTimerQueue   - Default timer queue to use if none available
 * output : None
 * return : Timer queue on success, NULL if not initialized in this thread.
 ************************************************************************/
RvTimerQueue* RvRtpGetTimerQueue(IN RvTimerQueue* defTimerQ)
{
    RvTimerQueue* timerQ;
    RvStatus status;

    status = ThreadInstanceGetObject(rtpTimerQIndex, (void**)&timerQ);

    if (status == RV_OK)
        return timerQ;

    return defTimerQ;
}





void ConvertToNetwork(void *data, int pos, int n)
{
    int i;
    for (i = pos; i < pos + n; ++i)
      ((RvUint32*)data)[i] = RvConvertHostToNetwork32(((RvUint32*)data)[i]);
}

void ConvertFromNetwork(void *data, int pos, int n)
{
    int i;
    for (i = pos; i < pos + n; ++i)
      ((RvUint32*)data)[i] = RvConvertNetworkToHost32(((RvUint32*)data)[i]);
}



⌨️ 快捷键说明

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