mirror of
https://github.com/lkmio/lkm.git
synced 2025-09-27 03:26:01 +08:00
支持运行程序时传参
This commit is contained in:
127
main.go
127
main.go
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/lkmio/avformat/transport"
|
||||
"github.com/lkmio/avformat/utils"
|
||||
"github.com/lkmio/lkm/flv"
|
||||
"github.com/lkmio/lkm/gb28181"
|
||||
"github.com/lkmio/lkm/hls"
|
||||
@@ -15,12 +16,98 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/lkmio/lkm/rtmp"
|
||||
"github.com/lkmio/lkm/stream"
|
||||
)
|
||||
|
||||
func readRunArgs() (map[string]string, map[string]string) {
|
||||
args := os.Args
|
||||
|
||||
// 运行参数项优先级高于config.json参数项
|
||||
// --disable-rtmp --enable-rtmp=11935
|
||||
// --disable-rtsp --enable-rtsp
|
||||
// --disable-hls --enable-hls
|
||||
// --disable-webrtc --enable-webrtc=18000
|
||||
// --disable-gb28181 --enable-gb28181
|
||||
// --disable-jt1078 --enable-jt1078=11078
|
||||
// --disable-hooks --enable-hooks
|
||||
// --disable-record --enable-record
|
||||
|
||||
disableOptions := map[string]string{}
|
||||
enableOptions := map[string]string{}
|
||||
for _, arg := range args {
|
||||
// 参数忽略大小写
|
||||
arg = strings.ToLower(arg)
|
||||
|
||||
var option string
|
||||
var enable bool
|
||||
if strings.HasPrefix(arg, "--disable-") {
|
||||
option = arg[len("--disable-"):]
|
||||
} else if strings.HasPrefix(arg, "--enable-") {
|
||||
option = arg[len("--enable-"):]
|
||||
enable = true
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
|
||||
pair := strings.Split(option, "=")
|
||||
var value string
|
||||
if len(pair) > 1 {
|
||||
value = pair[1]
|
||||
}
|
||||
|
||||
if enable {
|
||||
enableOptions[pair[0]] = value
|
||||
} else {
|
||||
disableOptions[pair[0]] = value
|
||||
}
|
||||
}
|
||||
|
||||
// 删除重叠参数, 禁用和开启同时声明时, 以开启为准.
|
||||
for k := range enableOptions {
|
||||
if _, ok := disableOptions[k]; ok {
|
||||
delete(disableOptions, k)
|
||||
}
|
||||
}
|
||||
|
||||
return disableOptions, enableOptions
|
||||
}
|
||||
|
||||
func mergeArgs(options map[string]stream.EnableConfig, disableOptions, enableOptions map[string]string) {
|
||||
for k := range disableOptions {
|
||||
option, ok := options[k]
|
||||
utils.Assert(ok)
|
||||
|
||||
option.SetEnable(false)
|
||||
}
|
||||
|
||||
for k, v := range enableOptions {
|
||||
var port int
|
||||
|
||||
if len(v) > 0 {
|
||||
atoi, err := strconv.Atoi(v)
|
||||
if err == nil && atoi > 0 {
|
||||
port = atoi
|
||||
}
|
||||
}
|
||||
|
||||
option, ok := options[k]
|
||||
utils.Assert(ok)
|
||||
|
||||
option.SetEnable(true)
|
||||
|
||||
if port > 0 {
|
||||
if config, ok := option.(stream.PortConfig); ok {
|
||||
config.SetPort(port)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
stream.RegisterTransStreamFactory(stream.TransStreamRtmp, rtmp.TransStreamFactory)
|
||||
stream.RegisterTransStreamFactory(stream.TransStreamHls, hls.TransStreamFactory)
|
||||
@@ -36,28 +123,49 @@ func init() {
|
||||
}
|
||||
|
||||
stream.SetDefaultConfig(config)
|
||||
|
||||
options := map[string]stream.EnableConfig{
|
||||
"rtmp": &config.Rtmp,
|
||||
"rtsp": &config.Rtsp,
|
||||
"hls": &config.Hls,
|
||||
"webrtc": &config.WebRtc,
|
||||
"gb28181": &config.GB28181,
|
||||
"jt1078": &config.JT1078,
|
||||
"hooks": &config.Hooks,
|
||||
"record": &config.Record,
|
||||
}
|
||||
|
||||
disableOptions, enableOptions := readRunArgs()
|
||||
mergeArgs(options, disableOptions, enableOptions)
|
||||
|
||||
stream.AppConfig = *config
|
||||
stream.InitHookUrls()
|
||||
// 设置公网IP和端口
|
||||
rtc.InitConfig()
|
||||
|
||||
if stream.AppConfig.Hooks.Enable {
|
||||
stream.InitHookUrls()
|
||||
}
|
||||
|
||||
if stream.AppConfig.WebRtc.Enable {
|
||||
// 设置公网IP和端口
|
||||
rtc.InitConfig()
|
||||
}
|
||||
|
||||
// 初始化日志
|
||||
log.InitLogger(zapcore.Level(stream.AppConfig.Log.Level), stream.AppConfig.Log.Name, stream.AppConfig.Log.MaxSize, stream.AppConfig.Log.MaxBackup, stream.AppConfig.Log.MaxAge, stream.AppConfig.Log.Compress)
|
||||
|
||||
if stream.AppConfig.GB28181.IsMultiPort() {
|
||||
if stream.AppConfig.GB28181.Enable && stream.AppConfig.GB28181.IsMultiPort() {
|
||||
gb28181.TransportManger = transport.NewTransportManager(uint16(stream.AppConfig.GB28181.Port[0]), uint16(stream.AppConfig.GB28181.Port[1]))
|
||||
}
|
||||
|
||||
if stream.AppConfig.Rtsp.IsMultiPort() {
|
||||
if stream.AppConfig.Rtsp.Enable && stream.AppConfig.Rtsp.IsMultiPort() {
|
||||
rtsp.TransportManger = transport.NewTransportManager(uint16(stream.AppConfig.Rtsp.Port[1]), uint16(stream.AppConfig.Rtsp.Port[2]))
|
||||
}
|
||||
|
||||
// 打印配置信息
|
||||
indent, _ := json.MarshalIndent(stream.AppConfig, "", "\t")
|
||||
log.Sugar.Infof("server config:\r\n%s", indent)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
if stream.AppConfig.Rtmp.Enable {
|
||||
rtmpAddr, err := net.ResolveTCPAddr("tcp", stream.ListenAddr(stream.AppConfig.Rtmp.Port))
|
||||
if err != nil {
|
||||
@@ -91,9 +199,9 @@ func main() {
|
||||
log.Sugar.Info("启动http服务 addr:", stream.ListenAddr(stream.AppConfig.Http.Port))
|
||||
go startApiServer(net.JoinHostPort(stream.AppConfig.ListenIP, strconv.Itoa(stream.AppConfig.Http.Port)))
|
||||
|
||||
//单端口模式下, 启动时就创建收流端口
|
||||
//多端口模式下, 创建GBSource时才创建收流端口
|
||||
if !stream.AppConfig.GB28181.IsMultiPort() {
|
||||
// 单端口模式下, 启动时就创建收流端口
|
||||
// 多端口模式下, 创建GBSource时才创建收流端口
|
||||
if stream.AppConfig.GB28181.Enable && !stream.AppConfig.GB28181.IsMultiPort() {
|
||||
if stream.AppConfig.GB28181.IsEnableUDP() {
|
||||
server, err := gb28181.NewUDPServer(gb28181.NewSSRCFilter(128))
|
||||
if err != nil {
|
||||
@@ -136,6 +244,7 @@ func main() {
|
||||
}()
|
||||
}
|
||||
|
||||
// 开启pprof调试
|
||||
err := http.ListenAndServe(":19999", nil)
|
||||
if err != nil {
|
||||
println(err)
|
||||
|
Reference in New Issue
Block a user