[mythtv] [patch] recorder open and error handling

Daniel Thor Kristjansson danielk at mrl.nyu.edu
Thu Oct 21 15:35:52 UTC 2004


This was a part of the hdtv-recorder patch. It patches nvr, dvbrec, 
hdtvrec, mpegrec, and recorderbase to both add an Open() function and to 
set an error value so that IsErrored() returns true when there is an 
error. dvbrecorder already had an Open() function and nvr already used 
IsErrored() correctly.

By itself this adds no user visible features.

-- Daniel
-------------- next part --------------
Index: libs/libmythtv/NuppelVideoRecorder.h
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/NuppelVideoRecorder.h,v
retrieving revision 1.74
diff -u -r1.74 NuppelVideoRecorder.h
--- libs/libmythtv/NuppelVideoRecorder.h	2 Jun 2004 03:45:29 -0000	1.74
+++ libs/libmythtv/NuppelVideoRecorder.h	21 Oct 2004 14:59:24 -0000
@@ -68,6 +68,7 @@
 
     long long GetFramesWritten(void); 
 
+    bool Open(void);
     int GetVideoFd(void);
     void Reset(void);
 
Index: libs/libmythtv/NuppelVideoRecorder.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/NuppelVideoRecorder.cpp,v
retrieving revision 1.180
diff -u -r1.180 NuppelVideoRecorder.cpp
--- libs/libmythtv/NuppelVideoRecorder.cpp	14 Oct 2004 04:58:04 -0000	1.180
+++ libs/libmythtv/NuppelVideoRecorder.cpp	21 Oct 2004 14:59:25 -0000
@@ -822,6 +822,79 @@
     strm = new signed char[w * h * 2 + 10];
 }
 
