2011-03-27 08:41:05 +02:00
|
|
|
/*
|
2013-01-21 18:24:26 +01:00
|
|
|
* Copyright (C) 2011-2013 The Music Player Daemon Project
|
2011-03-27 08:41:05 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* 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-21 18:24:26 +01:00
|
|
|
#include "DespotifyInputPlugin.hxx"
|
|
|
|
#include "DespotifyUtils.hxx"
|
2013-01-24 19:14:40 +01:00
|
|
|
#include "InputStream.hxx"
|
2013-01-25 22:43:01 +01:00
|
|
|
#include "InputPlugin.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/Tag.hxx"
|
2013-11-28 18:48:35 +01:00
|
|
|
#include "util/StringUtil.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2013-01-21 18:24:26 +01:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <despotify.h>
|
|
|
|
}
|
2011-03-27 08:41:05 +02:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-12-14 13:49:56 +01:00
|
|
|
class DespotifyInputStream {
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream base;
|
2011-03-27 08:41:05 +02:00
|
|
|
|
|
|
|
struct despotify_session *session;
|
|
|
|
struct ds_track *track;
|
2014-01-08 19:21:48 +01:00
|
|
|
Tag tag;
|
2011-03-27 08:41:05 +02:00
|
|
|
struct ds_pcm_data pcm;
|
|
|
|
size_t len_available;
|
|
|
|
bool eof;
|
2013-01-28 23:13:19 +01:00
|
|
|
|
|
|
|
DespotifyInputStream(const char *uri,
|
|
|
|
Mutex &mutex, Cond &cond,
|
|
|
|
despotify_session *_session,
|
|
|
|
ds_track *_track)
|
2013-01-28 20:32:23 +01:00
|
|
|
:base(input_plugin_despotify, uri, mutex, cond),
|
|
|
|
session(_session), track(_track),
|
2014-01-08 19:22:20 +01:00
|
|
|
tag(mpd_despotify_tag_from_track(*track)),
|
2013-01-28 23:13:19 +01:00
|
|
|
len_available(0), eof(false) {
|
|
|
|
|
|
|
|
memset(&pcm, 0, sizeof(pcm));
|
|
|
|
|
|
|
|
/* Despotify outputs pcm data */
|
2013-10-15 21:18:55 +02:00
|
|
|
base.mime = "audio/x-mpd-cdda-pcm";
|
2013-01-28 23:13:19 +01:00
|
|
|
base.ready = true;
|
|
|
|
}
|
|
|
|
|
2013-12-14 13:49:56 +01:00
|
|
|
public:
|
2013-01-28 23:13:19 +01:00
|
|
|
~DespotifyInputStream() {
|
|
|
|
despotify_free_track(track);
|
|
|
|
}
|
2013-12-14 13:49:56 +01:00
|
|
|
|
|
|
|
static InputStream *Open(const char *url, Mutex &mutex, Cond &cond,
|
|
|
|
Error &error);
|
|
|
|
|
|
|
|
bool IsEOF() const {
|
|
|
|
return eof;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Read(void *ptr, size_t size, Error &error);
|
|
|
|
|
|
|
|
Tag *ReadTag() {
|
2014-01-08 19:21:48 +01:00
|
|
|
if (tag.IsEmpty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
Tag *result = new Tag(std::move(tag));
|
|
|
|
tag.Clear();
|
2013-12-14 13:49:56 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Callback(int sig);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void FillBuffer();
|
2011-03-27 08:41:05 +02:00
|
|
|
};
|
|
|
|
|
2013-12-14 13:49:56 +01:00
|
|
|
inline void
|
|
|
|
DespotifyInputStream::FillBuffer()
|
2011-03-27 08:41:05 +02:00
|
|
|
{
|
|
|
|
/* Wait until there is data */
|
|
|
|
while (1) {
|
2013-12-14 13:49:56 +01:00
|
|
|
int rc = despotify_get_pcm(session, &pcm);
|
2011-03-27 08:41:05 +02:00
|
|
|
|
2013-12-14 13:49:56 +01:00
|
|
|
if (rc == 0 && pcm.len) {
|
|
|
|
len_available = pcm.len;
|
2011-03-27 08:41:05 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-12-14 13:49:56 +01:00
|
|
|
|
|
|
|
if (eof == true)
|
2011-03-27 08:41:05 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (rc < 0) {
|
2013-09-27 22:31:24 +02:00
|
|
|
LogDebug(despotify_domain, "despotify_get_pcm error");
|
2013-12-14 13:49:56 +01:00
|
|
|
eof = true;
|
2011-03-27 08:41:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait a while until next iteration */
|
|
|
|
usleep(50 * 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-14 13:49:56 +01:00
|
|
|
inline void
|
|
|
|
DespotifyInputStream::Callback(int sig)
|
2011-03-27 08:41:05 +02:00
|
|
|
{
|
|
|
|
switch (sig) {
|
|
|
|
case DESPOTIFY_NEW_TRACK:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DESPOTIFY_TIME_TELL:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DESPOTIFY_TRACK_PLAY_ERROR:
|
2013-09-27 22:31:24 +02:00
|
|
|
LogWarning(despotify_domain, "Track play error");
|
2013-12-14 13:49:56 +01:00
|
|
|
eof = true;
|
|
|
|
len_available = 0;
|
2011-03-27 08:41:05 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DESPOTIFY_END_OF_PLAYLIST:
|
2013-12-14 13:49:56 +01:00
|
|
|
eof = true;
|
2013-12-14 13:58:28 +01:00
|
|
|
LogDebug(despotify_domain, "End of playlist");
|
2011-03-27 08:41:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-14 13:49:56 +01:00
|
|
|
static void callback(gcc_unused struct despotify_session* ds,
|
|
|
|
int sig, gcc_unused void* data, void* callback_data)
|
2011-03-27 08:41:05 +02:00
|
|
|
{
|
2013-12-14 13:49:56 +01:00
|
|
|
DespotifyInputStream *ctx = (DespotifyInputStream *)callback_data;
|
|
|
|
|
|
|
|
ctx->Callback(sig);
|
|
|
|
}
|
2011-03-27 08:41:05 +02:00
|
|
|
|
2013-12-14 13:49:56 +01:00
|
|
|
inline InputStream *
|
|
|
|
DespotifyInputStream::Open(const char *url,
|
|
|
|
Mutex &mutex, Cond &cond,
|
|
|
|
gcc_unused Error &error)
|
|
|
|
{
|
2013-11-28 18:48:35 +01:00
|
|
|
if (!StringStartsWith(url, "spt://"))
|
2013-10-28 23:58:17 +01:00
|
|
|
return nullptr;
|
2011-03-27 08:41:05 +02:00
|
|
|
|
2013-12-14 13:49:56 +01:00
|
|
|
despotify_session *session = mpd_despotify_get_session();
|
|
|
|
if (session == nullptr)
|
2013-10-28 23:58:17 +01:00
|
|
|
return nullptr;
|
2011-03-27 08:41:05 +02:00
|
|
|
|
2013-12-14 13:49:56 +01:00
|
|
|
ds_link *ds_link = despotify_link_from_uri(url + 6);
|
2011-03-27 08:41:05 +02:00
|
|
|
if (!ds_link) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(despotify_domain, "Can't find %s", url);
|
2013-10-28 23:58:17 +01:00
|
|
|
return nullptr;
|
2011-03-27 08:41:05 +02:00
|
|
|
}
|
|
|
|
if (ds_link->type != LINK_TYPE_TRACK) {
|
|
|
|
despotify_free_link(ds_link);
|
2013-10-28 23:58:17 +01:00
|
|
|
return nullptr;
|
2011-03-27 08:41:05 +02:00
|
|
|
}
|
|
|
|
|
2013-12-14 13:49:56 +01:00
|
|
|
ds_track *track = despotify_link_get_track(session, ds_link);
|
2011-03-27 08:41:05 +02:00
|
|
|
despotify_free_link(ds_link);
|
2013-01-28 23:13:19 +01:00
|
|
|
if (!track)
|
2013-10-28 23:58:17 +01:00
|
|
|
return nullptr;
|
2011-03-27 08:41:05 +02:00
|
|
|
|
2013-01-28 23:13:19 +01:00
|
|
|
DespotifyInputStream *ctx =
|
|
|
|
new DespotifyInputStream(url, mutex, cond,
|
|
|
|
session, track);
|
2011-03-27 08:41:05 +02:00
|
|
|
|
|
|
|
if (!mpd_despotify_register_callback(callback, ctx)) {
|
2013-01-28 23:13:19 +01:00
|
|
|
delete ctx;
|
2013-10-28 23:58:17 +01:00
|
|
|
return nullptr;
|
2011-03-27 08:41:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (despotify_play(ctx->session, ctx->track, false) == false) {
|
2013-01-28 23:13:19 +01:00
|
|
|
mpd_despotify_unregister_callback(callback);
|
|
|
|
delete ctx;
|
2013-10-28 23:58:17 +01:00
|
|
|
return nullptr;
|
2011-03-27 08:41:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &ctx->base;
|
|
|
|
}
|
|
|
|
|
2013-12-14 13:49:56 +01:00
|
|
|
static InputStream *
|
|
|
|
input_despotify_open(const char *url, Mutex &mutex, Cond &cond, Error &error)
|
2011-03-27 08:41:05 +02:00
|
|
|
{
|
2013-12-14 13:49:56 +01:00
|
|
|
return DespotifyInputStream::Open(url, mutex, cond, error);
|
|
|
|
}
|
2011-03-27 08:41:05 +02:00
|
|
|
|
2013-12-14 13:49:56 +01:00
|
|
|
inline size_t
|
|
|
|
DespotifyInputStream::Read(void *ptr, size_t size, gcc_unused Error &error)
|
|
|
|
{
|
|
|
|
if (len_available == 0)
|
|
|
|
FillBuffer();
|
2011-03-27 08:41:05 +02:00
|
|
|
|
2013-12-14 13:49:56 +01:00
|
|
|
size_t to_cpy = std::min(size, len_available);
|
|
|
|
memcpy(ptr, pcm.buf, to_cpy);
|
|
|
|
len_available -= to_cpy;
|
2011-03-27 08:41:05 +02:00
|
|
|
|
2013-12-14 13:49:56 +01:00
|
|
|
base.offset += to_cpy;
|
2011-03-27 08:41:05 +02:00
|
|
|
|
|
|
|
return to_cpy;
|
|
|
|
}
|
|
|
|
|
2013-12-14 13:49:56 +01:00
|
|
|
static size_t
|
|
|
|
input_despotify_read(InputStream *is, void *ptr, size_t size, Error &error)
|
|
|
|
{
|
|
|
|
DespotifyInputStream *ctx = (DespotifyInputStream *)is;
|
|
|
|
return ctx->Read(ptr, size, error);
|
|
|
|
}
|
|
|
|
|
2011-03-27 08:41:05 +02:00
|
|
|
static void
|
2013-10-23 22:08:59 +02:00
|
|
|
input_despotify_close(InputStream *is)
|
2011-03-27 08:41:05 +02:00
|
|
|
{
|
2013-01-28 23:12:10 +01:00
|
|
|
DespotifyInputStream *ctx = (DespotifyInputStream *)is;
|
2011-03-27 08:41:05 +02:00
|
|
|
|
|
|
|
mpd_despotify_unregister_callback(callback);
|
2013-01-28 23:13:19 +01:00
|
|
|
delete ctx;
|
2011-03-27 08:41:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
input_despotify_eof(InputStream *is)
|
2011-03-27 08:41:05 +02:00
|
|
|
{
|
2013-01-28 23:12:10 +01:00
|
|
|
DespotifyInputStream *ctx = (DespotifyInputStream *)is;
|
2011-03-27 08:41:05 +02:00
|
|
|
|
2013-12-14 13:49:56 +01:00
|
|
|
return ctx->IsEOF();
|
2011-03-27 08:41:05 +02:00
|
|
|
}
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
static Tag *
|
2013-10-23 22:08:59 +02:00
|
|
|
input_despotify_tag(InputStream *is)
|
2011-03-27 08:41:05 +02:00
|
|
|
{
|
2013-01-28 23:12:10 +01:00
|
|
|
DespotifyInputStream *ctx = (DespotifyInputStream *)is;
|
2011-03-27 08:41:05 +02:00
|
|
|
|
2013-12-14 13:49:56 +01:00
|
|
|
return ctx->ReadTag();
|
2011-03-27 08:41:05 +02:00
|
|
|
}
|
|
|
|
|
2013-10-17 10:20:57 +02:00
|
|
|
const InputPlugin input_plugin_despotify = {
|
2013-01-21 18:24:26 +01:00
|
|
|
"spt",
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
input_despotify_open,
|
|
|
|
input_despotify_close,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2013-09-03 10:54:29 +02:00
|
|
|
input_despotify_tag,
|
2013-01-21 18:24:26 +01:00
|
|
|
nullptr,
|
|
|
|
input_despotify_read,
|
|
|
|
input_despotify_eof,
|
2013-09-03 10:54:29 +02:00
|
|
|
nullptr,
|
2011-03-27 08:41:05 +02:00
|
|
|
};
|