HLS is the abbreviation of HTTP Live Streaming. It is an adaptive bit rate streaming media transmission protocol based on HTTP developed by Apple, and in 2009. HLS streaming media has become the most widely used real-time video protocol. It is a format that decomposes streams into small file-based segments, which can be downloaded via HTTP, and HLS can be via standard HTTP or proxy servers, which is different from UDP-based protocols (such as RTP). Since HLS is so popular now, what are its advantages and disadvantages?

advantage

  • Wide range of applications
  • First of all, as mentioned earlier, HLS is the most frequently used real-time video protocol. Although Apple originally designed it for its own ecology, such as IOS, Safari browser, etc., but backed by Apple, it has strong ecology and research and development capabilities, and it is now implemented in almost all browsers. Although current mainstream browsers support a similar standard, called MPEG DASH, but because Apple's Safari browser and IOS devices do not support it, I personally think HLS is a better choice.
  • Adaptive bit rate
  • Another huge advantage of HLS is that it allows the client to select a suitable stream from various quality streams based on the available bandwidth. HLS is broken down into small file segments of about 10 seconds. Through the decomposition, the client application only needs to buffer 10 seconds in advance. Save a lot of potential bandwidth for users.

shortcoming

  • Terrible delay
  • Although HLS is designed to efficiently handle multi-quality streams, it is not designed to transmit video quickly. In fact, HLS introduces a rather long delay in the stream, generally around 20 seconds or even longer.
  • Having said that, you may want to ask why? HLS requires three clips in the queue to allow playback, and the clips are divided by key frames in the video. The only way to create an ultra-low latency stream with HLS is to encode a video with a key frame every 250 milliseconds. The HLS playlist window will be four items in length, increasing the frequency of HTTP calls that are occurring and putting additional pressure on the server.
  • Unpublished
  • HLS is a protocol for users only. Unlike WebRTC, which has a specification released from a browser, HLS only supports streaming. If you want to publish a device's real-time video stream, you only need to look for other SDKs, such as Red5 Pro (the scene is relatively single and expensive), To create a publishing application that uses RTP, and then relay these streams through HLS for people to view in a browser.
  • There are several more mature audio and video SDKs in China, such as platforms such as Shengwang, which provide audio and video solutions in many scenarios.

HLS is a brief introduction. Next, I will demonstrate a small demo. Using FFmpeg, you can easily convert mp3 files to HLS format. It consists of multiple files, one of which contains metadata (.m3u8). The metadata tells the client from Where to get each data file and what is contained in the data file. The data file extension is .ts and usually contains 10 seconds of audio.

First prepare an mp3 file. Then install FFmpeg, install FFmpeg on the Mac, if the speed is very slow, you can try to switch the mirror.

1 brew install ffmpeg
2

After successful installation, enter the folder where the mp3 is located and execute the following instructions.

1 ffmpeg -i 江南.mp3 -c:a libmp3lame -b:a 128k -map 0:0 -f segment -segment_time 10 -segment_list outputlist.m3u8 -segment_format mpegts output%03d.ts
2 

After execution, you should see some results, roughly as follows.

1 output000.ts  output008.ts  output016.ts  output024.ts
2 output001.ts  output009.ts  output017.ts  output025.ts
3 output002.ts  output010.ts  output018.ts  output026.ts
4 output003.ts  output011.ts  output019.ts  outputlist.m3u8
5 output004.ts  output012.ts  output020.ts  江南.mp3
6 output005.ts  output013.ts  output021.ts
7 output006.ts  output014.ts  output022.ts
8 output007.ts  output015.ts  output023.ts
9

At this step you have completed the file format conversion, and then enter the Coding stage.

Project structure

img

code show as below

1 package main
2 
3 import (  
4   "fmt" 
5   "log" 
6   "net/http"
7 )
8
9 func main() {  
10  // 设置文件目录
11  const songsDir = "song"  
12  const port = 8888
13
14  http.Handle("/", http.FileServer(http.Dir(songsDir)))  
15  log.Printf("Serving %s on HTTP port: %v\n", songsDir, port)
16
17  log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
18 }
19

Run the code

1 go run main.go
2 

Open the browser address

1 http://127.0.0.1:8888/outputlist.m3u8
2

Congratulations, you can hear this mp3 moving melody. But if you want to do further commercial development or personal development, it is recommended to use a more mature solution. First, audio and video are not separated. Audio requirements are often accompanied by video requirements. Audio and video are often a sub-requirement in the development process. If you spend a lot of time on it, it will often delay the progress of the project, and there are often some pitfalls in the details when dealing with multiple platforms. Using SDKs on the market can greatly save development costs. The sound network I personally use can support cross-platform, the most important thing, and it can be a certain amount of free prostitution every month, which is very friendly to individual developers. Of course, the cost is actually one aspect, and a very important point is that it can be accessed in a few lines of code, which greatly reduces the amount of stepping on the hole!

Summarize

As you can see, HLS is widely used on various platforms, mobile terminals, and the general support of browsers, making it a good choice for developers to distribute streams to users. However, everything is possible, because HLS is a relatively slow protocol. Although Apple's rules seem to be quite strict, when it comes to IOS streaming media requirements, it is actually more flexible. If you want to use it to build some real-time communication programs, then it may not be suitable for you. Finally, if you want to implement a more real-time communication system, it is best to rely on other more mature solutions on the market, which are more suitable for scenarios where real-time requirements are not so high, and applications built under Apple's ecosystem.


RTE开发者社区
647 声望966 粉丝

RTE 开发者社区是聚焦实时互动领域的中立开发者社区。不止于纯粹的技术交流,我们相信开发者具备更加丰盈的个体价值。行业发展变革、开发者职涯发展、技术创业创新资源,我们将陪跑开发者,共享、共建、共成长。