DecoderPlugin.hxx 7.02 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20 21
#ifndef MPD_DECODER_PLUGIN_HXX
#define MPD_DECODER_PLUGIN_HXX
22

23
#include <forward_list>  // IWYU pragma: export
24 25
#include <set>
#include <string>
26
#include <string_view>
27

28
struct ConfigBlock;
29
class InputStream;
30
class TagHandler;
31
class Path;
32
class DecoderClient;
33
class DetachedSong;
34

35
struct DecoderPlugin {
36 37 38
	const char *name;

	/**
39 40
	 * Initialize the decoder plugin.  Optional method.
	 *
Max Kellermann's avatar
Max Kellermann committed
41
	 * @param param a configuration block for this plugin, or nullptr
42 43 44
	 * if none is configured
	 * @return true if the plugin was initialized successfully,
	 * false if the plugin is not available
45
	 */
46
	bool (*init)(const ConfigBlock &block) = nullptr;
47 48

	/**
49 50
	 * Deinitialize a decoder plugin which was initialized
	 * successfully.  Optional method.
51
	 */
52
	void (*finish)() noexcept = nullptr;
53

54 55 56 57 58 59 60 61 62 63
	/**
	 * Return a set of supported protocols.
	 */
	std::set<std::string> (*protocols)() noexcept = nullptr;

	/**
	 * Decode an URI with a protocol listed in protocols().
	 */
	void (*uri_decode)(DecoderClient &client, const char *uri) = nullptr;

64
	/**
65
	 * Decode a stream (data read from an #InputStream object).
66
	 *
67 68 69
	 * Either implement this method or file_decode().  If
	 * possible, it is recommended to implement this method,
	 * because it is more versatile.
70
	 */
71
	void (*stream_decode)(DecoderClient &client, InputStream &is) = nullptr;
72 73

	/**
74
	 * Decode a local file.
75
	 *
76
	 * Either implement this method or stream_decode().
77
	 */
78
	void (*file_decode)(DecoderClient &client, Path path_fs) = nullptr;
79 80

	/**
81 82 83
         * Scan metadata of a file.
         *
	 * Throws on I/O error.
84
	 *
85
	 * @return false if the file was not recognized
86
	 */
87
	bool (*scan_file)(Path path_fs, TagHandler &handler) = nullptr;
88

89
	/**
90 91 92
	 * Scan metadata of a stream.
         *
	 * Throws on I/O error.
93
	 *
94
	 * @return false if the stream was not recognized
95
	 */
96
	bool (*scan_stream)(InputStream &is, TagHandler &handler) = nullptr;
97

98 99 100
	/**
	 * @brief Return a "virtual" filename for subtracks in
	 * container formats like flac
101
	 * @param path_fs full pathname for the file on fs
102
	 *
103 104
	 * @return an empty list if there are no multiple files
	 * a filename for every single track;
105 106
	 * do not include full pathname here, just the "virtual" file
	 */
107
	std::forward_list<DetachedSong> (*container_scan)(Path path_fs) = nullptr;
108

Max Kellermann's avatar
Max Kellermann committed
109
	/* last element in these arrays must always be a nullptr: */
110 111 112 113 114 115 116
	const char *const*suffixes = nullptr;
	const char *const*mime_types = nullptr;

	constexpr DecoderPlugin(const char *_name,
				void (*_file_decode)(DecoderClient &client,
						     Path path_fs),
				bool (*_scan_file)(Path path_fs,
117
						   TagHandler &handler)) noexcept
118 119 120 121 122 123
		:name(_name),
		 file_decode(_file_decode), scan_file(_scan_file) {}

	constexpr DecoderPlugin(const char *_name,
				void (*_stream_decode)(DecoderClient &client,
						       InputStream &is),
124
				bool (*_scan_stream)(InputStream &is, TagHandler &handler)) noexcept
125 126 127 128 129 130 131
		:name(_name),
		 stream_decode(_stream_decode),
		 scan_stream(_scan_stream) {}

	constexpr DecoderPlugin(const char *_name,
				void (*_stream_decode)(DecoderClient &client,
						       InputStream &is),
132
				bool (*_scan_stream)(InputStream &is, TagHandler &handler),
133 134 135
				void (*_file_decode)(DecoderClient &client,
						     Path path_fs),
				bool (*_scan_file)(Path path_fs,
136
						   TagHandler &handler)) noexcept
137 138 139 140 141 142 143
		:name(_name),
		 stream_decode(_stream_decode),
		 file_decode(_file_decode),
		 scan_file(_scan_file),
		 scan_stream(_scan_stream) {}

