config.cpp 10.5 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
xiongziliang committed
6 7 8
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
xiongziliang committed
9 10
 */

xiongziliang committed
11
#include "Common/config.h"
xzl committed
12
#include "Util/util.h"
13
#include "Util/logger.h"
xzl committed
14
#include "Util/onceToken.h"
15
#include "Util/NoticeCenter.h"
xzl committed
16 17
#include "Network/sockutil.h"

xiongziliang committed
18
using namespace toolkit;
xzl committed
19

xiongziliang committed
20
namespace mediakit {
xzl committed
21

22 23
bool loadIniConfig(const char *ini_path){
    string ini;
24
    if(ini_path && ini_path[0] != '\0'){
25 26 27 28
        ini = ini_path;
    }else{
        ini = exePath() + ".ini";
    }
29
    try{
30
        mINI::Instance().parseFile(ini);
31
        NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastReloadConfig);
32
        return true;
33 34
    }catch (std::exception &ex) {
        InfoL << "dump ini file to:" << ini;
35 36
        mINI::Instance().dumpFile(ini);
        return false;
37
    }
xiongziliang committed
38
}
xzl committed
39 40
////////////广播名称///////////
namespace Broadcast {
41 42
const string kBroadcastMediaChanged = "kBroadcastMediaChanged";
const string kBroadcastRecordMP4 = "kBroadcastRecordMP4";
43
const string kBroadcastRecordTs = "kBroadcastRecoredTs";
44 45 46 47 48 49 50 51 52 53 54
const string kBroadcastHttpRequest = "kBroadcastHttpRequest";
const string kBroadcastHttpAccess = "kBroadcastHttpAccess";
const string kBroadcastOnGetRtspRealm = "kBroadcastOnGetRtspRealm";
const string kBroadcastOnRtspAuth = "kBroadcastOnRtspAuth";
const string kBroadcastMediaPlayed = "kBroadcastMediaPlayed";
const string kBroadcastMediaPublish = "kBroadcastMediaPublish";
const string kBroadcastFlowReport = "kBroadcastFlowReport";
const string kBroadcastReloadConfig = "kBroadcastReloadConfig";
const string kBroadcastShellLogin = "kBroadcastShellLogin";
const string kBroadcastNotFoundStream = "kBroadcastNotFoundStream";
const string kBroadcastStreamNoneReader = "kBroadcastStreamNoneReader";
55
const string kBroadcastHttpBeforeAccess = "kBroadcastHttpBeforeAccess";
56
} //namespace Broadcast
57

58 59 60
//通用配置项目
namespace General{
#define GENERAL_FIELD "general."
61
const string kMediaServerId = GENERAL_FIELD"mediaServerId";
62 63 64 65
const string kFlowThreshold = GENERAL_FIELD"flowThreshold";
const string kStreamNoneReaderDelayMS = GENERAL_FIELD"streamNoneReaderDelayMS";
const string kMaxStreamWaitTimeMS = GENERAL_FIELD"maxStreamWaitMS";
const string kEnableVhost = GENERAL_FIELD"enableVhost";
66
const string kAddMuteAudio = GENERAL_FIELD"addMuteAudio";
67
const string kResetWhenRePlay = GENERAL_FIELD"resetWhenRePlay";
68 69
const string kPublishToHls = GENERAL_FIELD"publishToHls";
const string kPublishToMP4 = GENERAL_FIELD"publishToMP4";
xiongziliang committed
70
const string kMergeWriteMS = GENERAL_FIELD"mergeWriteMS";
71
const string kModifyStamp = GENERAL_FIELD"modifyStamp";
72

73 74
onceToken token([](){
    mINI::Instance()[kFlowThreshold] = 1024;
xiongziliang committed
75 76
    mINI::Instance()[kStreamNoneReaderDelayMS] = 20 * 1000;
    mINI::Instance()[kMaxStreamWaitTimeMS] = 15 * 1000;
77
    mINI::Instance()[kEnableVhost] = 0;
78 79 80 81
    mINI::Instance()[kAddMuteAudio] = 1;
    mINI::Instance()[kResetWhenRePlay] = 1;
    mINI::Instance()[kPublishToHls] = 1;
    mINI::Instance()[kPublishToMP4] = 0;
82
    mINI::Instance()[kMergeWriteMS] = 0;
83
    mINI::Instance()[kModifyStamp] = 0;
xiongziliang committed
84
    mINI::Instance()[kMediaServerId] = makeRandStr(16);
85
},nullptr);
86 87

}//namespace General
xzl committed
88 89 90 91 92

