aboutsummaryrefslogtreecommitdiffstats
path: root/src/output/RoarOutputPlugin.cxx
blob: 9d66bb63b0c248c27f1c30bcf89d04331415140a (plain) (blame)
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
/*
 * Copyright (C) 2003-2013 The Music Player Daemon Project
 * Copyright (C) 2010-2011 Philipp 'ph3-der-loewe' Schafft
 * Copyright (C) 2010-2011 Hans-Kristian 'maister' Arntzen
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
#include "RoarOutputPlugin.hxx"
#include "OutputAPI.hxx"
#include "MixerList.hxx"
#include "thread/Mutex.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "Log.hxx"

#include <glib.h>

/* libroar/services.h declares roar_service_stream::new - work around
   this C++ problem */
#define new _new
#include <roaraudio.h>
#undef new

struct RoarOutput {
	struct audio_output base;

	roar_vs_t * vss;
	int err;
	char *host;
	char *name;
	int role;
	struct roar_connection con;
	struct roar_audio_info info;
	Mutex mutex;
	volatile bool alive;

	RoarOutput()
		:err(ROAR_ERROR_NONE),
		 host(nullptr), name(nullptr) {}

	~RoarOutput() {
		g_free(host);
		g_free(name);
	}
};

static constexpr Domain roar_output_domain("roar_output");

static int
roar_output_get_volume_locked(RoarOutput *roar)
{
	if (roar->vss == nullptr || !roar->alive)
		return -1;

	float l, r;
	int error;
	if (roar_vs_volume_get(roar->vss, &l, &r, &error) < 0)
		return -1;

	return (l + r) * 50;
}

int
roar_output_get_volume(RoarOutput *roar)
{
	const ScopeLock protect(roar->mutex);
	return roar_output_get_volume_locked(roar);
}

static bool
roar_output_set_volume_locked(RoarOutput *roar, unsigned volume)
{
	assert(volume <= 100);

	if (roar->vss == nullptr || !roar->alive)
		return false;

	int error;
	float level = volume / 100.0;

	roar_vs_volume_mono(roar->vss, level, &error);
	return true;
}

bool
roar_output_set_volume(RoarOutput *roar, unsigned volume)
{
	const ScopeLock protect(roar->mutex);
	return roar_output_set_volume_locked(roar, volume);
}

static void
roar_configure(RoarOutput *self, const config_param &param)
{
	self->host = param.DupBlockString("server", nullptr);
	self->name = param.DupBlockString("name", "MPD");

	const char *role = param.GetBlockValue("role", "music");
	self->role = role != nullptr
		? roar_str2role(role)
		: ROAR_ROLE_MUSIC;
}

static struct audio_output *
roar_init(const config_param &param, Error &error)
{
	RoarOutput *self = new RoarOutput();

	if (!ao_base_init(&self->base, &roar_output_plugin, param, error)) {
		delete self;
		return nullptr;
	}

	roar_configure(self, param);
	return &self->base;
}

static void
roar_finish(struct audio_output *ao)
{
	RoarOutput *self = (RoarOutput *)ao;

	ao_base_finish(&self->base);
	delete self;
}

static void
roar_use_audio_format(struct roar_audio_info *info,
		      AudioFormat &audio_format)
{
	info->rate = audio_format.sample_rate;
	info->channels = audio_format.channels;
	info->codec = ROAR_CODEC_PCM_S;

	switch (audio_format.format) {
	case SampleFormat::UNDEFINED:
	case SampleFormat::FLOAT:
	case SampleFormat::DSD:
		info->bits = 16;
		audio_format.format = SampleFormat::S16;
		break;

	case SampleFormat::S8:
		info->bits = 8;
		break;

	case SampleFormat::S16:
		info->bits = 16;
		break;

	case SampleFormat::S24_P32:
		info->bits = 32;
		audio_format.format = SampleFormat::S32;
		break;

	case SampleFormat::S32:
		info->bits = 32;
		break;
	}
}

static bool
roar_open(struct audio_output *ao, AudioFormat &audio_format, Error &error)
{
	RoarOutput *self = (RoarOutput *)ao;
	const ScopeLock protect(self->mutex);

	if (roar_simple_connect(&(self->con), self->host, self->name) < 0)
	{
		error.Set(roar_output_domain,
			  "Failed to connect to Roar server");
		return false;
	}

	self->vss = roar_vs_new_from_con(&(self->con), &(self->err));

	if (self->vss == nullptr || self->err != ROAR_ERROR_NONE)
	{
		error.Set(roar_output_domain, "Failed to connect to server");
		return false;
	}

	roar_use_audio_format(&self->info, audio_format);

	if (roar_vs_stream(self->vss, &(self->info), ROAR_DIR_PLAY,
				&(self->err)) < 0)
	{
		error.Set(roar_output_domain, "Failed to start stream");
		return false;
	}
	roar_vs_role(self->vss, self->role, &(self->err));
	self->alive = true;

	return true;
}

