Parent: [4e7eb7] (diff)

Child: [6e6611] (diff)

Download this file

avtransport.cxx    469 lines (424 with data), 16.8 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
/* Copyright (C) 2014 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <string>
#include <functional>
using namespace std;
using namespace std::placeholders;
#include <upnp/upnp.h>
#include "libupnpp/soaphelp.hxx"
#include "libupnpp/upnpp_p.hxx"
#include "libupnpp/log.hxx"
#include "libupnpp/upnpavutils.hxx"
#include "libupnpp/control/avtransport.hxx"
#include "libupnpp/control/avlastchg.hxx"
#include "libupnpp/control/cdircontent.hxx"
namespace UPnPClient {
const string AVTransport::SType("urn:schemas-upnp-org:service:AVTransport:1");
// Check serviceType string (while walking the descriptions. We don't
// include a version in comparisons, as we are satisfied with version1
bool AVTransport::isAVTService(const string& st)
{
const string::size_type sz(SType.size()-2);
return !SType.compare(0, sz, st, 0, sz);
}
static AVTransport::TransportState stringToTpState(const string& s)
{
if (!stringuppercmp("STOPPED", s)) {
return AVTransport::Stopped;
} else if (!stringuppercmp("PLAYING", s)) {
return AVTransport::Playing;
} else if (!stringuppercmp("TRANSITIONING", s)) {
return AVTransport::Transitioning;
} else if (!stringuppercmp("PAUSED_PLAYBACK", s)) {
return AVTransport::PausedPlayback;
} else if (!stringuppercmp("PAUSED_RECORDING", s)) {
return AVTransport::PausedRecording;
} else if (!stringuppercmp("RECORDING", s)) {
return AVTransport::Recording;
} else if (!stringuppercmp("NO MEDIA PRESENT", s)) {
return AVTransport::NoMediaPresent;
} else {
LOGERR("AVTransport event: bad value for TransportState: "
<< s << endl);
return AVTransport::Unknown;
}
}
static AVTransport::TransportStatus stringToTpStatus(const string& s)
{
if (!stringuppercmp("OK", s)) {
return AVTransport::TPS_Ok;
} else if (!stringuppercmp("ERROR_OCCURRED", s)) {
return AVTransport::TPS_Error;
} else {
LOGERR("AVTransport event: bad value for TransportStatus: "
<< s << endl);
return AVTransport::TPS_Unknown;
}
}
static AVTransport::PlayMode stringToPlayMode(const string& s)
{
if (!stringuppercmp("NORMAL", s)) {
return AVTransport::PM_Normal;
} else if (!stringuppercmp("SHUFFLE", s)) {
return AVTransport::PM_Shuffle;
} else if (!stringuppercmp("REPEAT_ONE", s)) {
return AVTransport::PM_RepeatOne;
} else if (!stringuppercmp("REPEAT_ALL", s)) {
return AVTransport::PM_RepeatAll;
} else if (!stringuppercmp("RANDOM", s)) {
return AVTransport::PM_Random;
} else if (!stringuppercmp("DIRECT_1", s)) {
return AVTransport::PM_Direct1;
} else {
LOGERR("AVTransport event: bad value for PlayMode: "
<< s << endl);
return AVTransport::PM_Unknown;
}
}
void AVTransport::evtCallback(
const std::unordered_map<std::string, std::string>& props)
{
LOGDEB1("AVTransport::evtCallback:" << endl);
for (auto& entry: props) {
if (entry.first.compare("LastChange")) {
LOGINF("AVTransport:event: var not lastchange: "
<< entry.first << " -> " << entry.second << endl;);
continue;
}
LOGDEB1("AVTransport:event: "
<< entry.first << " -> " << entry.second << endl;);
std::unordered_map<std::string, std::string> props1;
if (!decodeAVLastChange(entry.second, props1)) {
LOGERR("AVTransport::evtCallback: bad LastChange value: "
<< entry.second << endl);
return;
}
for (auto& entry1: props1) {
if (!m_reporter) {
LOGDEB1("AVTransport::evtCallback: " << entry1.first << " -> "
<< entry1.second << endl);
continue;
}
if (!entry1.first.compare("TransportState")) {
m_reporter->changed(entry1.first.c_str(),
stringToTpState(entry1.second));
} else if (!entry1.first.compare("TransportStatus")) {
m_reporter->changed(entry1.first.c_str(),
stringToTpStatus(entry1.second));
} else if (!entry1.first.compare("CurrentPlayMode")) {
m_reporter->changed(entry1.first.c_str(),
stringToPlayMode(entry1.second));
} else if (!entry1.first.compare("CurrentTransportActions")) {
int iacts;
if (!CTAStringToBits(entry1.second, iacts))
m_reporter->changed(entry1.first.c_str(), iacts);
} else if (!entry1.first.compare("CurrentTrackURI") ||
!entry1.first.compare("AVTransportURI") ||
!entry1.first.compare("NextAVTransportURI")) {
m_reporter->changed(entry1.first.c_str(),
entry1.second.c_str());
} else if (!entry1.first.compare("TransportPlaySpeed") ||
!entry1.first.compare("CurrentTrack") ||
!entry1.first.compare("NumberOfTracks") ||
!entry1.first.compare("RelativeCounterPosition") ||
!entry1.first.compare("AbsoluteCounterPosition") ||
!entry1.first.compare("InstanceID")) {
m_reporter->changed(entry1.first.c_str(),
atoi(entry1.second.c_str()));
} else if (!entry1.first.compare("CurrentMediaDuration") ||
!entry1.first.compare("CurrentTrackDuration") ||
!entry1.first.compare("RelativeTimePosition") ||
!entry1.first.compare("AbsoluteTimePosition")) {
m_reporter->changed(entry1.first.c_str(),
upnpdurationtos(entry1.second));
} else if (!entry1.first.compare("AVTransportURIMetaData") ||
!entry1.first.compare("NextAVTransportURIMetaData") ||
!entry1.first.compare("CurrentTrackMetaData")) {
UPnPDirContent meta;
if (!meta.parse(entry1.second)) {
LOGERR("AVTransport event: bad metadata: [" <<
entry1.second << "]" << endl);
} else {
LOGDEB1("AVTransport event: good metadata: [" <<
entry1.second << "]" << endl);
if (meta.m_items.size() > 0) {
m_reporter->changed(entry1.first.c_str(),
meta.m_items[0]);
}
}
} else if (!entry1.first.compare("PlaybackStorageMedium") ||
!entry1.first.compare("PossiblePlaybackStorageMedium") ||
!entry1.first.compare("RecordStorageMedium") ||
!entry1.first.compare("PossibleRecordStorageMedium") ||
!entry1.first.compare("RecordMediumWriteStatus") ||
!entry1.first.compare("CurrentRecordQualityMode") ||
!entry1.first.compare("PossibleRecordQualityModes")){
m_reporter->changed(entry1.first.c_str(),entry1.second.c_str());
} else {
LOGERR("AVTransport event: unknown variable: name [" <<
entry1.first << "] value [" << entry1.second << endl);
m_reporter->changed(entry1.first.c_str(),entry1.second.c_str());
}
}
}
}
int AVTransport::setURI(const string& uri, const string& metadata,
int instanceID, bool next)
{
SoapEncodeInput args(m_serviceType, next ? "SetNextAVTransportURI" :
"SetAVTransportURI");
args("InstanceID", SoapHelp::i2s(instanceID))
(next ? "NextURI" : "CurrentURI", uri)
(next ? "NextURIMetaData" : "CurrentURIMetaData", metadata);
SoapDecodeOutput data;
return runAction(args, data);
}
int AVTransport::setPlayMode(PlayMode pm, int instanceID)
{
SoapEncodeInput args(m_serviceType, "SetPlayMode");
string playmode;
switch (pm) {
case PM_Normal: playmode = "NORMAL"; break;
case PM_Shuffle: playmode = "SHUFFLE"; break;
case PM_RepeatOne: playmode = "REPEAT_ONE"; break;
case PM_RepeatAll: playmode = "REPEAT_ALL"; break;
case PM_Random: playmode = "RANDOM"; break;
case PM_Direct1: playmode = "DIRECT_1"; break;
default: playmode = "NORMAL"; break;
}
args("InstanceID", SoapHelp::i2s(instanceID))
("NewPlayMode", playmode);
SoapDecodeOutput data;
return runAction(args, data);
}
int AVTransport::getMediaInfo(MediaInfo& info, int instanceID)
{
SoapEncodeInput args(m_serviceType, "GetMediaInfo");
args("InstanceID", SoapHelp::i2s(instanceID));
SoapDecodeOutput data;
int ret = runAction(args, data);
if (ret != UPNP_E_SUCCESS) {
return ret;
}
string s;
data.getInt("NrTracks", &info.nrtracks);
data.getString("MediaDuration", &s);
info.mduration = upnpdurationtos(s);
data.getString("CurrentURI", &info.cururi);
data.getString("CurrentURIMetaData", &s);
UPnPDirContent meta;
meta.parse(s);
if (meta.m_items.size() > 0)
info.curmeta = meta.m_items[0];
meta.clear();
data.getString("NextURI", &info.nexturi);
data.getString("NextURIMetaData", &s);
if (meta.m_items.size() > 0)
info.nextmeta = meta.m_items[0];
data.getString("PlayMedium", &info.pbstoragemed);
data.getString("RecordMedium", &info.pbstoragemed);
data.getString("WriteStatus", &info.ws);
return 0;
}
int AVTransport::getTransportInfo(TransportInfo& info, int instanceID)
{
SoapEncodeInput args(m_serviceType, "GetTransportInfo");
args("InstanceID", SoapHelp::i2s(instanceID));
SoapDecodeOutput data;
int ret = runAction(args, data);
if (ret != UPNP_E_SUCCESS) {
return ret;
}
string s;
data.getString("CurrentTransportState", &s);
info.tpstate = stringToTpState(s);
data.getString("CurrentTransportStatus", &s);
info.tpstatus = stringToTpStatus(s);
data.getInt("CurrentSpeed", &info.curspeed);
return 0;
}
int AVTransport::getPositionInfo(PositionInfo& info, int instanceID)
{
SoapEncodeInput args(m_serviceType, "GetPositionInfo");
args("InstanceID", SoapHelp::i2s(instanceID));
SoapDecodeOutput data;
int ret = runAction(args, data);
if (ret != UPNP_E_SUCCESS) {
return ret;
}
string s;
data.getInt("Track", &info.track);
data.getString("TrackDuration", &s);
info.trackduration = upnpdurationtos(s);
data.getString("TrackMetaData", &s);
UPnPDirContent meta;
meta.parse(s);
if (meta.m_items.size() > 0) {
info.trackmeta = meta.m_items[0];
LOGDEB1("AVTransport::getPositionInfo: size " <<
meta.m_items.size() << " current title: "
<< meta.m_items[0].m_title << endl);
}
data.getString("TrackURI", &info.trackuri);
data.getString("RelTime", &s);
info.reltime = upnpdurationtos(s);
data.getString("AbsTime", &s);
info.abstime = upnpdurationtos(s);
data.getInt("RelCount", &info.relcount);
data.getInt("AbsCount", &info.abscount);
return 0;
}
int AVTransport::getDeviceCapabilities(DeviceCapabilities& info, int iID)
{
SoapEncodeInput args(m_serviceType, "GetDeviceCapabilities");
args("InstanceID", SoapHelp::i2s(iID));
SoapDecodeOutput data;
int ret = runAction(args, data);
if (ret != UPNP_E_SUCCESS) {
return ret;
}
data.getString("PlayMedia", &info.playmedia);
data.getString("RecMedia", &info.recmedia);
data.getString("RecQualityModes", &info.recqualitymodes);
return 0;
}
int AVTransport::getTransportSettings(TransportSettings& info, int instanceID)
{
SoapEncodeInput args(m_serviceType, "GetTransportSettings");
args("InstanceID", SoapHelp::i2s(instanceID));
SoapDecodeOutput data;
int ret = runAction(args, data);
if (ret != UPNP_E_SUCCESS) {
return ret;
}
string s;
data.getString("PlayMedia", &s);
info.playmode = stringToPlayMode(s);
data.getString("RecQualityMode", &info.recqualitymode);
return 0;
}
int AVTransport::getCurrentTransportActions(int& iacts, int iID)
{
SoapEncodeInput args(m_serviceType, "GetCurrentTransportActions");
args("InstanceID", SoapHelp::i2s(iID));
SoapDecodeOutput data;
int ret = runAction(args, data);
if (ret != UPNP_E_SUCCESS) {
return ret;
}
string actions;
if (!data.getString("Actions", &actions)) {
LOGERR("AVTransport:getCurrentTransportActions: no actions in answer"
<< endl);
return UPNP_E_BAD_RESPONSE;
}
return CTAStringToBits(actions, iacts);
}
int AVTransport::CTAStringToBits(const string& actions, int& iacts)
{
vector<string> sacts;
if (!csvToStrings(actions, sacts)) {
LOGERR("AVTransport::CTAStringToBits: bad actions string:"
<< actions << endl);
return UPNP_E_BAD_RESPONSE;
}
iacts = 0;
for (auto it = sacts.begin(); it != sacts.end(); it++) {
trimstring(*it);
if (!it->compare("Next")) {
iacts |= TPA_Next;
} else if (!it->compare("Pause")) {
iacts |= TPA_Pause;
} else if (!it->compare("Play")) {
iacts |= TPA_Play;
} else if (!it->compare("Previous")) {
iacts |= TPA_Previous;
} else if (!it->compare("Seek")) {
iacts |= TPA_Seek;
} else if (!it->compare("Stop")) {
iacts |= TPA_Stop;
} else {
LOGERR("AVTransport::CTAStringToBits: unknown action "
<< *it << endl);
}
}
return 0;
}
int AVTransport::stop(int instanceID)
{
SoapEncodeInput args(m_serviceType, "Stop");
args("InstanceID", SoapHelp::i2s(instanceID));
SoapDecodeOutput data;
return runAction(args, data);
}
int AVTransport::pause(int instanceID)
{
SoapEncodeInput args(m_serviceType, "Pause");
args("InstanceID", SoapHelp::i2s(instanceID));
SoapDecodeOutput data;
return runAction(args, data);
}
int AVTransport::play(int speed, int instanceID)
{
SoapEncodeInput args(m_serviceType, "Play");
args("InstanceID", SoapHelp::i2s(instanceID))
("Speed", SoapHelp::i2s(speed));
SoapDecodeOutput data;
return runAction(args, data);
}
int AVTransport::seek(SeekMode mode, int target, int instanceID)
{
string sm;
string value = SoapHelp::i2s(target);
switch (mode) {
case SEEK_TRACK_NR: sm = "TRACK_NR"; break;
case SEEK_ABS_TIME: sm = "ABS_TIME";value = upnpduration(target*1000);break;
case SEEK_REL_TIME: sm = "REL_TIME";value = upnpduration(target*1000);break;
case SEEK_ABS_COUNT: sm = "ABS_COUNT"; break;
case SEEK_REL_COUNT: sm = "REL_COUNT"; break;
case SEEK_CHANNEL_FREQ: sm = "CHANNEL_FREQ"; break;
case SEEK_TAPE_INDEX: sm = "TAPE-INDEX"; break;
case SEEK_FRAME: sm = "FRAME"; break;
default:
return UPNP_E_INVALID_PARAM;
}
SoapEncodeInput args(m_serviceType, "Seek");
args("InstanceID", SoapHelp::i2s(instanceID))
("Unit", sm)
("Target", value);
SoapDecodeOutput data;
return runAction(args, data);
}
int AVTransport::next(int instanceID)
{
SoapEncodeInput args(m_serviceType, "Next");
args("InstanceID", SoapHelp::i2s(instanceID));
SoapDecodeOutput data;
return runAction(args, data);
}
int AVTransport::previous(int instanceID)
{
SoapEncodeInput args(m_serviceType, "Previous");
args("InstanceID", SoapHelp::i2s(instanceID));
SoapDecodeOutput data;
return runAction(args, data);
}
void AVTransport::registerCallback()
{
Service::registerCallback(bind(&AVTransport::evtCallback, this, _1));
}
} // End namespace UPnPClient