2011-02-08 00:17:58 +01:00
|
|
|
/*
|
2013-01-16 23:29:56 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2011-02-08 00:17:58 +01:00
|
|
|
* 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"
|
2013-01-16 23:29:56 +01:00
|
|
|
#include "RoarOutputPlugin.hxx"
|
2013-07-30 08:34:10 +02:00
|
|
|
#include "OutputAPI.hxx"
|
2013-02-22 20:51:23 +01:00
|
|
|
#include "MixerList.hxx"
|
2013-01-16 23:29:56 +01:00
|
|
|
#include "thread/Mutex.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
2011-02-08 00:17:58 +01:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2013-04-16 19:54:58 +02:00
|
|
|
/* libroar/services.h declares roar_service_stream::new - work around
|
|
|
|
this C++ problem */
|
|
|
|
#define new _new
|
2011-09-17 19:33:51 +02:00
|
|
|
#include <roaraudio.h>
|
2013-04-16 19:54:58 +02:00
|
|
|
#undef new
|
2011-02-08 00:17:58 +01:00
|
|
|
|
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "roaraudio"
|
|
|
|
|
2013-01-16 23:29:56 +01:00
|
|
|
struct RoarOutput {
|
2011-09-16 23:31:48 +02:00
|
|
|
struct audio_output base;
|
|
|
|
|
2011-09-17 19:33:51 +02:00
|
|
|
roar_vs_t * vss;
|
|
|
|
int err;
|
|
|
|
char *host;
|
|
|
|
char *name;
|
|
|
|
int role;
|
|
|
|
struct roar_connection con;
|
|
|
|
struct roar_audio_info info;
|
2013-01-16 23:29:56 +01:00
|
|
|
Mutex mutex;
|
2011-09-17 19:33:51 +02:00
|
|
|
volatile bool alive;
|
2013-01-16 23:29:56 +01:00
|
|
|
|
|
|
|
RoarOutput()
|
|
|
|
:err(ROAR_ERROR_NONE),
|
|
|
|
host(nullptr), name(nullptr) {}
|
|
|
|
|
|
|
|
~RoarOutput() {
|
|
|
|
g_free(host);
|
|
|
|
g_free(name);
|
|
|
|
}
|
|
|
|
};
|
2011-09-17 19:33:51 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain roar_output_domain("roar_output");
|
2011-02-08 00:17:58 +01:00
|
|
|
|
2011-09-18 11:51:18 +02:00
|
|
|
static int
|
2013-01-16 23:29:56 +01:00
|
|
|
roar_output_get_volume_locked(RoarOutput *roar)
|
2011-09-18 11:51:18 +02:00
|
|
|
{
|
2013-01-16 23:29:56 +01:00
|
|
|
if (roar->vss == nullptr || !roar->alive)
|
2011-09-18 12:06:46 +02:00
|
|
|
return -1;
|
2011-09-18 11:51:18 +02:00
|
|
|
|
|
|
|
float l, r;
|
|
|
|
int error;
|
2011-09-18 11:54:14 +02:00
|
|
|
if (roar_vs_volume_get(roar->vss, &l, &r, &error) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2011-09-18 11:51:18 +02:00
|
|
|
return (l + r) * 50;
|
|
|
|
}
|
|
|
|
|
2011-09-17 19:33:51 +02:00
|
|
|
int
|
2013-01-16 23:29:56 +01:00
|
|
|
roar_output_get_volume(RoarOutput *roar)
|
2011-09-17 19:33:51 +02:00
|
|
|
{
|
2013-01-16 23:29:56 +01:00
|
|
|
const ScopeLock protect(roar->mutex);
|
|
|
|
return roar_output_get_volume_locked(roar);
|
2011-09-18 11:51:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-01-16 23:29:56 +01:00
|
|
|
roar_output_set_volume_locked(RoarOutput *roar, unsigned volume)
|
2011-09-18 11:51:18 +02:00
|
|
|
{
|
|
|
|
assert(volume <= 100);
|
|
|
|
|
2013-01-16 23:29:56 +01:00
|
|
|
if (roar->vss == nullptr || !roar->alive)
|
2011-09-18 11:51:18 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
int error;
|
|
|
|
float level = volume / 100.0;
|
|
|
|
|
|
|
|
roar_vs_volume_mono(roar->vss, level, &error);
|
|
|
|
return true;
|
2011-09-17 19:33:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-01-16 23:29:56 +01:00
|
|
|
roar_output_set_volume(RoarOutput *roar, unsigned volume)
|
2011-09-17 19:33:51 +02:00
|
|
|
{
|
2013-01-16 23:29:56 +01:00
|
|
|
const ScopeLock protect(roar->mutex);
|
|
|
|
return roar_output_set_volume_locked(roar, volume);
|
2011-09-17 19:33:51 +02:00
|
|
|
}
|
|
|
|
|
2011-02-08 00:17:58 +01:00
|
|
|
static void
|
2013-08-04 12:25:08 +02:00
|
|
|
roar_configure(RoarOutput *self, const config_param ¶m)
|
2011-02-08 00:17:58 +01:00
|
|
|
{
|
2013-08-04 12:25:08 +02:00
|
|
|
self->host = param.DupBlockString("server", nullptr);
|
|
|
|
self->name = param.DupBlockString("name", "MPD");
|
2011-09-17 20:11:51 +02:00
|
|
|
|
2013-08-04 12:25:08 +02:00
|
|
|
const char *role = param.GetBlockValue("role", "music");
|
2013-01-16 23:29:56 +01:00
|
|
|
self->role = role != nullptr
|
2011-09-17 20:11:51 +02:00
|
|
|
? roar_str2role(role)
|
|
|
|
: ROAR_ROLE_MUSIC;
|
2011-02-08 00:17:58 +01:00
|
|
|
}
|
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
static struct audio_output *
|
2013-08-10 18:02:44 +02:00
|
|
|
roar_init(const config_param ¶m, Error &error)
|
2011-02-08 00:17:58 +01:00
|
|
|
{
|
2013-01-16 23:29:56 +01:00
|
|
|
RoarOutput *self = new RoarOutput();
|
2011-09-16 23:31:48 +02:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!ao_base_init(&self->base, &roar_output_plugin, param, error)) {
|
2013-01-16 23:29:56 +01:00
|
|
|
delete self;
|
|
|
|
return nullptr;
|
2011-09-16 23:31:48 +02:00
|
|
|
}
|
|
|
|
|
2011-02-08 00:17:58 +01:00
|
|
|
roar_configure(self, param);
|
2011-09-16 23:31:48 +02:00
|
|
|
return &self->base;
|
2011-02-08 00:17:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-09-16 23:31:48 +02:00
|
|
|
roar_finish(struct audio_output *ao)
|
2011-02-08 00:17:58 +01:00
|
|
|
{
|
2013-01-16 23:29:56 +01:00
|
|
|
RoarOutput *self = (RoarOutput *)ao;
|
2011-02-08 00:17:58 +01:00
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
ao_base_finish(&self->base);
|
2013-01-16 23:29:56 +01:00
|
|
|
delete self;
|
2011-02-08 00:17:58 +01:00
|
|
|
}
|
|
|
|
|
2011-10-08 14:46:17 +02:00
|
|
|
static void
|
|
|
|
roar_use_audio_format(struct roar_audio_info *info,
|
2013-08-03 21:00:50 +02:00
|
|
|
AudioFormat &audio_format)
|
2011-10-08 14:46:17 +02:00
|
|
|
{
|
2013-08-03 21:00:50 +02:00
|
|
|
info->rate = audio_format.sample_rate;
|
|
|
|
info->channels = audio_format.channels;
|
2011-10-08 14:46:17 +02:00
|
|
|
info->codec = ROAR_CODEC_PCM_S;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
switch (audio_format.format) {
|
|
|
|
case SampleFormat::UNDEFINED:
|
|
|
|
case SampleFormat::FLOAT:
|
|
|
|
case SampleFormat::DSD:
|
2011-10-08 14:46:17 +02:00
|
|
|
info->bits = 16;
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.format = SampleFormat::S16;
|
2011-10-08 14:46:17 +02:00
|
|
|
break;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S8:
|
2011-10-08 14:46:17 +02:00
|
|
|
info->bits = 8;
|
|
|
|
break;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S16:
|
2011-10-08 14:46:17 +02:00
|
|
|
info->bits = 16;
|
|
|
|
break;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S24_P32:
|
2011-10-08 14:46:17 +02:00
|
|
|
info->bits = 32;
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.format = SampleFormat::S32;
|
2011-10-08 14:46:17 +02:00
|
|
|
break;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S32:
|
2011-10-08 14:46:17 +02:00
|
|
|
info->bits = 32;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-08 00:17:58 +01:00
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
roar_open(struct audio_output *ao, AudioFormat &audio_format, Error &error)
|
2011-02-08 00:17:58 +01:00
|
|
|
{
|
2013-01-16 23:29:56 +01:00
|
|
|
RoarOutput *self = (RoarOutput *)ao;
|
|
|
|
const ScopeLock protect(self->mutex);
|
2011-02-08 00:17:58 +01:00
|
|
|
|
|
|
|
if (roar_simple_connect(&(self->con), self->host, self->name) < 0)
|
|
|
|
{
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(roar_output_domain,
|
|
|
|
"Failed to connect to Roar server");
|
2011-02-08 00:17:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->vss = roar_vs_new_from_con(&(self->con), &(self->err));
|
|
|
|
|
2013-01-16 23:29:56 +01:00
|
|
|
if (self->vss == nullptr || self->err != ROAR_ERROR_NONE)
|
2011-02-08 00:17:58 +01:00
|
|
|
{
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(roar_output_domain, "Failed to connect to server");
|
2011-02-08 00:17:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-08 14:46:17 +02:00
|
|
|
roar_use_audio_format(&self->info, audio_format);
|
2011-02-08 00:17:58 +01:00
|
|
|
|
|
|
|
if (roar_vs_stream(self->vss, &(self->info), ROAR_DIR_PLAY,
|
|
|
|
&(self->err)) < 0)
|
|
|
|
{
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(roar_output_domain, "Failed to start stream");
|
2011-02-08 00:17:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
roar_vs_role(self->vss, self->role, &(self->err));
|
|
|
|
self->alive = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-17 20:10:26 +02:00
|
|
|
static void
|
2011-09-16 23:31:48 +02:00
|
|
|
roar_close(struct audio_output *ao)
|
2011-09-17 20:10:26 +02:00
|
|
|
{
|
2013-01-16 23:29:56 +01:00
|
|
|
RoarOutput *self = (RoarOutput *)ao;
|
|
|
|
const ScopeLock protect(self->mutex);
|
|
|
|
|
2011-09-17 20:10:26 +02:00
|
|
|
self->alive = false;
|
|
|
|
|
2013-01-16 23:29:56 +01:00
|
|
|
if (self->vss != nullptr)
|
2011-09-17 20:10:26 +02:00
|
|
|
roar_vs_close(self->vss, ROAR_VS_TRUE, &(self->err));
|
2013-01-16 23:29:56 +01:00
|
|
|
self->vss = nullptr;
|
2011-09-17 20:10:26 +02:00
|
|
|
roar_disconnect(&(self->con));
|
|
|
|
}
|
|
|
|
|
2011-09-18 11:51:18 +02:00
|
|
|
static void
|
2013-01-16 23:29:56 +01:00
|
|
|
roar_cancel_locked(RoarOutput *self)
|
2011-09-18 11:51:18 +02:00
|
|
|
{
|
2013-01-16 23:29:56 +01:00
|
|
|
if (self->vss == nullptr)
|
2011-09-18 11:51:18 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
roar_vs_t *vss = self->vss;
|
2013-01-16 23:29:56 +01:00
|
|
|
self->vss = nullptr;
|
2011-09-18 11:51:18 +02:00
|
|
|
roar_vs_close(vss, ROAR_VS_TRUE, &(self->err));
|
|
|
|
self->alive = false;
|
|
|
|
|
|
|
|
vss = roar_vs_new_from_con(&(self->con), &(self->err));
|
2013-01-16 23:29:56 +01:00
|
|
|
if (vss == nullptr)
|
2011-09-18 11:51:18 +02:00
|
|
|
return;
|
|
|
|
|
2011-09-18 11:54:14 +02:00
|
|
|
if (roar_vs_stream(vss, &(self->info), ROAR_DIR_PLAY,
|
|
|
|
&(self->err)) < 0) {
|
|
|
|
roar_vs_close(vss, ROAR_VS_TRUE, &(self->err));
|
|
|
|
g_warning("Failed to start stream");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-18 11:51:18 +02:00
|
|
|
roar_vs_role(vss, self->role, &(self->err));
|
|
|
|
self->vss = vss;
|
|
|
|
self->alive = true;
|
|
|
|
}
|
|
|
|
|
2011-02-08 00:17:58 +01:00
|
|
|
static void
|
2011-09-16 23:31:48 +02:00
|
|
|
roar_cancel(struct audio_output *ao)
|
2011-02-08 00:17:58 +01:00
|
|
|
{
|
2013-01-16 23:29:56 +01:00
|
|
|
RoarOutput *self = (RoarOutput *)ao;
|
2011-02-08 00:17:58 +01:00
|
|
|
|
2013-01-16 23:29:56 +01:00
|
|
|
const ScopeLock protect(self->mutex);
|
2011-09-18 11:51:18 +02:00
|
|
|
roar_cancel_locked(self);
|
2011-02-08 00:17:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2013-08-10 18:02:44 +02:00
|
|
|
roar_play(struct audio_output *ao, const void *chunk, size_t size,
|
|
|
|
Error &error)
|
2011-02-08 00:17:58 +01:00
|
|
|
{
|
2013-01-16 23:29:56 +01:00
|
|
|
RoarOutput *self = (RoarOutput *)ao;
|
2011-02-08 00:17:58 +01:00
|
|
|
ssize_t rc;
|
|
|
|
|
2013-01-16 23:29:56 +01:00
|
|
|
if (self->vss == nullptr)
|
2011-02-08 00:17:58 +01:00
|
|
|
{
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(roar_output_domain, "Connection is invalid");
|
2011-02-08 00:17:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = roar_vs_write(self->vss, chunk, size, &(self->err));
|
|
|
|
if ( rc <= 0 )
|
|
|
|
{
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(roar_output_domain, "Failed to play data");
|
2011-02-08 00:17:58 +01:00
|
|
|
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:
|
2013-01-16 23:29:56 +01:00
|
|
|
return nullptr;
|
2011-02-08 00:17:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-07-30 20:11:57 +02:00
|
|
|
roar_send_tag(struct audio_output *ao, const Tag *meta)
|
2011-02-08 00:17:58 +01:00
|
|
|
{
|
2013-01-16 23:29:56 +01:00
|
|
|
RoarOutput *self = (RoarOutput *)ao;
|
2011-02-08 00:17:58 +01:00
|
|
|
|
2013-01-16 23:29:56 +01:00
|
|
|
if (self->vss == nullptr)
|
2011-02-08 00:17:58 +01:00
|
|
|
return;
|
|
|
|
|
2013-01-16 23:29:56 +01:00
|
|
|
const ScopeLock protect(self->mutex);
|
|
|
|
|
2011-02-08 00:17:58 +01:00
|
|
|
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);
|
2013-01-16 23:29:56 +01:00
|
|
|
if (key != nullptr)
|
2011-02-08 00:17:58 +01:00
|
|
|
{
|
|
|
|
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 = {
|
2013-01-16 23:29:56 +01:00
|
|
|
"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,
|
2011-02-08 00:17:58 +01:00
|
|
|
};
|