vdr  2.2.0
recording.h
Go to the documentation of this file.
1 /*
2  * recording.h: Recording file handling
3  *
4  * See the main source file 'vdr.c' for copyright information and
5  * how to reach the author.
6  *
7  * $Id: recording.h 3.8 2015/02/07 14:29:14 kls Exp $
8  */
9 
10 #ifndef __RECORDING_H
11 #define __RECORDING_H
12 
13 #include <time.h>
14 #include "channels.h"
15 #include "config.h"
16 #include "epg.h"
17 #include "thread.h"
18 #include "timers.h"
19 #include "tools.h"
20 
21 #define FOLDERDELIMCHAR '~'
22 
23 extern int DirectoryPathMax;
24 extern int DirectoryNameMax;
25 extern bool DirectoryEncoding;
26 extern int InstanceId;
27 
29  ruNone = 0x0000, // the recording is currently unused
30  ruTimer = 0x0001, // the recording is currently written to by a timer
31  ruReplay = 0x0002, // the recording is being replayed
32  // mutually exclusive:
33  ruCut = 0x0004, // the recording is being cut
34  ruMove = 0x0008, // the recording is being moved
35  ruCopy = 0x0010, // the recording is being copied
36  // mutually exclusive:
37  ruSrc = 0x0020, // the recording is the source of a cut, move or copy process
38  ruDst = 0x0040, // the recording is the destination of a cut, move or copy process
39  //
40  ruPending = 0x0080, // the recording is pending a cut, move or copy process
41  };
42 
43 void RemoveDeletedRecordings(void);
44 void ClearVanishedRecordings(void);
45 void AssertFreeDiskSpace(int Priority = 0, bool Force = false);
50 
51 class cResumeFile {
52 private:
53  char *fileName;
55 public:
56  cResumeFile(const char *FileName, bool IsPesRecording);
57  ~cResumeFile();
58  int Read(void);
59  bool Save(int Index);
60  void Delete(void);
61  };
62 
64  friend class cRecording;
65 private:
67  char *channelName;
68  const cEvent *event;
70  char *aux;
72  int priority;
73  int lifetime;
74  char *fileName;
75  cRecordingInfo(const cChannel *Channel = NULL, const cEvent *Event = NULL);
76  bool Read(FILE *f);
77  void SetData(const char *Title, const char *ShortText, const char *Description);
78  void SetAux(const char *Aux);
79 public:
80  cRecordingInfo(const char *FileName);
81  ~cRecordingInfo();
82  tChannelID ChannelID(void) const { return channelID; }
83  const char *ChannelName(void) const { return channelName; }
84  const cEvent *GetEvent(void) const { return event; }
85  const char *Title(void) const { return event->Title(); }
86  const char *ShortText(void) const { return event->ShortText(); }
87  const char *Description(void) const { return event->Description(); }
88  const cComponents *Components(void) const { return event->Components(); }
89  const char *Aux(void) const { return aux; }
90  double FramesPerSecond(void) const { return framesPerSecond; }
91  void SetFramesPerSecond(double FramesPerSecond);
92  void SetFileName(const char *FileName);
93  bool Write(FILE *f, const char *Prefix = "") const;
94  bool Read(void);
95  bool Write(void) const;
96  };
97 
98 class cRecording : public cListObject {
99  friend class cRecordings;
100 private:
101  mutable int resume;
102  mutable char *titleBuffer;
103  mutable char *sortBufferName;
104  mutable char *sortBufferTime;
105  mutable char *fileName;
106  mutable char *name;
107  mutable int fileSizeMB;
108  mutable int numFrames;
109  int channel;
112  mutable int isOnVideoDirectoryFileSystem; // -1 = unknown, 0 = no, 1 = yes
115  cRecording(const cRecording&); // can't copy cRecording
116  cRecording &operator=(const cRecording &); // can't assign cRecording
117  static char *StripEpisodeName(char *s, bool Strip);
118  char *SortName(void) const;
119  void ClearSortName(void);
120  time_t start;
121  int priority;
122  int lifetime;
123  time_t deleted;
124 public:
125  cRecording(cTimer *Timer, const cEvent *Event);
126  cRecording(const char *FileName);
127  virtual ~cRecording();
128  time_t Start(void) const { return start; }
129  int Priority(void) const { return priority; }
130  int Lifetime(void) const { return lifetime; }
131  time_t Deleted(void) const { return deleted; }
132  virtual int Compare(const cListObject &ListObject) const;
133  bool IsInPath(const char *Path);
136  cString Folder(void) const;
139  cString BaseName(void) const;
142  const char *Name(void) const { return name; }
145  const char *FileName(void) const;
148  const char *Title(char Delimiter = ' ', bool NewIndicator = false, int Level = -1) const;
149  const cRecordingInfo *Info(void) const { return info; }
150  const char *PrefixFileName(char Prefix);
151  int HierarchyLevels(void) const;
152  void ResetResume(void) const;
153  double FramesPerSecond(void) const { return framesPerSecond; }
154  int NumFrames(void) const;
157  int LengthInSeconds(void) const;
159  int FileSizeMB(void) const;
162  int GetResume(void) const;
165  bool IsNew(void) const { return GetResume() <= 0; }
166  bool IsEdited(void) const;
167  bool IsPesRecording(void) const { return isPesRecording; }
168  bool IsOnVideoDirectoryFileSystem(void) const;
169  bool HasMarks(void);
171  bool DeleteMarks(void);
175  void ReadInfo(void);
176  bool WriteInfo(const char *OtherFileName = NULL);
180  void SetStartTime(time_t Start);
188  bool ChangePriorityLifetime(int NewPriority, int NewLifetime);
192  bool ChangeName(const char *NewName);
199  bool Delete(void);
202  bool Remove(void);
205  bool Undelete(void);
209  int IsInUse(void) const;
217  };
218 
219 class cRecordings : public cList<cRecording>, public cThread {
220 private:
221  static char *updateFileName;
222  bool deleted;
223  bool initial;
224  time_t lastUpdate;
225  int state;
226  const char *UpdateFileName(void);
227  void Refresh(bool Foreground = false);
228  bool ScanVideoDir(const char *DirName, bool Foreground = false, int LinkLevel = 0, int DirLevel = 0);
229 protected:
230  void Action(void);
231 public:
232  cRecordings(bool Deleted = false);
233  virtual ~cRecordings();
234  bool Load(void) { return Update(true); }
238  bool Update(bool Wait = false);
244  void TouchUpdate(void);
248  bool NeedsUpdate(void);
249  void ChangeState(void) { state++; }
250  bool StateChanged(int &State);
251  void ResetResume(const char *ResumeFileName = NULL);
252  void ClearSortNames(void);
253  cRecording *GetByName(const char *FileName);
254  void AddByName(const char *FileName, bool TriggerUpdate = true);
255  void DelByName(const char *FileName);
256  void UpdateByName(const char *FileName);
257  int TotalFileSizeMB(void);
258  double MBperMinute(void);
261  int PathIsInUse(const char *Path);
269  int GetNumRecordingsInPath(const char *Path);
273  bool MoveRecordings(const char *OldPath, const char *NewPath);
282  };
283 
286 extern cRecordings Recordings;
288 
290 
292 private:
295  bool finished;
296  bool error;
297  cRecordingsHandlerEntry *Get(const char *FileName);
298 public:
299  cRecordingsHandler(void);
301  bool Add(int Usage, const char *FileNameSrc, const char *FileNameDst = NULL);
309  void Del(const char *FileName);
314  void DelAll(void);
316  int GetUsage(const char *FileName);
318  bool Active(void);
324  bool Finished(bool &Error);
329  };
330 
332 
333 #define DEFAULTFRAMESPERSECOND 25.0
334 
335 class cMark : public cListObject {
336  friend class cMarks; // for sorting
337 private:
339  int position;
341 public:
342  cMark(int Position = 0, const char *Comment = NULL, double FramesPerSecond = DEFAULTFRAMESPERSECOND);
343  virtual ~cMark();
344  int Position(void) const { return position; }
345  const char *Comment(void) const { return comment; }
346  void SetPosition(int Position) { position = Position; }
347  void SetComment(const char *Comment) { comment = Comment; }
348  cString ToText(void);
349  bool Parse(const char *s);
350  bool Save(FILE *f);
351  };
352 
353 class cMarks : public cConfig<cMark>, public cMutex {
354 private:
359  time_t nextUpdate;
360  time_t lastFileTime;
361  time_t lastChange;
362 public:
363  static cString MarksFileName(const cRecording *Recording);
366  bool Load(const char *RecordingFileName, double FramesPerSecond = DEFAULTFRAMESPERSECOND, bool IsPesRecording = false);
367  bool Update(void);
368  bool Save(void);
369  void Align(void);
370  void Sort(void);
371  void Add(int Position);
377  cMark *Get(int Position);
378  cMark *GetPrev(int Position);
379  cMark *GetNext(int Position);
380  cMark *GetNextBegin(cMark *EndMark = NULL);
384  cMark *GetNextEnd(cMark *BeginMark);
387  int GetNumSequences(void);
393  };
394 
395 #define RUC_BEFORERECORDING "before"
396 #define RUC_STARTRECORDING "started"
397 #define RUC_AFTERRECORDING "after"
398 #define RUC_EDITEDRECORDING "edited"
399 #define RUC_DELETERECORDING "deleted"
400 
402 private:
403  static const char *command;
404 public:
405  static void SetCommand(const char *Command) { command = Command; }
406  static void InvokeCommand(const char *State, const char *RecordingFileName, const char *SourceFileName = NULL);
407  };
408 
409 // The maximum size of a single frame (up to HDTV 1920x1080):
410 #define MAXFRAMESIZE (KILOBYTE(1024) / TS_SIZE * TS_SIZE) // multiple of TS_SIZE to avoid breaking up TS packets
411 
412 // The maximum file size is limited by the range that can be covered
413 // with a 40 bit 'unsigned int', which is 1TB. The actual maximum value
414 // used is 6MB below the theoretical maximum, to have some safety (the
415 // actual file size may be slightly higher because we stop recording only
416 // before the next independent frame, to have a complete Group Of Pictures):
417 #define MAXVIDEOFILESIZETS 1048570 // MB
418 #define MAXVIDEOFILESIZEPES 2000 // MB
419 #define MINVIDEOFILESIZE 100 // MB
420 #define MAXVIDEOFILESIZEDEFAULT MAXVIDEOFILESIZEPES
421 
422 struct tIndexTs;
423 class cIndexFileGenerator;
424 
425 class cIndexFile {
426 private:
427  int f;
429  int size, last;
435  void ConvertFromPes(tIndexTs *IndexTs, int Count);
436  void ConvertToPes(tIndexTs *IndexTs, int Count);
437  bool CatchUp(int Index = -1);
438 public:
439  cIndexFile(const char *FileName, bool Record, bool IsPesRecording = false, bool PauseLive = false, bool Update = false);
440  ~cIndexFile();
441  bool Ok(void) { return index != NULL; }
442  bool Write(bool Independent, uint16_t FileNumber, off_t FileOffset);
443  bool Get(int Index, uint16_t *FileNumber, off_t *FileOffset, bool *Independent = NULL, int *Length = NULL);
444  int GetNextIFrame(int Index, bool Forward, uint16_t *FileNumber = NULL, off_t *FileOffset = NULL, int *Length = NULL);
445  int GetClosestIFrame(int Index);
450  int Get(uint16_t FileNumber, off_t FileOffset);
451  int Last(void) { CatchUp(); return last; }
453  int GetResume(void) { return resumeFile.Read(); }
454  bool StoreResume(int Index) { return resumeFile.Save(Index); }
455  bool IsStillRecording(void);
456  void Delete(void);
457  static int GetLength(const char *FileName, bool IsPesRecording = false);
460  static cString IndexFileName(const char *FileName, bool IsPesRecording);
461  };
462 
463 class cFileName {
464 private:
466  uint16_t fileNumber;
468  bool record;
469  bool blocking;
471 public:
472  cFileName(const char *FileName, bool Record, bool Blocking = false, bool IsPesRecording = false);
473  ~cFileName();
474  const char *Name(void) { return fileName; }
475  uint16_t Number(void) { return fileNumber; }
476  bool GetLastPatPmtVersions(int &PatVersion, int &PmtVersion);
477  cUnbufferedFile *Open(void);
478  void Close(void);
479  cUnbufferedFile *SetOffset(int Number, off_t Offset = 0); // yes, Number is int for easier internal calculating
480  cUnbufferedFile *NextFile(void);
481  };
482 
483 cString IndexToHMSF(int Index, bool WithFrame = false, double FramesPerSecond = DEFAULTFRAMESPERSECOND);
484  // Converts the given index to a string, optionally containing the frame number.
485 int HMSFToIndex(const char *HMSF, double FramesPerSecond = DEFAULTFRAMESPERSECOND);
486  // Converts the given string (format: "hh:mm:ss.ff") to an index.
487 int SecondsToFrames(int Seconds, double FramesPerSecond = DEFAULTFRAMESPERSECOND);
488  // Returns the number of frames corresponding to the given number of seconds.
489 
490 int ReadFrame(cUnbufferedFile *f, uchar *b, int Length, int Max);
491 
492 char *ExchangeChars(char *s, bool ToFileSystem);
493  // Exchanges the characters in the given string to or from a file system
494  // specific representation (depending on ToFileSystem). The given string will
495  // be modified and may be reallocated if more space is needed. The return
496  // value points to the resulting string, which may be different from s.
497 
498 bool GenerateIndex(const char *FileName, bool Update = false);
503 
506 bool HasRecordingsSortMode(const char *Directory);
507 void GetRecordingsSortMode(const char *Directory);
508 void SetRecordingsSortMode(const char *Directory, eRecordingsSortMode SortMode);
509 void IncRecordingsSortMode(const char *Directory);
510 
511 #endif //__RECORDING_H
bool initial
Definition: recording.h:223
unsigned char uchar
Definition: tools.h:30
Definition: epg.h:71
int Priority(void) const
Definition: recording.h:129
int Position(void) const
Definition: recording.h:344
tIndexTs * index
Definition: recording.h:430
bool DirectoryEncoding
Definition: recording.c:76
#define DEFAULTFRAMESPERSECOND
Definition: recording.h:333
time_t Start(void) const
Definition: recording.h:128
cRecordingInfo * info
Definition: recording.h:114
cEvent * ownEvent
Definition: recording.h:69
char * fileName
Definition: recording.h:74
char * sortBufferName
Definition: recording.h:103
bool Load(void)
Loads the current list of recordings and returns true if there is anything in it (for compatibility w...
Definition: recording.h:234
const cRecordingInfo * Info(void) const
Definition: recording.h:149
void ResetResume(const char *ResumeFileName=NULL)
Definition: recording.c:1640
const cEvent * event
Definition: recording.h:68
cResumeFile(const char *FileName, bool IsPesRecording)
Definition: recording.c:240
void ChangeState(void)
Definition: recording.h:249
bool isPesRecording
Definition: recording.h:431
const char * ShortText(void) const
Definition: recording.h:86
int HMSFToIndex(const char *HMSF, double FramesPerSecond=DEFAULTFRAMESPERSECOND)
Definition: recording.c:2987
char * name
Definition: recording.h:106
double FramesPerSecond(void) const
Definition: recording.h:153
const char * Title(char Delimiter= ' ', bool NewIndicator=false, int Level=-1) const
Definition: recording.c:1060
time_t deleted
Definition: recording.h:123
cMutex mutex
Definition: recording.h:434
int fileSizeMB
Definition: recording.h:107
Definition: tools.h:489
bool Ok(void)
Definition: recording.h:441
tChannelID channelID
Definition: recording.h:66
const cComponents * Components(void) const
Definition: recording.h:88
cString IndexToHMSF(int Index, bool WithFrame=false, double FramesPerSecond=DEFAULTFRAMESPERSECOND)
Definition: recording.c:2971
double FramesPerSecond(void) const
Definition: recording.h:90
bool IsNew(void) const
Definition: recording.h:165
int Last(void)
Returns the index of the last entry in this file, or -1 if the file is empty.
Definition: recording.h:451
time_t start
Definition: recording.h:120
void SetPosition(int Position)
Definition: recording.h:346
eRecordingsSortMode
Definition: recording.h:504
cUnbufferedFile is used for large files that are mainly written or read in a streaming manner...
Definition: tools.h:418
static void SetCommand(const char *Command)
Definition: recording.h:405
uint16_t Number(void)
Definition: recording.h:475
static const char * command
Definition: recording.h:403
bool GenerateIndex(const char *FileName, bool Update=false)
Generates the index of the existing recording with the given FileName.
Definition: recording.c:2777
eRecordingUsage
Definition: recording.h:28
bool HasRecordingsSortMode(const char *Directory)
Definition: recording.c:3023
Definition: timers.h:27
void IncRecordingsSortMode(const char *Directory)
Definition: recording.c:3047
bool Save(int Index)
Definition: recording.c:303
eRecordingsSortMode RecordingsSortMode
Definition: recording.c:3021
bool isPesRecording
Definition: recording.h:54
int priority
Definition: recording.h:121
double framesPerSecond
Definition: recording.h:113
double framesPerSecond
Definition: recording.h:71
const char * Comment(void) const
Definition: recording.h:345
bool isPesRecording
Definition: recording.h:358
char * aux
Definition: recording.h:70
void RemoveDeletedRecordings(void)
Definition: recording.c:132
time_t lastChange
Definition: recording.h:361
bool deleted
Definition: recording.h:222
char * ExchangeChars(char *s, bool ToFileSystem)
Definition: recording.c:582
char * sortBufferTime
Definition: recording.h:104
bool record
Definition: recording.h:468
bool isPesRecording
Definition: recording.h:111
time_t Deleted(void) const
Definition: recording.h:131
bool blocking
Definition: recording.h:469
int instanceId
Definition: recording.h:110
char * channelName
Definition: recording.h:67
int position
Definition: recording.h:339
void bool Start(void)
Sets the description of this thread, which will be used when logging starting or stopping of the thre...
Definition: thread.c:273
int lifetime
Definition: recording.h:122
void AssertFreeDiskSpace(int Priority=0, bool Force=false)
The special Priority value -1 means that we shall get rid of any deleted recordings faster than norma...
Definition: recording.c:149
int InstanceId
Definition: recording.c:77
tChannelID ChannelID(void) const
Definition: recording.h:82
int Lifetime(void) const
Definition: recording.h:130
int isOnVideoDirectoryFileSystem
Definition: recording.h:112
char * pFileNumber
Definition: recording.h:467
static char * updateFileName
Definition: recording.h:221
Definition: thread.h:63
int SecondsToFrames(int Seconds, double FramesPerSecond=DEFAULTFRAMESPERSECOND)
Definition: recording.c:2998
const char * Name(void)
Definition: recording.h:474
cString fileName
Definition: recording.h:356
cRecordings DeletedRecordings
bool StoreResume(int Index)
Definition: recording.h:454
cIndexFileGenerator * indexFileGenerator
Definition: recording.h:433
cString comment
Definition: recording.h:340
void GetRecordingsSortMode(const char *Directory)
Definition: recording.c:3028
int ReadFrame(cUnbufferedFile *f, uchar *b, int Length, int Max)
Definition: recording.c:3005
char * titleBuffer
Definition: recording.h:102
void SetRecordingsSortMode(const char *Directory, eRecordingsSortMode SortMode)
Definition: recording.c:3039
int channel
Definition: recording.h:109
cRecordingsHandler RecordingsHandler
Definition: recording.c:1910
int GetResume(void)
Definition: recording.h:453
cString fileName
Definition: recording.h:428
time_t lastFileTime
Definition: recording.h:360
void Delete(void)
Definition: recording.c:331
const cEvent * GetEvent(void) const
Definition: recording.h:84
const char * Description(void) const
Definition: recording.h:87
const char * Name(void) const
Returns the full name of the recording (without the video directory.
Definition: recording.h:142
double framesPerSecond
Definition: recording.h:357
void ClearVanishedRecordings(void)
Definition: recording.c:232
cResumeFile resumeFile
Definition: recording.h:432
Definition: thread.h:77
char * fileName
Definition: recording.h:53
const char * Title(void) const
Definition: recording.h:85
cList< cRecordingsHandlerEntry > operations
Definition: recording.h:294
int Read(void)
Definition: recording.c:258
int resume
Definition: recording.h:101
time_t nextUpdate
Definition: recording.h:359
const char * ChannelName(void) const
Definition: recording.h:83
void SetComment(const char *Comment)
Definition: recording.h:347
const char * Aux(void) const
Definition: recording.h:89
static const uint8_t * GetLength(const uint8_t *Data, int &Length)
Definition: ci.c:40
bool isPesRecording
Definition: recording.h:470
uint16_t fileNumber
Definition: recording.h:466
cString recordingFileName
Definition: recording.h:355
char * fileName
Definition: recording.h:105
cRecordings Recordings
Any access to Recordings that loops through the list of recordings needs to hold a thread lock on thi...
Definition: recording.c:1365
int DirectoryPathMax
Definition: recording.c:74
const char * FileName(void) const
Returns the full path name to the recording directory, including the video directory and the actual &#39;...
Definition: recording.c:1042
time_t lastUpdate
Definition: recording.h:224
cUnbufferedFile * file
Definition: recording.h:465
int numFrames
Definition: recording.h:108
bool IsPesRecording(void) const
Definition: recording.h:167
int DirectoryNameMax
Definition: recording.c:75
double framesPerSecond
Definition: recording.h:338
Definition: tools.h:168