Compare commits

..

6 Commits

Author SHA1 Message Date
xia-chu
5d9ced2cf3 startSendRtp相关接口新增enable_origin_recv_limit参数控制是否开启源端限流
Some checks failed
Android / build (push) Has been cancelled
CodeQL / Analyze (cpp) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Docker / build (push) Has been cancelled
Linux / build (push) Has been cancelled
macOS / build (push) Has been cancelled
Windows / build (push) Has been cancelled
多倍速rtp转发时,在tcp模式下,如果转发速度跟不上接收速度将导致转发失败,通过开启该参数,可以确保历史视频多倍速下载成功
2025-08-02 21:14:42 +08:00
xia-chu
c9a0025620 新增startRecordTask接口,支持录制事件视频
用户可以录制当前时间前后一定时间内的视频,配置文件gop_cache设置较大时历史视频才能录全
2025-08-02 21:00:30 +08:00
xia-chu
b1e1a0f174 新增deleteSnapDirectory删除截图接口 2025-08-02 21:00:30 +08:00
xia-chu
88ef00e429 音视频时间戳相差太大时强制同步 2025-08-02 21:00:30 +08:00
xia-chu
32ab752263 Revert "优化SEI/AUD帧处理逻辑" (#4361)
This reverts commit a35444f27e.
2025-08-02 21:00:30 +08:00
Say_Bryan
5a10db5f1b
修改RtpServer.cpp中的注释错误 (#4372) 2025-08-02 20:32:30 +08:00
9 changed files with 260 additions and 24 deletions

View File

@ -157,7 +157,6 @@ bool H264Track::inputFrame(const Frame::Ptr &frame) {
// AUD帧丢弃
return false;
}
if ((type == H264Frame::NAL_B_P || type == H264Frame::NAL_IDR) && ready()) {
return inputFrame_l(frame);
}

View File

@ -310,6 +310,53 @@
},
"response": []
},
{
"name": "删除截图(deleteSnapDirectory)",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{ZLMediaKit_URL}}/index/api/deleteSnapDirectory?secret={{ZLMediaKit_secret}}&vhost={{defaultVhost}}&app=live&stream=test&file=71_1740828613.jpg",
"host": [
"{{ZLMediaKit_URL}}"
],
"path": [
"index",
"api",
"deleteSnapDirectory"
],
"query": [
{
"key": "secret",
"value": "{{ZLMediaKit_secret}}",
"description": "api操作密钥(配置文件配置)"
},
{
"key": "vhost",
"value": "{{defaultVhost}}",
"description": "筛选虚拟主机例如__defaultVhost__"
},
{
"key": "app",
"value": "live",
"description": "筛选应用名,例如 live"
},
{
"key": "stream",
"value": "test",
"description": "筛选流id例如 test"
},
{
"key": "file",
"value": "",
"disabled": true,
"description": "文件名,非必选"
}
]
}
},
"response": []
},
{
"name": "关断单个流(close_stream)",
"request": {
@ -522,7 +569,7 @@
"response": []
},
{
"name": "添加rtsp/rtmp/hls/srt拉流代理(addStreamProxy)",
"name": "添加拉流代理(addStreamProxy)",
"request": {
"method": "GET",
"header": [],
@ -560,7 +607,7 @@
{
"key": "url",
"value": "rtmp://live.hkstv.hk.lxdns.com/live/hks2",
"description": "拉流地址,例如rtmp://live.hkstv.hk.lxdns.com/live/hks2"
"description": "拉流地址,支持rtsp/rtmp/hls/srt/http-flv/http-ts协议"
},
{
"key": "rtp_type",
@ -1333,6 +1380,62 @@
},
"response": []
},
{
"name": "开始事件视频录制(startRecordTask)",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{ZLMediaKit_URL}}/index/api/startRecordTask?secret={{ZLMediaKit_secret}}&vhost={{defaultVhost}}&app=live&stream=test&path=1.mp4&back_ms=10000&forward_ms=10000",
"host": [
"{{ZLMediaKit_URL}}"
],
"path": [
"index",
"api",
"startRecordTask"
],
"query": [
{
"key": "secret",
"value": "{{ZLMediaKit_secret}}",
"description": "api操作密钥(配置文件配置)"
},
{
"key": "vhost",
"value": "{{defaultVhost}}",
"description": "虚拟主机例如__defaultVhost__"
},
{
"key": "app",
"value": "live",
"description": "应用名,例如 live"
},
{
"key": "stream",
"value": "test",
"description": "流id例如 obs"
},
{
"key": "path",
"value": "1.mp4",
"description": "录像文件保存相对路径,包括名称"
},
{
"key": "back_ms",
"value": "10000",
"description": "回溯录制时长"
},
{
"key": "forward_ms",
"value": "10000",
"description": "后续录制时长"
}
]
}
},
"response": []
},
{
"name": "设置录像速度(setRecordSpeed)",
"request": {
@ -2092,6 +2195,12 @@
"value": "",
"description": "发送rtp同时接收一般用于双向语言对讲, 如果不为空说明开启接收值为接收流的id",
"disabled": true
},
{
"key": "enable_origin_recv_limit",
"value": "1",
"description": "转发rtp(tcp模式)时如果发送不出去是否限制源端收流速度此参数在多倍速rtp转发时作用较大",
"disabled": true
}
]
}
@ -2186,6 +2295,12 @@
"value": "5000",
"description": "等待tcp连接超时时间单位毫秒默认5000毫秒",
"disabled": true
},
{
"key": "enable_origin_recv_limit",
"value": "1",
"description": "转发rtp(tcp模式)时如果发送不出去是否限制源端收流速度此参数在多倍速rtp转发时作用较大",
"disabled": true
}
]
}
@ -2261,6 +2376,12 @@
"value": "1",
"description": "rtp es方式打包时是否只打包音频该参数非必选参数",
"disabled": true
},
{
"key": "enable_origin_recv_limit",
"value": "1",
"description": "转发rtp(tcp模式)时如果发送不出去是否限制源端收流速度此参数在多倍速rtp转发时作用较大",
"disabled": true
}
]
}

