--- ./programs/mythcommflag/main.cpp.hibernate	2006-02-08 01:42:13.000000000 -0500
+++ ./programs/mythcommflag/main.cpp	2006-05-08 19:48:00.000000000 -0400
@@ -51,6 +51,13 @@
 
 int jobID = -1;
 int lastCmd = -1;
+JobMonitor *currentJobMonitor;
+
+static void rebuild_status_callback(int, void *)
+{
+    if (currentJobMonitor)
+	currentJobMonitor->hibernate();
+}
 
 int BuildVideoMarkup(QString& filename)
 {
@@ -81,7 +88,7 @@
                                                    program_info);
     nvp->SetRingBuffer(tmprbuf);
 
-    nvp->RebuildSeekTable(!quiet);
+    nvp->RebuildSeekTable(!quiet, rebuild_status_callback);
 
     cerr << "Rebuilt\n";
 
@@ -250,7 +257,9 @@
 
     if (jobID != -1)
     {
-        int curCmd = JobQueue::GetJobCmd(jobID);
+	int curCmd = currentJobMonitor ? currentJobMonitor->GetJobCmd()
+	    : JobQueue::GetJobCmd(jobID);
+
         if (curCmd == lastCmd)
             return;
 
@@ -391,6 +400,10 @@
 
     program_info->SetCommFlagged(COMM_FLAG_PROCESSING);
 
+    JobMonitor monitor(jobID);
+
+    currentJobMonitor= jobID == -1 ? NULL: &monitor;
+
     CustomEventRelayer cer(incomingCustomEvent);
     SlotRelayer a(commDetectorBreathe);
     SlotRelayer b(commDetectorStatusUpdate);
@@ -439,7 +452,7 @@
     }
 
     delete commDetector;
-
+    currentJobMonitor=NULL;
     return comms_found;
 }
 
@@ -529,7 +542,7 @@
 
     if (rebuildSeekTable)
     {
-        nvp->RebuildSeekTable();
+	nvp->RebuildSeekTable(false, rebuild_status_callback);
 
         if (!quiet)
             cerr << "Rebuilt\n";
--- ./programs/mythtranscode/transcode.cpp.hibernate	2006-02-10 21:57:43.000000000 -0500
+++ ./programs/mythtranscode/transcode.cpp	2006-05-07 23:09:11.000000000 -0400
@@ -543,6 +543,8 @@
                                     .arg(video_width).arg(video_height)
                                     .arg(newWidth).arg(newHeight));
 
