mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-26 04:31:37 +08:00
Avoid blocking the poller thread
This commit is contained in:
parent
7c42ac4d79
commit
a106f8dfc0
@ -10,11 +10,8 @@
|
|||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "Util/MD5.h"
|
|
||||||
#include "Util/File.h"
|
|
||||||
#include "Util/logger.h"
|
|
||||||
#include "Util/SSLBox.h"
|
|
||||||
#include "Util/util.h"
|
#include "Util/util.h"
|
||||||
|
#include "Util/logger.h"
|
||||||
#include "Network/TcpServer.h"
|
#include "Network/TcpServer.h"
|
||||||
#include "Common/config.h"
|
#include "Common/config.h"
|
||||||
#include "Rtsp/RtspSession.h"
|
#include "Rtsp/RtspSession.h"
|
||||||
@ -29,102 +26,102 @@ using namespace mediakit;
|
|||||||
static semaphore sem;
|
static semaphore sem;
|
||||||
|
|
||||||
#if defined(ENABLE_RTPPROXY)
|
#if defined(ENABLE_RTPPROXY)
|
||||||
static bool loadFile(const char *path, const EventPoller::Ptr &poller){
|
static bool loadFile(const char *path, const EventPoller::Ptr &poller) {
|
||||||
FILE *fp = fopen(path, "rb");
|
std::shared_ptr<FILE> fp(fopen(path, "rb"), [](FILE *fp) {
|
||||||
|
sem.post();
|
||||||
|
if (fp) {
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
});
|
||||||
if (!fp) {
|
if (!fp) {
|
||||||
WarnL << "open file failed:" << path;
|
WarnL << "open file failed:" << path;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t timeStamp_last = 0;
|
|
||||||
uint16_t len;
|
|
||||||
char rtp[0xFFFF];
|
|
||||||
struct sockaddr_storage addr;
|
struct sockaddr_storage addr;
|
||||||
memset(&addr, 0, sizeof(addr));
|
memset(&addr, 0, sizeof(addr));
|
||||||
addr.ss_family = AF_INET;
|
addr.ss_family = AF_INET;
|
||||||
auto sock = Socket::createSocket(poller);
|
auto sock = Socket::createSocket(poller);
|
||||||
size_t total_size = 0;
|
auto process = RtpSelector::Instance().getProcess("test", true);
|
||||||
RtpProcess::Ptr process;
|
|
||||||
uint32_t ssrc = 0;
|
uint64_t stamp_last = 0;
|
||||||
|
auto total_size = std::make_shared<size_t>(0);
|
||||||
|
auto do_read = [fp, total_size, sock, addr, process, stamp_last]() mutable -> int {
|
||||||
|
uint16_t len;
|
||||||
|
char rtp[0xFFFF];
|
||||||
while (true) {
|
while (true) {
|
||||||
if (2 != fread(&len, 1, 2, fp)) {
|
if (2 != fread(&len, 1, 2, fp.get())) {
|
||||||
WarnL;
|
WarnL << "Read rtp size failed";
|
||||||
break;
|
// 重新播放
|
||||||
|
fseek(fp.get(), 0, SEEK_SET);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
len = ntohs(len);
|
len = ntohs(len);
|
||||||
if (len < 12 || len > sizeof(rtp)) {
|
if (len < 12 || len > sizeof(rtp)) {
|
||||||
WarnL << len;
|
WarnL << "Invalid rtp size: " << len;
|
||||||
break;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len != fread(rtp, 1, len, fp)) {
|
if (len != fread(rtp, 1, len, fp.get())) {
|
||||||
WarnL;
|
WarnL << "Read rtp data failed";
|
||||||
break;
|
return 0;
|
||||||
}
|
}
|
||||||
total_size += len;
|
(*total_size) += len;
|
||||||
uint64_t timeStamp = 0;
|
uint64_t stamp = 0;
|
||||||
|
|
||||||
if (!process) {
|
|
||||||
if (!RtpSelector::getSSRC(rtp, len, ssrc)) {
|
|
||||||
WarnL << "get ssrc from rtp failed:" << len;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
process = RtpSelector::Instance().getProcess(printSSRC(ssrc), true);
|
|
||||||
}
|
|
||||||
if (process) {
|
|
||||||
try {
|
try {
|
||||||
process->inputRtp(true, sock, rtp, len, (struct sockaddr *)&addr, &timeStamp);
|
process->inputRtp(true, sock, rtp, len, (struct sockaddr *)&addr, &stamp);
|
||||||
} catch (...) {
|
} catch (std::exception &ex) {
|
||||||
RtpSelector::Instance().delProcess(printSSRC(ssrc), process.get());
|
WarnL << "Input rtp failed: " << ex.what();
|
||||||
throw;
|
return 0;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto diff = timeStamp - timeStamp_last;
|
auto diff = stamp - stamp_last;
|
||||||
if (diff > 0 && diff < 500) {
|
if (diff < 0 || diff > 500) {
|
||||||
usleep(diff * 1000);
|
diff = 1;
|
||||||
} else {
|
|
||||||
usleep(1 * 1000);
|
|
||||||
}
|
}
|
||||||
timeStamp_last = timeStamp;
|
if (diff) {
|
||||||
|
stamp_last = stamp;
|
||||||
|
return diff;
|
||||||
}
|
}
|
||||||
WarnL << total_size / 1024 << "KB";
|
}
|
||||||
fclose(fp);
|
};
|
||||||
|
poller->doDelayTask(1, [do_read, total_size, process]() mutable {
|
||||||
|
auto ret = do_read();
|
||||||
|
if (!ret) {
|
||||||
|
WarnL << *total_size / 1024 << "KB";
|
||||||
|
RtpSelector::Instance().delProcess("test", process.get());
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
#endif//#if defined(ENABLE_RTPPROXY)
|
#endif // #if defined(ENABLE_RTPPROXY)
|
||||||
|
|
||||||
int main(int argc,char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
//设置日志
|
// 设置日志
|
||||||
Logger::Instance().add(std::make_shared<ConsoleChannel>("ConsoleChannel"));
|
Logger::Instance().add(std::make_shared<ConsoleChannel>("ConsoleChannel"));
|
||||||
#if defined(ENABLE_RTPPROXY)
|
#if defined(ENABLE_RTPPROXY)
|
||||||
//启动异步日志线程
|
// 启动异步日志线程
|
||||||
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
||||||
loadIniConfig((exeDir() + "config.ini").data());
|
loadIniConfig((exeDir() + "config.ini").data());
|
||||||
TcpServer::Ptr rtspSrv(new TcpServer());
|
TcpServer::Ptr rtspSrv(new TcpServer());
|
||||||
TcpServer::Ptr rtmpSrv(new TcpServer());
|
TcpServer::Ptr rtmpSrv(new TcpServer());
|
||||||
TcpServer::Ptr httpSrv(new TcpServer());
|
TcpServer::Ptr httpSrv(new TcpServer());
|
||||||
rtspSrv->start<RtspSession>(554);//默认554
|
rtspSrv->start<RtspSession>(554); // 默认554
|
||||||
rtmpSrv->start<RtmpSession>(1935);//默认1935
|
rtmpSrv->start<RtmpSession>(1935); // 默认1935
|
||||||
httpSrv->start<HttpSession>(80);//默认80
|
httpSrv->start<HttpSession>(80); // 默认80
|
||||||
//此处选择是否导出调试文件
|
// 此处选择是否导出调试文件
|
||||||
// mINI::Instance()[RtpProxy::kDumpDir] = "/Users/xzl/Desktop/";
|
// mINI::Instance()[RtpProxy::kDumpDir] = "/Users/xzl/Desktop/";
|
||||||
|
|
||||||
if (argc == 2){
|
if (argc == 2) {
|
||||||
auto poller = EventPollerPool::Instance().getPoller();
|
loadFile(argv[1], EventPollerPool::Instance().getPoller());
|
||||||
poller->async_first([poller,argv](){
|
|
||||||
loadFile(argv[1],poller);
|
|
||||||
sem.post();
|
|
||||||
});
|
|
||||||
sem.wait();
|
sem.wait();
|
||||||
sleep(1);
|
} else {
|
||||||
}
|
|
||||||
else
|
|
||||||
ErrorL << "parameter error.";
|
ErrorL << "parameter error.";
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
ErrorL << "please ENABLE_RTPPROXY and then test";
|
ErrorL << "please ENABLE_RTPPROXY and then test";
|
||||||
#endif//#if defined(ENABLE_RTPPROXY)
|
#endif // #if defined(ENABLE_RTPPROXY)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user