Handler.hxx 5.25 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 14 15 16 17 18 19
 * 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.
 */

20 21
#ifndef MPD_TAG_HANDLER_HXX
#define MPD_TAG_HANDLER_HXX
22

23
#include "Type.h"
24
#include "Chrono.hxx"
25
#include "util/Compiler.h"
26

27
template<typename T> struct ConstBuffer;
28
struct StringView;
29
struct AudioFormat;
30
class TagBuilder;
31 32

/**
33
 * An interface for receiving metadata of a song.
34
 */
35 36 37 38 39 40 41
class TagHandler {
	const unsigned want_mask;

public:
	static constexpr unsigned WANT_DURATION = 0x1;
	static constexpr unsigned WANT_TAG = 0x2;
	static constexpr unsigned WANT_PAIR = 0x4;
42
	static constexpr unsigned WANT_AUDIO_FORMAT = 0x8;
43
	static constexpr unsigned WANT_PICTURE = 0x10;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

	explicit TagHandler(unsigned _want_mask) noexcept
		:want_mask(_want_mask) {}

	TagHandler(const TagHandler &) = delete;
	TagHandler &operator=(const TagHandler &) = delete;

	bool WantDuration() const noexcept {
		return want_mask & WANT_DURATION;
	}

	bool WantTag() const noexcept {
		return want_mask & WANT_TAG;
	}

	bool WantPair() const noexcept {
		return want_mask & WANT_PAIR;
	}

63 64 65 66
	bool WantAudioFormat() const noexcept {
		return want_mask & WANT_AUDIO_FORMAT;
	}

67 68 69 70
	bool WantPicture() const noexcept {
		return want_mask & WANT_PICTURE;
	}

71
	/**
72
	 * Declare the duration of a song.  Do not call
73 74 75
	 * this when the duration could not be determined, because
	 * there is no magic value for "unknown duration".
	 */
76
	virtual void OnDuration(SongTime duration) noexcept = 0;
77 78 79 80 81 82 83

	/**
	 * A tag has been read.
	 *
	 * @param the value of the tag; the pointer will become
	 * invalid after returning
	 */
84
	virtual void OnTag(TagType type, StringView value) noexcept = 0;
85 86 87 88 89

	/**
	 * A name-value pair has been read.  It is the codec specific
	 * representation of tags.
	 */
90
	virtual void OnPair(StringView key, StringView value) noexcept = 0;
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

	/**
	 * Declare the audio format of a song.
	 *
	 * Because the #AudioFormat type is limited to formats
	 * supported by MPD, the value passed to this method may be an
	 * approximation (should be the one passed to
	 * DecoderClient::Ready()).  For example, some codecs such as
	 * MP3 are bit depth agnostic, so the decoder plugin chooses a
	 * bit depth depending on what the codec library emits.
	 *
	 * This method is only called by those decoder plugins which
	 * implement it.  Some may not have any code for calling it,
	 * and others may decide that determining the audio format is
	 * too expensive.
	 */
	virtual void OnAudioFormat(AudioFormat af) noexcept = 0;
108 109 110 111 112 113 114 115 116 117 118 119

	/**
	 * A picture has been read.
	 *
	 * This method will only be called if #WANT_PICTURE was enabled.
	 *
	 * @param mime_type an optional MIME type string
	 * @param buffer the picture file contents; the buffer will be
	 * invalidated after this method returns
	 */
	virtual void OnPicture(const char *mime_type,
			       ConstBuffer<void> buffer) noexcept = 0;
120 121
};

122 123 124 125 126
class NullTagHandler : public TagHandler {
public:
	explicit NullTagHandler(unsigned _want_mask) noexcept
		:TagHandler(_want_mask) {}

Rosen Penev's avatar
Rosen Penev committed
127
	void OnDuration([[maybe_unused]] SongTime duration) noexcept override {}
128 129
	void OnTag(TagType type, StringView value) noexcept override;
	void OnPair(StringView key, StringView value) noexcept override;
130
	void OnAudioFormat(AudioFormat af) noexcept override;
131 132
	void OnPicture(const char *mime_type,
		       ConstBuffer<void> buffer) noexcept override;
133
};
134

135
/**
136 137
 * This #TagHandler implementation adds tag values to a #TagBuilder
 * object.
138
 */
139 140 141 142 143
class AddTagHandler : public NullTagHandler {
protected:
	TagBuilder &tag;

	AddTagHandler(unsigned _want_mask, TagBuilder &_builder) noexcept
144 145
		:NullTagHandler(WANT_DURATION|WANT_TAG|_want_mask),
		 tag(_builder) {}
146 147 148

public:
	explicit AddTagHandler(TagBuilder &_builder) noexcept
149
		:AddTagHandler(0, _builder) {}
150 151

	void OnDuration(SongTime duration) noexcept override;
152
	void OnTag(TagType type, StringView value) noexcept override;
153
};
154

155
/**
156
 * This #TagHandler implementation adds tag values to a #TagBuilder object
157 158 159
 * (casted from the context pointer), and supports the has_playlist
 * attribute.
 */
160
class FullTagHandler : public AddTagHandler {
161 162
	AudioFormat *const audio_format;

163
protected:
164 165 166 167 168 169
	FullTagHandler(unsigned _want_mask, TagBuilder &_builder,
		       AudioFormat *_audio_format) noexcept
		:AddTagHandler(WANT_PAIR|_want_mask
			       |(_audio_format ? WANT_AUDIO_FORMAT : 0),
			       _builder),
		 audio_format(_audio_format) {}
170

171
public:
172 173 174
	explicit FullTagHandler(TagBuilder &_builder,
				AudioFormat *_audio_format=nullptr) noexcept
		:FullTagHandler(0, _builder, _audio_format) {}
175

176
	void OnPair(StringView key, StringView value) noexcept override;
177
	void OnAudioFormat(AudioFormat af) noexcept override;
178
};
179

180
#endif