Switch to unified view

a b/src/ohradio.cxx
1
/* Copyright (C) 2014 J.F.Dockes
2
 *   This program is free software; you can redistribute it and/or modify
3
 *   it under the terms of the GNU General Public License as published by
4
 *   the Free Software Foundation; either version 2 of the License, or
5
 *   (at your option) any later version.
6
 *
7
 *   This program is distributed in the hope that it will be useful,
8
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 *   GNU General Public License for more details.
11
 *
12
 *   You should have received a copy of the GNU General Public License
13
 *   along with this program; if not, write to the
14
 *   Free Software Foundation, Inc.,
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
17
18
#include "ohradio.hxx"
19
20
#include <stdlib.h>
21
22
#include <upnp/upnp.h>
23
24
#include <functional>
25
#include <iostream>
26
#include <string>
27
#include <utility>
28
#include <vector>
29
30
#include "libupnpp/base64.hxx"
31
#include "libupnpp/log.hxx"
32
#include "libupnpp/soaphelp.hxx"
33
#include "libupnpp/upnpavutils.hxx"
34
35
#include "ohmetacache.hxx"
36
#include "mpdcli.hxx"
37
#include "upmpd.hxx"
38
#include "upmpdutils.hxx"
39
#include "conftree.hxx"
40
41
using namespace std;
42
using namespace std::placeholders;
43
44
static const string sTpProduct("urn:av-openhome-org:service:Radio:1");
45
static const string sIdProduct("urn:av-openhome-org:serviceId:Radio");
46
47
struct RadioMeta {
48
    RadioMeta(const string& t, const string& u)
49
        : title(t), uri(u) {
50
    }
51
    string title;
52
    string uri;
53
};
54
55
static vector<RadioMeta> o_radios;
56
57
OHRadio::OHRadio(UpMpd *dev)
58
    : UpnpService(sTpProduct, sIdProduct, dev), m_dev(dev), m_id(0)
59
{
60
    dev->addActionMapping(this, "Channel",
61
                          bind(&OHRadio::channel, this, _1, _2));
62
    dev->addActionMapping(this, "ChannelsMax",
63
                          bind(&OHRadio::channelsMax, this, _1, _2));
64
    dev->addActionMapping(this, "Id",
65
                          bind(&OHRadio::id, this, _1, _2));
66
    dev->addActionMapping(this, "IdArray",
67
                          bind(&OHRadio::idArray, this, _1, _2));
68
    dev->addActionMapping(this, "IdArrayChanged",
69
                          bind(&OHRadio::idArrayChanged, this, _1, _2));
70
    dev->addActionMapping(this, "Pause",
71
                          bind(&OHRadio::pause, this, _1, _2));
72
    dev->addActionMapping(this, "Play",
73
                          bind(&OHRadio::play, this, _1, _2));
74
    dev->addActionMapping(this, "ProtocolInfo",
75
                          bind(&OHRadio::protocolInfo, this, _1, _2));
76
    dev->addActionMapping(this, "Read",
77
                          bind(&OHRadio::ohread, this, _1, _2));
78
    dev->addActionMapping(this, "ReadList",
79
                          bind(&OHRadio::readList, this, _1, _2));
80
    dev->addActionMapping(this, "SeekSecondAbsolute",
81
                          bind(&OHRadio::seekSecondAbsolute, this, _1, _2));
82
    dev->addActionMapping(this, "SeekSecondRelative",
83
                          bind(&OHRadio::seekSecondRelative, this, _1, _2));
84
    dev->addActionMapping(this, "SetChannel",
85
                          bind(&OHRadio::setChannel, this, _1, _2));
86
    dev->addActionMapping(this, "SetId",
87
                          bind(&OHRadio::setId, this, _1, _2));
88
    dev->addActionMapping(this, "Stop",
89
                          bind(&OHRadio::stop, this, _1, _2));
90
    dev->addActionMapping(this, "TransportState",
91
                          bind(&OHRadio::transportState, this, _1, _2));
92
    readRadios();
93
}
94
95
void OHRadio::readRadios()
96
{
97
    // Id 0 means no selection
98
    o_radios.push_back(RadioMeta("Unknown radio", ""));
99
    
100
    vector<string> allsubk = g_config->getSubKeys();
101
    for (auto it = allsubk.begin(); it != allsubk.end(); it++) {
102
        LOGDEB("OHRadio::readRadios: subk " << *it << endl);
103
        if (it->find("radio ") == 0) {
104
            string uri;
105
            string title = it->substr(6);
106
            bool ok = g_config->get("url", uri, *it);
107
            if (ok && !uri.empty()) {
108
                o_radios.push_back(RadioMeta(title, uri));
109
                LOGDEB("OHRadio::readRadios:RADIO: [" <<
110
                       title << "] uri [" << uri << "]\n");
111
            }
112
        }
113
    }
114
    LOGDEB("OHRadio::readRadios: " << o_radios.size() << " radios found\n")
115
}
116
117
static const int channelsmax = 200;
118
119
static string mpdstatusToTransportState(MpdStatus::State st)
120
{
121
    string tstate;
122
    switch (st) {
123
    case MpdStatus::MPDS_PLAY:
124
        tstate = "Playing";
125
        break;
126
    case MpdStatus::MPDS_PAUSE:
127
        tstate = "Paused";
128
        break;
129
    default:
130
        tstate = "Stopped";
131
    }
132
    return tstate;
133
}
134
135
// The data format for id lists is an array of msb 32 bits ints
136
// encoded in base64...
137
bool OHRadio::makeIdArray(string& out)
138
{
139
    LOGDEB1("OHRadio::makeIdArray\n");
140
    string out1;
141
    string sdeb;
142
    for (unsigned int val = 1; val < o_radios.size(); val++) {
143
        out1 += (unsigned char) ((val & 0xff000000) >> 24);
144
        out1 += (unsigned char) ((val & 0x00ff0000) >> 16);
145
        out1 += (unsigned char) ((val & 0x0000ff00) >> 8);
146
        out1 += (unsigned char) ((val & 0x000000ff));
147
        sdeb += SoapHelp::i2s(val) + " ";
148
    }
149
    LOGDEB("OHRadio::translateIdArray: current ids: " << sdeb << endl);
150
    out = base64_encode(out1);
151
    return true;
152
}
153
154
bool OHRadio::makestate(unordered_map<string, string>& st)
155
{
156
    st.clear();
157
158
    const MpdStatus& mpds = m_dev->getMpdStatusNoUpdate();
159
160
    st["ChannelsMax"] = SoapHelp::i2s(channelsmax);
161
    st["Id"] = SoapHelp::i2s(m_id);
162
    makeIdArray(st["IdArray"]);
163
    if (m_id && m_id < o_radios.size()) {
164
        UpSong song;
165
        song.album = o_radios[m_id].title;
166
        song.uri = o_radios[m_id].uri;
167
        st["Metadata"] =  didlmake(song);
168
    } else {
169
        st["Metadata"] =  "";
170
    }
171
    st["ProtocolInfo"] = g_protocolInfo;
172
    st["TransportState"] =  mpdstatusToTransportState(mpds.state);
173
    st["Uri"] = mpds.currentsong.uri;
174
    return true;
175
}
176
177
bool OHRadio::getEventData(bool all, std::vector<std::string>& names,
178
                           std::vector<std::string>& values)
179
{
180
    //LOGDEB("OHRadio::getEventData" << endl);
181
182
    unordered_map<string, string> state;
183
184
    makestate(state);
185
186
    unordered_map<string, string> changed;
187
    if (all) {
188
        changed = state;
189
    } else {
190
        changed = diffmaps(m_state, state);
191
    }
192
    m_state = state;
193
194
    for (auto it = changed.begin(); it != changed.end(); it++) {
195
        names.push_back(it->first);
196
        values.push_back(it->second);
197
    }
198
199
    return true;
200
}
201
202
void OHRadio::maybeWakeUp(bool ok)
203
{
204
    if (ok && m_dev) {
205
        m_dev->loopWakeup();
206
    }
207
}
208
209
int OHRadio::channel(const SoapIncoming& sc, SoapOutgoing& data)
210
{
211
    LOGDEB("OHRadio::channel" << endl);
212
    const MpdStatus& mpds = m_dev->getMpdStatusNoUpdate();
213
    string metadata = didlmake(mpds.currentsong);
214
    data.addarg("Uri", mpds.currentsong.uri);
215
    data.addarg("Metadata", metadata);
216
217
    return UPNP_E_SUCCESS;
218
}
219
220
int OHRadio::channelsMax(const SoapIncoming& sc, SoapOutgoing& data)
221
{
222
    LOGDEB("OHRadio::channelsMax" << endl);
223
    data.addarg("Value", SoapHelp::i2s(channelsmax));
224
    return UPNP_E_SUCCESS;
225
}
226
227
int OHRadio::play(const SoapIncoming& sc, SoapOutgoing& data)
228
{
229
    LOGDEB("OHRadio::play" << endl);
230
    m_dev->m_mpdcli->consume(false);
231
    m_dev->m_mpdcli->single(false);
232
    bool ok = m_dev->m_mpdcli->play();
233
    maybeWakeUp(ok);
234
    return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
235
}
236
237
int OHRadio::pause(const SoapIncoming& sc, SoapOutgoing& data)
238
{
239
    LOGDEB("OHRadio::pause" << endl);
240
    bool ok = m_dev->m_mpdcli->pause(true);
241
    maybeWakeUp(ok);
242
    return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
243
}
244
245
int OHRadio::iStop()
246
{
247
    bool ok = m_dev->m_mpdcli->stop();
248
    maybeWakeUp(ok);
249
    return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
250
}
251
int OHRadio::stop(const SoapIncoming& sc, SoapOutgoing& data)
252
{
253
    LOGDEB("OHRadio::stop" << endl);
254
    return iStop();
255
}
256
257
int OHRadio::seekSecondAbsolute(const SoapIncoming& sc, SoapOutgoing& data)
258
{
259
    LOGDEB("OHRadio::seekSecondAbsolute" << endl);
260
    int seconds;
261
    bool ok = sc.get("Value", &seconds);
262
    if (ok) {
263
        ok = m_dev->m_mpdcli->seek(seconds);
264
        maybeWakeUp(ok);
265
    }
266
    return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
267
}
268
269
int OHRadio::seekSecondRelative(const SoapIncoming& sc, SoapOutgoing& data)
270
{
271
    LOGDEB("OHRadio::seekSecondRelative" << endl);
272
    int seconds;
273
    bool ok = sc.get("Value", &seconds);
274
    if (ok) {
275
        const MpdStatus& mpds =  m_dev->getMpdStatusNoUpdate();
276
        bool is_song = (mpds.state == MpdStatus::MPDS_PLAY) ||
277
                       (mpds.state == MpdStatus::MPDS_PAUSE);
278
        if (is_song) {
279
            seconds += mpds.songelapsedms / 1000;
280
            ok = m_dev->m_mpdcli->seek(seconds);
281
        } else {
282
            ok = false;
283
        }
284
        maybeWakeUp(ok);
285
    }
286
    return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
287
}
288
289
int OHRadio::transportState(const SoapIncoming& sc, SoapOutgoing& data)
290
{
291
    LOGDEB("OHRadio::transportState" << endl);
292
    const MpdStatus& mpds = m_dev->getMpdStatusNoUpdate();
293
    string tstate;
294
    switch (mpds.state) {
295
    case MpdStatus::MPDS_PLAY:
296
        tstate = "Playing";
297
        break;
298
    case MpdStatus::MPDS_PAUSE:
299
        tstate = "Paused";
300
        break;
301
    default:
302
        tstate = "Stopped";
303
    }
304
    data.addarg("Value", tstate);
305
    return UPNP_E_SUCCESS;
306
}
307
308
int OHRadio::setChannel(const SoapIncoming& sc, SoapOutgoing& data)
309
{
310
    LOGDEB("OHRadio::setId" << endl);
311
    string uri, metadata;
312
    bool ok = sc.get("Uri", &uri);
313
    if (ok) {
314
        ok = sc.get("Metadata", &metadata);
315
        m_id = 0;
316
        // Do something
317
        maybeWakeUp(ok);
318
    }
319
    return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
320
}
321
322
int OHRadio::setId(const SoapIncoming& sc, SoapOutgoing& data)
323
{
324
    LOGDEB("OHRadio::setId" << endl);
325
    int id;
326
    bool ok = sc.get("Value", &id);
327
    if (ok) {
328
        m_id = id;
329
        maybeWakeUp(ok);
330
    }
331
    return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
332
}
333
334
// Return current Id. Not the same as for playlist, this is our internal channel
335
// id, nothing to do with mpd's
336
int OHRadio::id(const SoapIncoming& sc, SoapOutgoing& data)
337
{
338
    LOGDEB("OHRadio::id" << endl);
339
    const MpdStatus& mpds = m_dev->getMpdStatusNoUpdate();
340
    data.addarg("Value", mpds.songid == -1 ? "0" : SoapHelp::i2s(mpds.songid));
341
    return UPNP_E_SUCCESS;
342
}
343
344
// Report the uri and metadata for a given channel id.
345
int OHRadio::ohread(const SoapIncoming& sc, SoapOutgoing& data)
346
{
347
    int id;
348
    bool ok = sc.get("Id", &id);
349
    LOGDEB("OHRadio::ohread id " << id << endl);
350
    if (ok) {
351
        if (id > 0 && id  < int(o_radios.size())) {
352
            data.addarg("Uri", o_radios[id].uri);
353
            UpSong song;
354
            song.album = o_radios[id].title;
355
            song.uri = o_radios[id].uri;
356
            data.addarg("Metadata", didlmake(song));
357
        } else {
358
            ok = false;
359
        }
360
    }
361
    return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
362
}
363
364
// Given a space separated list of track Id's, report their associated
365
// uri and metadata in the following xml form:
366
//
367
//  <TrackList>
368
//    <Entry>
369
//      <Id></Id>
370
//      <Uri></Uri>
371
//      <Metadata></Metadata>
372
//    </Entry>
373
//  </TrackList>
374
//
375
// Any ids not in the radio are ignored.
376
int OHRadio::readList(const SoapIncoming& sc, SoapOutgoing& data)
377
{
378
    string sids;
379
    UpSong song;
380
381
    bool ok = sc.get("IdList", &sids);
382
    LOGDEB("OHRadio::readList: [" << sids << "]" << endl);
383
384
    vector<string> ids;
385
    string out("<ChannelList>");
386
    if (ok) {
387
        stringToTokens(sids, ids);
388
        for (auto it = ids.begin(); it != ids.end(); it++) {
389
            int id = atoi(it->c_str());
390
            if (id <= 0 || id >= int(o_radios.size())) {
391
                LOGDEB("OHRadio::readlist: bad id " << id << endl);
392
                continue;
393
            }
394
            song.title = o_radios[id].title;
395
            song.uri = o_radios[id].uri;
396
            LOGDEB("OHRadio::readlist: get data for  " << id << " alb " <<
397
                   song.album << " uri " << song.uri << endl);
398
            out += "<Entry><Id>";
399
            out += *it;
400
            out += "</Id><Metadata>";
401
            out += SoapHelp::xmlQuote(didlmake(song));
402
            out += "</Metadata></Entry>";
403
        }
404
        out += "</ChannelList>";
405
        LOGDEB("OHRadio::readList: out: [" << out << "]" << endl);
406
        data.addarg("ChannelList", out);
407
    }
408
    return ok ? UPNP_E_SUCCESS : UPNP_E_INTERNAL_ERROR;
409
}
410
411
// Returns current list of id as array of big endian 32bits integers,
412
// base-64-encoded.
413
int OHRadio::idArray(const SoapIncoming& sc, SoapOutgoing& data)
414
{
415
    LOGDEB("OHRadio::idArray" << endl);
416
    string idarray;
417
    if (makeIdArray(idarray)) {
418
        data.addarg("Token", SoapHelp::i2s(1));
419
        data.addarg("Array", idarray);
420
        return UPNP_E_SUCCESS;
421
    }
422
    return UPNP_E_INTERNAL_ERROR;
423
}
424
425
// Check if id array changed since last call (which returned a gen token)
426
int OHRadio::idArrayChanged(const SoapIncoming& sc, SoapOutgoing& data)
427
{
428
    LOGDEB("OHRadio::idArrayChanged" << endl);
429
    data.addarg("Value", SoapHelp::i2s(0));
430
431
    return UPNP_E_SUCCESS;
432
}
433
434
int OHRadio::protocolInfo(const SoapIncoming& sc, SoapOutgoing& data)
435
{
436
    LOGDEB("OHRadio::protocolInfo" << endl);
437
    data.addarg("Value", g_protocolInfo);
438
    return UPNP_E_SUCCESS;
439
}