首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能从RTP数据包中提取RTP有效负载

不能从RTP数据包中提取RTP有效负载
EN

Stack Overflow用户
提问于 2018-10-14 09:21:27
回答 1查看 529关注 0票数 0

嗨,我想像udpxy那样将多播视频代理到单播:http://www.udpxy.com,但是在C#中,由于找不到我可以使用的合适的RTP库(它们太复杂了,或者我无法理解如何使用它们),所以我决定移植一个udpxy使用rtp.c:https://github.com/pcherenkov/udpxy/blob/master/chipmunk/rtp.c

一切都进行得很顺利(几乎,因为我不想使用指针),直到我想把RTP_Process翻译成C#

RTP_Process in C

代码语言:javascript
复制
RTP_process( void** pbuf, size_t* len, int verify, FILE* log )
{
    int rtp_padding = -1;
    size_t front_skip = 0, back_skip = 0, pad_len = 0;

    char* buf = NULL;
    size_t pkt_len = 0;

    assert( pbuf && len && log );
    buf = *pbuf;
    pkt_len = *len;

    if( verify && !RTP_verify( buf, pkt_len, log ) )
        return -1;

    if( 0 != RTP_hdrlen( buf, pkt_len, &front_skip, log ) )
        return -1;

    rtp_padding = buf[0] & 0x20;
    if( rtp_padding ) {
        pad_len = buf[ pkt_len - 1 ];
    }

    back_skip += pad_len;

    if( verify && (pkt_len < (front_skip + back_skip)) ) {
        (void) tmfprintf( log, "RTP_process: invalid header "
                "(skip [%lu] exceeds packet length [%lu])\n",
                (u_long)(front_skip + back_skip), (u_long)pkt_len );
        return -1;
    }

    /* adjust buffer/length to skip heading and padding */
    /*
    TRACE( (void)tmfprintf( log, "In: RTP buf=[%p] of [%lu] bytes, "
                "fskip=[%ld], bskip=[%lu]\n",
                (void*)buf, (u_long)pkt_len,
                (u_long)front_skip, (u_long)back_skip ) );
    */

    buf += front_skip;
    pkt_len -= (front_skip + back_skip);

    /*
    TRACE( (void)tmfprintf( log, "Out RTP buf=[%p] of [%lu] bytes\n",
                (void*)buf, (u_long)pkt_len ) );
    */
    *pbuf = buf;
    *len  = pkt_len;

    return 0;
}

RTP_Process in C#

代码语言:javascript
复制
public byte[] RTP_process(int verify)
{
    /* process RTP package to retrieve the payload: set
    * pbuf to the start of the payload area; set len to
    * be equal payload's length
    *
    * @param pbuf      address of pointer to beginning of RTP packet
    * @param len       pointer to RTP packet's length
    * @param verify    verify that it is an RTP packet if != 0
    * @param log       log file
    *
    * @return 0 if there was no error, -1 otherwise;
    *         set pbuf to point to beginning of payload and len
    *         be payload size in bytes
    */
    int rtp_padding = -1;
    int front_skip = 0, back_skip = 0, pad_len = 0;

    int pkt_len = 0;

    //assert(pbuf && len && log);
    //buf = *pbuf;
    pbuf = buf;
    //pkt_len = *len;
    len = pkt_len;

    /*
    if (verify != 1 && RTP_verify() != 1)
        RTPOK = - 1;

    if (0 != RTP_hdrlen(buf, pkt_len, front_skip)) //?????
        RTPOK = - 1;

    */

    rtp_padding = buf[0] & 0x20;
    if (rtp_padding != -1) //???????
    {
        pad_len = buf[pkt_len - 1];
    }

    back_skip += pad_len;

    if ((verify != -1) && (pkt_len < (front_skip + back_skip))) //???????
    {
        Console.WriteLine("RTP_process: invalid header (skip {0} exceeds packet length {1})\n", (long)(front_skip + back_skip), (long)pkt_len);
        RTPOK = - 1;
    }

    /* adjust buffer/length to skip heading and padding */
    /*
    TRACE( (void)tmfprintf( log, "In: RTP buf=[%p] of [%lu] bytes, "
                "fskip=[%ld], bskip=[%lu]\n",
                (void*)buf, (u_long)pkt_len,
                (u_long)front_skip, (u_long)back_skip ) );
    */

    //buf += front_skip;
    //pkt_len -= (front_skip + back_skip);

    /*
    TRACE( (void)tmfprintf( log, "Out RTP buf=[%p] of [%lu] bytes\n",
                (void*)buf, (u_long)pkt_len ) );
    */
    pbuf = buf;
    len = pkt_len;

    RTPOK = 0;
    return pbuf;
}

buf += front_skip;抱怨说,运算符+=不能应用于byte[]和int类型的操作数,那么为什么它在C中的RTP_Process中工作,以及什么是与之对应的C#。

代码语言:javascript
复制
if (rtp_padding != -1) //???????
{
    pad_len = buf[pkt_len - 1]; //There is an exeption trown: System.IndexOutOfRangeException: Index was outside the bounds of the array.

很明显,我解释和翻译了错误的东西,但我只想做的是让MPEG-TS帧离开RTP流,然后将它转发到TCP套接字,这样如果有人能提出更好的方法,我很想听听。

谢谢你的祝福和问候}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-19 20:19:33

首先,我建议仔细阅读RFC-3550,它包含关于RTP -包结构的所有信息(大多数您需要第5节:RTP固定头和扩展)。

  1. 然后您必须实现RTP_hdrlen来计算RTP头大小,它必须返回front_skip值作为RTP头大小(包括扩展)。因此,您不必使用buf += front_skip;,RTP有效负载从字节buf[front_skip]开始。
  2. 您在这里有错误的数据包长度参数:int pkt_len = 0;,这就是为什么异常在这里抛出的原因,pad_len = buf[pkt_len - 1];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52801233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档