decoder_list.c 4.08 KB
Newer Older
Warren Dukes's avatar
Warren Dukes committed
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
Warren Dukes's avatar
Warren Dukes committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * This project's homepage is: 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

19
#include "decoder_list.h"
20
#include "decoder_api.h"
21
#include "list.h"
22

23 24 25 26 27 28 29 30 31 32 33
extern struct decoder_plugin mp3Plugin;
extern struct decoder_plugin oggvorbisPlugin;
extern struct decoder_plugin flacPlugin;
extern struct decoder_plugin oggflacPlugin;
extern struct decoder_plugin audiofilePlugin;
extern struct decoder_plugin mp4Plugin;
extern struct decoder_plugin aacPlugin;
extern struct decoder_plugin mpcPlugin;
extern struct decoder_plugin wavpackPlugin;
extern struct decoder_plugin modPlugin;

34
static List *inputPlugin_list;
35

36
void decoder_plugin_load(struct decoder_plugin * inputPlugin)
Avuton Olrich's avatar
Avuton Olrich committed
37 38 39 40 41
{
	if (!inputPlugin)
		return;
	if (!inputPlugin->name)
		return;
Warren Dukes's avatar
Warren Dukes committed
42

43
	if (inputPlugin->init != NULL && inputPlugin->init() < 0)
Avuton Olrich's avatar
Avuton Olrich committed
44
		return;
45

46 47 48
	insertInList(inputPlugin_list, inputPlugin->name, (void *)inputPlugin);
}

49
void decoder_plugin_unload(struct decoder_plugin * inputPlugin)
Avuton Olrich's avatar
Avuton Olrich committed
50
{
51 52
	if (inputPlugin->finish != NULL)
		inputPlugin->finish();
53 54 55
	deleteFromList(inputPlugin_list, inputPlugin->name);
}

Max Kellermann's avatar
Max Kellermann committed
56
static int stringFoundInStringArray(const char *const*array, const char *suffix)
Avuton Olrich's avatar
Avuton Olrich committed
57 58 59 60
{
	while (array && *array) {
		if (strcasecmp(*array, suffix) == 0)
			return 1;
61 62
		array++;
	}
Avuton Olrich's avatar
Avuton Olrich committed
63

64 65 66
	return 0;
}

67 68
struct decoder_plugin *decoder_plugin_from_suffix(const char *suffix,
						  unsigned int next)
Avuton Olrich's avatar
Avuton Olrich committed
69
{
70
	static ListNode *pos;
Avuton Olrich's avatar
Avuton Olrich committed
71
	ListNode *node;
72
	struct decoder_plugin *plugin;
Avuton Olrich's avatar
Avuton Olrich committed
73 74 75

	if (suffix == NULL)
		return NULL;
76

77
	if (next) {
Avuton Olrich's avatar
Avuton Olrich committed
78 79 80 81
		if (pos)
			node = pos;
		else
			return NULL;
82 83
	} else
		node = inputPlugin_list->firstNode;
Warren Dukes's avatar
Warren Dukes committed
84

Avuton Olrich's avatar
Avuton Olrich committed
85
	while (node != NULL) {
86
		plugin = node->data;
Avuton Olrich's avatar
Avuton Olrich committed
87
		if (stringFoundInStringArray(plugin->suffixes, suffix)) {
88
			pos = node->nextNode;
89 90
			return plugin;
		}
Warren Dukes's avatar
Warren Dukes committed
91
		node = node->nextNode;
92 93 94 95 96
	}

	return NULL;
}

97 98
struct decoder_plugin *decoder_plugin_from_mime_type(const char *mimeType,
						     unsigned int next)
Avuton Olrich's avatar
Avuton Olrich committed
99
{
100
	static ListNode *pos;
Avuton Olrich's avatar
Avuton Olrich committed
101
	ListNode *node;
102
	struct decoder_plugin *plugin;
Avuton Olrich's avatar
Avuton Olrich committed
103 104 105

	if (mimeType == NULL)
		return NULL;
106

107
	node = (next && pos) ? pos : inputPlugin_list->firstNode;
108

Avuton Olrich's avatar
Avuton Olrich committed
109
	while (node != NULL) {
110
		plugin = node->data;
111
		if (stringFoundInStringArray(plugin->mime_types, mimeType)) {
112
			pos = node->nextNode;
113 114
			return plugin;
		}
Warren Dukes's avatar
Warren Dukes committed
115
		node = node->nextNode;
116 117 118 119 120
	}

	return NULL;
}

121
struct decoder_plugin *decoder_plugin_from_name(const char *name)
Avuton Olrich's avatar
Avuton Olrich committed
122 123
{
	void *plugin = NULL;
124 125 126

	findInList(inputPlugin_list, name, &plugin);

127
	return (struct decoder_plugin *) plugin;
128 129
}

130
void decoder_plugin_print_all_suffixes(FILE * fp)
Avuton Olrich's avatar
Avuton Olrich committed
131 132
{
	ListNode *node = inputPlugin_list->firstNode;
133
	struct decoder_plugin *plugin;
Max Kellermann's avatar
Max Kellermann committed
134
	const char *const*suffixes;
135

Avuton Olrich's avatar
Avuton Olrich committed
136
	while (node) {
137
		plugin = (struct decoder_plugin *) node->data;
138
		suffixes = plugin->suffixes;
Avuton Olrich's avatar
Avuton Olrich committed
139
		while (suffixes && *suffixes) {
140
			fprintf(fp, "%s ", *suffixes);
141 142 143 144
			suffixes++;
		}
		node = node->nextNode;
	}
145 146
	fprintf(fp, "\n");
	fflush(fp);
147 148
}

149
void decoder_plugin_init_all(void)
Avuton Olrich's avatar
Avuton Olrich committed
150
{
151
	inputPlugin_list = makeList(NULL, 1);
152 153

	/* load plugins here */
154 155 156 157 158 159 160 161 162 163
	decoder_plugin_load(&mp3Plugin);
	decoder_plugin_load(&oggvorbisPlugin);
	decoder_plugin_load(&oggflacPlugin);
	decoder_plugin_load(&flacPlugin);
	decoder_plugin_load(&audiofilePlugin);
	decoder_plugin_load(&mp4Plugin);
	decoder_plugin_load(&aacPlugin);
	decoder_plugin_load(&mpcPlugin);
	decoder_plugin_load(&wavpackPlugin);
	decoder_plugin_load(&modPlugin);
164 165
}

166
void decoder_plugin_deinit_all(void)
Avuton Olrich's avatar
Avuton Olrich committed
167
{
168 169
	freeList(inputPlugin_list);
}