////////////HTTP配置///////////
namespace Http {
#define HTTP_FIELD "http."
//http 文件发送缓存大小
93
const string kSendBufSize = HTTP_FIELD"sendBufSize";
xzl committed
94
//http 最大请求字节数
95
const string kMaxReqSize = HTTP_FIELD"maxReqSize";
xzl committed
96
//http keep-alive秒数
97
const string kKeepAliveSecond = HTTP_FIELD"keepAliveSecond";
xzl committed
98
//http 字符编码
99
const string kCharSet = HTTP_FIELD"charSet";
xzl committed
100
//http 服务器根目录
101
const string kRootPath = HTTP_FIELD"rootPath";
xzl committed
102
//http 404错误提示内容
103
const string kNotFound = HTTP_FIELD"notFound";
104 105
//是否显示文件夹菜单
const string kDirMenu = HTTP_FIELD"dirMenu";
xzl committed
106 107

onceToken token([](){
108 109 110
    mINI::Instance()[kSendBufSize] = 64 * 1024;
    mINI::Instance()[kMaxReqSize] = 4*1024;
    mINI::Instance()[kKeepAliveSecond] = 15;
111 112
    mINI::Instance()[kDirMenu] = true;

113
#if defined(_WIN32)
114
    mINI::Instance()[kCharSet] = "gb2312";
115
#else
116
    mINI::Instance()[kCharSet] ="utf-8";
117 118
#endif

119 120 121 122 123 124 125 126 127 128 129
    mINI::Instance()[kRootPath] = "./www";
    mINI::Instance()[kNotFound] =
                    "<html>"
                    "<head><title>404 Not Found</title></head>"
                    "<body bgcolor=\"white\">"
                    "<center><h1>您访问的资源不存在!</h1></center>"
                    "<hr><center>"
                    SERVER_NAME
                    "</center>"
                    "</body>"
                    "</html>";
xzl committed
130 131 132 133 134 135 136
},nullptr);

}//namespace Http

////////////SHELL配置///////////
namespace Shell {
#define SHELL_FIELD "shell."
137
const string kMaxReqSize = SHELL_FIELD"maxReqSize";
xzl committed
138 139

onceToken token([](){
140
    mINI::Instance()[kMaxReqSize] = 1024;
xzl committed
141 142 143 144 145 146
},nullptr);
} //namespace Shell

////////////RTSP服务器配置///////////
namespace Rtsp {
#define RTSP_FIELD "rtsp."
147 148 149
const string kAuthBasic = RTSP_FIELD"authBasic";
const string kHandshakeSecond = RTSP_FIELD"handshakeSecond";
const string kKeepAliveSecond = RTSP_FIELD"keepAliveSecond";
150 151
const string kDirectProxy = RTSP_FIELD"directProxy";

xzl committed
152
onceToken token([](){
153 154
    //默认Md5方式认证
    mINI::Instance()[kAuthBasic] = 0;
155 156
    mINI::Instance()[kHandshakeSecond] = 15;
    mINI::Instance()[kKeepAliveSecond] = 15;
157
    mINI::Instance()[kDirectProxy] = 1;
xzl committed
158 159 160 161 162 163
},nullptr);
} //namespace Rtsp

////////////RTMP服务器配置///////////
namespace Rtmp {
#define RTMP_FIELD "rtmp."
164 165 166
const string kModifyStamp = RTMP_FIELD"modifyStamp";
const string kHandshakeSecond = RTMP_FIELD"handshakeSecond";
const string kKeepAliveSecond = RTMP_FIELD"keepAliveSecond";
xzl committed
167 168

onceToken token([](){
169
    mINI::Instance()[kModifyStamp] = false;
170 171
    mINI::Instance()[kHandshakeSecond] = 15;
    mINI::Instance()[kKeepAliveSecond] = 15;
xzl committed
172 173 174 175 176 177 178 179
},nullptr);
} //namespace RTMP


////////////RTP配置///////////
namespace Rtp {
#define RTP_FIELD "rtp."
//RTP打包最大MTU,公网情况下更小
180 181
const string kVideoMtuSize = RTP_FIELD"videoMtuSize";
const string kAudioMtuSize = RTP_FIELD"audioMtuSize";
xzl committed
182
//RTP排序缓存最大个数
183
const string kMaxRtpCount = RTP_FIELD"maxRtpCount";
xzl committed
184
//如果RTP序列正确次数累计达到该数字就启动清空排序缓存
185
const string kClearCount = RTP_FIELD"clearCount";
xzl committed
186
//最大RTP时间为13个小时,每13小时回环一次
187
const string kCycleMS = RTP_FIELD"cycleMS";
xzl committed
188 189

onceToken token([](){
190 191 192 193 194
    mINI::Instance()[kVideoMtuSize] = 1400;
    mINI::Instance()[kAudioMtuSize] = 600;
    mINI::Instance()[kMaxRtpCount] = 50;
    mINI::Instance()[kClearCount] = 10;
    mINI::Instance()[kCycleMS] = 13*60*60*1000;
xzl committed
195 196 197 198 199 200 201
},nullptr);
} //namespace Rtsp

////////////组播配置///////////
namespace MultiCast {
#define MULTI_FIELD "multicast."
//组播分配起始地址
202
const string kAddrMin = MULTI_FIELD"addrMin";
xzl committed
203
//组播分配截止地址
204
const string kAddrMax = MULTI_FIELD"addrMax";
xzl committed
205
//组播TTL
206
const string kUdpTTL = MULTI_FIELD"udpTTL";
xzl committed
207 208

onceToken token([](){
209 210 211
    mINI::Instance()[kAddrMin] = "239.0.0.0";
    mINI::Instance()[kAddrMax] = "239.255.255.255";
    mINI::Instance()[kUdpTTL] = 64;
xzl committed
212 213 214 215 216 217 218
},nullptr);
} //namespace MultiCast

