Golang open source streaming media audio and video network transmission service - LAL

Posted by theperfectdrug on Sun, 20 Feb 2022 01:05:11 +0100

I Introduction to lal

lal is an open source live streaming media network transmission project, which is mainly composed of three parts:

  • lalserver: streaming media forwarding server. It is similar to nginx RTMP module and other services, but supports more protocols and provides richer functions.
  • demo: some small applications, such as push and pull flow clients, pressure measurement tools, flow analysis tools, scheduling sample programs, etc. Similar to ffmpeg, ffprobe and other applications.
  • pkg: streaming media protocol library. libavformat and other libraries similar to ffmpeg.

You can understand how lal is divided into the following function levels in the source code architecture:

lal github address: https://github.com/q191201771...

II lalserver quick start

1 compilation

Method 1: compile from the source code

$git clone https://github.com/q191201771/lal.git
$cd lal
$export GO111MODULE=on && export GOPROXY=https://goproxy.cn,https://goproxy.io,direct

Or compile using IDE such as GoLand.
If the Go compiler is not installed, refer to CentOS or macOS installation GoLang , the windows operating system can search the tutorial on the Internet by itself.

Method 2: directly download the compiled binary executable file

Click me to open the latest release page of github lal , you can download the lal binary executable file compiled on linux/macos/windows platform (in the form of zip package).

Method 3: using docker

Docker is divided into two ways. One is to download and run the compiled image directly from the Docker Hub:

$docker run -it -p 1935:1935 -p 8080:8080 -p 4433:4433 -p 5544:5544 -p 8083:8083 -p 8084:8084 -p 30000-30100:30000-30100/udp q191201771/lal /lal/bin/lalserver -c /lal/conf/lalserver.conf.json

The other is to generate an image and run it according to the local code and Dockerfile file:

$git clone https://github.com/q191201771/lal.git
$cd lal
$docker build -t lal .
$docker run -it -p 1935:1935 -p 8080:8080 -p 4433:4433 -p 5544:5544 -p 8083:8083 -p 8084:8084 -p 30000-30100:30000-30100/udp lal /lal/bin/lalserver -c /lal/conf/lalserver.conf.json

2 operation

$./bin/lalserver -c conf/lalserver.conf.json

3 experience function

After the lalserver service is started, you can push and pull the stream.

III Introduction to lalserver

lalserver is a streaming media forwarding service. Similar to nginx RTMP module services, simply put, the core function of these services is to forward the audio and video data sent by the streaming client to the corresponding streaming client.
However, lalserver supports more protocols and provides richer functions.

1. Lalserver features

  • [x] Full platform

    • [x] Support linux/macOS/windows multi system development, debugging and operation. Support multiple arch, such as amd64/arm64/arm32/ppc64le/mipsle/s390x
    • [x] Support cross compilation. Executable files of other platforms can be compiled on any platform
    • [x] No dependency. The generated executable file has no environment or library dependency, and can run independently of a single file
    • [x] (open source at the same time) provide the executable files of each platform, which can run directly without compilation
    • [x] Support docker
  • [x] High performance. Multi core multithreading extension
  • [x] Various live stream encapsulation protocols. Support RTMP/RTSP/HTTP-FLV/HTTP-TS/HLS, and support mutual conversion between different encapsulation protocols
  • [x] Multiple coding formats. Video supports H264/AVC, H265/HEVC, and audio supports AAC
  • [x] Recording in multiple formats. Support FLV, long MPEGTS and HLS recording (HLS live broadcasting and recording can be started at the same time)
  • [x] HTTPS. Support HTTPS-FLV, HTTPS-TS, HLS over HTTPS streaming
  • [x] WebSocket/WebSockets. Support websocket flv and websocket TS streaming
  • [x] HLS. Support real-time live broadcast and full list live broadcast. Slice files support multiple deletion methods. Support memory slicing
  • [x] RTSP. Support over TCP(interleaved mode). Support basic/digest auth verification. Support GET_PARAMETER. Compatible with various common H264/H265/AAC implementations
  • [x] RTMP. Fully support RTMP protocol, compatible with various common RTMP implementations. It supports adding mute audio data to single video and merging and sending
  • [x] HTTP API interface. It is used to obtain service information and send commands to the service.
  • [x] HTTP Notify event callback.
  • [x] Support multiple authentication methods
  • [x] Distributed cluster.
  • [x] Static pull back to the source. Configure back to source address through configuration file
  • [x] Static push to push. Support forwarding multiple addresses. Configure the forwarding address through the configuration file
  • [x] CORS cross domain. Support HTTP-FLV, HTTP-TS and HLS cross domain streaming
  • [x] HTTP file server. For example, HLS slice files can be played directly without additional HTTP file servers
  • [x] Listening port multiplexing. HTTP-FLV, HTTP-TS and HLS can use the same port. over HTTPS similar
  • [x] Play in seconds. GOP buffer

