通過RTMP play分析FLV格式詳解


最近做了一個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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM