[mythtv] [patch] exit cleanup of tv_rec.{cpp,h}

Daniel Thor Kristjansson danielk at mrl.nyu.edu
Tue Dec 28 20:07:40 UTC 2004


This removes the two exit's in tv_rec.cpp.

The first is removed by moving some initialization to the pre-existing 
Init() function, and changing it to return a boolean for success of 
failure and checking it for failure in the mythbackend main() function.

The second is eliminated by creating a 'errored' variable and setting it 
to true if an error is encountered in HandleEvent. This is then checked 
for in the RunTV loop, and we exit it if this the error condition is 
ever set. This changes the behavior somewhat in that instead of exiting, 
any other TVRec cards continue working. We should have a monitor thread 
that restarts a RunTV thread if it ever exits. But I'm considering this 
a feature that can be added later. (And it should probably wait as I'm 
likely to find other things that should be monitored in my random exit 
removal project.)

-- Daniel
-------------- next part --------------
Index: libs/libmythtv/tv_rec.h
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/tv_rec.h,v
retrieving revision 1.59
diff -u -r1.59 tv_rec.h
--- libs/libmythtv/tv_rec.h	15 Aug 2004 22:38:31 -0000	1.59
+++ libs/libmythtv/tv_rec.h	28 Dec 2004 19:55:31 -0000
@@ -46,7 +46,7 @@
     TVRec(int capturecardnum);
    ~TVRec(void);
 
-    void Init(void);
+    bool Init(void); // returns true if init is successful
 
     void RecordPending(ProgramInfo *rcinfo, int secsleft);
     int StartRecording(ProgramInfo *rcinfo);
@@ -139,6 +139,7 @@
 
     int GetCaptureCardNum(void) { return m_capturecardnum; }
 
+    bool IsErrored() { return errored; }
  protected:
     void RunTV(void);
     static void *EventThread(void *param);
@@ -232,6 +233,8 @@
 
     dvb_options_t dvb_options;
 
+    bool errored;
+
     char requestBuffer[256001];
 };
 
Index: libs/libmythtv/tv_rec.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/tv_rec.cpp,v
retrieving revision 1.168
diff -u -r1.168 tv_rec.cpp
--- libs/libmythtv/tv_rec.cpp	20 Nov 2004 05:35:34 -0000	1.168
+++ libs/libmythtv/tv_rec.cpp	28 Dec 2004 19:55:31 -0000
@@ -63,14 +63,17 @@
 
     ConnectDB(capturecardnum);
 
-    QString chanorder = gContext->GetSetting("ChannelOrdering", "channum + 0");
-
     audiosamplerate = -1;
     skip_btaudio = false;
+    errored = false;
+}
 
+bool TVRec::Init(void)
+{
+    QString chanorder = gContext->GetSetting("ChannelOrdering", "channum + 0");
     QString inputname, startchannel;
 
-    GetDevices(capturecardnum, videodev, vbidev, audiodev, audiosamplerate,
+    GetDevices(m_capturecardnum, videodev, vbidev, audiodev, audiosamplerate,
                inputname, startchannel, cardtype, dvb_options, skip_btaudio);
 
     if (cardtype == "DVB")
@@ -87,11 +90,13 @@
         if (dvb_options.dvb_on_demand) 
             ((DVBChannel *)channel)->CloseDVB();
 #else
-        VERBOSE(VB_IMPORTANT, "ERROR: DVB Card configured, "
-                              "but no DVB support compiled in!");
-        VERBOSE(VB_IMPORTANT, "Remove the card from configuration, "
-                              "or recompile MythTV.");
-        exit(-20);
+        QString msg =
+            QString("ERROR: DVB Card configured on %1, but no DVB support "
+                    "compiled in!\n Remove the card from configuration, or "
+                    "recompile MythTV.").arg(videodev);
+        VERBOSE(VB_IMPORTANT, msg);
+        errored = true;
+        return false;
 #endif
     }
     else if ((cardtype == "MPEG") && (videodev.lower().left(5) == "file:"))
@@ -112,10 +117,7 @@
         channel->SetChannelOrdering(chanorder);
         channel->Close();
     }
-}
 
-void TVRec::Init(void)
-{
     inoverrecord = false;
     overrecordseconds = gContext->GetNumSetting("RecordOverTime");
 
@@ -140,6 +142,8 @@
     curRecording = NULL;
     prevRecording = NULL;
     pendingRecording = NULL;
+
+    return true;
 }
 
 TVRec::~TVRec(void)
@@ -394,7 +398,8 @@
     if (nextState == kState_Error)
     {
         VERBOSE(VB_IMPORTANT, "TVRec: Attempting to set to an error state, exiting");
-        exit(-21);
+        errored = true;
+        return;
     }
 
     if (tmpInternalState == kState_None && nextState == kState_WatchingLiveTV)
@@ -847,6 +852,12 @@
     {
         if (changeState)
             HandleStateChange();
+        if (IsErrored())
+        {
+            VERBOSE(VB_IMPORTANT, "TVRec: RunTV encountered fatal error, exiting event thread.");
+            runMainLoop = false;
+            return;
+        }
 
         usleep(1000);
 
Index: programs/mythbackend/main.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythbackend/main.cpp,v
retrieving revision 1.78
diff -u -r1.78 main.cpp
--- programs/mythbackend/main.cpp	23 Oct 2004 20:00:03 -0000	1.78
+++ programs/mythbackend/main.cpp	28 Dec 2004 19:55:32 -0000
@@ -97,11 +97,11 @@
 
             if (host.isNull() || host.isEmpty())
             {
-                QString msg = "One of your capturecard entries does not have a "
-                              "hostname.\n  Please run setup and confirm all "
-                              "of the capture cards.\n";
+                QString msg = 
+                    "mythbackend: One of your capturecard entries does not have a hostname.\n"
+                    "Please run setup and confirm all of the capture cards.";
 
-                cerr << msg;
+                VERBOSE(VB_IMPORTANT, msg);
                 gContext->LogEntry("mythbackend", LP_CRITICAL,
                                    "Problem with capture cards", msg);
                 exit(2);
@@ -112,7 +112,11 @@
                 if (host == localhostname)
                 {
                     TVRec *tv = new TVRec(cardid);
-                    tv->Init();
+                    if (!tv->Init())
+                    {
+                        VERBOSE(VB_IMPORTANT, "mythbackend: Unable to initialize slave TVRec() class, exiting.");
+                        exit(51);
+                    }
                     EncoderLink *enc = new EncoderLink(cardid, tv);
                     tvList[cardid] = enc;
                 }
@@ -122,7 +126,11 @@
                 if (host == localhostname)
                 {
                     TVRec *tv = new TVRec(cardid);
-                    tv->Init();
+                    if (!tv->Init())
+                    {
+                        VERBOSE(VB_IMPORTANT, "mythbackend: Unable to initialize master TVRec() class, exiting.");
+                        exit(52);
+                    }
                     EncoderLink *enc = new EncoderLink(cardid, tv);
                     tvList[cardid] = enc;
                 }
@@ -136,8 +144,8 @@
     }
     else
     {
-        cerr << "ERROR: no capture cards are defined in the database.\n";
-        cerr << "Perhaps you should read the installation instructions?\n";
+        VERBOSE(VB_IMPORTANT, "mythbackend: ERROR, no capture cards are defined in the database.\n"
+                "Perhaps you should read the installation instructions?");
         gContext->LogEntry("mythbackend", LP_CRITICAL,
                            "No capture cards are defined", 
                            "Please run the setup program.");


More information about the mythtv-dev mailing list