当前位置:网站首页>Live HLS protocol

Live HLS protocol

2022-07-01 19:34:00 lcyw

HTTP Live Streaming( abbreviation HLS) It's based on HTTP Streaming media network transmission protocol .​ It's apple QuickTime X and iPhone Part of a software system . Its working principle is to divide the whole flow into small ones based on HTTP To download , Download only a few at a time . When the media stream is playing , Clients can choose to download the same resources at different rates from many different alternate sources , Allow streaming sessions to adapt to different data rates .

When starting a streaming session , The client will download a extended M3U (m3u8) file , Used to find available media streams .

HLS Just ask for basic HTTP message ,HLS Can go through any permit HTTP Data through firewalls or proxy servers .​ It's also easy to use content distribution networks to stream media .

HLS protocol

HLS Provisions of the agreement

  • The encapsulation format of video is TS.
  • The video is encoded in H264, The audio encoding format is MP3、AAC perhaps AC-3.
  • except TS The video file itself , It also defines the m3u8 file ( text file ).

Client playback HLS The logic of video streaming is actually very simple , Download level 1 first Index file, It records the secondary index file (Alternate-A、Alternate-B、Alternate-C) The address of , Then the client downloads the secondary index file , The secondary index file records TS Download address of the file , So clients can download in order TS Video file and play continuously .

Class A index file

HLS Is to provide a m3u8 Address ,Apple Of Safari The browser can be opened directly m3u8 Address , for example :

https://dco4urblvsasc.cloudfront.net/811/81095_ywfZjAuP/game/index.m3u8
 
Android It can't be opened directly , Need to use html5 Of video label , Then open this page in the browser , for example :

