Download this file

upsend.cpp    324 lines (287 with data), 9.6 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
/* Copyright (C) 2013 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 <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <strings.h>
#include <string>
#include <iostream>
#include "libupnpp/upnpplib.hxx"
#include "libupnpp/log.hxx"
#include "libupnpp/soaphelp.hxx"
#include "libupnpp/upnpputils.hxx"
#include "libupnpp/control/mediarenderer.hxx"
#include "libupnpp/control/avtransport.hxx"
#include "libupnpp/control/discovery.hxx"
#include "streamer.h"
#include "wav.h"
using namespace std;
using namespace UPnPClient;
using namespace UPnPP;
WorkQueue<AudioMessage*> audioqueue("audioqueue", 4);
// @param name can be uuid or friendly name, we try both. The chance that a
// device would have a uuid which would be the friendly name of
// another is small...
MRDH getRenderer(const string& name)
{
static UPnPDeviceDirectory *superdir;
if (superdir == 0) {
superdir = UPnPDeviceDirectory::getTheDir();
if (superdir == 0) {
cerr << "Discovery init failed\n";
return MRDH();
}
}
UPnPDeviceDesc ddesc;
if (superdir->getDevByFName(name, ddesc)) {
return MRDH(new MediaRenderer(ddesc));
} else if (superdir->getDevByUDN(name, ddesc)) {
return MRDH(new MediaRenderer(ddesc));
}
cerr << "Can't connect to " << name << endl;
return MRDH();
}
static string path_suffix(const string& s)
{
string::size_type dotp = s.rfind('.');
if (dotp == string::npos) {
return string();
}
return s.substr(dotp + 1);
}
static bool whatfile(const string& audiofile, AudioSink::Context *ctxt)
{
if (access(audiofile.c_str(), R_OK) != 0) {
cerr << "No read access " << audiofile << " errno " << errno << endl;
return false;
}
struct stat st;
if (stat(audiofile.c_str(), &st)) {
cerr << "Can't stat " << audiofile << " errno " << errno << endl;
return false;
}
string ext = path_suffix(audiofile);
ctxt->filename = audiofile;
ctxt->filesize = st.st_size;
ctxt->ext = ext;
const char* cext = ext.c_str();
if (!strcasecmp("flac", cext)) {
ctxt->content_type = "audio/flac";
} else if (!strcasecmp("mp3", cext)) {
ctxt->content_type = "audio/mpeg";
} else if (!strcasecmp("wav", cext)) {
ctxt->content_type = "audio/wav";
} else {
cerr << "Unknown extension " << ext << endl;
return false;
}
return true;
}
void *readworker(void *a)
{
AudioSink::Context *ctxt = (AudioSink::Context *)a;
int fd = 0;
if (ctxt->filename.compare("stdin")) {
if ((fd = open(ctxt->filename.c_str(), O_RDONLY)) < 0) {
cerr << "readWorker: can't open " << ctxt->filename <<
" for reading, errno " << errno << endl;
exit(1);
}
}
for (;;) {
unsigned int allocbytes = 4096;
char *buf = (char *)malloc(allocbytes);
if (buf == 0) {
cerr << "readWorker: can't allocate " << allocbytes << " bytes\n";
exit(1);
}
ssize_t readbytes = read(fd, buf, allocbytes);
//cerr << "readworker: got " << readbytes << "bytes\n";
if (readbytes < 0) {
cerr << "readWorker: read error on " << ctxt->filename <<
" errno " << errno << endl;
exit(1);
} else if (readbytes == 0) {
audioqueue.waitIdle();
audioqueue.setTerminateAndWait();
return nullptr;
}
AudioMessage *ap = new AudioMessage(buf, readbytes, allocbytes);
if (!audioqueue.put(ap, false)) {
cerr << "readWorker: queue dead: exiting\n";
exit(1);
}
}
}
string didlmake(const string& uri, const string& mime)
{
ostringstream ss;
ss << "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<DIDL-Lite xmlns:dc=\"http://purl.org/dc/elements/1.1/\" "
"xmlns:upnp=\"urn:schemas-upnp-org:metadata-1-0/upnp/\" "
"xmlns=\"urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/\" "
"xmlns:dlna=\"urn:schemas-dlna-org:metadata-1-0/\">"
<< "<item restricted=\"1\">";
ss << "<dc:title>" << SoapHelp::xmlQuote("Streaming") << "</dc:title>";
ss << "<upnp:class>object.item.audioItem.musicTrack</upnp:class>";
#warning "problem with resource values!"
ss << "<res " << "duration=\"" << upnpduration(30)
<< "\" "
<< "sampleFrequency=\"44100\" audioChannels=\"2\" "
<< "protocolInfo=\"http-get:*:" << mime << ":*\""
<< ">"
<< SoapHelp::xmlQuote(uri)
<< "</res>"
<< "</item></DIDL-Lite>";
return ss.str();
}
static char *thisprog;
static char usage [] =
"<audiofile> <renderer> : play audio on given renderer\n"
;
static void Usage(void)
{
fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
exit(1);
}
static int op_flags;
#define OPT_h 0x1
#define OPT_p 0x2
static struct option long_options[] = {
{"host", required_argument, 0, 'h'},
{"port", required_argument, 0, 'p'},
{0, 0, 0, 0}
};
int main(int argc, char *argv[])
{
thisprog = argv[0];
string host = "localhost";
int port = 8869;
int option_index = 0;
int ret;
while ((ret = getopt_long(argc, argv, "h:p:",
long_options, &option_index)) != -1) {
cerr << "ret is " << ret << endl;
switch (ret) {
case 'h': op_flags |= OPT_h; host = optarg; break;
case 'p': op_flags |= OPT_h; port = atoi(optarg); break;
default: Usage();
}
}
if (optind != argc - 2)
Usage();
string audiofile = argv[optind++];
string renderer = argv[optind++];
if (Logger::getTheLog("stderr") == 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;
}
if (!(op_flags & OPT_h)) {
#if FUTURE
host = mylib->host();
if (host.empty()) {
cerr << "Can't retrieve IP address\n";
return 1;
}
#else
char hostname[1024];
if (gethostname(hostname, 1024)) {
perror("gethostname failed. use -h:");
return 1;
}
host = hostname;
#endif
}
if (!mylib->ok()) {
cerr << "Lib init failed: " <<
mylib->errAsString("main", mylib->getInitError()) << endl;
return 1;
}
//mylib->setLogFileName("/tmp/libupnp.log", LibUPnP::LogLevelDebug);
MRDH rdr = getRenderer(renderer);
if (!rdr) {
cerr << "Can't connect to renderer\n";
return 1;
}
AVTH avth = rdr->avt();
if (!avth) {
cerr << "Device has no AVTransport service" << endl;
return 1;
}
// Identify file
AudioSink::Context *ctxt = new AudioSink::Context(&audioqueue);
bool makewav = false;
if (!audiofile.compare("stdin")) {
ctxt->filename = audiofile;
ctxt->ext = "wav";
ctxt->content_type = "audio/wav";
makewav = true;
} else {
if (!whatfile(audiofile, ctxt)) {
cerr << "Can't identify file " << audiofile << endl;
return 1;
}
}
unordered_map<string,string> c{{"httpport", SoapHelp::i2s(port)},
{"httphost", host}};
ctxt->config = c;
// Start the http thread
audioqueue.start(1, (void *(*)(void *))(httpAudioSink.worker), ctxt);
if (makewav) {
unsigned int allocbytes = 512;
char *buf = (char *)malloc(allocbytes);
if (buf == 0) {
cerr << "Can't allocate " << allocbytes << " bytes\n";
exit(1);
}
int freq = 44100;
int bits = 16;
int chans = 2;
int databytes = 2 * 1000 * 1000 * 1000;
// Using buf+bytes in case we ever insert icy before the audio
int sz = makewavheader(buf, allocbytes, freq, bits, chans, databytes);
AudioMessage *ap = new AudioMessage(buf, sz, allocbytes);
audioqueue.put(ap, false);
}
// Start the reading thread
std::thread readthread(readworker, ctxt);
string uri("http://" + host + ":" + SoapHelp::i2s(port) + "/stream." +
ctxt->ext);
// We'd need a few options here to decide what to do if already playing:
// wait (would allow to queue multiple songs), or interrupt.
// Start the renderer
if (avth->setAVTransportURI(uri, didlmake(uri, ctxt->content_type)) != 0) {
cerr << "setAVTransportURI failed\n";
return 1;
}
if (avth->play() != 0) {
cerr << "play failed\n";
return 1;
}
readthread.join();
return 0;
}