我正在解析Wireshark捕获的RTMP流,并将媒体数据写入.flv文件。我知道如何计算时间戳,但是溢出时如何处理?

最佳答案

所以我这样解决了问题:
1)解析RTMP header (从timestamp的开头读取4个字节)并获取timestamp(u_int 32);

if ( ( _timestamp >> 8 ) == 0xffffff ) /* check if extended timestamp is present */
    {
        _timestamp = (_timestamp & 0x000000ff) + 0xffffff;
    }
    else
    {
        _timestamp >>= 8 ;
    }

2)计算.flv文件的时间戳(.flv文件中的时间戳始终为32位,例如0x00000100是简单时间戳,等于1,0xffffff01是扩展时间戳,等于0xffffff + 0x01)
if ( ( timestamp_calc + _timestamp ) >= 0xffffff )
    {
        unsigned __int8 temp = _timestamp;
        unsigned __int8 * ptr;

        if ( (timestamp_calc + _timestamp) >= 0xffffffff ){ timestamp_calc = _timestamp; }
        else
        {
            timestamp_calc = 0xffffff;
            timestamp_calc <<= 8;
            ptr = (unsigned __int8 *)&timestamp_calc;
            ptr[ 0 ] = _timestamp;
        }
    }
    else
    {
        timestamp_calc += _timestamp;
    }

10-01 14:57