MythTV  master
dvbcam.cpp
Go to the documentation of this file.
1 /*
2  * Class DVBCam
3  *
4  * Original Project
5  * MythTV http://www.mythtv.org
6  *
7  * Author(s):
8  * Jesper Sorensen
9  * - Changed to work with Taylor Jacob's DVB rewrite
10  * Kenneth Aafloy
11  * - General Implementation
12  *
13  * Description:
14  * This Class has been developed from bits n' pieces of other
15  * projects.
16  *
17  *
18  *
19  * This program is free software; you can redistribute it and/or
20  * modify it under the terms of the GNU General Public License
21  * as published by the Free Software Foundation; either version 2
22  * of the License, or (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program; if not, write to the Free Software
31  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
32  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
33  *
34  */
35 
36 #include <cstdio>
37 #include <cstdlib>
38 #include <iostream>
39 #include <map>
40 #include <utility>
41 #include <vector>
42 
43 using namespace std;
44 
45 #include <fcntl.h>
46 #include <sys/ioctl.h>
47 #include <sys/poll.h>
48 #include <linux/dvb/ca.h>
49 
50 #include <dvbci.h>
51 
52 #include "recorderbase.h"
53 
54 #include "cardutil.h"
55 
56 #include "dvbcam.h"
57 #include "mthread.h"
58 #include "dvbchannel.h"
59 #include "dvbrecorder.h"
60 #include "mythlogging.h"
61 
62 #define LOC QString("DVB#%1 CA: ").arg(m_device)
63 
64 DVBCam::DVBCam(QString aDevice)
65  : m_device(std::move(aDevice))
66 {
67  QString dvbdev = CardUtil::GetDeviceName(DVB_DEV_CA, m_device);
68  QByteArray dev = dvbdev.toLatin1();
69  int cafd = open(dev.constData(), O_RDWR);
70  if (cafd >= 0)
71  {
72  ca_caps_t caps;
73  // slot_num will be uninitialised if ioctl fails
74  if (ioctl(cafd, CA_GET_CAP, &caps) >= 0)
75  m_numslots = caps.slot_num;
76  else
77  LOG(VB_GENERAL, LOG_ERR, "ioctl CA_GET_CAP failed: " + ENO);
78 
79  close(cafd);
80  }
81 }
82 
84 {
85  Stop();
86 }
87 
88 bool DVBCam::Start(void)
89 {
90  if (m_numslots == 0)
91  return false;
92 
93  m_havePmt = false;
94  m_pmtSent = false;
95  m_pmtUpdated = false;
96  m_pmtAdded = false;
97 
98  QString dvbdev = CardUtil::GetDeviceName(DVB_DEV_CA, m_device);
99  QByteArray dev = dvbdev.toLatin1();
100  m_ciHandler = cCiHandler::CreateCiHandler(dev.constData());
101  if (!m_ciHandler)
102  {
103  LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to initialize CI handler");
104  return false;
105  }
106 
107  QMutexLocker locker(&m_ciHandlerLock);
108  m_ciHandlerDoRun = true;
109  m_ciHandlerThread = new MThread("DVBCam", this);
112  m_ciHandlerWait.wait(locker.mutex(), 1000);
113 
114  if (m_ciHandlerRunning)
115  LOG(VB_DVBCAM, LOG_INFO, LOC + "CI handler successfully initialized!");
116 
117  return m_ciHandlerRunning;
118 }
119 
120 bool DVBCam::Stop(void)
121 {
122  {
123  QMutexLocker locker(&m_ciHandlerLock);
124  if (m_ciHandlerRunning)
125  {
126  m_ciHandlerDoRun = false;
127  locker.unlock();
129  locker.relock();
130  delete m_ciHandlerThread;
131  m_ciHandlerThread = nullptr;
132  }
133 
134  if (m_ciHandler)
135  {
136  delete m_ciHandler;
137  m_ciHandler = nullptr;
138  }
139  }
140 
141  QMutexLocker locker(&m_pmtLock);
142  pmt_list_t::iterator it;
143 
144  for (it = m_pmtList.begin(); it != m_pmtList.end(); ++it)
145  delete *it;
146  m_pmtList.clear();
147 
148  for (it = m_pmtAddList.begin(); it != m_pmtAddList.end(); ++it)
149  delete *it;
150  m_pmtAddList.clear();
151 
152  return true;
153 }
154 
156 {
158  if (enq != nullptr)
159  {
160  if (enq->Text() != nullptr)
161  LOG(VB_DVBCAM, LOG_INFO, LOC + QString("CAM: Received message: %1")
162  .arg(enq->Text()));
163  delete enq;
164  }
165 
167  if (menu != nullptr)
168  {
169  if (menu->TitleText() != nullptr)
170  LOG(VB_DVBCAM, LOG_INFO, LOC + QString("CAM: Menu Title: %1")
171  .arg(menu->TitleText()));
172  if (menu->SubTitleText() != nullptr)
173  LOG(VB_DVBCAM, LOG_INFO, LOC + QString("CAM: Menu SubTitle: %1")
174  .arg(menu->SubTitleText()));
175  if (menu->BottomText() != nullptr)
176  LOG(VB_DVBCAM, LOG_INFO, LOC + QString("CAM: Menu BottomText: %1")
177  .arg(menu->BottomText()));
178 
179  for (int i=0; i<menu->NumEntries(); i++)
180  if (menu->Entry(i) != nullptr)
181  LOG(VB_DVBCAM, LOG_INFO, LOC + QString("CAM: Menu Entry: %1")
182  .arg(menu->Entry(i)));
183 
184  if (menu->Selectable())
185  {
186  LOG(VB_CHANNEL, LOG_INFO, LOC + "CAM: Menu is selectable");
187  }
188 
189  if (menu->NumEntries() > 0)
190  {
191  LOG(VB_DVBCAM, LOG_INFO, LOC + "CAM: Selecting first entry");
192  menu->Select(0);
193  }
194  else
195  {
196  LOG(VB_DVBCAM, LOG_INFO, LOC + "CAM: Cancelling menu");
197  }
198 
199  delete menu;
200  }
201 }
202 
204 {
205  LOG(VB_DVBCAM, LOG_INFO, LOC + "CiHandler needs CA_PMT");
206  QMutexLocker locker(&m_pmtLock);
207 
208  if (m_pmtSent && m_pmtAdded && !m_pmtUpdated)
209  {
210  // Send added PMT
211  while (!m_pmtAddList.empty())
212  {
213  pmt_list_t::iterator it = m_pmtAddList.begin();
214  const ChannelBase *chan = it.key();
215  ProgramMapTable *pmt = (*it);
216  m_pmtList[chan] = pmt;
217  m_pmtAddList.erase(it);
218  SendPMT(*pmt, CPLM_ADD);
219  }
220 
221  m_pmtUpdated = false;
222  m_pmtAdded = false;
223  return;
224  }
225 
226  // Grab any added PMT
227  while (!m_pmtAddList.empty())
228  {
229  pmt_list_t::iterator it = m_pmtAddList.begin();
230  const ChannelBase *chan = it.key();
231  ProgramMapTable *pmt = (*it);
232  m_pmtList[chan] = pmt;
233  m_pmtAddList.erase(it);
234  }
235 
236  uint length = m_pmtList.size();
237  uint count = 0;
238 
239  pmt_list_t::const_iterator pmtit;
240  for (pmtit = m_pmtList.begin(); pmtit != m_pmtList.end(); ++pmtit)
241  {
242  uint cplm = (count == 0) ? CPLM_FIRST : CPLM_MORE;
243  cplm = (count + 1 == length) ? CPLM_LAST : cplm;
244  cplm = (length == 1) ? CPLM_ONLY : cplm;
245 
246  SendPMT(**pmtit, cplm);
247 
248  count++;
249  }
250 
251  m_pmtSent = true;
252  m_pmtUpdated = false;
253  m_pmtAdded = false;
254 }
255 
256 void DVBCam::run(void)
257 {
258  LOG(VB_DVBCAM, LOG_INFO, LOC + "CI handler thread running");
259 
260  QMutexLocker locker(&m_ciHandlerLock);
261  m_ciHandlerRunning = true;
262 
263  while (m_ciHandlerDoRun)
264  {
265  locker.unlock();
266  if (m_ciHandler->Process())
267  {
268  if (m_ciHandler->HasUserIO())
269  HandleUserIO();
270 
271  bool handle_pmt = m_pmtSent && (m_pmtUpdated || m_pmtAdded);
272  handle_pmt |= m_havePmt && m_ciHandler->NeedCaPmt();
273 
274  if (handle_pmt)
275  HandlePMT();
276  }
277  locker.relock();
278  m_ciHandlerWait.wait(locker.mutex(), 10);
279  }
280 
281  m_ciHandlerRunning = false;
282  LOG(VB_DVBCAM, LOG_INFO, LOC + "CiHandler thread stopped");
283 }
284 
285 void DVBCam::SetPMT(const ChannelBase *chan, const ProgramMapTable *pmt)
286 {
287  QMutexLocker locker(&m_pmtLock);
288 
289  pmt_list_t::iterator it = m_pmtList.find(chan);
290  pmt_list_t::iterator it2 = m_pmtAddList.find(chan);
291  if (!pmt && (it != m_pmtList.end()))
292  {
293  delete *it;
294  m_pmtList.erase(it);
295  m_pmtUpdated = true;
296  }
297  else if (!pmt && (it2 != m_pmtAddList.end()))
298  {
299  delete *it2;
300  m_pmtAddList.erase(it2);
301  m_pmtAdded = !m_pmtAddList.empty();
302  }
303  else if (pmt && (m_pmtList.empty() || (it != m_pmtList.end())))
304  {
305  if (it != m_pmtList.end())
306  delete *it;
307  m_pmtList[chan] = new ProgramMapTable(*pmt);
308  m_havePmt = true;
309  m_pmtUpdated = true;
310  }
311  else if (pmt && (it == m_pmtList.end()))
312  {
313  m_pmtAddList[chan] = new ProgramMapTable(*pmt);
314  m_pmtAdded = true;
315  }
316 }
317 
318 void DVBCam::SetTimeOffset(double offset_in_seconds)
319 {
320  QMutexLocker locker(&m_ciHandlerLock);
321  if (m_ciHandler)
322  m_ciHandler->SetTimeOffset(offset_in_seconds);
323 }
324 
325 static const char *cplm_info[] =
326 {
327  "CPLM_MORE",
328  "CPLM_FIRST",
329  "CPLM_LAST",
330  "CPLM_ONLY",
331  "CPLM_ADD",
332  "CPLM_UPDATE"
333 };
334 
335 cCiCaPmt CreateCAPMT(const ProgramMapTable& /*pmt*/, const unsigned short* /*casids*/, uint /*cplm*/);
336 
337 /*
338  * Send a CA_PMT object to the CAM (see EN50221, section 8.4.3.4)
339  */
340 void DVBCam::SendPMT(const ProgramMapTable &pmt, uint cplm)
341 {
342  bool success = false;
343 
344  for (uint s = 0; s < (uint)m_ciHandler->NumSlots(); s++)
345  {
346  const unsigned short *casids = m_ciHandler->GetCaSystemIds(s);
347 
348  if (!casids)
349  {
350  LOG(success ? VB_DVBCAM : VB_GENERAL, LOG_ERR,
351  LOC + "GetCaSystemIds returned NULL! " +
352  QString("(Slot #%1)").arg(s));
353  continue;
354  }
355 
356  if (!casids[0])
357  {
358  LOG(success ? VB_DVBCAM : VB_GENERAL, LOG_ERR,
359  LOC + "CAM supports no CA systems! " +
360  QString("(Slot #%1)").arg(s));
361  continue;
362  }
363 
364  LOG(VB_DVBCAM, LOG_INFO, LOC +
365  QString("Creating CA_PMT, ServiceID = %1")
366  .arg(pmt.ProgramNumber()));
367 
368  cCiCaPmt capmt = CreateCAPMT(pmt, casids, cplm);
369 
370  LOG(VB_DVBCAM, LOG_INFO, LOC +
371  QString("Sending CA_PMT with %1 to CI slot #%2")
372  .arg(cplm_info[cplm]).arg(s));
373 
374  if (!m_ciHandler->SetCaPmt(capmt, s))
375  LOG(success ? VB_DVBCAM : VB_GENERAL, LOG_ERR,
376  LOC + "CA_PMT send failed!");
377  else
378  success = true;
379  }
380 }
381 
382 static void process_desc(cCiCaPmt &capmt,
383  const unsigned short *casids,
384  const desc_list_t &desc)
385 {
386  desc_list_t::const_iterator it;
387  for (it = desc.begin(); it != desc.end(); ++it)
388  {
390  for (uint q = 0; casids[q]; q++)
391  {
392  if (!cad.IsValid() || cad.SystemID() != casids[q])
393  continue;
394 
395  LOG(VB_DVBCAM, LOG_INFO, QString("DVBCam: Adding CA descriptor: "
396  "CASID(0x%2), ECM PID(0x%3)")
397  .arg(cad.SystemID(),0,16).arg(cad.PID(),0,16));
398 
399  capmt.AddCaDescriptor(cad.SystemID(), cad.PID(),
400  cad.DataSize(), cad.Data());
401  }
402  }
403 
404 }
405 
407  const unsigned short *casids,
408  uint cplm)
409 {
410  cCiCaPmt capmt(pmt.ProgramNumber(), cplm);
411 
412  // Add CA descriptors for the service
414  pmt.ProgramInfo(), pmt.ProgramInfoLength(),
416 
417  process_desc(capmt, casids, gdesc);
418 
419  // Add elementary streams + CA descriptors
420  for (uint i = 0; i < pmt.StreamCount(); i++)
421  {
422  LOG(VB_DVBCAM, LOG_INFO,
423  QString("DVBCam: Adding elementary stream: %1, pid(0x%2)")
424  .arg(pmt.StreamDescription(i, "dvb"))
425  .arg(pmt.StreamPID(i),0,16));
426 
427  capmt.AddElementaryStream(pmt.StreamType(i), pmt.StreamPID(i));
428 
430  pmt.StreamInfo(i), pmt.StreamInfoLength(i),
432 
433  process_desc(capmt, casids, desc);
434  }
435  return capmt;
436 }
void start(QThread::Priority=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:294
#define LOC
Definition: dvbcam.cpp:62
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
virtual int NumSlots(void)=0
#define CPLM_LAST
Definition: dvbci.h:118
bool m_pmtAdded
Definition: dvbcam.h:63
static cCiHandler * CreateCiHandler(const char *FileName)
Definition: dvbci.cpp:1556
QMutex m_pmtLock
Definition: dvbcam.h:57
cCiCaPmt CreateCAPMT(const ProgramMapTable &, const unsigned short *, uint)
Definition: dvbcam.cpp:406
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:311
int m_numslots
Definition: dvbcam.h:48
virtual bool NeedCaPmt(void)=0
cCiHandler * m_ciHandler
Definition: dvbcam.h:54
bool Start(void)
Definition: dvbcam.cpp:88
vector< const unsigned char * > desc_list_t
static MythThemedMenu * menu
virtual bool HasUserIO(void)=0
static QString GetDeviceName(dvb_dev_type_t, const QString &device)
bool m_havePmt
Definition: dvbcam.h:60
bool Stop(void)
Definition: dvbcam.cpp:120
pmt_list_t m_pmtList
Definition: dvbcam.h:58
static const char * cplm_info[]
Definition: dvbcam.cpp:325
void AddCaDescriptor(int ca_system_id, int ca_pid, int data_len, const uint8_t *data)
Definition: dvbci.cpp:1498
~DVBCam()
Definition: dvbcam.cpp:83
uint StreamInfoLength(uint i) const
Definition: mpegtables.h:708
uint StreamType(uint i) const
Definition: mpegtables.h:702
static void process_desc(cCiCaPmt &capmt, const unsigned short *casids, const desc_list_t &desc)
Definition: dvbcam.cpp:382
void HandleUserIO(void)
Definition: dvbcam.cpp:155
uint StreamPID(uint i) const
Definition: mpegtables.h:705
virtual cCiEnquiry * GetEnquiry(void)=0
#define close
Definition: compat.h:16
QString m_device
Definition: dvbcam.h:47
#define CPLM_FIRST
Definition: dvbci.h:117
uint StreamCount(void) const
Definition: mpegtables.h:714
virtual bool SetCaPmt(cCiCaPmt &CaPmt, int Slot)=0
const unsigned char * Data(void) const
bool m_pmtUpdated
Definition: dvbcam.h:62
const unsigned char * StreamInfo(uint i) const
Definition: mpegtables.h:711
virtual const unsigned short * GetCaSystemIds(int Slot)=0
const char * Text(void)
Definition: dvbci.h:107
virtual cCiMenu * GetMenu(void)=0
bool m_ciHandlerDoRun
Definition: dvbcam.h:52
#define CPLM_ADD
Definition: dvbci.h:120
pmt_list_t m_pmtAddList
Definition: dvbcam.h:59
void SetTimeOffset(double offset_in_seconds)
Definition: dvbcam.cpp:318
unsigned int uint
Definition: compat.h:140
#define ENO
This can be appended to the LOG args with "+".
Definition: mythlogging.h:99
virtual void SetTimeOffset(double)
Definition: dvbci.h:156
void run(void) override
Definition: dvbcam.cpp:256
void HandlePMT(void)
Definition: dvbcam.cpp:203
#define CPLM_MORE
Definition: dvbci.h:116
#define CPLM_ONLY
Definition: dvbci.h:119
QMutex m_ciHandlerLock
Definition: dvbcam.h:50
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QString StreamDescription(uint i, const QString &sistandard) const
Returns a better (and more expensive) string representation of type at stream index i than StreamType...
Abstract class providing a generic interface to tuning hardware.
Definition: channelbase.h:31
DVBCam(QString device)
Definition: dvbcam.cpp:64
QWaitCondition m_ciHandlerWait
Definition: dvbcam.h:51
uint ProgramNumber(void) const
Definition: mpegtables.h:693
Definition: dvbci.h:72
bool m_pmtSent
Definition: dvbcam.h:61
uint ProgramInfoLength(void) const
Definition: mpegtables.h:696
bool m_ciHandlerRunning
Definition: dvbcam.h:53
bool IsValid(void) const
virtual bool Process(void)=0
A PMT table maps a program described in the ProgramAssociationTable to various PID's which describe t...
Definition: mpegtables.h:656
void SendPMT(const ProgramMapTable &pmt, uint cplm)
Definition: dvbcam.cpp:340
MThread * m_ciHandlerThread
Definition: dvbcam.h:55
const unsigned char * ProgramInfo(void) const
Definition: mpegtables.h:699
static desc_list_t ParseOnlyInclude(const unsigned char *data, uint len, int excluded_descid)
void SetPMT(const ChannelBase *chan, const ProgramMapTable *pmt)
Definition: dvbcam.cpp:285