ExtM3uPlaylistPlugin.cxx 3.57 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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/TagBuilder.hxx"
27
#include "util/StringUtil.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 SongEnumerator *
63
extm3u_open_stream(InputStreamPtr &&is)
64
{
65
	ExtM3uPlaylist *playlist = new ExtM3uPlaylist(std::move(is));
66

67 68
	is = playlist->CheckFirstLine();
	if (is) {
69 70
		/* no EXTM3U header: fall back to the plain m3u
		   plugin */
71
		delete playlist;
72
		return nullptr;
73 74
	}

75
	return playlist;
76 77 78 79 80 81 82
}

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

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

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

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

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

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

114
	return tag.Commit();
115 116
}

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

	do {
124 125
		line_s = tis.ReadLine();
		if (line_s == nullptr)
126
			return nullptr;
127

128 129
		StripRight(line_s);

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

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

138
	return std::unique_ptr<DetachedSong>(new DetachedSong(line_s, std::move(tag)));
139 140 141 142
}

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

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

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

	nullptr,
	nullptr,
	nullptr,
	extm3u_open_stream,

	nullptr,
	extm3u_suffixes,
	extm3u_mime_types,
163
};