	constexpr auto WithInit(bool (*_init)(const ConfigBlock &block),
144
				void (*_finish)() noexcept = nullptr) const noexcept {
145 146 147 148 149 150
		auto copy = *this;
		copy.init = _init;
		copy.finish = _finish;
		return copy;
	}

151
	constexpr auto WithContainer(std::forward_list<DetachedSong> (*_container_scan)(Path path_fs)) const noexcept {
152 153 154 155 156
		auto copy = *this;
		copy.container_scan = _container_scan;
		return copy;
	}

157
	constexpr auto WithProtocols(std::set<std::string> (*_protocols)() noexcept,
158
				     void (*_uri_decode)(DecoderClient &client, const char *uri)) const noexcept {
159 160 161 162 163 164
		auto copy = *this;
		copy.protocols = _protocols;
		copy.uri_decode = _uri_decode;
		return copy;
	}

165
	constexpr auto WithSuffixes(const char *const*_suffixes) const noexcept {
166 167 168 169 170
		auto copy = *this;
		copy.suffixes = _suffixes;
		return copy;
	}

171
	constexpr auto WithMimeTypes(const char *const*_mime_types) const noexcept {
172 173 174 175
		auto copy = *this;
		copy.mime_types = _mime_types;
		return copy;
	}
176

177 178 179
	/**
	 * Initialize a decoder plugin.
	 *
Max Kellermann's avatar
Max Kellermann committed
180
	 * @param block a configuration block for this plugin
181 182 183
	 * @return true if the plugin was initialized successfully, false if
	 * the plugin is not available
	 */
184
	bool Init(const ConfigBlock &block) const {
185
		return init != nullptr
186
			? init(block)
187 188
			: true;
	}
189

190 191 192 193 194 195 196
	/**
	 * Deinitialize a decoder plugin which was initialized successfully.
	 */
	void Finish() const {
		if (finish != nullptr)
			finish();
	}
197

198 199 200
	/**
	 * Decode a stream.
	 */
201 202
	void StreamDecode(DecoderClient &client, InputStream &is) const {
		stream_decode(client, is);
203
	}
204

205 206 207 208 209 210 211
	/**
	 * Decode an URI which is supported (check SupportsUri()).
	 */
	void UriDecode(DecoderClient &client, const char *uri) const {
		uri_decode(client, uri);
	}

212 213 214
	/**
	 * Decode a file.
	 */
215
	template<typename P>
216 217
	void FileDecode(DecoderClient &client, P path_fs) const {
		file_decode(client, path_fs);
218
	}
219

220 221 222
	/**
	 * Read the tag of a file.
	 */
223
	template<typename P>
224
	bool ScanFile(P path_fs, TagHandler &handler) const {
225
		return scan_file != nullptr
226
			? scan_file(path_fs, handler)
227 228
			: false;
	}
229

230 231 232
	/**
	 * Read the tag of a stream.
	 */
233
	bool ScanStream(InputStream &is, TagHandler &handler) const {
234
		return scan_stream != nullptr
235
			? scan_stream(is, handler)
236 237
			: false;
	}
238

239 240 241
	/**
	 * return "virtual" tracks in a container
	 */
242 243
	template<typename P>
	char *ContainerScan(P path, const unsigned int tnum) const {
244 245
		return container_scan(path, tnum);
	}
246

247
	[[gnu::pure]]
248 249
	bool SupportsUri(const char *uri) const noexcept;

250 251 252
	/**
	 * Does the plugin announce the specified file name suffix?
	 */
253
	[[gnu::pure]]
254
	bool SupportsSuffix(std::string_view suffix) const noexcept;
255

256 257 258
	/**
	 * Does the plugin announce the specified MIME type?
	 */
259
	[[gnu::pure]]
260
	bool SupportsMimeType(std::string_view mime_type) const noexcept;
261

262
	bool SupportsContainerSuffix(std::string_view suffix) const noexcept {
263 264
		return container_scan != nullptr && SupportsSuffix(suffix);
	}
265
};
266

267
#endif