最近做了一個(gè)rtmp中轉(zhuǎn)服務(wù)程序,通過(guò)實(shí)踐,熟悉rtmp play和push中各類格式,這里總結(jié)一下。

程序github地址: https://github.com/runner365/rtmp_relay
rtmp play接收?qǐng)?bào)文分析
第一幀收到的報(bào)文:

1) 0x46 4c 56:可參考文后:參考一
字符FLV頭
2) 0x01 05
Version TypeFlagsReserved TypeFlagsAudio TypeFlagsReserved TypeFlagsVideo
這個(gè)解析的時(shí)候,一般不用管
3)0x00 00 00 09
FLV header offset: 也就是從開頭9字節(jié)后,才是FLV真正的報(bào)文頭。
4)0x00 00 00 00
這個(gè)是第1幀的PreviousTagSize0(前幀長(zhǎng)度),因?yàn)槭堑谝粠钥隙ㄊ?;
5)0x08 可參考文后:參考二,參考三
幀開頭第一字節(jié):0x08表示音頻,0x09表示視頻
6)0x00 00 04
幀payload長(zhǎng)度:因?yàn)橐纛l第一幀是ASC,所以只有4字節(jié)。
7) 0x 00 00 00 00
timestamp,時(shí)間戳
8) 0x 00 00 00
streamid,流ID
9) 0x AF 00 13 90
音頻payload: 0xaf00開頭的后面是asc flag, 0xaf01開頭的后面是真正的音頻數(shù)據(jù)
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的長(zhǎng)度會(huì)變成4;
y字符: sample rate, 采樣率, 7表示22050采樣率
z字符: 通道數(shù),2是雙通道
10) 0x 00 00 00 0F
這個(gè)還是PreviousTagSize1,上一幀長(zhǎng)度15bytes
11) 0x09 視頻類型,新的一幀
12)0x00 00 22
視頻幀payload長(zhǎng)度
13) 0x00 00 0a 00
時(shí)間戳:這個(gè)地方有個(gè)大坑,順序是:a[3] a[0] a[1] a[2],最后一位是高位。
14) 0x00 00 00
streamid, 流id。
15) 0x 17 00
視頻幀開頭2字節(jié):
0x17 00: 表示內(nèi)容是SPS和PPS
0x17 01: 表示內(nèi)容是I-FRAME
0x27: 表示內(nèi)容是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 ; .實(shí)@??.刯...h?
0000004bh: 3C 80 ; <€
第12, 13字節(jié): 0x00 0E是spslen,也就是14字節(jié)長(zhǎng)度
跳過(guò)14字節(jié)后,0x01是pps開始的標(biāo)識(shí),跳過(guò)它。
0x00 04是ppslen,也就是4個(gè)字節(jié),最后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個(gè)字節(jié)為固定的‘F’‘L’‘V’,用來(lái)標(biāo)識(shí)這個(gè)文件是flv格式的.在做格式探測(cè)的時(shí)候,
如果發(fā)現(xiàn)前3個(gè)字節(jié)為“FLV”,就認(rèn)為它是flv文件.
Version: 第4個(gè)字節(jié)表示flv版本號(hào).
Flags: 第5個(gè)字節(jié)中的第0位和第2位,分別表示 video 與 audio 存在的情況.(1表示存在,0表示不存在)
DataOffset : 最后4個(gè)字節(jié)表示FLV header 長(zhǎng)度.
2,F(xiàn)LV 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構(gòu)成.back-pointers就是4個(gè)字節(jié)數(shù)據(jù),表示前一個(gè)tag的size.
3,F(xiàn)LV body細(xì)節(jié)
| 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 |
| 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 |
網(wǎng)站名稱:通過(guò)RTMPplay分析FLV格式詳解-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://chinadenli.net/article20/gcsjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、關(guān)鍵詞優(yōu)化、移動(dòng)網(wǎng)站建設(shè)、面包屑導(dǎo)航、品牌網(wǎng)站制作、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容