playlist_plugin: new plugin API for playlist parsers

Based on this API, we will add parsers for EXTM3U, PLS, ASX, last.fm
radio and others.

There is no integration into the MPD core yet.  Right now, we have a
command line test program.  This is work in progress.
This commit is contained in:
Max Kellermann 2009-10-12 22:34:04 +02:00
parent dbb1e732b8
commit 2bf740fc71
8 changed files with 599 additions and 0 deletions

1
.gitignore vendored
View File

@ -51,3 +51,4 @@ test/run_output
test/read_conf
test/run_input
test/read_mixer
test/dump_playlist

View File

@ -136,6 +136,9 @@ mpd_headers = \
src/playlist_print.h \
src/playlist_save.h \
src/playlist_state.h \
src/playlist_plugin.h \
src/playlist_list.h \
src/playlist/m3u_playlist_plugin.h \
src/poison.h \
src/riff.h \
src/aiff.h \
@ -180,6 +183,7 @@ src_mpd_SOURCES = \
$(mpd_headers) \
$(ARCHIVE_SRC) \
$(INPUT_SRC) \
$(PLAYLIST_SRC) \
$(TAG_SRC) \
$(DECODER_SRC) \
$(ENCODER_SRC) \
@ -640,6 +644,15 @@ OUTPUT_SRC += src/output/solaris_output_plugin.c
endif
#
# Playlist plugins
#
PLAYLIST_SRC = \
src/playlist/m3u_playlist_plugin.c \
src/playlist_list.c
#
# Filter plugins
#
@ -680,6 +693,7 @@ if ENABLE_TEST
noinst_PROGRAMS = \
test/read_conf \
test/run_input \
test/dump_playlist \
test/run_decoder \
test/read_tags \
test/run_filter \
@ -707,6 +721,22 @@ test_run_input_SOURCES = test/run_input.c \
$(ARCHIVE_SRC) \
$(INPUT_SRC)
test_dump_playlist_CPPFLAGS = $(AM_CPPFLAGS) \
$(ARCHIVE_CFLAGS) \
$(INPUT_CFLAGS)
test_dump_playlist_LDADD = $(MPD_LIBS) \
$(ARCHIVE_LIBS) \
$(INPUT_LIBS) \
$(GLIB_LIBS)
test_dump_playlist_SOURCES = test/dump_playlist.c \
src/conf.c src/tokenizer.c src/utils.c \
src/uri.c \
src/song.c src/tag.c src/tag_pool.c src/tag_save.c \
src/text_input_stream.c src/fifo_buffer.c \
$(ARCHIVE_SRC) \
$(INPUT_SRC) \
$(PLAYLIST_SRC)
test_run_decoder_CPPFLAGS = $(AM_CPPFLAGS) \
$(TAG_CFLAGS) \
$(ARCHIVE_CFLAGS) \

View File

