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

📄 rtp.c

📁 基于h323协议的软phone
💻 C
📖 第 1 页 / 共 3 页
字号:
        RvSocketDestruct(&s->socket, RV_FALSE, NULL);

        if (s->remoteAddressSet)
            RvAddressDestruct(&s->remoteAddress);

        if (s->isAllocated)
            RvMemoryFree(s);
    }

    return RV_OK;
}

/************************************************************************************
 * rtpGetSSRC
 * description: Returns the current SSRC (synchronization source value) of the RTP session.
 * input: hRTP - Handle of the RTP session.
 * output: none.
 * return value: If no error occurs, the function returns the current SSRC value.
 *               Otherwise, it returns a negative value.
 ***********************************************************************************/
RVAPI
RvUint32 RVCALLCONV rtpGetSSRC(
        IN  HRTPSESSION  hRTP)
{
    rtpSession *s = (rtpSession *)hRTP;

    return s->sSrc;
}

/************************************************************************************
 * rtpSetEventHandler
 * description: Set an Event Handler for the RTP session. The application must set
 *              an Event Handler for each RTP session.
 * input: hRTP          - Handle of the RTP session.
 *        eventHandler  - Pointer to the callback function that is called each time a
 *                        new RTP packet arrives to the RTP session.
 *        context       - The parameter is an application handle that identifies the
 *                        particular RTP session. The application passes the handle to
 *                        the Event Handler.
 * output: none.
 * return value: none.
 ***********************************************************************************/
RVAPI
void RVCALLCONV rtpSetEventHandler(
        IN  HRTPSESSION        hRTP,
        IN  LPRTPEVENTHANDLER  eventHandler,
        IN  void *             context)
{
    rtpSession *s = (rtpSession *)hRTP;
    RvBool hasFd, addFd;

    if (s)
    {
        hasFd = (s->eventHandler != NULL);
        addFd = (eventHandler != NULL);
        s->eventHandler = eventHandler;
        s->context      = context;

        /* Seems like we have to add it to our list of fds */
        if (addFd & (!hasFd))
        {
            RvSocketSetBlocking(&s->socket, RV_FALSE);
            RvFdConstruct(&s->selectFd, &s->socket);
            RvSelectAdd(RvRtpGetSelectEngine(rvRtpInstance.selectEngine),
                &s->selectFd, RvSelectRead, rtpEvent);
        }

        /* Seems like we have to remove it from our list of fds */
        if (!addFd & hasFd)
        {
            RvSelectRemove(RvRtpGetSelectEngine(rvRtpInstance.selectEngine), &s->selectFd);
            RvFdDestruct(&s->selectFd);
            RvSocketSetBlocking(&s->socket, RV_TRUE);
        }
    }
}

/************************************************************************************
 * rtpSetRemoteAddress
 * description: Defines the address of the remote peer or the address of a multicast
 *              group to which the RTP stream will be sent.
 * input: hRTP  - Handle of the RTP session.
 *        ip    - IP address to which RTP packets should be sent.
 *        port  - UDP port to which RTP packets should be sent.
 * output: none.
 * return value: none.
 ***********************************************************************************/
RVAPI
void RVCALLCONV rtpSetRemoteAddress(
        IN HRTPSESSION  hRTP,   /* RTP Session Opaque Handle */
        IN RvUint32     ip,
        IN RvUint16     port)
{
    rtpSession* s = (rtpSession *)hRTP;

    if (s != NULL)
    {
        if (s->remoteAddressSet)
            RvAddressDestruct(&s->remoteAddress);

        RvAddressConstructIpv4(&s->remoteAddress, ip, port);
        s->remoteAddressSet = RV_TRUE;
    }
}

/************************************************************************************
 * rtpPack
 * description: This routine sets the RTP header.
 * input: hRTP  - Handle of the RTP session.
 *        buf   - Pointer to buffer containing the RTP packet with room before first
 *                payload byte for RTP header.
 *        len   - Length in bytes of buf.
 *        p     - A struct of RTP param.
 * output: none.
 * return value:  If no error occurs, the function returns the non-neagtive value.
 *                Otherwise, it returns a negative value.
 ***********************************************************************************/
