2011-02-08 00:17:58 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 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"
|
2014-01-23 23:49:50 +01:00
|
|
|
#include "../OutputAPI.hxx"
|
2014-12-29 23:45:14 +01:00
|
|
|
#include "../Wrapper.hxx"
|
2014-01-24 16:25:21 +01:00
|
|
|
#include "mixer/MixerList.hxx"
|
2013-01-16 23:29:56 +01:00
|
|
|
#include "thread/Mutex.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Domain.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2011-02-08 00:17:58 +01:00
|
|
|
|
2013-10-15 22:55:17 +02:00
|
|
|
#include <string>
|
2016-09-09 12:52:51 +02:00
|
|
|
#include <stdexcept>
|
2013-10-15 22:55:17 +02:00
|
|
|
|
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
|
|
|
|
2013-10-15 22:58:38 +02:00
|
|
|
class RoarOutput {
|
2014-12-29 23:45:14 +01:00
|
|
|
friend struct AudioOutputWrapper<RoarOutput>;
|
|
|
|
|
2014-01-28 11:34:09 +01:00
|
|
|
AudioOutput base;
|
2011-09-16 23:31:48 +02:00
|
|
|
|
2016-11-09 08:29:44 +01:00
|
|
|
const std::string host, name;
|
2013-10-15 22:55:17 +02:00
|
|
|
|
2011-09-17 19:33:51 +02:00
|
|
|
roar_vs_t * vss;
|
2016-11-09 08:29:44 +01:00
|
|
|
int err = ROAR_ERROR_NONE;
|
|
|
|
const int role;
|
2011-09-17 19:33:51 +02:00
|
|
|
struct roar_connection con;
|
|
|
|
struct roar_audio_info info;
|
2013-10-15 22:58:38 +02:00
|
|
|
mutable Mutex mutex;
|
2014-10-23 23:29:56 +02:00
|
|
|
bool alive;
|
2013-01-16 23:29:56 +01:00
|
|
|
|
2013-10-15 22:58:38 +02:00
|
|
|
public:
|
2016-11-09 08:29:44 +01:00
|
|
|
RoarOutput(const ConfigBlock &block);
|
2013-10-15 22:58:38 +02:00
|
|
|
|
2014-01-28 11:34:09 +01:00
|
|
|
operator AudioOutput *() {
|
2013-10-15 22:58:38 +02:00
|
|
|
return &base;
|
|
|
|
}
|
|
|
|
|
2017-01-25 09:47:43 +01:00
|
|
|
static RoarOutput *Create(EventLoop &, const ConfigBlock &block) {
|
2017-01-25 09:56:51 +01:00
|
|
|
return new RoarOutput(block);
|
|
|
|
}
|
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
void Open(AudioFormat &audio_format);
|
2013-10-15 22:58:38 +02:00
|
|
|
void Close();
|
|
|
|
|
|
|
|
void SendTag(const Tag &tag);
|
2016-11-09 11:56:01 +01:00
|
|
|
size_t Play(const void *chunk, size_t size);
|
2013-10-15 22:58:38 +02:00
|
|
|
void Cancel();
|
|
|
|
|
|
|
|
int GetVolume() const;
|
2016-09-09 12:52:51 +02:00
|
|
|
void SetVolume(unsigned volume);
|
2013-01-16 23:29:56 +01:00
|
|
|
};
|
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
|
|
|
|
2016-11-09 08:29:44 +01:00
|
|
|
gcc_pure
|
|
|
|
static int
|
|
|
|
GetConfiguredRole(const ConfigBlock &block)
|
|
|
|
{
|
|
|
|
const char *role = block.GetBlockValue("role");
|
|
|
|
return role != nullptr
|
|
|
|
? roar_str2role(role)
|
|
|
|
: ROAR_ROLE_MUSIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
RoarOutput::RoarOutput(const ConfigBlock &block)
|
|
|
|
:base(roar_output_plugin, block),
|
|
|
|
host(block.GetBlockValue("server", "")),
|
|
|
|
name(block.GetBlockValue("name", "MPD")),
|
|
|
|
role(GetConfiguredRole(block))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-10-15 22:58:38 +02:00
|
|
|
inline int
|
|
|
|
RoarOutput::GetVolume() const
|
2011-09-18 11:51:18 +02:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2013-10-15 22:58:38 +02:00
|
|
|
|
|
|
|
if (vss == nullptr || !alive)
|
2011-09-18 12:06:46 +02:00
|
|
|
return -1;
|
2011-09-18 11:51:18 +02:00
|
|
|
|
|
|
|
float l, r;
|
|
|
|
int error;
|
2013-10-15 22:58:38 +02:00
|
|
|
if (roar_vs_volume_get(vss, &l, &r, &error) < 0)
|
2016-09-09 12:52:51 +02:00
|
|
|
throw std::runtime_error(roar_vs_strerr(error));
|
2011-09-18 11:54:14 +02:00
|
|
|
|
2011-09-18 11:51:18 +02:00
|
|
|
return (l + r) * 50;
|
|
|
|
}
|
|
|
|
|
2011-09-17 19:33:51 +02:00
|
|
|
int
|
2014-02-06 21:10:12 +01:00
|
|
|
roar_output_get_volume(RoarOutput &roar)
|
2011-09-17 19:33:51 +02:00
|
|
|
{
|
2014-02-06 21:10:12 +01:00
|
|
|
return roar.GetVolume();
|
2011-09-18 11:51:18 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 12:52:51 +02:00
|
|
|
inline void
|
2013-10-15 22:58:38 +02:00
|
|
|
RoarOutput::SetVolume(unsigned volume)
|
2011-09-18 11:51:18 +02:00
|
|
|
{
|
|
|
|
assert(volume <= 100);
|
|
|
|
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2013-10-15 22:58:38 +02:00
|
|
|
if (vss == nullptr || !alive)
|
2016-09-09 12:52:51 +02:00
|
|
|
throw std::runtime_error("closed");
|
2011-09-18 11:51:18 +02:00
|
|
|
|
|
|
|
int error;
|
|
|
|
float level = volume / 100.0;
|
|
|
|
|
2016-09-09 12:52:51 +02:00
|
|
|
if (roar_vs_volume_mono(vss, level, &error) < 0)
|
|
|
|
throw std::runtime_error(roar_vs_strerr(error));
|
2011-09-17 19:33:51 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 12:52:51 +02:00
|
|
|
void
|
2014-02-06 21:10:12 +01:00
|
|
|
roar_output_set_volume(RoarOutput &roar, unsigned volume)
|
2011-09-17 19:33:51 +02:00
|
|
|
{
|
2016-09-09 12:52:51 +02:00
|
|
|
roar.SetVolume(volume);
|
2011-09-17 19:33:51 +02: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 11:56:01 +01:00
|
|
|
inline void
|
|
|
|
RoarOutput::Open(AudioFormat &audio_format)
|
2011-02-08 00:17:58 +01:00
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2011-02-08 00:17:58 +01:00
|
|
|
|
2013-10-15 22:55:17 +02:00
|
|
|
if (roar_simple_connect(&con,
|
|
|
|
host.empty() ? nullptr : host.c_str(),
|
2016-11-09 08:29:44 +01:00
|
|
|
name.c_str()) < 0)
|
|
|
|
throw std::runtime_error("Failed to connect to Roar server");
|
2011-02-08 00:17:58 +01:00
|
|
|
|
2013-10-15 22:58:38 +02:00
|
|
|
vss = roar_vs_new_from_con(&con, &err);
|
2011-02-08 00:17:58 +01:00
|
|
|
|
2016-11-09 08:29:44 +01:00
|
|
|
if (vss == nullptr || err != ROAR_ERROR_NONE)
|
|
|
|
throw std::runtime_error("Failed to connect to server");
|
2011-02-08 00:17:58 +01:00
|
|
|
|
2013-10-15 22:58:38 +02:00
|
|
|
roar_use_audio_format(&info, audio_format);
|
2011-02-08 00:17:58 +01:00
|
|
|
|
2016-11-09 08:29:44 +01:00
|
|
|
if (roar_vs_stream(vss, &info, ROAR_DIR_PLAY, &err) < 0)
|
|
|
|
throw std::runtime_error("Failed to start stream");
|
2011-02-08 00:17:58 +01:00
|
|
|
|
2013-10-15 22:58:38 +02:00
|
|
|
roar_vs_role(vss, role, &err);
|
|
|
|
alive = true;
|
2011-02-08 00:17:58 +01:00
|
|
|
}
|
|
|
|
|
2013-10-15 22:58:38 +02:00
|
|
|
inline void
|
|
|
|
RoarOutput::Close()
|
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2013-10-15 22:58:38 +02:00
|
|
|
|
|
|
|
alive = false;
|
2011-09-17 20:10:26 +02:00
|
|
|
|
2013-10-15 22:58:38 +02:00
|
|
|
if (vss != nullptr)
|
|
|
|
roar_vs_close(vss, ROAR_VS_TRUE, &err);
|
|
|
|
vss = nullptr;
|
|
|
|
roar_disconnect(&con);
|
2011-09-17 20:10:26 +02:00
|
|
|
}
|
|
|
|
|
2013-10-15 22:58:38 +02:00
|
|
|
inline void
|
|
|
|
RoarOutput::Cancel()
|
|
|
|
{
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2011-09-18 11:51:18 +02:00
|
|
|
|
2013-01-16 23:29:56 +01:00
|
|
|
if (vss == nullptr)
|
2011-09-18 11:51:18 +02:00
|
|
|
return;
|
|
|
|
|
2013-10-15 22:58:38 +02:00
|
|
|
roar_vs_t *_vss = vss;
|
|
|
|
vss = nullptr;
|
|
|
|
roar_vs_close(_vss, ROAR_VS_TRUE, &err);
|
|
|
|
alive = false;
|
|
|
|
|
|
|
|
_vss = roar_vs_new_from_con(&con, &err);
|
|
|
|
if (_vss == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (roar_vs_stream(_vss, &info, ROAR_DIR_PLAY, &err) < 0) {
|
|
|
|
roar_vs_close(_vss, ROAR_VS_TRUE, &err);
|
2013-09-27 22:31:24 +02:00
|
|
|
LogError(roar_output_domain, "Failed to start stream");
|
2011-09-18 11:54:14 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-15 22:58:38 +02:00
|
|
|
roar_vs_role(_vss, role, &err);
|
|
|
|
vss = _vss;
|
|
|
|
alive = true;
|
2011-09-18 11:51:18 +02:00
|
|
|
}
|
|
|
|
|
2013-10-15 22:58:38 +02:00
|
|
|
inline size_t
|
2016-11-09 11:56:01 +01:00
|
|
|
RoarOutput::Play(const void *chunk, size_t size)
|
2011-02-08 00:17:58 +01:00
|
|
|
{
|
2016-11-09 08:29:44 +01:00
|
|
|
if (vss == nullptr)
|
|
|
|
throw std::runtime_error("Connection is invalid");
|
2011-02-08 00:17:58 +01:00
|
|
|
|
2013-10-15 22:58:38 +02:00
|
|
|
ssize_t nbytes = roar_vs_write(vss, chunk, size, &err);
|
2016-11-09 08:29:44 +01:00
|
|
|
if (nbytes <= 0)
|
|
|
|
throw std::runtime_error("Failed to play data");
|
2011-02-08 00:17:58 +01:00
|
|
|
|
2013-10-15 22:58:38 +02:00
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2011-02-08 00:17:58 +01:00
|
|
|
static const char*
|
2013-10-20 13:32:59 +02:00
|
|
|
roar_tag_convert(TagType type, bool *is_uuid)
|
2011-02-08 00:17:58 +01:00
|
|
|
{
|
|
|
|
*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";
|
2014-09-27 18:38:23 +02:00
|
|
|
case TAG_MUSICBRAINZ_RELEASETRACKID:
|
|
|
|
*is_uuid = true;
|
|
|
|
return "HASH";
|
2011-02-08 00:17:58 +01:00
|
|
|
|
|
|
|
default:
|
2013-01-16 23:29:56 +01:00
|
|
|
return nullptr;
|
2011-02-08 00:17:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-15 22:58:38 +02:00
|
|
|
inline void
|
|
|
|
RoarOutput::SendTag(const Tag &tag)
|
2011-02-08 00:17:58 +01:00
|
|
|
{
|
2013-10-15 22:58:38 +02:00
|
|
|
if (vss == nullptr)
|
2011-02-08 00:17:58 +01:00
|
|
|
return;
|
|
|
|
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2013-01-16 23:29:56 +01:00
|
|
|
|
2014-08-29 12:14:27 +02:00
|
|
|
size_t cnt = 0;
|
2011-02-08 00:17:58 +01:00
|
|
|
struct roar_keyval vals[32];
|
|
|
|
char uuid_buf[32][64];
|
|
|
|
|
|
|
|
char timebuf[16];
|
2014-08-29 12:14:27 +02:00
|
|
|
if (!tag.duration.IsNegative()) {
|
|
|
|
const unsigned seconds = tag.duration.ToS();
|
|
|
|
snprintf(timebuf, sizeof(timebuf), "%02d:%02d:%02d",
|
|
|
|
seconds / 3600, (seconds % 3600) / 60, seconds % 60);
|
|
|
|
|
|
|
|
vals[cnt].key = const_cast<char *>("LENGTH");
|
|
|
|
vals[cnt].value = timebuf;
|
|
|
|
++cnt;
|
|
|
|
}
|
2011-02-08 00:17:58 +01:00
|
|
|
|
2014-07-12 17:22:39 +02:00
|
|
|
for (const auto &item : tag) {
|
|
|
|
if (cnt >= 32)
|
|
|
|
break;
|
|
|
|
|
2011-02-08 00:17:58 +01:00
|
|
|
bool is_uuid = false;
|
2014-07-12 17:22:39 +02:00
|
|
|
const char *key = roar_tag_convert(item.type,
|
2013-10-15 22:58:38 +02:00
|
|
|
&is_uuid);
|
|
|
|
if (key != nullptr) {
|
2013-10-15 23:14:34 +02:00
|
|
|
vals[cnt].key = const_cast<char *>(key);
|
|
|
|
|
2013-10-15 22:58:38 +02:00
|
|
|
if (is_uuid) {
|
2011-02-08 00:17:58 +01:00
|
|
|
snprintf(uuid_buf[cnt], sizeof(uuid_buf[0]), "{UUID}%s",
|
2014-07-12 17:22:39 +02:00
|
|
|
item.value);
|
2011-02-08 00:17:58 +01:00
|
|
|
vals[cnt].value = uuid_buf[cnt];
|
2013-10-15 22:58:38 +02:00
|
|
|
} else {
|
2014-07-12 17:22:39 +02:00
|
|
|
vals[cnt].value = const_cast<char *>(item.value);
|
2011-02-08 00:17:58 +01:00
|
|
|
}
|
2013-10-15 22:58:38 +02:00
|
|
|
|
2011-02-08 00:17:58 +01:00
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-15 22:58:38 +02:00
|
|
|
roar_vs_meta(vss, vals, cnt, &(err));
|
2011-02-08 00:17:58 +01:00
|
|
|
}
|
|
|
|
|
2014-12-29 23:45:14 +01:00
|
|
|
typedef AudioOutputWrapper<RoarOutput> Wrapper;
|
|
|
|
|
2014-01-28 11:22:27 +01:00
|
|
|
const struct AudioOutputPlugin roar_output_plugin = {
|
2013-01-16 23:29:56 +01:00
|
|
|
"roar",
|
|
|
|
nullptr,
|
2017-01-25 09:56:51 +01:00
|
|
|
&Wrapper::Init,
|
2014-12-29 23:45:14 +01:00
|
|
|
&Wrapper::Finish,
|
2013-01-16 23:29:56 +01:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2014-12-29 23:45:14 +01:00
|
|
|
&Wrapper::Open,
|
|
|
|
&Wrapper::Close,
|
2013-01-16 23:29:56 +01:00
|
|
|
nullptr,
|
2017-01-25 09:56:51 +01:00
|
|
|
&Wrapper::SendTag,
|
2014-12-29 23:45:14 +01:00
|
|
|
&Wrapper::Play,
|
2013-01-16 23:29:56 +01:00
|
|
|
nullptr,
|
2014-12-29 23:45:14 +01:00
|
|
|
&Wrapper::Cancel,
|
2013-01-16 23:29:56 +01:00
|
|
|
nullptr,
|
|
|
|
&roar_mixer_plugin,
|
2011-02-08 00:17:58 +01:00
|
|
|
};
|