public class RtpHead {
// public char cc;
// public char x;
// public char p;
// public char v;
//
// public char pt;
// public char m;
public char h1;
public char h2;
public short sequence_number; //short用int表示,超过范围时置为0重新计数
public int timestamp;
public int ssrc;
public void init_rtp_header(){
// this.v = 2;
// this.p = 0;
// this.x = 0;
// this.cc = 0;
// this.m = 0;
// this.pt = 33;
h1 = 128;
h2 = 33;
this.sequence_number = 123;
this.timestamp = 123;
this.ssrc = 123;
}
public RtpHead(){
init_rtp_header();
}
public void sequence_number_increase(){
this.sequence_number++;
}
//offset 4 8
public static int bytes2int(byte[]src, int offset){
int value;
value = (int) ( ( src[offset] & 0xff) | ((src[offset+1] & 0xff)<<8) | ((src[offset+2] & 0xff)<<16) | ((src[offset+3] & 0xff)<<24 ) ) ;
return value;
}
//offset 2
public static int bytes2short(byte[]src, int offset){
int value;
value = (int) ( ( src[offset] & 0xff) | ((src[offset+1] & 0xff)<<8)) ;
return value;
}
//offset 2
public static void short2byte(byte[]src, int offset, short i){
src[offset] = (byte) (0xff & i);
src[offset+1] = (byte) ((0xff00 & i)>>8);
}
//offset 4 8
public static void int2byte(byte[]src, int offset, int i){
src[offset] = (byte) (0xff & i);
src[offset+1] = (byte) ((0xff00 & i)>>8);
src[offset+2] = (byte) ((0xff0000 & i)>>16);
src[offset+3] = (byte) ((0xff000000 & i)>>24);
}
//offset 2
public static void short2char(char[]src, int offset, short i){
src[offset] = (char) (0xff & i);
src[offset+1] = (char) ((0xff00 & i)>>8);
}
//offset 4 8
public static void int2char(char[]src, int offset, int i){
src[offset] = (char) (0xff & i);
src[offset+1] = (char) ((0xff00 & i)>>8);
src[offset+2] = (char) ((0xff0000 & i)>>16);
src[offset+3] = (char) ((0xff000000 & i)>>24);
}
//低位在前,高位在后 //short用int表示,超过范围时置为0重新计数
public static void seq_num_inc(char[]src){
// char l = src[2];
// char h = src[3];
// short value = (short) ( ( src[2] & 0xff) | ((src[3] & 0xff)<<8)) ;
// value++;
//
// src[2] = (char) (0xff & value);
// src[3] = (char) ((0xff00 & value)>>8);
int value = (int) ( ( src[2] & 0xff) | ((src[3] & 0xff)<<8)) ;
value++;
if(value == 65536){
value = 0; //unsigned short
}
src[2] = (char) (0xff & value);
src[3] = (char) ((0xff00 & value)>>8);
}
public static void seq_num_inc4byte(byte[]src){
// int value = (int) ( ( src[2] & 0xff) | ((src[3] & 0xff)<<8)) ;
// value++;
// if(value == 65536){
// value = 0; //unsigned short
// }
// src[2] = (byte) (0xff & value);
// src[3] = (byte) ((0xff00 & value)>>8);
int x1 = src[2] & 0xff; //低位
int x2 = src[3] & 0xff; //高位
int value = (x1<<8) | x2;
value++;
if(value == 65536){
value = 0; //unsigned short
}
src[2] = (byte) ((0xff00 & value)>>8);
src[3] = (byte) (0xff & value);
}
public static void initBytes(byte[]src, RtpHead rtpHead){
System.out.println("xxxxxxxx");
// char src[] = new char[RTPTest.MTU];
src[0] = (byte) rtpHead.h1;
src[1] = (byte) rtpHead.h2;
System.out.println("rtphead h1 " + Integer.toBinaryString(src[0])); //
System.out.println("rtphead h2 " + Integer.toBinaryString(src[1])); //
RtpHead.short2byte(src, 2, rtpHead.sequence_number);
System.out.println("rtphead sequence_number 1 " + (int)src[2]); //
System.out.println("rtphead sequence_number 2 " + src[3]); //
RtpHead.int2byte(src, 4, rtpHead.timestamp);
System.out.println("rtphead timestamp 1 " + src[4]); //
System.out.println("rtphead timestamp 2 " + src[5]); //
System.out.println("rtphead timestamp 3 " + src[6]); //
System.out.println("rtphead timestamp 4 " + src[7]); //
RtpHead.int2byte(src, 8, rtpHead.ssrc);
System.out.println("rtphead ssrc 1 " + src[8]); //
System.out.println("rtphead ssrc 2 " + src[9]); //
System.out.println("rtphead ssrc 3 " + src[10]); //
System.out.println("rtphead ssrc 4 " + src[11]); //
}
public static void initChars(char[]src, RtpHead rtpHead){
System.out.println("xxxxxxxx");
// char src[] = new char[RTPTest.MTU];
src[0] = rtpHead.h1;
src[1] = rtpHead.h2;
System.out.println("rtphead h1 " + Integer.toBinaryString(src[0])); //
System.out.println("rtphead h2 " + Integer.toBinaryString(src[1])); //
RtpHead.short2char(src, 2, rtpHead.sequence_number);
System.out.println("rtphead sequence_number 1 " + (int)src[2]); //
System.out.println("rtphead sequence_number 2 " + src[3]); //
RtpHead.int2char(src, 4, rtpHead.timestamp);
System.out.println("rtphead timestamp 1 " + src[4]); //
System.out.println("rtphead timestamp 2 " + src[5]); //
System.out.println("rtphead timestamp 3 " + src[6]); //
System.out.println("rtphead timestamp 4 " + src[7]); //
RtpHead.int2char(src, 8, rtpHead.ssrc);
System.out.println("rtphead ssrc 1 " + src[8]); //
System.out.println("rtphead ssrc 2 " + src[9]); //
System.out.println("rtphead ssrc 3 " + src[10]); //
System.out.println("rtphead ssrc 4 " + src[11]); //
}
public static void main(String[] args){
RtpHead rtpHead = new RtpHead();
// rtpHead.cc = 255;
byte[] temp = new byte[4];
System.out.println("rtphead h1 " + Integer.toBinaryString(rtpHead.h1)); //rtpHead.cc
System.out.println("rtphead h2 " + Integer.toBinaryString(rtpHead.h2)); //
RtpHead.short2byte(temp, 0, rtpHead.sequence_number);
System.out.println("rtphead sequence_number 1 " + temp[0]); //
System.out.println("rtphead sequence_number 2 " + temp[1]); //
RtpHead.int2byte(temp, 0, rtpHead.timestamp);
System.out.println("rtphead timestamp 1 " + temp[0]); //
System.out.println("rtphead timestamp 2 " + temp[1]); //
System.out.println("rtphead timestamp 3 " + temp[2]); //
System.out.println("rtphead timestamp 4 " + temp[3]); //
RtpHead.int2byte(temp, 0, rtpHead.ssrc);
System.out.println("rtphead ssrc 1 " + temp[0]); //
System.out.println("rtphead ssrc 2 " + temp[1]); //
System.out.println("rtphead ssrc 3 " + temp[2]); //
System.out.println("rtphead ssrc 4 " + temp[3]); //
////////////////////////////////////////////////////
////////////////////////////////////////////////////
char src[] = new char[RTPTest.MTU];
RtpHead.initChars(src, rtpHead);
////////////////////////////////////////////////////
for(int i=0; i<10; i++){
RtpHead.seq_num_inc(src);
System.out.println("rtphead sequence_number 1 " + (int)src[2]); //
System.out.println("rtphead sequence_number 2 " + src[3]); //
}
}
}