Parent: [c556d9] (diff)

Download this file

mediarenderer.cxx    301 lines (274 with data), 9.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* Copyright (C) 2006-2016 J.F.Dockes
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "libupnpp/config.h"
#include "libupnpp/control/mediarenderer.hxx"
#include <ostream> // for endl
#include <string> // for string
#include <utility> // for pair
#include <vector> // for vector
#include "libupnpp/control/description.hxx" // for UPnPDeviceDesc, etc
#include "libupnpp/control/discovery.hxx" // for UPnPDeviceDirectory, etc
#include "libupnpp/control/renderingcontrol.hxx" // for RenderingControl, etc
#include "libupnpp/log.hxx" // for LOGERR, LOGINF
using namespace std;
using namespace std::placeholders;
using namespace UPnPP;
namespace UPnPClient {
const string
MediaRenderer::DType("urn:schemas-upnp-org:device:MediaRenderer:1");
class MediaRenderer::Internal {
public:
std::weak_ptr<RenderingControl> rdc;
std::weak_ptr<AVTransport> avt;
std::weak_ptr<OHProduct> ohpr;
std::weak_ptr<OHPlaylist> ohpl;
std::weak_ptr<OHTime> ohtm;
std::weak_ptr<OHVolume> ohvl;
std::weak_ptr<OHReceiver> ohrc;
std::weak_ptr<OHRadio> ohrd;
std::weak_ptr<OHInfo> ohif;
std::weak_ptr<OHSender> ohsn;
};
// We don't include a version in comparisons, as we are satisfied with
// version 1
bool MediaRenderer::isMRDevice(const string& st)
{
const string::size_type sz(DType.size()-2);
return !DType.compare(0, sz, st, 0, sz);
}
// Look at all service descriptions and store parent devices for
// either UPnP RenderingControl or OpenHome Product. Some entries will
// be set multiple times, which does not matter
static bool MDAccum(std::unordered_map<string, UPnPDeviceDesc>* out,
const string& friendlyName,
const UPnPDeviceDesc& device,
const UPnPServiceDesc& service)
{
//LOGDEB("MDAccum: friendlyname: " << friendlyName <<
// " dev friendlyName " << device.friendlyName << endl);
if (
(RenderingControl::isRDCService(service.serviceType) ||
OHProduct::isOHPrService(service.serviceType))
&&
(friendlyName.empty() || !friendlyName.compare(device.friendlyName))) {
//LOGDEB("MDAccum setting " << device.UDN << endl);
(*out)[device.UDN] = device;
}
return true;
}
bool MediaRenderer::getDeviceDescs(vector<UPnPDeviceDesc>& devices,
const string& friendlyName)
{
std::unordered_map<string, UPnPDeviceDesc> mydevs;
UPnPDeviceDirectory::Visitor visitor = bind(MDAccum, &mydevs, friendlyName,
_1, _2);
UPnPDeviceDirectory::getTheDir()->traverse(visitor);
for (std::unordered_map<string, UPnPDeviceDesc>::iterator it =
mydevs.begin(); it != mydevs.end(); it++)
devices.push_back(it->second);
return !devices.empty();
}
MediaRenderer::MediaRenderer(const UPnPDeviceDesc& desc)
: Device(desc)
{
if ((m = new Internal()) == 0) {
LOGERR("MediaRenderer::MediaRenderer: out of memory" << endl);
return;
}
}
MediaRenderer::~MediaRenderer()
{
delete m;
}
bool MediaRenderer::hasOpenHome()
{
return ohpr() ? true : false;
}
RDCH MediaRenderer::rdc()
{
if (desc() == 0)
return RDCH();
RDCH rdcl = m->rdc.lock();
if (rdcl)
return rdcl;
for (vector<UPnPServiceDesc>::const_iterator it = desc()->services.begin();
it != desc()->services.end(); it++) {
if (RenderingControl::isRDCService(it->serviceType)) {
rdcl = RDCH(new RenderingControl(*desc(), *it));
break;
}
}
if (!rdcl)
LOGDEB("MediaRenderer: RenderingControl service not found" << endl);
m->rdc = rdcl;
return rdcl;
}
AVTH MediaRenderer::avt()
{
AVTH avtl = m->avt.lock();
if (avtl)
return avtl;
for (vector<UPnPServiceDesc>::const_iterator it = desc()->services.begin();
it != desc()->services.end(); it++) {
if (AVTransport::isAVTService(it->serviceType)) {
avtl = AVTH(new AVTransport(*desc(), *it));
break;
}
}
if (!avtl)
LOGDEB("MediaRenderer: AVTransport service not found" << endl);
m->avt = avtl;
return avtl;
}
OHPRH MediaRenderer::ohpr()
{
OHPRH ohprl = m->ohpr.lock();
if (ohprl)
return ohprl;
for (vector<UPnPServiceDesc>::const_iterator it = desc()->services.begin();
it != desc()->services.end(); it++) {
if (OHProduct::isOHPrService(it->serviceType)) {
ohprl = OHPRH(new OHProduct(*desc(), *it));
break;
}
}
if (!ohprl)
LOGDEB("MediaRenderer: OHProduct service not found" << endl);
m->ohpr = ohprl;
return ohprl;
}
OHPLH MediaRenderer::ohpl()
{
OHPLH ohpll = m->ohpl.lock();
if (ohpll)
return ohpll;
for (vector<UPnPServiceDesc>::const_iterator it = desc()->services.begin();
it != desc()->services.end(); it++) {
if (OHPlaylist::isOHPlService(it->serviceType)) {
ohpll = OHPLH(new OHPlaylist(*desc(), *it));
break;
}
}
if (!ohpll)
LOGDEB("MediaRenderer: OHPlaylist service not found" << endl);
m->ohpl = ohpll;
return ohpll;
}
OHRCH MediaRenderer::ohrc()
{
OHRCH ohrcl = m->ohrc.lock();
if (ohrcl)
return ohrcl;
for (vector<UPnPServiceDesc>::const_iterator it = desc()->services.begin();
it != desc()->services.end(); it++) {
if (OHReceiver::isOHRcService(it->serviceType)) {
ohrcl = OHRCH(new OHReceiver(*desc(), *it));
break;
}
}
if (!ohrcl)
LOGDEB("MediaRenderer: OHReceiver service not found" << endl);
m->ohrc = ohrcl;
return ohrcl;
}
OHRDH MediaRenderer::ohrd()
{
OHRDH handle = m->ohrd.lock();
if (handle)
return handle;
for (vector<UPnPServiceDesc>::const_iterator it = desc()->services.begin();
it != desc()->services.end(); it++) {
if (OHRadio::isOHRdService(it->serviceType)) {
handle = OHRDH(new OHRadio(*desc(), *it));
break;
}
}
if (!handle)
LOGDEB("MediaRenderer: OHRadio service not found" << endl);
m->ohrd = handle;
return handle;
}
OHIFH MediaRenderer::ohif()
{
OHIFH handle = m->ohif.lock();
if (handle)
return handle;
for (vector<UPnPServiceDesc>::const_iterator it = desc()->services.begin();
it != desc()->services.end(); it++) {
if (OHInfo::isOHInfoService(it->serviceType)) {
handle = OHIFH(new OHInfo(*desc(), *it));
break;
}
}
if (!handle)
LOGDEB("MediaRenderer: OHInfo service not found" << endl);
m->ohif = handle;
return handle;
}
OHSNH MediaRenderer::ohsn()
{
OHSNH handle = m->ohsn.lock();
if (handle)
return handle;
for (vector<UPnPServiceDesc>::const_iterator it = desc()->services.begin();
it != desc()->services.end(); it++) {
if (OHSender::isOHSenderService(it->serviceType)) {
handle = OHSNH(new OHSender(*desc(), *it));
break;
}
}
if (!handle)
LOGDEB("MediaRenderer: OHSender service not found" << endl);
m->ohsn = handle;
return handle;
}
OHTMH MediaRenderer::ohtm()
{
OHTMH ohtml = m->ohtm.lock();
if (ohtml)
return ohtml;
for (vector<UPnPServiceDesc>::const_iterator it = desc()->services.begin();
it != desc()->services.end(); it++) {
if (OHTime::isOHTMService(it->serviceType)) {
ohtml = OHTMH(new OHTime(*desc(), *it));
break;
}
}
if (!ohtml)
LOGDEB("MediaRenderer: OHTime service not found" << endl);
m->ohtm = ohtml;
return ohtml;
}
OHVLH MediaRenderer::ohvl()
{
OHVLH ohvll = m->ohvl.lock();
if (ohvll)
return ohvll;
for (vector<UPnPServiceDesc>::const_iterator it = desc()->services.begin();
it != desc()->services.end(); it++) {
if (OHVolume::isOHVLService(it->serviceType)) {
ohvll = OHVLH(new OHVolume(*desc(), *it));
break;
}
}
if (!ohvll)
LOGDEB("MediaRenderer: OHVolume service not found" << endl);
m->ohvl = ohvll;
return ohvll;
}
}