Merge branch 'master' into obi/master
[enigma2.git] / lib / dvb / esection.h
1 #ifndef __esection_h
2 #define __esection_h
3
4 #include <lib/dvb/idemux.h>
5 #include <set>
6
7 #define TABLE_eDebug(x...) do { if (m_debug) eDebug(x); } while(0)
8 #define TABLE_eDebugNoNewLine(x...) do { if (m_debug) eDebugNoNewLine(x); } while(0)
9
10 class eGTable: public iObject, public Object
11 {
12         DECLARE_REF(eGTable);
13         ePtr<iDVBSectionReader> m_reader;
14         eDVBTableSpec m_table;
15         
16         unsigned int m_tries;
17         
18         ePtr<eTimer> m_timeout;
19
20         void sectionRead(const __u8 *data);
21         void timeout();
22         ePtr<eConnection> m_sectionRead_conn;
23 protected:
24         bool m_debug;
25         virtual int createTable(unsigned int nr, const __u8 *data, unsigned int max)=0;
26 public:
27         Signal1<void, int> tableReady;
28         eGTable(bool debug=true);
29         RESULT start(iDVBSectionReader *reader, const eDVBTableSpec &table);
30         RESULT start(iDVBDemux *reader, const eDVBTableSpec &table);
31         RESULT getSpec(eDVBTableSpec &spec) { spec = m_table; return 0; }
32         virtual ~eGTable();
33         int error;
34         int ready;
35 };
36
37 template <class Section>
38 class eTable: public eGTable
39 {
40 private:
41         std::vector<Section*> sections;
42         std::set<int> avail;
43 protected:
44         int createTable(unsigned int nr, const __u8 *data, unsigned int max)
45         {
46                 unsigned int ssize = sections.size();
47                 if (max < ssize || nr >= max)
48                 {
49                         TABLE_eDebug("kaputt max(%d) < ssize(%d) || nr(%d) >= max(%d)",
50                                 max, ssize, nr, max);
51                         return 0;
52                 }
53                 if (avail.find(nr) != avail.end())
54                         delete sections[nr];
55
56                 sections.resize(max);
57                 sections[nr] = new Section(data);
58                 avail.insert(nr);
59
60                 for (unsigned int i = 0; i < max; ++i)
61                         if (avail.find(i) != avail.end())
62                                 TABLE_eDebugNoNewLine("+");
63                         else
64                                 TABLE_eDebugNoNewLine("-");
65                                 
66                 TABLE_eDebug(" %d/%d TID %02x", avail.size(), max, data[0]);
67
68                 if (avail.size() == max)
69                 {
70                         TABLE_eDebug("done!");
71                         return 1;
72                 } else
73                         return 0;
74         }
75 public:
76         std::vector<Section*> &getSections() { return sections; }
77         eTable(bool debug=true): eGTable(debug)
78         {
79         }
80         ~eTable()
81         {
82                 for (std::set<int>::iterator i(avail.begin()); i != avail.end(); ++i)
83                         delete sections[*i];
84         }
85 };
86
87 class eAUGTable: public Object
88 {
89 protected:
90         void slotTableReady(int);
91 public:
92         Signal1<void, int> tableReady;
93         virtual void getNext(int err)=0;
94 };
95
96 template <class Table>
97 class eAUTable: public eAUGTable
98 {
99         ePtr<Table> current, next;              // current is READY AND ERRORFREE, next is not yet ready
100         int first;
101         ePtr<iDVBDemux> m_demux;
102         eMainloop *ml;
103 public:
104
105         eAUTable()
106         {
107         }
108
109         ~eAUTable()
110         {
111                 stop();
112         }
113         
114         void stop()
115         {
116                 current = next = 0;
117                 m_demux = 0;
118         }
119         
120         int begin(eMainloop *m, const eDVBTableSpec &spec, ePtr<iDVBDemux> demux)
121         {
122                 ml = m;
123                 m_demux = demux;
124                 first= 1;
125                 current = 0;
126                 next = new Table();
127                 CONNECT(next->tableReady, eAUTable::slotTableReady);
128                 next->start(demux, spec);
129                 return 0;
130         }
131         
132         int get()
133         {
134                 if (current)
135                 {
136                         /*emit*/ tableReady(0);
137                         return 0;
138                 } else if (!next)
139                 {
140                         /*emit*/ tableReady(-1);
141                         return 0;
142                 } else
143                         return 1;
144         }
145         
146         RESULT getCurrent(ePtr<Table> &ptr)
147         {
148                 if (!current)
149                         return -1;
150                 ptr = current;
151                 return 0;
152         }
153
154 #if 0   
155         void abort()
156         {
157                 eDebug("eAUTable: aborted!");
158                 if (next)
159                         next->abort();
160                 delete next;
161                 next=0;
162         }
163 #endif
164
165         int ready()
166         {
167                 return !!current;
168         }
169         
170         void inject(Table *t)
171         {
172                 next=t;
173                 getNext(0);
174         }
175
176         void getNext(int error)
177         {
178                 current = 0;
179                 if (error)
180                 {
181                         next=0;
182                         if (first)
183                                 /*emit*/ tableReady(error);
184                         first=0;
185                         return;
186                 } else
187                         current=next;
188
189                 next=0;
190                 first=0;
191                 
192                 ASSERT(current->ready);
193                         
194                 /*emit*/ tableReady(0);
195                 
196                 eDVBTableSpec spec;
197
198                 if (current && (!current->getSpec(spec)))
199                 {
200                         next = new Table();
201                         CONNECT(next->tableReady, eAUTable::slotTableReady);
202                         spec.flags &= ~(eDVBTableSpec::tfAnyVersion|eDVBTableSpec::tfThisVersion|eDVBTableSpec::tfHaveTimeout);
203                         next->eGTable::start(m_demux, spec);
204                 }
205         }
206 };
207
208 #endif