config.h 10.9 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2 3
 * MIT License
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
xiongziliang committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

xzl committed
27

28 29
#ifndef COMMON_CONFIG_H
#define COMMON_CONFIG_H
xzl committed
30

31
#include <functional>
xiongzilaing committed
32
#include "Util/mini.h"
33
#include "Util/onceToken.h"
34 35
#include "Util/NoticeCenter.h"

36
using namespace std;
xiongziliang committed
37
using namespace toolkit;
xzl committed
38

xiongziliang committed
39
namespace mediakit {
xiongziliang committed
40

41 42 43 44 45 46
//加载配置文件,如果配置文件不存在,那么会导出默认配置并生成配置文件
//加载配置文件成功后会触发kBroadcastUpdateConfig广播
//如果指定的文件名(ini_path)为空,那么会加载默认配置文件
//默认配置文件名为 /path/to/your/exe.ini
//加载配置文件成功后返回true,否则返回false
bool loadIniConfig(const char *ini_path = nullptr);
xzl committed
47 48 49 50 51 52 53 54 55 56 57 58 59
////////////其他宏定义///////////
#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

xiongziliang committed
60
#define SERVER_NAME "ZLMediaKit-4.0"
61
#define VHOST_KEY "vhost"
62
#define HTTP_SCHEMA "http"
63 64 65
#define RTSP_SCHEMA "rtsp"
#define RTMP_SCHEMA "rtmp"
#define DEFAULT_VHOST "__defaultVhost__"
xzl committed
66 67 68

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

70
//注册或反注册MediaSource事件广播
71
extern const string kBroadcastMediaChanged;
72
#define BroadcastMediaChangedArgs const bool &bRegist, const string &schema,const string &vhost,const string &app,const string &stream,MediaSource &sender
73

74
//录制mp4文件成功后广播
75
extern const string kBroadcastRecordMP4;
76
#define BroadcastRecordMP4Args const Mp4Info &info
77

78
//收到http api请求广播
79
extern const string kBroadcastHttpRequest;
80
#define BroadcastHttpRequestArgs const Parser &parser,const HttpSession::HttpResponseInvoker &invoker,bool &consumed,TcpSession &sender
81

82
//收到http 访问文件或目录的广播,通过该事件控制访问http目录的权限
83
extern const string kBroadcastHttpAccess;
84 85
#define BroadcastHttpAccessArgs const Parser &parser,const MediaInfo &args,const string &path,const bool &is_dir,const HttpSession::HttpAccessPathInvoker &invoker,TcpSession &sender

86
//该流是否需要认证?是的话调用invoker并传入realm,否则传入空的realm.如果该事件不监听则不认证
87
extern const string kBroadcastOnGetRtspRealm;
xiongziliang committed
88
#define BroadcastOnGetRtspRealmArgs const MediaInfo &args,const RtspSession::onGetRealm &invoker,TcpSession &sender
89 90 91

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

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

99
//收到rtsp/rtmp推流事件广播,通过该事件控制推流鉴权
100
extern const string kBroadcastMediaPublish;
101 102
#define BroadcastMediaPublishArgs const MediaInfo &args,const Broadcast::AuthInvoker &invoker,TcpSession &sender

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

107
//shell登录鉴权
108
extern const string kBroadcastShellLogin;
109
#define BroadcastShellLoginArgs const string &user_name,const string &passwd,const Broadcast::AuthInvoker &invoker,TcpSession &sender
110

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

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

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

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

127
#define ReloadConfigTag  ((void *)(0xFF))
128 129 130 131 132 133 134 135 136 137
#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);

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

147
#define GET_CONFIG(type,arg,key) \
148
        static type arg = mINI::Instance()[key]; \
149
        LISTEN_RELOAD_KEY(arg,key);
150

151 152 153 154 155

//兼容老代码
#define GET_CONFIG_AND_REGISTER GET_CONFIG
#define BroadcastRtmpPublishArgs BroadcastMediaPublishArgs
#define kBroadcastRtmpPublish kBroadcastMediaPublish
xzl committed
156 157
} //namespace Broadcast

158 159 160
////////////通用配置///////////
namespace General{
//流量汇报事件流量阈值,单位KB,默认1MB
161
extern const string kFlowThreshold;
162 163
//流无人观看并且超过若干时间后才触发kBroadcastStreamNoneReader事件
//默认连续5秒无人观看然后触发kBroadcastStreamNoneReader事件
164
extern const string kStreamNoneReaderDelayMS;
165 166 167
//等待流注册超时时间,收到播放器后请求后,如果未找到相关流,服务器会等待一定时间,
//如果在这个时间内,相关流注册上了,那么服务器会立即响应播放器播放成功,
//否则会最多等待kMaxStreamWaitTimeMS毫秒,然后响应播放器播放失败
168
extern const string kMaxStreamWaitTimeMS;
169
//是否启动虚拟主机
170
extern const string kEnableVhost;
171 172 173
}//namespace General


