vdr  1.7.31
sections.c
Go to the documentation of this file.
1 /*
2  * sections.c: Section data handling
3  *
4  * See the main source file 'vdr.c' for copyright information and
5  * how to reach the author.
6  *
7  * $Id: sections.c 2.1 2012/08/26 12:53:39 kls Exp $
8  */
9 
10 #include "sections.h"
11 #include <unistd.h>
12 #include "channels.h"
13 #include "device.h"
14 #include "thread.h"
15 
16 // --- cFilterHandle----------------------------------------------------------
17 
18 class cFilterHandle : public cListObject {
19 public:
21  int handle;
22  int used;
23  cFilterHandle(const cFilterData &FilterData);
24  };
25 
27 {
28  filterData = FilterData;
29  handle = -1;
30  used = 0;
31 }
32 
33 // --- cSectionHandlerPrivate ------------------------------------------------
34 
36 public:
38  };
39 
40 // --- cSectionHandler -------------------------------------------------------
41 
43 :cThread("section handler")
44 {
46  device = Device;
47  statusCount = 0;
48  on = false;
49  waitForLock = false;
51  Start();
52 }
53 
55 {
56  Cancel(3);
57  cFilter *fi;
58  while ((fi = filters.First()) != NULL)
59  Detach(fi);
60  delete shp;
61 }
62 
64 {
65  return shp->channel.Source();
66 }
67 
69 {
70  return shp->channel.Transponder();
71 }
72 
74 {
75  return &shp->channel;
76 }
77 
78 void cSectionHandler::Add(const cFilterData *FilterData)
79 {
80  Lock();
81  statusCount++;
82  cFilterHandle *fh;
83  for (fh = filterHandles.First(); fh; fh = filterHandles.Next(fh)) {
84  if (fh->filterData.Is(FilterData->pid, FilterData->tid, FilterData->mask))
85  break;
86  }
87  if (!fh) {
88  int handle = device->OpenFilter(FilterData->pid, FilterData->tid, FilterData->mask);
89  if (handle >= 0) {
90  fh = new cFilterHandle(*FilterData);
91  fh->handle = handle;
92  filterHandles.Add(fh);
93  }
94  }
95  if (fh)
96  fh->used++;
97  Unlock();
98 }
99 
100 void cSectionHandler::Del(const cFilterData *FilterData)
101 {
102  Lock();
103  statusCount++;
104  cFilterHandle *fh;
105  for (fh = filterHandles.First(); fh; fh = filterHandles.Next(fh)) {
106  if (fh->filterData.Is(FilterData->pid, FilterData->tid, FilterData->mask)) {
107  if (--fh->used <= 0) {
108  device->CloseFilter(fh->handle);
109  filterHandles.Del(fh);
110  break;
111  }
112  }
113  }
114  Unlock();
115 }
116 
118 {
119  Lock();
120  statusCount++;
121  filters.Add(Filter);
122  Filter->sectionHandler = this;
123  if (on)
124  Filter->SetStatus(true);
125  Unlock();
126 }
127 
129 {
130  Lock();
131  statusCount++;
132  Filter->SetStatus(false);
133  Filter->sectionHandler = NULL;
134  filters.Del(Filter, false);
135  Unlock();
136 }
137 
139 {
140  Lock();
141  shp->channel = Channel ? *Channel : cChannel();
142  Unlock();
143 }
144 
146 {
147  Lock();
148  if (on != On) {
149  if (!On || device->HasLock()) {
150  statusCount++;
151  for (cFilter *fi = filters.First(); fi; fi = filters.Next(fi)) {
152  fi->SetStatus(false);
153  if (On)
154  fi->SetStatus(true);
155  }
156  on = On;
157  waitForLock = false;
158  }
159  else
160  waitForLock = On;
161  }
162  Unlock();
163 }
164 
166 {
167  SetPriority(19);
168  while (Running()) {
169 
170  Lock();
171  if (waitForLock)
172  SetStatus(true);
173  int NumFilters = filterHandles.Count();
174  pollfd pfd[NumFilters];
175  for (cFilterHandle *fh = filterHandles.First(); fh; fh = filterHandles.Next(fh)) {
176  int i = fh->Index();
177  pfd[i].fd = fh->handle;
178  pfd[i].events = POLLIN;
179  pfd[i].revents = 0;
180  }
181  int oldStatusCount = statusCount;
182  Unlock();
183 
184  if (poll(pfd, NumFilters, 1000) > 0) {
185  bool DeviceHasLock = device->HasLock();
186  if (!DeviceHasLock)
187  cCondWait::SleepMs(100);
188  for (int i = 0; i < NumFilters; i++) {
189  if (pfd[i].revents & POLLIN) {
190  cFilterHandle *fh = NULL;
191  LOCK_THREAD;
192  if (statusCount != oldStatusCount)
193  break;
194  for (fh = filterHandles.First(); fh; fh = filterHandles.Next(fh)) {
195  if (pfd[i].fd == fh->handle)
196  break;
197  }
198  if (fh) {
199  // Read section data:
200  unsigned char buf[4096]; // max. allowed size for any EIT section
201  int r = device->ReadFilter(fh->handle, buf, sizeof(buf));
202  if (!DeviceHasLock)
203  continue; // we do the read anyway, to flush any data that might have come from a different transponder
204  if (r > 3) { // minimum number of bytes necessary to get section length
205  int len = (((buf[1] & 0x0F) << 8) | (buf[2] & 0xFF)) + 3;
206  if (len == r) {
207  // Distribute data to all attached filters:
208  int pid = fh->filterData.pid;
209  int tid = buf[0];
210  for (cFilter *fi = filters.First(); fi; fi = filters.Next(fi)) {
211  if (fi->Matches(pid, tid))
212  fi->Process(pid, tid, buf, len);
213  }
214  }
215  else if (time(NULL) - lastIncompleteSection > 10) { // log them only every 10 seconds
216  dsyslog("read incomplete section - len = %d, r = %d", len, r);
217  lastIncompleteSection = time(NULL);
218  }
219  }
220  }
221  }
222  }
223  }
224  }
225 }