头图

1. Introduction to lal

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

  • lalserver: Streaming media forwarding server. Similar to services such as nginx-rtmp-module , but supports more protocols and provides richer functions.
  • demo: some small applications, such as push and pull stream clients, stress testing tools, stream analysis tools, scheduling sample programs, etc. Similar to ffmpeg, ffprobe and other applications.
  • pkg: Streaming media protocol library. Libraries like libavformat for ffmpeg.

The following is the lal source code architecture diagram, from which you can roughly understand how lal divides the functional level:

lal源码架构图

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

2. Lalserver Quick Start

1 Compile

method 1, self-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
$make

Or compile with an IDE such as GoLand.
If the Go compiler is not installed, you can refer to "Installing GoLang on CentOS or macOS" , the Windows operating system can search for tutorials on the Internet by itself.

method 2, directly download the compiled binary executable file

click me to open the "github lal latest release version page" , you can download the compiled lal binary executable file (zip compressed package) for the linux/macos/windows platform.

mode 3, using docker

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

$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 based on native code and Dockerfile and run:

$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 run

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

3 Experience features

After the lalserver service is started, push-pull streaming can be performed.

3. Introduction to lalserver

lalserver is a streaming media forwarding service. Similar to nginx-rtmp-module service, in simple terms, the core function of this type of service is to forward the audio and video data sent by the push client to the corresponding pull client.
But lalserver supports more protocols and provides richer functions.

1 lalserver features

  • [x] full platform

    • [x] Support linux/macOS/windows multi-system development, debugging and running. Support multiple arch such as amd64/arm64/arm32/ppc64le/mipsle/s390x
    • [x] Support cross-compilation. Executable files for other platforms can be compiled on any platform
    • [x] No dependencies. The generated executable file has no environment and library dependencies, and can be run independently by a single file
    • [x] (while open source) provides executable files for each platform, which can be run directly without compiling
    • [x] support docker
  • [x] high performance . Multi-core and multi-thread expansion
  • [x] Multiple live streaming encapsulation protocols . Support RTMP/RTSP/HTTP-FLV/HTTP-TS/HLS, support mutual conversion between different encapsulation protocols
  • [x] Multiple encoding formats . Video supports H264/AVC, H265/HEVC, audio supports AAC
  • [x] multiple formats. Support FLV, long MPEGTS, HLS recording (HLS live and recording can be turned on at the same time)
  • [x] HTTPS . Support HTTPS-FLV, HTTPS-TS, HLS over HTTPS pull streaming
  • [x] WebSocket/WebSockets . Support Websocket-FLV, WebSocket-TS pull stream
  • [x] HLS . Support real-time live broadcast and full list live broadcast. Slice files support multiple deletion methods. Support for memory slicing
  • [x] RTSP . Support over TCP (interleaved mode). Support basic/digest auth authentication. GET_PARAMETER supported. Compatible with various common H264/H265/AAC implementations
  • [x] RTMP . Full support for RTMP protocol, compatible with various common RTMP implementations. Support adding mute audio data to single video, support combined sending
  • [x] HTTP API interface . Used to obtain service information and send commands to the service.
  • [x] HTTP Notify event callback .
  • [x] supports multiple authentication
  • [x] distributed cluster .
  • [x] static pull back to source . Configure the back-to-source address through the configuration file
  • [x] static push retweet . Support for retweeting multiple addresses. Configure the retweet address through the configuration file
  • [x] CORS cross-domain . Support HTTP-FLV, HTTP-TS, HLS cross-domain pull streaming
  • [x] HTTP file server . For example, HLS slice files can be played directly without additional HTTP file server
  • [x] listening port reuse . HTTP-FLV, HTTP-TS, HLS can use the same port. over HTTPS similar
  • [x] seconds to play . GOP buffering

2 Protocols supported by lalserver

Support for conversion between encapsulation protocols

Repackage 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 encoding protocols by each encapsulation protocol

encoding typertmprtsphlsflvmpegts
aac
avc/h264
hevc/h265

Recording file type

recording typehlsflvmpegts
pub rtmp
pub rtsp

For the meaning of the table, see: session pub/sub/push/pull of connection type

Note that if only rtsp streams (to be precise, rtp packets) are forwarded to each other, it does not involve trans-encapsulation into other formats. In theory, other encoding types are also supported.

3 lalserver feature map

lal特性图

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

4. Lalserver push-pull stream url address list for each protocol

