IcyMetaDataServer.cxx 3.29 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"
Max Kellermann's avatar
Max Kellermann committed
22
#include "Page.hxx"
23
#include "tag/Tag.hxx"
24
#include "util/FormatString.hxx"
25
#include "util/AllocatedString.hxx"
26 27
#include "util/StringUtil.hxx"
#include "util/Macros.hxx"
28

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

31
AllocatedString<>
32 33 34 35
icy_server_metadata_header(const char *name,
			   const char *genre, const char *url,
			   const char *content_type, int metaint)
{
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	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);
56 57
}

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

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

73
	meta_length--; // subtract placeholder
74

75
	meta_length = meta_length / 16;
76 77 78

	icy_metadata[0] = meta_length;

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

	return icy_metadata;
}

Max Kellermann's avatar
Max Kellermann committed
85
Page *
86
icy_server_metadata_page(const Tag &tag, const TagType *types)
87
{
88
	const char *tag_items[TAG_NUM_OF_ITEM_TYPES];
89

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

97
	int item = 0;
98 99

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

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

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

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

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

116
	return Page::Copy(icy_string.c_str(), uint8_t(icy_string[0]) * 16 + 1);
117
}