////////////录像配置///////////
namespace Record {
#define RECORD_FIELD "record."
//查看录像的应用名称
219
const string kAppName = RECORD_FIELD"appName";
xzl committed
220
//每次流化MP4文件的时长,单位毫秒
221
const string kSampleMS = RECORD_FIELD"sampleMS";
222
//MP4文件录制大小,默认一个小时
223
const string kFileSecond = RECORD_FIELD"fileSecond";
xzl committed
224
//录制文件路径
225
const string kFilePath = RECORD_FIELD"filePath";
226 227
//mp4文件写缓存大小
const string kFileBufSize = RECORD_FIELD"fileBufSize";
228 229
//mp4录制完成后是否进行二次关键帧索引写入头部
const string kFastStart = RECORD_FIELD"fastStart";
Weiwei.Zhou committed
230 231 232
//mp4文件是否重头循环读取
const string kFileRepeat = RECORD_FIELD"fileRepeat";

xzl committed
233
onceToken token([](){
234 235 236 237 238 239 240
    mINI::Instance()[kAppName] = "record";
    mINI::Instance()[kSampleMS] = 500;
    mINI::Instance()[kFileSecond] = 60*60;
    mINI::Instance()[kFilePath] = "./www";
    mINI::Instance()[kFileBufSize] = 64 * 1024;
    mINI::Instance()[kFastStart] = false;
    mINI::Instance()[kFileRepeat] = false;
xzl committed
241 242 243 244 245 246 247
},nullptr);
} //namespace Record

////////////HLS相关配置///////////
namespace Hls {
#define HLS_FIELD "hls."
//HLS切片时长,单位秒
248
const string kSegmentDuration = HLS_FIELD"segDur";
xzl committed
249
//HLS切片个数
250
const string kSegmentNum = HLS_FIELD"segNum";
251 252
//HLS切片从m3u8文件中移除后,继续保留在磁盘上的个数
const string kSegmentRetain = HLS_FIELD"segRetain";
xzl committed
253
//HLS文件写缓存大小
254
const string kFileBufSize = HLS_FIELD"fileBufSize";
xzl committed
255
//录制文件路径
256
const string kFilePath = HLS_FIELD"filePath";
257 258
// 是否广播 ts 切片完成通知
const string kBroadcastRecordTs = HLS_FIELD"broadcastRecordTs";
xzl committed
259 260

onceToken token([](){
261 262 263 264 265
    mINI::Instance()[kSegmentDuration] = 2;
    mINI::Instance()[kSegmentNum] = 3;
    mINI::Instance()[kSegmentRetain] = 5;
    mINI::Instance()[kFileBufSize] = 64 * 1024;
    mINI::Instance()[kFilePath] = "./www";
266
    mINI::Instance()[kBroadcastRecordTs] = false;
xzl committed
267 268 269
},nullptr);
} //namespace Hls

270 271 272 273 274 275 276 277 278 279 280 281

////////////Rtp代理相关配置///////////
namespace RtpProxy {
#define RTP_PROXY_FIELD "rtp_proxy."
//rtp调试数据保存目录
const string kDumpDir = RTP_PROXY_FIELD"dumpDir";
//是否限制udp数据来源ip和端口
const string kCheckSource = RTP_PROXY_FIELD"checkSource";
//rtp接收超时时间
const string kTimeoutSec = RTP_PROXY_FIELD"timeoutSec";

onceToken token([](){
282 283 284
    mINI::Instance()[kDumpDir] = "";
    mINI::Instance()[kCheckSource] = 1;
    mINI::Instance()[kTimeoutSec] = 15;
285 286 287 288
},nullptr);
} //namespace RtpProxy


xiongziliang committed
289
namespace Client {
290 291 292 293 294 295 296 297 298
const string kNetAdapter = "net_adapter";
const string kRtpType = "rtp_type";
const string kRtspUser = "rtsp_user" ;
const string kRtspPwd = "rtsp_pwd";
const string kRtspPwdIsMD5 = "rtsp_pwd_md5";
const string kTimeoutMS = "protocol_timeout_ms";
const string kMediaTimeoutMS = "media_timeout_ms";
const string kBeatIntervalMS = "beat_interval_ms";
const string kMaxAnalysisMS = "max_analysis_ms";
299 300
const string kBenchmarkMode = "benchmark_mode";

xiongziliang committed
301 302
}

xiongziliang committed
303
}  // namespace mediakit
xzl committed
304 305


306 307 308 309 310 311 312
void Assert_Throw(int failed, const char *exp, const char *func, const char *file, int line){
    if(failed) {
        _StrPrinter printer;
        printer << "Assertion failed: (" << exp << "), function " << func << ", file " << file << ", line " << line << ".";
        throw std::runtime_error(printer);
    }
}