MythTV  master
mythdvdinfo.cpp
Go to the documentation of this file.
1 // Qt
2 #include <QDir>
3 
4 // MythTV
5 #include "mythlogging.h"
6 #include "mythiowrapper.h"
7 #include "mythdvdinfo.h"
8 
9 // Std
10 #include <fcntl.h>
11 #include <zlib.h>
12 
13 #undef Z_NULL
14 #define Z_NULL nullptr
15 
16 MythDVDInfo::MythDVDInfo(const QString &Filename)
17 {
18  LOG(VB_PLAYBACK, LOG_INFO, QString("DVDInfo: Trying %1").arg(Filename));
19  QString name = Filename;
20  if (name.startsWith("dvd:"))
21  {
22  name.remove(0,4);
23  while (name.startsWith("//"))
24  name.remove(0,1);
25  }
26 
27  QByteArray fname = name.toLocal8Bit();
28  dvdnav_status_t res = dvdnav_open(&m_nav, fname.constData());
29  if (res == DVDNAV_STATUS_ERR)
30  {
31  m_lastError = tr("Failed to open device at %1").arg(fname.constData());
32  LOG(VB_GENERAL, LOG_ERR, QString("DVDInfo: ") + m_lastError);
33  return;
34  }
35 
36  GetNameAndSerialNum(m_nav, m_name, m_serialnumber, name, QString("DVDInfo: "));
37 }
38 
40 {
41  if (m_nav)
42  dvdnav_close(m_nav);
43  LOG(VB_PLAYBACK, LOG_INFO, QString("DVDInfo: Finishing."));
44 }
45 
46 bool MythDVDInfo::IsValid(void) const
47 {
48  return m_nav != nullptr;
49 }
50 
52  QString &Name,
53  QString &Serialnum,
54  const QString &Filename,
55  const QString &LogPrefix)
56 {
57  const char* dvdname = nullptr;
58  const char* dvdserial = nullptr;
59 
60  if (dvdnav_get_title_string(Nav, &dvdname) == DVDNAV_STATUS_ERR)
61  LOG(VB_GENERAL, LOG_ERR, LogPrefix + "Failed to get name.");
62  if (dvdnav_get_serial_string(Nav, &dvdserial) == DVDNAV_STATUS_ERR)
63  LOG(VB_GENERAL, LOG_ERR, LogPrefix + "Failed to get serial number.");
64 
65  Name = QString(dvdname);
66  Serialnum = QString(dvdserial);
67 
68  if (Name.isEmpty() && Serialnum.isEmpty())
69  {
70  struct stat stat {};
71  if ((MythFileStat(Filename.toLocal8Bit(), &stat) == 0) && S_ISDIR(stat.st_mode))
72  {
73  // Name and serial number are empty because we're reading
74  // from a directory (and not a device or image).
75 
76  // Use the directory name for the DVD name
77  QDir dir(Filename);
78  Name = dir.dirName();
79  LOG(VB_PLAYBACK, LOG_DEBUG, LogPrefix + QString("Generated dvd name '%1'")
80  .arg(Name));
81 
82  // And use the CRC of VTS_01_0.IFO as a serial number
83  QString ifo = Filename + QString("/VIDEO_TS/VTS_01_0.IFO");
84  int fd = MythFileOpen(ifo.toLocal8Bit(), O_RDONLY);
85 
86  if (fd > 0)
87  {
88  uint8_t buf[2048];
89  ssize_t read = 0;
90  auto crc = static_cast<uint32_t>(crc32(0L, Z_NULL, 0));
91 
92  while((read = MythFileRead(fd, buf, sizeof(buf))) > 0)
93  crc = static_cast<uint32_t>(crc32(crc, buf, static_cast<uint>(read)));
94 
95  MythfileClose(fd);
96  Serialnum = QString("%1__gen").arg(crc, 0, 16, QChar('0'));
97  LOG(VB_PLAYBACK, LOG_DEBUG, LogPrefix + QString("Generated serial number '%1'")
98  .arg(Serialnum));
99  }
100  else
101  {
102  LOG(VB_GENERAL, LOG_ERR, LogPrefix + QString("Unable to open %2 to generate serial number")
103  .arg(ifo));
104  }
105  }
106  }
107 }
108 
109 bool MythDVDInfo::GetNameAndSerialNum(QString &Name, QString &SerialNumber)
110 {
111  Name = m_name;
112  SerialNumber = m_serialnumber;
113  return !(Name.isEmpty() && SerialNumber.isEmpty());
114 }
115 
116 QString MythDVDInfo::GetLastError(void) const
117 {
118  return m_lastError;
119 }
MythDVDInfo::IsValid
bool IsValid(void) const
Definition: mythdvdinfo.cpp:46
MythDVDInfo::MythDVDInfo
MythDVDInfo(const QString &Filename)
Definition: mythdvdinfo.cpp:16
discid.disc.read
def read(device=None, features=[])
Definition: disc.py:35
arg
arg(title).arg(filename).arg(doDelete))
MythfileClose
int MythfileClose(int FileID)
Definition: mythiowrapper.cpp:202
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MythDVDInfo::m_name
QString m_name
Definition: mythdvdinfo.h:32
MythFileRead
ssize_t MythFileRead(int FileID, void *Buffer, size_t Count)
Definition: mythiowrapper.cpp:282
MythDVDInfo::m_lastError
QString m_lastError
Definition: mythdvdinfo.h:34
MythDVDInfo::~MythDVDInfo
~MythDVDInfo(void)
Definition: mythdvdinfo.cpp:39
mythlogging.h
MythDVDInfo::m_serialnumber
QString m_serialnumber
Definition: mythdvdinfo.h:33
MythFileStat
int MythFileStat(const char *Path, struct stat *Buf)
Definition: mythiowrapper.cpp:320
uint
unsigned int uint
Definition: compat.h:140
mythdvdinfo.h
Z_NULL
#define Z_NULL
Definition: mythdvdinfo.cpp:14
MythDVDInfo::GetLastError
QString GetLastError(void) const
Definition: mythdvdinfo.cpp:116
MythDVDInfo::m_nav
dvdnav_t * m_nav
Definition: mythdvdinfo.h:31
dir
QDir dir
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:1171
Name
Definition: channelsettings.cpp:47
MythDVDInfo::GetNameAndSerialNum
bool GetNameAndSerialNum(QString &Name, QString &SerialNumber)
Definition: mythdvdinfo.cpp:109
MythFileOpen
int MythFileOpen(const char *Pathname, int Flags)
Definition: mythiowrapper.cpp:112
crc32
static uint32_t crc32(const unsigned char *data, int len)
Definition: dsmcc.cpp:612
mythiowrapper.h