config.h 13 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
 */

xzl committed
11

12 13
#ifndef COMMON_CONFIG_H
#define COMMON_CONFIG_H
xzl committed
14

15
#include <functional>
xiongzilaing committed
16
#include "Util/mini.h"
17
#include "Util/onceToken.h"
18 19
#include "Util/NoticeCenter.h"

20
using namespace std;
xiongziliang committed
21
using namespace toolkit;
xzl committed
22

xiongziliang committed
23
namespace mediakit {
xiongziliang committed
24

25 26 27 28 29 30
//加载配置文件,如果配置文件不存在,那么会导出默认配置并生成配置文件
//加载配置文件成功后会触发kBroadcastUpdateConfig广播
//如果指定的文件名(ini_path)为空,那么会加载默认配置文件
//默认配置文件名为 /path/to/your/exe.ini
//加载配置文件成功后返回true,否则返回false
bool loadIniConfig(const char *ini_path = nullptr);
xzl committed
31 32 33 34 35 36 37 38 39 40 41 42 43
////////////其他宏定义///////////
#ifndef MAX
#define MAX(a,b) ((a) > (b) ? (a) : (b) )
#endif //MAX

#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b) )
#endif //MIN

#ifndef CLEAR_ARR
#define CLEAR_ARR(arr) for(auto &item : arr){ item = 0;}
#endif //CLEAR_ARR

44
#define SERVER_NAME "ZLMediaKit-5.0(build in " __DATE__ " " __TIME__ ")"
45
#define VHOST_KEY "vhost"
46
#define HTTP_SCHEMA "http"
47 48
#define RTSP_SCHEMA "rtsp"
#define RTMP_SCHEMA "rtmp"
49
#define HLS_SCHEMA "hls"
50
#define DEFAULT_VHOST "__defaultVhost__"
xzl committed
51 52 53

////////////广播名称///////////
namespace Broadcast {
54

55
//注册或反注册MediaSource事件广播
56
extern const string kBroadcastMediaChanged;
xiongziliang committed
57
#define BroadcastMediaChangedArgs const bool &bRegist, MediaSource &sender
58

59
//录制mp4文件成功后广播
60
extern const string kBroadcastRecordMP4;
61
#define BroadcastRecordMP4Args const MP4Info &info
62

63
//收到http api请求广播
64
extern const string kBroadcastHttpRequest;
65
#define BroadcastHttpRequestArgs const Parser &parser,const HttpSession::HttpResponseInvoker &invoker,bool &consumed,SockInfo &sender
66

67
//在http文件服务器中,收到http访问文件或目录的广播,通过该事件控制访问http目录的权限
68
extern const string kBroadcastHttpAccess;
69
#define BroadcastHttpAccessArgs const Parser &parser,const string &path,const bool &is_dir,const HttpSession::HttpAccessPathInvoker &invoker,SockInfo &sender
70

71 72 73
//在http文件服务器中,收到http访问文件或目录前的广播,通过该事件可以控制http url到文件路径的映射
//在该事件中通过自行覆盖path参数,可以做到譬如根据虚拟主机或者app选择不同http根目录的目的
extern const string kBroadcastHttpBeforeAccess;
74
#define BroadcastHttpBeforeAccessArgs const Parser &parser,string &path,SockInfo &sender
75

76
//该流是否需要认证?是的话调用invoker并传入realm,否则传入空的realm.如果该事件不监听则不认证
77
extern const string kBroadcastOnGetRtspRealm;
78
#define BroadcastOnGetRtspRealmArgs const MediaInfo &args,const RtspSession::onGetRealm &invoker,SockInfo &sender
79 80 81

//请求认证用户密码事件,user_name为用户名,must_no_encrypt如果为true,则必须提供明文密码(因为此时是base64认证方式),否则会导致认证失败
//获取到密码后请调用invoker并输入对应类型的密码和密码类型,invoker执行时会匹配密码
82
extern const string kBroadcastOnRtspAuth;
83
#define BroadcastOnRtspAuthArgs const MediaInfo &args,const string &realm,const string &user_name,const bool &must_no_encrypt,const RtspSession::onAuth &invoker,SockInfo &sender
84

85
//推流鉴权结果回调对象
86
//如果errMessage为空则代表鉴权成功
87 88 89 90
//enableHls: 是否允许转换hls
//enableMP4: 是否运行MP4录制
//enableRtxp: rtmp推流时是否运行转rtsp;rtsp推流时,是否允许转rtmp
typedef std::function<void(const string &errMessage,bool enableRtxp,bool enableHls,bool enableMP4)> PublishAuthInvoker;
91

92
//收到rtsp/rtmp推流事件广播,通过该事件控制推流鉴权
93
extern const string kBroadcastMediaPublish;
94
#define BroadcastMediaPublishArgs const MediaInfo &args,const Broadcast::PublishAuthInvoker &invoker,SockInfo &sender
95 96 97 98

//播放鉴权结果回调对象
//如果errMessage为空则代表鉴权成功
typedef std::function<void(const string &errMessage)> AuthInvoker;
99

100
//播放rtsp/rtmp/http-flv事件广播,通过该事件控制播放鉴权
101
extern const string kBroadcastMediaPlayed;
102
#define BroadcastMediaPlayedArgs const MediaInfo &args,const Broadcast::AuthInvoker &invoker,SockInfo &sender
103

104
//shell登录鉴权
105
extern const string kBroadcastShellLogin;
106
#define BroadcastShellLoginArgs const string &user_name,const string &passwd,const Broadcast::AuthInvoker &invoker,SockInfo &sender
107

108
//停止rtsp/rtmp/http-flv会话后流量汇报事件广播
109
extern const string kBroadcastFlowReport;
110
#define BroadcastFlowReportArgs const MediaInfo &args,const uint64_t &totalBytes,const uint64_t &totalDuration,const bool &isPlayer, SockInfo &sender
111

112
//未找到流后会广播该事件,请在监听该事件后去拉流或其他方式产生流,这样就能按需拉流了
113
extern const string kBroadcastNotFoundStream;
114
#define BroadcastNotFoundStreamArgs const MediaInfo &args,SockInfo &sender
115

116
//某个流无人消费时触发,目的为了实现无人观看时主动断开拉流等业务逻辑
117
extern const string kBroadcastStreamNoneReader;
118 119
#define BroadcastStreamNoneReaderArgs MediaSource &sender

120
//更新配置文件事件广播,执行loadIniConfig函数加载配置文件成功后会触发该广播
121
extern const string kBroadcastReloadConfig;
122
#define BroadcastReloadConfigArgs void
123

124
#define ReloadConfigTag  ((void *)(0xFF))
125 126 127 128 129 130 131 132 133 134
#define RELOAD_KEY(arg,key) \
    do{ \
        decltype(arg) arg##tmp = mINI::Instance()[key]; \
        if(arg != arg##tmp ) { \
            arg = arg##tmp; \
            InfoL << "reload config:" << key << "=" <<  arg; \
        } \
    }while(0);

//监听某个配置发送变更
135
#define LISTEN_RELOAD_KEY(arg,key) \
136 137
    do{ \
        static onceToken s_token([](){ \
xiongziliang committed
138
            NoticeCenter::Instance().addListener(ReloadConfigTag,Broadcast::kBroadcastReloadConfig,[](BroadcastReloadConfigArgs){ \
139 140 141 142 143
                RELOAD_KEY(arg,key); \
            }); \
        }); \
    }while(0);

144
#define GET_CONFIG(type,arg,key) \
145
        static type arg = mINI::Instance()[key]; \
146
        LISTEN_RELOAD_KEY(arg,key);
147

xzl committed
148 149
} //namespace Broadcast

150 151 152
////////////通用配置///////////
namespace General{
//流量汇报事件流量阈值,单位KB,默认1MB
153
extern const string kFlowThreshold;
154 155
//流无人观看并且超过若干时间后才触发kBroadcastStreamNoneReader事件
//默认连续5秒无人观看然后触发kBroadcastStreamNoneReader事件
156
extern const string kStreamNoneReaderDelayMS;
157 158 159
//等待流注册超时时间,收到播放器后请求后,如果未找到相关流,服务器会等待一定时间,
//如果在这个时间内,相关流注册上了,那么服务器会立即响应播放器播放成功,
//否则会最多等待kMaxStreamWaitTimeMS毫秒,然后响应播放器播放失败
160
extern const string kMaxStreamWaitTimeMS;
161
//是否启动虚拟主机
162
extern const string kEnableVhost;
163 164
//拉流代理时是否添加静音音频
extern const string kAddMuteAudio;
165 166 167
//拉流代理时如果断流再重连成功是否删除前一次的媒体流数据,如果删除将重新开始,
//如果不删除将会接着上一次的数据继续写(录制hls/mp4时会继续在前一个文件后面写)
extern const string kResetWhenRePlay;
xiongziliang committed
168
//是否默认推流时转换成rtsp或rtmp,hook接口(on_publish)中可以覆盖该设置
169
extern const string kPublishToRtxp ;
xiongziliang committed
170
//是否默认推流时转换成hls,hook接口(on_publish)中可以覆盖该设置
171
extern const string kPublishToHls ;
xiongziliang committed
172
//是否默认推流时mp4录像,hook接口(on_publish)中可以覆盖该设置
173
extern const string kPublishToMP4 ;
xiongziliang committed
174
//合并写缓存大小(单位毫秒),合并写指服务器缓存一定的数据后才会一次性写入socket,这样能提高性能,但是会提高延时
175
//开启后会同时关闭TCP_NODELAY并开启MSG_MORE
xiongziliang committed
176
extern const string kMergeWriteMS ;
177 178
//全局的时间戳覆盖开关,在转协议时,对frame进行时间戳覆盖
extern const string kModifyStamp;
179 180 181
}//namespace General


xzl committed
182 183 184
////////////HTTP配置///////////
namespace Http {
//http 文件发送缓存大小
185
extern const string kSendBufSize;
xzl committed
186
//http 最大请求字节数
187
extern const string kMaxReqSize;
xzl committed
188
//http keep-alive秒数
189
extern const string kKeepAliveSecond;
xzl committed
190
//http 字符编码
191
extern const string kCharSet;
xzl committed
192
//http 服务器根目录
193
extern const string kRootPath;
xzl committed
194
//http 404错误提示内容
195
extern const string kNotFound;
196 197
//是否显示文件夹菜单
extern const string kDirMenu;
xzl committed
198 199 200 201
}//namespace Http

////////////SHELL配置///////////
namespace Shell {
202
extern const string kMaxReqSize;
xzl committed
203 204 205 206
} //namespace Shell

////////////RTSP服务器配置///////////
namespace Rtsp {
207
//是否优先base64方式认证?默认Md5方式认证
208
extern const string kAuthBasic;
209
//握手超时时间,默认15秒
210
extern const string kHandshakeSecond;
211
//维持链接超时时间,默认15秒
212
extern const string kKeepAliveSecond;
213 214

//rtsp拉流代理是否直接代理
xiongziliang committed
215
//直接代理后支持任意编码格式,但是会导致GOP缓存无法定位到I帧,可能会导致开播花屏
216 217 218 219
//并且如果是tcp方式拉流,如果rtp大于mtu会导致无法使用udp方式代理
//假定您的拉流源地址不是264或265或AAC,那么你可以使用直接代理的方式来支持rtsp代理
//默认开启rtsp直接代理,rtmp由于没有这些问题,是强制开启直接代理的
extern const string kDirectProxy;
xzl committed
220 221 222 223
} //namespace Rtsp

////////////RTMP服务器配置///////////
namespace Rtmp {
224
//rtmp推流时间戳覆盖开关
225
extern const string kModifyStamp;
226
//握手超时时间,默认15秒
227
extern const string kHandshakeSecond;
228
//维持链接超时时间,默认15秒
229
extern const string kKeepAliveSecond;
xzl committed
230 231 232 233 234 235
} //namespace RTMP


////////////RTP配置///////////
namespace Rtp {
//RTP打包最大MTU,公网情况下更小
236
extern const string kVideoMtuSize;
xzl committed
237
//RTP打包最大MTU,公网情况下更小
238
extern const string kAudioMtuSize;
xzl committed
239
//RTP排序缓存最大个数
240
extern const string kMaxRtpCount;
xzl committed
241
//如果RTP序列正确次数累计达到该数字就启动清空排序缓存
242
extern const string kClearCount;
xzl committed
243
//最大RTP时间为13个小时,每13小时回环一次
244
extern const string kCycleMS;
xzl committed
245 246 247 248 249
} //namespace Rtsp

////////////组播配置///////////
namespace MultiCast {
//组播分配起始地址
250
extern const string kAddrMin;
xzl committed
251
//组播分配截止地址
252
extern const string kAddrMax;
xzl committed
253
//组播TTL
254
extern const string kUdpTTL;
xzl committed
255 256 257 258 259
} //namespace MultiCast

////////////录像配置///////////
namespace Record {
//查看录像的应用名称
260
extern const string kAppName;
xzl committed
261
//每次流化MP4文件的时长,单位毫秒
262
extern const string kSampleMS;
263
//MP4文件录制大小,默认一个小时
264
extern const string kFileSecond;
xzl committed
265
//录制文件路径
266
extern const string kFilePath;
267 268
//mp4文件写缓存大小
extern const string kFileBufSize;
269 270
//mp4录制完成后是否进行二次关键帧索引写入头部
extern const string kFastStart;
Weiwei.Zhou committed
271 272
//mp4文件是否重头循环读取
extern const string kFileRepeat;
xzl committed
273 274 275 276 277
} //namespace Record

////////////HLS相关配置///////////
namespace Hls {
//HLS切片时长,单位秒
278
extern const string kSegmentDuration;
279
//m3u8文件中HLS切片个数,如果设置为0,则不删除切片,而是保存为点播
280
extern const string kSegmentNum;
281 282
//HLS切片从m3u8文件中移除后,继续保留在磁盘上的个数
extern const string kSegmentRetain;
xzl committed
283
//HLS文件写缓存大小
284
extern const string kFileBufSize;
xzl committed
285
//录制文件路径
286
extern const string kFilePath;
xzl committed
287 288
} //namespace Hls

289 290 291 292 293 294 295 296 297
////////////Rtp代理相关配置///////////
namespace RtpProxy {
//rtp调试数据保存目录,置空则不生成
extern const string kDumpDir;
//是否限制udp数据来源ip和端口
extern const string kCheckSource;
//rtp接收超时时间
extern const string kTimeoutSec;
} //namespace RtpProxy
xiongziliang committed
298

xiongziliang committed
299 300 301 302 303
/**
 * rtsp/rtmp播放器、推流器相关设置名,
 * 这些设置项都不是配置文件用
 * 只用于设置某个播放器或推流器实例用
 */
xiongziliang committed
304 305
namespace Client {
//指定网卡ip
306
extern const string kNetAdapter;
xiongziliang committed
307 308
//设置rtp传输类型,可选项有0(tcp,默认)、1(udp)、2(组播)
//设置方法:player[PlayerBase::kRtpType] = 0/1/2;
309
extern const string kRtpType;
xiongziliang committed
310
//rtsp认证用户名
311
extern const string kRtspUser;
xiongziliang committed
312
//rtsp认证用用户密码,可以是明文也可以是md5,md5密码生成方式 md5(username:realm:password)
313
extern const string kRtspPwd;
xiongziliang committed
314
//rtsp认证用用户密码是否为md5类型
315
extern const string kRtspPwdIsMD5;
xiongziliang committed
316
//握手超时时间,默认10,000 毫秒
317
extern const string kTimeoutMS;
xiongziliang committed
318
//rtp/rtmp包接收超时时间,默认5000秒
319
extern const string kMediaTimeoutMS;
xiongziliang committed
320
//rtsp/rtmp心跳时间,默认5000毫秒
321
extern const string kBeatIntervalMS;
xiongziliang committed
322
//Track编码格式探测最大时间,单位毫秒,默认2000
323
extern const string kMaxAnalysisMS;
324 325
//是否为性能测试模式,性能测试模式开启后不会解析rtp或rtmp包
extern const string kBenchmarkMode;
xiongziliang committed
326
}
xiongziliang committed
327
}  // namespace mediakit
xzl committed
328

329
#endif /* COMMON_CONFIG_H */