EmbeddedCuePlaylistPlugin.cxx 3.98 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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.
 */

/** \file
 *
 * Playlist plugin that reads embedded cue sheets from the "CUESHEET"
 * tag of a music file.
 */

26
#include "EmbeddedCuePlaylistPlugin.hxx"
27 28
#include "../PlaylistPlugin.hxx"
#include "../SongEnumerator.hxx"
29
#include "../cue/CueParser.hxx"
30
#include "tag/Handler.hxx"
31
#include "tag/Generic.hxx"
32
#include "song/DetachedSong.hxx"
33
#include "TagFile.hxx"
34
#include "fs/Traits.hxx"
35
#include "fs/AllocatedPath.hxx"
36
#include "util/StringView.hxx"
37

38 39
#include <memory>

40 41
#include <string.h>

42 43
class EmbeddedCuePlaylist final : public SongEnumerator {
public:
44 45 46 47 48
	/**
	 * This is an override for the CUE's "FILE".  An embedded CUE
	 * sheet must always point to the song file it is contained
	 * in.
	 */
Max Kellermann's avatar
Max Kellermann committed
49
	std::string filename;
50

51 52 53
	/**
	 * The value of the file's "CUESHEET" tag.
	 */
54
	std::string cuesheet;
55 56 57 58 59 60

	/**
	 * The offset of the next line within "cuesheet".
	 */
	char *next;

61
	std::unique_ptr<CueParser> parser;
62

63
	std::unique_ptr<DetachedSong> NextSong() override;
64 65
};

66 67 68
class ExtractCuesheetTagHandler final : public NullTagHandler {
public:
	std::string cuesheet;
69

70
	ExtractCuesheetTagHandler() noexcept:NullTagHandler(WANT_PAIR) {}
71

72
	void OnPair(StringView key, StringView value) noexcept override;
73 74
};

75
void
76
ExtractCuesheetTagHandler::OnPair(StringView name, StringView value) noexcept
77
{
78 79
	if (cuesheet.empty() && name.EqualsIgnoreCase("cuesheet"))
		cuesheet = {value.data, value.size};
80 81
}

82
static std::unique_ptr<SongEnumerator>
83
embcue_playlist_open_uri(const char *uri,
Rosen Penev's avatar
Rosen Penev committed
84
			 [[maybe_unused]] Mutex &mutex)
85
{
86
	if (!PathTraitsUTF8::IsAbsolute(uri))
87
		/* only local files supported */
88
		return nullptr;
89

90
	const auto path_fs = AllocatedPath::FromUTF8Throw(uri);
91

92
	ExtractCuesheetTagHandler extract_cuesheet;
93
	ScanFileTagsNoGeneric(path_fs, extract_cuesheet);
94 95
	if (extract_cuesheet.cuesheet.empty())
		ScanGenericTags(path_fs, extract_cuesheet);
96

97
	if (extract_cuesheet.cuesheet.empty())
98
		/* no "CUESHEET" tag found */
99
		return nullptr;
100

101 102
	auto playlist = std::make_unique<EmbeddedCuePlaylist>();

103
	playlist->filename = PathTraitsUTF8::GetBase(uri);
104

105 106
	playlist->cuesheet = std::move(extract_cuesheet.cuesheet);

107
	playlist->next = &playlist->cuesheet[0];
108
	playlist->parser = std::make_unique<CueParser>();
109

110
	return playlist;
111 112
}

113
std::unique_ptr<DetachedSong>
114
EmbeddedCuePlaylist::NextSong()
115
{
116
	auto song = parser->Get();
xent's avatar
xent committed
117 118
	if (song != nullptr) {
		song->SetURI(filename);
119
		return song;
xent's avatar
xent committed
120
	}
121

122 123 124
	while (*next != 0) {
		const char *line = next;
		char *eol = strpbrk(next, "\r\n");
125
		if (eol != nullptr) {
126 127
			/* null-terminate the line */
			*eol = 0;
128
			next = eol + 1;
129 130 131
		} else
			/* last line; put the "next" pointer to the
			   end of the buffer */
132
			next += strlen(line);
133

134 135
		parser->Feed(line);
		song = parser->Get();
136 137
		if (song != nullptr) {
			song->SetURI(filename);
138
			return song;
139
		}
140 141
	}

142 143
	parser->Finish();
	song = parser->Get();
144
	if (song != nullptr)
145
		song->SetURI(filename);
146
	return song;
147 148 149 150 151 152 153 154 155 156 157
}

static const char *const embcue_playlist_suffixes[] = {
	/* a few codecs that are known to be supported; there are
	   probably many more */
	"flac",
	"mp3", "mp2",
	"mp4", "mp4a", "m4b",
	"ape",
	"wv",
	"ogg", "oga",
158
	nullptr
159 160
};

161 162 163
const PlaylistPlugin embcue_playlist_plugin =
	PlaylistPlugin("embcue", embcue_playlist_open_uri)
	.WithSuffixes(embcue_playlist_suffixes);