一個字節數組,對於一個項目我的工作,我實現RTPpacket,我必須填寫字節的頭陣列RTP報頭字段的一部分。灌裝在Java中
//size of the RTP header:
static int HEADER_SIZE = 12; // bytes
//Fields that compose the RTP header
public int Version; // 2 bits
public int Padding; // 1 bit
public int Extension; // 1 bit
public int CC; // 4 bits
public int Marker; // 1 bit
public int PayloadType; // 7 bits
public int SequenceNumber; // 16 bits
public int TimeStamp; // 32 bits
public int Ssrc; // 32 bits
//Bitstream of the RTP header
public byte[] header = new byte[ HEADER_SIZE ];
這是我的方法:
/*
* bits 0-1: Version
* bit 2: Padding
* bit 3: Extension
* bits 4-7: CC
*/
header[0] = new Integer((Version << 6)|(Padding << 5)|(Extension << 6)|CC).byteValue();
/*
* bit 0: Marker
* bits 1-7: PayloadType
*/
header[1] = new Integer((Marker << 7)|PayloadType).byteValue();
/* SequenceNumber takes 2 bytes = 16 bits */
header[2] = new Integer(SequenceNumber >> 8).byteValue();
header[3] = new Integer(SequenceNumber).byteValue();
/* TimeStamp takes 4 bytes = 32 bits */
for (int i = 0; i < 4; i++)
header[7-i] = new Integer(TimeStamp >> (8*i)).byteValue();
/* Ssrc takes 4 bytes = 32 bits */
for (int i = 0; i < 4; i++)
header[11-i] = new Integer(Ssrc >> (8*i)).byteValue();
任何其他的,也許 '更好' 的方式來做到這一點?
不要創建一個對象只是爲了從中提取一個基元。它醜陋而且效率低下。 – 2010-04-10 21:02:17