summaryrefslogtreecommitdiff
path: root/status.c
blob: 15fa8f3e138740abf5d03e8863201a3f274861ae (plain)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * status.c: EPG2VDR plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include <vdr/tools.h>

#include "update.h"
#include "ttools.h"

#if defined (APIVERSNUM) && APIVERSNUM < 20304
#  error "VDR-2.3.4 API version or greater is required!"
#endif

//***************************************************************************
// ----> Copied from epgsearch ;-)
//***************************************************************************

#define LOC_INDEXFILESUFFIX "/index"

bool IsPesRecording(const cRecording* pRecording)
{
#if VDRVERSNUM < 10703
  return true;
#else
  return pRecording && pRecording->IsPesRecording();
#endif
}

#if VDRVERSNUM < 10703

int RecLengthInSecs(const cRecording* pRecording)
{
   struct stat buf;
   cString fullname = cString::sprintf("%s%s", pRecording->FileName(), "/index.vdr");

   if (stat(fullname, &buf) == 0)
   {
      struct tIndex { int offset; uchar type; uchar number; short reserved; };
      int delta = buf.st_size % sizeof(tIndex);

      if (delta)
      {
         delta = sizeof(tIndex) - delta;
         tell(0, "ERROR: invalid file size (%ld) in '%s'", buf.st_size, *fullname);
      }

      return (buf.st_size + delta) / sizeof(tIndex) / SecondsToFrames(1);
   }

   return -1;
}

#else

struct tIndexTs
{
   uint64_t offset:40; // up to 1TB per file (not using off_t here - must definitely be exactly 64 bit!)
   int reserved:7;     // reserved for future use
   int independent:1;  // marks frames that can be displayed by themselves (for trick modes)
   uint16_t number:16; // up to 64K files per recording

   tIndexTs(off_t Offset, bool Independent, uint16_t Number)
   {
      offset = Offset;
      reserved = 0;
      independent = Independent;
      number = Number;
   }
};

int RecLengthInSecs(const cRecording* pRecording)
{
  struct stat buf;
  cString fullname = cString::sprintf("%s%s", pRecording->FileName(), IsPesRecording(pRecording) ? LOC_INDEXFILESUFFIX ".vdr" : LOC_INDEXFILESUFFIX);

  if (pRecording->FileName() && *fullname && access(fullname, R_OK) == 0 && stat(fullname, &buf) == 0)
  {
     double frames = buf.st_size ? (buf.st_size - 1) / sizeof(tIndexTs) + 1 : 0;
     double Seconds = 0;
     modf((frames + 0.5) / pRecording->FramesPerSecond(), &Seconds);
     return Seconds;
  }

  return -1;
}

#endif

//***************************************************************************
//  Copied from epgsearch ;-)  <----------
//***************************************************************************

//***************************************************************************
// Notifications from VDRs Status Interface
//***************************************************************************
//***************************************************************************
// Timers Change Notification
//***************************************************************************

void cUpdate::TimerChange(const cTimer* Timer, eTimerChange Change)
{
   if (!Epg2VdrConfig.shareInWeb)
      return;

   tell(1, "Timer changed, trigger update. Action was (%d)", Change);

   timerTableUpdateTriggered = yes;
   waitCondition.Broadcast();         // wakeup
}

//***************************************************************************
// Recording Notification (cStatus::MsgRecording(....))
//***************************************************************************

void cUpdate::Recording(const cDevice* Device, const char* Name, const char* FileName, bool On)
{
   RecordingAction action;

   // Recording of 'Peter Hase' has 'started' [/srv/vdr/video.00/Peter_Hase/2014-10-08.11.05.18-0.rec]
   // Recording of '(null)' has 'stopped' [/srv/vdr/video.00/Peter_Hase/2014-10-08.11.05.18-0.rec]

   tell(1, "Recording of '%s' has '%s' [%s]", Name, On ? "started" : "stopped", FileName);

   // schedule this notification to perfrom it in oure context not in the cStatus Interface context
   //  due to the needed list locks!

   action.name = notNull(Name, "");
   action.fileName = FileName;
   action.cardIndex = Device->CardIndex();
   action.on = On;
   pendingRecordingActions.push(action);

   if (On)
      pendingNewRecordings.push(FileName);

   recordingStateChangedTrigger = yes;
   waitCondition.Broadcast();            // wakeup
}

//***************************************************************************
// Perform Pending Recording Notification
//  (got by cStatus::MsgRecording(....) above)
//***************************************************************************

