简体   繁体   中英

RTP sequence extract

A RTP packet consists of a 12-byte RTP header and subsequent RTP payload The 3rd and 4th byte of the header contain the Most-Significant-Byte and Least-Significant-Byte of the sequence number of the RTP packet Seq Num= (MSB<<8)+LSB

char pszPacket[12];

...

long lSeq = ???? - How to get the sequence number from a packet?

unsigned short seq = (packet[2] << 8) | packet[3];

肯定就是“ long lSeq =(unsigned char)(pszPacket [2] << 8)|(unsigned char)pszPacket [3];”吗?

If you need a proper implementation for that:

typedef struct _RTPHeader
{
  //first byte
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
  unsigned int         CC:4;        /* CC field */
  unsigned int         X:1;         /* X field */
  unsigned int         P:1;         /* padding flag */
  unsigned int         version:2;
#elif G_BYTE_ORDER == G_BIG_ENDIAN
  unsigned int         version:2;
  unsigned int         P:1;         /* padding flag */
  unsigned int         X:1;         /* X field */
  unsigned int         CC:4;        /* CC field*/
#else
#error "G_BYTE_ORDER should be big or little endian."
#endif
  //second byte
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
  unsigned int         PT:7;     /* PT field */
  unsigned int         M:1;       /* M field */
#elif G_BYTE_ORDER == G_BIG_ENDIAN
  unsigned int         M:1;         /* M field */
  unsigned int         PT:7;       /* PT field */
#else
#error "G_BYTE_ORDER should be big or little endian."
#endif
  guint16              seq_num;      /* length of the recovery */
  guint32              TS;                   /* Timestamp */
  guint32              ssrc;
} RTPHeader; //12 bytes

And what you can do is:

char pszPacket[12];
RTPHeader* myRTPPacket = (RTPHeader*) pszPacket;
printf("Sequence number is: %hu", myRTPPacket->seq_num;
unsigned __int16 seq = _rotr16( *( unsigned __int16* )&packet[2] , 8 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM