ExtM3uPlaylistPlugin.cxx 3.54 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
#include "ExtM3uPlaylistPlugin.hxx"
21 22
#include "../PlaylistPlugin.hxx"
#include "../SongEnumerator.hxx"
23
#include "song/DetachedSong.hxx"
24
#include "tag/Tag.hxx"
25
#include "tag/Builder.hxx"
26
#include "util/StringStrip.hxx"
27
#include "util/StringCompare.hxx"
Max Kellermann's avatar
Max Kellermann committed
28
#include "input/TextInputStream.hxx"
29
#include "input/InputStream.hxx"
30

31 32 33
#include <string.h>
#include <stdlib.h>

34
class ExtM3uPlaylist final : public SongEnumerator {
35 36
	TextInputStream tis;

37
public:
Max Kellermann's avatar
Max Kellermann committed
38
	explicit ExtM3uPlaylist(InputStreamPtr &&is)
39
		:tis(std::move(is)) {
40
	}
41

42 43 44 45 46
	/**
	 * @return nullptr if ExtM3U was recognized, or the original
	 * InputStream on error
	 */
	InputStreamPtr CheckFirstLine() {
47 48
		char *line = tis.ReadLine();
		if (line == nullptr)
49
			return tis.StealInputStream();
50 51

		StripRight(line);
52 53 54 55
		if (strcmp(line, "#EXTM3U") != 0)
			return tis.StealInputStream();

		return nullptr;
56 57
	}

58
	std::unique_ptr<DetachedSong> NextSong() override;
59 60
};

61
static std::unique_ptr<SongEnumerator>
62
extm3u_open_stream(InputStreamPtr &&is)
63
{
64
	auto playlist = std::make_unique<ExtM3uPlaylist>(std::move(is));
65

66
	is = playlist->CheckFirstLine();
67
	if (is)
68 69
		/* no EXTM3U header: fall back to the plain m3u
		   plugin */
70
		playlist.reset();
71

72
	return playlist;
73 74 75 76 77 78 79
}

/**
 * Parse a EXTINF line.
 *
 * @param line the rest of the input line after the colon
 */
80
static Tag
81 82 83 84 85 86 87 88 89
extm3u_parse_tag(const char *line)
{
	long duration;
	char *endptr;
	const char *name;

	duration = strtol(line, &endptr, 10);
	if (endptr[0] != ',')
		/* malformed line */
90
		return Tag();
91 92 93 94 95

	if (duration < 0)
		/* 0 means unknown duration */
		duration = 0;

96
	name = StripLeft(endptr + 1);
97 98 99
	if (*name == 0 && duration == 0)
		/* no information available; don't allocate a tag
		   object */
100
		return Tag();
101

102
	TagBuilder tag;
103
	tag.SetDuration(SignedSongTime::FromS(unsigned(duration)));
104 105 106 107 108

	/* unfortunately, there is no real specification for the
	   EXTM3U format, so we must assume that the string after the
	   comma is opaque, and is just the song name*/
	if (*name != 0)
109
		tag.AddItem(TAG_NAME, name);
110

111
	return tag.Commit();
112 113
}

114
std::unique_ptr<DetachedSong>
115
ExtM3uPlaylist::NextSong()
116
{
117
	Tag tag;
118
	char *line_s;
119 120

	do {
121 122
		line_s = tis.ReadLine();
		if (line_s == nullptr)
123
			return nullptr;
124

125 126
		StripRight(line_s);

127
		if (StringStartsWith(line_s, "#EXTINF:")) {
128
			tag = extm3u_parse_tag(line_s + 8);
129 130 131
			continue;
		}

132
		line_s = StripLeft(line_s);
133
	} while (line_s[0] == '#' || *line_s == 0);
134

135
	return std::make_unique<DetachedSong>(line_s, std::move(tag));
136 137 138 139
}

static const char *const extm3u_suffixes[] = {
	"m3u",
140
	"m3u8",
141
	nullptr
142 143 144 145
};

static const char *const extm3u_mime_types[] = {
	"audio/x-mpegurl",
146
	"audio/mpegurl",
147
	nullptr
148 149
};

150
constexpr PlaylistPlugin extm3u_playlist_plugin =
151 152 153
	PlaylistPlugin("extm3u", extm3u_open_stream)
	.WithSuffixes(extm3u_suffixes)
	.WithMimeTypes(extm3u_mime_types);