Compare commits

..

No commits in common. "1ead079af46e5f84b0c1515438df3f65121d94ff" and "fb2a3f51793248007bbeb54f4b65a5416273a59e" have entirely different histories.

2 changed files with 78 additions and 84 deletions

View File

@ -84,92 +84,86 @@ 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()) {
auto cmd_it = mINI::Instance().find(ffmpeg_cmd_key); auto cmd_it = mINI::Instance().find(ffmpeg_cmd_key);
if (cmd_it != mINI::Instance().end()) { if (cmd_it != mINI::Instance().end()) {
ffmpeg_cmd = cmd_it->second; ffmpeg_cmd = cmd_it->second;
} else { } else {
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 };
snprintf(cmd, sizeof(cmd), ffmpeg_cmd.data(), File::absolutePath("", ffmpeg_bin).data(), src_url.data(), dst_url.data());
auto log_file = ffmpeg_log.empty() ? "" : File::absolutePath("", ffmpeg_log);
_process.run(cmd, log_file);
_cmd = cmd;
InfoL << cmd;
if (is_local_ip(_media_info.host)) {
// 推流给自己的,通过判断流是否注册上来判断是否正常 [AUTO-TRANSLATED:423f2be6]
// 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) {
cb(SockException(Err_other, "本服务只支持rtmp/rtsp推流"));
return;
} }
weak_ptr<FFmpegSource> weakSelf = shared_from_this();
char cmd[2048] = { 0 }; findAsync(timeout_ms, [cb, weakSelf, timeout_ms](const MediaSource::Ptr &src) {
snprintf(cmd, sizeof(cmd), ffmpeg_cmd.data(), File::absolutePath("", ffmpeg_bin).data(), src_url.data(), dst_url.data()); auto strongSelf = weakSelf.lock();
auto log_file = ffmpeg_log.empty() ? "" : File::absolutePath("", ffmpeg_log); if (!strongSelf) {
_process.run(cmd, log_file); // 自己已经销毁 [AUTO-TRANSLATED:3d45c3b0]
_cmd = cmd; // Self has been destroyed
InfoL << cmd;
if (is_local_ip(_media_info.host)) {
// 推流给自己的,通过判断流是否注册上来判断是否正常 [AUTO-TRANSLATED:423f2be6]
// 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 && _media_info.schema != "srt") {
cb(SockException(Err_other, "本服务只支持rtmp/rtsp/srt推流"));
return; return;
} }
weak_ptr<FFmpegSource> weakSelf = shared_from_this(); if (src) {
findAsync(timeout_ms, [cb, weakSelf, timeout_ms](const MediaSource::Ptr &src) { // 推流给自己成功 [AUTO-TRANSLATED:65dba71b]
auto strongSelf = weakSelf.lock(); // Push stream to yourself successfully
if (!strongSelf) { cb(SockException());
// 自己已经销毁 [AUTO-TRANSLATED:3d45c3b0] strongSelf->onGetMediaSource(src);
// Self has been destroyed strongSelf->startTimer(timeout_ms);
return; return;
} }
if (src) { // 推流失败 [AUTO-TRANSLATED:4d8d226a]
// 推流给自己成功 [AUTO-TRANSLATED:65dba71b] // Push stream failed
// Push stream to yourself successfully if (!strongSelf->_process.wait(false)) {
cb(SockException()); // ffmpeg进程已经退出 [AUTO-TRANSLATED:04193893]
strongSelf->onGetMediaSource(src); // ffmpeg process has exited
strongSelf->startTimer(timeout_ms); cb(SockException(Err_other, StrPrinter << "ffmpeg已经退出,exit code = " << strongSelf->_process.exit_code()));
return; return;
} }
// 推流失败 [AUTO-TRANSLATED:4d8d226a] // ffmpeg进程还在线但是等待推流超时 [AUTO-TRANSLATED:9f71f17b]
// Push stream failed // ffmpeg process is still online, but waiting for the stream to timeout
if (!strongSelf->_process.wait(false)) { cb(SockException(Err_other, "等待超时"));
// ffmpeg进程已经退出 [AUTO-TRANSLATED:04193893] });
// ffmpeg process has exited } else{
cb(SockException(Err_other, StrPrinter << "ffmpeg已经退出,exit code = " << strongSelf->_process.exit_code())); // 推流给其他服务器的通过判断FFmpeg进程是否在线判断是否成功 [AUTO-TRANSLATED:9b963da5]
return; // 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();
// ffmpeg进程还在线但是等待推流超时 [AUTO-TRANSLATED:9f71f17b] _timer = std::make_shared<Timer>(timeout_ms / 1000.0f, [weakSelf, cb, timeout_ms]() {
// ffmpeg process is still online, but waiting for the stream to timeout auto strongSelf = weakSelf.lock();
cb(SockException(Err_other, "等待超时")); if (!strongSelf) {
}); // 自身已经销毁 [AUTO-TRANSLATED:5f954f8a]
} else { // Self has been destroyed
// 推流给其他服务器的通过判断FFmpeg进程是否在线判断是否成功 [AUTO-TRANSLATED:9b963da5] return false;
// 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(); // FFmpeg还在线那么我们认为推流成功 [AUTO-TRANSLATED:4330df49]
_timer = std::make_shared<Timer>( // FFmpeg is still online, so we think the push stream is successful
timeout_ms / 1000.0f, if (strongSelf->_process.wait(false)) {
[weakSelf, cb, timeout_ms]() { cb(SockException());
auto strongSelf = weakSelf.lock(); strongSelf->startTimer(timeout_ms);
if (!strongSelf) { return false;
// 自身已经销毁 [AUTO-TRANSLATED:5f954f8a] }
// Self has been destroyed // ffmpeg进程已经退出 [AUTO-TRANSLATED:04193893]
return false; // ffmpeg process has exited
} cb(SockException(Err_other, StrPrinter << "ffmpeg已经退出,exit code = " << strongSelf->_process.exit_code()));
// FFmpeg还在线那么我们认为推流成功 [AUTO-TRANSLATED:4330df49] return false;
// FFmpeg is still online, so we think the push stream is successful }, _poller);
if (strongSelf->_process.wait(false)) {
cb(SockException());
strongSelf->startTimer(timeout_ms);
return false;
}
// ffmpeg进程已经退出 [AUTO-TRANSLATED:04193893]
// ffmpeg process has exited
cb(SockException(Err_other, StrPrinter << "ffmpeg已经退出,exit code = " << strongSelf->_process.exit_code()));
return false;
},
_poller);
}
} catch (std::exception &ex) {
WarnL << ex.what();
cb(SockException(Err_other, ex.what()));
} }
} }

View File

@ -89,11 +89,11 @@ std::string getValue(Args &args, const Key &key) {
template<typename Key> template<typename Key>
std::string getValue(Json::Value &args, const Key &key) { std::string getValue(Json::Value &args, const Key &key) {
auto value = args.find(key); auto it = args.find(key);
if (value == nullptr) { if (it == args.end()) {
return ""; return "";
} }
return value->asString(); return it->second.asString();
} }
template<typename Key> template<typename Key>