From 9ec069104f82184fbdc33bb82ead393441a41728 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 21 Jan 2013 18:24:26 +0100 Subject: input,playlist/despotify: convert to C++ --- src/DespotifyUtils.cxx | 144 +++++++++++++++++++ src/DespotifyUtils.hxx | 67 +++++++++ src/InputRegistry.cxx | 2 +- src/despotify_utils.c | 140 ------------------ src/despotify_utils.h | 69 --------- src/input/DespotifyInputPlugin.cxx | 238 +++++++++++++++++++++++++++++++ src/input/DespotifyInputPlugin.hxx | 25 ++++ src/input/despotify_input_plugin.c | 230 ----------------------------- src/input/despotify_input_plugin.h | 25 ---- src/playlist/DespotifyPlaylistPlugin.cxx | 223 +++++++++++++++++++++++++++++ src/playlist/DespotifyPlaylistPlugin.hxx | 25 ++++ src/playlist/despotify_playlist_plugin.c | 217 ---------------------------- src/playlist/despotify_playlist_plugin.h | 25 ---- src/playlist_list.c | 2 +- 14 files changed, 724 insertions(+), 708 deletions(-) create mode 100644 src/DespotifyUtils.cxx create mode 100644 src/DespotifyUtils.hxx delete mode 100644 src/despotify_utils.c delete mode 100644 src/despotify_utils.h create mode 100644 src/input/DespotifyInputPlugin.cxx create mode 100644 src/input/DespotifyInputPlugin.hxx delete mode 100644 src/input/despotify_input_plugin.c delete mode 100644 src/input/despotify_input_plugin.h create mode 100644 src/playlist/DespotifyPlaylistPlugin.cxx create mode 100644 src/playlist/DespotifyPlaylistPlugin.hxx delete mode 100644 src/playlist/despotify_playlist_plugin.c delete mode 100644 src/playlist/despotify_playlist_plugin.h (limited to 'src') diff --git a/src/DespotifyUtils.cxx b/src/DespotifyUtils.cxx new file mode 100644 index 000000000..c9a1edf0c --- /dev/null +++ b/src/DespotifyUtils.cxx @@ -0,0 +1,144 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * 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 "DespotifyUtils.hxx" +#include "tag.h" +#include "conf.h" + +#include + +extern "C" { +#include +} + +static struct despotify_session *g_session; +static void (*registered_callbacks[8])(struct despotify_session *, + int, void *, void *); +static void *registered_callback_data[8]; + +static void callback(struct despotify_session* ds, int sig, + void* data, G_GNUC_UNUSED void* callback_data) +{ + size_t i; + + for (i = 0; i < sizeof(registered_callbacks) / sizeof(registered_callbacks[0]); i++) { + void (*cb)(struct despotify_session *, int, void *, void *) = registered_callbacks[i]; + void *cb_data = registered_callback_data[i]; + + if (cb) + cb(ds, sig, data, cb_data); + } +} + +bool mpd_despotify_register_callback(void (*cb)(struct despotify_session *, int, void *, void *), + void *cb_data) +{ + size_t i; + + for (i = 0; i < sizeof(registered_callbacks) / sizeof(registered_callbacks[0]); i++) { + + if (!registered_callbacks[i]) { + registered_callbacks[i] = cb; + registered_callback_data[i] = cb_data; + + return true; + } + } + + return false; +} + +void mpd_despotify_unregister_callback(void (*cb)(struct despotify_session *, int, void *, void *)) +{ + size_t i; + + for (i = 0; i < sizeof(registered_callbacks) / sizeof(registered_callbacks[0]); i++) { + + if (registered_callbacks[i] == cb) { + registered_callbacks[i] = NULL; + } + } +} + + +struct tag *mpd_despotify_tag_from_track(struct ds_track *track) +{ + char tracknum[20]; + char comment[80]; + char date[20]; + struct tag *tag; + + tag = tag_new(); + + if (!track->has_meta_data) + return tag; + + g_snprintf(tracknum, sizeof(tracknum), "%d", track->tracknumber); + g_snprintf(date, sizeof(date), "%d", track->year); + g_snprintf(comment, sizeof(comment), "Bitrate %d Kbps, %sgeo restricted", + track->file_bitrate / 1000, track->geo_restricted ? "" : "not "); + tag_add_item(tag, TAG_TITLE, track->title); + tag_add_item(tag, TAG_ARTIST, track->artist->name); + tag_add_item(tag, TAG_TRACK, tracknum); + tag_add_item(tag, TAG_ALBUM, track->album); + tag_add_item(tag, TAG_DATE, date); + tag_add_item(tag, TAG_COMMENT, comment); + tag->time = track->length / 1000; + + return tag; +} + +struct despotify_session *mpd_despotify_get_session(void) +{ + const char *user; + const char *passwd; + bool high_bitrate; + + if (g_session) + return g_session; + + user = config_get_string(CONF_DESPOTIFY_USER, NULL); + passwd = config_get_string(CONF_DESPOTIFY_PASSWORD, NULL); + high_bitrate = config_get_bool(CONF_DESPOTIFY_HIGH_BITRATE, true); + + if (user == NULL || passwd == NULL) { + g_debug("disabling despotify because account is not configured"); + return nullptr; + } + + if (!despotify_init()) { + g_debug("Can't initialize despotify\n"); + return nullptr; + } + + g_session = despotify_init_client(callback, NULL, + high_bitrate, true); + if (!g_session) { + g_debug("Can't initialize despotify client\n"); + return nullptr; + } + + if (!despotify_authenticate(g_session, user, passwd)) { + g_debug("Can't authenticate despotify session\n"); + despotify_exit(g_session); + return nullptr; + } + + return g_session; +} diff --git a/src/DespotifyUtils.hxx b/src/DespotifyUtils.hxx new file mode 100644 index 000000000..7e35edc3c --- /dev/null +++ b/src/DespotifyUtils.hxx @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2003-2011 The Music Player Daemon Project + * 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. + */ + +#ifndef MPD_DESPOTIFY_H +#define MPD_DESPOTIFY_H + +struct despotify_session; +struct ds_track; + +/** + * Return the current despotify session. + * + * If the session isn't initialized, this function will initialize + * it and connect to Spotify. + * + * @return a pointer to the despotify session, or NULL if it can't + * be initialized (e.g., if the configuration isn't supplied) + */ +struct despotify_session *mpd_despotify_get_session(void); + +/** + * Create a MPD tags structure from a spotify track + * + * @param track the track to convert + * + * @return a pointer to the filled in tags structure + */ +struct tag *mpd_despotify_tag_from_track(struct ds_track *track); + +/** + * Register a despotify callback. + * + * Despotify calls this e.g., when a track ends. + * + * @param cb the callback + * @param cb_data the data to pass to the callback + * + * @return true if the callback could be registered + */ +bool mpd_despotify_register_callback(void (*cb)(struct despotify_session *, int, void *, void *), + void *cb_data); + +/** + * Unregister a despotify callback. + * + * @param cb the callback to unregister. + */ +void mpd_despotify_unregister_callback(void (*cb)(struct despotify_session *, int, void *, void *)); + +#endif + diff --git a/src/InputRegistry.cxx b/src/InputRegistry.cxx index 6c0898477..fa468724b 100644 --- a/src/InputRegistry.cxx +++ b/src/InputRegistry.cxx @@ -46,7 +46,7 @@ #endif #ifdef ENABLE_DESPOTIFY -#include "input/despotify_input_plugin.h" +#include "input/DespotifyInputPlugin.hxx" #endif #include diff --git a/src/despotify_utils.c b/src/despotify_utils.c deleted file mode 100644 index 9f23c6336..000000000 --- a/src/despotify_utils.c +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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 "despotify_utils.h" -#include "tag.h" -#include "conf.h" - -#include -#include - -static struct despotify_session *g_session; -static void (*registered_callbacks[8])(struct despotify_session *, - int, void *, void *); -static void *registered_callback_data[8]; - -static void callback(struct despotify_session* ds, int sig, - void* data, G_GNUC_UNUSED void* callback_data) -{ - size_t i; - - for (i = 0; i < sizeof(registered_callbacks) / sizeof(registered_callbacks[0]); i++) { - void (*cb)(struct despotify_session *, int, void *, void *) = registered_callbacks[i]; - void *cb_data = registered_callback_data[i]; - - if (cb) - cb(ds, sig, data, cb_data); - } -} - -bool mpd_despotify_register_callback(void (*cb)(struct despotify_session *, int, void *, void *), - void *cb_data) -{ - size_t i; - - for (i = 0; i < sizeof(registered_callbacks) / sizeof(registered_callbacks[0]); i++) { - - if (!registered_callbacks[i]) { - registered_callbacks[i] = cb; - registered_callback_data[i] = cb_data; - - return true; - } - } - - return false; -} - -void mpd_despotify_unregister_callback(void (*cb)(struct despotify_session *, int, void *, void *)) -{ - size_t i; - - for (i = 0; i < sizeof(registered_callbacks) / sizeof(registered_callbacks[0]); i++) { - - if (registered_callbacks[i] == cb) { - registered_callbacks[i] = NULL; - } - } -} - - -struct tag *mpd_despotify_tag_from_track(struct ds_track *track) -{ - char tracknum[20]; - char comment[80]; - char date[20]; - struct tag *tag; - - tag = tag_new(); - - if (!track->has_meta_data) - return tag; - - g_snprintf(tracknum, sizeof(tracknum), "%d", track->tracknumber); - g_snprintf(date, sizeof(date), "%d", track->year); - g_snprintf(comment, sizeof(comment), "Bitrate %d Kbps, %sgeo restricted", - track->file_bitrate / 1000, track->geo_restricted ? "" : "not "); - tag_add_item(tag, TAG_TITLE, track->title); - tag_add_item(tag, TAG_ARTIST, track->artist->name); - tag_add_item(tag, TAG_TRACK, tracknum); - tag_add_item(tag, TAG_ALBUM, track->album); - tag_add_item(tag, TAG_DATE, date); - tag_add_item(tag, TAG_COMMENT, comment); - tag->time = track->length / 1000; - - return tag; -} - -struct despotify_session *mpd_despotify_get_session(void) -{ - const char *user; - const char *passwd; - bool high_bitrate; - - if (g_session) - return g_session; - - user = config_get_string(CONF_DESPOTIFY_USER, NULL); - passwd = config_get_string(CONF_DESPOTIFY_PASSWORD, NULL); - high_bitrate = config_get_bool(CONF_DESPOTIFY_HIGH_BITRATE, true); - - if (user == NULL || passwd == NULL) { - g_debug("disabling despotify because account is not configured"); - return NULL; - } - if (!despotify_init()) { - g_debug("Can't initialize despotify\n"); - return false; - } - - g_session = despotify_init_client(callback, NULL, - high_bitrate, true); - if (!g_session) { - g_debug("Can't initialize despotify client\n"); - return false; - } - - if (!despotify_authenticate(g_session, user, passwd)) { - g_debug("Can't authenticate despotify session\n"); - despotify_exit(g_session); - return false; - } - - return g_session; -} diff --git a/src/despotify_utils.h b/src/despotify_utils.h deleted file mode 100644 index ab2968ab0..000000000 --- a/src/despotify_utils.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2003-2011 The Music Player Daemon Project - * 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. - */ - -#ifndef MPD_DESPOTIFY_H -#define MPD_DESPOTIFY_H - -#include - -struct despotify_session; -struct ds_track; - -/** - * Return the current despotify session. - * - * If the session isn't initialized, this function will initialize - * it and connect to Spotify. - * - * @return a pointer to the despotify session, or NULL if it can't - * be initialized (e.g., if the configuration isn't supplied) - */ -struct despotify_session *mpd_despotify_get_session(void); - -/** - * Create a MPD tags structure from a spotify track - * - * @param track the track to convert - * - * @return a pointer to the filled in tags structure - */ -struct tag *mpd_despotify_tag_from_track(struct ds_track *track); - -/** - * Register a despotify callback. - * - * Despotify calls this e.g., when a track ends. - * - * @param cb the callback - * @param cb_data the data to pass to the callback - * - * @return true if the callback could be registered - */ -bool mpd_despotify_register_callback(void (*cb)(struct despotify_session *, int, void *, void *), - void *cb_data); - -/** - * Unregister a despotify callback. - * - * @param cb the callback to unregister. - */ -void mpd_despotify_unregister_callback(void (*cb)(struct despotify_session *, int, void *, void *)); - -#endif - diff --git a/src/input/DespotifyInputPlugin.cxx b/src/input/DespotifyInputPlugin.cxx new file mode 100644 index 000000000..b42979e3b --- /dev/null +++ b/src/input/DespotifyInputPlugin.cxx @@ -0,0 +1,238 @@ +/* + * Copyright (C) 2011-2013 The Music Player Daemon Project + * 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" +#include "DespotifyInputPlugin.hxx" +#include "DespotifyUtils.hxx" +#include "input_internal.h" +#include "input_plugin.h" +#include "tag.h" + +extern "C" { +#include +} + +#include + +#include +#include +#include + +#include + +struct input_despotify { + struct input_stream base; + + struct despotify_session *session; + struct ds_track *track; + struct tag *tag; + struct ds_pcm_data pcm; + size_t len_available; + bool eof; +}; + + +static void +refill_buffer(struct input_despotify *ctx) +{ + /* 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); + } +} + +static void callback(G_GNUC_UNUSED struct despotify_session* ds, + int sig, G_GNUC_UNUSED void* data, void* callback_data) +{ + struct input_despotify *ctx = (struct input_despotify *)callback_data; + + 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 * +input_despotify_open(const char *url, + GMutex *mutex, GCond *cond, + G_GNUC_UNUSED GError **error_r) +{ + struct input_despotify *ctx; + 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; + } + + ctx = g_new(struct input_despotify, 1); + memset(ctx, 0, sizeof(*ctx)); + + track = despotify_link_get_track(session, ds_link); + despotify_free_link(ds_link); + if (!track) { + g_free(ctx); + return NULL; + } + + input_stream_init(&ctx->base, &input_plugin_despotify, url, + mutex, cond); + ctx->session = session; + ctx->track = track; + ctx->tag = mpd_despotify_tag_from_track(track); + ctx->eof = false; + /* Despotify outputs pcm data */ + ctx->base.mime = g_strdup("audio/x-mpd-cdda-pcm"); + ctx->base.ready = true; + + if (!mpd_despotify_register_callback(callback, ctx)) { + despotify_free_link(ds_link); + + return NULL; + } + + if (despotify_play(ctx->session, ctx->track, false) == false) { + despotify_free_track(ctx->track); + g_free(ctx); + return NULL; + } + + return &ctx->base; +} + +static size_t +input_despotify_read(struct input_stream *is, void *ptr, size_t size, + G_GNUC_UNUSED GError **error_r) +{ + struct input_despotify *ctx = (struct input_despotify *)is; + 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) +{ + struct input_despotify *ctx = (struct input_despotify *)is; + + if (ctx->tag != NULL) + tag_free(ctx->tag); + + mpd_despotify_unregister_callback(callback); + despotify_free_track(ctx->track); + input_stream_deinit(&ctx->base); + g_free(ctx); +} + +static bool +input_despotify_eof(struct input_stream *is) +{ + struct input_despotify *ctx = (struct input_despotify *)is; + + return ctx->eof; +} + +static bool +input_despotify_seek(G_GNUC_UNUSED struct input_stream *is, + G_GNUC_UNUSED goffset offset, G_GNUC_UNUSED int whence, + G_GNUC_UNUSED GError **error_r) +{ + return false; +} + +static struct tag * +input_despotify_tag(struct input_stream *is) +{ + struct input_despotify *ctx = (struct input_despotify *)is; + struct tag *tag = ctx->tag; + + ctx->tag = NULL; + + return tag; +} + +const struct input_plugin input_plugin_despotify = { + "spt", + nullptr, + nullptr, + input_despotify_open, + input_despotify_close, + nullptr, + nullptr, + .tag = input_despotify_tag, + nullptr, + input_despotify_read, + input_despotify_eof, + input_despotify_seek, +}; diff --git a/src/input/DespotifyInputPlugin.hxx b/src/input/DespotifyInputPlugin.hxx new file mode 100644 index 000000000..00d699408 --- /dev/null +++ b/src/input/DespotifyInputPlugin.hxx @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2011-2013 The Music Player Daemon Project + * 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. + */ + +#ifndef INPUT_DESPOTIFY_HXX +#define INPUT_DESPOTIFY_HXX + +extern const struct input_plugin input_plugin_despotify; + +#endif diff --git a/src/input/despotify_input_plugin.c b/src/input/despotify_input_plugin.c deleted file mode 100644 index 200a0afd6..000000000 --- a/src/input/despotify_input_plugin.c +++ /dev/null @@ -1,230 +0,0 @@ -/* - * Copyright (C) 2011 The Music Player Daemon Project - * 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" -#include "input/despotify_input_plugin.h" -#include "input_internal.h" -#include "input_plugin.h" -#include "tag.h" -#include "despotify_utils.h" - -#include - -#include -#include -#include -#include - -#include - -struct input_despotify { - struct input_stream base; - - struct despotify_session *session; - struct ds_track *track; - struct tag *tag; - struct ds_pcm_data pcm; - size_t len_available; - bool eof; -}; - - -static void -refill_buffer(struct input_despotify *ctx) -{ - /* 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); - } -} - -static void callback(G_GNUC_UNUSED struct despotify_session* ds, - int sig, G_GNUC_UNUSED void* data, void* callback_data) -{ - struct input_despotify *ctx = (struct input_despotify *)callback_data; - - 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 * -input_despotify_open(const char *url, - GMutex *mutex, GCond *cond, - G_GNUC_UNUSED GError **error_r) -{ - struct input_despotify *ctx; - 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; - } - - ctx = g_new(struct input_despotify, 1); - memset(ctx, 0, sizeof(*ctx)); - - track = despotify_link_get_track(session, ds_link); - despotify_free_link(ds_link); - if (!track) { - g_free(ctx); - return NULL; - } - - input_stream_init(&ctx->base, &input_plugin_despotify, url, - mutex, cond); - ctx->session = session; - ctx->track = track; - ctx->tag = mpd_despotify_tag_from_track(track); - ctx->eof = false; - /* Despotify outputs pcm data */ - ctx->base.mime = g_strdup("audio/x-mpd-cdda-pcm"); - ctx->base.ready = true; - - if (!mpd_despotify_register_callback(callback, ctx)) { - despotify_free_link(ds_link); - - return NULL; - } - - if (despotify_play(ctx->session, ctx->track, false) == false) { - despotify_free_track(ctx->track); - g_free(ctx); - return NULL; - } - - return &ctx->base; -} - -static size_t -input_despotify_read(struct input_stream *is, void *ptr, size_t size, - G_GNUC_UNUSED GError **error_r) -{ - struct input_despotify *ctx = (struct input_despotify *)is; - 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) -{ - struct input_despotify *ctx = (struct input_despotify *)is; - - if (ctx->tag != NULL) - tag_free(ctx->tag); - - mpd_despotify_unregister_callback(callback); - despotify_free_track(ctx->track); - input_stream_deinit(&ctx->base); - g_free(ctx); -} - -static bool -input_despotify_eof(struct input_stream *is) -{ - struct input_despotify *ctx = (struct input_despotify *)is; - - return ctx->eof; -} - -static bool -input_despotify_seek(G_GNUC_UNUSED struct input_stream *is, - G_GNUC_UNUSED goffset offset, G_GNUC_UNUSED int whence, - G_GNUC_UNUSED GError **error_r) -{ - return false; -} - -static struct tag * -input_despotify_tag(struct input_stream *is) -{ - struct input_despotify *ctx = (struct input_despotify *)is; - struct tag *tag = ctx->tag; - - ctx->tag = NULL; - - return tag; -} - -const struct input_plugin input_plugin_despotify = { - .name = "spt", - .open = input_despotify_open, - .close = input_despotify_close, - .read = input_despotify_read, - .eof = input_despotify_eof, - .seek = input_despotify_seek, - .tag = input_despotify_tag, -}; diff --git a/src/input/despotify_input_plugin.h b/src/input/despotify_input_plugin.h deleted file mode 100644 index 4c070d882..000000000 --- a/src/input/despotify_input_plugin.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) 2011 The Music Player Daemon Project - * 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. - */ - -#ifndef INPUT_DESPOTIFY_H -#define INPUT_DESPOTIFY_H - -extern const struct input_plugin input_plugin_despotify; - -#endif diff --git a/src/playlist/DespotifyPlaylistPlugin.cxx b/src/playlist/DespotifyPlaylistPlugin.cxx new file mode 100644 index 000000000..049c9499c --- /dev/null +++ b/src/playlist/DespotifyPlaylistPlugin.cxx @@ -0,0 +1,223 @@ +/* + * Copyright (C) 2011-2013 The Music Player Daemon Project + * 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" +#include "DespotifyPlaylistPlugin.hxx" +#include "DespotifyUtils.hxx" +#include "playlist_plugin.h" +#include "playlist_list.h" +#include "conf.h" +#include "uri.h" +#include "tag.h" +#include "song.h" +#include "input_stream.h" + +extern "C" { +#include +} + +#include + +#include +#include +#include + +struct despotify_playlist { + struct playlist_provider base; + + struct despotify_session *session; + GSList *list; +}; + +static void +add_song(struct despotify_playlist *ctx, struct ds_track *track) +{ + const char *dsp_scheme = despotify_playlist_plugin.schemes[0]; + struct song *song; + char uri[128]; + char *ds_uri; + + /* Create a spt://... URI for MPD */ + g_snprintf(uri, sizeof(uri), "%s://", dsp_scheme); + ds_uri = uri + strlen(dsp_scheme) + 3; + + if (despotify_track_to_uri(track, ds_uri) != ds_uri) { + /* Should never really fail, but let's be sure */ + g_debug("Can't add track %s\n", track->title); + return; + } + + song = song_remote_new(uri); + song->tag = mpd_despotify_tag_from_track(track); + + ctx->list = g_slist_prepend(ctx->list, song); +} + +static bool +parse_track(struct despotify_playlist *ctx, + struct ds_link *link) +{ + struct ds_track *track; + + track = despotify_link_get_track(ctx->session, link); + if (!track) + return false; + add_song(ctx, track); + + return true; +} + +static bool +parse_playlist(struct despotify_playlist *ctx, + struct ds_link *link) +{ + struct ds_playlist *playlist; + struct ds_track *track; + + playlist = despotify_link_get_playlist(ctx->session, link); + if (!playlist) + return false; + + for (track = playlist->tracks; track; track = track->next) + add_song(ctx, track); + + return true; +} + +static bool +despotify_playlist_init(G_GNUC_UNUSED const struct config_param *param) +{ + return true; +} + +static void +despotify_playlist_finish(void) +{ +} + + +static struct playlist_provider * +despotify_playlist_open_uri(const char *url, G_GNUC_UNUSED GMutex *mutex, + G_GNUC_UNUSED GCond *cond) +{ + struct despotify_playlist *ctx; + struct despotify_session *session; + struct ds_link *link; + bool parse_result; + + session = mpd_despotify_get_session(); + if (!session) + goto clean_none; + + /* Get link without spt:// */ + link = despotify_link_from_uri(url + strlen(despotify_playlist_plugin.schemes[0]) + 3); + if (!link) { + g_debug("Can't find %s\n", url); + goto clean_none; + } + + ctx = g_new(struct despotify_playlist, 1); + + ctx->list = nullptr; + ctx->session = session; + playlist_provider_init(&ctx->base, &despotify_playlist_plugin); + + switch (link->type) + { + case LINK_TYPE_TRACK: + parse_result = parse_track(ctx, link); + break; + case LINK_TYPE_PLAYLIST: + parse_result = parse_playlist(ctx, link); + break; + default: + parse_result = false; + break; + } + despotify_free_link(link); + if (!parse_result) + goto clean_playlist; + + ctx->list = g_slist_reverse(ctx->list); + + return &ctx->base; + +clean_playlist: + g_slist_free(ctx->list); +clean_none: + + return nullptr; +} + +static void +track_free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data) +{ + struct song *song = (struct song *)data; + + song_free(song); +} + +static void +despotify_playlist_close(struct playlist_provider *_playlist) +{ + struct despotify_playlist *ctx = (struct despotify_playlist *)_playlist; + + g_slist_foreach(ctx->list, track_free_callback, nullptr); + g_slist_free(ctx->list); + + g_free(ctx); +} + + +static struct song * +despotify_playlist_read(struct playlist_provider *_playlist) +{ + struct despotify_playlist *ctx = (struct despotify_playlist *)_playlist; + + if (!ctx->list) + return nullptr; + + /* Remove the current track */ + song *out = (song *)ctx->list->data; + ctx->list = g_slist_remove(ctx->list, out); + + return out; +} + + +static const char *const despotify_schemes[] = { + "spt", + nullptr +}; + +const struct playlist_plugin despotify_playlist_plugin = { + "despotify", + + despotify_playlist_init, + despotify_playlist_finish, + + despotify_playlist_open_uri, + nullptr, + despotify_playlist_close, + despotify_playlist_read, + + despotify_schemes, + nullptr, + nullptr, +}; diff --git a/src/playlist/DespotifyPlaylistPlugin.hxx b/src/playlist/DespotifyPlaylistPlugin.hxx new file mode 100644 index 000000000..c1e5b7f39 --- /dev/null +++ b/src/playlist/DespotifyPlaylistPlugin.hxx @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2011-2013 The Music Player Daemon Project + * 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. + */ + +#ifndef MPD_PLAYLIST_DESPOTIFY_PLAYLIST_PLUGIN_HXX +#define MPD_PLAYLIST_DESPOTIFY_PLAYLIST_PLUGIN_HXX + +extern const struct playlist_plugin despotify_playlist_plugin; + +#endif diff --git a/src/playlist/despotify_playlist_plugin.c b/src/playlist/despotify_playlist_plugin.c deleted file mode 100644 index 30b852c73..000000000 --- a/src/playlist/despotify_playlist_plugin.c +++ /dev/null @@ -1,217 +0,0 @@ -/* - * Copyright (C) 2011 The Music Player Daemon Project - * 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" -#include "playlist/despotify_playlist_plugin.h" -#include "playlist_plugin.h" -#include "playlist_list.h" -#include "conf.h" -#include "uri.h" -#include "tag.h" -#include "song.h" -#include "input_stream.h" -#include "despotify_utils.h" - -#include - -#include -#include -#include -#include - -struct despotify_playlist { - struct playlist_provider base; - - struct despotify_session *session; - GSList *list; -}; - -static void -add_song(struct despotify_playlist *ctx, struct ds_track *track) -{ - const char *dsp_scheme = despotify_playlist_plugin.schemes[0]; - struct song *song; - char uri[128]; - char *ds_uri; - - /* Create a spt://... URI for MPD */ - g_snprintf(uri, sizeof(uri), "%s://", dsp_scheme); - ds_uri = uri + strlen(dsp_scheme) + 3; - - if (despotify_track_to_uri(track, ds_uri) != ds_uri) { - /* Should never really fail, but let's be sure */ - g_debug("Can't add track %s\n", track->title); - return; - } - - song = song_remote_new(uri); - song->tag = mpd_despotify_tag_from_track(track); - - ctx->list = g_slist_prepend(ctx->list, song); -} - -static bool -parse_track(struct despotify_playlist *ctx, - struct ds_link *link) -{ - struct ds_track *track; - - track = despotify_link_get_track(ctx->session, link); - if (!track) - return false; - add_song(ctx, track); - - return true; -} - -static bool -parse_playlist(struct despotify_playlist *ctx, - struct ds_link *link) -{ - struct ds_playlist *playlist; - struct ds_track *track; - - playlist = despotify_link_get_playlist(ctx->session, link); - if (!playlist) - return false; - - for (track = playlist->tracks; track; track = track->next) - add_song(ctx, track); - - return true; -} - -static bool -despotify_playlist_init(G_GNUC_UNUSED const struct config_param *param) -{ - return true; -} - -static void -despotify_playlist_finish(void) -{ -} - - -static struct playlist_provider * -despotify_playlist_open_uri(const char *url, G_GNUC_UNUSED GMutex *mutex, - G_GNUC_UNUSED GCond *cond) -{ - struct despotify_playlist *ctx; - struct despotify_session *session; - struct ds_link *link; - bool parse_result; - - session = mpd_despotify_get_session(); - if (!session) - goto clean_none; - - /* Get link without spt:// */ - link = despotify_link_from_uri(url + strlen(despotify_playlist_plugin.schemes[0]) + 3); - if (!link) { - g_debug("Can't find %s\n", url); - goto clean_none; - } - - ctx = g_new(struct despotify_playlist, 1); - - ctx->list = NULL; - ctx->session = session; - playlist_provider_init(&ctx->base, &despotify_playlist_plugin); - - switch (link->type) - { - case LINK_TYPE_TRACK: - parse_result = parse_track(ctx, link); - break; - case LINK_TYPE_PLAYLIST: - parse_result = parse_playlist(ctx, link); - break; - default: - parse_result = false; - break; - } - despotify_free_link(link); - if (!parse_result) - goto clean_playlist; - - ctx->list = g_slist_reverse(ctx->list); - - return &ctx->base; - -clean_playlist: - g_slist_free(ctx->list); -clean_none: - - return NULL; -} - -static void -track_free_callback(gpointer data, G_GNUC_UNUSED gpointer user_data) -{ - struct song *song = (struct song *)data; - - song_free(song); -} - -static void -despotify_playlist_close(struct playlist_provider *_playlist) -{ - struct despotify_playlist *ctx = (struct despotify_playlist *)_playlist; - - g_slist_foreach(ctx->list, track_free_callback, NULL); - g_slist_free(ctx->list); - - g_free(ctx); -} - - -static struct song * -despotify_playlist_read(struct playlist_provider *_playlist) -{ - struct despotify_playlist *ctx = (struct despotify_playlist *)_playlist; - struct song *out; - - if (!ctx->list) - return NULL; - - /* Remove the current track */ - out = ctx->list->data; - ctx->list = g_slist_remove(ctx->list, out); - - return out; -} - - -static const char *const despotify_schemes[] = { - "spt", - NULL -}; - -const struct playlist_plugin despotify_playlist_plugin = { - .name = "despotify", - - .init = despotify_playlist_init, - .finish = despotify_playlist_finish, - .open_uri = despotify_playlist_open_uri, - .read = despotify_playlist_read, - .close = despotify_playlist_close, - - .schemes = despotify_schemes, -}; diff --git a/src/playlist/despotify_playlist_plugin.h b/src/playlist/despotify_playlist_plugin.h deleted file mode 100644 index f8ee20de0..000000000 --- a/src/playlist/despotify_playlist_plugin.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) 2011 The Music Player Daemon Project - * 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. - */ - -#ifndef MPD_PLAYLIST_DESPOTIFY_PLAYLIST_PLUGIN_H -#define MPD_PLAYLIST_DESPOTIFY_PLAYLIST_PLUGIN_H - -extern const struct playlist_plugin despotify_playlist_plugin; - -#endif diff --git a/src/playlist_list.c b/src/playlist_list.c index 68d24fe49..9ba78f38c 100644 --- a/src/playlist_list.c +++ b/src/playlist_list.c @@ -24,7 +24,7 @@ #include "playlist/m3u_playlist_plugin.h" #include "playlist/xspf_playlist_plugin.h" #include "playlist/lastfm_playlist_plugin.h" -#include "playlist/despotify_playlist_plugin.h" +#include "playlist/DespotifyPlaylistPlugin.hxx" #include "playlist/soundcloud_playlist_plugin.h" #include "playlist/pls_playlist_plugin.h" #include "playlist/asx_playlist_plugin.h" -- cgit v1.2.3