2017-03-25 09:21:39 +00:00
|
|
|
/**
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2021-04-16 01:29:43 +00:00
|
|
|
* Copyright (c) 2013-2021 Winlin
|
2017-03-25 09:21:39 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2015-08-24 13:51:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SRS_APP_PROCESS_HPP
|
|
|
|
#define SRS_APP_PROCESS_HPP
|
|
|
|
|
|
|
|
#include <srs_core.hpp>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-04-30 00:24:52 +00:00
|
|
|
// Start and stop a process. Call cycle to restart the process when terminated.
|
|
|
|
// The usage:
|
|
|
|
// // the binary is the process to fork.
|
|
|
|
// binary = "./objs/ffmpeg/bin/ffmpeg";
|
|
|
|
// // where argv is a array contains each params.
|
|
|
|
// argv = ["./objs/ffmpeg/bin/ffmpeg", "-i", "in.flv", "1", ">", "/dev/null", "2", ">", "/dev/null"];
|
|
|
|
//
|
|
|
|
// process = new SrsProcess();
|
|
|
|
// if ((ret = process->initialize(binary, argv)) != ERROR_SUCCESS) { return ret; }
|
|
|
|
// if ((ret = process->start()) != ERROR_SUCCESS) { return ret; }
|
|
|
|
// if ((ret = process->cycle()) != ERROR_SUCCESS) { return ret; }
|
|
|
|
// process->fast_stop();
|
|
|
|
// process->stop();
|
2015-08-24 13:51:05 +00:00
|
|
|
class SrsProcess
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
bool is_started;
|
2019-04-30 00:24:52 +00:00
|
|
|
// Whether SIGTERM send but need to wait or SIGKILL.
|
2015-08-24 13:51:05 +00:00
|
|
|
bool fast_stopped;
|
|
|
|
pid_t pid;
|
|
|
|
private:
|
|
|
|
std::string bin;
|
2015-08-24 14:19:57 +00:00
|
|
|
std::string stdout_file;
|
|
|
|
std::string stderr_file;
|
2015-08-24 13:51:05 +00:00
|
|
|
std::vector<std::string> params;
|
2019-04-30 00:24:52 +00:00
|
|
|
// The cli to fork process.
|
2015-08-24 14:19:57 +00:00
|
|
|
std::string cli;
|
2016-01-08 05:58:19 +00:00
|
|
|
std::string actual_cli;
|
2015-08-24 13:51:05 +00:00
|
|
|
public:
|
|
|
|
SrsProcess();
|
|
|
|
virtual ~SrsProcess();
|
|
|
|
public:
|
2019-04-30 00:24:52 +00:00
|
|
|
// Get pid of process.
|
2016-01-08 05:58:19 +00:00
|
|
|
virtual int get_pid();
|
2019-04-30 00:24:52 +00:00
|
|
|
// whether process is already started.
|
2015-08-24 13:51:05 +00:00
|
|
|
virtual bool started();
|
2019-04-30 00:24:52 +00:00
|
|
|
// Initialize the process with binary and argv.
|
|
|
|
// @param binary the binary path to exec.
|
|
|
|
// @param argv the argv for binary path, the argv[0] generally is the binary.
|
|
|
|
// @remark the argv[0] must be the binary.
|
2018-01-01 11:39:57 +00:00
|
|
|
virtual srs_error_t initialize(std::string binary, std::vector<std::string> argv);
|
2015-08-24 13:51:05 +00:00
|
|
|
public:
|
2019-04-30 00:24:52 +00:00
|
|
|
// Start the process, ignore when already started.
|
2018-01-01 11:39:57 +00:00
|
|
|
virtual srs_error_t start();
|
2019-04-30 00:24:52 +00:00
|
|
|
// cycle check the process, update the state of process.
|
|
|
|
// @remark when process terminated(not started), user can restart it again by start().
|
2018-01-01 11:39:57 +00:00
|
|
|
virtual srs_error_t cycle();
|
2019-04-30 00:24:52 +00:00
|
|
|
// Send SIGTERM then SIGKILL to ensure the process stopped.
|
|
|
|
// the stop will wait [0, SRS_PROCESS_QUIT_TIMEOUT_MS] depends on the
|
|
|
|
// process quit timeout.
|
|
|
|
// @remark use fast_stop before stop one by one, when got lots of process to quit.
|
2015-08-24 13:51:05 +00:00
|
|
|
virtual void stop();
|
|
|
|
public:
|
2019-04-30 00:24:52 +00:00
|
|
|
// The fast stop is to send a SIGTERM.
|
|
|
|
// for example, the ingesters owner lots of FFMPEG, it will take a long time
|
|
|
|
// to stop one by one, instead the ingesters can fast_stop all FFMPEG, then
|
|
|
|
// wait one by one to stop, it's more faster.
|
|
|
|
// @remark user must use stop() to ensure the ffmpeg to stopped.
|
|
|
|
// @remark we got N processes to stop, compare the time we spend,
|
|
|
|
// when use stop without fast_stop, we spend maybe [0, SRS_PROCESS_QUIT_TIMEOUT_MS * N]
|
|
|
|
// but use fast_stop then stop, the time is almost [0, SRS_PROCESS_QUIT_TIMEOUT_MS].
|
2015-08-24 13:51:05 +00:00
|
|
|
virtual void fast_stop();
|
2020-01-29 12:22:28 +00:00
|
|
|
// Directly kill process, never use it except server quiting.
|
|
|
|
virtual void fast_kill();
|
2015-08-24 13:51:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|