You need to sign in or sign up before continuing.
IcyMetaDataServer.cxx 3.32 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 "IcyMetaDataServer.hxx"
22
#include "tag/Tag.hxx"
23
#include "util/FormatString.hxx"
24
#include "util/AllocatedString.hxx"
25
#include "util/TruncateString.hxx"
26
#include "util/Macros.hxx"
27

Max Kellermann's avatar
Max Kellermann committed
28
#include <string.h>
29

30
AllocatedString<>
31 32 33 34
icy_server_metadata_header(const char *name,
			   const char *genre, const char *url,
			   const char *content_type, int metaint)
{
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
	return FormatString("ICY 200 OK\r\n"
			    "icy-notice1:<BR>This stream requires an audio player!<BR>\r\n" /* TODO */
			    "icy-notice2:MPD - The music player daemon<BR>\r\n"
			    "icy-name: %s\r\n"             /* TODO */
			    "icy-genre: %s\r\n"            /* TODO */
			    "icy-url: %s\r\n"              /* TODO */
			    "icy-pub:1\r\n"
			    "icy-metaint:%d\r\n"
			    /* TODO "icy-br:%d\r\n" */
			    "Content-Type: %s\r\n"
			    "Connection: close\r\n"
			    "Pragma: no-cache\r\n"
			    "Cache-Control: no-cache, no-store\r\n"
			    "\r\n",
			    name,
			    genre,
			    url,
			    metaint,
			    /* bitrate, */
			    content_type);
55 56
}

57
static AllocatedString<>
58 59 60
icy_server_metadata_string(const char *stream_title, const char* stream_url)
{
	// The leading n is a placeholder for the length information
61
	auto icy_metadata = FormatString("nStreamTitle='%s';"
62 63 64 65 66
					 "StreamUrl='%s';"
					 /* pad 15 spaces just in case
					    the length needs to be
					    rounded up */
					 "               ",
67 68
					 stream_title,
					 stream_url);
69

70
	size_t meta_length = strlen(icy_metadata.c_str());
71

72
	meta_length--; // subtract placeholder
73

74
	meta_length = meta_length / 16;
75 76 77

	icy_metadata[0] = meta_length;

78
	if (meta_length > 255)
79
		return nullptr;
80 81 82 83

	return icy_metadata;
}

84
PagePtr
85
icy_server_metadata_page(const Tag &tag, const TagType *types)
86
{
87
	const char *tag_items[TAG_NUM_OF_ITEM_TYPES];
88

89
	int last_item = -1;
90
	while (*types != TAG_NUM_OF_ITEM_TYPES) {
91
		const char *tag_item = tag.GetValue(*types++);
92 93 94 95
		if (tag_item)
			tag_items[++last_item] = tag_item;
	}

96
	int item = 0;
97 98

	// Length + Metadata - "StreamTitle='';StreamUrl='';" = 4081 - 28
99
	char stream_title[(1 + 255 - 28) * 16];
100
	char *p = stream_title, *const end = stream_title + ARRAY_SIZE(stream_title);
101
	stream_title[0] =  '\0';
102

103
	while (p < end && item <= last_item) {
104
		p = CopyTruncateString(p, tag_items[item++], end - p);
105

106
		if (item <= last_item)
107
			p = CopyTruncateString(p, " - ", end - p);
108 109
	}

110
	const auto icy_string = icy_server_metadata_string(stream_title, "");
111

112
	if (icy_string.IsNull())
113
		return nullptr;
114

115 116
	return std::make_shared<Page>(icy_string.c_str(),
				      uint8_t(icy_string[0]) * 16 + 1);
117
}