```css
<!-- livestream.html -->
<video width="640" height="360"
        autoplay controls autobuffer 
        src="https://dco4urblvsasc.cloudfront.net/811/81095_ywfZjAuP/game/index.m3u8"
        type="application/vnd.apple.mpegurl">
</video> 

as well as m3u8 After the file is opened, the contents are as follows :

#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1064000
1000kbps.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=564000
500kbps.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=282000
250kbps.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2128000
2000kbps.m3u8

bandwidth Specifies the bit rate of the video stream ,
PROGRAM-ID Useless without attention ,
every last #EXT-X-STREAM-INF The next line on the list is level two index Path to file , You can use relative path or absolute path . In the example, the relative path is used . This file records the secondary data of video streams with different bit rates index File path , The client can judge its current network bandwidth by itself , To decide which video stream to play . You can also smoothly switch to the video stream matching the bandwidth when the network bandwidth changes .

second level index file

#EXTM3U
#EXT-X-PLAYLIST-TYPE:VOD
#EXT-X-TARGETDURATION:10
#EXTINF:10,
2000kbps-00001.ts
#EXTINF:10,
2000kbps-00002.ts
#EXTINF:10,
2000kbps-00003.ts
#EXTINF:10,
... ...
#EXTINF:10,
2000kbps-00099.ts
#EXTINF:10,
2000kbps-00100.ts
#ZEN-TOTAL-DURATION:999.66667
#ZEN-AVERAGE-BANDWIDTH:2190954
#ZEN-MAXIMUM-BANDWIDTH:3536205
#EXT-X-ENDLIST

Level II documents are actually responsible for giving ts Download address of the file , Relative paths are also used here .

  • EXTM3U
    Every M3U The first line of the document must be this tag, Provide marking function

  • EXT-X-PLAYLIST-TYPE
    #EXT-X-PLAYLIST-TYPE:VOD The current video stream is an on-demand stream , That is to say, the whole of the video ts The file has been generated ,

  • EXT-X-VERSION
    Used to mark the protocol version . Here is 3, So what's used here is HLS The third version of the agreement , This label can only have 0 or 1 individual , Not writing means using version 1

  • EXT-X-TARGETDURATION
    #EXT-X-TARGETDURATION Specifies the maximum duration of the slice file in the current video stream , That is to say, these ts The length of slicing cannot be greater than #EXT-X-TARGETDURATION Value .

  • EXT-X-MEDIA-SEQUENCE
    The start sequence number of the slice . Each slice has a unique serial number , Serial number between adjacent +1. This number will continue to grow , Ensure the continuity of flow .

  • EXTINF
    ts Actual length of slicing .duration : Media duration

	#EXTINF <duration>,<title>
  • EXT-X-ENDLIST
    #EXT-X-ENDLIST End of file symbol , Indicates the end of the video , No more media files are added to the playlist file. This flag also indicates that the current stream is a non live stream .

Play mode

on demand VOD The feature of is that you can get all the information at the current time point index Document and ts file , second level index All of them are recorded in the file ts File address . This client mode allows all content to be accessed . In the above example, it is an on-demand mode m3u8 Structure .

Live Pattern It's real-time generation M3u8 and ts file . Its index file has been in dynamic change , When playing, you need to constantly download secondary index file , To get the latest generated ts File play video . If a secondary index There is no... At the end of the file #EXT-X-ENDLIST sign , That means it's a Live Video streaming .

The client is playing VOD In fact, you only need to download level 1 video once index Documents and secondary index You can get all the files ts Download address of the file , Unless the client performs bit rate switching , Otherwise, there is no need to download any more index file , Just download in sequence ts File and play it . however Live Mode is slightly different , Because while playing , new ts Files are also being generated , So the client is actually downloading a secondary index file , Then download ts file , Download Level 2 index file ( At this time, this secondary index The file has been rewritten , The newly generated ts Download address of the file ), Download new ts file , Play it over and over again .

HLS Main application scenarios

Cross platform :PC The main live broadcast scheme is RTMP, There are also some libraries that can play HLS, for example jwplayer, be based on osmf Of hls There are also a lot of plug-ins . So in fact, if you choose a protocol, you can cross PC/Android/IOS, That's it HLS.

IOS Demanding stability requirements on :IOS Of course, the most stable is HLS, The stability is not worse than RTMP stay PC-flash Performance on .

Amicable CDN Distribution method : at present CDN about RTMP It's also the basic agreement , however HLS The basis of distribution is HTTP, therefore CDN Access and distribution will be better than RTMP More perfect . It can be used in various fields CDN Switch between ,RTMP Can also , It's just that docking tests may be required .

Simple :HLS As a streaming media protocol, it is very simple ,apple The support is also very perfect .Android Yes HLS Our support will become more and more perfect . as for DASH/HDS, There seems to be no special reason , It's like linux Has become popular and open , Other systems are difficult to be widely used .

HLS And RTMP

RTMP finger Adobe Of RTMP(Realtime Message Protocol), It is widely used in low delay live broadcast , It is also the actual standard protocol for the connection between encoder and server , stay PC(Flash) It has the best viewing experience and the best stability .

HLS Mainly to solve RTMP There are some problems with the agreement . such as RTMP The protocol does not use standard HTTP The interface transmits data , So in some special network environment, it may be blocked by firewall . however HLS Because of the HTTP Protocol transfer data

RTMP There is a state of agreement , It's hard to scale the video server smoothly , Because it is necessary to maintain the status for each client playing the video stream . and HLS Based on stateless protocol (HTTP), The client just downloads the common data stored in the server in order TS file , Being responsible and balanced is like ordinary HTTP File server load balancing is as simple as .

Besides ,HLS The protocol itself realizes rate adaptation , Different bandwidth devices can automatically switch to the most suitable video playback rate .

but HLS There are also some problems , Such as HLS The video delay of the protocol is large , Is basically 10 More than seconds , and RTMP The delay of the agreement can be up to 3、4 About seconds .

TS Protocol analysis part I (PAT)
TS Protocol analysis part II (PMT)
TS Protocol analysis part III (PES)
TS Protocol analysis part IV (adaptation field)

原网站

版权声明
本文为[lcyw]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207011752508074.html