+    JobMonitor monitor(jobID);
+
     while (nvp->TranscodeGetNextFrame(dm_iter, &did_ff, &is_key, honorCutList))
     {
         if (first_loop)
@@ -785,7 +787,7 @@
 
             if (jobID >= 0)
             {
-                if (JobQueue::GetJobCmd(jobID) == JOB_STOP)
+                if (monitor.GetJobCmd() == JOB_STOP)
                 {
                     unlink(outputname);
                     delete newFrame;
--- ./programs/mythtranscode/mpeg2fix.cpp.hibernate	2006-01-26 11:42:44.000000000 -0500
+++ ./programs/mythtranscode/mpeg2fix.cpp	2006-05-07 23:09:11.000000000 -0400
@@ -14,6 +14,7 @@
 #include <netinet/in.h>
 #include <getopt.h>
 #include <stdint.h>
+#include "jobqueue.h"
 
 #ifndef O_LARGEFILE
 #define O_LARGEFILE 0
@@ -199,8 +200,11 @@
 MPEG2fixup::MPEG2fixup(const char *inf, const char *outf,
                        QMap<long long, int> *deleteMap,
                        const char *fmt, int norp, int fixPTS, int maxf,
-                       bool showprog)
+                       bool showprog, int jobIDarg)
+    : rx(jobID)
 {
+    jobID=jobIDarg;
+
     displayFrame = new QPtrListIterator<MPEG2frame> (vFrame);
 
     infile = inf;
@@ -428,13 +432,14 @@
     return (rx->WaitBuffers());
 }
 
-MPEG2replex::MPEG2replex()
+MPEG2replex::MPEG2replex(int jobIDarg)
 {
     memset(&vrbuf, 0, sizeof(ringbuffer));
     memset(&index_vrbuf, 0, sizeof(ringbuffer));
     memset(&extrbuf, 0, sizeof(ringbuffer) * N_AUDIO);
     memset(&index_extrbuf, 0, sizeof(ringbuffer) * N_AUDIO);
     ext_count = 0;
+    jobID=jobIDarg;
 }
 
 MPEG2replex::~MPEG2replex()
@@ -1763,8 +1768,12 @@
 
     InitReplex();
 
+    JobMonitor monitor(jobID);
+
     while (1)
     {
+	monitor.hibernate();
+
         /* read packet */
         if (! file_end)
         {
@@ -2384,6 +2393,8 @@
     AVPacket pkt;
     int count = 0;
 
+    JobMonitor monitor(jobID);
+
     /*============ initialise AV ===============*/
     if (!InitAV(file.ascii(), NULL, 0))
         return TRANSCODE_EXIT_UNKNOWN_ERROR;
@@ -2392,6 +2403,8 @@
 
     while (av_read_frame(inputFC, &pkt) >= 0)
     {
+	monitor.hibernate();
+
         if (pkt.stream_index == vid_id)
         {
             if (pkt.flags & PKT_FLAG_KEY)
--- ./programs/mythtranscode/mpeg2fix.h.hibernate	2006-01-26 10:35:55.000000000 -0500
+++ ./programs/mythtranscode/mpeg2fix.h	2006-05-07 23:09:11.000000000 -0400
@@ -102,7 +102,7 @@
 class MPEG2replex
 {
   public:
-    MPEG2replex();
+    MPEG2replex(int jobID);
     ~MPEG2replex();
     void Start();
     int WaitBuffers();
@@ -123,6 +123,7 @@
 
   private:
     multiplex_t *mplex;
+    int jobID;
 };
 
 class MPEG2fixup
@@ -130,7 +131,8 @@
   public:
     MPEG2fixup(const char *inf, const char *outf,
                QMap<long long, int> *deleteMap, const char *fmt, int norp,
-               int fixPTS, int maxf, bool showprog);
+               int fixPTS, int maxf, bool showprog,
+	       int jobID);
     ~MPEG2fixup();
     int Start();
     void AddRangeList(QStringList cutlist, int type);
@@ -223,6 +225,8 @@
     QMap<long long, int> delMap;
     QMap<long long, int> saveMap;
 
+    int jobID;
+
     pthread_t thread;
 
     AVFormatContext *inputFC;
--- ./programs/mythtranscode/main.cpp.hibernate	2006-01-16 01:45:15.000000000 -0500
+++ ./programs/mythtranscode/main.cpp	2006-05-07 23:09:11.000000000 -0400
@@ -401,7 +401,7 @@
        
         MPEG2fixup *m2f = new MPEG2fixup(infile.ascii(), outfile.ascii(),
                                          &deleteMap, NULL, false, false, 20,
-                                         showprogress);
+                                         showprogress, jobID);
         if (build_index)
         {
             int err = BuildKeyframeIndex(m2f, infile, posMap, jobID);
--- ./libs/libmythtv/jobqueue.cpp.hibernate	2005-12-16 00:17:00.000000000 -0500
+++ ./libs/libmythtv/jobqueue.cpp	2006-05-08 20:40:00.000000000 -0400
@@ -2212,4 +2212,83 @@
     return JOB_NONE;
 }
 
+JobMonitor::JobMonitor(int jobIDArg)
+    : jobID(jobIDArg), jobLastChk(time(NULL)), counter(1),
+      doMonitor(gContext->GetNumSetting("JobHibernate", 1))
+{
+}
+
+JobMonitor::~JobMonitor()
+{
+}
+
+int JobMonitor::GetJobCmd()
+{
+    int jobCmd;
+
+    while ((jobCmd=JobQueue::GetJobCmd(jobID)) == JOB_RUN)
+    {
+	if (!doHibernateCheck())
+	    break;
+    }
+
+    return jobCmd;
+}
+
+void JobMonitor::hibernate()
+{
+    while (doHibernateCheck())
+	;
+}
+
+bool JobMonitor::doHibernateCheck()
+{
+    if (!doMonitor)
+	return false;
+
+    time_t t=time(NULL);
+
+    if (t == jobLastChk)
+	return false;
+
+    if (t < jobLastChk)
+	counter=0; // Time went backwards?
+    else if (t - jobLastChk > counter)
+	counter=0;
+    else
+	counter -= t - jobLastChk;
+
+    jobLastChk=t;
+
+    if (counter)
+	return false;
+
+    counter=5;
+
+    bool isActive=false;
+
+    MSqlQuery query(MSqlQuery::InitCon());
+    query.prepare("SELECT recusage FROM inuseprograms");
+    if (query.exec() && query.isActive() && query.size() > 0)
+	while (query.next())
+	{
+	    QString usage=
+		query.value(0).toString().lower();
+
+	    if (usage == "player" ||
+		usage == "recorder" ||
+		usage.left(7) == "preview")
+		isActive=true;
+	}
+
+    if (!isActive)
+	return false;
+    counter=1;
+    if (!JobQueue::ChangeJobComment(jobID,
+				    "waiting for record/playback to stop"))
+	return false;
+    sleep(5);
+    return true;
+}
+
 /* vim: set expandtab tabstop=4 shiftwidth=4: */
--- ./libs/libmythtv/jobqueue.h.hibernate	2005-11-19 02:11:30.000000000 -0500
+++ ./libs/libmythtv/jobqueue.h	2006-05-07 23:09:11.000000000 -0400
@@ -217,6 +217,25 @@
     pthread_t queueThread;
 };
 
+#include <time.h>
+
+class JobMonitor {
+    int jobID;
+    time_t jobLastChk;
+    int counter;
+
+    int doMonitor;
+
+ public:
+    JobMonitor(int jobID);
+    ~JobMonitor();
+
+    int GetJobCmd();
+    void hibernate();
+ private:
+    bool doHibernateCheck();
+};
+
 #endif
 
 /* vim: set expandtab tabstop=4 shiftwidth=4: */
