java, android实现可用的rtp封包解包h264

  介绍

这篇文章运用简单易懂的例子给大家介绍java, android实现可用的rtp封包解包h264,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

首先看看关键类:

包com.imsdk.socket.udp.codec;
  进口android.os.SystemClock;
  进口android.util.Log;
  
  进口java.io.ByteArrayInputStream;
  进口java.io.IOException;
  进口java.io.InputStream;
  进口java.math.BigDecimal;
  进口java.util.Random;
  进口java.util.concurrent.Semaphore;
  
  公开课RtspPacketEncode {
  私有静态最终字符串标签=癛tspPacketEncode";//- - - - - - - - - - - -视频转换数据监听- - - - - - - - - - - -
  公共接口H264ToRtpLinsener {
  空白h364ToRtpResponse (byte [], int len);
  }
  
  私人H264ToRtpLinsener h364ToRtpLinsener;//执行回调
  私人空间exceuteH264ToRtpLinsener (byte [], int len) {
  如果这一点。h364ToRtpLinsener !=null) {
  h364ToRtpLinsener。h364ToRtpResponse (, len);
  }
  }//- - - - - - -视频- - - - - - - - -
  私人int帧速率=10;
  私人byte [] sendbuf=新字节[1500];
  私人int packageSize=1400;
  私人int seq_num=0;
  私人int timestamp_increse=(int)(90000.0/帧速率);//帧速率是帧率
  私人int ts_current=0;
  私人int字节=0;//- - - - - - -视频结束- - - - - - - - - -
  
  公共RtspPacketEncode (H264ToRtpLinsener h364ToRtpLinsener) {
  这一点。h364ToRtpLinsener=h364ToRtpLinsener;
  }/* *
  *一帧一帧的RTP封包
  *
  * @param r
  * @return
  */公共空间h364ToRtp (byte [] r, int h364len){抛出异常
  
  CalculateUtil。memset (sendbuf 0 1500);
  sendbuf[1]=(字节)(sendbuf [1] | 96);//负载类型96号,其值为:01100000
  sendbuf[0]=(字节)(sendbuf [0] | 0 x80);//版本号,此版本固定为2
  sendbuf[1]=(字节)(sendbuf [1],254);//标志位,由具体协议规定其值,其值为:01100000
  sendbuf[11]=10;//随机指定10,并在本RTP回话中全局唯一,java默认采用网络字节序号不用转换(同源标识符的最后一个字节)
  如果(h364len & lt;=packageSize) {
  sendbuf[1]=(字节)(sendbuf [1] | 0 x80);//设置rtp M位为1时,其值为:11100000,分包的最后一片,M位(第一位)为0,后7位是十进制的96年,表示负载类型
  sendbuf[3]=(字节)seq_num + +;
  System.arraycopy (CalculateUtil.intToByte (seq_num + +), 0, sendbuf, 2, 2);//发送[2]和[3]发送为序列号,共两位
  {//java默认的网络字节序是大端字节序(无论在什么平台上),因为windows为小字节序,所以必须倒序/* *参考:
  * http://blog.csdn.net/u011068702/article/details/51857557
  * http://cpjsjxy.iteye.com/blog/1591261
  */字节temp=0;
  temp=sendbuf [3];
  sendbuf [3]=sendbuf [2];
  sendbuf [2]=temp;
  }//FU-A头,并将这个标题填入sendbuf [12]
  sendbuf[12]=(字节)(sendbuf[12] |((字节)(r [0],0 x80) & lt; & lt;7);
  sendbuf[12]=(字节)(sendbuf[12] |((字节)((r [0],0 x60)在祝辞5)& lt; & lt;5);
  sendbuf[12]=(字节)(sendbuf[12] |((字节)(r [0],0 x1f)));//同理将sendbuf[13]赋给nalu_payload//芬丫吹絪endbuf[12]中,接下来则存放的是最终的第一个字节之后的数据,所以从r的第二个字节开始复制
  系统。sendbuf arraycopy (r, 1日,13日,h364len - 1);
  ts_current=ts_current + timestamp_increse;
  System.arraycopy (CalculateUtil.intToByte (ts_current), 0, sendbuf, 4, 4);//序列号接下来是时间戳,4个字节,存储后也需要倒序
  {
  字节temp=0;
  temp=sendbuf [4];
  sendbuf [4]=sendbuf [7];
  sendbuf [7]=temp;
  temp=sendbuf [5];
  sendbuf [5]=sendbuf [6];
  sendbuf [6]=temp;
  }
  字节=h364len + 12;//获sendbuf的长度,为温州市的长度(包含温州市头但取出起始前缀,加上rtp_header固定长度12个字节)//客户端。发送(新DatagramPacket (sendbuf字节,addr,端口/* 9200 */));//发送(sendbuf字节);
  exceuteH264ToRtpLinsener (sendbuf字节);
  
  }else if (h364len比;packageSize) {
  int k=0 l=0;
  k=h364len/packageSize;
  l=h364len % packageSize;
  int t=0;
  ts_current=ts_current + timestamp_increse;
  System.arraycopy (CalculateUtil.intToByte (ts_current), 0, sendbuf, 4, 4);//时间戳,并且倒序
  {
  字节temp=0;
  temp=sendbuf [4];
  sendbuf [4]=sendbuf [7];
  sendbuf [7]=temp;
  temp=sendbuf [5];
  sendbuf [5]=sendbuf [6];
  sendbuf [6]=temp;
  }
  而(t & lt;=k) {
  System.arraycopy (CalculateUtil.intToByte (seq_num + +), 0, sendbuf, 2, 2);//序列号,并且倒序
  {
  字节temp=0;
  temp=sendbuf [3];
  sendbuf [3]=sendbuf [2];
  sendbuf [2]=temp;
  }
  如果(t==0){//分包的第一片
  sendbuf[1]=(字节)(sendbuf [1],0 x7f);//其值为:01100000,不是最后一片,M位(第一位)设为0//傅指标,一个字节,紧接在RTP报头之后,包括F,新名词,头
  sendbuf[12]=(字节)(sendbuf[12] |((字节)(r [0],0 x80) & lt; & lt;7);//禁止位,为0
  sendbuf[12]=(字节)(sendbuf[12] |((字节)((r [0],0 x60)在祝辞5)& lt; & lt;5);//新名词,表示包的重要性
  sendbuf[12]=(字节)(sendbuf[12] |(字节)(28);//类型,表示此FU-A包为什么类型,一般此处为28//傅头,一个字节,年代,E, R,类型
  sendbuf[13]=(字节)(sendbuf [13],0 xbf);//E=0,表示是否为最后一个包,是则为1
  sendbuf[13]=(字节)(sendbuf [13],0 xdf);//R=0,保留位,必须设置为0
  sendbuf[13]=(字节)(sendbuf [13] | 0 x80);//S=1,表示是否为第一个包,是则为1
  sendbuf[13]=(字节)(sendbuf[13] |((字节)(r [0],0 x1f)));//类型,即温州市头对应的类型//将除去温州市头剩下的温州市数据写入sendbuf的第14个字节之后,前14个字节包括:12字节的RTP报头,傅指示器,傅头
  系统。sendbuf arraycopy (r, 1日,14日,packageSize);//客户端。发送(新DatagramPacket (addr sendbuf packageSize + 14日,端口/* 9200 */));
  exceuteH264ToRtpLinsener (sendbuf packageSize + 14);
  t + +;
  }else if (t==k){//分片的最后一片
  sendbuf[1]=(字节)(sendbuf [1] | 0 x80);
  
  sendbuf[12]=(字节)(sendbuf[12] |((字节)(r [0],0 x80) & lt; & lt;7);
  sendbuf[12]=(字节)(sendbuf[12] |((字节)((r [0],0 x60)在祝辞5)& lt; & lt;5);
  sendbuf[12]=(字节)(sendbuf[12] |(字节)(28));
  
  sendbuf[13]=(字节)(sendbuf [13],0 xdf);//R=0,保留位必须设为0
  sendbuf[13]=(字节)(sendbuf [13],0 x7f);//S=0,不是第一个包
  sendbuf[13]=(字节)(sendbuf [13] | 0 x40);null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null

java, android实现可用的rtp封包解包h264