@ -0,0 +1,89 @@
/*
* Copyright (C) 2003-2009 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 "playlist/m3u_playlist_plugin.h"
#include "playlist_plugin.h"
#include "text_input_stream.h"
#include "uri.h"
#include "song.h"
#include <glib.h>
struct m3u_playlist {
struct playlist_provider base;
struct text_input_stream *tis;
};
static struct playlist_provider *
m3u_open_stream(struct input_stream *is)
{
struct m3u_playlist *playlist = g_new(struct m3u_playlist, 1);
playlist_provider_init(&playlist->base, &m3u_playlist_plugin);
playlist->tis = text_input_stream_new(is);
return &playlist->base;
}
static void
m3u_close(struct playlist_provider *_playlist)
{
struct m3u_playlist *playlist = (struct m3u_playlist *)_playlist;
text_input_stream_free(playlist->tis);
g_free(playlist);
}
static struct song *
m3u_read(struct playlist_provider *_playlist)
{
struct m3u_playlist *playlist = (struct m3u_playlist *)_playlist;
const char *line;
do {
line = text_input_stream_read(playlist->tis);
if (line == NULL)
return NULL;
while (*line != 0 && g_ascii_isspace(*line))
++line;
} while (line[0] == '#' || !uri_has_scheme(line));
return song_remote_new(line);
}
static const char *const m3u_suffixes[] = {
"m3u",
NULL
};
static const char *const m3u_mime_types[] = {
"audio/x-mpegurl",
NULL
};
const struct playlist_plugin m3u_playlist_plugin = {
.open_stream = m3u_open_stream,
.close = m3u_close,
.read = m3u_read,
.suffixes = m3u_suffixes,
.mime_types = m3u_mime_types,
};

View File

@ -0,0 +1,25 @@
/*
* Copyright (C) 2003-2009 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_M3U_PLAYLIST_PLUGIN_H
#define MPD_PLAYLIST_M3U_PLAYLIST_PLUGIN_H
extern const struct playlist_plugin m3u_playlist_plugin;
#endif

146
src/playlist_list.c Normal file
View File

@ -0,0 +1,146 @@
/*
* Copyright (C) 2003-2009 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 "playlist_list.h"
#include "playlist_plugin.h"
#include "playlist/m3u_playlist_plugin.h"
#include "input_stream.h"
#include "uri.h"
#include "utils.h"
#include <glib.h>
#include <assert.h>
static const struct playlist_plugin *const playlist_plugins[] = {
&m3u_playlist_plugin,
NULL
};
/** which plugins have been initialized successfully? */
static bool playlist_plugins_enabled[G_N_ELEMENTS(playlist_plugins)];
void
playlist_list_global_init(void)
{
for (unsigned i = 0; playlist_plugins[i] != NULL; ++i)
playlist_plugins_enabled[i] =
playlist_plugin_init(playlist_plugins[i], NULL);
}
void
playlist_list_global_finish(void)
{
for (unsigned i = 0; playlist_plugins[i] != NULL; ++i)
if (playlist_plugins_enabled[i])
playlist_plugin_finish(playlist_plugins[i]);
}
struct playlist_provider *
playlist_list_open_uri(const char *uri)
{
char *scheme;
struct playlist_provider *playlist;
assert(uri != NULL);
scheme = g_uri_parse_scheme(uri);
if (scheme == NULL)
return NULL;
for (unsigned i = 0; playlist_plugins[i] != NULL; ++i) {
const struct playlist_plugin *plugin = playlist_plugins[i];
if (playlist_plugins_enabled[i] &&
stringFoundInStringArray(plugin->schemes, scheme)) {
playlist = playlist_plugin_open_uri(plugin, uri);
if (playlist != NULL)
break;
}
}
g_free(scheme);
return playlist;
}
static struct playlist_provider *
playlist_list_open_stream_mime(struct input_stream *is)
{
struct playlist_provider *playlist;
assert(is != NULL);
assert(is->mime != NULL);
for (unsigned i = 0; playlist_plugins[i] != NULL; ++i) {
const struct playlist_plugin *plugin = playlist_plugins[i];
if (playlist_plugins_enabled[i] &&
stringFoundInStringArray(plugin->mime_types, is->mime)) {
playlist = playlist_plugin_open_stream(plugin, is);
if (playlist != NULL)
return playlist;
}
}
return NULL;
}
static struct playlist_provider *
playlist_list_open_stream_suffix(struct input_stream *is, const char *suffix)
{
struct playlist_provider *playlist;
assert(is != NULL);
assert(suffix != NULL);
for (unsigned i = 0; playlist_plugins[i] != NULL; ++i) {
const struct playlist_plugin *plugin = playlist_plugins[i];
if (playlist_plugins_enabled[i] &&
stringFoundInStringArray(plugin->suffixes, suffix)) {
playlist = playlist_plugin_open_stream(plugin, is);
if (playlist != NULL)
return playlist;
}
}
return NULL;
}
struct playlist_provider *
playlist_list_open_stream(struct input_stream *is, const char *uri)
{
const char *suffix;
struct playlist_provider *playlist;
if (is->mime != NULL) {
playlist = playlist_list_open_stream_mime(is);
if (playlist != NULL)
return playlist;
}
suffix = uri != NULL ? uri_get_suffix(uri) : NULL;
if (suffix != NULL) {
playlist = playlist_list_open_stream_suffix(is, suffix);
if (playlist != NULL)
return playlist;
}
return NULL;
}

54
src/playlist_list.h Normal file
View File

@ -0,0 +1,54 @@
/*
* Copyright (C) 2003-2009 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_LIST_H
#define MPD_PLAYLIST_LIST_H
struct playlist_provider;
struct input_stream;
/**
* Initializes all playlist plugins.
*/
void
playlist_list_global_init(void);
/**
* Deinitializes all playlist plugins.
*/
void
playlist_list_global_finish(void);
/**
* Opens a playlist by its URI.
*/
struct playlist_provider *
playlist_list_open_uri(const char *uri);
/**
* Opens a playlist from an input stream.
*
* @param is an #input_stream object which is open and ready
* @param uri optional URI which was used to open the stream; may be
* used to select the appropriate playlist plugin
*/
struct playlist_provider *
playlist_list_open_stream(struct input_stream *is, const char *uri);
#endif

137
src/playlist_plugin.h Normal file
View File