2. Protocols supported by lalserver

Support for conversion between encapsulation protocols

Transfer package typesub rtmpsub http[s]/websocket[s]-flvsub http[s]/websocket[s]-tssub hlssub rtsprelay push rtmp
pub rtmp
pub rtsp
relay pull rtmpX.

Support of each encapsulation protocol to coding protocol

Coding typertmprtsphlsflvmpegts

Type of recording file

Recording typehlsflvmpegts
pub rtmp
pub rtsp

The meaning of the table is shown in: session pub/sub/push/pull of connection type

Note that if only rtsp streams (specifically rtp packets) are forwarded to each other, it does not involve encapsulation into other formats. Theoretically, other coding types are also supported.

3 characteristic diagram of lalserver

lal github address: https://github.com/q191201771...

IV List of url addresses of push-pull streams of each protocol of lalserver

agreementurl addressProtocol standard port
RTMP streamingrtmp://
RTSP streamingrtsp://localhost:5544/live/test110554
RTMP pull flowrtmp://
HTTP-FLV streaminghttp:// (https address)
Websocket flv pull flowws://
wss:// (websockets address)
HLS(m3u8+ts) pull flowhttp:// (live address format 1) (live address format 2) (full volume recording and broadcasting address)
RTSP pull flowrtsp://localhost:5544/live/test110554
HTTP-TS streaminghttp:// (http address) (https address)
ws:// (websocket address)
wss:// (websockets address)

About ports

If the protocol standard port is used, the port in the address can be omitted. For example, the default port of HTTP is 80 become

If you are not familiar with how to use the push-pull stream client, you can refer to Summary of common push-pull stream client information

lal github address: https://github.com/q191201771...

