Child: [05d975] (diff)

Download this file

upexplo.cpp    545 lines (494 with data), 15.4 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// Exercise a variety of libupnpp features...
#include <getopt.h>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <mutex>
#include <condition_variable>
#include "libupnpp/upnpplib.hxx"
#include "libupnpp/log.hxx"
#include "libupnpp/upnpputils.hxx"
#include "libupnpp/control/service.hxx"
#include "libupnpp/control/cdirectory.hxx"
#include "libupnpp/control/mediarenderer.hxx"
#include "libupnpp/control/renderingcontrol.hxx"
#include "libupnpp/control/discovery.hxx"
using namespace UPnPClient;
using namespace UPnPP;
using namespace std;
UPnPDeviceDirectory *superdir;
std::mutex reporterLock;
std::condition_variable evloopcond;
vector<string> deviceFNs;
vector<string> deviceUDNs;
vector<string> deviceTypes;
static void clearDevices() {
deviceFNs.clear();
deviceUDNs.clear();
deviceTypes.clear();
}
static bool
reporter(const UPnPDeviceDesc& device, const UPnPServiceDesc&)
{
std::unique_lock<std::mutex> lock(reporterLock);
//cerr << "reporter: " << device.friendlyName << " s " <<
// device.deviceType << endl;
if (find(deviceUDNs.begin(), deviceUDNs.end(), device.UDN)
== deviceUDNs.end()) {
deviceFNs.push_back(device.friendlyName);
deviceUDNs.push_back(device.UDN);
deviceTypes.push_back(device.deviceType);
evloopcond.notify_all();
}
return true;
}
static bool traverser(const UPnPDeviceDesc& device, const UPnPServiceDesc& srv)
{
if (find(deviceUDNs.begin(), deviceUDNs.end(), device.UDN)
== deviceUDNs.end()) {
cout << device.friendlyName <<" ("<< device.deviceType << ")" << endl;
deviceFNs.push_back(device.friendlyName);
deviceUDNs.push_back(device.UDN);
deviceTypes.push_back(device.deviceType);
}
return true;
}
void listDevices()
{
cout << "UPnP devices:" << endl;
static int cbindex = -1;
if (cbindex == -1) {
cbindex = UPnPDeviceDirectory::addCallback(reporter);
}
if (superdir == 0) {
superdir = UPnPDeviceDirectory::getTheDir(1);
if (superdir == 0) {
cerr << "can't get superdir\n";
exit(1);
}
}
// Until the initial delay is through, use the reporter to list
// devices as they come
unsigned int ndevices = 0;
for (;;) {
std::unique_lock<std::mutex> lock(reporterLock);
#if FUTURE
int ms = superdir->getRemainingDelayMs();
#else
int ms = superdir->getRemainingDelay() * 1000;
#endif
if (ms > 0) {
evloopcond.wait_for(lock, std::chrono::milliseconds(ms));
if (deviceFNs.size() > ndevices) {
for (unsigned int i = ndevices; i < deviceFNs.size(); i++) {
cout << deviceFNs[i] <<" ("<< deviceTypes[i] << ")" << endl;
}
ndevices = deviceFNs.size();
}
} else {
break;
}
}
cerr << "Initial delay done\n";
// Called after initial delay done. Unset the callback and
// traverse the directory
if (cbindex >= 0) {
UPnPDeviceDirectory::delCallback(cbindex);
cbindex = -2;
}
clearDevices();
auto ret = superdir->traverse(traverser);
cerr << "Now having " << deviceUDNs.size() << " devices " << endl;
}
void listServers()
{
cout << "Content Directories:" << endl;
vector<CDSH> dirservices;
if (!ContentDirectory::getServices(dirservices)) {
cerr << "listDirServices failed" << endl;
return;
}
for (vector<CDSH>::iterator it = dirservices.begin();
it != dirservices.end(); it++) {
cout << (*it)->getFriendlyName() << endl;
}
cout << endl;
}
void listPlayers()
{
cout << "Media Renderers:" << endl;
vector<UPnPDeviceDesc> vdds;
if (!MediaRenderer::getDeviceDescs(vdds)) {
cerr << "MediaRenderer::getDeviceDescs" << endl;
return;
}
for (auto& entry : vdds) {
cout << entry.friendlyName << endl;
}
cout << endl;
}
class MReporter : public UPnPClient::VarEventReporter {
public:
void changed(const char *nm, int value)
{
cout << "Changed: " << nm << " : " << value << endl;
}
void changed(const char *nm, const char *value)
{
cout << "Changed: " << nm << " : " << value << endl;
}
void changed(const char *nm, UPnPDirObject meta)
{
string s = meta.dump();
cout << "Changed: " << nm << " : " << s << endl;
}
};
MRDH getRenderer(const string& friendlyName)
{
if (superdir == 0) {
superdir = UPnPDeviceDirectory::getTheDir();
}
UPnPDeviceDesc ddesc;
if (superdir->getDevByFName(friendlyName, ddesc)) {
return MRDH(new MediaRenderer(ddesc));
}
cerr << "getDevByFname failed" << endl;
return MRDH();
}
void getsetVolume(const string& friendlyName, int volume = -1)
{
MRDH rdr = getRenderer(friendlyName);
if (!rdr) {
return;
}
RDCH rdc = rdr->rdc();
if (!rdc) {
cerr << "Device has no RenderingControl service" << endl;
return;
}
if (volume == -1) {
volume = rdc->getVolume();
cout << "Current volume: " << volume << endl;
return;
} else {
if ((volume = rdc->setVolume(volume)) != 0) {
cerr << "Error setting volume: " << volume << endl;
return;
}
}
}
void tpMonitor(const string& friendlyName)
{
MRDH rdr = getRenderer(friendlyName);
if (!rdr) {
return;
}
AVTH avt = rdr->avt();
if (!avt) {
cerr << "Device has no AVTransport service" << endl;
return;
}
MReporter reporter;
avt->installReporter(&reporter);
while (true) {
AVTransport::PositionInfo info;
int ret;
if ((ret = avt->getPositionInfo(info))) {
cerr << "getPositionInfo failed. Code " << ret << endl;
} else {
cout << info.trackmeta.m_title << " reltime " << info.reltime
<< endl;
}
sleep(2);
}
}
int tpAlbumArt(const string& fname)
{
MRDH rdr = getRenderer(fname);
if (!rdr) {
return 1;
}
AVTH avt = rdr->avt();
if (!avt) {
cerr << "Device " << fname << " has no AVTransport service" << endl;
return 1;
}
string uri;
AVTransport::TransportInfo tinfo;
int ret;
if ((ret = avt->getTransportInfo(tinfo))) {
cerr << "getTransportInfo failed. Code " << ret << endl;
return 1;
} else if (tinfo.tpstatus == AVTransport::TPS_Ok &&
(tinfo.tpstate == AVTransport::Playing ||
tinfo.tpstate == AVTransport::PausedPlayback)) {
AVTransport::PositionInfo info;
if ((ret = avt->getPositionInfo(info))) {
cerr << "getPositionInfo failed. Code " << ret << endl;
return 1;
} else {
uri = info.trackmeta.getprop("upnp:albumArtURI");
}
}
cout << uri << endl;
return 0;
}
void tpPlayStop(const string& friendlyName, bool doplay)
{
MRDH rdr = getRenderer(friendlyName);
if (!rdr) {
return;
}
AVTH avt = rdr->avt();
if (!avt) {
cerr << "Device has no AVTransport service" << endl;
return;
}
int ret;
if (doplay) {
ret = avt->play();
} else {
ret = avt->stop();
}
if (ret != 0) {
cerr << "Operation failed: code: " << ret << endl;
}
}
void tpPause(const string& friendlyName)
{
MRDH rdr = getRenderer(friendlyName);
if (!rdr) {
return;
}
AVTH avt = rdr->avt();
if (!avt) {
cerr << "Device has no AVTransport service" << endl;
return;
}
avt->pause();
}
void readdir(const string& friendlyName, const string& cid)
{
cout << "readdir: [" << friendlyName << "] [" << cid << "]" << endl;
CDSH server;
if (!ContentDirectory::getServerByName(friendlyName, server)) {
cerr << "Server not found" << endl;
return;
}
UPnPDirContent dirbuf;
int code = server->readDir(cid, dirbuf);
if (code) {
cerr << LibUPnP::errAsString("readdir", code) << endl;
return;
}
cout << "Browse: got " << dirbuf.m_containers.size() <<
" containers and " << dirbuf.m_items.size() << " items " << endl;
for (unsigned int i = 0; i < dirbuf.m_containers.size(); i++) {
cout << dirbuf.m_containers[i].dump();
}
for (unsigned int i = 0; i < dirbuf.m_items.size(); i++) {
cout << dirbuf.m_items[i].dump();
}
}
void getMetadata(const string& friendlyName, const string& cid)
{
cout << "getMeta: [" << friendlyName << "] [" << cid << "]" << endl;
CDSH server;
if (!ContentDirectory::getServerByName(friendlyName, server)) {
cerr << "Server not found" << endl;
return;
}
UPnPDirContent dirbuf;
int code = server->getMetadata(cid, dirbuf);
if (code) {
cerr << LibUPnP::errAsString("readdir", code) << endl;
return;
}
cout << "getMeta: got " << dirbuf.m_containers.size() <<
" containers and " << dirbuf.m_items.size() << " items " << endl;
for (unsigned int i = 0; i < dirbuf.m_containers.size(); i++) {
cout << dirbuf.m_containers[i].dump();
}
for (unsigned int i = 0; i < dirbuf.m_items.size(); i++) {
cout << dirbuf.m_items[i].dump();
}
}
void search(const string& friendlyName, const string& ss)
{
cout << "search: [" << friendlyName << "] [" << ss << "]" << endl;
CDSH server;
if (!ContentDirectory::getServerByName(friendlyName, server)) {
cerr << "Server not found" << endl;
return;
}
UPnPDirContent dirbuf;
string cid("0");
int code = server->search(cid, ss, dirbuf);
if (code) {
cerr << LibUPnP::errAsString("search", code) << endl;
return;
}
cout << "Search: got " << dirbuf.m_containers.size() <<
" containers and " << dirbuf.m_items.size() << " items " << endl;
for (unsigned int i = 0; i < dirbuf.m_containers.size(); i++) {
cout << dirbuf.m_containers[i].dump();
}
for (unsigned int i = 0; i < dirbuf.m_items.size(); i++) {
cout << dirbuf.m_items[i].dump();
}
}
void getSearchCaps(const string& friendlyName)
{
cout << "getSearchCaps: [" << friendlyName << "]" << endl;
CDSH server;
if (!ContentDirectory::getServerByName(friendlyName, server)) {
cerr << "Server not found" << endl;
return;
}
set<string> capa;
int code = server->getSearchCapabilities(capa);
if (code) {
cerr << LibUPnP::errAsString("readdir", code) << endl;
return;
}
if (capa.empty()) {
cout << "No search capabilities";
} else {
for (set<string>::const_iterator it = capa.begin();
it != capa.end(); it++) {
cout << "[" << *it << "]";
}
}
cout << endl;
}
static char *thisprog;
static char usage [] =
" -l : list devices\n"
" -r <server> <objid> list object id (root is '0')\n"
" -s <server> <searchstring> search for string\n"
" -m <server> <objid> : list object metadata\n"
" -c <server> get search capabilities\n"
" -M <renderer>: monitor AVTransport\n"
" -v <renderer> get volume\n"
" -V <renderer> <volume> set volume\n"
" -p <renderer> 1|0 play/stop\n"
" -P <renderer> pause\n"
" --album-art <renderer> print album art uri for playing track\n"
"\nFor now all <renderer> and <server> params should be "
"\"friendly names\", not UUIDs\n"
" \n"
;
static void
Usage(void)
{
fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
exit(1);
}
static int op_flags;
#define OPT_l 0x1
#define OPT_r 0x2
#define OPT_s 0x4
#define OPT_m 0x8
#define OPT_c 0x10
#define OPT_M 0x20
#define OPT_v 0x40
#define OPT_V 0x80
#define OPT_p 0x100
#define OPT_P 0x200
#define OPT_aa 0x10000
static struct option long_options[] = {
{"album-art", 0, 0, OPT_aa},
{0, 0, 0, 0}
};
int main(int argc, char *argv[])
{
string fname;
string arg;
thisprog = argv[0];
int ret;
int option_index = 0;
while ((ret = getopt_long(argc, argv, "MPSVclmprsvx",
long_options, &option_index)) != -1) {
switch (ret) {
case 'M': if (op_flags) Usage(); op_flags |= OPT_M; break;
case 'P': if (op_flags) Usage(); op_flags |= OPT_P; break;
case 'V': if (op_flags) Usage(); op_flags |= OPT_V; break;
case 'c': if (op_flags) Usage(); op_flags |= OPT_c; break;
case 'l': if (op_flags) Usage(); op_flags |= OPT_l; break;
case 'm': if (op_flags) Usage(); op_flags |= OPT_m; break;
case 'p': if (op_flags) Usage(); op_flags |= OPT_p; break;
case 'r': if (op_flags) Usage(); op_flags |= OPT_r; break;
case 's': if (op_flags) Usage(); op_flags |= OPT_s; break;
case 'v': if (op_flags) Usage(); op_flags |= OPT_v; break;
case OPT_aa: if (op_flags) Usage(); op_flags |= OPT_aa; break;
default:
Usage();
}
}
if (op_flags & (OPT_l)) {
if (optind < argc)
Usage();
}
if (op_flags & (OPT_c | OPT_v | OPT_P | OPT_M | OPT_aa)) {
if (optind != argc - 1)
Usage();
fname = argv[optind++];
}
if (op_flags & (OPT_r | OPT_s | OPT_m | OPT_V | OPT_p)) {
cerr << "optind " << optind << " argc " << argc << endl;
if (optind != argc - 2)
Usage();
fname = argv[optind++];
arg = argv[optind++];
}
if (Logger::getTheLog("/tmp/upexplo.log") == 0) {
cerr << "Can't initialize log" << endl;
return 1;
}
Logger::getTheLog("")->setLogLevel(Logger::LLDEB1);
string hwa;
LibUPnP *mylib = LibUPnP::getLibUPnP(false, &hwa);
if (!mylib) {
cerr << "Can't get LibUPnP" << endl;
return 1;
}
cerr << "hwaddr " << hwa << endl;
if (!mylib->ok()) {
cerr << "Lib init failed: " <<
mylib->errAsString("main", mylib->getInitError()) << endl;
return 1;
}
mylib->setLogFileName("/tmp/libupnp.log", LibUPnP::LogLevelDebug);
if ((op_flags & OPT_l)) {
while (true) {
listDevices();
sleep(5);
}
} else if ((op_flags & OPT_m)) {
getMetadata(fname, arg);
} else if ((op_flags & OPT_r)) {
readdir(fname, arg);
} else if ((op_flags & OPT_s)) {
search(fname, arg);
} else if ((op_flags & OPT_c)) {
getSearchCaps(fname);
} else if ((op_flags & OPT_V)) {
int volume = atoi(arg.c_str());
getsetVolume(fname, volume);
} else if ((op_flags & OPT_v)) {
getsetVolume(fname);
} else if ((op_flags & OPT_M)) {
tpMonitor(fname);
} else if ((op_flags & OPT_p)) {
int iarg = atoi(arg.c_str());
tpPlayStop(fname, iarg);
} else if ((op_flags & OPT_P)) {
tpPause(fname);
} else if ((op_flags & OPT_aa)) {
return tpAlbumArt(fname);
} else {
Usage();
}
return 0;
}