diff --git a/trunk/src/app/srs_app_ffmpeg.cpp b/trunk/src/app/srs_app_ffmpeg.cpp index eacdd04f6..ca1f8850a 100644 --- a/trunk/src/app/srs_app_ffmpeg.cpp +++ b/trunk/src/app/srs_app_ffmpeg.cpp @@ -24,12 +24,16 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include -#include #include #include #include #include +// for srs-librtmp, @see https://github.com/simple-rtmp-server/srs/issues/213 +#ifndef _WIN32 +#include +#endif + #include using namespace std; @@ -407,7 +411,7 @@ int SrsFFMPEG::start() params.push_back(_output); // when specified the log file. - if (false && !log_file.empty()) { + if (!log_file.empty()) { // stdout params.push_back("1"); params.push_back(">"); diff --git a/trunk/src/app/srs_app_process.cpp b/trunk/src/app/srs_app_process.cpp index f43d41067..25735d3f6 100644 --- a/trunk/src/app/srs_app_process.cpp +++ b/trunk/src/app/srs_app_process.cpp @@ -30,6 +30,11 @@ #include #include +// for srs-librtmp, @see https://github.com/simple-rtmp-server/srs/issues/213 +#ifndef _WIN32 +#include +#endif + using namespace std; #include @@ -57,8 +62,34 @@ int SrsProcess::initialize(string binary, vector argv) { int ret = ERROR_SUCCESS; - bin = binary; - params = argv; + cli = bin = binary; + + for (int i = 0; i < (int)argv.size(); i++) { + std::string ffp = argv[i]; + cli += " " + ffp; + } + + for (int i = 0; i < (int)argv.size(); i++) { + std::string ffp = argv[i]; + + // remove the stdout and stderr. + if (ffp == "1") { + if (i + 2 < (int)argv.size()) { + stdout_file = argv[i + 2]; + i += 2; + } + continue; + } else if (ffp == "2") { + if (i + 2 < (int)argv.size()) { + stderr_file = argv[i + 2]; + i += 2; + } + continue; + } + + // startup params. + params.push_back(ffp); + } return ret; } @@ -71,20 +102,12 @@ int SrsProcess::start() return ret; } - std::string cli; - if (true) { - for (int i = 0; i < (int)params.size(); i++) { - std::string ffp = params[i]; - cli += ffp; - if (i < (int)params.size() - 1) { - cli += " "; - } - } - srs_trace("fork process: %s %s", bin.c_str(), cli.c_str()); - } + // generate the argv of process. + srs_trace("fork process: %s", cli.c_str()); // for log int cid = _srs_context->get_id(); + int ppid = getpid(); // TODO: fork or vfork? if ((pid = fork()) < 0) { @@ -99,12 +122,51 @@ int SrsProcess::start() signal(SIGINT, SIG_IGN); signal(SIGTERM, SIG_IGN); + // redirect stdout to file. + if (!stdout_file.empty()) { + int stdout_fd = -1; + int flags = O_CREAT|O_WRONLY|O_APPEND; + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH; + + if ((stdout_fd = ::open(stdout_file.c_str(), flags, mode)) < 0) { + ret = ERROR_ENCODER_OPEN; + fprintf(stderr, "open process stdout %s failed. ret=%d", stdout_file.c_str(), ret); + return ret; + } + + if (dup2(stdout_fd, STDOUT_FILENO) < 0) { + ret = ERROR_ENCODER_DUP2; + srs_error("dup2 process stdout failed. ret=%d", ret); + return ret; + } + } + + // redirect stderr to file. + if (!stderr_file.empty()) { + int stderr_fd = -1; + int flags = O_CREAT|O_WRONLY|O_APPEND; + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH; + + if ((stderr_fd = ::open(stderr_file.c_str(), flags, mode)) < 0) { + ret = ERROR_ENCODER_OPEN; + fprintf(stderr, "open process stderr %s failed. ret=%d", stderr_file.c_str(), ret); + return ret; + } + + if (dup2(stderr_fd, STDERR_FILENO) < 0) { + ret = ERROR_ENCODER_DUP2; + srs_error("dup2 process stderr failed. ret=%d", ret); + return ret; + } + } + // log basic info if (true) { fprintf(stderr, "\n"); - fprintf(stderr, "process parent cid=%d", cid); - fprintf(stderr, "process binary=%s", bin.c_str()); - fprintf(stderr, "process params: %s %s", bin.c_str(), cli.c_str()); + fprintf(stderr, "process parent pid=%d\n", ppid); + fprintf(stderr, "process parent cid=%d\n", cid); + fprintf(stderr, "process binary=%s\n", bin.c_str()); + fprintf(stderr, "process cli: %s\n", cli.c_str()); } // close other fds @@ -133,7 +195,7 @@ int SrsProcess::start() // parent. if (pid > 0) { is_started = true; - srs_trace("vfored process, pid=%d, cli=%s", pid, bin.c_str()); + srs_trace("vfored process, pid=%d, bin=%s", pid, bin.c_str()); return ret; } diff --git a/trunk/src/app/srs_app_process.hpp b/trunk/src/app/srs_app_process.hpp index 9b999b06e..1b668f20b 100644 --- a/trunk/src/app/srs_app_process.hpp +++ b/trunk/src/app/srs_app_process.hpp @@ -51,7 +51,11 @@ private: pid_t pid; private: std::string bin; + std::string stdout_file; + std::string stderr_file; std::vector params; + // the cli to fork process. + std::string cli; public: SrsProcess(); virtual ~SrsProcess();