2002-06-22 10:11:59 +02:00
|
|
|
/*
|
|
|
|
* cutter.c: The video cutting facilities
|
|
|
|
*
|
|
|
|
* See the main source file 'vdr.c' for copyright information and
|
|
|
|
* how to reach the author.
|
|
|
|
*
|
2012-09-22 11:52:33 +02:00
|
|
|
* $Id: cutter.c 2.14 2012/09/20 09:12:47 kls Exp $
|
2002-06-22 10:11:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cutter.h"
|
2012-02-16 12:20:46 +01:00
|
|
|
#include "menu.h"
|
2002-06-22 10:11:59 +02:00
|
|
|
#include "recording.h"
|
|
|
|
#include "remux.h"
|
|
|
|
#include "videodir.h"
|
|
|
|
|
|
|
|
// --- cCuttingThread --------------------------------------------------------
|
|
|
|
|
|
|
|
class cCuttingThread : public cThread {
|
|
|
|
private:
|
|
|
|
const char *error;
|
2009-04-19 11:07:07 +02:00
|
|
|
bool isPesRecording;
|
2005-10-31 13:14:26 +01:00
|
|
|
cUnbufferedFile *fromFile, *toFile;
|
2002-06-22 10:11:59 +02:00
|
|
|
cFileName *fromFileName, *toFileName;
|
|
|
|
cIndexFile *fromIndex, *toIndex;
|
|
|
|
cMarks fromMarks, toMarks;
|
2009-01-24 15:24:19 +01:00
|
|
|
off_t maxVideoFileSize;
|
2002-06-22 10:11:59 +02:00
|
|
|
protected:
|
|
|
|
virtual void Action(void);
|
|
|
|
public:
|
|
|
|
cCuttingThread(const char *FromFileName, const char *ToFileName);
|
|
|
|
virtual ~cCuttingThread();
|
|
|
|
const char *Error(void) { return error; }
|
|
|
|
};
|
|
|
|
|
|
|
|
cCuttingThread::cCuttingThread(const char *FromFileName, const char *ToFileName)
|
2003-10-18 12:29:08 +02:00
|
|
|
:cThread("video cutting")
|
2002-06-22 10:11:59 +02:00
|
|
|
{
|
|
|
|
error = NULL;
|
2005-10-31 13:14:26 +01:00
|
|
|
fromFile = toFile = NULL;
|
2002-06-22 10:11:59 +02:00
|
|
|
fromFileName = toFileName = NULL;
|
|
|
|
fromIndex = toIndex = NULL;
|
2009-01-06 14:41:11 +01:00
|
|
|
cRecording Recording(FromFileName);
|
2009-04-19 11:07:07 +02:00
|
|
|
isPesRecording = Recording.IsPesRecording();
|
2009-01-06 14:41:11 +01:00
|
|
|
if (fromMarks.Load(FromFileName, Recording.FramesPerSecond(), isPesRecording) && fromMarks.Count()) {
|
|
|
|
fromFileName = new cFileName(FromFileName, false, true, isPesRecording);
|
|
|
|
toFileName = new cFileName(ToFileName, true, true, isPesRecording);
|
|
|
|
fromIndex = new cIndexFile(FromFileName, false, isPesRecording);
|
|
|
|
toIndex = new cIndexFile(ToFileName, true, isPesRecording);
|
|
|
|
toMarks.Load(ToFileName, Recording.FramesPerSecond(), isPesRecording); // doesn't actually load marks, just sets the file name
|
2009-01-24 15:24:19 +01:00
|
|
|
maxVideoFileSize = MEGABYTE(Setup.MaxVideoFileSize);
|
|
|
|
if (isPesRecording && maxVideoFileSize > MEGABYTE(MAXVIDEOFILESIZEPES))
|
|
|
|
maxVideoFileSize = MEGABYTE(MAXVIDEOFILESIZEPES);
|
2002-06-22 10:11:59 +02:00
|
|
|
Start();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog("no editing marks found for %s", FromFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
cCuttingThread::~cCuttingThread()
|
|
|
|
{
|
|
|
|
Cancel(3);
|
|
|
|
delete fromFileName;
|
|
|
|
delete toFileName;
|
|
|
|
delete fromIndex;
|
|
|
|
delete toIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCuttingThread::Action(void)
|
|
|
|
{
|
|
|
|
cMark *Mark = fromMarks.First();
|
|
|
|
if (Mark) {
|
2011-03-06 14:59:57 +01:00
|
|
|
SetPriority(19);
|
|
|
|
SetIOPriority(7);
|
2002-06-22 10:11:59 +02:00
|
|
|
fromFile = fromFileName->Open();
|
|
|
|
toFile = toFileName->Open();
|
2005-10-31 13:14:26 +01:00
|
|
|
if (!fromFile || !toFile)
|
2005-08-13 13:17:24 +02:00
|
|
|
return;
|
2006-02-04 14:12:17 +01:00
|
|
|
fromFile->SetReadAhead(MEGABYTE(20));
|
2011-08-21 11:34:30 +02:00
|
|
|
int Index = Mark->Position();
|
2002-06-22 10:11:59 +02:00
|
|
|
Mark = fromMarks.Next(Mark);
|
2009-01-06 14:41:11 +01:00
|
|
|
off_t FileSize = 0;
|
2002-06-22 10:11:59 +02:00
|
|
|
int CurrentFileNumber = 0;
|
|
|
|
int LastIFrame = 0;
|
|
|
|
toMarks.Add(0);
|
|
|
|
toMarks.Save();
|
2012-06-10 14:33:36 +02:00
|
|
|
uchar buffer[MAXFRAMESIZE], buffer2[MAXFRAMESIZE];
|
|
|
|
int Length2;
|
|
|
|
bool CheckForSeamlessStream = false;
|
2003-05-24 12:01:52 +02:00
|
|
|
bool LastMark = false;
|
2003-04-26 15:11:17 +02:00
|
|
|
bool cutIn = true;
|
2012-09-22 11:52:33 +02:00
|
|
|
bool suspensionLogged = false;
|
2005-08-14 11:24:57 +02:00
|
|
|
while (Running()) {
|
2009-01-06 14:41:11 +01:00
|
|
|
uint16_t FileNumber;
|
|
|
|
off_t FileOffset;
|
|
|
|
int Length;
|
|
|
|
bool Independent;
|
2002-06-22 10:11:59 +02:00
|
|
|
|
2012-09-22 11:52:33 +02:00
|
|
|
// Suspend cutting if we have severe throughput problems:
|
|
|
|
|
|
|
|
if (cIoThrottle::Engaged()) {
|
|
|
|
if (!suspensionLogged) {
|
|
|
|
dsyslog("suspending cutter thread");
|
|
|
|
suspensionLogged = true;
|
|
|
|
}
|
|
|
|
cCondWait::SleepMs(100);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (suspensionLogged) {
|
|
|
|
dsyslog("resuming cutter thread");
|
|
|
|
suspensionLogged = false;
|
|
|
|
}
|
|
|
|
|
2002-06-22 10:11:59 +02:00
|
|
|
// Make sure there is enough disk space:
|
|
|
|
|
2003-08-17 09:18:40 +02:00
|
|
|
AssertFreeDiskSpace(-1);
|
2002-06-22 10:11:59 +02:00
|
|
|
|
|
|
|
// Read one frame:
|
|
|
|
|
2009-01-06 14:41:11 +01:00
|
|
|
if (fromIndex->Get(Index++, &FileNumber, &FileOffset, &Independent, &Length)) {
|
2002-06-22 10:11:59 +02:00
|
|
|
if (FileNumber != CurrentFileNumber) {
|
|
|
|
fromFile = fromFileName->SetOffset(FileNumber, FileOffset);
|
2011-12-04 12:56:55 +01:00
|
|
|
if (fromFile)
|
|
|
|
fromFile->SetReadAhead(MEGABYTE(20));
|
2002-06-22 10:11:59 +02:00
|
|
|
CurrentFileNumber = FileNumber;
|
|
|
|
}
|
2005-10-31 13:14:26 +01:00
|
|
|
if (fromFile) {
|
2002-06-22 10:11:59 +02:00
|
|
|
int len = ReadFrame(fromFile, buffer, Length, sizeof(buffer));
|
|
|
|
if (len < 0) {
|
|
|
|
error = "ReadFrame";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (len != Length) {
|
|
|
|
CurrentFileNumber = 0; // this re-syncs in case the frame was larger than the buffer
|
|
|
|
Length = len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error = "fromFile";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-08-25 10:36:10 +02:00
|
|
|
else {
|
2008-01-13 12:23:39 +01:00
|
|
|
// Error, unless we're past the last cut-in and there's no cut-out
|
|
|
|
if (Mark || LastMark)
|
|
|
|
error = "index";
|
2002-06-22 10:11:59 +02:00
|
|
|
break;
|
2007-08-25 10:36:10 +02:00
|
|
|
}
|
2002-06-22 10:11:59 +02:00
|
|
|
|
|
|
|
// Write one frame:
|
|
|
|
|
2009-01-06 14:41:11 +01:00
|
|
|
if (Independent) { // every file shall start with an independent frame
|
2003-05-24 12:01:52 +02:00
|
|
|
if (LastMark) // edited version shall end before next I-frame
|
2002-06-22 10:11:59 +02:00
|
|
|
break;
|
2009-01-24 15:24:19 +01:00
|
|
|
if (FileSize > maxVideoFileSize) {
|
2002-06-22 10:11:59 +02:00
|
|
|
toFile = toFileName->NextFile();
|
2006-01-27 13:47:08 +01:00
|
|
|
if (!toFile) {
|
2002-06-22 10:11:59 +02:00
|
|
|
error = "toFile 1";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
FileSize = 0;
|
|
|
|
}
|
|
|
|
LastIFrame = 0;
|
2012-06-10 14:33:36 +02:00
|
|
|
// Compare the current frame with the previously stored one, to see if this is a seamlessly merged recording of the same stream:
|
|
|
|
if (CheckForSeamlessStream) {
|
|
|
|
if (Length == Length2) {
|
|
|
|
int diffs = 0;
|
|
|
|
for (int i = 0; i < Length; i++) {
|
|
|
|
if (buffer[i] != buffer2[i]) {
|
|
|
|
if (diffs++ > 10)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (diffs < 10) // the continuity counters of the PAT/PMT packets may differ
|
|
|
|
cutIn = false; // it's apparently a seamless stream, so no need for "broken" handling
|
|
|
|
}
|
|
|
|
CheckForSeamlessStream = false;
|
|
|
|
}
|
2003-04-26 15:11:17 +02:00
|
|
|
if (cutIn) {
|
2009-04-19 11:07:07 +02:00
|
|
|
if (isPesRecording)
|
|
|
|
cRemux::SetBrokenLink(buffer, Length);
|
|
|
|
else
|
|
|
|
TsSetTeiOnBrokenPackets(buffer, Length);
|
2003-04-26 15:11:17 +02:00
|
|
|
cutIn = false;
|
|
|
|
}
|
2002-06-22 10:11:59 +02:00
|
|
|
}
|
2005-10-31 13:14:26 +01:00
|
|
|
if (toFile->Write(buffer, Length) < 0) {
|
2002-06-22 10:11:59 +02:00
|
|
|
error = "safe_write";
|
|
|
|
break;
|
|
|
|
}
|
2009-01-06 14:41:11 +01:00
|
|
|
if (!toIndex->Write(Independent, toFileName->Number(), FileSize)) {
|
2002-06-22 10:11:59 +02:00
|
|
|
error = "toIndex";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
FileSize += Length;
|
|
|
|
if (!LastIFrame)
|
|
|
|
LastIFrame = toIndex->Last();
|
|
|
|
|
|
|
|
// Check editing marks:
|
|
|
|
|
2011-08-21 11:34:30 +02:00
|
|
|
if (Mark && Index >= Mark->Position()) {
|
2002-06-22 10:11:59 +02:00
|
|
|
Mark = fromMarks.Next(Mark);
|
|
|
|
toMarks.Add(LastIFrame);
|
|
|
|
if (Mark)
|
|
|
|
toMarks.Add(toIndex->Last() + 1);
|
|
|
|
toMarks.Save();
|
|
|
|
if (Mark) {
|
2012-06-10 14:33:36 +02:00
|
|
|
// Read the next frame, for later comparison with the first frame at this mark:
|
|
|
|
if (fromIndex->Get(Index, &FileNumber, &FileOffset, &Independent, &Length2)) {
|
|
|
|
if (FileNumber != CurrentFileNumber)
|
|
|
|
fromFile = fromFileName->SetOffset(FileNumber, FileOffset);
|
|
|
|
if (fromFile) {
|
|
|
|
int len = ReadFrame(fromFile, buffer2, Length2, sizeof(buffer2));
|
|
|
|
if (len >= 0 && len == Length2)
|
|
|
|
CheckForSeamlessStream = true;
|
|
|
|
}
|
|
|
|
}
|
2011-08-21 11:34:30 +02:00
|
|
|
Index = Mark->Position();
|
2002-06-22 10:11:59 +02:00
|
|
|
Mark = fromMarks.Next(Mark);
|
|
|
|
CurrentFileNumber = 0; // triggers SetOffset before reading next frame
|
2003-04-26 15:11:17 +02:00
|
|
|
cutIn = true;
|
2002-06-22 10:11:59 +02:00
|
|
|
if (Setup.SplitEditedFiles) {
|
|
|
|
toFile = toFileName->NextFile();
|
2006-01-27 13:47:08 +01:00
|
|
|
if (!toFile) {
|
2002-06-22 10:11:59 +02:00
|
|
|
error = "toFile 2";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
FileSize = 0;
|
|
|
|
}
|
|
|
|
}
|
2003-05-24 12:01:52 +02:00
|
|
|
else
|
|
|
|
LastMark = true;
|
2002-06-22 10:11:59 +02:00
|
|
|
}
|
|
|
|
}
|
2006-07-30 10:29:24 +02:00
|
|
|
Recordings.TouchUpdate();
|
2002-06-22 10:11:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog("no editing marks found!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- cCutter ---------------------------------------------------------------
|
|
|
|
|
2010-08-29 13:40:37 +02:00
|
|
|
cMutex cCutter::mutex;
|
2012-02-16 12:20:46 +01:00
|
|
|
cString cCutter::originalVersionName;
|
|
|
|
cString cCutter::editedVersionName;
|
2002-06-22 10:11:59 +02:00
|
|
|
cCuttingThread *cCutter::cuttingThread = NULL;
|
|
|
|
bool cCutter::error = false;
|
|
|
|
bool cCutter::ended = false;
|
|
|
|
|
|
|
|
bool cCutter::Start(const char *FileName)
|
|
|
|
{
|
2010-08-29 13:40:37 +02:00
|
|
|
cMutexLock MutexLock(&mutex);
|
2002-06-22 10:11:59 +02:00
|
|
|
if (!cuttingThread) {
|
|
|
|
error = false;
|
|
|
|
ended = false;
|
2012-02-16 12:20:46 +01:00
|
|
|
originalVersionName = FileName;
|
2002-06-22 10:11:59 +02:00
|
|
|
cRecording Recording(FileName);
|
2011-08-20 10:09:05 +02:00
|
|
|
|
|
|
|
cMarks FromMarks;
|
2011-10-09 16:12:21 +02:00
|
|
|
FromMarks.Load(FileName, Recording.FramesPerSecond(), Recording.IsPesRecording());
|
2011-08-20 10:09:05 +02:00
|
|
|
if (cMark *First = FromMarks.First())
|
2011-08-21 11:34:30 +02:00
|
|
|
Recording.SetStartTime(Recording.Start() + (int(First->Position() / Recording.FramesPerSecond() + 30) / 60) * 60);
|
2011-08-20 10:09:05 +02:00
|
|
|
|
2002-06-22 10:11:59 +02:00
|
|
|
const char *evn = Recording.PrefixFileName('%');
|
|
|
|
if (evn && RemoveVideoFile(evn) && MakeDirs(evn, true)) {
|
|
|
|
// XXX this can be removed once RenameVideoFile() follows symlinks (see videodir.c)
|
|
|
|
// remove a possible deleted recording with the same name to avoid symlink mixups:
|
|
|
|
char *s = strdup(evn);
|
|
|
|
char *e = strrchr(s, '.');
|
|
|
|
if (e) {
|
|
|
|
if (strcmp(e, ".rec") == 0) {
|
|
|
|
strcpy(e, ".del");
|
|
|
|
RemoveVideoFile(s);
|
|
|
|
}
|
|
|
|
}
|
2002-08-11 13:32:23 +02:00
|
|
|
free(s);
|
2002-06-22 10:11:59 +02:00
|
|
|
// XXX
|
2012-02-16 12:20:46 +01:00
|
|
|
editedVersionName = evn;
|
2005-05-16 14:45:11 +02:00
|
|
|
Recording.WriteInfo();
|
2006-07-30 10:29:24 +02:00
|
|
|
Recordings.AddByName(editedVersionName, false);
|
2002-06-22 10:11:59 +02:00
|
|
|
cuttingThread = new cCuttingThread(FileName, editedVersionName);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCutter::Stop(void)
|
|
|
|
{
|
2010-08-29 13:40:37 +02:00
|
|
|
cMutexLock MutexLock(&mutex);
|
2005-08-14 11:24:57 +02:00
|
|
|
bool Interrupted = cuttingThread && cuttingThread->Active();
|
2002-06-22 10:11:59 +02:00
|
|
|
const char *Error = cuttingThread ? cuttingThread->Error() : NULL;
|
|
|
|
delete cuttingThread;
|
|
|
|
cuttingThread = NULL;
|
2012-02-16 12:20:46 +01:00
|
|
|
if ((Interrupted || Error) && *editedVersionName) {
|
2002-06-22 10:11:59 +02:00
|
|
|
if (Interrupted)
|
|
|
|
isyslog("editing process has been interrupted");
|
|
|
|
if (Error)
|
|
|
|
esyslog("ERROR: '%s' during editing process", Error);
|
2012-02-16 12:20:46 +01:00
|
|
|
if (cReplayControl::NowReplaying() && strcmp(cReplayControl::NowReplaying(), editedVersionName) == 0)
|
|
|
|
cControl::Shutdown();
|
|
|
|
RemoveVideoFile(editedVersionName);
|
2004-06-13 20:26:51 +02:00
|
|
|
Recordings.DelByName(editedVersionName);
|
2002-06-22 10:11:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-16 12:20:46 +01:00
|
|
|
bool cCutter::Active(const char *FileName)
|
2002-06-22 10:11:59 +02:00
|
|
|
{
|
2010-08-29 13:40:37 +02:00
|
|
|
cMutexLock MutexLock(&mutex);
|
2002-06-22 10:11:59 +02:00
|
|
|
if (cuttingThread) {
|
2005-08-14 11:24:57 +02:00
|
|
|
if (cuttingThread->Active())
|
2012-02-16 12:20:46 +01:00
|
|
|
return !FileName || strcmp(FileName, originalVersionName) == 0 || strcmp(FileName, editedVersionName) == 0;
|
2002-06-22 10:11:59 +02:00
|
|
|
error = cuttingThread->Error();
|
|
|
|
Stop();
|
|
|
|
if (!error)
|
2012-06-02 13:57:41 +02:00
|
|
|
cRecordingUserCommand::InvokeCommand(RUC_EDITEDRECORDING, editedVersionName, originalVersionName);
|
2012-02-16 12:20:46 +01:00
|
|
|
originalVersionName = NULL;
|
2002-06-22 10:11:59 +02:00
|
|
|
editedVersionName = NULL;
|
|
|
|
ended = true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cCutter::Error(void)
|
|
|
|
{
|
2010-08-29 13:40:37 +02:00
|
|
|
cMutexLock MutexLock(&mutex);
|
2002-06-22 10:11:59 +02:00
|
|
|
bool result = error;
|
|
|
|
error = false;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cCutter::Ended(void)
|
|
|
|
{
|
2010-08-29 13:40:37 +02:00
|
|
|
cMutexLock MutexLock(&mutex);
|
2002-06-22 10:11:59 +02:00
|
|
|
bool result = ended;
|
|
|
|
ended = false;
|
|
|
|
return result;
|
|
|
|
}
|
2010-01-02 14:02:48 +01:00
|
|
|
|
|
|
|
#define CUTTINGCHECKINTERVAL 500 // ms between checks for the active cutting process
|
|
|
|
|
|
|
|
bool CutRecording(const char *FileName)
|
|
|
|
{
|
|
|
|
if (DirectoryOk(FileName)) {
|
|
|
|
cRecording Recording(FileName);
|
|
|
|
if (Recording.Name()) {
|
|
|
|
cMarks Marks;
|
|
|
|
if (Marks.Load(FileName, Recording.FramesPerSecond(), Recording.IsPesRecording()) && Marks.Count()) {
|
|
|
|
if (cCutter::Start(FileName)) {
|
|
|
|
while (cCutter::Active())
|
|
|
|
cCondWait::SleepMs(CUTTINGCHECKINTERVAL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fprintf(stderr, "can't start editing process\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fprintf(stderr, "'%s' has no editing marks\n", FileName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fprintf(stderr, "'%s' is not a recording\n", FileName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fprintf(stderr, "'%s' is not a directory\n", FileName);
|
|
|
|
return false;
|
|
|
|
}
|