MythTV  master
upnputil.cpp
Go to the documentation of this file.
1 // Program Name: upnputil.cpp
3 // Created : Jan. 15, 2007
4 //
5 // Purpose : Global Helper Methods...
6 //
7 // Copyright (c) 2007 David Blain <dblain@mythtv.org>
8 //
9 // Licensed under the GPL v2 or later, see COPYING for details
10 //
12 
13 // POSIX headers
14 #include <sys/types.h>
15 #include <sys/time.h>
16 #include <cerrno>
17 
18 // Qt headers
19 #include <QUuid>
20 #include <QStringList>
21 
22 // MythTV headers
23 #include "upnputil.h"
24 #include "upnp.h"
25 #include "upnphelpers.h"
26 #include "compat.h"
27 #include "mythconfig.h" // for HAVE_GETIFADDRS
28 #include "mythlogging.h"
29 #include "httprequest.h"
30 
31 // POSIX headers 2, needs to be after compat.h for OS X
32 #ifndef _WIN32
33 #include <sys/ioctl.h>
34 #endif // _WIN32
35 
36 
37 #include <zlib.h>
38 #undef Z_NULL
39 #define Z_NULL nullptr
40 
42 //
44 
45 QString LookupUDN( const QString &sDeviceType )
46 {
47 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
48  QStringList sList = sDeviceType.split(':', QString::SkipEmptyParts);
49 #else
50  QStringList sList = sDeviceType.split(':', Qt::SkipEmptyParts);
51 #endif
52  QString sLoc = "LookupUDN(" + sDeviceType + ')';
53 
54  if (sList.size() <= 2)
55  {
56  LOG(VB_GENERAL, LOG_ERR, sLoc + "- bad device type '" +
57  sDeviceType + "', not enough tokens");
58  return QString();
59  }
60 
61  sList.removeLast();
62  QString sName = "UPnP/UDN/" + sList.last();
63  QString sUDN = UPnp::GetConfiguration()->GetValue( sName, "" );
64 
65  LOG(VB_UPNP, LOG_INFO, sLoc + " sName=" + sName + ", sUDN=" + sUDN);
66 
67  // Generate new UUID if current is missing or broken
68  if (sUDN.isEmpty() || sUDN.startsWith("{"))
69  {
70  sUDN = QUuid::createUuid().toString();
71  // QUuid returns the uuid enclosed with braces {} which is not
72  // DLNA compliant, we need to remove them
73  sUDN = sUDN.mid(1, 36);
74 
76 
77  pConfig->SetValue( sName, sUDN );
78  pConfig->Save();
79  }
80 
81  return( sUDN );
82 }
83 
85 //
87 
89 {
90  return (t1.tv_sec < t2.tv_sec) ||
91  ((t1.tv_sec == t2.tv_sec) && (t1.tv_usec < t2.tv_usec));
92 }
93 
95 //
97 
99 {
100  return (t1.tv_sec == t2.tv_sec) && (t1.tv_usec == t2.tv_usec);
101 }
102 
104 //
106 
108 {
109  uSecs += t.tv_usec;
110 
111  t.tv_sec += (uSecs / 1000000);
112  t.tv_usec = (uSecs % 1000000);
113 }
114 
116 //
118 
119 void AddSecondsToTaskTime( TaskTime &t, long nSecs )
120 {
121  t.tv_sec += nSecs;
122 }
123 
131 {
132  QStringList mimeTypes = HTTPRequest::GetSupportedMimeTypes();
133 
134  QString protocolStr("http-get:*:%1:%2");
135  QStringList protocolList;
136  QStringList::Iterator it;
137  for (it = mimeTypes.begin(); it < mimeTypes.end(); ++it)
138  {
139  // HACK
141  if (*it == "video/mpeg")
142  {
143  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=MPEG_PS_PAL;" + flags);
144  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=MPEG_PS_NTSC;" + flags);
145  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=MPEG_PS_SD_DTS;" + flags);
146  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=AVC_TS_NA_ISO;" + flags);
147  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=MPEG_TS_HD_NA_ISO;" + flags);
148  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=MPEG_TS_SD_NA_ISO;" + flags);
149  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=AVC_TS_EU_ISO;" + flags);
150  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=MPEG_TS_SD_EU_ISO;" + flags);
151  }
152  else if (*it == "audio/mpeg")
153  {
154  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=MP3;" + flags); // Technically we don't actually serve these
155  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=MP3X;" + flags);
156  }
157  else if (*it == "audio/mp4")
158  {
159  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=AAC_ISO_320;" + flags);
160  }
161  else if (*it == "audio/vnd.dolby.dd-raw")
162  {
163  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=AC3;" + flags);
164  }
165  else if (*it == "audio/x-ms-wma")
166  {
167  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=WMAFULL;" + flags);
168  }
169  else
170  protocolList << protocolStr.arg(*it).arg("*");
171  }
172 
173  return protocolList;
174 }
175 
182 QStringList GetSinkProtocolInfos()
183 {
184  QStringList mimeTypes = HTTPRequest::GetSupportedMimeTypes();
185 
186  QStringList protocolList;
187  QStringList::Iterator it;
188  for (it = mimeTypes.begin(); it < mimeTypes.end(); ++it)
189  {
190  protocolList << QString("http-get:*:%1:*").arg(*it);
191  }
192 
193  return protocolList;
194 }
195 
ktm_b
ktm_b
Definition: upnphelpers.h:252
GetSourceProtocolInfos
QStringList GetSourceProtocolInfos()
Return a QStringList containing the supported Source Protocols.
Definition: upnputil.cpp:130
HTTPRequest::GetSupportedMimeTypes
static QStringList GetSupportedMimeTypes()
Definition: httprequest.cpp:1060
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
TaskTime
struct timeval TaskTime
Definition: httpserver.h:45
operator<
bool operator<(TaskTime t1, TaskTime t2)
Definition: upnputil.cpp:88
LookupUDN
QString LookupUDN(const QString &sDeviceType)
Definition: upnputil.cpp:45
Configuration::SetValue
virtual void SetValue(const QString &sSetting, int value)=0
upnp.h
mythlogging.h
hardwareprofile.i18n.t
t
Definition: i18n.py:36
compat.h
GetSinkProtocolInfos
QStringList GetSinkProtocolInfos()
Return a QStringList containing the supported Sink Protocols.
Definition: upnputil.cpp:182
operator==
bool operator==(TaskTime t1, TaskTime t2)
Definition: upnputil.cpp:98
kv1_5_flag
kv1_5_flag
Definition: upnphelpers.h:258
upnphelpers.h
AddMicroSecToTaskTime
void AddMicroSecToTaskTime(TaskTime &t, suseconds_t uSecs)
Definition: upnputil.cpp:107
Configuration
Definition: configuration.h:22
Configuration::Save
virtual bool Save(void)=0
DLNA::FlagsString
QString FlagsString(uint32_t flags)
Convert an integer composed of DNLA_Flags to a properly formatted string for use in XML.
Definition: upnphelpers.cpp:367
ktm_s
ktm_s
Definition: upnphelpers.h:250
Configuration::GetValue
virtual int GetValue(const QString &sSetting, int Default)=0
suseconds_t
long suseconds_t
Definition: compat.h:332
UPnp::GetConfiguration
static Configuration * GetConfiguration()
Definition: upnp.cpp:84
AddSecondsToTaskTime
void AddSecondsToTaskTime(TaskTime &t, long nSecs)
Definition: upnputil.cpp:119
upnputil.h
httprequest.h