github.com/cnotch/ipchub@v1.1.0/config/global.go (about)

     1  // Copyright (c) 2019,CAOHONGJU All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package config
     6  
     7  import (
     8  	"os"
     9  	"os/exec"
    10  	"path/filepath"
    11  	"strings"
    12  	"time"
    13  
    14  	"github.com/cnotch/ipchub/provider/auth"
    15  	cfg "github.com/cnotch/loader"
    16  	"github.com/cnotch/xlog"
    17  )
    18  
    19  // 服务名
    20  const (
    21  	Vendor  = "CAOHONGJU"
    22  	Name    = "ipchub"
    23  	Version = "V1.1.0"
    24  )
    25  
    26  var (
    27  	globalC       *config
    28  	consoleAppDir string
    29  	demosAppDir   string
    30  )
    31  
    32  // InitConfig 初始化 Config
    33  func InitConfig() {
    34  	exe, err := os.Executable()
    35  	if err != nil {
    36  		xlog.Panic(err.Error())
    37  	}
    38  
    39  	configPath := filepath.Join(filepath.Dir(exe), Name+".conf")
    40  	consoleAppDir = filepath.Join(filepath.Dir(exe), "console")
    41  	demosAppDir = filepath.Join(filepath.Dir(exe), "demos")
    42  
    43  	globalC = new(config)
    44  	globalC.initFlags()
    45  
    46  	// 创建或加载配置文件
    47  	if err := cfg.Load(globalC,
    48  		&cfg.JSONLoader{Path: configPath, CreatedIfNonExsit: true},
    49  		&cfg.EnvLoader{Prefix: strings.ToUpper(Name)},
    50  		&cfg.FlagLoader{}); err != nil {
    51  		// 异常,直接退出
    52  		xlog.Panic(err.Error())
    53  	}
    54  
    55  	if globalC.HlsPath != "" {
    56  		if !filepath.IsAbs(globalC.HlsPath) {
    57  			globalC.HlsPath = filepath.Join(filepath.Dir(exe), globalC.HlsPath)
    58  		}
    59  
    60  		_, err = os.Stat(globalC.HlsPath)
    61  		if err != nil {
    62  			if os.IsNotExist(err) {
    63  				if err = os.MkdirAll(globalC.HlsPath, os.ModePerm); err != nil {
    64  					panic(err)
    65  				}
    66  			} else {
    67  				panic(err)
    68  			}
    69  		}
    70  	}
    71  
    72  	// 初始化日志
    73  	globalC.Log.initLogger()
    74  }
    75  
    76  // Addr Listen addr
    77  func Addr() string {
    78  	if globalC == nil {
    79  		return ":554"
    80  	}
    81  	return globalC.ListenAddr
    82  }
    83  
    84  // Auth 是否启用验证
    85  func Auth() bool {
    86  	if globalC == nil {
    87  		return false
    88  	}
    89  	return globalC.Auth
    90  }
    91  
    92  // CacheGop 是否Cache Gop
    93  func CacheGop() bool {
    94  	if globalC == nil {
    95  		return false
    96  	}
    97  	return globalC.CacheGop
    98  }
    99  
   100  // Profile 是否启动 Http Profile
   101  func Profile() bool {
   102  	if globalC == nil {
   103  		return false
   104  	}
   105  	return globalC.Profile
   106  }
   107  
   108  // GetTLSConfig 获取TLSConfig
   109  func GetTLSConfig() *TLSConfig {
   110  	if globalC == nil {
   111  		return nil
   112  	}
   113  	return globalC.TLS
   114  }
   115  
   116  // ConsoleAppDir 管理员控制台应用的目录
   117  func ConsoleAppDir() (string, bool) {
   118  	if consoleAppDir == "" {
   119  		return "", false
   120  	}
   121  	finfo, err := os.Stat(consoleAppDir)
   122  	if err != nil || !finfo.IsDir() {
   123  		return "", false
   124  	}
   125  	return consoleAppDir, true
   126  }
   127  
   128  // DemosAppDir 例子应用目录
   129  func DemosAppDir() (string, bool) {
   130  	if demosAppDir == "" {
   131  		return "", false
   132  	}
   133  	finfo, err := os.Stat(demosAppDir)
   134  	if err != nil || !finfo.IsDir() {
   135  		return "", false
   136  	}
   137  	return demosAppDir, true
   138  }
   139  
   140  // NetTimeout 返回网络超时设置
   141  func NetTimeout() time.Duration {
   142  	return time.Second * 45
   143  }
   144  
   145  // NetHeartbeatInterval 返回网络心跳间隔
   146  func NetHeartbeatInterval() time.Duration {
   147  	return time.Second * 30
   148  }
   149  
   150  // NetBufferSize 网络通讯时的BufferSize
   151  func NetBufferSize() int {
   152  	return 128 * 1024
   153  }
   154  
   155  // NetFlushRate 网络刷新频率
   156  func NetFlushRate() int {
   157  	return 30
   158  }
   159  
   160  // RtspAuthMode rtsp 认证模式
   161  func RtspAuthMode() auth.Mode {
   162  	if globalC == nil || !globalC.Auth {
   163  		return auth.NoneAuth
   164  	}
   165  	return auth.DigestAuth
   166  }
   167  
   168  // MulticastTTL 组播TTL值
   169  func MulticastTTL() int {
   170  	return 127
   171  }
   172  
   173  // HlsEnable 是否启动 Hls
   174  func HlsEnable() bool {
   175  	return true
   176  }
   177  
   178  // HlsFragment TS片段时长(s)
   179  func HlsFragment() int {
   180  	if globalC == nil || globalC.HlsFragment < 5 {
   181  		return 5
   182  	}
   183  	return globalC.HlsFragment
   184  }
   185  
   186  // HlsPath hls 存储目录
   187  func HlsPath() string {
   188  	if globalC == nil {
   189  		return ""
   190  	}
   191  	return globalC.HlsPath
   192  }
   193  
   194  // LoadRoutetableProvider 加载路由表提供者
   195  func LoadRoutetableProvider(providers ...Provider) Provider {
   196  	if globalC == nil {
   197  		return LoadProvider(nil, providers...)
   198  	}
   199  	return LoadProvider(globalC.Routetable, providers...)
   200  }
   201  
   202  // LoadUsersProvider 加载用户提供者
   203  func LoadUsersProvider(providers ...Provider) Provider {
   204  	if globalC == nil {
   205  		return LoadProvider(nil, providers...)
   206  	}
   207  	return LoadProvider(globalC.Users, providers...)
   208  }
   209  
   210  // DetectFfmpeg 判断ffmpeg命令行是否存在
   211  func DetectFfmpeg(l *xlog.Logger) bool {
   212  	out, err := exec.Command("ffmpeg", "-version").Output()
   213  	if err != nil {
   214  		return false
   215  	}
   216  
   217  	i := strings.Index(string(out), "Copyright")
   218  	if i > 0 {
   219  		l.Infof("detect %s", out[:i])
   220  	}
   221  	return true
   222  }