protocolurl addressProtocol standard port
RTMP push streamrtmp://127.0.0.1:1935/live/test1101935
RTSP push streamrtsp://localhost:5544/live/test110554
...
RTMP pull streamrtmp://127.0.0.1:1935/live/test1101935
HTTP-FLV pull streamhttp://127.0.0.1:8080/live/test110.flv
https://127.0.0.1:4433/live/test110.flv (https address)
80
443
WebSocket-FLV pull streamws://127.0.0.1:8080/live/test110.flv
wss://127.0.0.1:4433/live/test110.flv (websockets address)
80
443
HLS (m3u8+ts) pull streamhttp://127.0.0.1:8080/hls/test110.m3u8 (live address format 1)
http://127.0.0.1:8080/hls/test110/playlist.m3u8 (live broadcast address format 2)
http://127.0.0.1:8080/hls/test110/record.m3u8 (full recording address)
80
RTSP pull streamrtsp://localhost:5544/live/test110554
HTTP-TS pull streamhttp://127.0.0.1:8080/live/test110.ts (http address)
https://127.0.0.1:4433/live/test110.ts (https address)
ws://127.0.0.1:8080/live/test110.ts (websockets/sockets address)
wss://127.0.0.0. )
80
443
80
443

on port

If the standard port of the protocol is used, the port in the address can be omitted. For example, the default port of http is 80, then http://127.0.0.1:80/live/test110.flv becomes http://127.0.0.1/live/test110.flv

If you are not familiar with how to use push-pull streaming clients, please refer to Common Push-pull Streaming Client Information Summary

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

Five. lalserver configuration file description