View File

@ -1603,6 +1603,7 @@ void installWebApi() {
// Record the app and vhost of the sending stream
args.recv_stream_app = allArgs["app"];
args.recv_stream_vhost = allArgs["vhost"];
args.enable_origin_recv_limit = allArgs["enable_origin_recv_limit"];
src->getOwnerPoller()->async([=]() mutable {
try {
src->startSendRtp(args, [val, headerOut, invoker](uint16_t local_port, const SockException &ex) mutable {
@ -1649,6 +1650,7 @@ void installWebApi() {
args.recv_stream_id = allArgs["recv_stream_id"];
args.recv_stream_app = allArgs["app"];
args.recv_stream_vhost = allArgs["vhost"];
args.enable_origin_recv_limit = allArgs["enable_origin_recv_limit"];
src->getOwnerPoller()->async([=]() mutable {
try {
@ -1776,6 +1778,30 @@ void installWebApi() {
});
});
api_regist("/index/api/startRecordTask",[](API_ARGS_MAP_ASYNC){
CHECK_SECRET();
CHECK_ARGS("vhost", "app", "stream", "path", "back_ms", "forward_ms");
auto src = MediaSource::find(allArgs["vhost"], allArgs["app"], allArgs["stream"]);
if (!src) {
throw ApiRetException("can not find the stream", API::NotFound);
}
src->getOwnerPoller()->async([=]() mutable {
std::string err;
std::string path;
try {
path = src->getMuxer()->startRecord(allArgs["path"], allArgs["back_ms"], allArgs["forward_ms"]);
} catch (std::exception &ex) {
err = ex.what();
}
val["code"] = err.empty() ? API::Success : API::OtherFailed;
val["data"]["path"] = path;
val["msg"] = err;
invoker(200, headerOut, val.toStyledString());
});
});
// 设置录像流播放速度 [AUTO-TRANSLATED:a8d82298]
// Set the playback speed of the recording stream
api_regist("/index/api/setRecordSpeed", [](API_ARGS_MAP_ASYNC) {
@ -1888,11 +1914,13 @@ void installWebApi() {
// http://127.0.0.1/index/api/deleteRecordDirectroy?vhost=__defaultVhost__&app=live&stream=ss&period=2020-01-01
api_regist("/index/api/deleteRecordDirectory", [](API_ARGS_MAP) {
CHECK_SECRET();
CHECK_ARGS("vhost", "app", "stream", "period");
CHECK_ARGS("vhost", "app", "stream");
auto tuple = MediaTuple{allArgs["vhost"], allArgs["app"], allArgs["stream"], ""};
auto record_path = Recorder::getRecordPath(Recorder::type_mp4, tuple, allArgs["customized_path"]);
auto period = allArgs["period"];
record_path = record_path + period + "/";
if (!period.empty()) {
record_path = record_path + period + "/";
}
bool recording = false;
auto name = allArgs["name"];
@ -1927,6 +1955,15 @@ void installWebApi() {
File::deleteEmptyDir(record_path);
});
api_regist("/index/api/deleteSnapDirectory", [](API_ARGS_MAP) {
CHECK_SECRET();
CHECK_ARGS("vhost", "app", "stream");
GET_CONFIG(std::string, root, API::kSnapRoot);
auto path = File::absolutePath(allArgs["vhost"] + "/" + allArgs["app"] + "/" + allArgs["stream"] + "/" + allArgs["file"], root);
InfoL << "delete " << path;
File::delete_file(path, true);
});
// 获取录像文件夹列表或mp4文件列表 [AUTO-TRANSLATED:f7e299bc]
// Get the list of recording folders or mp4 files
//http://127.0.0.1/index/api/getMP4RecordFile?vhost=__defaultVhost__&app=live&stream=ss&period=2020-01

View File

@ -11,6 +11,7 @@
#include <math.h>
#include "Common/config.h"
#include "MultiMediaSourceMuxer.h"
#include "Thread/WorkThreadPool.h"
using namespace std;
using namespace toolkit;
@ -400,6 +401,88 @@ bool MultiMediaSourceMuxer::setupRecord(MediaSource &sender, Recorder::type type
}
}
std::string MultiMediaSourceMuxer::startRecord(const std::string &file_path, uint32_t back_time_ms, uint32_t forward_time_ms) {
#if !defined(ENABLE_MP4)
throw std::invalid_argument("mp4相关功能未打开请开启ENABLE_MP4宏后编译再测试");
#else
if (!_ring) {
throw std::runtime_error("frame gop cache disabled, start record event video failed");
}
auto path = Recorder::getRecordPath(Recorder::type_mp4, _tuple, _option.mp4_save_path);
path += file_path;
TraceL << "mp4 save path: " << path;
auto muxer = std::make_shared<MP4Muxer>();
muxer->openMP4(path);
for (auto &track : MediaSink::getTracks()) {
muxer->addTrack(track);
}
muxer->addTrackCompleted();
std::list<Frame::Ptr> history;
_ring->flushGop([&](const Frame::Ptr &frame) { history.emplace_back(frame); });
if (!history.empty()) {
auto now_dts = history.back()->dts();
decltype(history)::iterator pos = history.end();
for (auto it = history.rbegin(); it != history.rend(); ++it) {
auto &frame = *it;
if (frame->getTrackType() != TrackVideo || (!frame->configFrame() && !frame->keyFrame())) {
continue;
}
// 如果视频关键帧到末尾的时长超过一定的时间,那前面的数据应该全部删除
if (frame->dts() + back_time_ms < now_dts) {
pos = it.base();
--pos;
break;
}
}
if (pos != history.end()) {
// 移除前面过多的数据
TraceL << "clear history video: " << history.front()->dts() << " -> " << (*pos)->dts();
history.erase(history.begin(), pos);
}
if (!history.empty()) {
auto &front = history.front();
InfoL << "start record: " << path
<< ", start_dts: " << front->dts() << ", key_frame: " << front->keyFrame() << ", config_frame: " << front->configFrame()
<< ", now_dts: " << now_dts;
}
for (auto &frame : history) {
muxer->inputFrame(frame);
}
}
auto reader = _ring->attach(MultiMediaSourceMuxer::getOwnerPoller(MediaSource::NullMediaSource()), false);
uint64_t now_dts = 0;
int selected_index = -1;
reader->setReadCB([muxer, now_dts, selected_index, forward_time_ms, reader, path](const Frame::Ptr &frame) mutable {
// 循环引用自身
if (!now_dts) {
now_dts = frame->dts();
selected_index = frame->getIndex();
}
if (frame->getIndex() == selected_index && now_dts + forward_time_ms < frame->dts()) {
InfoL << "stop record: " << path << ", end dts: " << frame->dts();
WorkThreadPool::Instance().getPoller()->async([muxer]() { muxer->closeMP4(); });
reader = nullptr;
return;
}
muxer->inputFrame(frame);
});
std::weak_ptr<RingType::RingReader> weak_reader = reader;
reader->setDetachCB([weak_reader]() {
if (auto strong_reader = weak_reader.lock()) {
// 防止循环引用
strong_reader->setReadCB(nullptr);
}
});
return path;
#endif
}
// 此函数可能跨线程调用 [AUTO-TRANSLATED:e8c5f74d]
// This function may be called across threads
bool MultiMediaSourceMuxer::isRecording(MediaSource &sender, Recorder::type type) {

View File

@ -122,6 +122,15 @@ public:
*/
bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const std::string &custom_path, size_t max_second) override;
/**
* mp4
* @param file_path mp4相对路径
* @param back_time_ms
* @param forward_time_ms
* @return
*/
std::string startRecord(const std::string &file_path, uint32_t back_time_ms, uint32_t forward_time_ms);
/**
*
* @param type

View File

@ -148,10 +148,11 @@ void Stamp::revise_l(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_ou
if (ABS(dts_diff) < 5000 || _need_sync > 3) {
// 两种时间戳相差不得大于300ms
dts_diff = _relative_stamp - _sync_master->_relative_stamp;
// 强制同步音视频
if (dts_diff > 300) {
dts_diff = 300;
dts_diff = 0;
} else if (dts_diff < -300) {
dts_diff = -300;
dts_diff = 0;
}
// 如果绝对时间戳小于5秒那么说明他们的起始时间戳是一致的那么强制同步 [AUTO-TRANSLATED:5d11ef6a]
// If the absolute timestamp is less than 5 seconds, then it means that their starting timestamps are consistent, then force synchronization

View File

@ -205,11 +205,7 @@ bool FrameMerger::willFlush(const Frame::Ptr &frame) const{
case mp4_nal_size:
case h264_prefix: {
if (frame->dropAble() && !_have_config_frame) {
// 遇到SEI帧且未缓存配置帧flush之前的帧
return true;
}
if (!_have_decode_able_frame && !_have_drop_able_frame) {
if (!_have_decode_able_frame) {
// 缓存中没有有效的能解码的帧所以这次不flush [AUTO-TRANSLATED:5d860722]
// There are no valid frames that can be decoded in the cache, so no flush this time.
return _frame_cache.size() > kMaxFrameCacheSize;
@ -294,8 +290,6 @@ bool FrameMerger::inputFrame(const Frame::Ptr &frame, onOutput cb, BufferLikeStr
cb(back->dts(), back->pts(), merged_frame, have_key_frame);
_frame_cache.clear();
_have_decode_able_frame = false;
_have_drop_able_frame = false;
_have_config_frame = false;
}
if (!frame) {
@ -305,12 +299,6 @@ bool FrameMerger::inputFrame(const Frame::Ptr &frame, onOutput cb, BufferLikeStr
if (frame->decodeAble()) {
_have_decode_able_frame = true;
}
if (frame->dropAble()) {
_have_drop_able_frame = true;
}
if (frame->configFrame()) {
_have_config_frame = true;
}
_cb = std::move(cb);
_frame_cache.emplace_back(Frame::getCacheAbleFrame(frame));
return true;

View File

@ -660,8 +660,6 @@ private:
private:
int _type;
bool _have_decode_able_frame = false;
bool _have_drop_able_frame = false;
bool _have_config_frame = false;
onOutput _cb;
toolkit::List<Frame::Ptr> _frame_cache;
};

View File

@ -207,8 +207,8 @@ void RtpServer::start(uint16_t local_port, const char *local_ip, const MediaTupl
TcpServer::Ptr tcp_server;
if (tcp_mode == PASSIVE || tcp_mode == ACTIVE) {
auto processor = helper ? helper->getProcess() : nullptr;
// 如果共享同一个processor对象那么tcp server深圳为单线程模式确保线程安全 [AUTO-TRANSLATED:68bdd877]
// If the same processor object is shared, then the TCP server Shenzhen is in single-threaded mode to ensure thread safety
// 如果共享同一个processor对象那么tcp server声明为单线程模式确保线程安全 [AUTO-TRANSLATED:68bdd877]
// If the same processor object is shared, declare the TCP server in single-threaded mode to ensure thread safety.
tcp_server = std::make_shared<TcpServer>(processor ? poller : nullptr);
(*tcp_server)[RtpSession::kVhost] = tuple.vhost;
(*tcp_server)[RtpSession::kApp] = tuple.app;