1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
#include "ffmpeg.h"
#include "setup.h"
#include <exception>
#include <unistd.h>
#include <sys/wait.h>
#include <sstream>
#include <fstream>
#include <vdr/tools.h>
#include <tnt/tntnet.h>
namespace vdrlive {
FFmpegThread::FFmpegThread()
:cThread("stream utility handler")
{
dsyslog("Live: FFmpegTread() created");
}
FFmpegThread::~FFmpegThread()
{
Stop();
dsyslog("Live: FFmpegTread() destructed");
}
void FFmpegThread::StartFFmpeg(std::string s, std::string url, std::string tag)
{
if ( targetUrl.compare(url) || targetTag.compare(tag)) {
dsyslog("Live: FFmpegTread::StartFFmpeg() change %s [%s] -> %s [%s]", targetUrl.c_str(), targetTag.c_str(), url.c_str(), tag.c_str());
if ( Active() ) Stop();
targetUrl = url;
targetTag = tag;
}
session = s;
Start();
dsyslog("Live: FFmpegTread::StartFFmpeg() completed");
}
void FFmpegThread::Stop()
{
cw.Signal();
dsyslog("Live: FFmpegTread::Stop() try stopping");
if ( Active() ) Cancel( 5 );
dsyslog("Live: FFmpegTread::Stop() stopped");
}
void FFmpegThread::Touch()
{
touch = true;
}
void FFmpegThread::Action()
{
dsyslog("Live: FFmpegTread::Action() started url = %s [%s]", targetUrl.c_str(), targetTag.c_str());
// read command for tag from FFMPG configuration file
std::string packerCmd = LiveSetup().ReadStreamVideoFFmpegCmdFromConfigFile(targetTag);
if (packerCmd.empty()) {
esyslog("ERROR: live could not find FFMPEG command for tag \"%s\"", targetTag.c_str());
} else {
std::stringstream ss;
ss.str("");
ss << "\"http://localhost:" << LiveSetup().GetStreamdevPort() << "/" << targetUrl << "\"";
packerCmd.replace(packerCmd.find("<input>"), 7, ss.str());
dsyslog("Live: FFmpegTread::Action packetizer cmd: %s", packerCmd.c_str());
try {
cPipe2 pp;
int retry = 0;
int count = 0;
do {
ss.str("");
ss << "mkdir -p " << tmpHlsBufferDir << session << " && "
"cd " << tmpHlsBufferDir << session << " && rm -rf * && "
"exec " << packerCmd << " "
"-f hls -hls_time 1 -hls_start_number_source datetime -hls_flags delete_segments "
"-master_pl_name master_";
ss << targetUrl;
ss << ".m3u8 ffmpeg_";
ss << targetUrl;
ss << "_data.m3u8";
bool ret = pp.Open(ss.str().c_str(), "w"); // start ffmpeg
dsyslog("Live: FFmpegTread::Action::Open(%d) ffmpeg started", ret);
ss.str("");
ss << tmpHlsBufferDir << session << "/master_";
ss << targetUrl;
ss << ".m3u8";
count = 0;
do {
cw.Wait(1000);
std::ifstream f(ss.str().c_str());
if (f.good()) break; // check if ffmpeg starts to generate output
dsyslog("Live: FFmpegTread::Action() ffmpeg starting... %d", count);
} while (Running() && pp.Check() == 0 && ++count < 6);
if (pp.Check() < 0) continue;
if (count < 6) {
dsyslog("Live: FFmpegTread::Action() ffmpeg running %d", count);
break;
}
else { // ffmpeg did not start properly
fwrite("q", 1, 1, pp); fflush(pp); // send quit commmand to ffmpeg
usleep(200e3);
int r = pp.Close();
dsyslog("Live: FFmpegTread::Action::Close(%d) disabled ffmpeg", r);
usleep(500e3);
}
} while (retry++ < 2 && Running());
if (retry > 1) return;
touch = false;
count = 0;
while (Running() && pp.Check() == 0 && count++ < 60) {
if (touch) {
touch = false;
count = 0;
}
cw.Wait(1000);
}
fwrite("q", 1, 1, pp); fflush(pp); // send quit commmand to ffmpeg
usleep(500e3);
int r = pp.Close();
dsyslog("Live: FFmpegTread::Action::Close(%d) disabled ffmpeg", r);
} catch (std::exception const& ex) {
esyslog("ERROR: live FFmpegTread::Action() failed: %s", ex.what());
}
}
dsyslog("Live: FFmpegTread::Action() finished");
}
// --- cPipe2 -----------------------------------------------------------------
// cPipe2::Open() and cPipe2::Close() are based on code originally received from
// Andreas Vitting <Andreas@huji.de>
cPipe2::cPipe2(void)
{
pid = -1;
f = NULL;
}
cPipe2::~cPipe2()
{
Close();
}
bool cPipe2::Open(const char *Command, const char *Mode)
{
int fd[2];
if (pipe(fd) < 0) {
LOG_ERROR;
return false;
}
if ((pid = fork()) < 0) { // fork failed
LOG_ERROR;
close(fd[0]);
close(fd[1]);
return false;
}
const char *mode = "w";
int iopipe = 0;
if (pid > 0) { // parent process
terminated = false;
if (strcmp(Mode, "r") == 0) {
mode = "r";
iopipe = 1;
}
close(fd[iopipe]);
if ((f = fdopen(fd[1 - iopipe], mode)) == NULL) {
LOG_ERROR;
close(fd[1 - iopipe]);
}
return f != NULL;
}
else { // child process
int iofd = STDOUT_FILENO;
if (strcmp(Mode, "w") == 0) {
iopipe = 1;
iofd = STDIN_FILENO;
}
close(fd[iopipe]);
if (dup2(fd[1 - iopipe], iofd) == -1) { // now redirect
LOG_ERROR;
close(fd[1 - iopipe]);
_exit(-1);
}
else {
int MaxPossibleFileDescriptors = getdtablesize();
for (int i = STDERR_FILENO + 1; i < MaxPossibleFileDescriptors; i++)
close(i); //close all dup'ed filedescriptors
if (execl("/bin/sh", "sh", "-c", Command, NULL) == -1) {
LOG_ERROR_STR(Command);
close(fd[1 - iopipe]);
_exit(-1);
}
}
_exit(0);
}
}
int cPipe2::Check(void)
{
int ret = -1;
if (terminated) return -1;
if (pid > 0) {
int status = 0;
ret = waitpid(pid, &status, WNOHANG);;
if (ret < 0) {
if (errno != EINTR && errno != ECHILD) {
LOG_ERROR;
return ret;
}
}
if (ret > 0) terminated = true;
}
return ret;
}
int cPipe2::Close(void)
{
int ret = -1;
if (f) {
fclose(f);
f = NULL;
}
if (pid > 0) {
int status = 0;
int i = 5;
while (i > 0) {
ret = waitpid(pid, &status, WNOHANG);
if (ret < 0) {
if (errno != EINTR && errno != ECHILD) {
LOG_ERROR;
break;
}
}
else if (ret == pid)
break;
i--;
cCondWait::SleepMs(100);
}
if (!i) {
kill(pid, SIGINT);
cCondWait::SleepMs(100);
ret = waitpid(pid, &status, WNOHANG);
kill(pid, SIGKILL);
cCondWait::SleepMs(100);
ret = waitpid(pid, &status, WNOHANG);
}
else if (ret == -1 || !WIFEXITED(status))
ret = -1;
pid = -1;
}
return ret;
}
} // namespace vdrlive
|