0

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

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

C中的RTP_Process

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;
}

C# 中的 RTP_Process

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;
}

问题从这里开始 1.buf += front_skip;抱怨 operator += 不能应用于 byte[] 和 int 类型的操作数,那么为什么它在 C 中的 RTP_Process 中工作以及什么是 C# 等价物 2. 在

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.

很明显,我以错误的方式解释和翻译了某些东西,但我唯一想做的就是从 RTP 流中获取 MPEG-TS 帧,然后将其转发到 TCP 套接字,所以如果有人能提出更好的做法我很想听

感谢您的回答和最诚挚的问候}

4

1 回答 1

0

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

  1. 然后你必须实现 RTP_hdrlen 来计算 RTP 头大小,它必须返回 front_skip 值作为 RTP 头大小,包括扩展。因此,您不必使用buf += front_skip;, RTP 有效负载从 byte 开始buf[front_skip]

  2. 你在这里有错误的数据包长度参数:int pkt_len = 0;,这就是这里抛出异常的原因pad_len = buf[pkt_len - 1];

于 2018-10-19T20:19:33.003 回答