MythTV  master
mhi.cpp
Go to the documentation of this file.
1 #include "mhi.h"
2 
3 #include <QRegion>
4 #include <QVector>
5 #include <QUrl>
6 #include <QPoint> // for QPoint
7 #include <QRgb> // for QRgb
8 #include <QVariant> // for QVariant
9 #include <QByteArray> // for QByteArray
10 #include <QStringList> // for QStringList
11 #include <QTime> // for QTime
12 #include <QHash> // for QHash
13 
14 #include <algorithm> // for min
15 #include <cmath> // for round, sqrt
16 #include <cstdint> // for uint8_t
17 #include <cstring> // for memcpy, memset
18 #include <deque> // for _Deque_iterator, operator!=
19 
20 #include "dsmcc.h" // for Dsmcc
21 #include "interactivetv.h" // for InteractiveTV
22 #include "interactivescreen.h"
23 #include "mythpainter.h"
24 #include "mythimage.h"
25 #include "mythuiimage.h"
26 #include "mythdirs.h"
27 #include "mythlogging.h"
28 #include "mythmainwindow.h"
29 #include "mythavutil.h"
30 #include "mthread.h" // for MThread
31 #include "mythcorecontext.h" // for MythCoreContext, etc
32 #include "mythdb.h" // for MythDB
33 #include "mythdbcon.h" // for MSqlQuery
34 #include "mythevent.h" // for MythEvent
35 #include "mythplayer.h" // for MythPlayer
36 #include "mythrect.h" // for MythRect
37 #include "mythuiactions.h" // for ACTION_0, ACTION_1, etc
38 #include "tv_actions.h" // for ACTION_MENUTEXT, etc
39 
40 extern "C" {
41 #include "libavutil/imgutils.h"
42 }
43 
44 static bool ft_loaded = false;
45 static FT_Library ft_library;
46 
47 #define FONT_WIDTHRES 54
48 #define FONT_HEIGHTRES 72 // 1 pixel per point
49 #define FONT_TO_USE "FreeSans.ttf" // Tiresias Screenfont.ttf is mandated
50 
51 
52 // LifecycleExtension tuneinfo:
53 const unsigned kTuneQuietly = 1U<<0; // b0 tune quietly
54 const unsigned kTuneKeepApp = 1U<<1; // b1 keep app running
55 const unsigned kTuneCarId = 1U<<2; // b2 carousel id in bits 8..16
56 const unsigned kTuneCarReset = 1U<<3; // b3 get carousel id from gateway info
57 //const unsigned kTuneBcastDisa = 1U<<4; // b4 broadcaster_interrupt disable
58 // b5..7 reserverd
59 // b8..15 carousel id
60 const unsigned kTuneKeepChnl = 1U<<16; // Keep current channel
61 // b17..31 reserved
62 
67 {
68  public:
69  QImage m_image;
70  int m_x {0};
71  int m_y {0};
72  bool m_bUnder {false};
73 };
74 
76  : m_parent(parent), m_dsmcc(new Dsmcc()),
77  m_engine(MHCreateEngine(this))
78 {
79  if (!ft_loaded)
80  {
81  FT_Error error = FT_Init_FreeType(&ft_library);
82  if (!error)
83  ft_loaded = true;
84  }
85 
86  if (ft_loaded)
87  {
88  // TODO: We need bold and italic versions.
89  if (LoadFont(FONT_TO_USE))
90  m_faceLoaded = true;
91  }
92 }
93 
94 // Load the font. Copied, generally, from OSD::LoadFont.
95 bool MHIContext::LoadFont(const QString& name)
96 {
97  QString fullnameA = GetConfDir() + "/" + name;
98  QByteArray fnameA = fullnameA.toLatin1();
99  FT_Error errorA = FT_New_Face(ft_library, fnameA.constData(), 0, &m_face);
100  if (!errorA)
101  return true;
102 
103  QString fullnameB = GetFontsDir() + name;
104  QByteArray fnameB = fullnameB.toLatin1();
105  FT_Error errorB = FT_New_Face(ft_library, fnameB.constData(), 0, &m_face);
106  if (!errorB)
107  return true;
108 
109  QString fullnameC = GetShareDir() + "themes/" + name;
110  QByteArray fnameC = fullnameC.toLatin1();
111  FT_Error errorC = FT_New_Face(ft_library, fnameC.constData(), 0, &m_face);
112  if (!errorC)
113  return true;
114 
115  const QString& fullnameD = name;
116  QByteArray fnameD = fullnameD.toLatin1();
117  FT_Error errorD = FT_New_Face(ft_library, fnameD.constData(), 0, &m_face);
118  if (!errorD)
119  return true;
120 
121  LOG(VB_GENERAL, LOG_ERR, QString("[mhi] Unable to find font: %1").arg(name));
122  return false;
123 }
124 
126 {
127  StopEngine();
128  delete(m_engine);
129  delete(m_dsmcc);
130  if (m_faceLoaded) FT_Done_Face(m_face);
131 
132  ClearDisplay();
133  ClearQueue();
134 }
135 
136 // NB caller must hold m_display_lock
138 {
139  for (auto & it : m_display)
140  delete it;
141  m_display.clear();
142  m_videoDisplayRect = QRect();
143 }
144 
145 // NB caller must hold m_dsmccLock
147 {
148  for (auto & it : m_dsmccQueue)
149  delete it;
150  m_dsmccQueue.clear();
151 }
152 
153 // Ask the engine to stop and block until it has.
155 {
156  if (nullptr == m_engineThread)
157  return;
158 
159  m_stop = true;
160  m_runLock.lock();
161  m_engine_wait.wakeAll();
162  m_runLock.unlock();
163 
164  m_engineThread->wait();
165  delete m_engineThread;
166  m_engineThread = nullptr;
167 }
168 
169 
170 // Start or restart the MHEG engine.
171 void MHIContext::Restart(int chanid, int sourceid, bool isLive)
172 {
173  int tuneinfo = m_tuneInfo.isEmpty() ? 0 : m_tuneInfo.takeFirst();
174 
175  LOG(VB_MHEG, LOG_INFO,
176  QString("[mhi] Restart ch=%1 source=%2 live=%3 tuneinfo=0x%4")
177  .arg(chanid).arg(sourceid).arg(isLive).arg(tuneinfo,0,16));
178 
179  if (m_currentSource != sourceid)
180  {
181  m_currentSource = sourceid;
182  QMutexLocker locker(&m_channelMutex);
183  m_channelCache.clear();
184  }
185  m_currentStream = (chanid) ? chanid : -1;
186  if (!(tuneinfo & kTuneKeepChnl))
188 
189  if (tuneinfo & kTuneKeepApp)
190  {
191  // We have tuned to the channel in order to find the streams.
192  // Leave the MHEG engine running but restart the DSMCC carousel.
193  // This is a bit of a mess but it's the only way to be able to
194  // select streams from a different channel.
195  {
196  QMutexLocker locker(&m_dsmccLock);
197  if (tuneinfo & kTuneCarReset)
198  m_dsmcc->Reset();
199  ClearQueue();
200  }
201 
202  if (tuneinfo & (kTuneCarReset|kTuneCarId))
203  {
204  QMutexLocker locker(&m_runLock);
205  m_engine->EngineEvent(10); // NonDestructiveTuneOK
206  }
207  }
208  else
209  {
210  StopEngine();
211 
212  m_audioTag = -1;
213  m_videoTag = -1;
214 
215  {
216  QMutexLocker locker(&m_dsmccLock);
217  m_dsmcc->Reset();
218  ClearQueue();
219  }
220 
221  {
222  QMutexLocker locker(&m_keyLock);
223  m_keyQueue.clear();
224  }
225 
226  m_engine->SetBooting();
227  ClearDisplay();
228  m_updated = true;
229  m_stop = false;
230  m_isLive = isLive;
231  // Don't set the NBI version here. Restart is called
232  // after the PMT is processed.
233  m_engineThread = new MThread("MHEG", this);
235  }
236 }
237 
238 void MHIContext::run(void)
239 {
240  QMutexLocker locker(&m_runLock);
241 
242  while (!m_stop)
243  {
244  int toWait = 0;
245  // Dequeue and process any key presses.
246  int key = 0;
247  do
248  {
251  {
252  QMutexLocker locker2(&m_keyLock);
253  key = m_keyQueue.dequeue();
254  }
255 
256  if (key != 0)
258 
259  // Run the engine and find out how long to pause.
260  toWait = m_engine->RunAll();
261  if (toWait < 0)
262  return;
263  } while (key != 0);
264 
265  toWait = (toWait > 1000 || toWait <= 0) ? 1000 : toWait;
266 
267  if (!m_stop && (toWait > 0))
268  m_engine_wait.wait(locker.mutex(), toWait);
269  }
270 }
271 
272 // Dequeue and process any DSMCC packets.
274 {
275  DSMCCPacket *packet = nullptr;
276  do
277  {
278  QMutexLocker locker(&m_dsmccLock);
279  packet = m_dsmccQueue.dequeue();
280  if (packet)
281  {
283  packet->m_data, packet->m_length,
284  packet->m_componentTag, packet->m_carouselId,
285  packet->m_dataBroadcastId);
286 
287  delete packet;
288  }
289  } while (packet);
290 }
291 
293  unsigned char *data, int length, int componentTag,
294  unsigned carouselId, int dataBroadcastId)
295 {
296  auto *dataCopy = (unsigned char*) malloc(length * sizeof(unsigned char));
297 
298  if (dataCopy == nullptr)
299  return;
300 
301  memcpy(dataCopy, data, length*sizeof(unsigned char));
302  {
303  QMutexLocker locker(&m_dsmccLock);
304  m_dsmccQueue.enqueue(new DSMCCPacket(dataCopy, length,
305  componentTag, carouselId,
306  dataBroadcastId));
307  }
308  m_engine_wait.wakeAll();
309 }
310 
311 // A NetworkBootInfo sub-descriptor is present in the PMT.
312 void MHIContext::SetNetBootInfo(const unsigned char *data, uint length)
313 {
314  if (length < 2) // A valid descriptor should always have at least 2 bytes.
315  return;
316 
317  LOG(VB_MHEG, LOG_INFO, QString("[mhi] SetNetBootInfo version %1 mode %2 len %3")
318  .arg(data[0]).arg(data[1]).arg(length));
319 
320  QMutexLocker locker(&m_dsmccLock);
321  // The carousel should be reset now as the stream has changed
322  m_dsmcc->Reset();
323  ClearQueue();
324  // Save the data from the descriptor.
325  m_nbiData.resize(0);
326  m_nbiData.reserve(length);
327  m_nbiData.insert(m_nbiData.begin(), data, data+length);
328  // If there is no Network Boot Info or we're setting it
329  // for the first time just update the "last version".
331  m_lastNbiVersion = data[0];
332  else
333  m_engine_wait.wakeAll();
334 }
335 
336 // Called only by m_engineThread
338 {
339  QMutexLocker locker(&m_dsmccLock);
340  if (m_nbiData.size() >= 2 && m_nbiData[0] != m_lastNbiVersion)
341  {
342  m_lastNbiVersion = m_nbiData[0]; // Update the saved version
343  switch (m_nbiData[1])
344  {
345  case 1:
346  m_dsmcc->Reset();
347  m_engine->SetBooting();
348  locker.unlock();
349  {QMutexLocker locker2(&m_displayLock);
350  ClearDisplay();
351  m_updated = true;}
352  break;
353  case 2:
354  m_engine->EngineEvent(9); // NetworkBootInfo EngineEvent
355  break;
356  default:
357  LOG(VB_MHEG, LOG_INFO, QString("[mhi] Unknown NetworkBoot type %1")
358  .arg(m_nbiData[1]));
359  break;
360  }
361  }
362 }
363 
364 // Called by the engine to check for the presence of an object in the carousel.
365 bool MHIContext::CheckCarouselObject(QString objectPath)
366 {
367  if (objectPath.startsWith("http:") || objectPath.startsWith("https:"))
368  {
369  QByteArray cert;
370 
371  // Verify access to server
372  if (!CheckAccess(objectPath, cert))
373  return false;
374 
375  return m_ic.CheckFile(objectPath, cert);
376  }
377 
378  QStringList path = objectPath.split(QChar('/'), QString::SkipEmptyParts);
379  QByteArray result; // Unused
380  QMutexLocker locker(&m_dsmccLock);
381  int res = m_dsmcc->GetDSMCCObject(path, result);
382  return res == 0; // It's available now.
383 }
384 
385 bool MHIContext::GetDSMCCObject(const QString &objectPath, QByteArray &result)
386 {
387  QStringList path = objectPath.split(QChar('/'), QString::SkipEmptyParts);
388  QMutexLocker locker(&m_dsmccLock);
389  int res = m_dsmcc->GetDSMCCObject(path, result);
390  return (res == 0);
391 }
392 
393 bool MHIContext::CheckAccess(const QString &objectPath, QByteArray &cert)
394 {
395  cert.clear();
396 
397  // Verify access to server
398  QByteArray servers;
399  if (!GetDSMCCObject("/auth.servers", servers))
400  {
401  LOG(VB_MHEG, LOG_INFO, QString(
402  "[mhi] CheckAccess(%1) No auth.servers").arg(objectPath) );
403  return false;
404  }
405 
406  QByteArray host = QUrl(objectPath).host().toLocal8Bit();
407  if (!servers.contains(host))
408  {
409  LOG(VB_MHEG, LOG_INFO, QString("[mhi] CheckAccess(%1) Host not known")
410  .arg(objectPath) );
411  LOG(VB_MHEG, LOG_DEBUG, QString("[mhi] Permitted servers: %1")
412  .arg(servers.constData()) );
413 
414  // BUG: https://securegate.iplayer.bbc.co.uk is not listed
415  if (!objectPath.startsWith("https:"))
416  return false;
417  }
418 
419  if (!objectPath.startsWith("https:"))
420  return true;
421 
422  // Use TLS cert from carousel file auth.tls.<x>
423  if (!GetDSMCCObject("/auth.tls.1", cert))
424  return false;
425 
426  // The cert has a 5 byte header: 16b cert_count + 24b cert_len
427  cert = cert.mid(5);
428  return true;
429 }
430 
431 // Called by the engine to request data from the carousel.
432 // Caller must hold m_runLock
433 bool MHIContext::GetCarouselData(QString objectPath, QByteArray &result)
434 {
435  QByteArray cert;
436  bool const isIC = objectPath.startsWith("http:") || objectPath.startsWith("https:");
437  if (isIC)
438  {
439  // Verify access to server
440  if (!CheckAccess(objectPath, cert))
441  return false;
442  }
443 
444  // Get the path components. The string will normally begin with "//"
445  // since this is an absolute path but that will be removed by split.
446  QStringList path = objectPath.split(QChar('/'), QString::SkipEmptyParts);
447  // Since the DSMCC carousel and the MHEG engine are currently on the
448  // same thread this is safe. Otherwise we need to make a deep copy of
449  // the result.
450 
451  bool bReported = false;
452  QElapsedTimer t; t.start();
453  while (!m_stop)
454  {
455  if (isIC)
456  {
457  switch (m_ic.GetFile(objectPath, result, cert))
458  {
460  if (bReported)
461  LOG(VB_MHEG, LOG_INFO, QString("[mhi] Received %1").arg(objectPath));
462  return true;
464  if (bReported)
465  LOG(VB_MHEG, LOG_INFO, QString("[mhi] Not found %1").arg(objectPath));
466  return false;
468  break;
469  }
470  }
471  else
472  {
473  QMutexLocker locker(&m_dsmccLock);
474  int res = m_dsmcc->GetDSMCCObject(path, result);
475  if (res == 0)
476  {
477  if (bReported)
478  LOG(VB_MHEG, LOG_INFO, QString("[mhi] Received %1").arg(objectPath));
479  return true; // Found it
480  }
481  // NB don't exit if -1 (not present) is returned as the object may
482  // arrive later. Exiting can cause the inital app to not be found
483  }
484 
485  if (t.hasExpired(60000)) // TODO get this from carousel info
486  {
487  if (bReported)
488  LOG(VB_MHEG, LOG_INFO, QString("[mhi] timed out %1").arg(objectPath));
489  return false; // Not there.
490  }
491  // Otherwise we block.
492  if (!bReported)
493  {
494  bReported = true;
495  LOG(VB_MHEG, LOG_INFO, QString("[mhi] Waiting for %1").arg(objectPath));
496  }
497  // Process DSMCC packets then block for a while or until we receive
498  // some more packets. We should eventually find out if this item is
499  // present.
501  m_engine_wait.wait(&m_runLock, 300);
502  }
503  return false; // Stop has been set. Say the object isn't present.
504 }
505 
506 // Mapping from key name & UserInput register to UserInput EventData
508 {
509  using key_t = QPair< QString, int /*UserInput register*/ >;
510 
511 public:
512  MHKeyLookup();
513 
514  int Find(const QString &name, int reg) const
515  { return m_map.value(key_t(name,reg), 0); }
516 
517 private:
518  void key(const QString &name, int code, int r1,
519  int r2=0, int r3=0, int r4=0, int r5=0, int r6=0, int r7=0, int r8=0, int r9=0);
520 
521  QHash<key_t,int /*EventData*/ > m_map;
522 };
523 
524 void MHKeyLookup::key(const QString &name, int code, int r1,
525  int r2, int r3, int r4, int r5, int r6, int r7, int r8, int r9)
526 {
527  if (r1 > 0)
528  m_map.insert(key_t(name,r1), code);
529  if (r2 > 0)
530  m_map.insert(key_t(name,r2), code);
531  if (r3 > 0)
532  m_map.insert(key_t(name,r3), code);
533  if (r4 > 0)
534  m_map.insert(key_t(name,r4), code);
535  if (r5 > 0)
536  m_map.insert(key_t(name,r5), code);
537  if (r6 > 0)
538  m_map.insert(key_t(name,r6), code);
539  if (r7 > 0)
540  m_map.insert(key_t(name,r7), code);
541  if (r8 > 0)
542  m_map.insert(key_t(name,r8), code);
543  if (r9 > 0)
544  m_map.insert(key_t(name,r9), code);
545 }
546 
548 {
549  // Use a modification of the standard key mapping for RC's with a single
550  // stop button which is used for both Esc and TEXTEXIT (Back).
551  // This mapping doesn't pass Esc to the MHEG app in registers 3 or 5 and
552  // hence allows the user to exit playback when the red button icon is shown
553  QStringList keylist = GET_KEY("TV Playback", "TEXTEXIT").split(QChar(','));
554  bool strict = !keylist.contains("Esc", Qt::CaseInsensitive);
555 
556  // This supports the UK and NZ key profile registers.
557  // The UK uses 3, 4 and 5 and NZ 13, 14 and 15. These are
558  // similar but the NZ profile also provides an EPG key.
559  // ETSI ES 202 184 V2.2.1 (2011-03) adds group 6 for ICE.
560  // The BBC use group 7 for ICE
561  key(ACTION_UP, 1, 4,5,6,7,14,15);
562  key(ACTION_DOWN, 2, 4,5,6,7,14,15);
563  key(ACTION_LEFT, 3, 4,5,6,7,14,15);
564  key(ACTION_RIGHT, 4, 4,5,6,7,14,15);
565  key(ACTION_0, 5, 4,6,7,14);
566  key(ACTION_1, 6, 4,6,7,14);
567  key(ACTION_2, 7, 4,6,7,14);
568  key(ACTION_3, 8, 4,6,7,14);
569  key(ACTION_4, 9, 4,6,7,14);
570  key(ACTION_5, 10, 4,6,7,14);
571  key(ACTION_6, 11, 4,6,7,14);
572  key(ACTION_7, 12, 4,6,7,14);
573  key(ACTION_8, 13, 4,6,7,14);
574  key(ACTION_9, 14, 4,6,7,14);
575  key(ACTION_SELECT, 15, 4,5,6,7,14,15);
576  key(ACTION_TEXTEXIT, 16, strict ? 3 : 0,4,strict ? 5 : 0,6,7,13,14,15); // 16= Cancel
577  // 17= help
578  // 18..99 reserved by DAVIC
579  key(ACTION_MENURED, 100, 3,4,5,6,7,13,14,15);
580  key(ACTION_MENUGREEN, 101, 3,4,5,6,7,13,14,15);
581  key(ACTION_MENUYELLOW, 102, 3,4,5,6,7,13,14,15);
582  key(ACTION_MENUBLUE, 103, 3,4,5,6,7,13,14,15);
583  key(ACTION_MENUTEXT, 104, 3,4,5,6,7);
584  key(ACTION_MENUTEXT, 105, 13,14,15); // NB from original Myth code
585  // 105..119 reserved for future spec
586  key(ACTION_STOP, 120, 6,7);
587  key(ACTION_PLAY, 121, 6,7);
588  key(ACTION_PAUSE, 122, 6,7);
589  key(ACTION_JUMPFFWD, 123, 6,7); // 123= Skip Forward
590  key(ACTION_JUMPRWND, 124, 6,7); // 124= Skip Back
591 #if 0 // These conflict with left & right
592  key(ACTION_SEEKFFWD, 125, 6,7); // 125= Fast Forward
593  key(ACTION_SEEKRWND, 126, 6,7); // 126= Rewind
594 #endif
595  key(ACTION_PLAYBACK, 127, 6,7);
596  // 128..256 reserved for future spec
597  // 257..299 vendor specific
598  key(ACTION_MENUEPG, 300, 13,14,15);
599  // 301.. Vendor specific
600 }
601 
602 // Called from tv_play when a key is pressed.
603 // If it is one in the current profile we queue it for the engine
604 // and return true otherwise we return false.
605 bool MHIContext::OfferKey(const QString& key)
606 {
607  static const MHKeyLookup kKeymap;
608  int action = kKeymap.Find(key, m_keyProfile);
609  if (action == 0)
610  return false;
611 
612  LOG(VB_GENERAL, LOG_INFO, QString("[mhi] Adding MHEG key %1:%2:%3")
613  .arg(key).arg(action).arg(m_keyQueue.size()) );
614  { QMutexLocker locker(&m_keyLock);
616  m_engine_wait.wakeAll();
617  return true;
618 }
619 
620 // Called from MythPlayer::VideoStart and MythPlayer::ReinitOSD
621 void MHIContext::Reinit(const QRect &videoRect, const QRect &dispRect, float aspect)
622 {
623  LOG(VB_MHEG, LOG_INFO,
624  QString("[mhi] Reinit video(y:%1 x:%2 w:%3 h:%4) "
625  "vis(y:%5 x:%6 w:%7 h:%8) aspect=%9")
626  .arg(videoRect.y()).arg(videoRect.x())
627  .arg(videoRect.width()).arg(videoRect.height())
628  .arg(dispRect.y()).arg(dispRect.x())
629  .arg(dispRect.width()).arg(dispRect.height()).arg(aspect));
630  m_videoDisplayRect = QRect();
631 
632  // MHEG presumes square pixels
633  enum { kNone, kHoriz, kBoth };
634  int mode = gCoreContext->GetNumSetting("MhegAspectCorrection", kNone);
635  auto const aspectd = static_cast<double>(aspect);
636  double const vz = (mode == kBoth) ? min(1.15, 1. / sqrt(aspectd)) : 1.;
637  double const hz = (mode > kNone) ? vz * aspectd : 1.;
638 
639  m_displayRect = QRect( int(dispRect.width() * (1 - hz) / 2),
640  int(dispRect.height() * (1 - vz) / 2),
641  int(dispRect.width() * hz), int(dispRect.height() * vz) );
642  m_videoRect = QRect( dispRect.x() + m_displayRect.x(),
643  dispRect.y() + int(dispRect.height() * (1 - hz) / 2),
644  int(dispRect.width() * hz), int(dispRect.height() * hz) );
645 }
646 
648 {
649  LOG(VB_MHEG, LOG_INFO, QString("[mhi] SetInputRegister %1").arg(num));
650  QMutexLocker locker(&m_keyLock);
651  m_keyQueue.clear();
652  m_keyProfile = num;
653 }
654 
656 {
657  // 0= Active, 1= Inactive, 2= Disabled
659 }
660 
661 // Called by the video player to redraw the image.
663  MythPainter *osdPainter)
664 {
665  if (!osdWindow || !osdPainter)
666  return;
667 
668  QMutexLocker locker(&m_displayLock);
669 
670  // In MHEG the video is just another item in the display stack
671  // but when we create the OSD we overlay everything over the video.
672  // We need to cut out anything belowthe video on the display stack
673  // to leave the video area clear.
674  auto it = m_display.begin();
675  for (; it != m_display.end(); ++it)
676  {
677  MHIImageData *data = *it;
678  if (!data->m_bUnder)
679  continue;
680 
681  QRect imageRect(data->m_x, data->m_y,
682  data->m_image.width(), data->m_image.height());
683  if (!m_videoDisplayRect.intersects(imageRect))
684  continue;
685 
686  // Replace this item with a set of cut-outs.
687  it = m_display.erase(it);
688 
689 #if QT_VERSION < QT_VERSION_CHECK(5, 8, 0)
690  QVector<QRect> rects =
691  (QRegion(imageRect) - QRegion(m_videoDisplayRect)).rects();
692  for (uint j = 0; j < (uint)rects.size(); j++)
693  {
694  const QRect &rect = rects[j];
695 #else
696  for (const QRect& rect : QRegion(imageRect)-QRegion(m_videoDisplayRect))
697  {
698 #endif
699  QImage image =
700  data->m_image.copy(rect.x()-data->m_x, rect.y()-data->m_y,
701  rect.width(), rect.height());
702  auto *newData = new MHIImageData;
703  newData->m_image = image;
704  newData->m_x = rect.x();
705  newData->m_y = rect.y();
706  newData->m_bUnder = true;
707  it = m_display.insert(it, newData);
708  ++it;
709  }
710  --it;
711  delete data;
712  }
713 
714  m_updated = false;
715  osdWindow->DeleteAllChildren();
716  // Copy all the display items into the display.
717  it = m_display.begin();
718  for (int count = 0; it != m_display.end(); ++it, count++)
719  {
720  MHIImageData *data = *it;
721  MythImage* image = osdPainter->GetFormatImage();
722  if (!image)
723  continue;
724 
725  image->Assign(data->m_image);
726  auto *uiimage = new MythUIImage(osdWindow, QString("itv%1").arg(count));
727  if (uiimage)
728  {
729  uiimage->SetImage(image);
730  uiimage->SetArea(MythRect(data->m_x, data->m_y,
731  data->m_image.width(), data->m_image.height()));
732  }
733  image->DecrRef();
734  }
735  osdWindow->OptimiseDisplayedArea();
736  // N.B. bypasses OSD class hence no expiry set
737  osdWindow->SetVisible(true);
738 }
739 
740 void MHIContext::GetInitialStreams(int &audioTag, int &videoTag) const
741 {
742  audioTag = m_audioTag;
743  videoTag = m_videoTag;
744 }
745 
746 
747 // An area of the screen/image needs to be redrawn.
748 // Called from the MHEG engine.
749 // We always redraw the whole scene.
750 void MHIContext::RequireRedraw(const QRegion & /*region*/)
751 {
752  m_updated = false;
753  m_displayLock.lock();
754  ClearDisplay();
755  m_displayLock.unlock();
756  // Always redraw the whole screen
758  m_updated = true;
759 }
760 
761 inline int MHIContext::ScaleX(int n, bool roundup) const
762 {
763  return (n * m_displayRect.width() + (roundup ? kStdDisplayWidth - 1 : 0)) / kStdDisplayWidth;
764 }
765 
766 inline int MHIContext::ScaleY(int n, bool roundup) const
767 {
768  return (n * m_displayRect.height() + (roundup ? kStdDisplayHeight - 1 : 0)) / kStdDisplayHeight;
769 }
770 
771 inline QRect MHIContext::Scale(const QRect &r) const
772 {
773  return { m_displayRect.topLeft() + QPoint(ScaleX(r.x()), ScaleY(r.y())),
774  QSize(ScaleX(r.width(), true), ScaleY(r.height(), true)) };
775 }
776 
777 inline int MHIContext::ScaleVideoX(int n, bool roundup) const
778 {
779  return (n * m_videoRect.width() + (roundup ? kStdDisplayWidth - 1 : 0)) / kStdDisplayWidth;
780 }
781 
782 inline int MHIContext::ScaleVideoY(int n, bool roundup) const
783 {
784  return (n * m_videoRect.height() + (roundup ? kStdDisplayHeight - 1 : 0)) / kStdDisplayHeight;
785 }
786 
787 inline QRect MHIContext::ScaleVideo(const QRect &r) const
788 {
789  return { m_videoRect.topLeft() + QPoint(ScaleVideoX(r.x()), ScaleVideoY(r.y())),
790  QSize(ScaleVideoX(r.width(), true), ScaleVideoY(r.height(), true)) };
791 }
792 
793 void MHIContext::AddToDisplay(const QImage &image, const QRect &displayRect, bool bUnder /*=false*/)
794 {
795  const QRect scaledRect = Scale(displayRect);
796 
797  auto *data = new MHIImageData;
798 
799  data->m_image = image.convertToFormat(QImage::Format_ARGB32).scaled(
800  scaledRect.width(), scaledRect.height(),
801  Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
802  data->m_x = scaledRect.x();
803  data->m_y = scaledRect.y();
804  data->m_bUnder = bUnder;
805 
806  QMutexLocker locker(&m_displayLock);
807  if (!bUnder)
808  m_display.push_back(data);
809  else
810  {
811  // Replace any existing items under the video with this
812  auto it = m_display.begin();
813  while (it != m_display.end())
814  {
815  MHIImageData *old = *it;
816  if (!old->m_bUnder)
817  ++it;
818  else
819  {
820  it = m_display.erase(it);
821  delete old;
822  }
823  }
824  m_display.push_front(data);
825  }
826 }
827 
828 inline int Roundup(int n, int r)
829 {
830  // NB assumes 2's complement arithmetic
831  return n + (-n & (r - 1));
832 }
833 
834 // The videoRect gives the size and position to which the video must be scaled.
835 // The displayRect gives the rectangle reserved for the video.
836 // e.g. part of the video may be clipped within the displayRect.
837 void MHIContext::DrawVideo(const QRect &videoRect, const QRect &dispRect)
838 {
839  // tell the video player to resize the video stream
840  if (m_parent->GetNVP())
841  {
842  QRect vidRect;
843  if (videoRect != QRect(QPoint(0,0),QSize(kStdDisplayWidth,kStdDisplayHeight)))
844  {
845  vidRect = ScaleVideo(videoRect);
846  vidRect.setWidth(Roundup(vidRect.width(), 2));
847  vidRect.setHeight(Roundup(vidRect.height(), 2));
848  }
849  m_parent->GetNVP()->SetVideoResize(vidRect);
850  }
851 
852  m_videoDisplayRect = Scale(dispRect);
853 
854  // Mark all existing items in the display stack as under the video
855  QMutexLocker locker(&m_displayLock);
856  for (auto & it : m_display)
857  it->m_bUnder = true;
858 }
859 
860 // Caller must hold m_channelMutex
862 {
864  query.prepare(
865  "SELECT networkid, serviceid, transportid, chanid "
866  "FROM channel, dtv_multiplex "
867  "WHERE channel.deleted IS NULL "
868  " AND channel.mplexid = dtv_multiplex.mplexid "
869  " AND channel.sourceid = dtv_multiplex.sourceid "
870  " AND channel.sourceid = :SOURCEID ;" );
871  query.bindValue(":SOURCEID", m_currentSource);
872  if (!query.exec())
873  {
874  MythDB::DBError("MHIContext::LoadChannelCache", query);
875  return false;
876  }
877  if (!query.isActive())
878  return false;
879  while (query.next())
880  {
881  int nid = query.value(0).toInt();
882  int sid = query.value(1).toInt();
883  int tid = query.value(2).toInt();
884  int cid = query.value(3).toInt();
885  m_channelCache.insertMulti( Key_t(nid, sid), Val_t(tid, cid) );
886  }
887  return true;
888 }
889 
890 // Tuning. Get the index corresponding to a given channel.
891 // The format of the service is dvb://netID.[transPortID].serviceID
892 // where the IDs are in hex.
893 // or rec://svc/lcn/N where N is the "logical channel number"
894 // i.e. the Freeview channel.
895 // Returns -1 if it cannot find it.
896 int MHIContext::GetChannelIndex(const QString &str)
897 {
898  int nResult = -1;
899 
900  do
901  {
902  if (str.startsWith("dvb://"))
903  {
904  QStringList list = str.mid(6).split('.');
905  if (list.size() != 3)
906  break; // Malformed.
907  // The various fields are expressed in hexadecimal.
908  // Convert them to decimal for the DB.
909  bool ok = false;
910  int netID = list[0].toInt(&ok, 16);
911  if (!ok)
912  break;
913  int transportID = !list[1].isEmpty() ? list[1].toInt(&ok, 16) : -1;
914  if (!ok)
915  break;
916  int serviceID = list[2].toInt(&ok, 16);
917  if (!ok)
918  break;
919 
920  QMutexLocker locker(&m_channelMutex);
921  if (m_channelCache.isEmpty())
923 
924  ChannelCache_t::const_iterator it = m_channelCache.find(
925  Key_t(netID,serviceID) );
926  if (it == m_channelCache.end())
927  break;
928  if (transportID < 0)
929  nResult = Cid(it);
930  else
931  {
932  do
933  {
934  if (Tid(it) == transportID)
935  {
936  nResult = Cid(it);
937  break;
938  }
939  }
940  while (++it != m_channelCache.end());
941  }
942  }
943  else if (str.startsWith("rec://svc/lcn/"))
944  {
945  // I haven't seen this yet so this is untested.
946  bool ok = false;
947  int channelNo = str.mid(14).toInt(&ok); // Decimal integer
948  if (!ok)
949  break;
951  query.prepare("SELECT chanid "
952  "FROM channel "
953  "WHERE deleted IS NULL AND "
954  " channum = :CHAN AND "
955  " channel.sourceid = :SOURCEID");
956  query.bindValue(":CHAN", channelNo);
957  query.bindValue(":SOURCEID", m_currentSource);
958  if (query.exec() && query.isActive() && query.next())
959  nResult = query.value(0).toInt();
960  }
961  else if (str == "rec://svc/cur")
963  else if (str == "rec://svc/def")
964  nResult = m_currentChannel;
965  else
966  {
967  LOG(VB_GENERAL, LOG_WARNING,
968  QString("[mhi] GetChannelIndex -- Unrecognized URL %1")
969  .arg(str));
970  }
971  }
972  while (false);
973 
974  LOG(VB_MHEG, LOG_INFO, QString("[mhi] GetChannelIndex %1 => %2")
975  .arg(str).arg(nResult));
976  return nResult;
977 
978 }
979 
980 // Get netId etc from the channel index. This is the inverse of GetChannelIndex.
981 bool MHIContext::GetServiceInfo(int channelId, int &netId, int &origNetId,
982  int &transportId, int &serviceId)
983 {
984  QMutexLocker locker(&m_channelMutex);
985  if (m_channelCache.isEmpty())
987 
988  for ( ChannelCache_t::const_iterator it = m_channelCache.begin();
989  it != m_channelCache.end(); ++it)
990  {
991  if (Cid(it) == channelId)
992  {
993  transportId = Tid(it);
994  netId = Nid(it);
995  origNetId = netId; // We don't have this in the database.
996  serviceId = Sid(it);
997  LOG(VB_MHEG, LOG_INFO, QString("[mhi] GetServiceInfo %1 => NID=%2 TID=%3 SID=%4")
998  .arg(channelId).arg(netId).arg(transportId).arg(serviceId));
999  return true;
1000  }
1001  }
1002 
1003  LOG(VB_MHEG, LOG_WARNING, QString("[mhi] GetServiceInfo %1 failed").arg(channelId));
1004  return false;
1005 }
1006 
1007 bool MHIContext::TuneTo(int channel, int tuneinfo)
1008 {
1009  if (!m_isLive)
1010  {
1011  LOG(VB_MHEG, LOG_WARNING, QString("[mhi] Can't TuneTo %1 0x%2 while not live")
1012  .arg(channel).arg(tuneinfo,0,16));
1013  return false; // Can't tune if this is a recording.
1014  }
1015 
1016  LOG(VB_GENERAL, LOG_INFO, QString("[mhi] TuneTo %1 0x%2")
1017  .arg(channel).arg(tuneinfo,0,16));
1018  m_tuneInfo.append(tuneinfo);
1019 
1020  // Post an event requesting a channel change.
1021  MythEvent me(QString("NETWORK_CONTROL CHANID %1").arg(channel));
1022  gCoreContext->dispatch(me);
1023  // Reset the NBI version here to prevent a reboot.
1024  QMutexLocker locker(&m_dsmccLock);
1026  m_nbiData.resize(0);
1027  return true;
1028 }
1029 
1030 
1031 // Begin playing the specified stream
1032 bool MHIContext::BeginStream(const QString &stream, MHStream *notify)
1033 {
1034  LOG(VB_MHEG, LOG_INFO, QString("[mhi] BeginStream %1 0x%2")
1035  .arg(stream).arg((quintptr)notify,0,16));
1036 
1037  m_audioTag = -1;
1038  m_videoTag = -1;
1039  m_notify = notify;
1040 
1041  if (stream.startsWith("http://") || stream.startsWith("https://"))
1042  {
1043  m_currentStream = -1;
1044 
1045  // The url is sometimes only http:// during stream startup
1046  if (QUrl(stream).authority().isEmpty())
1047  return false;
1048 
1049  return m_parent->GetNVP()->SetStream(stream);
1050  }
1051 
1052  int chan = GetChannelIndex(stream);
1053  if (chan < 0)
1054  return false;
1055  if (VERBOSE_LEVEL_CHECK(VB_MHEG, LOG_ANY))
1056  {
1057  int netId = 0;
1058  int origNetId = 0;
1059  int transportId = 0;
1060  int serviceId = 0;
1061  GetServiceInfo(chan, netId, origNetId, transportId, serviceId);
1062  }
1063 
1064  if (chan != m_currentStream)
1065  {
1066  // We have to tune to the channel where the stream is to be found.
1067  // Because the audio and video are both components of an MHEG stream
1068  // they will both be on the same channel.
1069  m_currentStream = chan;
1071  }
1072 
1073  return true;
1074 }
1075 
1077 {
1078  LOG(VB_MHEG, LOG_INFO, QString("[mhi] EndStream 0x%1")
1079  .arg((quintptr)m_notify,0,16) );
1080 
1081  m_notify = nullptr;
1082  (void)m_parent->GetNVP()->SetStream(QString());
1083 }
1084 
1085 // Callback from MythPlayer when a stream starts or stops
1086 bool MHIContext::StreamStarted(bool bStarted)
1087 {
1088  if (!m_engine || !m_notify)
1089  return false;
1090 
1091  LOG(VB_MHEG, LOG_INFO, QString("[mhi] Stream 0x%1 %2")
1092  .arg((quintptr)m_notify,0,16).arg(bStarted ? "started" : "stopped"));
1093 
1094  QMutexLocker locker(&m_runLock);
1095  m_engine->StreamStarted(m_notify, bStarted);
1096  if (!bStarted)
1097  m_notify = nullptr;
1098  return m_currentStream == -1; // Return true if it's an http stream
1099 }
1100 
1101 // Begin playing audio
1103 {
1104  LOG(VB_MHEG, LOG_INFO, QString("[mhi] BeginAudio %1").arg(tag));
1105 
1106  if (tag < 0)
1107  return true; // Leave it at the default.
1108 
1109  m_audioTag = tag;
1110  if (m_parent->GetNVP())
1111  return m_parent->GetNVP()->SetAudioByComponentTag(tag);
1112  return false;
1113  }
1114 
1115 // Stop playing audio
1117 {
1118  // Do nothing at the moment.
1119 }
1120 
1121 // Begin displaying video from the specified stream
1123 {
1124  LOG(VB_MHEG, LOG_INFO, QString("[mhi] BeginVideo %1").arg(tag));
1125 
1126  if (tag < 0)
1127  return true; // Leave it at the default.
1128 
1129  m_videoTag = tag;
1130  if (m_parent->GetNVP())
1131  return m_parent->GetNVP()->SetVideoByComponentTag(tag);
1132  return false;
1133 }
1134 
1135  // Stop displaying video
1137 {
1138  // Do nothing at the moment.
1139 }
1140 
1141 // Get current stream position, -1 if unknown
1143 {
1144  return m_parent->GetNVP() ? m_parent->GetNVP()->GetStreamPos() : -1;
1145 }
1146 
1147 // Get current stream size, -1 if unknown
1149 {
1150  return m_parent->GetNVP() ? m_parent->GetNVP()->GetStreamMaxPos() : -1;
1151 }
1152 
1153 // Set current stream position
1155 {
1156  return m_parent->GetNVP() ? m_parent->GetNVP()->SetStreamPos(pos) : -1;
1157 }
1158 
1159 // Play or pause a stream
1160 void MHIContext::StreamPlay(bool play)
1161 {
1162  if (m_parent->GetNVP())
1163  m_parent->GetNVP()->StreamPlay(play);
1164 }
1165 
1166 // Create a new object to draw dynamic line art.
1168  bool isBoxed, MHRgba lineColour, MHRgba fillColour)
1169 {
1170  return new MHIDLA(this, isBoxed, lineColour, fillColour);
1171 }
1172 
1173 // Create a new object to draw text.
1175 {
1176  return new MHIText(this);
1177 }
1178 
1179 // Create a new object to draw bitmaps.
1181 {
1182  return new MHIBitmap(this, tiled);
1183 }
1184 
1185 // Draw a rectangle. This is complicated if we want to get transparency right.
1186 void MHIContext::DrawRect(int xPos, int yPos, int width, int height,
1187  MHRgba colour)
1188 {
1189  if (colour.alpha() == 0 || height == 0 || width == 0)
1190  return; // Fully transparent
1191 
1192  QImage qImage(width, height, QImage::Format_ARGB32);
1193  qImage.fill(qRgba(colour.red(), colour.green(), colour.blue(), colour.alpha()));
1194 
1195  AddToDisplay(qImage, QRect(xPos, yPos, width, height));
1196 }
1197 
1198 // Draw an image at the specified position.
1199 // Generally the whole of the image is drawn but sometimes the
1200 // image may be clipped. x and y define the origin of the bitmap
1201 // and usually that will be the same as the origin of the bounding
1202 // box (clipRect).
1203 void MHIContext::DrawImage(int x, int y, const QRect &clipRect,
1204  const QImage &qImage, bool bScaled, bool bUnder)
1205 {
1206  if (qImage.isNull())
1207  return;
1208 
1209  QRect imageRect(x, y, qImage.width(), qImage.height());
1210  QRect displayRect = clipRect & imageRect;
1211 
1212  if (bScaled || displayRect == imageRect) // No clipping required
1213  {
1214  AddToDisplay(qImage, displayRect, bUnder);
1215  }
1216  else if (!displayRect.isEmpty())
1217  { // We must clip the image.
1218  QImage clipped = qImage.copy(displayRect.translated(-x, -y));
1219  AddToDisplay(clipped, displayRect, bUnder);
1220  }
1221  // Otherwise draw nothing.
1222 }
1223 
1224 // Fill in the background. This is only called if there is some area of
1225 // the screen that is not covered with other visibles.
1226 void MHIContext::DrawBackground(const QRegion &reg)
1227 {
1228  if (reg.isEmpty())
1229  return;
1230 
1231  QRect bounds = reg.boundingRect();
1232  DrawRect(bounds.x(), bounds.y(), bounds.width(), bounds.height(),
1233  MHRgba(0, 0, 0, 255)/* black. */);
1234 }
1235 
1236 void MHIText::Draw(int x, int y)
1237 {
1238  m_parent->DrawImage(x, y, QRect(x, y, m_width, m_height), m_image);
1239 }
1240 
1241 void MHIText::SetSize(int width, int height)
1242 {
1243  m_width = width;
1244  m_height = height;
1245 }
1246 
1247 void MHIText::SetFont(int size, bool isBold, bool isItalic)
1248 {
1249  m_fontSize = size;
1250  m_fontItalic = isItalic;
1251  m_fontBold = isBold;
1252  // TODO: Only the size is currently used.
1253  // Bold and Italic are currently ignored.
1254 }
1255 
1256 // FT sizes are in 26.6 fixed point form
1257 const int kShift = 6;
1258 static inline FT_F26Dot6 Point2FT(int pt)
1259 {
1260  return pt << kShift;
1261 }
1262 
1263 static inline int FT2Point(FT_F26Dot6 fp)
1264 {
1265  return (fp + (1<<(kShift-1))) >> kShift;
1266 }
1267 
1268 // Return the bounding rectangle for a piece of text drawn in the
1269 // current font. If maxSize is non-negative it sets strLen to the
1270 // number of characters that will fit in the space and returns the
1271 // bounds for those characters.
1272 // N.B. The box is relative to the origin so the y co-ordinate will
1273 // be negative. It's also possible that the x co-ordinate could be
1274 // negative for slanted fonts but that doesn't currently happen.
1275 QRect MHIText::GetBounds(const QString &str, int &strLen, int maxSize)
1276 {
1277  if (!m_parent->IsFaceLoaded())
1278  return {0,0,0,0};
1279 
1280  FT_Face face = m_parent->GetFontFace();
1281  FT_Error error = FT_Set_Char_Size(face, 0, Point2FT(m_fontSize),
1283  if (error)
1284  return {0,0,0,0};
1285 
1286  int maxAscent = face->size->metrics.ascender;
1287  int maxDescent = -face->size->metrics.descender;
1288  int width = 0;
1289  FT_Bool useKerning = FT_HAS_KERNING(face);
1290  FT_UInt previous = 0;
1291 
1292  for (int n = 0; n < strLen; n++)
1293  {
1294  QChar ch = str.at(n);
1295  FT_UInt glyphIndex = FT_Get_Char_Index(face, ch.unicode());
1296 
1297  if (glyphIndex == 0)
1298  {
1299  LOG(VB_MHEG, LOG_INFO, QString("[mhi] Unknown glyph 0x%1")
1300  .arg(ch.unicode(),0,16));
1301  previous = 0;
1302  continue;
1303  }
1304 
1305  int kerning = 0;
1306 
1307  if (useKerning && previous != 0)
1308  {
1309  FT_Vector delta;
1310  FT_Get_Kerning(face, previous, glyphIndex,
1311  FT_KERNING_DEFAULT, &delta);
1312  kerning = delta.x;
1313  }
1314 
1315  error = FT_Load_Glyph(face, glyphIndex, 0); // Don't need to render.
1316 
1317  if (error)
1318  continue; // ignore errors.
1319 
1320  FT_GlyphSlot slot = face->glyph; /* a small shortcut */
1321  FT_Pos advance = slot->metrics.horiAdvance + kerning;
1322 
1323  if (maxSize >= 0)
1324  {
1325  if (FT2Point(width + advance) > maxSize)
1326  {
1327  // There isn't enough space for this character.
1328  strLen = n;
1329  break;
1330  }
1331  }
1332  // Calculate the ascent and descent of this glyph.
1333  int descent = slot->metrics.height - slot->metrics.horiBearingY;
1334 
1335  if (slot->metrics.horiBearingY > maxAscent)
1336  maxAscent = slot->metrics.horiBearingY;
1337 
1338  if (descent > maxDescent)
1339  maxDescent = descent;
1340 
1341  width += advance;
1342  previous = glyphIndex;
1343  }
1344 
1345  return {0, -FT2Point(maxAscent), FT2Point(width), FT2Point(maxAscent + maxDescent)};
1346 }
1347 
1348 // Reset the image and fill it with transparent ink.
1349 // The UK MHEG profile says that we should consider the background
1350 // as paper and the text as ink. We have to consider these as two
1351 // different layers. The background is drawn separately as a rectangle.
1352 void MHIText::Clear(void)
1353 {
1354  m_image = QImage(m_width, m_height, QImage::Format_ARGB32);
1355  // QImage::fill doesn't set the alpha buffer.
1356  for (int i = 0; i < m_height; i++)
1357  {
1358  for (int j = 0; j < m_width; j++)
1359  {
1360  m_image.setPixel(j, i, qRgba(0, 0, 0, 0));
1361  }
1362  }
1363 }
1364 
1365 // Draw a line of text in the given position within the image.
1366 // It would be nice to be able to use TTFFont for this but it doesn't provide
1367 // what we want.
1368 void MHIText::AddText(int x, int y, const QString &str, MHRgba colour)
1369 {
1370  if (!m_parent->IsFaceLoaded()) return;
1371  FT_Face face = m_parent->GetFontFace();
1372 
1373  FT_Set_Char_Size(face, 0, Point2FT(m_fontSize),
1375 
1376  // X positions are computed to 64ths and rounded.
1377  // Y positions are in pixels
1378  int posX = Point2FT(x);
1379  int pixelY = y;
1380  FT_Bool useKerning = FT_HAS_KERNING(face);
1381  FT_UInt previous = 0;
1382 
1383  int len = str.length();
1384  for (int n = 0; n < len; n++)
1385  {
1386  // Load the glyph.
1387  QChar ch = str[n];
1388  FT_UInt glyphIndex = FT_Get_Char_Index(face, ch.unicode());
1389  if (glyphIndex == 0)
1390  {
1391  previous = 0;
1392  continue;
1393  }
1394 
1395  if (useKerning && previous != 0)
1396  {
1397  FT_Vector delta;
1398  FT_Get_Kerning(face, previous, glyphIndex,
1399  FT_KERNING_DEFAULT, &delta);
1400  posX += delta.x;
1401  }
1402  FT_Error error = FT_Load_Glyph(face, glyphIndex, FT_LOAD_RENDER);
1403 
1404  if (error)
1405  continue; // ignore errors
1406 
1407  FT_GlyphSlot slot = face->glyph;
1408  if (slot->format != FT_GLYPH_FORMAT_BITMAP)
1409  continue; // Problem
1410 
1411  if ((enum FT_Pixel_Mode_)slot->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
1412  continue;
1413 
1414  unsigned char *source = slot->bitmap.buffer;
1415  // Get the origin for the bitmap
1416  int baseX = FT2Point(posX) + slot->bitmap_left;
1417  int baseY = pixelY - slot->bitmap_top;
1418  // Copy the bitmap into the image.
1419  for (unsigned int i = 0; i < slot->bitmap.rows; i++)
1420  {
1421  for (unsigned int j = 0; j < slot->bitmap.width; j++)
1422  {
1423  int greyLevel = source[j];
1424  // Set the pixel to the specified colour but scale its
1425  // brightness according to the grey scale of the pixel.
1426  int red = colour.red();
1427  int green = colour.green();
1428  int blue = colour.blue();
1429  int alpha = colour.alpha() *
1430  greyLevel / slot->bitmap.num_grays;
1431  int xBit = j + baseX;
1432  int yBit = i + baseY;
1433 
1434  // The bits ought to be inside the bitmap but
1435  // I guess there's the possibility
1436  // that rounding might put it outside.
1437  if (xBit >= 0 && xBit < m_width &&
1438  yBit >= 0 && yBit < m_height)
1439  {
1440  m_image.setPixel(xBit, yBit,
1441  qRgba(red, green, blue, alpha));
1442  }
1443  }
1444  source += slot->bitmap.pitch;
1445  }
1446  posX += slot->advance.x;
1447  previous = glyphIndex;
1448  }
1449 }
1450 
1451 // Internal function to fill a rectangle with a colour
1452 void MHIDLA::DrawRect(int x, int y, int width, int height, MHRgba colour)
1453 {
1454  QRgb qColour = qRgba(colour.red(), colour.green(),
1455  colour.blue(), colour.alpha());
1456 
1457  // Constrain the drawing within the image.
1458  if (x < 0)
1459  {
1460  width += x;
1461  x = 0;
1462  }
1463 
1464  if (y < 0)
1465  {
1466  height += y;
1467  y = 0;
1468  }
1469 
1470  if (width <= 0 || height <= 0)
1471  return;
1472 
1473  int imageWidth = m_image.width();
1474  int imageHeight = m_image.height();
1475  if (x+width > imageWidth)
1476  width = imageWidth - x;
1477 
1478  if (y+height > imageHeight)
1479  height = imageHeight - y;
1480 
1481  if (width <= 0 || height <= 0)
1482  return;
1483 
1484  for (int i = 0; i < height; i++)
1485  {
1486  for (int j = 0; j < width; j++)
1487  {
1488  m_image.setPixel(x+j, y+i, qColour);
1489  }
1490  }
1491 }
1492 
1493 // Reset the drawing.
1495 {
1496  if (m_width == 0 || m_height == 0)
1497  {
1498  m_image = QImage();
1499  return;
1500  }
1501  m_image = QImage(m_width, m_height, QImage::Format_ARGB32);
1502  // Fill the image with "transparent colour".
1503  DrawRect(0, 0, m_width, m_height, MHRgba(0, 0, 0, 0));
1504 }
1505 
1506 void MHIDLA::Draw(int x, int y)
1507 {
1508  QRect bounds(x, y, m_width, m_height);
1509  if (m_boxed && m_lineWidth != 0)
1510  {
1511  // Draw the lines round the outside.
1512  // These don't form part of the drawing.
1513  m_parent->DrawRect(x, y, m_width,
1514  m_lineWidth, m_boxLineColour);
1515 
1516  m_parent->DrawRect(x, y + m_height - m_lineWidth,
1517  m_width, m_lineWidth, m_boxLineColour);
1518 
1519  m_parent->DrawRect(x, y + m_lineWidth,
1520  m_lineWidth, m_height - m_lineWidth * 2,
1521  m_boxLineColour);
1522 
1523  m_parent->DrawRect(x + m_width - m_lineWidth, y + m_lineWidth,
1524  m_lineWidth, m_height - m_lineWidth * 2,
1525  m_boxLineColour);
1526 
1527  // Deflate the box to within the border.
1528  bounds = QRect(bounds.x() + m_lineWidth,
1529  bounds.y() + m_lineWidth,
1530  bounds.width() - 2*m_lineWidth,
1531  bounds.height() - 2*m_lineWidth);
1532  }
1533 
1534  // Draw the background.
1535  m_parent->DrawRect(x + m_lineWidth,
1536  y + m_lineWidth,
1537  m_width - m_lineWidth * 2,
1538  m_height - m_lineWidth * 2,
1539  m_boxFillColour);
1540 
1541  // Now the drawing.
1542  m_parent->DrawImage(x, y, bounds, m_image);
1543 }
1544 
1545 // The UK MHEG profile defines exactly how transparency is supposed to work.
1546 // The drawings are made using possibly transparent ink with any crossings
1547 // just set to that ink and then the whole drawing is alpha-merged with the
1548 // underlying graphics.
1549 // DynamicLineArt no longer seems to be used in transmissions in the UK
1550 // although it appears that DrawPoly is used in New Zealand. These are
1551 // very basic implementations of the functions.
1552 
1553 // Lines
1554 void MHIDLA::DrawLine(int x1, int y1, int x2, int y2)
1555 {
1556  // Get the arguments so that the lower x comes first and the
1557  // absolute gradient is less than one.
1558  if (abs(y2-y1) > abs(x2-x1))
1559  {
1560  if (y2 > y1)
1561  DrawLineSub(y1, x1, y2, x2, true);
1562  else
1563  DrawLineSub(y2, x2, y1, x1, true);
1564  }
1565  else
1566  {
1567  if (x2 > x1)
1568  DrawLineSub(x1, y1, x2, y2, false);
1569  else
1570  DrawLineSub(x2, y2, x1, y1, false);
1571  }
1572 }
1573 
1574 // Based on the Bresenham line drawing algorithm but extended to draw
1575 // thick lines.
1576 void MHIDLA::DrawLineSub(int x1, int y1, int x2, int y2, bool swapped)
1577 {
1578  QRgb colour = qRgba(m_lineColour.red(), m_lineColour.green(),
1579  m_lineColour.blue(), m_lineColour.alpha());
1580  int dx = x2-x1;
1581  int dy = abs(y2-y1);
1582  int yStep = y2 >= y1 ? 1 : -1;
1583  // Adjust the starting positions to take account of the
1584  // line width.
1585  int error2 = dx/2;
1586  for (int k = 0; k < m_lineWidth/2; k++)
1587  {
1588  y1--;
1589  y2--;
1590  error2 += dy;
1591  if (error2*2 > dx)
1592  {
1593  error2 -= dx;
1594  x1 += yStep;
1595  x2 += yStep;
1596  }
1597  }
1598  // Main loop
1599  int y = y1;
1600  int error = dx/2;
1601  for (int x = x1; x <= x2; x++) // Include both endpoints
1602  {
1603  error2 = dx/2;
1604  int j = 0;
1605  // Inner loop also uses the Bresenham algorithm to draw lines
1606  // perpendicular to the principal direction.
1607  for (int i = 0; i < m_lineWidth; i++)
1608  {
1609  if (swapped)
1610  {
1611  if (x+j >= 0 && y+i >= 0 && y+i < m_width && x+j < m_height)
1612  m_image.setPixel(y+i, x+j, colour);
1613  }
1614  else
1615  {
1616  if (x+j >= 0 && y+i >= 0 && x+j < m_width && y+i < m_height)
1617  m_image.setPixel(x+j, y+i, colour);
1618  }
1619  error2 += dy;
1620  if (error2*2 > dx)
1621  {
1622  error2 -= dx;
1623  j -= yStep;
1624  if (i < m_lineWidth-1)
1625  {
1626  // Add another pixel in this case.
1627  if (swapped)
1628  {
1629  if (x+j >= 0 && y+i >= 0 && y+i < m_width && x+j < m_height)
1630  m_image.setPixel(y+i, x+j, colour);
1631  }
1632  else
1633  {
1634  if (x+j >= 0 && y+i >= 0 && x+j < m_width && y+i < m_height)
1635  m_image.setPixel(x+j, y+i, colour);
1636  }
1637  }
1638  }
1639  }
1640  error += dy;
1641  if (error*2 > dx)
1642  {
1643  error -= dx;
1644  y += yStep;
1645  }
1646  }
1647 }
1648 
1649 // Rectangles
1650 void MHIDLA::DrawBorderedRectangle(int x, int y, int width, int height)
1651 {
1652  if (m_lineWidth != 0)
1653  {
1654  // Draw the lines round the rectangle.
1655  DrawRect(x, y, width, m_lineWidth,
1656  m_lineColour);
1657 
1658  DrawRect(x, y + height - m_lineWidth,
1659  width, m_lineWidth,
1660  m_lineColour);
1661 
1662  DrawRect(x, y + m_lineWidth,
1663  m_lineWidth, height - m_lineWidth * 2,
1664  m_lineColour);
1665 
1666  DrawRect(x + width - m_lineWidth, y + m_lineWidth,
1667  m_lineWidth, height - m_lineWidth * 2,
1668  m_lineColour);
1669 
1670  // Fill the rectangle.
1671  DrawRect(x + m_lineWidth, y + m_lineWidth,
1672  width - m_lineWidth * 2, height - m_lineWidth * 2,
1673  m_fillColour);
1674  }
1675  else
1676  {
1677  DrawRect(x, y, width, height, m_fillColour);
1678  }
1679 }
1680 
1681 // Ovals (ellipses)
1682 void MHIDLA::DrawOval(int /*x*/, int /*y*/, int /*width*/, int /*height*/)
1683 {
1684  // Not implemented. Not actually used in practice.
1685 }
1686 
1687 // Arcs and sectors
1688 void MHIDLA::DrawArcSector(int /*x*/, int /*y*/, int /*width*/, int /*height*/,
1689  int /*start*/, int /*arc*/, bool /*isSector*/)
1690 {
1691  // Not implemented. Not actually used in practice.
1692 }
1693 
1694 // Polygons. This is used directly and also to draw other figures.
1695 // The UK profile says that MHEG should not contain concave or
1696 // self-crossing polygons but we can get the former at least as
1697 // a result of rounding when drawing ellipses.
1698 struct lineSeg { int m_yBottom, m_yTop, m_xBottom; float m_slope; };
1699 
1700 void MHIDLA::DrawPoly(bool isFilled, int nPoints, const int *xArray, const int *yArray)
1701 {
1702  if (nPoints < 2)
1703  return;
1704 
1705  if (isFilled)
1706  {
1707  QVector <lineSeg> lineArray(nPoints);
1708  int nLines = 0;
1709  // Initialise the line segment array. Include all lines
1710  // apart from horizontal. Close the polygon by starting
1711  // with the last point in the array.
1712  int lastX = xArray[nPoints-1]; // Last point
1713  int lastY = yArray[nPoints-1];
1714  int yMin = lastY;
1715  int yMax = lastY;
1716  for (int k = 0; k < nPoints; k++)
1717  {
1718  int thisX = xArray[k];
1719  int thisY = yArray[k];
1720  if (lastY != thisY)
1721  {
1722  if (lastY > thisY)
1723  {
1724  lineArray[nLines].m_yBottom = thisY;
1725  lineArray[nLines].m_yTop = lastY;
1726  lineArray[nLines].m_xBottom = thisX;
1727  }
1728  else
1729  {
1730  lineArray[nLines].m_yBottom = lastY;
1731  lineArray[nLines].m_yTop = thisY;
1732  lineArray[nLines].m_xBottom = lastX;
1733  }
1734  lineArray[nLines++].m_slope =
1735  (float)(thisX-lastX) / (float)(thisY-lastY);
1736  }
1737  if (thisY < yMin)
1738  yMin = thisY;
1739  if (thisY > yMax)
1740  yMax = thisY;
1741  lastX = thisX;
1742  lastY = thisY;
1743  }
1744 
1745  // Find the intersections of each line in the line segment array
1746  // with the scan line. Because UK MHEG says that figures should be
1747  // convex we only need to consider two intersections.
1748  QRgb fillColour = qRgba(m_fillColour.red(), m_fillColour.green(),
1749  m_fillColour.blue(), m_fillColour.alpha());
1750  for (int y = yMin; y < yMax; y++)
1751  {
1752  int crossings = 0;
1753  int xMin = 0;
1754  int xMax = 0;
1755  for (int l = 0; l < nLines; l++)
1756  {
1757  if (y >= lineArray[l].m_yBottom && y < lineArray[l].m_yTop)
1758  {
1759  int x = (int)round((float)(y - lineArray[l].m_yBottom) *
1760  lineArray[l].m_slope) + lineArray[l].m_xBottom;
1761  if (crossings == 0 || x < xMin)
1762  xMin = x;
1763  if (crossings == 0 || x > xMax)
1764  xMax = x;
1765  crossings++;
1766  }
1767  }
1768  if (crossings == 2)
1769  {
1770  for (int x = xMin; x <= xMax; x++)
1771  m_image.setPixel(x, y, fillColour);
1772  }
1773  }
1774 
1775  // Draw the boundary
1776  int lastXpoint = xArray[nPoints-1]; // Last point
1777  int lastYpoint = yArray[nPoints-1];
1778  for (int i = 0; i < nPoints; i++)
1779  {
1780  DrawLine(xArray[i], yArray[i], lastXpoint, lastYpoint);
1781  lastXpoint = xArray[i];
1782  lastYpoint = yArray[i];
1783  }
1784  }
1785  else // PolyLine - draw lines between the points but don't close it.
1786  {
1787  for (int i = 1; i < nPoints; i++)
1788  {
1789  DrawLine(xArray[i], yArray[i], xArray[i-1], yArray[i-1]);
1790  }
1791  }
1792 }
1793 
1794 MHIBitmap::MHIBitmap(MHIContext *parent, bool tiled)
1795  : m_parent(parent), m_tiled(tiled),
1796  m_copyCtx(new MythAVCopy(false))
1797 {
1798 }
1799 
1801 {
1802  delete m_copyCtx;
1803 }
1804 
1805 void MHIBitmap::Draw(int x, int y, QRect rect, bool tiled, bool bUnder)
1806 {
1807  if (tiled)
1808  {
1809  if (m_image.width() == 0 || m_image.height() == 0)
1810  return;
1811  // Construct an image the size of the bounding box and tile the
1812  // bitmap over this.
1813  QImage tiledImage = QImage(rect.width(), rect.height(),
1814  QImage::Format_ARGB32);
1815 
1816  for (int i = 0; i < rect.width(); i++)
1817  {
1818  for (int j = 0; j < rect.height(); j++)
1819  {
1820  tiledImage.setPixel(i, j, m_image.pixel(i % m_image.width(), j % m_image.height()));
1821  }
1822  }
1823  m_parent->DrawImage(rect.x(), rect.y(), rect, tiledImage, true, bUnder);
1824  }
1825  else
1826  {
1827  // NB THe BBC expects bitmaps to be scaled, not clipped
1828  m_parent->DrawImage(x, y, rect, m_image, true, bUnder);
1829  }
1830 }
1831 
1832 // Create a bitmap from PNG.
1833 void MHIBitmap::CreateFromPNG(const unsigned char *data, int length)
1834 {
1835  m_image = QImage();
1836 
1837  if (!m_image.loadFromData(data, length, "PNG"))
1838  {
1839  m_image = QImage();
1840  return;
1841  }
1842 
1843  // Assume that if it has an alpha buffer then it's partly transparent.
1844  m_opaque = ! m_image.hasAlphaChannel();
1845 }
1846 
1847 // Create a bitmap from JPEG.
1848 //virtual
1849 void MHIBitmap::CreateFromJPEG(const unsigned char *data, int length)
1850 {
1851  m_image = QImage();
1852 
1853  if (!m_image.loadFromData(data, length, "JPG"))
1854  {
1855  m_image = QImage();
1856  return;
1857  }
1858 
1859  // Assume that if it has an alpha buffer then it's partly transparent.
1860  m_opaque = ! m_image.hasAlphaChannel();
1861 }
1862 
1863 // Convert an MPEG I-frame into a bitmap. This is used as the way of
1864 // sending still pictures. We convert the image to a QImage even
1865 // though that actually means converting it from YUV and eventually
1866 // converting it back again but we do this very infrequently so the
1867 // cost is outweighed by the simplification.
1868 void MHIBitmap::CreateFromMPEG(const unsigned char *data, int length)
1869 {
1870  AVCodecContext *c = nullptr;
1871  MythAVFrame picture;
1872  AVPacket pkt;
1873  uint8_t *buff = nullptr;
1874  bool gotPicture = false;
1875  m_image = QImage();
1876 
1877  // Find the mpeg2 video decoder.
1878  AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_MPEG2VIDEO);
1879  if (!codec)
1880  return;
1881  if (!picture)
1882  return;
1883 
1884  c = avcodec_alloc_context3(nullptr);
1885 
1886  if (avcodec_open2(c, codec, nullptr) < 0)
1887  goto Close;
1888 
1889  // Copy the data into AVPacket
1890  if (av_new_packet(&pkt, length) < 0)
1891  goto Close;
1892 
1893  memcpy(pkt.data, data, length);
1894  buff = pkt.data;
1895 
1896  // Get a picture from the packet. Allow 9 loops for
1897  // packet to be decoded. It should take only 2-3 loops
1898  for (int limit=0; limit<10 && !gotPicture; limit++)
1899  {
1900  int len = avcodec_receive_frame(c, picture);
1901  if (len == 0)
1902  gotPicture = true;
1903  if (len == AVERROR(EAGAIN))
1904  len = 0;
1905  if (len == 0)
1906  len = avcodec_send_packet(c, &pkt);
1907  if (len == AVERROR(EAGAIN) || len == AVERROR_EOF)
1908  len = 0;
1909  if (len < 0) // Error
1910  {
1911  char error[AV_ERROR_MAX_STRING_SIZE];
1912  LOG(VB_GENERAL, LOG_ERR,
1913  QString("[mhi] video decode error: %1 (%2)")
1914  .arg(av_make_error_string(error, sizeof(error), len))
1915  .arg(gotPicture));
1916  goto Close;
1917  }
1918  else
1919  {
1920  pkt.data = nullptr;
1921  pkt.size = 0;
1922  }
1923  }
1924 
1925  if (gotPicture)
1926  {
1927  int nContentWidth = c->width;
1928  int nContentHeight = c->height;
1929  m_image = QImage(nContentWidth, nContentHeight, QImage::Format_ARGB32);
1930  m_opaque = true; // MPEG images are always opaque.
1931 
1932  AVFrame retbuf;
1933  memset(&retbuf, 0, sizeof(AVFrame));
1934 
1935  int bufflen = nContentWidth * nContentHeight * 3;
1936  auto *outputbuf = (unsigned char*)av_malloc(bufflen);
1937 
1938  av_image_fill_arrays(retbuf.data, retbuf.linesize,
1939  outputbuf, AV_PIX_FMT_RGB24,
1940  nContentWidth, nContentHeight,IMAGE_ALIGN);
1941 
1942  AVFrame *tmp = picture;
1943  m_copyCtx->Copy(&retbuf, AV_PIX_FMT_RGB24, tmp, c->pix_fmt,
1944  nContentWidth, nContentHeight);
1945 
1946  uint8_t * buf = outputbuf;
1947 
1948  // Copy the data a pixel at a time.
1949  // This should handle endianness correctly.
1950  for (int i = 0; i < nContentHeight; i++)
1951  {
1952  for (int j = 0; j < nContentWidth; j++)
1953  {
1954  int red = *buf++;
1955  int green = *buf++;
1956  int blue = *buf++;
1957  m_image.setPixel(j, i, qRgb(red, green, blue));
1958  }
1959  }
1960  av_freep(&outputbuf);
1961  }
1962 
1963 Close:
1964  pkt.data = buff;
1965  av_packet_unref(&pkt);
1966  avcodec_free_context(&c);
1967 }
1968 
1969 // Scale the bitmap. Only used for image derived from MPEG I-frames.
1970 void MHIBitmap::ScaleImage(int newWidth, int newHeight)
1971 {
1972  if (m_image.isNull())
1973  return;
1974 
1975  if (newWidth == m_image.width() && newHeight == m_image.height())
1976  return;
1977 
1978  if (newWidth <= 0 || newHeight <= 0)
1979  { // This would be a bit silly but handle it anyway.
1980  m_image = QImage();
1981  return;
1982  }
1983 
1984  m_image = m_image.scaled(newWidth, newHeight,
1985  Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
1986 }
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:783
QMutex m_displayLock
Definition: mhi.h:200
unsigned m_carouselId
Definition: mhi.h:389
void EndStream() override
Definition: mhi.cpp:1076
long GetStreamPos() override
Definition: mhi.cpp:1142
#define ACTION_6
Definition: mythuiactions.h:10
#define GET_KEY(a, b)
bool CheckFile(const QString &csPath, const QByteArray &cert=QByteArray())
Definition: mhegic.cpp:69
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:864
bool CheckAccess(const QString &objectPath, QByteArray &cert)
Definition: mhi.cpp:393
void run(void) override
Definition: mhi.cpp:238
void DrawLine(int x1, int y1, int x2, int y2) override
Definition: mhi.cpp:1554
#define FONT_HEIGHTRES
Definition: mhi.cpp:48
bool SetStream(const QString &stream)
static void error(const char *str,...)
Definition: vbi.cpp:42
int Copy(VideoFrame *dst, const VideoFrame *src)
Definition: mythavutil.cpp:330
QMutex m_dsmccLock
Definition: mhi.h:185
int m_currentChannel
Definition: mhi.h:210
virtual void EngineEvent(int)=0
ChannelCache_t m_channelCache
Definition: mhi.h:229
const unsigned kTuneKeepApp
Definition: mhi.cpp:54
Data for items in the interactive television display stack.
Definition: mhi.cpp:66
void Draw(int x, int y, QRect rect, bool tiled, bool bUnder) override
Draw the completed drawing onto the display.
Definition: mhi.cpp:1805
#define round(x)
Definition: mythplayer.cpp:64
bool SetVideoByComponentTag(int tag)
Selects the video stream using the DVB component tag.
bool BeginVideo(int tag) override
Begin displaying video.
Definition: mhi.cpp:1122
void ClearQueue(void)
Definition: mhi.cpp:146
MHStream * m_notify
Definition: mhi.h:189
#define ACTION_0
Definition: mythuiactions.h:4
MThread * m_engineThread
Definition: mhi.h:208
int DecrRef(void) override
Decrements reference count and deletes on 0.
Definition: mythimage.cpp:55
#define ACTION_2
Definition: mythuiactions.h:6
Image widget, displays a single image or multiple images in sequence.
Definition: mythuiimage.h:97
void Restart(int chanid, int sourceid, bool isLive)
Restart the MHEG engine.
Definition: mhi.cpp:171
static int Tid(ChannelCache_t::const_iterator it)
Definition: mhi.h:231
int m_currentStream
Definition: mhi.h:211
bool m_bUnder
Definition: mhi.cpp:72
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
const int kShift
Definition: mhi.cpp:1257
static int Nid(ChannelCache_t::const_iterator it)
Definition: mhi.h:233
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:311
Object for displaying Dynamic Line Art.
Definition: mhi.h:315
void DrawRect(int x, int y, int width, int height, MHRgba colour)
Definition: mhi.cpp:1452
MythAVCopy Copy AVFrame<->frame, performing the required conversion if any.
Definition: mythavutil.h:106
#define ACTION_SEEKFFWD
Definition: tv_actions.h:43
bool GetServiceInfo(int channelId, int &netId, int &origNetId, int &transportId, int &serviceId) override
Get netId etc from the channel index.
Definition: mhi.cpp:981
int m_keyProfile
Definition: mhi.h:193
Data for the queued DSMCC tables.
Definition: mhi.h:369
#define ACTION_UP
Definition: mythuiactions.h:16
Definition: dsmcc.h:75
~MHIBitmap() override
Definition: mhi.cpp:1800
int GetChannelIndex(const QString &str) override
Definition: mhi.cpp:896
struct AVFrame AVFrame
virtual void GenerateUserAction(int nCode)=0
static const int kStdDisplayHeight
Definition: mhi.h:169
int Roundup(int n, int r)
Definition: mhi.cpp:828
bool m_stop
Definition: mhi.h:199
bool LoadFont(const QString &name)
Definition: mhi.cpp:95
#define ACTION_MENUBLUE
Definition: tv_actions.h:80
const unsigned kTuneCarId
Definition: mhi.cpp:55
EResult GetFile(const QString &csPath, QByteArray &data, const QByteArray &cert=QByteArray())
Definition: mhegic.cpp:103
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
void DeleteAllChildren(void)
Delete all child widgets.
Definition: mythuitype.cpp:202
#define ACTION_MENUGREEN
Definition: tv_actions.h:78
void DrawArcSector(int x, int y, int width, int height, int start, int arc, bool isSector) override
Definition: mhi.cpp:1688
bool m_isLive
Definition: mhi.h:212
void CreateFromPNG(const unsigned char *data, int length) override
Create bitmap from PNG.
Definition: mhi.cpp:1833
#define ACTION_RIGHT
Definition: mythuiactions.h:19
void DrawVideo(const QRect &videoRect, const QRect &dispRect) override
Definition: mhi.cpp:837
QWaitCondition m_engine_wait
Definition: mhi.h:198
int ScaleVideoX(int n, bool roundup=false) const
Definition: mhi.cpp:777
bool BeginStream(const QString &str, MHStream *notify) override
Begin playing the specified stream.
Definition: mhi.cpp:1032
static guint32 * tmp
Definition: goom_core.cpp:30
QRect m_displayRect
Definition: mhi.h:223
void enqueue(T d)
Adds item to the back of the list. O(1).
Definition: mythdeque.h:42
static int Sid(ChannelCache_t::const_iterator it)
Definition: mhi.h:234
long GetStreamMaxPos() override
Definition: mhi.cpp:1148
void DrawImage(int x, int y, const QRect &rect, const QImage &image, bool bScaled=false, bool bUnder=false)
Definition: mhi.cpp:1203
int m_length
Definition: mhi.h:387
#define ACTION_STOP
Definition: tv_actions.h:8
MHKeyLookup()
Definition: mhi.cpp:547
QString GetConfDir(void)
Definition: mythdirs.cpp:224
#define ACTION_SELECT
Definition: mythuiactions.h:15
QVariant value(int i) const
Definition: mythdbcon.h:198
unsigned char * m_data
Definition: mhi.h:386
void SetInputRegister(int num) override
Definition: mhi.cpp:647
bool BeginAudio(int tag) override
Begin playing audio.
Definition: mhi.cpp:1102
QPair< QString, int > key_t
Definition: mhi.cpp:509
void DrawBackground(const QRegion &reg) override
Definition: mhi.cpp:1226
virtual void SetVisible(bool visible)
Contains various utility functions for interactive television.
Definition: mhi.h:50
void NetworkBootRequested(void)
Definition: mhi.cpp:337
#define ACTION_8
Definition: mythuiactions.h:12
void CreateFromJPEG(const unsigned char *data, int length) override
Create bitmap from JPEG.
Definition: mhi.cpp:1849
QRect m_videoDisplayRect
Definition: mhi.h:222
This class is used as a container for messages.
Definition: mythevent.h:16
#define FONT_TO_USE
Definition: mhi.cpp:49
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:85
int m_dataBroadcastId
Definition: mhi.h:390
void QueueDSMCCPacket(unsigned char *data, int length, int componentTag, unsigned carouselId, int dataBroadcastId)
Definition: mhi.cpp:292
MSqlQuery query(MSqlQuery::InitCon())
void DrawBorderedRectangle(int x, int y, int width, int height) override
Definition: mhi.cpp:1650
void DrawPoly(bool isFilled, int nPoints, const int *xArray, const int *yArray) override
Definition: mhi.cpp:1700
static int x2
Definition: mythsocket.cpp:61
QList< int > m_tuneInfo
Definition: mhi.h:217
void StreamPlay(bool play=true)
#define NBI_VERSION_UNSET
Definition: mhi.h:45
static const int kStdDisplayWidth
Definition: mhi.h:168
#define ACTION_PLAY
Definition: tv_actions.h:30
#define ACTION_MENUYELLOW
Definition: tv_actions.h:79
#define VERBOSE_LEVEL_CHECK(_MASK_, _LEVEL_)
Definition: mythlogging.h:14
QHash< key_t, int > m_map
Definition: mhi.cpp:521
virtual void DrawDisplay(QRegion toDraw)=0
MythDeque< int > m_keyQueue
Definition: mhi.h:192
virtual int RunAll(void)=0
#define ACTION_1
Definition: mythuiactions.h:5
QPair< int, int > Key_t
Definition: mhi.h:227
T dequeue()
Removes item from front of list and returns a copy. O(1).
Definition: mythdeque.h:32
bool m_opaque
Definition: mhi.h:308
MHIBitmap(MHIContext *parent, bool tiled)
Definition: mhi.cpp:1794
QString GetShareDir(void)
Definition: mythdirs.cpp:222
bool m_faceLoaded
Definition: mhi.h:206
bool isActive(void) const
Definition: mythdbcon.h:204
#define ACTION_PAUSE
Definition: tv_actions.h:15
int m_yTop
Definition: mhi.cpp:1698
QMutex m_channelMutex
Definition: mhi.h:230
bool StreamStarted(bool bStarted=true)
Definition: mhi.cpp:1086
Wrapper around QRect allowing us to handle percentage and other relative values for areas in mythui.
Definition: mythrect.h:17
MHIContext(InteractiveTV *parent)
Definition: mhi.cpp:75
uint m_lastNbiVersion
Definition: mhi.h:219
#define ACTION_7
Definition: mythuiactions.h:11
void DrawLineSub(int x1, int y1, int x2, int y2, bool swapped)
Definition: mhi.cpp:1576
virtual void StreamStarted(MHStream *, bool bStarted=true)=0
Dsmcc * m_dsmcc
Definition: mhi.h:184
unsigned int uint
Definition: compat.h:140
bool GetCarouselData(QString objectPath, QByteArray &result) override
Definition: mhi.cpp:433
int GetDSMCCObject(QStringList &objectPath, QByteArray &result)
Definition: dsmcc.cpp:545
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
InteractiveTV * m_parent
Definition: mhi.h:181
FT_Face m_face
Definition: mhi.h:205
long GetStreamPos()
int ScaleY(int n, bool roundup=false) const
Definition: mhi.cpp:766
static int Cid(ChannelCache_t::const_iterator it)
Definition: mhi.h:232
MHBitmapDisplay * CreateBitmap(bool tiled) override
Definition: mhi.cpp:1180
const unsigned kTuneQuietly
Definition: mhi.cpp:53
void * av_malloc(unsigned int size)
int m_audioTag
Definition: mhi.h:215
QPair< int, int > Val_t
Definition: mhi.h:226
void dispatch(const MythEvent &event)
void Clear(void) override
Clear the drawing.
Definition: mhi.cpp:1494
#define ACTION_MENURED
Definition: tv_actions.h:77
MythPlayer * GetNVP(void)
Definition: interactivetv.h:48
#define ACTION_MENUTEXT
Definition: tv_actions.h:82
QRect m_videoRect
Definition: mhi.h:222
#define ACTION_PLAYBACK
Definition: tv_actions.h:7
QRect ScaleVideo(const QRect &r) const
Definition: mhi.cpp:787
long SetStreamPos(long ms)
void Reset()
Definition: dsmcc.cpp:536
void Draw(int x, int y) override
Draw the completed drawing onto the display.
Definition: mhi.cpp:1506
vector< unsigned char > m_nbiData
Definition: mhi.h:220
int m_x
Definition: mhi.cpp:70
int GetNumSetting(const QString &key, int defaultval=0)
MHEG * MHCreateEngine(MHContext *context)
Definition: Engine.cpp:43
virtual void SetBooting()=0
MHDLADisplay * CreateDynamicLineArt(bool isBoxed, MHRgba lineColour, MHRgba fillColour) override
Creation functions for various visibles.
Definition: mhi.cpp:1167
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:808
int alpha() const
Definition: freemheg.h:85
void ProcessSection(const unsigned char *data, int length, int componentTag, unsigned carouselId, int dataBroadcastId)
Definition: dsmcc.cpp:446
void AddText(int x, int y, const QString &str, MHRgba colour) override
Definition: mhi.cpp:1368
int Find(const QString &name, int reg) const
Definition: mhi.cpp:514
void Clear(void) override
Definition: mhi.cpp:1352
static FT_F26Dot6 Point2FT(int pt)
Definition: mhi.cpp:1258
Definition: mhi.h:238
MHEG * m_engine
Definition: mhi.h:195
int green() const
Definition: freemheg.h:83
void DrawRect(int xPos, int yPos, int width, int height, MHRgba colour) override
Additional drawing functions.
Definition: mhi.cpp:1186
QString GetFontsDir(void)
Definition: mythdirs.cpp:309
QImage m_image
Definition: mhi.h:307
#define ACTION_4
Definition: mythuiactions.h:8
void SetFont(int size, bool isBold, bool isItalic) override
Definition: mhi.cpp:1247
#define ACTION_9
Definition: mythuiactions.h:13
QRect Scale(const QRect &r) const
Definition: mhi.cpp:771
bool OfferKey(const QString &key)
Definition: mhi.cpp:605
#define ACTION_JUMPFFWD
Definition: tv_actions.h:44
void CreateFromMPEG(const unsigned char *data, int length) override
Create bitmap from single I frame MPEG.
Definition: mhi.cpp:1868
static bool ft_loaded
Definition: mhi.cpp:44
#define ACTION_SEEKRWND
Definition: tv_actions.h:42
MythAVCopy * m_copyCtx
Definition: mhi.h:309
MHIContext * m_parent
Definition: mhi.h:305
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:294
MythDeque< DSMCCPacket * > m_dsmccQueue
Definition: mhi.h:186
void SetSize(int width, int height) override
Definition: mhi.cpp:1241
int m_videoTag
Definition: mhi.h:216
MHInteractionChannel m_ic
Definition: mhi.h:188
long GetStreamMaxPos()
Object for drawing bitmaps.
Definition: mhi.h:267
list< MHIImageData * > m_display
Definition: mhi.h:203
void ScaleImage(int newWidth, int newHeight) override
Scale the bitmap. Only used for image derived from MPEG I-frames.
Definition: mhi.cpp:1970
bool m_updated
Definition: mhi.h:201
void RequireRedraw(const QRegion &region) override
An area of the screen/image needs to be redrawn.
Definition: mhi.cpp:750
QImage m_image
Definition: mhi.cpp:69
int ScaleVideoY(int n, bool roundup=false) const
Definition: mhi.cpp:782
MythAVFrame little utility class that act as a safe way to allocate an AVFrame which can then be allo...
Definition: mythavutil.h:42
bool TuneTo(int channel, int tuneinfo) override
Definition: mhi.cpp:1007
QMutex m_keyLock
Definition: mhi.h:191
void StreamPlay(bool play) override
Definition: mhi.cpp:1160
int red() const
Definition: freemheg.h:82
#define ACTION_JUMPRWND
Definition: tv_actions.h:45
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
#define ACTION_LEFT
Definition: mythuiactions.h:18
MHTextDisplay * CreateText(void) override
Definition: mhi.cpp:1174
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:179
void UpdateOSD(InteractiveScreen *osdWindow, MythPainter *osdPainter)
Update the display.
Definition: mhi.cpp:662
void Assign(const QImage &img)
Definition: mythimage.cpp:80
#define ACTION_5
Definition: mythuiactions.h:9
int ScaleX(int n, bool roundup=false) const
Definition: mhi.cpp:761
#define ACTION_TEXTEXIT
Definition: tv_actions.h:81
int m_currentSource
Definition: mhi.h:213
#define ACTION_3
Definition: mythuiactions.h:7
bool LoadChannelCache()
Definition: mhi.cpp:861
void OptimiseDisplayedArea(void)
long SetStreamPos(long pos) override
Definition: mhi.cpp:1154
QRect GetBounds(const QString &str, int &strLen, int maxSize=-1) override
Definition: mhi.cpp:1275
int m_componentTag
Definition: mhi.h:388
void SetNetBootInfo(const unsigned char *data, uint length)
Definition: mhi.cpp:312
bool SetAudioByComponentTag(int tag)
Selects the audio stream using the DVB component tag.
void StopVideo() override
Stop displaying video.
Definition: mhi.cpp:1136
MythImage * GetFormatImage()
Returns a blank reference counted image in the format required for the Draw functions for this painte...
void ProcessDSMCCQueue(void)
Definition: mhi.cpp:273
void AddToDisplay(const QImage &image, const QRect &rect, bool bUnder=false)
Definition: mhi.cpp:793
void SetVideoResize(const QRect &videoRect)
void Draw(int x, int y) override
Definition: mhi.cpp:1236
#define ACTION_MENUEPG
Definition: tv_actions.h:83
void ClearDisplay(void)
Definition: mhi.cpp:137
const unsigned kTuneCarReset
Definition: mhi.cpp:56
~MHIContext() override
Definition: mhi.cpp:125
void key(const QString &name, int code, int r1, int r2=0, int r3=0, int r4=0, int r5=0, int r6=0, int r7=0, int r8=0, int r9=0)
Definition: mhi.cpp:524
void GetInitialStreams(int &audioTag, int &videoTag) const
Get the initial component tags.
Definition: mhi.cpp:740
QMutex m_runLock
Definition: mhi.h:197
int GetICStatus() override
Definition: mhi.cpp:655
#define ACTION_DOWN
Definition: mythuiactions.h:17
This is the interface between an MHEG engine and a MythTV TV object.
Definition: interactivetv.h:12
bool CheckCarouselObject(QString objectPath) override
Definition: mhi.cpp:365
static EStatus status()
Definition: mhegic.cpp:44
void Reinit(const QRect &videoRect, const QRect &dispRect, float aspect)
The display area has changed.
Definition: mhi.cpp:621
static FT_Library ft_library
Definition: mhi.cpp:45
int m_y
Definition: mhi.cpp:71
void StopAudio() override
Stop playing audio.
Definition: mhi.cpp:1116
const unsigned kTuneKeepChnl
Definition: mhi.cpp:60
int blue() const
Definition: freemheg.h:84
static int x1
Definition: mythsocket.cpp:60
bool GetDSMCCObject(const QString &objectPath, QByteArray &result)
Definition: mhi.cpp:385
#define FONT_WIDTHRES
Definition: mhi.cpp:47
void DrawOval(int x, int y, int width, int height) override
Definition: mhi.cpp:1682
static int FT2Point(FT_F26Dot6 fp)
Definition: mhi.cpp:1263
void StopEngine(void)
Stop the MHEG engine if it's running and waits until it has.
Definition: mhi.cpp:154
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23