M3uPlaylistPlugin.cxx 1.9 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 "M3uPlaylistPlugin.hxx"
21 22
#include "../PlaylistPlugin.hxx"
#include "../SongEnumerator.hxx"
23
#include "song/DetachedSong.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "input/TextInputStream.hxx"
25
#include "util/StringStrip.hxx"
26

27
class M3uPlaylist final : public SongEnumerator {
28 29
	TextInputStream tis;

30
public:
Max Kellermann's avatar
Max Kellermann committed
31
	explicit M3uPlaylist(InputStreamPtr &&is)
32
		:tis(std::move(is)) {
33
	}
34

35
	std::unique_ptr<DetachedSong> NextSong() override;
36 37
};

38
static std::unique_ptr<SongEnumerator>
39
m3u_open_stream(InputStreamPtr &&is)
40
{
41
	return std::make_unique<M3uPlaylist>(std::move(is));
42 43
}

44
std::unique_ptr<DetachedSong>
45
M3uPlaylist::NextSong()
46
{
47
	char *line_s;
48 49

	do {
50 51
		line_s = tis.ReadLine();
		if (line_s == nullptr)
52
			return nullptr;
53

54
		line_s = Strip(line_s);
55
	} while (line_s[0] == '#' || *line_s == 0);
56

57
	return std::make_unique<DetachedSong>(line_s);
58 59 60 61
}

static const char *const m3u_suffixes[] = {
	"m3u",
62
	"m3u8",
63
	nullptr
64 65 66 67
};

static const char *const m3u_mime_types[] = {
	"audio/x-mpegurl",
68
	"audio/mpegurl",
69
	nullptr
70 71
};

72 73 74 75
const PlaylistPlugin m3u_playlist_plugin =
	PlaylistPlugin("m3u", m3u_open_stream)
	.WithSuffixes(m3u_suffixes)
	.WithMimeTypes(m3u_mime_types);