forked from bytelang/kplayer-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
201 lines (171 loc) · 5.2 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/bytelang/kplayer/types/config"
errortypes "github.com/bytelang/kplayer/types/error"
"io/ioutil"
"os"
"runtime"
"github.com/bytelang/kplayer/app"
"github.com/bytelang/kplayer/cmd"
"github.com/bytelang/kplayer/module"
"github.com/bytelang/kplayer/server"
kptypes "github.com/bytelang/kplayer/types"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func init() {
log.SetOutput(os.Stdout)
log.SetReportCaller(true)
log.SetLevel(log.TraceLevel)
logFormat := &log.TextFormatter{
TimestampFormat: "2006-01-02 15:04:05",
DisableColors: false,
FullTimestamp: true,
CallerPrettyfier: func(f *runtime.Frame) (function string, file string) {
return "", fmt.Sprintf("%s:%d", f.File, f.Line)
},
}
log.SetFormatter(logFormat)
}
func main() {
rootCmd := cmd.NewRootCmd()
if err := Execute(rootCmd, app.DefaultConfigFilePath, app.DefaultConfigFileName); err != nil {
switch e := err.(type) {
case kptypes.ErrorCode:
os.Exit(e.Code)
default:
os.Exit(1)
}
}
}
// Execute execute from flags and commands
func Execute(rootCmd *cobra.Command, defaultHome string, defaultFile string) error {
rootCmd.PersistentFlags().String(kptypes.FlagLogLevel, log.InfoLevel.String(), "The logging level (trace|debug|info|warn|error|fatal|panic)")
rootCmd.PersistentFlags().String(kptypes.FlagLogFormat, "plain", "The logging format (json|plain)")
rootCmd.PersistentFlags().StringP(kptypes.FlagHome, "", defaultHome, "directory for config and data")
rootCmd.PersistentFlags().StringP(kptypes.FlagConfigFileName, "c", defaultFile, "config file name")
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
// set home path
homePath, err := cmd.Flags().GetString(kptypes.FlagHome)
if err != nil {
return err
}
if homePath != "" {
if err := os.Chdir(homePath); err != nil {
log.WithField("error", err).Fatal("chdir failed")
}
}
// init context
InitGlobalContextConfig(cmd)
return nil
}
ctx := context.Background()
ctx = context.WithValue(ctx, kptypes.ClientContextKey, kptypes.DefaultClientContext())
ctx = context.WithValue(ctx, kptypes.ModuleManagerContextKey, app.ModuleManager)
ctx = context.WithValue(ctx, kptypes.ServerCreatorContextKey, server.NewJsonRPCServer())
return kptypes.SetCommandContextAndExecute(rootCmd, ctx)
}
func InitGlobalContextConfig(cmd *cobra.Command) {
mm := cmd.Context().Value(kptypes.ModuleManagerContextKey).(module.ModuleManager)
clientCtx := cmd.Context().Value(kptypes.ClientContextKey).(*kptypes.ClientContext)
configFileName, err := kptypes.GetConfigFileName(cmd)
if err != nil {
log.Fatal(err)
}
// set log level
logLevel, err := kptypes.GetLogLevel(cmd)
if err != nil {
log.Fatal(err)
}
log.SetLevel(logLevel)
if logLevel == log.InfoLevel {
log.SetReportCaller(false)
}
// viper
v := viper.New()
v.AddConfigPath(".")
v.SetConfigType("json")
v.SetConfigName(configFileName)
// skip on init stage
if cmd.Parent().Use == "init" {
return
}
// load config context in file
if err := v.ReadInConfig(); err != nil {
log.Fatal(err)
}
// set default value
setDefaultConfig(v)
clientCtx.Viper = v
if err := v.Unmarshal(clientCtx.Config); err != nil {
log.Fatal(err)
}
// validate global config
if err := ValidateConfig(clientCtx.Config); err != nil {
log.Fatal(err)
}
// init module
for _, item := range mm.OrderInitConfig {
m := mm.GetModule(item)
// init config and set default value
d, err := json.Marshal(v.AllSettings()[m.GetModuleName()])
if err != nil {
log.Fatal(err)
}
modifyData, err := m.InitConfig(clientCtx, d)
if err != nil {
log.Fatal(err)
}
v.Set(m.GetModuleName(), modifyData)
// validate config
if err := m.ValidateConfig(); err != nil {
log.Fatal(err)
}
}
// set context before module modify
if err := v.Unmarshal(clientCtx.Config); err != nil {
log.Fatal(err)
}
}
func ValidateConfig(config *config.KPConfig) error {
if config.Version != app.ConfigVersion {
return errortypes.VersionInvalidMainError
}
// load user token file
if config.TokenPath != "" {
if !kptypes.FileExists(config.TokenPath) {
return errortypes.TokenFileNotFoundMainError
}
fileContent, err := ioutil.ReadFile(config.TokenPath)
if err != nil {
return err
}
if err := kptypes.LoadClientToken(string(fileContent)); err != nil {
log.Fatal(err)
}
}
return nil
}
func setDefaultConfig(v *viper.Viper) {
v.SetDefault("play.start_point", 1)
v.SetDefault("play.play_model", "list")
v.SetDefault("play.encode_model", "rtmp")
v.SetDefault("play.cache_on", false)
v.SetDefault("play.cache_uncheck", false)
v.SetDefault("play.skip_invalid_resource", true)
v.SetDefault("play.rpc.on", true)
v.SetDefault("play.rpc.port", kptypes.DefaultRPCPort)
v.SetDefault("play.rpc.address", kptypes.DefaultRPCAddress)
v.SetDefault("play.encode.video_width", 780)
v.SetDefault("play.encode.video_height", 480)
v.SetDefault("play.encode.video_fps", 30)
v.SetDefault("play.encode.audio_channel_layout", 3)
v.SetDefault("play.encode.audio_channels", 2)
v.SetDefault("play.encode.audio_sample_rate", 48000)
v.SetDefault("play.encode.bit_rate", 0)
v.SetDefault("play.encode.avg_quality", 0)
}