通过RTMP play分析FLV格式详解

五迷三道 提交于 2020-03-23 12:17:07

最近做了一个rtmp中转服务程序,通过实践,熟悉rtmp play和push中各类格式,这里总结一下。

程序github地址: https://github.com/runner365/rtmp_relay

rtmp play接收报文分析

第一帧收到的报文:

 

1) 0x46 4c 56:可参考文后:参考一

字符FLV头

2) 0x01 05 

Version TypeFlagsReserved TypeFlagsAudio TypeFlagsReserved TypeFlagsVideo

这个解析的时候,一般不用管

3)0x00 00 00 09

FLV header offset: 也就是从开头9字节后,才是FLV真正的报文头。

4)0x00 00 00 00 

这个是第1帧的PreviousTagSize0(前帧长度),因为是第一帧,所以肯定是0;

5)0x08 可参考文后:参考二,参考三

帧开头第一字节:0x08表示音频,0x09表示视频

6)0x00 00 04

帧payload长度:因为音频第一帧是ASC,所以只有4字节。

7) 0x 00 00 00 00

timestamp,时间戳

8) 0x 00 00 00

streamid,流ID

9) 0x AF 00 13 90

音频payload: 0xaf00开头的后面是asc flag, 0xaf01开头的后面是真正的音频数据

0x13 90,也就是0b0001 0011 1001 0000, 

ASC flag格式:xxxx xyyy yzzz z000

x字符: aac type,类型2表示AAC-LC,5是SBR, 29是ps,5和29比较特殊ascflag的长度会变成4;

y字符:  sample rate, 采样率, 7表示22050采样率

z字符:  通道数,2是双通道

10) 0x 00 00 00 0F

这个还是PreviousTagSize1,上一帧长度15bytes

11) 0x09 视频类型,新的一帧

12)0x00 00 22

视频帧payload长度

13) 0x00 00 0a 00 

时间戳:这个地方有个大坑,顺序是:a[3] a[0] a[1] a[2],最后一位是最高位。

14) 0x00 00 00

streamid, 流id。

15) 0x 17 00 

视频帧开头2字节:

0x17 00: 表示内容是SPS和PPS

0x17 01: 表示内容是I-FRAME

0x27:      表示内容是P-FRAME

16) 

0000002bh: 17 00 00 00 00 01 42 C0 1F FF E1 00 0E 67 42 C0 ; ......B??.gB?
0000003bh: 1F 8C 8D 40 F0 28 90 0F 08 84 6A 01 00 04 68 CE ; .實@??.刯...h?
0000004bh: 3C 80 ; <€

第12, 13字节: 0x00 0E是spslen,也就是14字节长度

跳过14字节后,0x01是pps开始的标识,跳过它。

0x00 04是ppslen,也就是4个字节,最后0x68 ce 3c 80就是pps。

 

参考:

1, The FLV header

  Type Comment
Signature UI8 Signature byte always 'F' (0x46)
Signature UI8 Signature byte always 'L' (0x4C)
Signature UI8 Signature byte always 'V' (0x56)
Version UI8 File version (for example, 0x01 for FLV version 1)
TypeFlagsReserved UB [5] Shall be 0
TypeFlagsAudio UB [1] 1 = Audio tags are present
TypeFlagsReserved UB [1] Shall be 0
TypeFlagsVideo UB [1] 1 = Video tags are present
DataOffset UI32 The length of this header in bytes

Signature: FLV 文件的前3个字节为固定的‘F’‘L’‘V’,用来标识这个文件是flv格式的.在做格式探测的时候,

如果发现前3个字节为“FLV”,就认为它是flv文件.

Version: 第4个字节表示flv版本号.

Flags: 第5个字节中的第0位和第2位,分别表示 video 与 audio 存在的情况.(1表示存在,0表示不存在)

DataOffset : 最后4个字节表示FLV header 长度.

2,FLV body整体

Field Type Comment
PreviousTagSize0 UI32 Always 0
Tag1 FLVTAG First tag
PreviousTagSize1 UI32

Size of previous tag, including its header, in bytes. For FLV version1,

this value is 11 plus the DataSize of the previous tag.

Tag2 FLVTAG Second tag
... ... ...
PreviousTagSizeN-1 UI32 Size of second-to-last tag, including its header, in bytes.
TagN FLVTAG Last tag
PreviousTagSizeN UI32 Size of last tag, including its header, in bytes

FLV header之后,就是 FLV File Body.

FLV File Body是由一连串的back-pointers + tags构成.back-pointers就是4个字节数据,表示前一个tag的size.

3,FLV body细节

 

Field Type Comment
Reserved UB [2] Reserved for FMS, should be 0
Filter UB [1] Indicates if packets are filtered.
0 = No pre-processing required.
1 = Pre-processing (such as decryption) of the packet is
required before it can be rendered.
Shall be 0 in unencrypted files, and 1 for encrypted tags.
See Annex F. FLV Encryption for the use of filters.
TagType UB [5]

Type of contents in this tag. The following types are
defined:
8 = audio
9 = video
18 = script data

DataSize UI24 Length of the message. Number of bytes after StreamID to
end of tag (Equal to length of the tag – 11)
Timestamp UI24 Time in milliseconds at which the data in this tag applies.
This value is relative to the first tag in the FLV file, which
always has a timestamp of 0.
TimestampExtended UI8 Extension of the Timestamp field to form a SI32 value. This
field represents the upper 8 bits, while the previous
Timestamp field represents the lower 24 bits of the time in
milliseconds.
StreamID UI24 Always 0.
AudioTagHeader IF TagType == 8
AudioTagHeader
 
VideoTagHeader IF TagType == 9
VideoTagHeader
 
EncryptionHeader IF Filter == 1
EncryptionTagHeader
 
FilterParams IF Filter == 1
FilterParams
 
Data IF TagType == 8
AUDIODATA
IF TagType == 9
VIDEODATA
IF TagType == 18
SCRIPTDATA
Data specific for each media t
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!