{
  "# doc of config": "https://pengrl.com/lal/#/ConfigBrief", //. 配置文件对应的文档说明链接,在程序中没实际用途
  "conf_version": "0.2.8",                                   //. 配置文件版本号,业务方不应该手动修改,程序中会检查该版本
                                                             //  号是否与代码中声明的一致
  "rtmp": {
    "enable": true,                      //. 是否开启rtmp服务的监听
                                         //  注意,配置文件中控制各协议类型的enable开关都应该按需打开,避免造成不必要的协议转换的开销
    "addr": ":1935",                     //. RTMP服务监听的端口,客户端向lalserver推拉流都是这个地址
    "gop_num": 0,                        //. RTMP拉流的GOP缓存数量,加速流打开时间,但是可能增加延时
                                         //. 如果为0,则不使用缓存发送
    "merge_write_size": 0,               //. 将小包数据合并进行发送,单位字节,提高服务器性能,但是可能造成卡顿
                                         //  如果为0,则不合并发送
    "add_dummy_audio_enable": false,     //. 是否开启动态检测添加静音AAC数据的功能
                                         //  如果开启,rtmp pub推流时,如果超过`add_dummy_audio_wait_audio_ms`时间依然没有
                                         //  收到音频数据,则会自动为这路流叠加AAC的数据
    "add_dummy_audio_wait_audio_ms": 150 //. 单位毫秒,具体见`add_dummy_audio_enable`
  },
  "default_http": {                       //. http监听相关的默认配置,如果hls, httpflv, httpts中没有单独配置以下配置项,
                                          //  则使用default_http中的配置
                                          //  注意,hls, httpflv, httpts服务是否开启,不由此处决定
    "http_listen_addr": ":8080",          //. HTTP监听地址
    "https_listen_addr": ":4433",         //. HTTPS监听地址
    "https_cert_file": "./conf/cert.pem", //. HTTPS的本地cert文件地址
    "https_key_file": "./conf/key.pem"    //. HTTPS的本地key文件地址
  },
  "httpflv": {
    "enable": true,          //. 是否开启HTTP-FLV服务的监听
    "enable_https": true,    //. 是否开启HTTPS-FLV监听
    "url_pattern": "/",      //. 拉流url路由路径地址。默认值为`/`,表示不受限制,路由地址可以为任意路径地址。
                             //  如果设置为`/live/`,则只能从`/live/`路径下拉流,比如`/live/test110.flv`
    "gop_num": 0             //. 见rtmp.gop_num
  },
  "hls": {
    "enable": true,                  //. 是否开启HLS服务的监听
    "enable_https": true,            //. 是否开启HTTPS-HLS监听
                                     //
    "url_pattern": "/hls/",          //. 拉流url路由地址,默认值`/hls/`,对应的HLS(m3u8)拉流url地址:
                                     //  - `/hls/{streamName}.m3u8`
                                     //  - `/hls/{streamName}/playlist.m3u8`
                                     //  - `/hls/{streamName}/record.m3u8`
                                     //
                                     //  playlist.m3u8文件对应直播hls,列表中只保存<fragment_num>个ts文件名称,会持续增
                                     //  加新生成的ts文件,并去除过期的ts文件
                                     //  record.m3u8文件对应录制hls,列表中会保存从第一个ts文件到最新生成的ts文件,会持
                                     //  续追加新生成的ts文件
                                     //
                                     //  ts文件地址备注如下:
                                     //  - `/hls/{streamName}/{streamName}-{timestamp}-{index}.ts` 或
                                     //    `/hls/{streamName}-{timestamp}-{index}.ts`
                                     //
                                     //  注意,hls的url_pattern不能和httpflv、httpts的url_pattern相同
                                     //
    "out_path": "./lal_record/hls/", //. HLS的m3u8和文件的输出根目录
    "fragment_duration_ms": 3000,    //. 单个TS文件切片时长,单位毫秒
    "fragment_num": 6,               //. playlist.m3u8文件列表中ts文件的数量
                                     //
    "delete_threshold": 6,           //. ts文件的删除时机
                                     //  注意,只在配置项`cleanup_mode`为2时使用
                                     //  含义是只保存最近从playlist.m3u8中移除的ts文件的个数,更早过期的ts文件将被删除
                                     //  如果没有,默认值取配置项`fragment_num`的值
                                     //  注意,该值应该不小于1,避免删除过快导致播放失败
                                     //
    "cleanup_mode": 1,               //. HLS文件清理模式:
                                     //
                                     //  0 不删除m3u8+ts文件,可用于录制等场景
                                     //
                                     //  1 在输入流结束后删除m3u8+ts文件
                                     //    注意,确切的删除时间点是推流结束后的
                                     //    `fragment_duration_ms * (fragment_num + delete_threshold)`
                                     //    推迟一小段时间删除,是为了避免输入流刚结束,HLS的拉流端还没有拉取完
                                     //
                                     //  2 推流过程中,持续删除过期的ts文件,只保留最近的
                                     //    `delete_threshold + fragment_num + 1`
                                     //    个左右的ts文件
                                     //    并且,在输入流结束后,也会执行清理模式1的逻辑
                                     //
                                     //  注意,record.m3u8只在0和1模式下生成
                                     //
    "use_memory_as_disk_flag": false //. 是否使用内存取代磁盘,保存m3u8+ts文件
                                     //  注意,使用该模式要注意内存容量。一般来说不应该搭配`cleanup_mode`为0或1使用
  },
  "httpts": {
    "enable": true,         //. 是否开启HTTP-TS服务的监听。注意,这并不是HLS中的TS,而是在一条HTTP长连接上持续性传输TS流
    "enable_https": true,   //. 是否开启HTTPS-TS监听
    "url_pattern": "/"      //. 拉流url路由路径地址。默认值为`/`,表示不受限制,路由地址可以为任意路径地址。
                            //  如果设置为`/live/`,则只能从`/live/`路径下拉流,比如`/live/test110.ts`
  },
  "rtsp": {
    "enable": true, //. 是否开启rtsp服务的监听,目前只支持rtsp推流
    "addr": ":5544" //. rtsp推流地址
  },
  "record": {
    "enable_flv": true,                      //. 是否开启flv录制
    "flv_out_path": "./lal_record/flv/",     //. flv录制目录
    "enable_mpegts": true,                   //. 是否开启mpegts录制。注意,此处是长ts文件录制,hls录制由上面的hls配置控制
    "mpegts_out_path": "./lal_record/mpegts" //. mpegts录制目录
  },
  "relay_push": {
    "enable": false, //. 是否开启中继转推功能,开启后,自身接收到的所有流都会转推出去
    "addr_list":[    //. 中继转推的对端地址,支持填写多个地址,做1对n的转推。格式举例 "127.0.0.1:19351"
    ]
  },
  "relay_pull": {
    "enable": false, //. 是否开启回源拉流功能,开启后,当自身接收到拉流请求,而流不存在时,会从其他服务器拉取这个流到本地
    "addr": ""       //. 回源拉流的地址。格式举例 "127.0.0.1:19351"
  },
  "http_api": {
    "enable": true, //. 是否开启HTTP API接口
    "addr": ":8083" //. 监听地址
  },
  "server_id": "1", //. 当前lalserver唯一ID。多个lalserver HTTP Notify同一个地址时,可通过该ID区分
  "http_notify": {
    "enable": true,                                              //. 是否开启HTTP Notify事件回调
    "update_interval_sec": 5,                                    //. update事件回调间隔,单位毫秒
    "on_server_start": "http://127.0.0.1:10101/on_server_start", //. 各事件HTTP Notify事件回调地址
    "on_update": "http://127.0.0.1:10101/on_update",
    "on_pub_start": "http://127.0.0.1:10101/on_pub_start",
    "on_pub_stop": "http://127.0.0.1:10101/on_pub_stop",
    "on_sub_start": "http://127.0.0.1:10101/on_sub_start",
    "on_sub_stop": "http://127.0.0.1:10101/on_sub_stop",
    "on_rtmp_connect": "http://127.0.0.1:10101/on_rtmp_connect"
  },
  "simple_auth": {                    // 鉴权文档见: https://pengrl.com/lal/#/auth
    "key": "q191201771",              // 私有key,计算md5鉴权参数时使用
    "dangerous_lal_secret": "pengrl", // 后门鉴权参数,所有的流可通过该参数值鉴权
    "pub_rtmp_enable": false,         // rtmp推流是否开启鉴权,true为开启鉴权,false为不开启鉴权
    "sub_rtmp_enable": false,         // rtmp拉流是否开启鉴权
    "sub_httpflv_enable": false,      // httpflv拉流是否开启鉴权
    "sub_httpts_enable": false,       // httpts拉流是否开启鉴权
    "pub_rtsp_enable": false,         // rtsp推流是否开启鉴权
    "sub_rtsp_enable": false,         // rtsp拉流是否开启鉴权
    "hls_m3u8_enable": true           // m3u8拉流是否开启鉴权
  },
  "pprof": {
    "enable": true, //. 是否开启Go pprof web服务的监听
    "addr": ":8084" //. Go pprof web地址
  },
  "log": {
    "level": 1,                         //. 日志级别,0 trace, 1 debug, 2 info, 3 warn, 4 error, 5 fatal
    "filename": "./logs/lalserver.log", //. 日志输出文件
    "is_to_stdout": true,               //. 是否打印至标志控制台输出
    "is_rotate_daily": true,            //. 日志按天翻滚
    "short_file_flag": true,            //. 日志末尾是否携带源码文件名以及行号的信息
    "assert_behavior": 1                //. 日志断言的行为,1 只打印错误日志 2 打印并退出程序 3 打印并panic
  },
  "debug": {
    "log_group_interval_sec": 30,          // 打印group调试日志的间隔时间,单位秒。如果为0,则不打印
    "log_group_max_group_num": 10,         // 最多打印多少个group
    "log_group_max_sub_num_per_group": 10  // 每个group最多打印多少个sub session
  }
}

