Child: [372bcc] (diff)

Download this file

ohsndrcv.cxx    168 lines (146 with data), 4.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
/* Copyright (C) 2015 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 "ohsndrcv.hxx"
#include "libupnpp/log.hxx"
#include "libupnpp/base64.hxx"
#include "execmd.h"
#include "upmpd.hxx"
#include "mpdcli.hxx"
#include "upmpdutils.hxx"
#include "ohreceiver.hxx"
#include "ohplaylist.hxx"
using namespace std;
using namespace std::placeholders;
using namespace UPnPP;
static const string makesendercmd("scmakesender.py");
static int mpdport = 6700;
class SenderReceiver::Internal {
public:
Internal(UpMpd *dv)
: dev(dv), mpd(0), origmpd(0), cmd(0) {
}
~Internal() {
clear();
}
void clear() {
if (dev && origmpd) {
dev->m_mpdcli = origmpd;
origmpd = 0;
if (dev->m_ohpl) {
dev->m_ohpl->resetQVers();
}
if (dev->m_ohrcv) {
dev->m_ohrcv->iStop();
}
}
delete mpd;
delete cmd;
}
UpMpd *dev;
MPDCli *mpd;
MPDCli *origmpd;
ExecCmd *cmd;
};
SenderReceiver::SenderReceiver(UpMpd *dev)
{
m = new Internal(dev);
}
SenderReceiver::~SenderReceiver()
{
if (m)
delete m;
}
bool SenderReceiver::start(int seekms)
{
LOGDEB("SenderReceiver::start. seekms " << seekms << endl);
if (!m->dev || !m->dev->m_ohpl) {
LOGERR("SenderReceiver::start: no ohpl\n");
return false;
}
// Playing state. Playing is stopped at this point (source switch), so
// we get the elapsedms (would be 0) as input param.
MpdStatus mpds = m->dev->getMpdStatusNoUpdate();
// Retrieve the current playlist from the normal MPD
vector<UpSong> playlist;
if (!m->dev->m_mpdcli || !m->dev->m_mpdcli->getQueueData(playlist)) {
LOGERR("SenderReceiver::start: can't retrieve current playlist\n");
return false;
}
// Stop MPD Play (normally already done)
m->dev->m_mpdcli->stop();
// Start fifo MPD and Sender
m->cmd = new ExecCmd();
vector<string> args;
args.push_back("-p");
args.push_back(SoapHelp::i2s(mpdport));
m->cmd->startExec(makesendercmd, args, false, true);
string output;
if (!m->cmd->getline(output)) {
LOGERR("SenderReceiver::start: makesender command failed\n");
m->clear();
return false;
}
LOGDEB("SenderReceiver::start got [" << output << "] from script\n");
// Output is like [Ok mpdport URI base64-encoded-uri METADATA b64-meta]
vector<string> toks;
stringToTokens(output, toks);
if (toks.size() != 6 || toks[0].compare("Ok")) {
LOGERR("SenderReceiver::start: bad output from script: " << output
<< endl);
m->clear();
return false;
}
string uri = base64_decode(toks[3]);
string meta = base64_decode(toks[5]);
// Connect to the new MPD, and copy the playlist
m->mpd = new MPDCli("localhost", mpdport);
if (!m->mpd || !m->mpd->ok()) {
LOGERR("SenderReceiver::start: can't connect to new MPD\n");
m->clear();
return false;
}
for (unsigned int i = 0; i < playlist.size(); i++) {
if (m->mpd->insert(playlist[i].uri, i, playlist[i]) < 0) {
LOGERR("SenderReceiver::start: mpd->insert failed\n");
m->clear();
return false;
}
}
// Start our receiver
if (!m->dev->m_ohrcv->iSetSender(uri, meta) ||
!m->dev->m_ohrcv->iPlay()) {
m->clear();
return false;
}
// Replace the original mpd and play
m->origmpd = m->dev->m_mpdcli;
m->dev->m_mpdcli = m->mpd;
LOGDEB("SenderReceiver::starting new mpd. songelapsedms: " <<
mpds.songelapsedms << endl);
m->mpd->setVolume(mpds.volume);
m->mpd->play(mpds.songpos);
m->mpd->seek(seekms/1000);
m->mpd->setVolume(mpds.volume);
return true;
}
bool SenderReceiver::stop()
{
LOGDEB("SenderReceiver::stop()\n");
// Do we want to transfer the playlist back ? Probably we do.
m->clear();
return true;
}