• In the last article we implemented the live streaming service, but there are some details that need to be worked out

Let’s talk about three common protocols for live streaming

  • There are several common live broadcast protocols in China: RTMP, HLS and HTTP-FLV. Let’s introduce them one by one.

RTMP is a Real Time Messaging Protocol.

Adobe’s proprietary protocol for transferring audio and video data between Flash players and servers. A plaintext protocol that works over TCP and uses port 1935 by default. The basic data unit in the protocol is called a Message, which is divided into smaller Chunk units during transmission. Finally, the segmented message block is transmitted through TCP protocol, and the receiving end decomparts the received message block to restore the streaming media data.

  • RTMP has the following advantages: RTMP is a protocol developed for streaming media, the underlying optimization is better than other protocols, and it has good support for Adobe Flash, almost all encoders (such as cameras) support RTMP output. Now the PC market is huge, the PC is mainly Windows, the Windows browser basically supports Flash. In addition, RTMP is suitable for a long time playback, there has been a test, contact 1 million seconds, that is, more than 10 days of continuous playback without problems. Finally, the delay of RTMP is relatively low, generally between 1-3s. General video conference and interactive live broadcast are sufficient.

  • Of course, RTMP is not perfect, and it has its drawbacks. On the one hand, it is based on TCP transmission, non-public port, may be blocked by the firewall; On the other hand, RTMP is an Adobe proprietary protocol, which cannot be played on many devices, especially on iOS, and requires a third-party decoder to play.

Flash Video (FLV) is another Video format developed by Adobe. It is a storage container format for streaming media data transmitted over the network.

The format is relatively simple and lightweight, and does not require large media headers. The FLV consists of The FLV Header, The FLV Body, and other tags. So the loading speed is extremely fast. Files encapsulated in FLV format are.flv.

  • Http-flv relies on the characteristics of MIME to select the corresponding program to process the corresponding Content according to the content-Type in the protocol, so that the streaming media can be transmitted through HTTP. Compared with RTMP, HTTP-FLV can penetrate firewalls better. It is based on HTTP/80 and effectively avoids being intercepted by firewalls. In addition, it can flexibly schedule and load balance through HTTP 302 jump, supports HTTPS encrypted transmission, and is compatible with Android and iOS mobile terminals.
  • The disadvantage of HTTP-FLV is that it can cache streaming media resources on the local client due to its transport nature, which is not good enough in terms of confidentiality. Because of the large network traffic, it is not suitable for pull flow protocol.

HLS (HTTP Live Streaming) is apple’s HTTP-based Streaming protocol.

It is mainly applied to iOS devices, including (iPhone, iPad, iPod Touch) and Mac OSX to provide audio and video live services and recorded content (on-demand) and other services.

  • The biggest difference between HLS and common streaming protocols is that it does not request a complete data stream. It will slice the streaming media data into continuous short TS files on the server side and access the TS files sequentially through the M3U8 index file. As long as the client does not stop in order to play the file obtained from the server, so as to achieve the playback of audio and video.

conclusion

Http-flv uses A long HTTP connection similar to RTMP streaming, which needs to be distributed by a specific streaming server, taking into account the advantages of both. And streaming protocols that can reuse existing HTTP distribution resources. Its real-time performance is equal to that of RTMP, and compared with RTMP, it saves part of the protocol interaction time, has a shorter first screen time, and has more extensible functions. As a live broadcast protocol proposed by Apple, HLS occupies an unshakeable position in iOS terminal, and Android terminal also provides corresponding support.

How to access vm IP on LAN?

  • After the bridge mode is changed, shut down and restart the service

Ob configuration (skipped, application-level software, go see for yourself)

If you want to use FFMPEG push live

  • Camera that identifies the current system

./ffmpeg -f avfoundation -list_devices true -i ""

  • Open the camera and start pushing

./ffmpeg -f avfoundation -framerate 30 -s 1280x720 -i "0" -vcodec libx264 -an -preset veryfast -f flv RTMP: / / 172.16.131.131 / myapp/m

How to push the recorded video

zhangguofu@localhost bin $ ./ffmpeg -re -i .. Copy/file/ylm. FLV – c – f FLV RTMP: / / 192.168.8.141 / myapp/m related command to explain

  • “-re” : reads input at the video frame rate
  • “-c copy” : Output stream uses the same codec as input stream
  • “-f FLV “: indicates that the output stream encapsulation format is FLV

How does the server save streaming media

  • There is a record function under the RMTP module, which is used to save streaming media data
application myapp { live on; gop_cache on; # open GOP cache, reduce first screen wait time recorder rec1{record all; record_unique on; record_path /tmp/record; record_suffix -%Y-%m-%d-%H_%M_%S.flv; # cache file name and format}}Copy the code
  • Do the following to stream a video

./ffmpeg -re -i .. Copy/file/ylm. FLV - c - f FLV RTMP: / / 172.16.131.131 / myapp/m

  • Checking whether the storage device is stored

  • So that’s the end of the tutorial, but there’s definitely more to live streaming than that, like filters for live streaming, like Control controllers for HTTP modules,
  • A little bit more about the Control controller

It can control the RTMP module externally via the HTTP protocol. With the Control controller, we can use the record, DROP and Redirect commands to implement our business scenarios.

  • You can use the record command to record and stop live streams.
  • Rtmp_stat is a stream statistics module. Configured in the HTTP module, rtmp_stat can monitor the status of streaming media in real time by URL.
  • You can selectively kick out a push or pull user by using the drop command.
  • You can use the redirect command to selectively redirect a push or pull user.

This is the end of the demo! Thank you for watching