EmbeddedCuePlaylistPlugin.cxx 3.92 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 26
 * 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.
 */

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

39 40
#include <string.h>

41 42
class EmbeddedCuePlaylist final : public SongEnumerator {
public:
43 44 45 46 47
	/**
	 * 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
48
	std::string filename;
49

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

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

60
	CueParser *parser;
61

62
public:
63
	EmbeddedCuePlaylist()
64
		:parser(nullptr) {
65 66
	}

67
	virtual ~EmbeddedCuePlaylist() {
68 69 70
		delete parser;
	}

71
	virtual std::unique_ptr<DetachedSong> NextSong() override;
72 73 74 75 76
};

static void
embcue_tag_pair(const char *name, const char *value, void *ctx)
{
77
	EmbeddedCuePlaylist *playlist = (EmbeddedCuePlaylist *)ctx;
78

79
	if (playlist->cuesheet.empty() &&
80
	    StringEqualsCaseASCII(name, "cuesheet"))
81
		playlist->cuesheet = value;
82 83
}

84
static constexpr TagHandler embcue_tag_handler = {
85 86 87
	nullptr,
	nullptr,
	embcue_tag_pair,
88 89
};

90
static std::unique_ptr<SongEnumerator>
91
embcue_playlist_open_uri(const char *uri,
92
			 gcc_unused Mutex &mutex)
93
{
94
	if (!PathTraitsUTF8::IsAbsolute(uri))
95
		/* only local files supported */
96
		return nullptr;
97

98
	const auto path_fs = AllocatedPath::FromUTF8Throw(uri);
99

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

102
	tag_file_scan(path_fs, embcue_tag_handler, playlist.get());
103
	if (playlist->cuesheet.empty())
104
		ScanGenericTags(path_fs, embcue_tag_handler, playlist.get());
105

106
	if (playlist->cuesheet.empty())
107
		/* no "CUESHEET" tag found */
108
		return nullptr;
109

110
	playlist->filename = PathTraitsUTF8::GetBase(uri);
111

112
	playlist->next = &playlist->cuesheet[0];
113
	playlist->parser = new CueParser();
114

115
	return playlist;
116 117
}

118
std::unique_ptr<DetachedSong>
119
EmbeddedCuePlaylist::NextSong()
120
{
121
	auto song = parser->Get();
xent's avatar
xent committed
122 123
	if (song != nullptr) {
		song->SetURI(filename);
124
		return song;
xent's avatar
xent committed
125
	}
126

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

139 140
		parser->Feed(line);
		song = parser->Get();
141 142
		if (song != nullptr) {
			song->SetURI(filename);
143
			return song;
144
		}
145 146
	}

147 148
	parser->Finish();
	song = parser->Get();
149
	if (song != nullptr)
150
		song->SetURI(filename);
151
	return song;
152 153 154 155 156 157 158 159 160 161 162
}

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",
163
	nullptr
164 165 166
};

const struct playlist_plugin embcue_playlist_plugin = {
167
	"embcue",
168 169 170 171 172 173 174

	nullptr,
	nullptr,
	embcue_playlist_open_uri,
	nullptr,

	nullptr,
175
	embcue_playlist_suffixes,
176
	nullptr,
177
};