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

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

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

38
public:
39 40
	ExtM3uPlaylist(InputStreamPtr &&is)
		:tis(std::move(is)) {
41
	}
42

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

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

		return nullptr;
57 58
	}

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

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

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

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

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

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

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

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

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

	/* 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)
110
		tag.AddItem(TAG_NAME, name);
111

112
	return tag.Commit();
113 114
}

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

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

126 127
		StripRight(line_s);

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

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

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

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

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

const struct playlist_plugin extm3u_playlist_plugin = {
152 153 154 155 156 157 158 159 160 161
	"extm3u",

	nullptr,
	nullptr,
	nullptr,
	extm3u_open_stream,

	nullptr,
	extm3u_suffixes,
	extm3u_mime_types,
162
};