common.cpp 4.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*
 * MIT License
 *
 * Copyright (c) 2016 xiongziliang <771730766@qq.com>
 *
 * 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.
 */

#include "common.h"
#include <stdarg.h>
#include <unordered_map>
#include "Util/logger.h"
#include "Util/onceToken.h"
#include "Util/TimeTicker.h"
#include "Network/TcpServer.h"
#include "Poller/EventPoller.h"
#include "Rtsp/UDPServer.h"
#include "Rtsp/RtspSession.h"
#include "Rtmp/RtmpSession.h"
#include "Http/HttpSession.h"
#include "cleaner.h"

using namespace std;
xiongziliang committed
42
using namespace toolkit;
43

44 45 46
static TcpServer::Ptr s_pRtspSrv;
static TcpServer::Ptr s_pRtmpSrv;
static TcpServer::Ptr s_pHttpSrv;
47 48 49

//////////////////////////environment init///////////////////////////

50
API_EXPORT void API_CALL onAppStart(){
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	static onceToken s_token([](){
		Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
		cleaner::Instance().push_back([](){
			s_pRtspSrv.reset();
			s_pRtmpSrv.reset();
			s_pHttpSrv.reset();
			WorkThreadPool::Destory();
			UDPServer::Destory();
			AsyncTaskThread::Destory();
			EventPoller::Destory();
			DebugL << "clear common" << endl;
			Logger::Destory();
		});
	},nullptr);
}


68
API_EXPORT void API_CALL onAppExit(){
69 70
	cleaner::Destory();
}
71
API_EXPORT void API_CALL setGlobalOptionString(const char *key,const char *val){
xiongziliang committed
72 73 74
    if(mINI::Instance().find(key) == mINI::Instance().end()){
        WarnL << "key:" << key << " not existed!";
    }
75 76
    mINI::Instance()[key] = val;
}
xiongziliang committed
77
API_EXPORT unsigned short API_CALL initHttpServer(unsigned short port){
78
	s_pHttpSrv.reset(new TcpServer());
79
	try {
80
		s_pHttpSrv->start<HttpSession>(port);
xiongziliang committed
81
		return s_pHttpSrv->getPort();
82 83 84
	} catch (std::exception &ex) {
		s_pHttpSrv.reset();
		WarnL << ex.what();
xiongziliang committed
85
		return 0;
86 87
	}
}
xiongziliang committed
88
API_EXPORT unsigned short API_CALL initRtspServer(unsigned short port) {
89
	s_pRtspSrv.reset(new TcpServer());
90
	try {
91
		s_pRtspSrv->start<RtspSession>(port);
xiongziliang committed
92
		return s_pRtspSrv->getPort();
93 94 95
	} catch (std::exception &ex) {
		s_pRtspSrv.reset();
		WarnL << ex.what();
xiongziliang committed
96
		return 0;
97 98 99
	}
}

xiongziliang committed
100
API_EXPORT unsigned short API_CALL initRtmpServer(unsigned short port) {
101
	s_pRtmpSrv.reset(new TcpServer());
102
	try {
103
		s_pRtmpSrv->start<RtmpSession>(port);
xiongziliang committed
104
		return s_pRtmpSrv->getPort();
105 106 107
	} catch (std::exception &ex) {
		s_pRtmpSrv.reset();
		WarnL << ex.what();
xiongziliang committed
108
		return 0;
109 110 111 112
	}
}


113
API_EXPORT void API_CALL log_printf(LogType level,const char* file, const char* function, int line,const char *fmt,...){
114 115 116 117 118 119 120 121 122
	LogInfoMaker info((LogLevel)level,file,function,line);
	va_list pArg;
	va_start(pArg, fmt);
	char buf[4096];
	int n = vsprintf(buf, fmt, pArg);
	buf[n] = '\0';
	va_end(pArg);
	info << buf;
}
123
API_EXPORT void API_CALL log_setLevel(LogType level){
124 125 126 127 128 129
	Logger::Instance().setLevel((LogLevel)level);
}

class LogoutChannel: public LogChannel {
public:
	LogoutChannel(const string &name, onLogOut cb, LogLevel level = LDebug)
xiongziliang committed
130
:LogChannel(name, level){
131 132 133
		_cb = cb;
	}
	virtual ~LogoutChannel(){}
xiongziliang committed
134
	void write(const LogInfoPtr &logInfo){
xiongziliang committed
135
		if (level() > logInfo->_level) {
136 137 138
			return;
		}
		stringstream strStream;
xiongziliang committed
139
		logInfo->format(strStream, false);
140 141 142 143 144 145 146 147 148
		auto strTmp = strStream.str();
		if (_cb) {
			_cb(strTmp.data(), strTmp.size());
		}
	}
private:
	onLogOut _cb = nullptr;
};

149
API_EXPORT void API_CALL log_setOnLogOut(onLogOut cb){
150 151 152 153 154
	std::shared_ptr<LogoutChannel> chn(new LogoutChannel("LogoutChannel",cb,LTrace));
	Logger::Instance().add(chn);
}