rooltex rooltex -4 years ago 226
C++ Question

How to handle timestamp in RTMP?

I'm parse RTMP stream from wireshark capture and write media data to .flv file. I know how to calculate timestamp but a how can i handle timestamp when it overflow?

Answer Source

So i solved problem this way : 1) Parse RTMP header( read 4 bytes from the beginning of timestamp ) and get timestamp( u_int 32 ) ;

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

2) Calculate timestamp for .flv file ( timestamp in .flv file is always 32 bit for example 0x00000100 is simple timestamp and equal 1 and 0xffffff01 is extended timestamp and equal 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;
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download