ZLMediaKit/tests/test_flv.cpp

113 lines
3.0 KiB
C++
Raw Permalink Normal View History

Push force replace (#2899) Co-authored-by: Alex <liyu7352@gmail.com> Co-authored-by: xia-chu <771730766@qq.com> Co-authored-by: Johnny <hellojinqiang@gmail.com> Co-authored-by: BackT0TheFuture <10088733+BackT0TheFuture@users.noreply.github.com> Co-authored-by: ljx0305 <ljx0305@gmail.com> Co-authored-by: Per-Arne Andersen <per@sysx.no> Co-authored-by: codeRATny <60806889+codeRATny@users.noreply.github.com> Co-authored-by: 老衲不出家 <tannzh2018@outlook.com> Co-authored-by: Kiki <haijuanchen.sun@gmail.com> Co-authored-by: PioLing <964472638@qq.com> Co-authored-by: dengjfzh <76604422+dengjfzh@users.noreply.github.com> Co-authored-by: a-ucontrol <55526028+a-ucontrol@users.noreply.github.com> Co-authored-by: 百鸣 <94030128+ixingqiao@users.noreply.github.com> Co-authored-by: fruit Juice <2317232721@qq.com> Co-authored-by: Luosh <fjlshyyyy@qq.com> Co-authored-by: tbago <moonzalor@gmail.com> Co-authored-by: Talus <xiaoxiaochenjian@gmail.com> Co-authored-by: 朱如洪 <zhu410289616@163.com> Co-authored-by: pedoc <pedoc@qq.com> Co-authored-by: XiaoYan Lin <linxiaoyan87@foxmail.com> Co-authored-by: xiangshengjye <46069012+xiangshengjye@users.noreply.github.com> Co-authored-by: tjpgt <602950305@qq.com> Co-authored-by: Nick <joyouswind@gmail.com> Co-authored-by: yogo-zhangyingzhe <100331270+yogo-zhangyingzhe@users.noreply.github.com> Co-authored-by: Xiaofeng Wang <wasphin@gmail.com> Co-authored-by: Dw9 <xweimvp@gmail.com> Co-authored-by: waken <33921191+mc373906408@users.noreply.github.com> Co-authored-by: Deepslient <1154547394@qq.com>
2023-10-12 17:09:20 +08:00
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* 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.
*/
#include <map>
#include <iostream>
#include "Util/logger.h"
#include "Util/util.h"
#include "Network/TcpServer.h"
#include "Common/config.h"
#include "Rtsp/RtspSession.h"
#include "Rtmp/RtmpSession.h"
#include "Http/HttpSession.h"
#include "Rtmp/FlvSplitter.h"
#include "Rtmp/RtmpMediaSourceImp.h"
using namespace std;
using namespace toolkit;
using namespace mediakit;
class FlvSplitterImp : public FlvSplitter {
public:
FlvSplitterImp() {
_src = std::make_shared<RtmpMediaSourceImp>(MediaTuple{DEFAULT_VHOST, "live", "test"});
}
~FlvSplitterImp() override = default;
void inputData(const char *data, size_t len, uint32_t &stamp) {
FlvSplitter::input(data, len);
stamp = _stamp;
}
protected:
void onRecvFlvHeader(const FLVHeader &header) override {
}
bool onRecvMetadata(const AMFValue &metadata) override {
_src->setMetaData(metadata);
_src->setProtocolOption(ProtocolOption());
return true;
}
void onRecvRtmpPacket(RtmpPacket::Ptr packet) override {
_stamp = packet->time_stamp;
_src->onWrite(std::move(packet));
}
private:
uint32_t _stamp;
RtmpMediaSourceImp::Ptr _src;
};
static bool loadFile(const char *path){
FILE *fp = fopen(path, "rb");
if (!fp) {
WarnL << "open file failed:" << path;
return false;
}
char buffer[64 * 1024];
uint32_t timeStamp_last = 0;
size_t len;
size_t total_size = 0;
FlvSplitterImp flv;
while (true) {
len = fread(buffer, 1, sizeof(buffer), fp);
if (len <= 0) {
break;
}
total_size += len;
uint32_t timeStamp;
flv.inputData(buffer, len, timeStamp);
auto diff = timeStamp - timeStamp_last;
if (diff > 0) {
usleep(diff * 1000);
} else {
usleep(1 * 1000);
}
timeStamp_last = timeStamp;
}
WarnL << total_size / 1024 << "KB";
fclose(fp);
return true;
}
int main(int argc,char *argv[]) {
//设置日志
Logger::Instance().add(std::make_shared<ConsoleChannel>("ConsoleChannel"));
//启动异步日志线程
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
loadIniConfig((exeDir() + "config.ini").data());
TcpServer::Ptr rtspSrv(new TcpServer());
TcpServer::Ptr rtmpSrv(new TcpServer());
TcpServer::Ptr httpSrv(new TcpServer());
rtspSrv->start<RtspSession>(554);//默认554
rtmpSrv->start<RtmpSession>(1935);//默认1935
httpSrv->start<HttpSession>(80);//默认80
if (argc == 2)
loadFile(argv[1]);
else
ErrorL << "parameter error.";
return 0;
}