+bool NuppelVideoRecorder::Open(void)
+{
+    if (channelfd>0)
+        return true;
+
+    int retries = 0;
+    fd = open(videodevice.ascii(), O_RDWR);
+    while (fd <= 0)
+    {
+        usleep(30000);
+        fd = open(videodevice.ascii(), O_RDWR);
+        if (retries++ > 5)
+        {
+            cerr << "Can't open video device: " << videodevice << endl;
+            perror("open video:");
+            KillChildren();
+            errored = true;
+            return false;
+        }
+    }
+
+    usingv4l2 = true;
+
+    struct v4l2_capability vcap;
+    memset(&vcap, 0, sizeof(vcap));
+
+    if (ioctl(fd, VIDIOC_QUERYCAP, &vcap) < 0)
+    {
+        usingv4l2 = false;
+    }
+
+    if (usingv4l2 && !(vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
+    {
+        cerr << "Not a v4l2 capture device, falling back to v4l\n";
+        usingv4l2 = false;
+    }
+
+    if (usingv4l2 && !(vcap.capabilities & V4L2_CAP_STREAMING))
+    {
+        cerr << "Won't work with the streaming interface, falling back\n";
+        usingv4l2 = false;
+    }
+
+    usingv4l2 = false;
+
+    if (usingv4l2)
+    {
+        if (vcap.card[0] == 'B' && vcap.card[1] == 'T' &&
+            vcap.card[2] == '8' && vcap.card[4] == '8')
+            correct_bttv = true;
+
+        if (QString("cx8800") == QString((char *)vcap.driver))
+        {
+            channelfd = open(videodevice.ascii(), O_RDWR);
+            if (channelfd < 0)
+            {
+                cerr << "Can't open video device: " << videodevice << endl;
+                perror("open video:");
+                KillChildren();
+                return false;
+            }
+            
+            inpixfmt = FMT_NONE;
+            InitFilters();
+            DoV4L2();
+            return false;
+        }
+    }
+
+    channelfd = fd;
+    return true;
+}
+
 void NuppelVideoRecorder::StartRecording(void)
 {
     if (lzo_init() != LZO_E_OK)
@@ -898,70 +971,12 @@
     if (getuid() == 0)
         nice(-10);
 
-    int retries = 0;
-    fd = open(videodevice.ascii(), O_RDWR);
-    while (fd <= 0)
-    {
-        usleep(30000);
-        fd = open(videodevice.ascii(), O_RDWR);
-        if (retries++ > 5)
-        { 
-            cerr << "Can't open video device: " << videodevice << endl;
-            perror("open video:");
-            KillChildren();
-            errored = true;
-            return;
-        }
-    }
-
-    usingv4l2 = true;
-
-    struct v4l2_capability vcap;
-    memset(&vcap, 0, sizeof(vcap));
-
-    if (ioctl(fd, VIDIOC_QUERYCAP, &vcap) < 0)
-    {
-        usingv4l2 = false;
-    }
-
-    if (usingv4l2 && !(vcap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
-    {
-        cerr << "Not a v4l2 capture device, falling back to v4l\n";
-        usingv4l2 = false;
-    }
-
-    if (usingv4l2 && !(vcap.capabilities & V4L2_CAP_STREAMING))
-    {
-        cerr << "Won't work with the streaming interface, falling back\n";
-        usingv4l2 = false;
-    }
-
-    if (usingv4l2)
+    if (!Open())
     {
-        if (vcap.card[0] == 'B' && vcap.card[1] == 'T' &&
-            vcap.card[2] == '8' && vcap.card[4] == '8')
-            correct_bttv = true;
-
-        if (QString("cx8800") == QString((char *)vcap.driver))
-        {
-            channelfd = open(videodevice.ascii(), O_RDWR);
-            if (channelfd < 0)
-            {
-                cerr << "Can't open video device: " << videodevice << endl;
-                perror("open video:");
-                KillChildren();
-                return;
-            }
-     
-            inpixfmt = FMT_NONE;
-            InitFilters();
-            DoV4L2();
-            return;
-        }
+        errored = true;
+        return;
     }
 
-    channelfd = fd;
-
     struct video_capability vc;
     struct video_mmap mm;
     struct video_mbuf vm;
@@ -980,6 +995,7 @@
     {
         perror("VIDIOCGCAP:");
         KillChildren(); 
+        errored = true;
         return;
     }
 
@@ -990,6 +1006,7 @@
     if ((vc.type & VID_TYPE_MJPEG_ENCODER) && hardware_encode)
     {
         DoMJPEG();
+        errored = true;
         return;
     }
 
@@ -1000,6 +1017,7 @@
     {
         perror("VIDOCGMBUF:");
         KillChildren();
+        errored = true;
         return;
     }
 
@@ -1007,6 +1025,7 @@
     {
         fprintf(stderr, "need a minimum of 2 capture buffers\n");
         KillChildren();
+        errored = true;
         return;
     }
 
@@ -1020,6 +1039,7 @@
     {
         perror("mmap");
         KillChildren();
+        errored = true;
         return;
     }
 
Index: libs/libmythtv/dvbrecorder.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/dvbrecorder.cpp,v
retrieving revision 1.38
diff -u -r1.38 dvbrecorder.cpp
--- libs/libmythtv/dvbrecorder.cpp	6 Aug 2004 17:34:55 -0000	1.38
+++ libs/libmythtv/dvbrecorder.cpp	21 Oct 2004 14:59:26 -0000
@@ -58,6 +58,7 @@
 
 DVBRecorder::DVBRecorder(DVBChannel* advbchannel): RecorderBase()
 {
+    error = false;
     isopen = false;
     cardnum = 0;
     swfilter = false;
@@ -372,7 +373,10 @@
 void DVBRecorder::StartRecording()
 {
     if (!Open())
+    {
+        error = true;
         return;
+    }
 
     int ret, dataflow = -1;
     receiving = false;
@@ -721,6 +725,7 @@
 
 void DVBRecorder::Reset(void)
 {
+    error = false;
     framesWritten = 0;
 
     positionMap.clear();
Index: libs/libmythtv/dvbrecorder.h
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/dvbrecorder.h,v
retrieving revision 1.21
diff -u -r1.21 dvbrecorder.h
--- libs/libmythtv/dvbrecorder.h	2 Jun 2004 03:45:29 -0000	1.21
+++ libs/libmythtv/dvbrecorder.h	21 Oct 2004 14:59:27 -0000
@@ -47,7 +47,7 @@
     void WaitForPause(void);
 
     bool IsRecording(void);
-    bool IsErrored(void) { return false; }
+    bool IsErrored(void) { return error; }
 
     long long GetFramesWritten(void);
 
@@ -87,6 +87,7 @@
     void QualityMonitorSample(int cardnum, dvb_stats_t& sample);
     void ExpireQualityData();
 
+    bool error;
     bool recording;
     bool encoding;
 
Index: libs/libmythtv/hdtvrecorder.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/hdtvrecorder.cpp,v
retrieving revision 1.26
diff -u -r1.26 hdtvrecorder.cpp
--- libs/libmythtv/hdtvrecorder.cpp	6 Jul 2004 04:44:36 -0000	1.26
+++ libs/libmythtv/hdtvrecorder.cpp	21 Oct 2004 14:59:28 -0000
@@ -82,6 +82,7 @@
 HDTVRecorder::HDTVRecorder()
             : RecorderBase()
 {
+    _error = false;
     paused = false;
     mainpaused = false;
     recording = false;
@@ -140,6 +141,23 @@
     SetOption("vbiformat", gContext->GetSetting("VbiFormat"));
 }
 
+bool HDTVRecorder::Open()
+{
+    if (!buffer) 
+        return false;
+
+    if (chanfd <= 0)
+        chanfd = open(videodevice.ascii(), O_RDWR);
+
+    if (chanfd <= 0)
+    {
+        VERBOSE(VB_IMPORTANT, QString("Can't open video device: %1 chanfd = %2")
+                .arg(videodevice).arg(chanfd));
+        perror("open video:");
+    }
+    return (chanfd>0);
+}
+
 void HDTVRecorder::StartRecording(void)
 {
     uint8_t * buf;
@@ -150,18 +168,16 @@
     int insync = 0;
     int ret;
 
-    chanfd = open(videodevice.ascii(), O_RDWR);
-    if (chanfd <= 0)
+    if (!Open())
     {
-        cerr << "HD1 Can't open video device: " << videodevice 
-             << " chanfd = "<< chanfd << endl;
-        perror("open video:");
+        _error = true;        
         return;
     }
 
     if (!SetupRecording())
     {
         cerr << "HD Error initializing recording\n";
+        _error = true;
         return;
     }
 
@@ -821,6 +837,8 @@
 
 void HDTVRecorder::Reset(void)
 {
+    _error = true;
+
     framesWritten = 0;
     framesSeen = 0;
     gopset = false;
Index: libs/libmythtv/hdtvrecorder.h
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/hdtvrecorder.h,v
retrieving revision 1.17
diff -u -r1.17 hdtvrecorder.h
--- libs/libmythtv/hdtvrecorder.h	2 Jun 2004 03:45:29 -0000	1.17
+++ libs/libmythtv/hdtvrecorder.h	21 Oct 2004 14:59:29 -0000
@@ -36,6 +36,7 @@
 
     long long GetFramesWritten(void);
 
+    bool Open(void);
     int GetVideoFd(void);
 
     long long GetKeyframePosition(long long desired);
@@ -64,6 +65,7 @@
     bool paused;
     bool mainpaused;
     bool cleartimeonpause;
+    bool _error;
 
     long long framesWritten;
     long long framesSeen;
Index: libs/libmythtv/mpegrecorder.h
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/mpegrecorder.h,v
retrieving revision 1.13
diff -u -r1.13 mpegrecorder.h
--- libs/libmythtv/mpegrecorder.h	12 Jun 2004 01:25:46 -0000	1.13
+++ libs/libmythtv/mpegrecorder.h	21 Oct 2004 14:59:29 -0000
@@ -31,10 +31,11 @@
     bool GetPause(void);
     void WaitForPause(void);
     bool IsRecording(void);
-    bool IsErrored(void) { return false; }
+    bool IsErrored(void) { return errored; }
 
     long long GetFramesWritten(void);
 
+    bool Open(void);
     int GetVideoFd(void);
 
     long long GetKeyframePosition(long long desired);
@@ -47,9 +48,10 @@
     bool PacketHasHeader(unsigned char *buf, int len, unsigned int startcode);
     void ProcessData(unsigned char *buffer, int len);
 
-    void openMpegFileAsInput(void);
-    void openV4L2DeviceAsInput(void);
+    bool OpenMpegFileAsInput(void);
+    bool OpenV4L2DeviceAsInput(void);
 
+    bool errored;
     bool deviceIsMpegFile;
     int bufferSize;
 
Index: libs/libmythtv/mpegrecorder.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/mpegrecorder.cpp,v
retrieving revision 1.42
diff -u -r1.42 mpegrecorder.cpp
--- libs/libmythtv/mpegrecorder.cpp	14 Oct 2004 04:58:04 -0000	1.42
+++ libs/libmythtv/mpegrecorder.cpp	21 Oct 2004 14:59:30 -0000
@@ -45,6 +45,7 @@
 MpegRecorder::MpegRecorder()
             : RecorderBase()
 {
+    errored = false;
     paused = false;
     mainpaused = false;
     recording = false;
@@ -223,7 +224,7 @@
     }
 }
 
-void MpegRecorder::openMpegFileAsInput(void)
+bool MpegRecorder::OpenMpegFileAsInput(void)
 {
     chanfd = readfd = open(videodevice.ascii(), O_RDONLY);
 
@@ -231,18 +232,19 @@
     {
         cerr << "Can't open MPEG File: " << videodevice << endl;
         perror("open mpeg file:");
-        return;
+        return false;
     }
+    return true;
 }
 
-void MpegRecorder::openV4L2DeviceAsInput(void)
+bool MpegRecorder::OpenV4L2DeviceAsInput(void)
 {
     chanfd = open(videodevice.ascii(), O_RDWR);
     if (chanfd <= 0)
     {
         cerr << "Can't open video device: " << videodevice << endl;
         perror("open video:");
-        return;
+        return false;
     }
 
     struct v4l2_format vfmt;
@@ -254,7 +256,7 @@
     {
         cerr << "Error getting format\n";
         perror("VIDIOC_G_FMT:");
-        return;
+        return false;
     }
 
     vfmt.fmt.pix.width = width;
@@ -264,7 +266,7 @@
     {
         cerr << "Error setting format\n";
         perror("VIDIOC_S_FMT:");
-        return;
+        return false;
     }
 
     struct ivtv_ioctl_codec ivtvcodec;
@@ -274,7 +276,7 @@
     {
         cerr << "Error getting codec params\n";
         perror("IVTV_IOC_G_CODEC:");
-        return;
+        return false;
     }
 
     // only 48kHz works properly.
@@ -327,7 +329,7 @@
     {
         cerr << "Error setting codec params\n";
         perror("IVTV_IOC_S_CODEC:");
-        return;
+        return false;
     }
 
     struct v4l2_control ctrl;
@@ -338,7 +340,7 @@
     {
         cerr << "Error setting codec params\n";
         perror("VIDIOC_S_CTRL:");
-        return;
+        return false;
     }
 
     readfd = open(videodevice.ascii(), O_RDWR);
@@ -346,24 +348,32 @@
     {
         cerr << "Can't open video device: " << videodevice << endl;
         perror("open video:");
-        return;
+        return false;
     }
+    return true;
 }
 
-void MpegRecorder::StartRecording(void)
+bool MpegRecorder::Open(void)
 {
     if (deviceIsMpegFile)
-        openMpegFileAsInput();
+        return OpenMpegFileAsInput();
     else
-        openV4L2DeviceAsInput();
+        return OpenV4L2DeviceAsInput();
+}
 
-    if ((chanfd <= 0) || (readfd <= 0))
-        return;
+void MpegRecorder::StartRecording(void)
+{
+    if (!Open())
+    {
+	errored = true;
+	return;
+    }
 
     if (!SetupRecording())
     {
-        cerr << "Error initializing recording\n";
-        return;
+        VERBOSE(VB_IMPORTANT, "Error initializing recording");
+	errored = true;
+	return;
     }
 
     encoding = true;
@@ -646,6 +656,7 @@
 
 void MpegRecorder::Reset(void)
 {
+    errored = false;
     AVPacketList *pktl = NULL;
     while ((pktl = ic->packet_buffer))
     {
Index: libs/libmythtv/recorderbase.h
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/recorderbase.h,v
retrieving revision 1.11
diff -u -r1.11 recorderbase.h
--- libs/libmythtv/recorderbase.h	14 Oct 2004 04:58:04 -0000	1.11
+++ libs/libmythtv/recorderbase.h	21 Oct 2004 14:59:30 -0000
@@ -49,6 +49,7 @@
 
     virtual long long GetFramesWritten(void) = 0;
     
+    virtual bool Open(void) = 0;
     virtual int GetVideoFd(void) = 0;
     
     virtual long long GetKeyframePosition(long long desired) = 0;


More information about the mythtv-dev mailing list