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  QStringList sList = sDeviceType.split(':', QString::SkipEmptyParts);
48  QString sLoc = "LookupUDN(" + sDeviceType + ')';
49 
50  if (sList.size() <= 2)
51  {
52  LOG(VB_GENERAL, LOG_ERR, sLoc + "- bad device type '" +
53  sDeviceType + "', not enough tokens");
54  return QString();
55  }
56 
57  sList.removeLast();
58  QString sName = "UPnP/UDN/" + sList.last();
59  QString sUDN = UPnp::GetConfiguration()->GetValue( sName, "" );
60 
61  LOG(VB_UPNP, LOG_INFO, sLoc + " sName=" + sName + ", sUDN=" + sUDN);
62 
63  // Generate new UUID if current is missing or broken
64  if (sUDN.isEmpty() || sUDN.startsWith("{"))
65  {
66  sUDN = QUuid::createUuid().toString();
67  // QUuid returns the uuid enclosed with braces {} which is not
68  // DLNA compliant, we need to remove them
69  sUDN = sUDN.mid(1, 36);
70 
72 
73  pConfig->SetValue( sName, sUDN );
74  pConfig->Save();
75  }
76 
77  return( sUDN );
78 }
79 
81 //
83 
85 {
86  return (t1.tv_sec < t2.tv_sec) ||
87  ((t1.tv_sec == t2.tv_sec) && (t1.tv_usec < t2.tv_usec));
88 }
89 
91 //
93 
95 {
96  return (t1.tv_sec == t2.tv_sec) && (t1.tv_usec == t2.tv_usec);
97 }
98 
100 //
102 
104 {
105  uSecs += t.tv_usec;
106 
107  t.tv_sec += (uSecs / 1000000);
108  t.tv_usec = (uSecs % 1000000);
109 }
110 
112 //
114 
115 void AddSecondsToTaskTime( TaskTime &t, long nSecs )
116 {
117  t.tv_sec += nSecs;
118 }
119 
127 {
128  QStringList mimeTypes = HTTPRequest::GetSupportedMimeTypes();
129 
130  QString protocolStr("http-get:*:%1:%2");
131  QStringList protocolList;
132  QStringList::Iterator it;
133  for (it = mimeTypes.begin(); it < mimeTypes.end(); ++it)
134  {
135  // HACK
137  if (*it == "video/mpeg")
138  {
139  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=MPEG_PS_PAL;" + flags);
140  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=MPEG_PS_NTSC;" + flags);
141  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=MPEG_PS_SD_DTS;" + flags);
142  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=AVC_TS_NA_ISO;" + flags);
143  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=MPEG_TS_HD_NA_ISO;" + flags);
144  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=MPEG_TS_SD_NA_ISO;" + flags);
145  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=AVC_TS_EU_ISO;" + flags);
146  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=MPEG_TS_SD_EU_ISO;" + flags);
147  }
148  else if (*it == "audio/mpeg")
149  {
150  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=MP3;" + flags); // Technically we don't actually serve these
151  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=MP3X;" + flags);
152  }
153  else if (*it == "audio/mp4")
154  {
155  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=AAC_ISO_320;" + flags);
156  }
157  else if (*it == "audio/vnd.dolby.dd-raw")
158  {
159  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=AC3;" + flags);
160  }
161  else if (*it == "audio/x-ms-wma")
162  {
163  protocolList << protocolStr.arg(*it).arg("DLNA.ORG_PN=WMAFULL;" + flags);
164  }
165  else
166  protocolList << protocolStr.arg(*it).arg("*");
167  }
168 
169  return protocolList;
170 }
171 
178 QStringList GetSinkProtocolInfos()
179 {
180  QStringList mimeTypes = HTTPRequest::GetSupportedMimeTypes();
181 
182  QStringList protocolList;
183  QStringList::Iterator it;
184  for (it = mimeTypes.begin(); it < mimeTypes.end(); ++it)
185  {
186  protocolList << QString("http-get:*:%1:*").arg(*it);
187  }
188 
189  return protocolList;
190 }
191 
QString LookupUDN(const QString &sDeviceType)
Definition: upnputil.cpp:45
kv1_5_flag
Definition: upnphelpers.h:258
virtual int GetValue(const QString &sSetting, int Default)=0
QString FlagsString(uint32_t flags)
Convert an integer composed of DNLA_Flags to a properly formatted string for use in XML.
bool operator==(TaskTime t1, TaskTime t2)
Definition: upnputil.cpp:94
struct timeval TaskTime
Definition: httpserver.h:45
void AddMicroSecToTaskTime(TaskTime &t, suseconds_t uSecs)
Definition: upnputil.cpp:103
static QStringList GetSupportedMimeTypes()
virtual bool Save(void)=0
static Configuration * GetConfiguration()
Definition: upnp.cpp:71
void AddSecondsToTaskTime(TaskTime &t, long nSecs)
Definition: upnputil.cpp:115
ktm_s
Definition: upnphelpers.h:250
ktm_b
Definition: upnphelpers.h:252
long suseconds_t
Definition: compat.h:331
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QStringList GetSourceProtocolInfos()
Return a QStringList containing the supported Source Protocols.
Definition: upnputil.cpp:126
QStringList GetSinkProtocolInfos()
Return a QStringList containing the supported Sink Protocols.
Definition: upnputil.cpp:178
bool operator<(TaskTime t1, TaskTime t2)
Definition: upnputil.cpp:84
virtual void SetValue(const QString &sSetting, int value)=0