6. Introduction to Demo

In the lal project, in addition to the relatively core service /app/lalserver , some additional small applications are provided in the /app/demo directory. The function introduction:

| demo | Description|
| - | - |
| pushrtmp | RTMP push client; stress testing tool |
| pullrtmp | RTMP pull client; stress testing tool |
| pullrtmp2pushrtmp | Pull RTMP stream from remote server, and use RTMP to repost, support 1-to-n repost|
| pullrtmp2pushrtsp | Pull the RTMP stream from the remote server and use RTSP to push it out |
| pullrtmp2hls | Pull RTMP stream from remote server and store as local m3u8+ts file|
| pullhttpflv | HTTP-FLV pull streaming client |
| pullrtsp | RTSP pull stream 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 send it out
benchrtmpconnectDo concurrent connection stress test on rtmp
calcrtmpdelayTest the delay of sending and receiving data from the rtmp server
analyseflvPull HTTP-FLV stream from remote server and analyze it
dispatchA simple demonstration of how to implement a simple scheduling service so that multiple lalserver nodes can form a cluster
flvfile2esSeparate local FLV files into ES stream files of H264/AVC and AAC
modflvfileModify some information of the flv file (such as the timestamp of some tags) and save the file as a file

(For more specific functions, please refer to the header description of each source code file)

7. Contact the author

Any communication, technical and non-technical, is welcome.


就想叫yoko
50 声望307 粉丝

[链接]