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 LICENSE 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 #include "mythcorecontext.h"
31 #include "configuration.h"
32 
33 // POSIX headers 2, needs to be after compat.h for OS X
34 #ifndef _WIN32
35 #include <sys/ioctl.h>
36 #endif // _WIN32
37 
38 
39 #include <zlib.h>
40 #undef Z_NULL
41 #define Z_NULL nullptr
42 
44 //
46 
47 QString LookupUDN( const QString &sDeviceType )
48 {
49 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
50  QStringList sList = sDeviceType.split(':', QString::SkipEmptyParts);
51 #else
52  QStringList sList = sDeviceType.split(':', Qt::SkipEmptyParts);
53 #endif
54  QString sLoc = "LookupUDN(" + sDeviceType + ')';
55 
56  if (sList.size() <= 2)
57  {
58  LOG(VB_GENERAL, LOG_ERR, sLoc + "- bad device type '" +
59  sDeviceType + "', not enough tokens");
60  return QString();
61  }
62 
63  sList.removeLast();
65  QString sName = "UPnP/UDN/" + sList.last();
66  QString sUDN = pConfig->GetValue( sName, "" );
67 
68  LOG(VB_UPNP, LOG_INFO, sLoc + " sName=" + sName + ", sUDN=" + sUDN);
69 
70  // Generate new UUID if current is missing or broken
71  if (sUDN.isEmpty() || sUDN.startsWith("{"))
72  {
73  sUDN = QUuid::createUuid().toString();
74  // QUuid returns the uuid enclosed with braces {} which is not
75  // DLNA compliant, we need to remove them
76  sUDN = sUDN.mid(1, 36);
77 
78  pConfig->SetValue( sName, sUDN );
79  pConfig->Save();
80  }
81 
82  return( sUDN );
83 }
84 
92 {
93  QStringList mimeTypes = HTTPRequest::GetSupportedMimeTypes();
94 
95  QString protocolStr("http-get:*:%1:%2");
96  QStringList protocolList;
97  QStringList::Iterator it;
98  for (it = mimeTypes.begin(); it < mimeTypes.end(); ++it)
99  {
100  // HACK
102  if (*it == "video/mpeg")
103  {
104  protocolList << protocolStr.arg(*it, "DLNA.ORG_PN=MPEG_PS_PAL;" + flags);
105  protocolList << protocolStr.arg(*it, "DLNA.ORG_PN=MPEG_PS_NTSC;" + flags);
106  protocolList << protocolStr.arg(*it, "DLNA.ORG_PN=MPEG_PS_SD_DTS;" + flags);
107  protocolList << protocolStr.arg(*it, "DLNA.ORG_PN=AVC_TS_NA_ISO;" + flags);
108  protocolList << protocolStr.arg(*it, "DLNA.ORG_PN=MPEG_TS_HD_NA_ISO;" + flags);
109  protocolList << protocolStr.arg(*it, "DLNA.ORG_PN=MPEG_TS_SD_NA_ISO;" + flags);
110  protocolList << protocolStr.arg(*it, "DLNA.ORG_PN=AVC_TS_EU_ISO;" + flags);
111  protocolList << protocolStr.arg(*it, "DLNA.ORG_PN=MPEG_TS_SD_EU_ISO;" + flags);
112  }
113  else if (*it == "audio/mpeg")
114  {
115  protocolList << protocolStr.arg(*it, "DLNA.ORG_PN=MP3;" + flags); // Technically we don't actually serve these
116  protocolList << protocolStr.arg(*it, "DLNA.ORG_PN=MP3X;" + flags);
117  }
118  else if (*it == "audio/mp4")
119  {
120  protocolList << protocolStr.arg(*it, "DLNA.ORG_PN=AAC_ISO_320;" + flags);
121  }
122  else if (*it == "audio/vnd.dolby.dd-raw")
123  {
124  protocolList << protocolStr.arg(*it, "DLNA.ORG_PN=AC3;" + flags);
125  }
126  else if (*it == "audio/x-ms-wma")
127  {
128  protocolList << protocolStr.arg(*it, "DLNA.ORG_PN=WMAFULL;" + flags);
129  }
130  else
131  protocolList << protocolStr.arg(*it, "*");
132  }
133 
134  return protocolList;
135 }
136 
143 QStringList GetSinkProtocolInfos()
144 {
145  QStringList mimeTypes = HTTPRequest::GetSupportedMimeTypes();
146 
147  QStringList protocolList;
148  QStringList::Iterator it;
149  for (it = mimeTypes.begin(); it < mimeTypes.end(); ++it)
150  {
151  protocolList << QString("http-get:*:%1:*").arg(*it);
152  }
153 
154  return protocolList;
155 }
156 
DLNA::ktm_s
@ ktm_s
Definition: upnphelpers.h:253
GetSourceProtocolInfos
QStringList GetSourceProtocolInfos()
Return a QStringList containing the supported Source Protocols.
Definition: upnputil.cpp:91
HTTPRequest::GetSupportedMimeTypes
static QStringList GetSupportedMimeTypes()
Definition: httprequest.cpp:998
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
LookupUDN
QString LookupUDN(const QString &sDeviceType)
Definition: upnputil.cpp:47
Configuration::SetValue
virtual void SetValue(const QString &sSetting, int value)=0
upnp.h
mythlogging.h
compat.h
GetSinkProtocolInfos
QStringList GetSinkProtocolInfos()
Return a QStringList containing the supported Sink Protocols.
Definition: upnputil.cpp:143
DLNA::kv1_5_flag
@ kv1_5_flag
Definition: upnphelpers.h:261
upnphelpers.h
DLNA::ktm_b
@ ktm_b
Definition: upnphelpers.h:255
mythcorecontext.h
Configuration
Definition: configuration.h:20
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
configuration.h
Configuration::GetValue
virtual int GetValue(const QString &sSetting, int Default)=0
upnputil.h
httprequest.h
MythCoreContext::GetConfiguration
static Configuration * GetConfiguration()
Definition: mythcorecontext.cpp:1841