ZLMediaKit/c_wrapper/src/common.cpp

160 lines
4.5 KiB
C++
Raw Normal View History

2017-12-13 18:15:22 +08:00
/*
* 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;
using namespace ZL::Util;
using namespace ZL::Rtmp;
using namespace ZL::Rtsp;
using namespace ZL::Http;
2018-02-24 10:45:19 +08:00
static TcpServer::Ptr s_pRtspSrv;
static TcpServer::Ptr s_pRtmpSrv;
static TcpServer::Ptr s_pHttpSrv;
2017-12-13 18:15:22 +08:00
//////////////////////////environment init///////////////////////////
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL onAppStart(){
2017-12-13 18:15:22 +08:00
static onceToken s_token([](){
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
2018-07-03 17:30:58 +08:00
EventPoller::Instance().runLoop(false);
2017-12-13 18:15:22 +08:00
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);
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL onAppExit(){
2017-12-13 18:15:22 +08:00
cleaner::Destory();
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL setGlobalOptionString(const char *key,const char *val){
2018-02-14 23:47:56 +08:00
if(mINI::Instance().find(key) == mINI::Instance().end()){
WarnL << "key:" << key << " not existed!";
}
2017-12-19 10:34:52 +08:00
mINI::Instance()[key] = val;
}
2018-02-28 10:58:36 +08:00
API_EXPORT unsigned short API_CALL initHttpServer(unsigned short port){
2018-02-24 10:45:19 +08:00
s_pHttpSrv.reset(new TcpServer());
2017-12-13 18:15:22 +08:00
try {
2018-02-24 10:45:19 +08:00
s_pHttpSrv->start<HttpSession>(port);
2018-02-28 10:58:36 +08:00
return s_pHttpSrv->getPort();
2017-12-13 18:15:22 +08:00
} catch (std::exception &ex) {
s_pHttpSrv.reset();
WarnL << ex.what();
2018-02-28 10:58:36 +08:00
return 0;
2017-12-13 18:15:22 +08:00
}
}
2018-02-28 10:58:36 +08:00
API_EXPORT unsigned short API_CALL initRtspServer(unsigned short port) {
2018-02-24 10:45:19 +08:00
s_pRtspSrv.reset(new TcpServer());
2017-12-13 18:15:22 +08:00
try {
2018-02-24 10:45:19 +08:00
s_pRtspSrv->start<RtspSession>(port);
2018-02-28 10:58:36 +08:00
return s_pRtspSrv->getPort();
2017-12-13 18:15:22 +08:00
} catch (std::exception &ex) {
s_pRtspSrv.reset();
WarnL << ex.what();
2018-02-28 10:58:36 +08:00
return 0;
2017-12-13 18:15:22 +08:00
}
}
2018-02-28 10:58:36 +08:00
API_EXPORT unsigned short API_CALL initRtmpServer(unsigned short port) {
2018-02-24 10:45:19 +08:00
s_pRtmpSrv.reset(new TcpServer());
2017-12-13 18:15:22 +08:00
try {
2018-02-24 10:45:19 +08:00
s_pRtmpSrv->start<RtmpSession>(port);
2018-02-28 10:58:36 +08:00
return s_pRtmpSrv->getPort();
2017-12-13 18:15:22 +08:00
} catch (std::exception &ex) {
s_pRtmpSrv.reset();
WarnL << ex.what();
2018-02-28 10:58:36 +08:00
return 0;
2017-12-13 18:15:22 +08:00
}
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL log_printf(LogType level,const char* file, const char* function, int line,const char *fmt,...){
2017-12-13 18:15:22 +08:00
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;
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL log_setLevel(LogType level){
2017-12-13 18:15:22 +08:00
Logger::Instance().setLevel((LogLevel)level);
}
class LogoutChannel: public LogChannel {
public:
LogoutChannel(const string &name, onLogOut cb, LogLevel level = LDebug)
2018-05-28 18:05:39 +08:00
:LogChannel(name, level){
2017-12-13 18:15:22 +08:00
_cb = cb;
}
virtual ~LogoutChannel(){}
2018-07-19 17:16:46 +08:00
void write(const LogInfoPtr &logInfo){
2018-05-28 18:05:39 +08:00
if (level() > logInfo->_level) {
2017-12-13 18:15:22 +08:00
return;
}
stringstream strStream;
2018-05-28 18:05:39 +08:00
logInfo->format(strStream, false);
2017-12-13 18:15:22 +08:00
auto strTmp = strStream.str();
if (_cb) {
_cb(strTmp.data(), strTmp.size());
}
}
private:
onLogOut _cb = nullptr;
};
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL log_setOnLogOut(onLogOut cb){
2017-12-13 18:15:22 +08:00
std::shared_ptr<LogoutChannel> chn(new LogoutChannel("LogoutChannel",cb,LTrace));
Logger::Instance().add(chn);
}