record timing information from a single PID
[enigma2.git] / lib / dvb / esection.cpp
1 #include <lib/dvb/esection.h>
2 #include <lib/base/eerror.h>
3
4 void eGTable::sectionRead(const __u8 *d)
5 {
6         unsigned int last_section_number = d[7];
7         m_table.flags &= ~eDVBTableSpec::tfAnyVersion;
8         m_table.flags |= eDVBTableSpec::tfThisVersion;
9         m_table.version = (d[5]>>1)&0x1F;
10
11                 /* if a section is missing, we retry reading the
12                    whole data up to 5 times. if after that the
13                    section is still missing, we timeout. */
14         if (m_tries > 5 * (last_section_number+1))
15         {
16                 timeout();
17                 return;
18         }
19         
20         m_tries++;
21
22         if (createTable(d[6], d, last_section_number + 1))
23         {
24                 if (m_timeout)
25                         m_timeout->stop();
26                 m_reader->stop();
27                 ready = 1;
28                 tableReady(error);
29         } else if ((m_table.flags & eDVBTableSpec::tfHaveTimeout) && m_timeout)
30                 m_timeout->start(m_table.timeout, 1); // reset timeout
31 }
32
33 void eGTable::timeout()
34 {
35         eDebug("timeout!");
36         m_reader->stop();
37         ready = 1;
38         error = -1;
39         tableReady(error);
40 }
41
42 eGTable::eGTable():
43                 m_timeout(0), error(0)
44 {
45 }
46
47 DEFINE_REF(eGTable);
48
49 RESULT eGTable::start(iDVBSectionReader *reader, const eDVBTableSpec &table)
50 {
51         RESULT res;
52         m_table = table;
53
54         m_reader = reader;
55         m_reader->connectRead(slot(*this, &eGTable::sectionRead), m_sectionRead_conn);
56         
57         m_tries = 0;
58         
59         // setup filter struct
60         eDVBSectionFilterMask mask;
61         
62         memset(&mask, 0, sizeof(mask));
63         mask.pid   = m_table.pid;
64         mask.flags = 0;
65         
66         if (m_table.flags & eDVBTableSpec::tfCheckCRC)
67                 mask.flags |= eDVBSectionFilterMask::rfCRC;
68         
69         if (m_table.flags & eDVBTableSpec::tfHaveTID)
70         {
71                 mask.data[0] = m_table.tid;
72                 if (m_table.flags & eDVBTableSpec::tfHaveTIDMask)
73                         mask.mask[0] = m_table.tid_mask;
74                 else
75                         mask.mask[0] = 0xFF;
76         }
77
78         if (m_table.flags & eDVBTableSpec::tfHaveTIDExt)
79         {
80                 mask.data[1] = m_table.tidext >> 8;
81                 mask.data[2] = m_table.tidext;
82                 if (m_table.flags & eDVBTableSpec::tfHaveTIDExtMask)
83                 {
84                         mask.mask[1] = m_table.tidext_mask >> 8;
85                         mask.mask[2] = m_table.tidext_mask;
86                 }
87                 else
88                 {
89                         mask.mask[1] = 0xFF;
90                         mask.mask[2] = 0xFF;
91                 }
92         }
93         
94         if (!(m_table.flags & eDVBTableSpec::tfAnyVersion))
95         {
96                 eDebug("doing version filtering");
97                 mask.data[3] |= (m_table.version << 1)|1;
98                 mask.mask[3] |= 0x3f;
99                 if (!(m_table.flags & eDVBTableSpec::tfThisVersion))
100                         mask.mode[3] |= 0x3e; // negative filtering
101         } else
102                 eDebug("no version filtering");
103         
104         eDebug("%04x:  %02x %02x %02x %02x %02x %02x",
105                 mask.pid,
106                 mask.data[0], mask.data[1], mask.data[2],
107                 mask.data[3], mask.data[4], mask.data[5]);
108         eDebug("mask:  %02x %02x %02x %02x %02x %02x",
109                 mask.mask[0], mask.mask[1], mask.mask[2],
110                 mask.mask[3], mask.mask[4], mask.mask[5]);
111         eDebug("mode:  %02x %02x %02x %02x %02x %02x",
112                 mask.mode[0], mask.mode[1], mask.mode[2],
113                 mask.mode[3], mask.mode[4], mask.mode[5]);
114
115         if ((res = m_reader->start(mask)))
116         {
117                 eDebug("reader failed to start.");
118                 return res;
119         }
120         
121         if (m_table.flags & eDVBTableSpec::tfHaveTimeout)
122         {
123                 if (m_timeout)
124                         delete m_timeout;
125                 m_timeout = new eTimer(eApp);
126                 m_timeout->start(m_table.timeout, 1); // begin timeout
127                 CONNECT(m_timeout->timeout, eGTable::timeout);
128         }
129         
130         return 0;
131 }
132
133 RESULT eGTable::start(iDVBDemux *demux, const eDVBTableSpec &table)
134 {
135         int res;
136         ePtr<iDVBSectionReader> reader;
137         res = demux->createSectionReader(eApp, reader);
138         if (res)
139                 return res;
140         return start(reader, table);
141 }
142
143 eGTable::~eGTable()
144 {
145         if (m_timeout)
146                 delete m_timeout;
147 }
148
149 void eAUGTable::slotTableReady(int error)
150 {
151         getNext(error);
152 }