ZLMediaKit/src/Common/config.cpp

420 lines
13 KiB
C++
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2017-09-27 16:20:30 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2017-09-27 16:20:30 +08:00
*
2020-04-04 20:30:09 +08:00
* 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.
2017-09-27 16:20:30 +08:00
*/
2017-05-02 17:15:12 +08:00
#include "Common/config.h"
2017-04-01 16:35:56 +08:00
#include "Util/util.h"
2019-05-10 13:25:22 +08:00
#include "Util/logger.h"
2017-04-01 16:35:56 +08:00
#include "Util/onceToken.h"
2019-05-10 13:25:22 +08:00
#include "Util/NoticeCenter.h"
2017-04-01 16:35:56 +08:00
#include "Network/sockutil.h"
2018-10-24 17:17:55 +08:00
using namespace toolkit;
2017-04-01 16:35:56 +08:00
2018-10-24 17:17:55 +08:00
namespace mediakit {
2017-04-01 16:35:56 +08:00
bool loadIniConfig(const char *ini_path){
string ini;
2019-05-20 11:22:59 +08:00
if(ini_path && ini_path[0] != '\0'){
ini = ini_path;
}else{
ini = exePath() + ".ini";
}
2020-03-20 11:51:24 +08:00
try{
mINI::Instance().parseFile(ini);
2018-02-09 11:42:55 +08:00
NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastReloadConfig);
return true;
}catch (std::exception &) {
2020-03-20 11:51:24 +08:00
InfoL << "dump ini file to:" << ini;
mINI::Instance().dumpFile(ini);
return false;
2020-03-20 11:51:24 +08:00
}
2017-05-05 18:03:23 +08:00
}
2017-04-01 16:35:56 +08:00
////////////广播名称///////////
namespace Broadcast {
2019-06-12 18:37:52 +08:00
const string kBroadcastMediaChanged = "kBroadcastMediaChanged";
const string kBroadcastRecordMP4 = "kBroadcastRecordMP4";
2020-09-13 14:08:05 +08:00
const string kBroadcastRecordTs = "kBroadcastRecoredTs";
2019-06-12 18:37:52 +08:00
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";
const string kBroadcastHttpBeforeAccess = "kBroadcastHttpBeforeAccess";
2019-05-28 17:14:36 +08:00
} //namespace Broadcast
2019-05-28 17:14:36 +08:00
//通用配置项目
namespace General{
#define GENERAL_FIELD "general."
const string kMediaServerId = GENERAL_FIELD"mediaServerId";
2019-06-12 18:37:52 +08:00
const string kFlowThreshold = GENERAL_FIELD"flowThreshold";
const string kStreamNoneReaderDelayMS = GENERAL_FIELD"streamNoneReaderDelayMS";
const string kMaxStreamWaitTimeMS = GENERAL_FIELD"maxStreamWaitMS";
const string kEnableVhost = GENERAL_FIELD"enableVhost";
2019-09-29 10:33:05 +08:00
const string kAddMuteAudio = GENERAL_FIELD"addMuteAudio";
const string kResetWhenRePlay = GENERAL_FIELD"resetWhenRePlay";
const string kPublishToHls = GENERAL_FIELD"publishToHls";
const string kPublishToMP4 = GENERAL_FIELD"publishToMP4";
2020-04-09 16:19:03 +08:00
const string kMergeWriteMS = GENERAL_FIELD"mergeWriteMS";
2020-05-25 12:07:38 +08:00
const string kModifyStamp = GENERAL_FIELD"modifyStamp";
const string kHlsDemand = GENERAL_FIELD"hls_demand";
const string kRtspDemand = GENERAL_FIELD"rtsp_demand";
const string kRtmpDemand = GENERAL_FIELD"rtmp_demand";
const string kTSDemand = GENERAL_FIELD"ts_demand";
const string kFMP4Demand = GENERAL_FIELD"fmp4_demand";
2021-05-22 10:17:52 +08:00
const string kEnableAudio = GENERAL_FIELD"enable_audio";
2019-09-29 10:33:05 +08:00
onceToken token([](){
mINI::Instance()[kFlowThreshold] = 1024;
2019-12-29 11:52:02 +08:00
mINI::Instance()[kStreamNoneReaderDelayMS] = 20 * 1000;
mINI::Instance()[kMaxStreamWaitTimeMS] = 15 * 1000;
2019-12-26 12:23:38 +08:00
mINI::Instance()[kEnableVhost] = 0;
2020-03-20 11:51:24 +08:00
mINI::Instance()[kAddMuteAudio] = 1;
mINI::Instance()[kResetWhenRePlay] = 1;
mINI::Instance()[kPublishToHls] = 1;
mINI::Instance()[kPublishToMP4] = 0;
2020-04-29 11:08:43 +08:00
mINI::Instance()[kMergeWriteMS] = 0;
2020-05-25 12:07:38 +08:00
mINI::Instance()[kModifyStamp] = 0;
2020-10-01 14:37:52 +08:00
mINI::Instance()[kMediaServerId] = makeRandStr(16);
mINI::Instance()[kHlsDemand] = 0;
mINI::Instance()[kRtspDemand] = 0;
mINI::Instance()[kRtmpDemand] = 0;
mINI::Instance()[kTSDemand] = 0;
mINI::Instance()[kFMP4Demand] = 0;
2021-05-22 10:17:52 +08:00
mINI::Instance()[kEnableAudio] = 1;
},nullptr);
2019-05-28 17:14:36 +08:00
}//namespace General
2017-04-01 16:35:56 +08:00
////////////HTTP配置///////////
namespace Http {
#define HTTP_FIELD "http."
//http 文件发送缓存大小
2019-06-12 18:37:52 +08:00
const string kSendBufSize = HTTP_FIELD"sendBufSize";
2017-04-01 16:35:56 +08:00
//http 最大请求字节数
2019-06-12 18:37:52 +08:00
const string kMaxReqSize = HTTP_FIELD"maxReqSize";
2017-04-01 16:35:56 +08:00
//http keep-alive秒数
2019-06-12 18:37:52 +08:00
const string kKeepAliveSecond = HTTP_FIELD"keepAliveSecond";
2017-04-01 16:35:56 +08:00
//http 字符编码
2019-06-12 18:37:52 +08:00
const string kCharSet = HTTP_FIELD"charSet";
2017-04-01 16:35:56 +08:00
//http 服务器根目录
2019-06-12 18:37:52 +08:00
const string kRootPath = HTTP_FIELD"rootPath";
2017-04-01 16:35:56 +08:00
//http 404错误提示内容
2019-06-12 18:37:52 +08:00
const string kNotFound = HTTP_FIELD"notFound";
//是否显示文件夹菜单
const string kDirMenu = HTTP_FIELD"dirMenu";
2017-04-01 16:35:56 +08:00
onceToken token([](){
2020-03-20 11:51:24 +08:00
mINI::Instance()[kSendBufSize] = 64 * 1024;
mINI::Instance()[kMaxReqSize] = 4*1024;
mINI::Instance()[kKeepAliveSecond] = 15;
mINI::Instance()[kDirMenu] = true;
#if defined(_WIN32)
2020-03-20 11:51:24 +08:00
mINI::Instance()[kCharSet] = "gb2312";
#else
2020-03-20 11:51:24 +08:00
mINI::Instance()[kCharSet] ="utf-8";
#endif
2020-03-20 11:51:24 +08:00
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>";
2017-04-01 16:35:56 +08:00
},nullptr);
}//namespace Http
////////////SHELL配置///////////
namespace Shell {
#define SHELL_FIELD "shell."
2019-06-12 18:37:52 +08:00
const string kMaxReqSize = SHELL_FIELD"maxReqSize";
2017-04-01 16:35:56 +08:00
onceToken token([](){
2020-03-20 11:51:24 +08:00
mINI::Instance()[kMaxReqSize] = 1024;
2017-04-01 16:35:56 +08:00
},nullptr);
} //namespace Shell
////////////RTSP服务器配置///////////
namespace Rtsp {
#define RTSP_FIELD "rtsp."
2019-06-12 18:37:52 +08:00
const string kAuthBasic = RTSP_FIELD"authBasic";
const string kHandshakeSecond = RTSP_FIELD"handshakeSecond";
const string kKeepAliveSecond = RTSP_FIELD"keepAliveSecond";
const string kDirectProxy = RTSP_FIELD"directProxy";
2017-04-01 16:35:56 +08:00
onceToken token([](){
2020-03-20 11:51:24 +08:00
//默认Md5方式认证
mINI::Instance()[kAuthBasic] = 0;
2019-05-29 18:24:35 +08:00
mINI::Instance()[kHandshakeSecond] = 15;
mINI::Instance()[kKeepAliveSecond] = 15;
2020-03-20 11:51:24 +08:00
mINI::Instance()[kDirectProxy] = 1;
2017-04-01 16:35:56 +08:00
},nullptr);
} //namespace Rtsp
////////////RTMP服务器配置///////////
namespace Rtmp {
#define RTMP_FIELD "rtmp."
2019-06-12 18:37:52 +08:00
const string kModifyStamp = RTMP_FIELD"modifyStamp";
const string kHandshakeSecond = RTMP_FIELD"handshakeSecond";
const string kKeepAliveSecond = RTMP_FIELD"keepAliveSecond";
2017-04-01 16:35:56 +08:00
onceToken token([](){
2020-03-20 11:51:24 +08:00
mINI::Instance()[kModifyStamp] = false;
2019-05-29 18:24:35 +08:00
mINI::Instance()[kHandshakeSecond] = 15;
mINI::Instance()[kKeepAliveSecond] = 15;
2017-04-01 16:35:56 +08:00
},nullptr);
} //namespace RTMP
////////////RTP配置///////////
namespace Rtp {
#define RTP_FIELD "rtp."
//RTP打包最大MTU,公网情况下更小
2019-06-12 18:37:52 +08:00
const string kVideoMtuSize = RTP_FIELD"videoMtuSize";
const string kAudioMtuSize = RTP_FIELD"audioMtuSize";
2017-04-01 16:35:56 +08:00
onceToken token([](){
2020-03-20 11:51:24 +08:00
mINI::Instance()[kVideoMtuSize] = 1400;
mINI::Instance()[kAudioMtuSize] = 600;
2017-04-01 16:35:56 +08:00
},nullptr);
} //namespace Rtsp
////////////组播配置///////////
namespace MultiCast {
#define MULTI_FIELD "multicast."
//组播分配起始地址
2019-06-12 18:37:52 +08:00
const string kAddrMin = MULTI_FIELD"addrMin";
2017-04-01 16:35:56 +08:00
//组播分配截止地址
2019-06-12 18:37:52 +08:00
const string kAddrMax = MULTI_FIELD"addrMax";
2017-04-01 16:35:56 +08:00
//组播TTL
2019-06-12 18:37:52 +08:00
const string kUdpTTL = MULTI_FIELD"udpTTL";
2017-04-01 16:35:56 +08:00
onceToken token([](){
2020-03-20 11:51:24 +08:00
mINI::Instance()[kAddrMin] = "239.0.0.0";
mINI::Instance()[kAddrMax] = "239.255.255.255";
mINI::Instance()[kUdpTTL] = 64;
2017-04-01 16:35:56 +08:00
},nullptr);
} //namespace MultiCast
////////////录像配置///////////
namespace Record {
#define RECORD_FIELD "record."
//查看录像的应用名称
2019-06-12 18:37:52 +08:00
const string kAppName = RECORD_FIELD"appName";
2017-04-01 16:35:56 +08:00
//每次流化MP4文件的时长,单位毫秒
2019-06-12 18:37:52 +08:00
const string kSampleMS = RECORD_FIELD"sampleMS";
2019-04-03 11:09:50 +08:00
//MP4文件录制大小,默认一个小时
2019-06-12 18:37:52 +08:00
const string kFileSecond = RECORD_FIELD"fileSecond";
2017-04-01 16:35:56 +08:00
//录制文件路径
2019-06-12 18:37:52 +08:00
const string kFilePath = RECORD_FIELD"filePath";
2019-08-01 22:13:26 +08:00
//mp4文件写缓存大小
const string kFileBufSize = RECORD_FIELD"fileBufSize";
//mp4录制完成后是否进行二次关键帧索引写入头部
const string kFastStart = RECORD_FIELD"fastStart";
2019-09-24 20:48:30 +08:00
//mp4文件是否重头循环读取
const string kFileRepeat = RECORD_FIELD"fileRepeat";
2017-04-01 16:35:56 +08:00
onceToken token([](){
2020-03-20 11:51:24 +08:00
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;
2017-04-01 16:35:56 +08:00
},nullptr);
} //namespace Record
////////////HLS相关配置///////////
namespace Hls {
#define HLS_FIELD "hls."
//HLS切片时长,单位秒
2019-06-12 18:37:52 +08:00
const string kSegmentDuration = HLS_FIELD"segDur";
2017-04-01 16:35:56 +08:00
//HLS切片个数
2019-06-12 18:37:52 +08:00
const string kSegmentNum = HLS_FIELD"segNum";
//HLS切片从m3u8文件中移除后继续保留在磁盘上的个数
const string kSegmentRetain = HLS_FIELD"segRetain";
2017-04-01 16:35:56 +08:00
//HLS文件写缓存大小
2019-06-12 18:37:52 +08:00
const string kFileBufSize = HLS_FIELD"fileBufSize";
2017-04-01 16:35:56 +08:00
//录制文件路径
2019-06-12 18:37:52 +08:00
const string kFilePath = HLS_FIELD"filePath";
// 是否广播 ts 切片完成通知
const string kBroadcastRecordTs = HLS_FIELD"broadcastRecordTs";
2017-04-01 16:35:56 +08:00
onceToken token([](){
2020-03-20 11:51:24 +08:00
mINI::Instance()[kSegmentDuration] = 2;
mINI::Instance()[kSegmentNum] = 3;
mINI::Instance()[kSegmentRetain] = 5;
mINI::Instance()[kFileBufSize] = 64 * 1024;
mINI::Instance()[kFilePath] = "./www";
mINI::Instance()[kBroadcastRecordTs] = false;
2017-04-01 16:35:56 +08:00
},nullptr);
} //namespace Hls
2019-12-06 11:54:10 +08:00
////////////Rtp代理相关配置///////////
namespace RtpProxy {
#define RTP_PROXY_FIELD "rtp_proxy."
//rtp调试数据保存目录
const string kDumpDir = RTP_PROXY_FIELD"dumpDir";
//rtp接收超时时间
const string kTimeoutSec = RTP_PROXY_FIELD"timeoutSec";
onceToken token([](){
2020-03-20 11:51:24 +08:00
mINI::Instance()[kDumpDir] = "";
mINI::Instance()[kTimeoutSec] = 15;
2019-12-06 11:54:10 +08:00
},nullptr);
} //namespace RtpProxy
2019-03-27 18:56:49 +08:00
namespace Client {
2019-06-12 18:37:52 +08:00
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";
2020-04-08 11:16:09 +08:00
const string kBenchmarkMode = "benchmark_mode";
2019-03-27 18:56:49 +08:00
}
2018-10-24 17:17:55 +08:00
} // namespace mediakit
2017-04-01 16:35:56 +08:00
2021-02-21 21:28:17 +08:00
extern "C" {
void Assert_Throw(int failed, const char *exp, const char *func, const char *file, int line) {
if (failed) {
_StrPrinter printer;
2021-02-21 21:28:17 +08:00
printer << "Assertion failed: (" << exp << "), function " << func << ", file " << file << ", line " << line
<< ".";
throw std::runtime_error(printer);
}
}
2021-02-21 21:28:17 +08:00
}
#ifdef ENABLE_MEM_DEBUG
static atomic<uint64_t> mem_usage(0);
uint64_t getTotalMemUsage() {
return mem_usage.load();
}
extern "C" {
#include <stdio.h>
#define MAGIC_BYTES 0xFEFDFCFB
#define MAGIC_BYTES_SIZE 4
#define MEM_PREFIX_SIZE 8
extern void *__real_malloc(size_t);
extern void __real_free(void *);
extern void *__real_realloc(void *ptr, size_t c);
void *__wrap_malloc(size_t c) {
c += MEM_PREFIX_SIZE;
char *ret = (char *) __real_malloc(c);
if (ret) {
mem_usage += c;
*((uint32_t *) (ret)) = MAGIC_BYTES;
*((uint32_t *) (ret + MAGIC_BYTES_SIZE)) = c;
return ret + MEM_PREFIX_SIZE;
}
return nullptr;
}
void __wrap_free(void *ptr) {
if (!ptr) {
return;
}
ptr = (char *) ptr - MEM_PREFIX_SIZE;
uint32_t magic = *((uint32_t *) (ptr));
if (magic != MAGIC_BYTES) {
throw std::invalid_argument("attempt to free invalid memory");
}
mem_usage -= *((uint32_t *) ((char *) ptr + MAGIC_BYTES_SIZE));
__real_free(ptr);
}
void *__wrap_calloc(size_t __nmemb, size_t __size) {
auto size = __nmemb * __size;
auto ret = malloc(size);
if (ret) {
memset(ret, 0, size);
}
return ret;
}
void *__wrap_realloc(void *ptr, size_t c){
if (!ptr) {
return malloc(c);
}
c += MEM_PREFIX_SIZE;
ptr = (char *) ptr - MEM_PREFIX_SIZE;
uint32_t magic = *((uint32_t *) (ptr));
if (magic != MAGIC_BYTES) {
throw std::invalid_argument("attempt to realloc invalid memory");
}
auto old_size = *((uint32_t *) ((char *) ptr + MAGIC_BYTES_SIZE));
char *ret = (char *) __real_realloc(ptr, c);
if (ret) {
mem_usage += c - old_size;
*((uint32_t *) (ret)) = MAGIC_BYTES;
*((uint32_t *) (ret + MAGIC_BYTES_SIZE)) = c;
return ret + MEM_PREFIX_SIZE;
}
free(ptr);
mem_usage -= old_size;
return nullptr;
}
}
void *operator new(std::size_t size) {
auto ret = malloc(size);
if (ret) {
return ret;
}
throw std::bad_alloc();
}
void operator delete(void *ptr) {
free(ptr);
}
void *operator new[](std::size_t size) {
auto ret = malloc(size);
if (ret) {
return ret;
}
throw std::bad_alloc();
}
void operator delete[](void *ptr) {
free(ptr);
}
#endif