RVAPI
RvInt32 RVCALLCONV rtpPack(
        IN  HRTPSESSION  hRTP,
        IN  void *       buf,
        IN  RvInt32      len,
        IN  rtpParam *   p)
{
    rtpSession *s = (rtpSession *)hRTP;
    RvUint32 *header;
    RvUint32 seq;

    p->sByte-=12;
    p->len=len - p->sByte;

    if (s->useSequenceNumber)
        s->sequenceNumber=p->sequenceNumber;
    p->sequenceNumber=s->sequenceNumber;
    seq = s->sequenceNumber;

    /* sets the fields inside RTP message.*/
    header=(RvUint32*)((char*)buf + p->sByte);
    header[0]=0;
    header[0]=bitfieldSet(header[0],2,30,2);
    header[0]=bitfieldSet(header[0],p->marker,23,1);
    header[0]=bitfieldSet(header[0],p->payload,16,7);
    header[0]=bitfieldSet(header[0],seq,0,16);
    header[1]=p->timestamp;
    header[2]=s->sSrc;

    /* increment the internal sequence number for this session */
    s->sequenceNumber++;

    /* converts an array of 4-byte integers from host format to network format.*/
    ConvertToNetwork(header, 0, 3);

    return RV_OK;
}
#if 0
RVAPI
RvInt32 RVCALLCONV rtpPackTest(
        IN  HRTPSESSION  hRTP,
        IN  void *       buf,
        IN  RvInt32      len,
        IN  rtpParam *   p)
{
    rtpSession *s = (rtpSession *)hRTP;
    RvUint32 *header;
    RvUint32 seq;

    p->sByte-=12;
    p->len=len - p->sByte;

    if (s->useSequenceNumber)
        s->sequenceNumber=p->sequenceNumber;
    p->sequenceNumber=s->sequenceNumber;
    seq = s->sequenceNumber;

    /* sets the fields inside RTP message.*/
    header=(RvUint32*)((char*)buf + p->sByte);
    header[0]=0;
    header[0]=bitfieldSet(header[0],2,30,2);
    header[0]=bitfieldSet(header[0],p->marker,23,1);
    header[0]=bitfieldSet(header[0],p->payload,16,7);
    header[0]=bitfieldSet(header[0],seq,0,16);
    header[1]=p->timestamp;
    header[2]=s->sSrc;

    /* increment the internal sequence number for this session */
    

    /* converts an array of 4-byte integers from host format to network format.*/
    ConvertToNetwork(header, 0, 3);

    return RV_OK;
}
#endif
/************************************************************************************
 * rtpWrite
 * description: This routine sends the RTP packet.
 * input: hRTP  - Handle of the RTP session.
 *        buf   - Pointer to buffer containing the RTP packet with room before first
 *                payload byte for RTP header.
 *        len   - Length in bytes of buf.
 *        p     - A struct of RTP param.
 * output: none.
 * return value:  If no error occurs, the function returns the non-neagtive value.
 *                Otherwise, it returns a negative value.
 ***********************************************************************************/
