windows视频FFmpeg拉流代理

This commit is contained in:
xiongziliang 2020-01-07 14:37:18 +08:00
parent 01bae24fdc
commit 9c42c5ed8c
7 changed files with 178 additions and 393 deletions

View File

@ -3,12 +3,7 @@ file(GLOB jsoncpp_src_list ../3rdpart/jsoncpp/*.cpp ../3rdpart/jsoncpp/*.h )
add_library(jsoncpp STATIC ${jsoncpp_src_list})
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
set(MediaServer_src_list ./WebApi.cpp ./WebHook.cpp main.cpp)
else()
file(GLOB MediaServer_src_list ./*.cpp ./*.h)
endif()
file(GLOB MediaServer_src_list ./*.cpp ./*.h)
#message(STATUS ${MediaServer_src_list})
add_executable(MediaServer ${MediaServer_src_list})

View File

@ -32,13 +32,22 @@
namespace FFmpeg {
#define FFmpeg_FIELD "ffmpeg."
const char kBin[] = FFmpeg_FIELD"bin";
const char kCmd[] = FFmpeg_FIELD"cmd";
const char kLog[] = FFmpeg_FIELD"log";
const string kBin = FFmpeg_FIELD"bin";
const string kCmd = FFmpeg_FIELD"cmd";
const string kLog = FFmpeg_FIELD"log";
onceToken token([]() {
mINI::Instance()[kBin] = trim(System::execute("which ffmpeg"));
mINI::Instance()[kCmd] = "%s -re -i %s -c:a aac -strict -2 -ar 44100 -ab 48k -c:v libx264 -f flv %s";
#ifdef _WIN32
string ffmpeg_bin = System::execute("where ffmpeg");
//windows下先关闭FFmpeg日志(目前不支持日志重定向)
mINI::Instance()[kCmd] = "%s -re -i \"%s\" -loglevel quiet -c:a aac -strict -2 -ar 44100 -ab 48k -c:v libx264 -f flv %s ";
#else
string ffmpeg_bin = System::execute("which ffmpeg");
mINI::Instance()[kCmd] = "%s -re -i \"%s\" -c:a aac -strict -2 -ar 44100 -ab 48k -c:v libx264 -f flv %s ";
#endif
//默认ffmpeg命令路径为环境变量中路径
mINI::Instance()[kBin] = ffmpeg_bin.empty() ? "ffmpeg" : ffmpeg_bin;
//ffmpeg日志保存路径
mINI::Instance()[kLog] = "./ffmpeg/ffmpeg.log";
});
}

View File

@ -26,8 +26,15 @@
#include <limits.h>
#include <sys/stat.h>
#ifndef _WIN32
#include <sys/resource.h>
#include <unistd.h>
#else
//#include <TlHelp32.h>
#include <windows.h>
#endif
#include <stdexcept>
#include <signal.h>
#include "Util/util.h"
@ -40,13 +47,32 @@ using namespace toolkit;
void Process::run(const string &cmd, const string &log_file_tmp) {
kill(2000);
#ifdef _WIN32
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si)); //结构体初始化;
ZeroMemory(&pi, sizeof(pi));
LPTSTR lpDir = const_cast<char*>(cmd .data());
if (CreateProcess(NULL, lpDir, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)){
//下面两行关闭句柄,解除本进程和新进程的关系,不然有可能 不小心调用TerminateProcess函数关掉子进程
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
_pid = pi.dwProcessId;
InfoL << "start child proces " << _pid;
} else {
WarnL << "start child proces fail: " << GetLastError();
}
#else
_pid = fork();
if (_pid < 0) {
throw std::runtime_error(StrPrinter << "fork child process falied,err:" << get_uv_errmsg());
}
if (_pid == 0) {
//子进程关闭core文件生成
struct rlimit rlim = {0,0};
struct rlimit rlim = { 0,0 };
setrlimit(RLIMIT_CORE, &rlim);
//在启动子进程时暂时禁用SIGINT、SIGTERM信号
@ -54,10 +80,11 @@ void Process::run(const string &cmd, const string &log_file_tmp) {
signal(SIGINT, SIG_IGN);
signal(SIGTERM, SIG_IGN);
string log_file ;
if(log_file_tmp.empty()){
string log_file;
if (log_file_tmp.empty()) {
log_file = "/dev/null";
}else{
}
else {
log_file = StrPrinter << log_file_tmp << "." << getpid();
}
@ -67,7 +94,8 @@ void Process::run(const string &cmd, const string &log_file_tmp) {
File::createfile_path(log_file.data(), mode);
if ((log_fd = ::open(log_file.c_str(), flags, mode)) < 0) {
fprintf(stderr, "open log file %s failed:%d(%s)\r\n", log_file.data(), errno, strerror(errno));
} else {
}
else {
// dup to stdout and stderr.
if (dup2(log_fd, STDOUT_FILENO) < 0) {
fprintf(stderr, "dup2 stdout file %s failed:%d(%s)\r\n", log_file.data(), errno, strerror(errno));
@ -89,9 +117,9 @@ void Process::run(const string &cmd, const string &log_file_tmp) {
auto params = split(cmd, " ");
// memory leak in child process, it's ok.
char **charpv_params = new char *[params.size() + 1];
for (int i = 0; i < (int) params.size(); i++) {
for (int i = 0; i < (int)params.size(); i++) {
std::string &p = params[i];
charpv_params[i] = (char *) p.data();
charpv_params[i] = (char *)p.data();
}
// EOF: NULL
charpv_params[params.size()] = NULL;
@ -103,11 +131,10 @@ void Process::run(const string &cmd, const string &log_file_tmp) {
}
exit(ret);
}
InfoL << "start child proces " << _pid;
#endif // _WIN32
}
/**
*
* @param pid
@ -120,9 +147,18 @@ static bool s_wait(pid_t pid,int *exit_code_ptr,bool block) {
return false;
}
int status = 0;
#ifdef _WIN32
HANDLE hProcess = NULL;
hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid); //打开目标进程
if (hProcess == NULL) {
return false;
}
CloseHandle(hProcess);
#else
pid_t p = waitpid(pid, &status, block ? 0 : WNOHANG);
int exit_code = (status & 0xFF00) >> 8;
if(exit_code_ptr){
if (exit_code_ptr) {
*exit_code_ptr = (status & 0xFF00) >> 8;
}
if (p < 0) {
@ -133,7 +169,8 @@ static bool s_wait(pid_t pid,int *exit_code_ptr,bool block) {
InfoL << "process terminated, pid=" << pid << ", exit code=" << exit_code;
return false;
}
//WarnL << "process is running, pid=" << _pid;
#endif // _WIN32
return true;
}
@ -142,12 +179,25 @@ static void s_kill(pid_t pid,int max_delay,bool force){
//pid无效
return;
}
#ifdef _WIN32
HANDLE hProcess = NULL;
hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid); //打开目标进程
if (hProcess == NULL) {
WarnL << "\nOpen Process fAiled: " << GetLastError();
return;
}
DWORD ret = TerminateProcess(hProcess, 0); //结束目标进程
if (ret == 0) {
WarnL << GetLastError;
}
#else
if (::kill(pid, force ? SIGKILL : SIGTERM) == -1) {
//进程可能已经退出了
WarnL << "kill process " << pid << " failed:" << get_uv_errmsg();
return;
}
#endif // _WIN32
if(force){
//发送SIGKILL信号后阻塞等待退出

View File

@ -23,10 +23,15 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef IPTV_PROCESS_H
#define IPTV_PROCESS_H
#ifndef ZLMEDIAKIT_PROCESS_H
#define ZLMEDIAKIT_PROCESS_H
#ifdef _WIN32
typedef int pid_t;
#else
#include <sys/wait.h>
#endif // _WIN32
#include <fcntl.h>
#include <string>
using namespace std;
@ -45,4 +50,4 @@ private:
};
#endif //IPTV_PROCESS_H
#endif //ZLMEDIAKIT_PROCESS_H

View File

@ -24,239 +24,36 @@
* SOFTWARE.
*/
#include "System.h"
#include <stdlib.h>
#include <signal.h>
#include <arpa/inet.h>
#if !defined(_WIN32)
#include <limits.h>
#include <sys/resource.h>
#include <unistd.h>
#include <sys/wait.h>
#ifndef ANDROID
#if !defined(ANDROID)
#include <execinfo.h>
#endif
#include <map>
#include <string>
#include <iostream>
#include "Util/mini.h"
#include "Util/util.h"
#include "Util/logger.h"
#include "Util/onceToken.h"
#include "Util/NoticeCenter.h"
#endif//!defined(ANDROID)
#endif//!defined(_WIN32)
#include "System.h"
#include <signal.h>
#include <map>
#include <iostream>
#include "Util/logger.h"
#include "Util/NoticeCenter.h"
#include "Util/uv_errno.h"
#include "Util/CMD.h"
#include "Util/MD5.h"
using namespace toolkit;
const int MAX_STACK_FRAMES = 128;
#define BroadcastOnCrashDumpArgs int &sig,const vector<vector<string> > &stack
const char kBroadcastOnCrashDump[] = "kBroadcastOnCrashDump";
//#if defined(__MACH__) || defined(__APPLE__)
//#define TEST_LINUX
//#endif
vector<string> splitWithEmptyLine(const string &s, const char *delim) {
vector<string> ret;
int last = 0;
int index = s.find(delim, last);
while (index != string::npos) {
ret.push_back(s.substr(last, index - last));
last = index + strlen(delim);
index = s.find(delim, last);
}
if (s.size() - last > 0) {
ret.push_back(s.substr(last));
}
return ret;
}
map<string, mINI> splitTopStr(const string &cmd_str) {
map<string, mINI> ret;
auto lines = splitWithEmptyLine(cmd_str, "\n");
int i = 0;
for (auto &line : lines) {
if(i++ < 1 && line.empty()){
continue;
}
if (line.empty()) {
break;
}
auto line_vec = split(line, ":");
if (line_vec.size() < 2) {
continue;
}
trim(line_vec[0], " \r\n\t");
auto args_vec = split(line_vec[1], ",");
for (auto &arg : args_vec) {
auto arg_vec = split(trim(arg, " \r\n\t."), " ");
if (arg_vec.size() < 2) {
continue;
}
ret[line_vec[0]].emplace(arg_vec[1], arg_vec[0]);
}
}
return ret;
}
bool System::getSystemUsage(SystemUsage &usage) {
try {
#if defined(__linux) || defined(__linux__) || defined(TEST_LINUX)
string cmd_str;
#if !defined(TEST_LINUX)
cmd_str = System::execute("top -b -n 1");
#else
cmd_str = "top - 07:21:31 up 5:48, 2 users, load average: 0.03, 0.62, 0.54\n"
"Tasks: 80 total, 1 running, 78 sleeping, 0 stopped, 1 zombie\n"
"%Cpu(s): 0.8 us, 0.4 sy, 0.0 ni, 98.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st\n"
"KiB Mem: 2058500 total, 249100 used, 1809400 free, 19816 buffers\n"
"KiB Swap: 1046524 total, 0 used, 1046524 free. 153012 cached Mem\n"
"\n";
#ifdef _WIN32
#define popen _popen
#define pclose _pclose
#endif
if (cmd_str.empty()) {
WarnL << "System::execute(\"top -b -n 1\") return empty";
return false;
}
auto topMap = splitTopStr(cmd_str);
usage.task_total = topMap["Tasks"]["total"];
usage.task_running = topMap["Tasks"]["running"];
usage.task_sleeping = topMap["Tasks"]["sleeping"];
usage.task_stopped = topMap["Tasks"]["stopped"];
usage.cpu_user = topMap["%Cpu(s)"]["us"];
usage.cpu_sys = topMap["%Cpu(s)"]["sy"];
usage.cpu_idle = topMap["%Cpu(s)"]["id"];
usage.mem_total = topMap["KiB Mem"]["total"];
usage.mem_free = topMap["KiB Mem"]["free"];
usage.mem_used = topMap["KiB Mem"]["used"];
return true;
#elif defined(__MACH__) || defined(__APPLE__)
/*
"Processes: 275 total, 2 running, 1 stuck, 272 sleeping, 1258 threads \n"
"2018/09/12 10:41:32\n"
"Load Avg: 2.06, 2.88, 2.86 \n"
"CPU usage: 14.54% user, 25.45% sys, 60.0% idle \n"
"SharedLibs: 117M resident, 37M data, 15M linkedit.\n"
"MemRegions: 46648 total, 3654M resident, 62M private, 714M shared.\n"
"PhysMem: 7809M used (1906M wired), 381M unused.\n"
"VM: 751G vsize, 614M framework vsize, 0(0) swapins, 0(0) swapouts.\n"
"Networks: packets: 502366/248M in, 408957/87M out.\n"
"Disks: 349435/6037M read, 78622/2577M written.";
*/
string cmd_str = System::execute("top -l 1");
if(cmd_str.empty()){
WarnL << "System::execute(\"top -n 1\") return empty";
return false;
}
auto topMap = splitTopStr(cmd_str);
usage.task_total = topMap["Processes"]["total"];
usage.task_running = topMap["Processes"]["running"];
usage.task_sleeping = topMap["Processes"]["sleeping"];
usage.task_stopped = topMap["Processes"]["stuck"];
usage.cpu_user = topMap["CPU usage"]["user"];
usage.cpu_sys = topMap["CPU usage"]["sys"];
usage.cpu_idle = topMap["CPU usage"]["idle"];
usage.mem_free = topMap["PhysMem"]["unused"].as<uint32_t>() * 1024 * 1024;
usage.mem_used = topMap["PhysMem"]["used"].as<uint32_t>() * 1024 * 1024;
usage.mem_total = usage.mem_free + usage.mem_used;
return true;
#else
WarnL << "System not supported";
return false;
#endif
} catch (std::exception &ex) {
WarnL << ex.what();
return false;
}
}
bool System::getNetworkUsage(vector<NetworkUsage> &usage) {
try {
#if defined(__linux) || defined(__linux__) || defined(TEST_LINUX)
string cmd_str;
#if !defined(TEST_LINUX)
cmd_str = System::execute("cat /proc/net/dev");
#else
cmd_str =
"Inter-| Receive | Transmit\n"
" face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed\n"
" lo: 475978 7546 0 0 0 0 0 0 475978 7546 0 0 0 0 0 0\n"
"enp3s0: 151747818 315136 0 0 0 0 0 145 1590783447 1124616 0 0 0 0 0 0";
#endif
if (cmd_str.empty()) {
return false;
}
auto lines = split(cmd_str, "\n");
int i = 0;
vector<string> column_name_vec;
vector<string> category_prefix_vec;
for (auto &line : lines) {
switch (i++) {
case 0: {
category_prefix_vec = split(line, "|");
}
break;
case 1: {
auto category_suffix_vec = split(line, "|");
int j = 0;
for (auto &category_suffix : category_suffix_vec) {
auto column_suffix_vec = split(category_suffix, " ");
for (auto &column_suffix : column_suffix_vec) {
column_name_vec.emplace_back(trim(category_prefix_vec[j]) + "-" + trim(column_suffix));
}
j++;
}
}
break;
default: {
mINI valMap;
auto vals = split(line, " ");
int j = 0;
for (auto &val : vals) {
valMap[column_name_vec[j++]] = trim(val, " \r\n\t:");
}
usage.emplace_back(NetworkUsage());
auto &ifrUsage = usage.back();
ifrUsage.interface = valMap["Inter--face"];
ifrUsage.recv_bytes = valMap["Receive-bytes"];
ifrUsage.recv_packets = valMap["Receive-packets"];
ifrUsage.snd_bytes = valMap["Transmit-bytes"];
ifrUsage.snd_packets = valMap["Transmit-packets"];
}
break;
}
}
return true;
#else
WarnL << "System not supported";
return false;
#endif
} catch (std::exception &ex) {
WarnL << ex.what();
return false;
}
}
bool System::getTcpUsage(System::TcpUsage &usage) {
usage.established = atoi(trim(System::execute("netstat -na|grep ESTABLISHED|wc -l")).data());
usage.syn_recv = atoi(trim(System::execute("netstat -na|grep SYN_RECV|wc -l")).data());
usage.time_wait = atoi(trim(System::execute("netstat -na|grep TIME_WAIT|wc -l")).data());
usage.close_wait = atoi(trim(System::execute("netstat -na|grep CLOSE_WAIT|wc -l")).data());
return true;
}
string System::execute(const string &cmd) {
// DebugL << cmd;
FILE *fPipe = popen(cmd.data(), "r");
FILE *fPipe = NULL;
fPipe = popen(cmd.data(), "r");
if(!fPipe){
return "";
}
@ -269,12 +66,12 @@ string System::execute(const string &cmd) {
return ret;
}
#if !defined(ANDROID) && !defined(_WIN32)
static string addr2line(const string &address) {
string cmd = StrPrinter << "addr2line -e " << exePath() << " " << address;
return System::execute(cmd);
}
#ifndef ANDROID
static void sig_crash(int sig) {
signal(sig, SIG_DFL);
void *array[MAX_STACK_FRAMES];
@ -294,14 +91,13 @@ static void sig_crash(int sig) {
#endif//__linux
}
free(strings);
NoticeCenter::Instance().emitEvent(kBroadcastOnCrashDump,sig,stack);
}
#endif//#ifndef ANDROID
#endif // !defined(ANDROID) && !defined(_WIN32)
void System::startDaemon() {
#ifndef _WIN32
static pid_t pid;
do{
pid = fork();
@ -336,21 +132,11 @@ void System::startDaemon() {
DebugL << "waitpid被中断:" << get_uv_errmsg();
}while (true);
}while (true);
}
static string currentDateTime(){
time_t ts = time(NULL);
std::tm tm_snapshot;
localtime_r(&ts, &tm_snapshot);
char buffer[1024] = {0};
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tm_snapshot);
return buffer;
#endif // _WIN32
}
void System::systemSetup(){
#if !defined(_WIN32)
struct rlimit rlim,rlim_new;
if (getrlimit(RLIMIT_CORE, &rlim)==0) {
rlim_new.rlim_cur = rlim_new.rlim_max = RLIM_INFINITY;
@ -373,11 +159,9 @@ void System::systemSetup(){
#ifndef ANDROID
signal(SIGSEGV, sig_crash);
signal(SIGABRT, sig_crash);
#endif//#ifndef ANDROID
NoticeCenter::Instance().addListener(nullptr,kBroadcastOnCrashDump,[](BroadcastOnCrashDumpArgs){
stringstream ss;
ss << "## crash date:" << currentDateTime() << endl;
ss << "## crash date:" << getTimeStr("%Y-%m-%d %H:%M:%S") << endl;
ss << "## exe: " << exeName() << endl;
ss << "## signal: " << sig << endl;
ss << "## stack: " << endl;
@ -391,8 +175,9 @@ void System::systemSetup(){
ofstream out(StrPrinter << exeDir() << "/crash." << getpid(), ios::out | ios::binary | ios::trunc);
out << stack_info;
out.flush();
cerr << stack_info << endl;
});
#endif// ANDROID
#endif//!defined(_WIN32)
}

View File

@ -24,67 +24,17 @@
* SOFTWARE.
*/
#ifndef IPTV_BASH_H
#define IPTV_BASH_H
#ifndef ZLMEDIAKIT_SYSTEM_H
#define ZLMEDIAKIT_SYSTEM_H
#include <cstdint>
#include <string>
#include <vector>
#include <map>
#include "Util/NoticeCenter.h"
using namespace std;
using namespace toolkit;
class System {
public:
typedef struct {
uint32_t task_total;
uint32_t task_running;
uint32_t task_sleeping;
uint32_t task_stopped;
uint64_t mem_total;
uint64_t mem_free;
uint64_t mem_used;
float cpu_user;
float cpu_sys;
float cpu_idle;
} SystemUsage;
typedef struct {
uint64_t recv_bytes;
uint64_t recv_packets;
uint64_t snd_bytes;
uint64_t snd_packets;
string interface;
} NetworkUsage;
typedef struct {
uint64_t available;
uint64_t used;
float used_per;
string mounted_on;
string filesystem;
bool mounted;
} DiskUsage;
typedef struct {
uint32_t established;
uint32_t syn_recv;
uint32_t time_wait;
uint32_t close_wait;
} TcpUsage;
static bool getSystemUsage(SystemUsage &usage);
static bool getNetworkUsage(vector<NetworkUsage> &usage);
static bool getTcpUsage(TcpUsage &usage);
static string execute(const string &cmd);
static void startDaemon();
static void systemSetup();
};
#endif //IPTV_BASH_H
#endif //ZLMEDIAKIT_SYSTEM_H

View File

@ -47,10 +47,7 @@
#include "WebHook.h"
#include "Thread/WorkThreadPool.h"
#include "Rtp/RtpSelector.h"
#if !defined(_WIN32)
#include "FFmpegSource.h"
#endif//!defined(_WIN32)
using namespace Json;
using namespace toolkit;
@ -268,10 +265,8 @@ static inline string getProxyKey(const string &vhost,const string &app,const str
return vhost + "/" + app + "/" + stream;
}
#if !defined(_WIN32)
static unordered_map<string ,FFmpegSource::Ptr> s_ffmpegMap;
static recursive_mutex s_ffmpegMapMtx;
#endif//#if !defined(_WIN32)
/**
* api接口
@ -646,7 +641,6 @@ void installWebApi() {
val["data"]["flag"] = s_proxyMap.erase(allArgs["key"]) == 1;
});
#if !defined(_WIN32)
static auto addFFmpegSource = [](const string &src_url,
const string &dst_url,
int timeout_ms,
@ -713,7 +707,6 @@ void installWebApi() {
API_REGIST(api,delFFmepgSource,{
api_delFFmpegSource(API_ARGS_VALUE);
});
#endif
//新增http api下载可执行程序文件接口
//测试url http://127.0.0.1/index/api/downloadBin
@ -932,10 +925,8 @@ void unInstallWebApi(){
s_proxyMap.clear();
}
#if !defined(_WIN32)
{
lock_guard<recursive_mutex> lck(s_ffmpegMapMtx);
s_ffmpegMap.clear();
}
#endif
}