-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathmain.go
304 lines (265 loc) · 8.28 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/bytelang/kplayer/app"
"github.com/bytelang/kplayer/cmd"
"github.com/bytelang/kplayer/module"
"github.com/bytelang/kplayer/module/play/provider"
"github.com/bytelang/kplayer/server"
kptypes "github.com/bytelang/kplayer/types"
"github.com/bytelang/kplayer/types/config"
errortypes "github.com/bytelang/kplayer/types/error"
"github.com/go-playground/validator/v10"
"github.com/golang/protobuf/ptypes"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tidwall/gjson"
"google.golang.org/protobuf/types/known/anypb"
"io/ioutil"
"os"
"runtime"
"strings"
)
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.NewHttpServer())
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")
if !kptypes.FileExists(configFileName) && !kptypes.FileExists(configFileName+".json") {
v.SetConfigType("yaml")
}
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)
// refill resource list proto.any
// viper decode not support protobuf any unpack
// prepare constructing a resource list
var resourceLists []*anypb.Any
for _, item := range v.Get("resource.lists").([]interface{}) {
switch itemResource := item.(type) {
case string:
singleResource := &config.SingleResource{
Path: itemResource,
}
any, err := ptypes.MarshalAny(singleResource)
if err != nil {
log.WithField("error", err).Fatal("unmarshal any failed")
}
resourceLists = append(resourceLists, any)
case map[string]interface{}:
var found bool = false
bytes, err := json.Marshal(item)
if err != nil {
log.WithField("error", err).Fatal("unmarshal resource item failed")
}
// single resource
{
singleResource := &config.SingleResource{}
if err := kptypes.UnmarshalProtoMessageContinue(string(bytes), singleResource); err == nil {
any, err := ptypes.MarshalAny(singleResource)
if err != nil {
log.WithField("error", err).Fatal("unmarshal any failed")
}
resourceLists = append(resourceLists, any)
found = true
}
}
// mix resource
{
mixResource := &config.MixResource{}
if err := kptypes.UnmarshalProtoMessageContinue(string(bytes), mixResource); err == nil {
any, err := ptypes.MarshalAny(mixResource)
if err != nil {
log.WithField("error", err).Fatal("unmarshal any failed")
}
resourceLists = append(resourceLists, any)
found = true
}
}
if !found {
log.WithField("content", item).Warn("unrecognized resource structure")
}
}
}
v.Set("resource.lists", map[string]interface{}{})
// unmarshal config
clientCtx.Viper = v
if err := v.Unmarshal(clientCtx.Config); err != nil {
log.Fatal(err)
}
// set resource list
clientCtx.Config.Resource.Lists = resourceLists
// custom config
{
// add generate cache config
if cmd.Flag(provider.FlagGenerateCache) != nil {
if cmd.Flag(provider.FlagGenerateCache).Value.String() == provider.FlagYesValue {
clientCtx.Config.Play.PlayModel = strings.ToLower(config.PLAY_MODEL_name[int32(config.PLAY_MODEL_LIST)])
clientCtx.Config.Play.EncodeModel = strings.ToLower(config.ENCODE_MODEL_name[int32(config.ENCODE_MODEL_FILE)])
clientCtx.Config.Output.Lists = nil
clientCtx.Config.Play.CacheOn = true
}
}
}
reInitConfig, err := json.Marshal(clientCtx.Config)
if err != nil {
log.Fatal(err)
}
if err := v.ReadConfig(bytes.NewBuffer(reInitConfig)); 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(clientCtx.Config)
if err != nil {
log.Fatal(err)
}
modifyData, err := m.InitConfig(clientCtx, []byte(gjson.Parse(string(d)).Get(m.GetModuleName()).String()))
if err != nil {
log.Fatal(err)
}
// validator
validate := validator.New()
if err := validate.Struct(modifyData); err != nil {
log.Fatal(err)
}
// set modify data
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", false)
v.SetDefault("play.delay_queue_size", 50)
v.SetDefault("play.fill_strategy", "tile")
v.SetDefault("play.rpc.on", true)
v.SetDefault("play.rpc.http_port", kptypes.DefaultHttpPort)
v.SetDefault("play.rpc.grpc_port", kptypes.DefaultRPCPort)
v.SetDefault("play.rpc.address", kptypes.DefaultRPCAddress)
v.SetDefault("play.encode.video_width", 854)
v.SetDefault("play.encode.video_height", 480)
v.SetDefault("play.encode.video_fps", 25)
v.SetDefault("play.encode.audio_channel_layout", 3)
v.SetDefault("play.encode.audio_channels", 2)
v.SetDefault("play.encode.audio_sample_rate", 44100)
v.SetDefault("play.encode.bit_rate", 0)
v.SetDefault("play.encode.avg_quality", 0)
// auth
v.SetDefault("auth.auth_on", false)
}