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-25 22:43:01 +01:00
|
|
|
#include "InputInternal.hxx"
|
2013-01-24 19:14:40 +01:00
|
|
|
#include "InputStream.hxx"
|
2013-01-25 22:43:01 +01:00
|
|
|
#include "InputPlugin.hxx"
|
2013-07-30 20:11:57 +02:00
|
|
|
#include "Tag.hxx"
|
2013-01-21 18:24:26 +01:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <despotify.h>
|
|
|
|
}
|
2011-03-27 08:41:05 +02:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2013-01-28 23:12:10 +01:00
|
|
|
struct DespotifyInputStream {
|
2011-03-27 08:41:05 +02:00
|
|
|
struct input_stream base;
|
|
|
|
|
|
|
|
struct despotify_session *session;
|
|
|
|
struct ds_track *track;
|
2013-07-30 20:11:57 +02: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),
|
2013-01-28 23:13:19 +01:00
|
|
|
tag(mpd_despotify_tag_from_track(track)),
|
|
|
|
len_available(0), eof(false) {
|
|
|
|
|
|
|
|
memset(&pcm, 0, sizeof(pcm));
|
|
|
|
|
|
|
|
/* Despotify outputs pcm data */
|
|
|
|
base.mime = g_strdup("audio/x-mpd-cdda-pcm");
|
|
|
|
base.ready = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
~DespotifyInputStream() {
|
2013-07-30 20:11:57 +02:00
|
|
|
delete tag;
|
2013-01-28 23:13:19 +01:00
|
|
|
|
|
|
|
despotify_free_track(track);
|
|
|
|
}
|
2011-03-27 08:41:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2013-01-28 23:12:10 +01:00
|
|
|
refill_buffer(DespotifyInputStream *ctx)
|
2011-03-27 08:41:05 +02:00
|
|
|
{
|
|
|
|
/* Wait until there is data */
|
|
|
|
while (1) {
|
|
|
|
int rc = despotify_get_pcm(ctx->session, &ctx->pcm);
|
|
|
|
|
|
|
|
if (rc == 0 && ctx->pcm.len) {
|
|
|
|
ctx->len_available = ctx->pcm.len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ctx->eof == true)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
g_debug("despotify_get_pcm error\n");
|
|
|
|
ctx->eof = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait a while until next iteration */
|
|
|
|
usleep(50 * 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-04 23:48:01 +02: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-01-28 23:12:10 +01:00
|
|
|
DespotifyInputStream *ctx = (DespotifyInputStream *)callback_data;
|
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:
|
|
|
|
g_debug("Track play error\n");
|
|
|
|
ctx->eof = true;
|
|
|
|
ctx->len_available = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DESPOTIFY_END_OF_PLAYLIST:
|
|
|
|
ctx->eof = true;
|
|
|
|
g_debug("End of playlist: %d\n", ctx->eof);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct input_stream *
|
2011-09-14 21:46:41 +02:00
|
|
|
input_despotify_open(const char *url,
|
2013-01-27 17:20:50 +01:00
|
|
|
Mutex &mutex, Cond &cond,
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_unused GError **error_r)
|
2011-03-27 08:41:05 +02:00
|
|
|
{
|
|
|
|
struct despotify_session *session;
|
|
|
|
struct ds_link *ds_link;
|
|
|
|
struct ds_track *track;
|
|
|
|
|
|
|
|
if (!g_str_has_prefix(url, "spt://"))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
session = mpd_despotify_get_session();
|
|
|
|
if (!session)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ds_link = despotify_link_from_uri(url + 6);
|
|
|
|
if (!ds_link) {
|
|
|
|
g_debug("Can't find %s\n", url);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (ds_link->type != LINK_TYPE_TRACK) {
|
|
|
|
despotify_free_link(ds_link);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
track = despotify_link_get_track(session, ds_link);
|
|
|
|
despotify_free_link(ds_link);
|
2013-01-28 23:13:19 +01:00
|
|
|
if (!track)
|
2011-03-27 08:41:05 +02:00
|
|
|
return NULL;
|
|
|
|
|
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;
|
2011-03-27 08:41:05 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (despotify_play(ctx->session, ctx->track, false) == false) {
|
2013-01-28 23:13:19 +01:00
|
|
|
mpd_despotify_unregister_callback(callback);
|
|
|
|
delete ctx;
|
2011-03-27 08:41:05 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ctx->base;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
input_despotify_read(struct input_stream *is, void *ptr, size_t size,
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_unused GError **error_r)
|
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
|
|
|
size_t to_cpy = size;
|
|
|
|
|
|
|
|
if (ctx->len_available == 0)
|
|
|
|
refill_buffer(ctx);
|
|
|
|
|
|
|
|
if (ctx->len_available < size)
|
|
|
|
to_cpy = ctx->len_available;
|
|
|
|
memcpy(ptr, ctx->pcm.buf, to_cpy);
|
|
|
|
ctx->len_available -= to_cpy;
|
|
|
|
|
|
|
|
is->offset += to_cpy;
|
|
|
|
|
|
|
|
return to_cpy;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
input_despotify_close(struct input_stream *is)
|
|
|
|
{
|
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
|
|
|
|
input_despotify_eof(struct input_stream *is)
|
|
|
|
{
|
2013-01-28 23:12:10 +01:00
|
|
|
DespotifyInputStream *ctx = (DespotifyInputStream *)is;
|
2011-03-27 08:41:05 +02:00
|
|
|
|
|
|
|
return ctx->eof;
|
|
|
|
}
|
|
|
|
|
2013-07-30 20:11:57 +02:00
|
|
|
static Tag *
|
2011-03-27 08:41:05 +02:00
|
|
|
input_despotify_tag(struct input_stream *is)
|
|
|
|
{
|
2013-01-28 23:12:10 +01:00
|
|
|
DespotifyInputStream *ctx = (DespotifyInputStream *)is;
|
2013-07-30 20:11:57 +02:00
|
|
|
Tag *tag = ctx->tag;
|
2011-03-27 08:41:05 +02:00
|
|
|
|
|
|
|
ctx->tag = NULL;
|
|
|
|
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct input_plugin 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
|
|
|
};
|