@ -0,0 +1,137 @@
/*
* Copyright (C) 2003-2009 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_PLUGIN_H
#define MPD_PLAYLIST_PLUGIN_H
#include <stdbool.h>
#include <stddef.h>
struct config_param;
struct input_stream;
struct tag;
/**
* An object which provides the contents of a playlist.
*/
struct playlist_provider {
const struct playlist_plugin *plugin;
};
static inline void
playlist_provider_init(struct playlist_provider *playlist,
const struct playlist_plugin *plugin)
{
playlist->plugin = plugin;
}
struct playlist_plugin {
const char *name;
/**
* Initialize the plugin. Optional method.
*
* @param param a configuration block for this plugin, or NULL
* if none is configured
* @return true if the plugin was initialized successfully,
* false if the plugin is not available
*/
bool (*init)(const struct config_param *param);
/**
* Deinitialize a plugin which was initialized successfully.
* Optional method.
*/
void (*finish)(void);
/**
* Opens the playlist on the specified URI. This URI has
* either matched one of the schemes or one of the suffixes.
*/
struct playlist_provider *(*open_uri)(const char *uri);
/**
* Opens the playlist in the specified input stream. It has
* either matched one of the suffixes or one of the MIME
* types.
*/
struct playlist_provider *(*open_stream)(struct input_stream *is);
void (*close)(struct playlist_provider *playlist);
struct song *(*read)(struct playlist_provider *playlist);
const char *const*schemes;
const char *const*suffixes;
const char *const*mime_types;
};
/**
* Initialize a plugin.
*
* @param param a configuration block for this plugin, or NULL if none
* is configured
* @return true if the plugin was initialized successfully, false if
* the plugin is not available
*/
static inline bool
playlist_plugin_init(const struct playlist_plugin *plugin,
const struct config_param *param)
{
return plugin->init != NULL
? plugin->init(param)
: true;
}
/**
* Deinitialize a plugin which was initialized successfully.
*/
static inline void
playlist_plugin_finish(const struct playlist_plugin *plugin)
{
if (plugin->finish != NULL)
plugin->finish();
}
static inline struct playlist_provider *
playlist_plugin_open_uri(const struct playlist_plugin *plugin, const char *uri)
{
return plugin->open_uri(uri);
}
static inline struct playlist_provider *
playlist_plugin_open_stream(const struct playlist_plugin *plugin,
struct input_stream *is)
{
return plugin->open_stream(is);
}
static inline void
playlist_plugin_close(struct playlist_provider *playlist)
{
playlist->plugin->close(playlist);
}
static inline struct song *
playlist_plugin_read(struct playlist_provider *playlist)
{
return playlist->plugin->read(playlist);
}
#endif

117
test/dump_playlist.c Normal file
View File

@ -0,0 +1,117 @@
/*
* Copyright (C) 2003-2009 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 "input_stream.h"
#include "tag_pool.h"
#include "tag_save.h"
#include "conf.h"
#include "song.h"
#include "playlist_list.h"
#include "playlist_plugin.h"
#include <glib.h>
#include <unistd.h>
static void
my_log_func(const gchar *log_domain, G_GNUC_UNUSED GLogLevelFlags log_level,
const gchar *message, G_GNUC_UNUSED gpointer user_data)
{
if (log_domain != NULL)
g_printerr("%s: %s\n", log_domain, message);
else
g_printerr("%s\n", message);
}
int main(int argc, char **argv)
{
const char *uri;
struct input_stream is;
bool success;
struct playlist_provider *playlist;
struct song *song;
if (argc != 2) {
g_printerr("Usage: dump_playlist URI\n");
return 1;
}
uri = argv[1];
/* initialize GLib */
g_thread_init(NULL);
g_log_set_default_handler(my_log_func, NULL);
/* initialize MPD */
tag_pool_init();
config_global_init();
input_stream_global_init();
playlist_list_global_init();
/* open the stream and wait until it becomes ready */
success = input_stream_open(&is, uri);
if (!success) {
g_printerr("input_stream_open() failed\n");
return 2;
}
while (!is.ready) {
int ret = input_stream_buffer(&is);
if (ret < 0)
/* error */
return 2;
if (ret == 0)
/* nothing was buffered - wait */
g_usleep(10000);
}
/* open the playlist */
playlist = playlist_list_open_stream(&is, uri);
if (playlist == NULL) {
input_stream_close(&is);
g_printerr("Failed to open playlist\n");
return 2;
}
/* dump the playlist */
while ((song = playlist_plugin_read(playlist)) != NULL) {
g_print("%s\n", song->url);
if (song->tag != NULL)
tag_save(stdout, song->tag);
song_free(song);
}
/* deinitialize everything */
playlist_plugin_close(playlist);
input_stream_close(&is);
playlist_list_global_finish();
input_stream_global_finish();
config_global_finish();
tag_pool_deinit();
return 0;
}