V lalserver configuration file description

  "# doc of config": "https://pengrl. COM / LAL / # / configfbrief ", / /. The document description link corresponding to the configuration file has no practical use in the program
  "conf_version": "0.2.8",                                   //. the version number of the configuration file should not be manually modified by the business party, and the version will be checked in the program
                                                             //  Is the number consistent with that declared in the code
  "rtmp": {
    "enable": true,                      //. whether to enable rtmp service listening
                                         //  Note that the enable switch controlling each protocol type in the configuration file should be turned on as needed to avoid unnecessary protocol conversion overhead
    "addr": ":1935",                     //This is the port that the RTMP service listens to and the client pushes and pulls streams to the lalserver
    "gop_num": 0,                        //The number of GOP caches pulled by RTMP accelerates the stream opening time, but may increase the delay
                                         //. if 0, the cache is not used
    "merge_write_size": 0,               //Combine the small packet data for transmission, unit byte, to improve the server performance, but may cause jamming
                                         //  If it is 0, send without merging
    "add_dummy_audio_enable": false,     //. whether to enable the function of dynamic detection and adding silent AAC data
                                         //  If enabled, when rtmp pub pushes the stream, if it exceeds ` add_dummy_audio_wait_audio_ms ` time is still running out
                                         //  When audio data is received, AAC data will be automatically superimposed for this stream
    "add_dummy_audio_wait_audio_ms": 150 //. the unit is milliseconds. See ` add for details_ dummy_ audio_ enable`
  "default_http": {                       //. default configuration related to http listening. If the following configuration items are not separately configured in HLS, httpflv and httpts,
                                          //  Default is used_ Configuration in http
                                          //  Note that whether HLS, httpflv and httpts services are enabled or not is not determined here
    "http_listen_addr": ":8080",          //. HTTP listening address
    "https_listen_addr": ":4433",         //. HTTPS listening address
    "https_cert_file": "./conf/cert.pem", //. local cert file address of HTTPS
    "https_key_file": "./conf/key.pem"    //. local key file address of HTTPS
  "httpflv": {
    "enable": true,          //. whether to enable listening of HTTP-FLV service
    "enable_https": true,    //. whether to enable HTTPS-FLV listening
    "url_pattern": "/",      //. pull stream url routing path address. The default value is ` / `, which means unlimited. The routing address can be any path address.
                             //  If it is set to ` / live / ', the stream can only be pulled down from the ` / live /' path, such as ` / live / test110 flv`
    "gop_num": 0             //See RTMP gop_ num
  "hls": {
    "enable": true,                  //. whether to enable HLS service listening
    "enable_https": true,            //. whether to enable HTTPS-HLS listening
    "url_pattern": "/hls/",          //. streaming url routing address, default value ` / hls / `, corresponding HLS(m3u8) streaming url address:
                                     //  - `/hls/{streamName}.m3u8`
                                     //  - `/hls/{streamName}/playlist.m3u8`
                                     //  - `/hls/{streamName}/record.m3u8`
                                     //  playlist.m3u8 file corresponds to live hls, and only < fragment is saved in the list_ Num > ts file names, which will continue to increase
                                     //  Add the newly generated ts files and remove the expired ts files
                                     //  record.m3u8 file corresponds to recording hls. From the first ts file to the newly generated ts file will be saved in the list
                                     //  Continue to append the newly generated ts file
                                     //  The ts file address is noted as follows:
                                     //  - `/hls/{streamName}/{streamName}-{timestamp}-{index}.ts ` or
                                     //    `/hls/{streamName}-{timestamp}-{index}.ts`
                                     //  Notice the URL of the hls_ Pattern cannot match the URL of httpflv and httpts_ Same pattern
    "out_path": "./lal_record/hls/", //Root directory of hlu8. And HLS
    "fragment_duration_ms": 3000,    //. single TS file slice duration, in milliseconds
    "fragment_num": 6,               //. playlist. Number of ts files in m3u8 file list
    "delete_threshold": 6,           //. ts file deletion timing
                                     //  Note that only in the configuration item ` cleanup_ Used when mode ` is 2
                                     //  The meaning is to save only the most recent from playlist The number of ts files removed in m3u8. ts files expired earlier will be deleted
                                     //  If not, the default value is the configuration item ` fragment_ Value of num `
                                     //  Note that this value should not be less than 1 to avoid playing failure due to too fast deletion
    "cleanup_mode": 1,               //. HLS file cleanup mode:
                                     //  0 does not delete m3u8+ts files. It can be used for recording and other scenes
                                     //  1 delete the m3u8+ts file after the input stream ends
                                     //    Note that the exact deletion time point is after the end of streaming
                                     //    `fragment_duration_ms * (fragment_num + delete_threshold)`
                                     //    The deletion is delayed for a short period of time to avoid that the pull end of HLS has not been pulled out just after the end of the input stream
                                     //  2. In the process of streaming, the expired ts files are continuously deleted, and only the latest ones are retained
                                     //    `delete_threshold + fragment_num + 1`
                                     //    About ts files
                                     //    Also, after the input stream ends, the logic of cleaning mode 1 will also be executed
                                     //  Note that record M3u8 is generated only in 0 and 1 modes
    "use_memory_as_disk_flag": false //. whether to use memory instead of disk to save m3u8+ts files
                                     //  Note that you should pay attention to the memory capacity when using this mode. Generally speaking, it should not be combined with ` cleanup_mode ` is 0 or 1
  "httpts": {
    "enable": true,         //. whether to enable the listening of HTTP-TS service. Note that this is not the TS in HLS, but the TS stream is continuously transmitted over a long HTTP connection
    "enable_https": true,   //. whether to enable HTTPS-TS listening
    "url_pattern": "/"      //. pull stream url routing path address. The default value is ` / `, which means unlimited. The routing address can be any path address.
                            //  If it is set to ` / live / ', the stream can only be pulled down from the ` / live /' path, such as ` / live / test110 ts`
  "rtsp": {
    "enable": true, //. whether to enable rtsp service listening. At present, only rtsp streaming is supported
    "addr": ":5544" //. rtsp streaming address
  "record": {
    "enable_flv": true,                      //. turn on flv recording
    "flv_out_path": "./lal_record/flv/",     //. flv recording directory
    "enable_mpegts": true,                   //. whether to enable mpegts recording. Note that this is a long ts file recording, and hls recording is controlled by the hls configuration above
    "mpegts_out_path": "./lal_record/mpegts" //. mpegts recording directory
  "relay_push": {
    "enable": false, //Whether to turn on the relay forwarding function. After it is turned on, all streams received by itself will be forwarded
    "addr_list":[    //. the opposite end address of relay forwarding. Multiple addresses can be filled in for 1-to-n forwarding. Format example ""
  "relay_pull": {
    "enable": false, //. whether to turn on the function of pulling back to the source. When it is turned on, when it receives a streaming request and the stream does not exist, it will pull the stream from other servers to the local server
    "addr": ""       //. the address of the pull stream back to the source. Format example ""
  "http_api": {
    "enable": true, //. whether to open HTTP API interface
    "addr": ":8083" //2. Listening address
  "server_id": "1", //. the unique ID of the current lalserver. When multiple lalservers HTTP notify the same address, it can be distinguished by this ID
  "http_notify": {
    "enable": true,                                              //. whether to enable HTTP Notify event callback
    "update_interval_sec": 5,                                    //. update event callback interval, in milliseconds
    "on_server_start": " ", / /. HTTP Notify event callback address of each event
    "on_update": "",
    "on_pub_start": "",
    "on_pub_stop": "",
    "on_sub_start": "",
    "on_sub_stop": "",
    "on_rtmp_connect": ""
  "simple_auth": {                    // For authentication documents, see: https://pengrl.com/lal/#/auth
    "key": "q191201771",              // Private key, used when calculating md5 authentication parameters
    "dangerous_lal_secret": "pengrl", // Backdoor authentication parameter. All streams can be authenticated through this parameter value
    "pub_rtmp_enable": false,         // Whether authentication is enabled for rtmp streaming. true means authentication is enabled and false means authentication is not enabled
    "sub_rtmp_enable": false,         // Whether authentication is enabled for rtmp streaming
    "sub_httpflv_enable": false,      // Whether authentication is enabled for httpflv streaming
    "sub_httpts_enable": false,       // Whether authentication is enabled for httpts streaming
    "pub_rtsp_enable": false,         // Whether authentication is enabled for rtsp streaming
    "sub_rtsp_enable": false,         // Whether authentication is enabled for rtsp streaming
    "hls_m3u8_enable": true           // m3u8 pull whether authentication is enabled
  "pprof": {
    "enable": true, //. whether to enable the listening of Go pprof web Service
    "addr": ":8084" //. Go pprof web address
  "log": {
    "level": 1,                         //. log level, 0 trace, 1 debug, 2 info, 3 warn, 4 error, 5 fatal
    "filename": "./logs/lalserver.log", //. log output file
    "is_to_stdout": true,               //Whether to print the output to the sign console
    "is_rotate_daily": true,            //. log scrolling by day
    "short_file_flag": true,            //. whether the source file name and line number are carried at the end of the log
    "assert_behavior": 1                //Behavior of log assertion, 1 only print error log, 2 print and exit program, 3 print and panic
  "debug": {
    "log_group_interval_sec": 30,          // The interval between printing group debugging logs, in seconds. If it is 0, it will not print
    "log_group_max_group_num": 10,         // How many group s can you print at most
    "log_group_max_sub_num_per_group": 10  // How many sub session s can each group print at most

Vi Introduction to Demo

In the lal project, except /app/lalserver In addition to this core service, some small applications are also provided in the / app/demo directory. Function introduction:

|demo | description|
| - | - |
|Pushrtmp | RTMP streaming client; Pressure test tool|
|Pullrtmp | RTMP streaming client; Pressure test tool|
|pullrtmp2pushrtmp | pull the RTMP stream from the remote server and use RTMP to push it out. It supports 1-to-n push|
|pullrtmp2pushrtsp | pull the RTMP stream from the remote server and use RTSP to push it out|
|pullrtmp2hls | pull the RTMP stream from the remote server and store it as a local m3u8+ts file|
|Pullhttpflv | http-flv streaming client|
|Pullrtsp | RTSP streaming client|
|pullrtsp2pushrtsp | pull the RTSP stream from the remote server and use RTSP to push it out|

pullrtsp2pushrtmpPull the RTSP stream from the remote server and use RTMP to push it out
benchrtmpconnectConduct concurrent connection pressure test on rtmp
calcrtmpdelayTest the delay of rtmp server sending and receiving data
analyseflvPull the HTTP-FLV stream from the remote server and analyze it
dispatchSimply demonstrate how to implement a simple scheduling service so that multiple lalserver nodes can form a cluster
flvfile2esSeparate the local FLV file into ES stream files of H264/AVC and AAC
modflvfileModify some information of the flv file (such as the timestamp of some tag s) and save it as a file

(for more specific functions, refer to the header description of each source file)

VII Contact author

Welcome any technical and non-technical exchanges.

Topics: Go github server websocket