xzl committed
174 175 176
////////////HTTP配置///////////
namespace Http {
//http 文件发送缓存大小
177
extern const string kSendBufSize;
xzl committed
178
//http 最大请求字节数
179
extern const string kMaxReqSize;
xzl committed
180
//http keep-alive秒数
181
extern const string kKeepAliveSecond;
xzl committed
182
//http keep-alive最大请求数
183
extern const string kMaxReqCount;
xzl committed
184
//http 字符编码
185
extern const string kCharSet;
xzl committed
186
//http 服务器根目录
187
extern const string kRootPath;
xzl committed
188
//http 404错误提示内容
189
extern const string kNotFound;
xzl committed
190 191 192 193
}//namespace Http

////////////SHELL配置///////////
namespace Shell {
194
extern const string kMaxReqSize;
xzl committed
195 196 197 198
} //namespace Shell

////////////RTSP服务器配置///////////
namespace Rtsp {
199
//是否优先base64方式认证?默认Md5方式认证
200
extern const string kAuthBasic;
201
//握手超时时间,默认15秒
202
extern const string kHandshakeSecond;
203
//维持链接超时时间,默认15秒
204
extern const string kKeepAliveSecond;
xzl committed
205 206 207 208
} //namespace Rtsp

////////////RTMP服务器配置///////////
namespace Rtmp {
209
extern const string kModifyStamp;
210
//握手超时时间,默认15秒
211
extern const string kHandshakeSecond;
212
//维持链接超时时间,默认15秒
213
extern const string kKeepAliveSecond;
xzl committed
214 215 216 217 218 219
} //namespace RTMP


////////////RTP配置///////////
namespace Rtp {
//RTP打包最大MTU,公网情况下更小
220
extern const string kVideoMtuSize;
xzl committed
221
//RTP打包最大MTU,公网情况下更小
222
extern const string kAudioMtuSize;
xzl committed
223
//RTP排序缓存最大个数
224
extern const string kMaxRtpCount;
xzl committed
225
//如果RTP序列正确次数累计达到该数字就启动清空排序缓存
226
extern const string kClearCount;
xzl committed
227
//最大RTP时间为13个小时,每13小时回环一次
228
extern const string kCycleMS;
xzl committed
229 230 231 232 233
} //namespace Rtsp

////////////组播配置///////////
namespace MultiCast {
//组播分配起始地址
234
extern const string kAddrMin;
xzl committed
235
//组播分配截止地址
236
extern const string kAddrMax;
xzl committed
237
//组播TTL
238
extern const string kUdpTTL;
xzl committed
239 240 241 242 243
} //namespace MultiCast

////////////录像配置///////////
namespace Record {
//查看录像的应用名称
244
extern const string kAppName;
xzl committed
245
//每次流化MP4文件的时长,单位毫秒
246
extern const string kSampleMS;
247
//MP4文件录制大小,默认一个小时
248
extern const string kFileSecond;
xzl committed
249
//录制文件路径
250
extern const string kFilePath;
xzl committed
251 252 253 254 255
} //namespace Record

////////////HLS相关配置///////////
namespace Hls {
//HLS切片时长,单位秒
256
extern const string kSegmentDuration;
xzl committed
257
//HLS切片个数
258
extern const string kSegmentNum;
xzl committed
259
//HLS文件写缓存大小
260
extern const string kFileBufSize;
xzl committed
261
//录制文件路径
262
extern const string kFilePath;
xzl committed
263 264
} //namespace Hls

xiongziliang committed
265

xiongziliang committed
266 267 268 269 270
/**
 * rtsp/rtmp播放器、推流器相关设置名,
 * 这些设置项都不是配置文件用
 * 只用于设置某个播放器或推流器实例用
 */
xiongziliang committed
271 272
namespace Client {
//指定网卡ip
273
extern const string kNetAdapter;
xiongziliang committed
274 275
//设置rtp传输类型,可选项有0(tcp,默认)、1(udp)、2(组播)
//设置方法:player[PlayerBase::kRtpType] = 0/1/2;
276
extern const string kRtpType;
xiongziliang committed
277
//rtsp认证用户名
278
extern const string kRtspUser;
xiongziliang committed
279
//rtsp认证用用户密码,可以是明文也可以是md5,md5密码生成方式 md5(username:realm:password)
280
extern const string kRtspPwd;
xiongziliang committed
281
//rtsp认证用用户密码是否为md5类型
282
extern const string kRtspPwdIsMD5;
xiongziliang committed
283
//握手超时时间,默认10,000 毫秒
284
extern const string kTimeoutMS;
xiongziliang committed
285
//rtp/rtmp包接收超时时间,默认5000秒
286
extern const string kMediaTimeoutMS;
xiongziliang committed
287
//rtsp/rtmp心跳时间,默认5000毫秒
288
extern const string kBeatIntervalMS;
xiongziliang committed
289
//Track编码格式探测最大时间,单位毫秒,默认2000
290
extern const string kMaxAnalysisMS;
xiongziliang committed
291
}
xiongziliang committed
292
}  // namespace mediakit
xzl committed
293

294
#endif /* COMMON_CONFIG_H */