2002-06-16 12:57:31 +02:00
|
|
|
/*
|
2002-12-22 11:33:08 +01:00
|
|
|
* recorder.c: The actual DVB recorder
|
2002-06-16 12:57:31 +02:00
|
|
|
*
|
|
|
|
* See the main source file 'vdr.c' for copyright information and
|
|
|
|
* how to reach the author.
|
|
|
|
*
|
2009-01-23 16:44:46 +01:00
|
|
|
* $Id: recorder.c 2.2 2009/01/23 16:44:29 kls Exp $
|
2002-06-16 12:57:31 +02:00
|
|
|
*/
|
|
|
|
|
2006-01-08 11:03:44 +01:00
|
|
|
#include "recorder.h"
|
2007-02-25 10:56:29 +01:00
|
|
|
#include "shutdown.h"
|
2002-06-16 12:57:31 +02:00
|
|
|
|
2004-10-16 09:36:28 +02:00
|
|
|
#define RECORDERBUFSIZE MEGABYTE(5)
|
2002-06-16 12:57:31 +02:00
|
|
|
|
2003-05-16 13:36:06 +02:00
|
|
|
// The maximum time we wait before assuming that a recorded video data stream
|
|
|
|
// is broken:
|
|
|
|
#define MAXBROKENTIMEOUT 30 // seconds
|
|
|
|
|
2002-06-16 12:57:31 +02:00
|
|
|
#define MINFREEDISKSPACE (512) // MB
|
|
|
|
#define DISKCHECKINTERVAL 100 // seconds
|
|
|
|
|
2009-01-06 14:41:11 +01:00
|
|
|
// --- cRecorder -------------------------------------------------------------
|
2004-10-16 09:36:28 +02:00
|
|
|
|
2009-01-06 14:41:11 +01:00
|
|
|
cRecorder::cRecorder(const char *FileName, tChannelID ChannelID, int Priority, int VPid, const int *APids, const int *DPids, const int *SPids)
|
|
|
|
:cReceiver(ChannelID, Priority, VPid, APids, Setup.UseDolbyDigital ? DPids : NULL, SPids)
|
|
|
|
,cThread("recording")
|
|
|
|
,recordingInfo(FileName)
|
2002-06-16 12:57:31 +02:00
|
|
|
{
|
2009-01-06 14:41:11 +01:00
|
|
|
// Make sure the disk is up and running:
|
|
|
|
|
|
|
|
SpinUpDisk(FileName);
|
|
|
|
|
|
|
|
ringBuffer = new cRingBufferLinear(RECORDERBUFSIZE, TS_SIZE * 2, true, "Recorder");
|
|
|
|
ringBuffer->SetTimeouts(0, 100);
|
|
|
|
cChannel *Channel = Channels.GetByChannelID(ChannelID);
|
|
|
|
int Pid = VPid;
|
|
|
|
int Type = Channel ? Channel->Vtype() : 0;
|
|
|
|
if (!Pid && APids) {
|
|
|
|
Pid = APids[0];
|
|
|
|
Type = 0x04;
|
|
|
|
}
|
|
|
|
if (!Pid && DPids) {
|
|
|
|
Pid = DPids[0];
|
|
|
|
Type = 0x06;
|
|
|
|
}
|
|
|
|
frameDetector = new cFrameDetector(Pid, Type);
|
2009-01-23 16:44:46 +01:00
|
|
|
patPmtGenerator.SetChannel(Channel);
|
2002-06-16 12:57:31 +02:00
|
|
|
fileName = NULL;
|
|
|
|
index = NULL;
|
|
|
|
fileSize = 0;
|
|
|
|
lastDiskSpaceCheck = time(NULL);
|
|
|
|
fileName = new cFileName(FileName, true);
|
|
|
|
recordFile = fileName->Open();
|
2005-10-31 13:14:26 +01:00
|
|
|
if (!recordFile)
|
2002-06-16 12:57:31 +02:00
|
|
|
return;
|
|
|
|
// Create the index file:
|
|
|
|
index = new cIndexFile(FileName, true);
|
|
|
|
if (!index)
|
|
|
|
esyslog("ERROR: can't allocate index");
|
|
|
|
// let's continue without index, so we'll at least have the recording
|
|
|
|
}
|
|
|
|
|
2009-01-06 14:41:11 +01:00
|
|
|
cRecorder::~cRecorder()
|
2002-06-16 12:57:31 +02:00
|
|
|
{
|
2009-01-06 14:41:11 +01:00
|
|
|
Detach();
|
2002-06-16 12:57:31 +02:00
|
|
|
delete index;
|
|
|
|
delete fileName;
|
2009-01-06 14:41:11 +01:00
|
|
|
delete frameDetector;
|
|
|
|
delete ringBuffer;
|
2002-06-16 12:57:31 +02:00
|
|
|
}
|
|
|
|
|
2009-01-06 14:41:11 +01:00
|
|
|
bool cRecorder::RunningLowOnDiskSpace(void)
|
2002-06-16 12:57:31 +02:00
|
|
|
{
|
|
|
|
if (time(NULL) > lastDiskSpaceCheck + DISKCHECKINTERVAL) {
|
|
|
|
int Free = FreeDiskSpaceMB(fileName->Name());
|
|
|
|
lastDiskSpaceCheck = time(NULL);
|
|
|
|
if (Free < MINFREEDISKSPACE) {
|
|
|
|
dsyslog("low disk space (%d MB, limit is %d MB)", Free, MINFREEDISKSPACE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-01-06 14:41:11 +01:00
|
|
|
bool cRecorder::NextFile(void)
|
2002-06-16 12:57:31 +02:00
|
|
|
{
|
2009-01-06 14:41:11 +01:00
|
|
|
if (recordFile && frameDetector->IndependentFrame()) { // every file shall start with an independent frame
|
|
|
|
if (fileSize > MEGABYTE(off_t(Setup.MaxVideoFileSize)) || RunningLowOnDiskSpace()) {
|
2002-06-16 12:57:31 +02:00
|
|
|
recordFile = fileName->NextFile();
|
|
|
|
fileSize = 0;
|
|
|
|
}
|
|
|
|
}
|
2005-10-31 13:14:26 +01:00
|
|
|
return recordFile != NULL;
|
2002-06-16 12:57:31 +02:00
|
|
|
}
|
|
|
|
|
2004-10-16 09:36:28 +02:00
|
|
|
void cRecorder::Activate(bool On)
|
|
|
|
{
|
2009-01-06 14:41:11 +01:00
|
|
|
if (On)
|
2004-10-16 09:36:28 +02:00
|
|
|
Start();
|
2005-08-13 13:17:24 +02:00
|
|
|
else
|
2004-10-16 09:36:28 +02:00
|
|
|
Cancel(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cRecorder::Receive(uchar *Data, int Length)
|
|
|
|
{
|
2005-08-14 11:24:57 +02:00
|
|
|
if (Running()) {
|
2004-10-16 09:36:28 +02:00
|
|
|
int p = ringBuffer->Put(Data, Length);
|
2005-08-14 11:24:57 +02:00
|
|
|
if (p != Length && Running())
|
2004-10-16 09:36:28 +02:00
|
|
|
ringBuffer->ReportOverflow(Length - p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cRecorder::Action(void)
|
|
|
|
{
|
2009-01-06 14:41:11 +01:00
|
|
|
time_t t = time(NULL);
|
|
|
|
bool Synced = false;
|
|
|
|
bool InfoWritten = false;
|
2005-08-14 11:24:57 +02:00
|
|
|
while (Running()) {
|
2004-10-16 09:36:28 +02:00
|
|
|
int r;
|
|
|
|
uchar *b = ringBuffer->Get(r);
|
|
|
|
if (b) {
|
2009-01-06 14:41:11 +01:00
|
|
|
int Count = frameDetector->Analyze(b, r);
|
|
|
|
if (Count) {
|
|
|
|
if (!Running() && frameDetector->IndependentFrame()) // finish the recording before the next independent frame
|
|
|
|
break;
|
|
|
|
if (Synced |= frameDetector->IndependentFrame()) { // start with first independent frame
|
|
|
|
if (!InfoWritten) {
|
|
|
|
if (recordingInfo.Read()) {
|
|
|
|
if (frameDetector->FramesPerSecond() > 0 && recordingInfo.FramesPerSecond() != frameDetector->FramesPerSecond()) {
|
|
|
|
recordingInfo.SetFramesPerSecond(frameDetector->FramesPerSecond());
|
|
|
|
recordingInfo.Write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
InfoWritten = true;
|
|
|
|
}
|
|
|
|
if (!NextFile())
|
|
|
|
break;
|
|
|
|
if (index && frameDetector->NewFrame())
|
|
|
|
index->Write(frameDetector->IndependentFrame(), fileName->Number(), fileSize);
|
|
|
|
if (frameDetector->IndependentFrame()) {
|
|
|
|
recordFile->Write(patPmtGenerator.GetPat(), TS_SIZE);
|
|
|
|
fileSize += TS_SIZE;
|
|
|
|
int Index = 0;
|
|
|
|
while (uchar *pmt = patPmtGenerator.GetPmt(Index)) {
|
|
|
|
recordFile->Write(pmt, TS_SIZE);
|
|
|
|
fileSize += TS_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (recordFile->Write(b, Count) < 0) {
|
|
|
|
LOG_ERROR_STR(fileName->Name());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fileSize += Count;
|
|
|
|
t = time(NULL);
|
|
|
|
}
|
2004-10-16 09:36:28 +02:00
|
|
|
ringBuffer->Del(Count);
|
2009-01-06 14:41:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (time(NULL) - t > MAXBROKENTIMEOUT) {
|
|
|
|
esyslog("ERROR: video data stream broken");
|
|
|
|
ShutdownHandler.RequestEmergencyExit();
|
|
|
|
t = time(NULL);
|
2004-10-16 09:36:28 +02:00
|
|
|
}
|
2002-06-16 12:57:31 +02:00
|
|
|
}
|
|
|
|
}
|