RTMP学习笔记(一) Basic Header说明与解析

129 阅读1分钟

欢迎关注公众号:冒泡的肥皂

7be186a5c42b62ef234fab298155547.jpg

1.数据快结构

image.png

2.Basic Header说明

basic heaher占用的字节大小数是不固定的,大小有1个或2个或3个字节;

下面是数据结构的描述: chunk type(即fmt)占2bit, chunk stream id一般被简写为CSID

image.png

3 .Basic Header解析

public class RtmpHeader {
  int csid;
  int fmt;
}

private RtmpHeader readHeader(ByteBuf in) {
    RtmpHeader rtmpHeader = new RtmpHeader();
    //最短一个字节
    int headerLength = 1;
    
    //读取一个字节
    byte firstByte = in.readByte();
    
    //向右移6位保留两位  
    int fmt = (firstByte & 0xff) >> 6;
    
    //开头两位置为0 计算后面的6位
    int csid = (firstByte & 0x3f);

    if (csid == 0) {//两个字节大小计算
      csid = in.readByte() & 0xff + 64;
      headerLength += 1;
    } else if (csid == 1) {//三个字节大小结算
      byte secondByte = in.readByte();
      byte thirdByte = in.readByte();
      csid = (thirdByte & 0xff) << 8 + (secondByte & 0xff) + 64;
      headerLength += 2;
    } else if (csid >= 2) {//一个字节的
    }
    
    rtmpHeader.setCsid(csid);
    rtmpHeader.setFmt(fmt);