feat: add transform from start

This commit is contained in:
langhuihui
2024-11-10 11:42:51 +08:00
parent 07566e4780
commit e0f4d79d75
3 changed files with 33 additions and 17 deletions

View File

@@ -94,9 +94,10 @@ type (
Quic Quic
TCP TCP
UDP UDP
Pull map[string]Pull Pull map[string]Pull
OnSub OnSubscribe Transform map[Regexp]Transform
OnPub OnPublish OnSub OnSubscribe
OnPub OnPublish
DB DB
} }
ICommonConf interface { ICommonConf interface {

View File

@@ -3,8 +3,6 @@ package transcode
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"m7s.live/pro/pkg"
"m7s.live/pro/pkg/filerotate"
"net" "net"
"net/url" "net/url"
"os" "os"
@@ -12,7 +10,10 @@ import (
"strings" "strings"
"time" "time"
"m7s.live/pro" "m7s.live/pro/pkg"
"m7s.live/pro/pkg/filerotate"
m7s "m7s.live/pro"
"m7s.live/pro/pkg/config" "m7s.live/pro/pkg/config"
"m7s.live/pro/pkg/task" "m7s.live/pro/pkg/task"
"m7s.live/pro/pkg/util" "m7s.live/pro/pkg/util"
@@ -21,18 +22,20 @@ import (
// / 定义传输模式的常量 // / 定义传输模式的常量
const ( const (
TRANS_MODE_PIPE TransMode = "pipe" TRANS_MODE_PIPE TransMode = "pipe"
TRANS_MODE_RTSP TransMode = "rtsp" TRANS_MODE_RTSP TransMode = "rtsp"
TRANS_MODE_RTMP TransMode = "rtmp" TRANS_MODE_RTMP TransMode = "rtmp"
TRANS_MODE_LIB TransMode = "lib" TRANS_MODE_LIB TransMode = "lib"
TRANS_MODE_REMOTE TransMode = "remote"
) )
type ( type (
TransMode string TransMode string
DecodeConfig struct { DecodeConfig struct {
Mode TransMode `default:"pipe" json:"mode" desc:"转码模式"` //转码模式 Mode TransMode `default:"pipe" json:"mode" desc:"转码模式"` //转码模式
Codec string `json:"codec" desc:"解码器"` Codec string `json:"codec" desc:"解码器"`
Args string `json:"args" desc:"解码参数"` Args string `json:"args" desc:"解码参数"`
Remote string `json:"remote" desc:"远程地址"`
} }
EncodeConfig struct { EncodeConfig struct {
Codec string `json:"codec" desc:"编码器"` Codec string `json:"codec" desc:"编码器"`
@@ -81,13 +84,13 @@ func (t *Transformer) Start() (err error) {
args = append(args, "-c:v", t.From.Codec) args = append(args, "-c:v", t.From.Codec)
} }
switch t.From.Mode { switch t.From.Mode {
case "pipe": case TRANS_MODE_PIPE:
err = t.TransformJob.Subscribe() err = t.TransformJob.Subscribe()
if err != nil { if err != nil {
return return
} }
args = append(args, "-f", "flv", "-i", "pipe:0") args = append(args, "-f", "flv", "-i", "pipe:0")
case "rtsp": case TRANS_MODE_RTSP:
if rtspPlugin, ok := t.TransformJob.Plugin.Server.Plugins.Get("RTSP"); ok { if rtspPlugin, ok := t.TransformJob.Plugin.Server.Plugins.Get("RTSP"); ok {
listenAddr := rtspPlugin.GetCommonConf().TCP.ListenAddr listenAddr := rtspPlugin.GetCommonConf().TCP.ListenAddr
if strings.HasPrefix(listenAddr, ":") { if strings.HasPrefix(listenAddr, ":") {
@@ -95,7 +98,7 @@ func (t *Transformer) Start() (err error) {
} }
args = append(args, "-i", "rtsp://"+listenAddr+"/"+t.TransformJob.StreamPath) args = append(args, "-i", "rtsp://"+listenAddr+"/"+t.TransformJob.StreamPath)
} }
case "rtmp": case TRANS_MODE_RTMP:
if rtmpPlugin, ok := t.TransformJob.Plugin.Server.Plugins.Get("RTMP"); ok { if rtmpPlugin, ok := t.TransformJob.Plugin.Server.Plugins.Get("RTMP"); ok {
listenAddr := rtmpPlugin.GetCommonConf().TCP.ListenAddr listenAddr := rtmpPlugin.GetCommonConf().TCP.ListenAddr
if strings.HasPrefix(listenAddr, ":") { if strings.HasPrefix(listenAddr, ":") {
@@ -103,6 +106,8 @@ func (t *Transformer) Start() (err error) {
} }
args = append(args, "-i", "rtmp://"+listenAddr+"/"+t.TransformJob.StreamPath) args = append(args, "-i", "rtmp://"+listenAddr+"/"+t.TransformJob.StreamPath)
} }
case TRANS_MODE_REMOTE:
args = append(args, "-i", t.From.Remote)
} }
t.To = make([]EncodeConfig, len(t.TransformJob.Config.Output)) t.To = make([]EncodeConfig, len(t.TransformJob.Config.Output))
for i, to := range t.TransformJob.Config.Output { for i, to := range t.TransformJob.Config.Output {

View File

@@ -142,6 +142,9 @@ func exit() {
func init() { func init() {
Servers.Init() Servers.Init()
Servers.OnBeforeDispose(func() {
time.AfterFunc(3*time.Second, exit)
})
Servers.OnDispose(exit) Servers.OnDispose(exit)
for k, v := range myip.LocalAndInternalIPs() { for k, v := range myip.LocalAndInternalIPs() {
Routes[k] = v Routes[k] = v
@@ -299,6 +302,12 @@ func (s *Server) Start() (err error) {
plugin.handler.Pull(streamPath, conf) plugin.handler.Pull(streamPath, conf)
} }
} }
if plugin.Meta.Transformer != nil {
for streamPath, conf := range plugin.config.Transform {
transformer := plugin.Meta.Transformer()
transformer.GetTransformJob().Init(transformer, plugin, streamPath.String(), conf)
}
}
} }
if s.DB != nil { if s.DB != nil {
s.DB.AutoMigrate(&Device{}) s.DB.AutoMigrate(&Device{})
@@ -342,11 +351,12 @@ func (s *Server) Start() (err error) {
s.DB.Find(&devices) s.DB.Find(&devices)
for _, d := range devices { for _, d := range devices {
d.server = s d.server = s
d.Logger = s.Logger.With("device", d.ID, "type", d.Type, "name", d.Name)
d.ChangeStatus(DeviceStatusOffline) d.ChangeStatus(DeviceStatusOffline)
if d.PubConf == nil { if d.PubConf == nil {
d.PubConf = config.NewPublish() d.PubConf = config.NewPublish()
} }
s.Devices.Add(d, s.Logger.With("device", d.ID, "type", d.Type, "name", d.Name)) s.Devices.Add(d)
} }
} }
return nil return nil