int cUpdate::performRecordingActions()
{
   const int allowedBreakDuration = 2;

   GET_TIMERS_READ(timers);           // get timers lock
   GET_RECORDINGS_READ(recordings);   // recordings lock

   while (!pendingRecordingActions.empty())
   {
      cMutexLock lock(&runningRecMutex);

      RecordingAction action = pendingRecordingActions.front();
      pendingRecordingActions.pop();

      if (action.on && action.name.length())    // recording started ...
      {
         for (const cTimer* ti = timers->First(); ti; ti = timers->Next(ti))
         {
            if (!ti->Local())
               continue;

            if (ti->Recording())                     // timer nimmt gerade auf
            {
               cRunningRecording* recording = 0;

               // check if already known

               for (cRunningRecording* rr = runningRecordings.First(); rr;  rr = runningRecordings.Next(rr))
               {
                  if (ti->Id() == rr->id)
                  {
                     if (!rr->remote && !ti->Remote() || rr->remote && ti->Remote() && strcmp(rr->remote, ti->Remote()) == 0)
                     {
                        recording = rr;
                        break;
                     }
                  }
               }

               if (recording) // already handled -> a resume?!
               {
                  tell(1, "Info: Detected resume of '%s' on device %d", action.name.c_str(), action.cardIndex);
                  continue;
               }

               int doneid = na;
               contentOfTag(ti, "doneid", doneid);

               recording = new cRunningRecording(ti, doneid);
               runningRecordings.Add(recording);
               tell(1, "Info: Recording '%s' with doneid %d added to running list", action.name.c_str(), doneid);
            }
         }
      }

      // recording stopped ...

      if (!action.on)
      {
         // loop over running recordings ..

         for (cRunningRecording* rr = runningRecordings.First(); rr;  rr = runningRecordings.Next(rr))
         {
            const cRecording* pRecording = recordings->GetByName(action.fileName.c_str());
            const cTimer* pendingTimer = 0;
            int complete;
            double recFraction = 100.0;
            long timerLengthSecs = 0;
            bool vpsUsed = false;

            pendingTimer = Timers->GetById(rr->id, rr->remote);

            if (pendingTimer)
            {
               tell(2,"Info: valid timer for recording '%s' on Server '%s'", pendingTimer->File(), pendingTimer->Remote());
               timerLengthSecs = pendingTimer->StopTime() - pendingTimer->StartTime();

               if (pendingTimer->Event())
               {
                 tell(2,"Info: valid event for recording '%s'", pendingTimer->File());
                 tell(2,"Info: pendingTimer->Event()->Vps() (%ld)", pendingTimer->Event()->Vps());
                 vpsUsed = pendingTimer->HasFlags(tfVps) && pendingTimer->Event()->Vps();
               }
               else
               {
                  tell(2,"Info: no event for recording '%s'", pendingTimer->File());
               }

               // still recording :o ?

               if (pendingTimer && pendingTimer->Recording())
                  continue;

               if (pRecording && timerLengthSecs)
               {
                  int recLen = RecLengthInSecs(pRecording);
                  recFraction = double(recLen) * 100 / timerLengthSecs;
               }

               // assure timer has reached it's end or at least 90% (vps) / 98% were recorded

               complete = recFraction >= (vpsUsed ? 90 : 98);

               if (complete)
                  tell(1, "Info: Finished (%d) '%s'; recorded %d%%; VPS %s", getTimerIdOf(rr->aux),
                     pendingTimer->File(), (int)round(recFraction), vpsUsed ? "Yes": "No");
               else
                  tell(1, "Info: Finished (%d) '%s' (not complete! - recorded only %d%%); VPS %s", getTimerIdOf(rr->aux),
                       pendingTimer->File(), (int)round(recFraction), vpsUsed ? "Yes": "No");

               if (complete)
                  rr->lastBreak = 0;         // reset break
               else if (!rr->lastBreak)
                  rr->lastBreak = time(0);   // store first break

               if (!rr->lastBreak ||
                   time(0) - rr->lastBreak > allowedBreakDuration ||
                   time(0) >= pendingTimer->StopTime())
               {
                  char* infoTxt;

                  asprintf(&infoTxt, "Recording '%s' finished - %s complete (%d%%)",
                           pendingTimer->File(), complete ? "" : "NOT", (int)round(recFraction));

                  tell(1, "Info: %s", infoTxt);

                  rr->finished = yes;
                  rr->failed = !complete;
                  rr->setInfo(infoTxt);

                  free(infoTxt);
               }
            }
            else
            {
               tell(2,"Info: no timer for id '%d'", rr->id);
            }
         }
      }
   }

   return done;
}

//***************************************************************************
// Channel Switch Notification
//***************************************************************************

void cUpdate::ChannelSwitch(const cDevice* Device, int ChannelNumber, bool LiveView)
{
   // to be implemented ... ???
}