static void
roar_close(struct audio_output *ao)
{
	RoarOutput *self = (RoarOutput *)ao;
	const ScopeLock protect(self->mutex);

	self->alive = false;

	if (self->vss != nullptr)
		roar_vs_close(self->vss, ROAR_VS_TRUE, &(self->err));
	self->vss = nullptr;
	roar_disconnect(&(self->con));
}

static void
roar_cancel_locked(RoarOutput *self)
{
	if (self->vss == nullptr)
		return;

	roar_vs_t *vss = self->vss;
	self->vss = nullptr;
	roar_vs_close(vss, ROAR_VS_TRUE, &(self->err));
	self->alive = false;

	vss = roar_vs_new_from_con(&(self->con), &(self->err));
	if (vss == nullptr)
		return;

	if (roar_vs_stream(vss, &(self->info), ROAR_DIR_PLAY,
			   &(self->err)) < 0) {
		roar_vs_close(vss, ROAR_VS_TRUE, &(self->err));
		LogError(roar_output_domain, "Failed to start stream");
		return;
	}

	roar_vs_role(vss, self->role, &(self->err));
	self->vss = vss;
	self->alive = true;
}

static void
roar_cancel(struct audio_output *ao)
{
	RoarOutput *self = (RoarOutput *)ao;

	const ScopeLock protect(self->mutex);
	roar_cancel_locked(self);
}

static size_t
roar_play(struct audio_output *ao, const void *chunk, size_t size,
	  Error &error)
{
	RoarOutput *self = (RoarOutput *)ao;
	ssize_t rc;

	if (self->vss == nullptr)
	{
		error.Set(roar_output_domain, "Connection is invalid");
		return 0;
	}

	rc = roar_vs_write(self->vss, chunk, size, &(self->err));
	if ( rc <= 0 )
	{
		error.Set(roar_output_domain, "Failed to play data");
		return 0;
	}

	return rc;
}

static const char*
roar_tag_convert(enum tag_type type, bool *is_uuid)
{
	*is_uuid = false;
	switch (type)
	{
		case TAG_ARTIST:
		case TAG_ALBUM_ARTIST:
			return "AUTHOR";
		case TAG_ALBUM:
			return "ALBUM";
		case TAG_TITLE:
			return "TITLE";
		case TAG_TRACK:
			return "TRACK";
		case TAG_NAME:
			return "NAME";
		case TAG_GENRE:
			return "GENRE";
		case TAG_DATE:
			return "DATE";
		case TAG_PERFORMER:
			return "PERFORMER";
		case TAG_COMMENT:
			return "COMMENT";
		case TAG_DISC:
			return "DISCID";
		case TAG_COMPOSER:
#ifdef ROAR_META_TYPE_COMPOSER
			return "COMPOSER";
#else
			return "AUTHOR";
#endif
		case TAG_MUSICBRAINZ_ARTISTID:
		case TAG_MUSICBRAINZ_ALBUMID:
		case TAG_MUSICBRAINZ_ALBUMARTISTID:
		case TAG_MUSICBRAINZ_TRACKID:
			*is_uuid = true;
			return "HASH";

		default:
			return nullptr;
	}
}

static void
roar_send_tag(struct audio_output *ao, const Tag *meta)
{
	RoarOutput *self = (RoarOutput *)ao;

	if (self->vss == nullptr)
		return;

	const ScopeLock protect(self->mutex);

	size_t cnt = 1;
	struct roar_keyval vals[32];
	memset(vals, 0, sizeof(vals));
	char uuid_buf[32][64];

	char timebuf[16];
	snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d",
			meta->time / 3600, (meta->time % 3600) / 60, meta->time % 60);

	vals[0].key = g_strdup("LENGTH");
	vals[0].value = timebuf;

	for (unsigned i = 0; i < meta->num_items && cnt < 32; i++)
	{
		bool is_uuid = false;
		const char *key = roar_tag_convert(meta->items[i]->type, &is_uuid);
		if (key != nullptr)
		{
			if (is_uuid)
			{
				snprintf(uuid_buf[cnt], sizeof(uuid_buf[0]), "{UUID}%s",
						meta->items[i]->value);
				vals[cnt].key = g_strdup(key);
				vals[cnt].value = uuid_buf[cnt];
			}
			else
			{
				vals[cnt].key = g_strdup(key);
				vals[cnt].value = meta->items[i]->value;
			}
			cnt++;
		}
	}

	roar_vs_meta(self->vss, vals, cnt, &(self->err));

	for (unsigned i = 0; i < 32; i++)
		g_free(vals[i].key);
}

const struct audio_output_plugin roar_output_plugin = {
	"roar",
	nullptr,
	roar_init,
	roar_finish,
	nullptr,
	nullptr,
	roar_open,
	roar_close,
	nullptr,
	roar_send_tag,
	roar_play,
	nullptr,
	roar_cancel,
	nullptr,
	&roar_mixer_plugin,
};