RVAPI
RvInt32 RVCALLCONV rtpWrite(
        IN  HRTPSESSION  hRTP,
        IN  void *       buf,
        IN  RvInt32      len,
        IN  rtpParam *   p)
{
    RvStatus res;
    rtpSession *s = (rtpSession *)hRTP;

    res = rtpPack(hRTP, buf, len, p);

    if (res >= 0)
    {
        /* send udp data through the specified socket to the remote host.*/
        res = RvSocketSendBuffer(&s->socket, (RvUint8*)buf+p->sByte, (RvSize_t)p->len, &s->remoteAddress, NULL);
    }

    if (res == RV_OK)
    {
        if ((s->hRTCP != NULL) &&  (res == RV_OK))
        {
            /* inform the RTCP session about a packet that was sent in the corresponding RTP session.*/
            rtcpRTPPacketSent(s->hRTCP, p->len, p->timestamp);
        }
    }

    return res;
}
#if 0
RVAPI
RvInt32 RVCALLCONV rtpWriteTest(
        IN  HRTPSESSION  hRTP,
        IN  void *       buf,
        IN  RvInt32      len,
        IN  rtpParam *   p)
{
    RvStatus res;
    rtpSession *s = (rtpSession *)hRTP;

    res = rtpPackTest(hRTP, buf, len, p);

    if (res >= 0)
    {
        /* send udp data through the specified socket to the remote host.*/
        res = RvSocketSendBuffer(&s->socket, (RvUint8*)buf+p->sByte, (RvSize_t)p->len, &s->remoteAddress, NULL);
    }

    if (res == RV_OK)
    {
        if ((s->hRTCP != NULL) &&  (res == RV_OK))
        {
            /* inform the RTCP session about a packet that was sent in the corresponding RTP session.*/
            rtcpRTPPacketSent(s->hRTCP, p->len, p->timestamp);
        }
    }

    return res;
}
#endif
RVAPI
RvInt32 RVCALLCONV rtpUnpack(
        IN  HRTPSESSION  hRTP,
        IN  void *buf,
        IN  RvInt32 len,
        OUT rtpParam* p)
{
    RvUint32 *header=(RvUint32*)buf;
    if (len || hRTP);

    if (p->len < 12)
        return RV_ERROR_UNKNOWN;

    ConvertFromNetwork(buf, 3, (int)bitfieldGet(header[0], 24, 4));
    p->timestamp=header[1];
    p->sequenceNumber=(RvUint16)bitfieldGet(header[0],0,16);
    p->sSrc=header[2];
    p->marker=bitfieldGet(header[0],23,1);
    p->payload=(unsigned char)bitfieldGet(header[0],16,7);

    p->sByte=12+bitfieldGet(header[0],24,4)*sizeof(RvUint32);

    if (bitfieldGet(header[0],28,1))/*Extension Bit Set*/
    {
        int xStart=p->sByte / sizeof(RvUint32);

        ConvertFromNetwork(buf, xStart, 1);
        p->sByte+=bitfieldGet(header[xStart],0,16)*sizeof(RvUint32);
        if (p->sByte > p->len)
        {
            /* This packet is probably corrupted */
            p->sByte = 12;
            return RV_ERROR_UNKNOWN;
        }
    }

    if (bitfieldGet(header[0],29,1))/*Padding Bit Set*/
    {
        p->len-=((char*)buf)[p->len-1];
    }
    return RV_OK;
}

/************************************************************************************
 * rtpRead
 * description: This routine sets the header of the RTP message.
 * input: hRTP  - Handle of the RTP session.
 *        buf   - Pointer to buffer containing the RTP packet with room before first
 *                payload byte for RTP header.
 *        len   - Length in bytes of buf.
 *
 * output: p    - A struct of RTP param,contain the fields of RTP header.
 * return value: If no error occurs, the function returns the non-neagtive value.
 *                Otherwise, it returns a negative value.
 ***********************************************************************************/
RVAPI
RvInt32 RVCALLCONV rtpRead(
        IN  HRTPSESSION  hRTP,
        IN  void *buf,
        IN  RvInt32 len,
        OUT rtpParam* p)
{
    rtpSession* s = (rtpSession *)hRTP;
    RvAddress remoteAddress;
    RvSize_t messageSize;
    RvStatus res;
    RvUint32 ip;

    res = RvSocketReceiveBuffer(&s->socket, (RvUint8*)buf, (RvSize_t)len, &messageSize, &remoteAddress);
    if (res != RV_OK)
        return res;

    ip = RvAddressIpv4GetIp(RvAddressGetIpv4(&remoteAddress));

    RvAddressDestruct(&remoteAddress);

    p->len = (int)messageSize;

    ConvertFromNetwork(buf, 0, 3);

    /* If the SSRC is ours and the is our IP, we should ignore this packet - it probably came
       in as a multicast packet we sent out. */
    if ( (s->sSrc == ((RvUint32*)buf)[2]) && isMyIP(ip) )
        return RV_ERROR_UNKNOWN;

    if (p->len <= 0)
        return RV_ERROR_UNKNOWN;

    return rtpUnpack(hRTP, buf, len, p);
}

/************************************************************************************
 * rtpReadEx
 * description: Receives an RTP packet and updates the corresponding RTCP session.
 * input: hRTP      - Handle of the RTP session.
 *        buf       - Pointer to buffer containing the RTP packet with room before first
 *                    payload byte for RTP header.
 *        len       - Length in bytes of buf.
 *        timestamp -
 *        p         - A struct of RTP param,contain the fields of RTP header.
 * output: none.
 * return value: If no error occurs, the function returns the non-neagtive value.
 *               Otherwise, it returns a negative value.
 ***********************************************************************************/
RVAPI
RvInt32 RVCALLCONV rtpReadEx(
        IN  HRTPSESSION  hRTP,
        IN  void *       buf,
        IN  RvInt32      len,
        IN  RvUint32     timestamp,
        OUT rtpParam *   p)
{
    rtpSession *s = (rtpSession *)hRTP;
    int retVal;

⌨️ 快捷键说明

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