Add exception handling for FFmpeg cmd checked. (#4343)

This commit is contained in:
PioLing 2025-07-10 14:14:57 +08:00 committed by GitHub
parent fb2a3f5179
commit 47fd8e9fc3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -84,10 +84,6 @@ void FFmpegSource::play(const string &ffmpeg_cmd_key, const string &src_url, con
try { try {
_media_info.parse(dst_url); _media_info.parse(dst_url);
} catch (std::exception &ex) {
cb(SockException(Err_other, ex.what()));
return;
}
auto ffmpeg_cmd = ffmpeg_cmd_default; auto ffmpeg_cmd = ffmpeg_cmd_default;
if (!ffmpeg_cmd_key.empty()) { if (!ffmpeg_cmd_key.empty()) {
@ -98,6 +94,9 @@ void FFmpegSource::play(const string &ffmpeg_cmd_key, const string &src_url, con
WarnL << "配置文件中,ffmpeg命令模板(" << ffmpeg_cmd_key << ")不存在,已采用默认模板(" << ffmpeg_cmd_default << ")"; WarnL << "配置文件中,ffmpeg命令模板(" << ffmpeg_cmd_key << ")不存在,已采用默认模板(" << ffmpeg_cmd_default << ")";
} }
} }
if (!toolkit::start_with(ffmpeg_cmd, "%s")) {
throw std::invalid_argument("ffmpeg cmd template must start with '%s'");
}
char cmd[2048] = { 0 }; char cmd[2048] = { 0 };
snprintf(cmd, sizeof(cmd), ffmpeg_cmd.data(), File::absolutePath("", ffmpeg_bin).data(), src_url.data(), dst_url.data()); snprintf(cmd, sizeof(cmd), ffmpeg_cmd.data(), File::absolutePath("", ffmpeg_bin).data(), src_url.data(), dst_url.data());
@ -109,8 +108,8 @@ void FFmpegSource::play(const string &ffmpeg_cmd_key, const string &src_url, con
if (is_local_ip(_media_info.host)) { if (is_local_ip(_media_info.host)) {
// 推流给自己的,通过判断流是否注册上来判断是否正常 [AUTO-TRANSLATED:423f2be6] // 推流给自己的,通过判断流是否注册上来判断是否正常 [AUTO-TRANSLATED:423f2be6]
// Push stream to yourself, judge whether the stream is registered to determine whether it is normal // Push stream to yourself, judge whether the stream is registered to determine whether it is normal
if (_media_info.schema != RTSP_SCHEMA && _media_info.schema != RTMP_SCHEMA) { if (_media_info.schema != RTSP_SCHEMA && _media_info.schema != RTMP_SCHEMA && _media_info.schema != "srt") {
cb(SockException(Err_other, "本服务只支持rtmp/rtsp推流")); cb(SockException(Err_other, "本服务只支持rtmp/rtsp/srt推流"));
return; return;
} }
weak_ptr<FFmpegSource> weakSelf = shared_from_this(); weak_ptr<FFmpegSource> weakSelf = shared_from_this();
@ -141,11 +140,13 @@ void FFmpegSource::play(const string &ffmpeg_cmd_key, const string &src_url, con
// ffmpeg process is still online, but waiting for the stream to timeout // ffmpeg process is still online, but waiting for the stream to timeout
cb(SockException(Err_other, "等待超时")); cb(SockException(Err_other, "等待超时"));
}); });
} else{ } else {
// 推流给其他服务器的通过判断FFmpeg进程是否在线判断是否成功 [AUTO-TRANSLATED:9b963da5] // 推流给其他服务器的通过判断FFmpeg进程是否在线判断是否成功 [AUTO-TRANSLATED:9b963da5]
// Push stream to other servers, judge whether it is successful by judging whether the FFmpeg process is online // Push stream to other servers, judge whether it is successful by judging whether the FFmpeg process is online
weak_ptr<FFmpegSource> weakSelf = shared_from_this(); weak_ptr<FFmpegSource> weakSelf = shared_from_this();
_timer = std::make_shared<Timer>(timeout_ms / 1000.0f, [weakSelf, cb, timeout_ms]() { _timer = std::make_shared<Timer>(
timeout_ms / 1000.0f,
[weakSelf, cb, timeout_ms]() {
auto strongSelf = weakSelf.lock(); auto strongSelf = weakSelf.lock();
if (!strongSelf) { if (!strongSelf) {
// 自身已经销毁 [AUTO-TRANSLATED:5f954f8a] // 自身已经销毁 [AUTO-TRANSLATED:5f954f8a]
@ -163,7 +164,12 @@ void FFmpegSource::play(const string &ffmpeg_cmd_key, const string &src_url, con
// ffmpeg process has exited // ffmpeg process has exited
cb(SockException(Err_other, StrPrinter << "ffmpeg已经退出,exit code = " << strongSelf->_process.exit_code())); cb(SockException(Err_other, StrPrinter << "ffmpeg已经退出,exit code = " << strongSelf->_process.exit_code()));
return false; return false;
}, _poller); },
_poller);
}
} catch (std::exception &ex) {
WarnL << ex.what();
cb(SockException(Err_other, ex.what()));
} }
} }