ExtM3uPlaylistPlugin.cxx 3.27 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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

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

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

37
public:
38
	ExtM3uPlaylist(InputStream &is)
39 40
		:tis(is) {
	}
41 42

	bool CheckFirstLine() {
43 44 45 46 47 48
		char *line = tis.ReadLine();
		if (line == nullptr)
			return false;

		StripRight(line);
		return strcmp(line, "#EXTM3U") == 0;
49 50
	}

51
	virtual DetachedSong *NextSong() override;
52 53
};

54
static SongEnumerator *
55
extm3u_open_stream(InputStream &is)
56
{
57
	ExtM3uPlaylist *playlist = new ExtM3uPlaylist(is);
58

59
	if (!playlist->CheckFirstLine()) {
60 61
		/* no EXTM3U header: fall back to the plain m3u
		   plugin */
62
		delete playlist;
63
		return nullptr;
64 65
	}

66
	return playlist;
67 68 69 70 71 72 73
}

/**
 * Parse a EXTINF line.
 *
 * @param line the rest of the input line after the colon
 */
74
static Tag
75 76 77 78 79 80 81 82 83
extm3u_parse_tag(const char *line)
{
	long duration;
	char *endptr;
	const char *name;

	duration = strtol(line, &endptr, 10);
	if (endptr[0] != ',')
		/* malformed line */
84
		return Tag();
85 86 87 88 89

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

90
	name = StripLeft(endptr + 1);
91 92 93
	if (*name == 0 && duration == 0)
		/* no information available; don't allocate a tag
		   object */
94
		return Tag();
95

96
	TagBuilder tag;
97
	tag.SetDuration(SignedSongTime::FromS(unsigned(duration)));
98 99 100 101 102

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

105
	return tag.Commit();
106 107
}

108
DetachedSong *
109
ExtM3uPlaylist::NextSong()
110
{
111
	Tag tag;
112
	char *line_s;
113 114

	do {
115 116
		line_s = tis.ReadLine();
		if (line_s == nullptr)
117
			return nullptr;
118

119 120
		StripRight(line_s);

121
		if (StringStartsWith(line_s, "#EXTINF:")) {
122
			tag = extm3u_parse_tag(line_s + 8);
123 124 125
			continue;
		}

126
		line_s = StripLeft(line_s);
127
	} while (line_s[0] == '#' || *line_s == 0);
128

129
	return new DetachedSong(line_s, std::move(tag));
130 131 132 133
}

static const char *const extm3u_suffixes[] = {
	"m3u",
134
	"m3u8",
135
	nullptr
136 137 138 139
};

static const char *const extm3u_mime_types[] = {
	"audio/x-mpegurl",
140
	nullptr
141 142 143
};

const struct playlist_plugin extm3u_playlist_plugin = {
144 145 146 147 148 149 150 151 152 153
	"extm3u",

	nullptr,
	nullptr,
	nullptr,
	extm3u_open_stream,

	nullptr,
	extm3u_suffixes,
	extm3u_mime_types,
154
};