DecoderPlugin.hxx 4.53 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 24
#include "Compiler.h"

25
struct ConfigBlock;
26
class InputStream;
27
struct TagHandler;
28
class Path;
29
template<typename T> class AllocatedString;
30 31 32 33 34

/**
 * Opaque handle which the decoder plugin passes to the functions in
 * this header.
 */
35
struct Decoder;
36

37
struct DecoderPlugin {
38 39 40
	const char *name;

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

	/**
51 52
	 * Deinitialize a decoder plugin which was initialized
	 * successfully.  Optional method.
53
	 */
54
	void (*finish)();
55 56

	/**
57
	 * Decode a stream (data read from an #InputStream object).
58
	 *
59 60 61
	 * Either implement this method or file_decode().  If
	 * possible, it is recommended to implement this method,
	 * because it is more versatile.
62
	 */
63
	void (*stream_decode)(Decoder &decoder, InputStream &is);
64 65

	/**
66
	 * Decode a local file.
67
	 *
68
	 * Either implement this method or stream_decode().
69
	 */
70
	void (*file_decode)(Decoder &decoder, Path path_fs);
71 72

	/**
73
	 * Scan metadata of a file.
74
	 *
75
	 * @return false if the operation has failed
76
	 */
77
	bool (*scan_file)(Path path_fs,
78
			  const TagHandler &handler,
79
			  void *handler_ctx);
80

81
	/**
82
	 * Scan metadata of a file.
83
	 *
84
	 * @return false if the operation has failed
85
	 */
86
	bool (*scan_stream)(InputStream &is,
87
			    const TagHandler &handler,
88
			    void *handler_ctx);
89

90 91 92 93 94 95
	/**
	 * @brief Return a "virtual" filename for subtracks in
	 * container formats like flac
	 * @param const char* pathname full pathname for the file on fs
	 * @param const unsigned int tnum track number
	 *
Max Kellermann's avatar
Max Kellermann committed
96
	 * @return nullptr if there are no multiple files
97 98 99
	 * a filename for every single track according to tnum (param 2)
	 * do not include full pathname here, just the "virtual" file
	 */
100
	AllocatedString<char> (*container_scan)(Path path_fs, unsigned tnum);
101

Max Kellermann's avatar
Max Kellermann committed
102
	/* last element in these arrays must always be a nullptr: */
103 104 105
	const char *const*suffixes;
	const char *const*mime_types;

106 107 108
	/**
	 * Initialize a decoder plugin.
	 *
Max Kellermann's avatar
Max Kellermann committed
109
	 * @param block a configuration block for this plugin
110 111 112
	 * @return true if the plugin was initialized successfully, false if
	 * the plugin is not available
	 */
113
	bool Init(const ConfigBlock &block) const {
114
		return init != nullptr
115
			? init(block)
116 117
			: true;
	}
118

119 120 121 122 123 124 125
	/**
	 * Deinitialize a decoder plugin which was initialized successfully.
	 */
	void Finish() const {
		if (finish != nullptr)
			finish();
	}
126

127 128 129
	/**
	 * Decode a stream.
	 */
130 131
	void StreamDecode(Decoder &decoder, InputStream &is) const {
		stream_decode(decoder, is);
132
	}
133

134 135 136
	/**
	 * Decode a file.
	 */
137 138
	template<typename P>
	void FileDecode(Decoder &decoder, P path_fs) const {
139
		file_decode(decoder, path_fs);
140
	}
141

142 143 144
	/**
	 * Read the tag of a file.
	 */
145 146
	template<typename P>
	bool ScanFile(P path_fs,
147
		      const TagHandler &handler, void *handler_ctx) const {
148
		return scan_file != nullptr
149
			? scan_file(path_fs, handler, handler_ctx)
150 151
			: false;
	}
152

153 154 155
	/**
	 * Read the tag of a stream.
	 */
156
	bool ScanStream(InputStream &is,
157
			const TagHandler &handler, void *handler_ctx) const {
158
		return scan_stream != nullptr
159
			? scan_stream(is, handler, handler_ctx)
160 161
			: false;
	}
162

163 164 165
	/**
	 * return "virtual" tracks in a container
	 */
166 167
	template<typename P>
	char *ContainerScan(P path, const unsigned int tnum) const {
168 169
		return container_scan(path, tnum);
	}
170

171 172 173 174 175
	/**
	 * Does the plugin announce the specified file name suffix?
	 */
	gcc_pure gcc_nonnull_all
	bool SupportsSuffix(const char *suffix) const;
176

177 178 179 180 181 182
	/**
	 * Does the plugin announce the specified MIME type?
	 */
	gcc_pure gcc_nonnull_all
	bool SupportsMimeType(const char *mime_type) const;
};
183

184
#endif