Ticket #1791: mythtv-0.19.jobhibernate.patch.txt

File mythtv-0.19.jobhibernate.patch.txt, 7.7 KB (added by mrsam@…, 18 years ago)

Patch to myth 0.19

Line 
1--- ./programs/mythcommflag/main.cpp.hibernate  2006-02-08 01:42:13.000000000 -0500
2+++ ./programs/mythcommflag/main.cpp    2006-05-08 19:48:00.000000000 -0400
3@@ -51,6 +51,13 @@
4 
5 int jobID = -1;
6 int lastCmd = -1;
7+JobMonitor *currentJobMonitor;
8+
9+static void rebuild_status_callback(int, void *)
10+{
11+    if (currentJobMonitor)
12+       currentJobMonitor->hibernate();
13+}
14 
15 int BuildVideoMarkup(QString& filename)
16 {
17@@ -81,7 +88,7 @@
18                                                    program_info);
19     nvp->SetRingBuffer(tmprbuf);
20 
21-    nvp->RebuildSeekTable(!quiet);
22+    nvp->RebuildSeekTable(!quiet, rebuild_status_callback);
23 
24     cerr << "Rebuilt\n";
25 
26@@ -250,7 +257,9 @@
27 
28     if (jobID != -1)
29     {
30-        int curCmd = JobQueue::GetJobCmd(jobID);
31+       int curCmd = currentJobMonitor ? currentJobMonitor->GetJobCmd()
32+           : JobQueue::GetJobCmd(jobID);
33+
34         if (curCmd == lastCmd)
35             return;
36 
37@@ -391,6 +400,10 @@
38 
39     program_info->SetCommFlagged(COMM_FLAG_PROCESSING);
40 
41+    JobMonitor monitor(jobID);
42+
43+    currentJobMonitor= jobID == -1 ? NULL: &monitor;
44+
45     CustomEventRelayer cer(incomingCustomEvent);
46     SlotRelayer a(commDetectorBreathe);
47     SlotRelayer b(commDetectorStatusUpdate);
48@@ -439,7 +452,7 @@
49     }
50 
51     delete commDetector;
52-
53+    currentJobMonitor=NULL;
54     return comms_found;
55 }
56 
57@@ -529,7 +542,7 @@
58 
59     if (rebuildSeekTable)
60     {
61-        nvp->RebuildSeekTable();
62+       nvp->RebuildSeekTable(false, rebuild_status_callback);
63 
64         if (!quiet)
65             cerr << "Rebuilt\n";
66--- ./programs/mythtranscode/transcode.cpp.hibernate    2006-02-10 21:57:43.000000000 -0500
67+++ ./programs/mythtranscode/transcode.cpp      2006-05-07 23:09:11.000000000 -0400
68@@ -543,6 +543,8 @@
69                                     .arg(video_width).arg(video_height)
70                                     .arg(newWidth).arg(newHeight));
71 
72+    JobMonitor monitor(jobID);
73+
74     while (nvp->TranscodeGetNextFrame(dm_iter, &did_ff, &is_key, honorCutList))
75     {
76         if (first_loop)
77@@ -785,7 +787,7 @@
78 
79             if (jobID >= 0)
80             {
81-                if (JobQueue::GetJobCmd(jobID) == JOB_STOP)
82+                if (monitor.GetJobCmd() == JOB_STOP)
83                 {
84                     unlink(outputname);
85                     delete newFrame;
86--- ./programs/mythtranscode/mpeg2fix.cpp.hibernate     2006-01-26 11:42:44.000000000 -0500
87+++ ./programs/mythtranscode/mpeg2fix.cpp       2006-05-07 23:09:11.000000000 -0400
88@@ -14,6 +14,7 @@
89 #include <netinet/in.h>
90 #include <getopt.h>
91 #include <stdint.h>
92+#include "jobqueue.h"
93 
94 #ifndef O_LARGEFILE
95 #define O_LARGEFILE 0
96@@ -199,8 +200,11 @@
97 MPEG2fixup::MPEG2fixup(const char *inf, const char *outf,
98                        QMap<long long, int> *deleteMap,
99                        const char *fmt, int norp, int fixPTS, int maxf,
100-                       bool showprog)
101+                       bool showprog, int jobIDarg)
102+    : rx(jobID)
103 {
104+    jobID=jobIDarg;
105+
106     displayFrame = new QPtrListIterator<MPEG2frame> (vFrame);
107 
108     infile = inf;
109@@ -428,13 +432,14 @@
110     return (rx->WaitBuffers());
111 }
112 
113-MPEG2replex::MPEG2replex()
114+MPEG2replex::MPEG2replex(int jobIDarg)
115 {
116     memset(&vrbuf, 0, sizeof(ringbuffer));
117     memset(&index_vrbuf, 0, sizeof(ringbuffer));
118     memset(&extrbuf, 0, sizeof(ringbuffer) * N_AUDIO);
119     memset(&index_extrbuf, 0, sizeof(ringbuffer) * N_AUDIO);
120     ext_count = 0;
121+    jobID=jobIDarg;
122 }
123 
124 MPEG2replex::~MPEG2replex()
125@@ -1763,8 +1768,12 @@
126 
127     InitReplex();
128 
129+    JobMonitor monitor(jobID);
130+
131     while (1)
132     {
133+       monitor.hibernate();
134+
135         /* read packet */
136         if (! file_end)
137         {
138@@ -2384,6 +2393,8 @@
139     AVPacket pkt;
140     int count = 0;
141 
142+    JobMonitor monitor(jobID);
143+
144     /*============ initialise AV ===============*/
145     if (!InitAV(file.ascii(), NULL, 0))
146         return TRANSCODE_EXIT_UNKNOWN_ERROR;
147@@ -2392,6 +2403,8 @@
148 
149     while (av_read_frame(inputFC, &pkt) >= 0)
150     {
151+       monitor.hibernate();
152+
153         if (pkt.stream_index == vid_id)
154         {
155             if (pkt.flags & PKT_FLAG_KEY)
156--- ./programs/mythtranscode/mpeg2fix.h.hibernate       2006-01-26 10:35:55.000000000 -0500
157+++ ./programs/mythtranscode/mpeg2fix.h 2006-05-07 23:09:11.000000000 -0400
158@@ -102,7 +102,7 @@
159 class MPEG2replex
160 {
161   public:
162-    MPEG2replex();
163+    MPEG2replex(int jobID);
164     ~MPEG2replex();
165     void Start();
166     int WaitBuffers();
167@@ -123,6 +123,7 @@
168 
169   private:
170     multiplex_t *mplex;
171+    int jobID;
172 };
173 
174 class MPEG2fixup
175@@ -130,7 +131,8 @@
176   public:
177     MPEG2fixup(const char *inf, const char *outf,
178                QMap<long long, int> *deleteMap, const char *fmt, int norp,
179-               int fixPTS, int maxf, bool showprog);
180+               int fixPTS, int maxf, bool showprog,
181+              int jobID);
182     ~MPEG2fixup();
183     int Start();
184     void AddRangeList(QStringList cutlist, int type);
185@@ -223,6 +225,8 @@
186     QMap<long long, int> delMap;
187     QMap<long long, int> saveMap;
188 
189+    int jobID;
190+
191     pthread_t thread;
192 
193     AVFormatContext *inputFC;
194--- ./programs/mythtranscode/main.cpp.hibernate 2006-01-16 01:45:15.000000000 -0500
195+++ ./programs/mythtranscode/main.cpp   2006-05-07 23:09:11.000000000 -0400
196@@ -401,7 +401,7 @@
197       
198         MPEG2fixup *m2f = new MPEG2fixup(infile.ascii(), outfile.ascii(),
199                                          &deleteMap, NULL, false, false, 20,
200-                                         showprogress);
201+                                         showprogress, jobID);
202         if (build_index)
203         {
204             int err = BuildKeyframeIndex(m2f, infile, posMap, jobID);
205--- ./libs/libmythtv/jobqueue.cpp.hibernate     2005-12-16 00:17:00.000000000 -0500
206+++ ./libs/libmythtv/jobqueue.cpp       2006-05-08 20:40:00.000000000 -0400
207@@ -2212,4 +2212,83 @@
208     return JOB_NONE;
209 }
210 
211+JobMonitor::JobMonitor(int jobIDArg)
212+    : jobID(jobIDArg), jobLastChk(time(NULL)), counter(1),
213+      doMonitor(gContext->GetNumSetting("JobHibernate", 1))
214+{
215+}
216+
217+JobMonitor::~JobMonitor()
218+{
219+}
220+
221+int JobMonitor::GetJobCmd()
222+{
223+    int jobCmd;
224+
225+    while ((jobCmd=JobQueue::GetJobCmd(jobID)) == JOB_RUN)
226+    {
227+       if (!doHibernateCheck())
228+           break;
229+    }
230+
231+    return jobCmd;
232+}
233+
234+void JobMonitor::hibernate()
235+{
236+    while (doHibernateCheck())
237+       ;
238+}
239+
240+bool JobMonitor::doHibernateCheck()
241+{
242+    if (!doMonitor)
243+       return false;
244+
245+    time_t t=time(NULL);
246+
247+    if (t == jobLastChk)
248+       return false;
249+
250+    if (t < jobLastChk)
251+       counter=0; // Time went backwards?
252+    else if (t - jobLastChk > counter)
253+       counter=0;
254+    else
255+       counter -= t - jobLastChk;
256+
257+    jobLastChk=t;
258+
259+    if (counter)
260+       return false;
261+
262+    counter=5;
263+
264+    bool isActive=false;
265+
266+    MSqlQuery query(MSqlQuery::InitCon());
267+    query.prepare("SELECT recusage FROM inuseprograms");
268+    if (query.exec() && query.isActive() && query.size() > 0)
269+       while (query.next())
270+       {
271+           QString usage=
272+               query.value(0).toString().lower();
273+
274+           if (usage == "player" ||
275+               usage == "recorder" ||
276+               usage.left(7) == "preview")
277+               isActive=true;
278+       }
279+
280+    if (!isActive)
281+       return false;
282+    counter=1;
283+    if (!JobQueue::ChangeJobComment(jobID,
284+                                   "waiting for record/playback to stop"))
285+       return false;
286+    sleep(5);
287+    return true;
288+}
289+
290 /* vim: set expandtab tabstop=4 shiftwidth=4: */
291--- ./libs/libmythtv/jobqueue.h.hibernate       2005-11-19 02:11:30.000000000 -0500
292+++ ./libs/libmythtv/jobqueue.h 2006-05-07 23:09:11.000000000 -0400
293@@ -217,6 +217,25 @@
294     pthread_t queueThread;
295 };
296 
297+#include <time.h>
298+
299+class JobMonitor {
300+    int jobID;
301+    time_t jobLastChk;
302+    int counter;
303+
304+    int doMonitor;
305+
306+ public:
307+    JobMonitor(int jobID);
308+    ~JobMonitor();
309+
310+    int GetJobCmd();
311+    void hibernate();
312+ private:
313+    bool doHibernateCheck();
314+};
315+
316 #endif
317 
318 /* vim: set expandtab tabstop=4 shiftwidth=4: */