summaryrefslogtreecommitdiffstats
path: root/abs/core-testing/mythtv/trunk/mythtv/hdpvr-signalmonitor.patch
blob: 2465cfe09de51b4a34927caf5ae2ab1cbcb6f866 (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
Index: libs/libmythtv/analogsignalmonitor.cpp
===================================================================
--- libs/libmythtv/analogsignalmonitor.cpp.orig
+++ libs/libmythtv/analogsignalmonitor.cpp
@@ -4,23 +4,92 @@
 #include <cerrno>
 #include <unistd.h>
 #include <sys/ioctl.h>
+#include <poll.h>
 
 #include "videodev_myth.h"
 #include "mythcontext.h"
 #include "analogsignalmonitor.h"
 #include "v4lchannel.h"
 
-#define LOC QString("AnalogSM: ").arg(channel->GetDevice())
-#define LOC_ERR QString("AnalogSM, Error: ").arg(channel->GetDevice())
+#define LOC QString("AnalogSM: %1 ").arg(channel->GetDevice())
+#define LOC_ERR QString("AnalogSM, Error: %1 ").arg(channel->GetDevice())
 
-AnalogSignalMonitor::AnalogSignalMonitor(
-    int db_cardnum, V4LChannel *_channel, uint64_t _flags) :
+AnalogSignalMonitor::AnalogSignalMonitor(int db_cardnum, V4LChannel *_channel,
+                                         uint64_t _flags) :
     SignalMonitor(db_cardnum, _channel, _flags),
-    usingv4l2(false)
+    m_usingv4l2(false),
+    m_stage(0)
 {
     int videofd = channel->GetFd();
     if (videofd >= 0)
-        usingv4l2 = CardUtil::hasV4L2(videofd);
+    {
+        m_usingv4l2 = CardUtil::hasV4L2(videofd);
+        CardUtil::GetV4LInfo(videofd, m_card, m_driver, m_version);
+        VERBOSE(VB_RECORD, LOC + QString("card '%1' driver '%2' version '%3'")
+                .arg(m_card).arg(m_driver).arg(m_version));
+    }
+}
+
+bool AnalogSignalMonitor::handleHDPVR(int videofd)
+{
+    struct v4l2_encoder_cmd command;
+    struct pollfd polls;
+    int    idx;
+
+    if (m_stage == 0)
+    {
+        VERBOSE(VB_RECORD, LOC + "hd-pvr start encoding");
+        // Tell it to start encoding, then wait for it to actually feed us
+        // some data.
+        memset(&command, 0, sizeof(struct v4l2_encoder_cmd));
+        command.cmd = V4L2_ENC_CMD_START;
+        if (ioctl(videofd, VIDIOC_ENCODER_CMD, &command) == 0)
+            m_stage = 1;
+        else
+            VERBOSE(VB_IMPORTANT, LOC_ERR + "Start encoding failed" + ENO);
+    }
+
+    if (m_stage == 1)
+    {
+        VERBOSE(VB_RECORD, LOC + "hd-pvr wait for data");
+
+        polls.fd      = videofd;
+        polls.events  = POLLIN;
+        polls.revents = 0;
+
+        if (poll(&polls, 1, 1500) > 0)
+            m_stage = 2;
+        else
+        {
+            VERBOSE(VB_RECORD, LOC + "Poll timed-out.  Resetting");
+            memset(&command, 0, sizeof(struct v4l2_encoder_cmd));
+            command.cmd = V4L2_ENC_CMD_STOP;
+            ioctl(videofd, VIDIOC_ENCODER_CMD, &command);
+            m_stage = 0;
+        }
+    }
+
+    if (m_stage == 2)
+    {
+        VERBOSE(VB_RECORD, LOC + "hd-pvr data ready.  Stop encoding");
+
+        command.cmd = V4L2_ENC_CMD_STOP;
+        if (ioctl(videofd, VIDIOC_ENCODER_CMD, &command) == 0)
+            m_stage = 3;
+    }
+
+    if (m_stage == 3)
+    {
+        struct v4l2_format vfmt;
+        memset(&vfmt, 0, sizeof(vfmt));
+        vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+            
+        VERBOSE(VB_RECORD, LOC + "hd-pvr waiting for valid resolution");
+        if ((ioctl(videofd, VIDIOC_G_FMT, &vfmt) == 0) && vfmt.fmt.pix.width)
+            m_stage = 4;
+    }
+
+    return (m_stage == 4);
 }
 
 void AnalogSignalMonitor::UpdateValues(void)
@@ -33,19 +102,24 @@ void AnalogSignalMonitor::UpdateValues(v
         return;
 
     bool isLocked = false;
-    if (usingv4l2)
+    if (m_usingv4l2)
     {
-        struct v4l2_tuner tuner;
-        bzero(&tuner, sizeof(tuner));
-
-        if (ioctl(videofd, VIDIOC_G_TUNER, &tuner, 0) < 0)
-        {
-            VERBOSE(VB_IMPORTANT,
-                    LOC_ERR + "Failed to probe signal (v4l2)" + ENO);
-        }
+        if (m_driver == "hdpvr")
+            isLocked = handleHDPVR(videofd);
         else
         {
-            isLocked = tuner.signal;
+            struct v4l2_tuner tuner;
+            bzero(&tuner, sizeof(tuner));
+
+            if (ioctl(videofd, VIDIOC_G_TUNER, &tuner, 0) < 0)
+            {
+                VERBOSE(VB_IMPORTANT,
+                        LOC_ERR + "Failed to probe signal (v4l2)" + ENO);
+            }
+            else
+            {
+                isLocked = tuner.signal;
+            }
         }
     }
     else 
Index: libs/libmythtv/analogsignalmonitor.h
===================================================================
--- libs/libmythtv/analogsignalmonitor.h.orig
+++ libs/libmythtv/analogsignalmonitor.h
@@ -19,7 +19,13 @@ class AnalogSignalMonitor : public Signa
     virtual void UpdateValues(void);
 
   private:
-    bool usingv4l2;
+    bool handleHDPVR(int videofd);
+
+    bool     m_usingv4l2;
+    QString  m_card;
+    QString  m_driver;
+    uint32_t m_version;
+    int      m_stage;
 };
 
 #endif // _ANALOG_SIGNAL_MONITOR_H_
Index: libs/libmythtv/mpegrecorder.cpp
===================================================================
--- libs/libmythtv/mpegrecorder.cpp.orig
+++ libs/libmythtv/mpegrecorder.cpp
@@ -1041,14 +1041,6 @@ void MpegRecorder::StartRecording(void)
 
     if (driver == "hdpvr")
     {
-        if (curRecording->recgroup == "LiveTV")
-        {
-            // Don't bother checking resolution, always use best bitrate
-            int maxbitrate = std::max(high_mpeg4peakbitrate,
-                                      high_mpeg4avgbitrate);
-            SetBitrate(high_mpeg4avgbitrate, maxbitrate, "LiveTV");
-        }
-
         int progNum = 1;
         MPEGStreamData *sd = new MPEGStreamData(progNum, true);
         sd->SetRecordingType(_recording_type);
@@ -1492,7 +1484,7 @@ bool MpegRecorder::StartEncoding(int fd)
     memset(&command, 0, sizeof(struct v4l2_encoder_cmd));
     command.cmd = V4L2_ENC_CMD_START;
 
-    if (driver == "hdpvr" && curRecording->recgroup != "LiveTV")
+    if (driver == "hdpvr")
         HandleResolutionChanges();
 
     VERBOSE(VB_RECORD, LOC + "StartEncoding");
@@ -1615,64 +1607,6 @@ void MpegRecorder::HandleSingleProgramPM
         DTVRecorder::BufferedWrite(*(reinterpret_cast<TSPacket*>(&buf[i])));
 }
 
-/// After a resolution change, it can take the HD-PVR a few
-/// seconds before it is usable again.
-bool MpegRecorder::WaitFor_HDPVR(void)
-{
-    struct v4l2_encoder_cmd command;
-    struct v4l2_format vfmt;
-    struct pollfd polls;
-    int    idx;
-
-    // Tell it to start encoding, then wait for it to actually feed us
-    // some data.
-    QMutexLocker locker(&start_stop_encoding_lock);
-
-    // Sleep any less than 1.5 seconds, and the HD-PVR will
-    // return the old resolution, when the resolution is changing.
-    usleep(1500 * 1000);
-
-    memset(&command, 0, sizeof(struct v4l2_encoder_cmd));
-    command.cmd = V4L2_ENC_CMD_START;
-
-    for (idx = 0; idx < 20; ++idx)
-    {
-        if (ioctl(readfd, VIDIOC_ENCODER_CMD, &command) == 0)
-            break;
-        usleep(100 * 1000);
-    }
-
-    if (idx == 20)
-        return false;
-
-    polls.fd      = readfd;
-    polls.events  = POLLIN;
-    polls.revents = 0;
-
-    if (poll(&polls, 1, 5000) <= 0)
-        return false;
-
-    // HD-PVR should now be "ready"
-    command.cmd = V4L2_ENC_CMD_STOP;
-
-    if (ioctl(readfd, VIDIOC_ENCODER_CMD, &command) < 0)
-        return false;
-
-    memset(&vfmt, 0, sizeof(vfmt));
-    vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
-    for (idx = 0; idx < 20; ++idx)
-    {
-        if (0 == ioctl(chanfd, VIDIOC_G_FMT, &vfmt))
-            return true;
-        // Typically takes 0.9 seconds after a resolution change
-        usleep(100 * 1000);
-    }
-
-    VERBOSE(VB_RECORD, LOC + "WaitForHDPVR failed");
-    return false;
-}
-
 void MpegRecorder::SetBitrate(int bitrate, int maxbitrate,
                               const QString & reason)
 {
@@ -1710,9 +1644,6 @@ void MpegRecorder::HandleResolutionChang
     memset(&vfmt, 0, sizeof(vfmt));
     vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
             
-    if (driver == "hdpvr")
-        WaitFor_HDPVR();
-
     if (0 == ioctl(chanfd, VIDIOC_G_FMT, &vfmt))
     {
         VERBOSE(VB_RECORD, LOC + QString("Got Resolution %1x%2")
Index: libs/libmythtv/mpegrecorder.h
===================================================================
--- libs/libmythtv/mpegrecorder.h.orig
+++ libs/libmythtv/mpegrecorder.h
@@ -86,7 +86,6 @@ class MpegRecorder : public DTVRecorder,
 
     void ResetForNewFile(void);
 
-    bool WaitFor_HDPVR(void);
     void SetBitrate(int bitrate, int maxbitrate, const QString & reason);
     void HandleResolutionChanges(void);
 
Index: libs/libmythtv/signalmonitor.h
===================================================================
--- libs/libmythtv/signalmonitor.h.orig
+++ libs/libmythtv/signalmonitor.h
@@ -287,6 +287,7 @@ inline bool SignalMonitor::IsRequired(co
     return (CardUtil::IsDVBCardType(cardtype) ||
             (cardtype.toUpper() == "HDTV")      ||
             (cardtype.toUpper() == "HDHOMERUN") ||
+            (cardtype.toUpper() == "HDPVR") ||
             (cardtype.toUpper() == "FIREWIRE")  ||
             (cardtype.toUpper() == "FREEBOX"));
 }
@@ -295,6 +296,7 @@ inline bool SignalMonitor::IsSupported(c
 {
     return (IsRequired(cardtype)        ||
             (cardtype.toUpper() == "V4L") ||
+            (cardtype.toUpper() == "HDPVR") ||
             (cardtype.toUpper() == "MPEG"));
 }