forked from Monibuca/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (93 loc) · 2.58 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package engine
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"path/filepath"
"runtime"
"strings"
"time" // colorable
"github.com/Monibuca/utils/v3"
"github.com/Monibuca/engine/v3/util"
"github.com/BurntSushi/toml"
. "github.com/logrusorgru/aurora"
)
var Version = "3.2.2"
var (
config = &struct {
EnableAudio bool
EnableVideo bool
PublishTimeout time.Duration
MaxRingSize int
AutoCloseAfter int
}{true, true, 60, 256, -1}
// ConfigRaw 配置信息的原始数据
ConfigRaw []byte
StartTime time.Time //启动时间
Plugins = make(map[string]*PluginConfig) // Plugins 所有的插件配置
HasTranscoder bool
Ctx context.Context
)
//PluginConfig 插件配置定义
type PluginConfig struct {
Name string //插件名称
Config interface{} //插件配置
Version string //插件版本
Dir string //插件代码路径
Run func() //插件启动函数
HotConfig map[string]func(interface{}) //热修改配置
}
// InstallPlugin 安装插件
func InstallPlugin(opt *PluginConfig) {
Plugins[opt.Name] = opt
_, pluginFilePath, _, _ := runtime.Caller(1)
opt.Dir = filepath.Dir(pluginFilePath)
if parts := strings.Split(opt.Dir, "@"); len(parts) > 1 {
opt.Version = parts[len(parts)-1]
}
utils.Print(Green("install plugin"), BrightCyan(opt.Name), BrightBlue(opt.Version))
}
func init() {
if parts := strings.Split(utils.CurrentDir(), "@"); len(parts) > 1 {
Version = parts[len(parts)-1]
}
}
// Run 启动Monibuca引擎
func Run(ctx context.Context, configFile string) (err error) {
Ctx = ctx
util.CreateShutdownScript()
StartTime = time.Now()
if ConfigRaw, err = ioutil.ReadFile(configFile); err != nil {
utils.Print(Red("read config file error:"), err)
return
}
utils.Print(BgGreen(Black("Ⓜ starting m7s ")), BrightBlue(Version))
var cg map[string]interface{}
if _, err = toml.Decode(string(ConfigRaw), &cg); err == nil {
if cfg, ok := cg["Engine"]; ok {
b, _ := json.Marshal(cfg)
if err = json.Unmarshal(b, config); err != nil {
log.Println(err)
}
config.PublishTimeout *= time.Second
}
for name, config := range Plugins {
if cfg, ok := cg[name]; ok {
b, _ := json.Marshal(cfg)
if err = json.Unmarshal(b, config.Config); err != nil {
log.Println(err)
continue
}
} else if config.Config != nil {
continue
}
if config.Run != nil {
go config.Run()
}
}
} else {
utils.Print(Red("decode config file error:"), err)
}
return
}