configure.ac: Don't allow UNIX IPC to be configured for a native Windows build.
[mpd-mk.git] / src / icy_server.c
blobb655ea49c47073d075e1e75ca6c6626097ee4512
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "config.h"
21 #include "icy_server.h"
23 #include <glib.h>
25 #include <assert.h>
27 #undef G_LOG_DOMAIN
28 #define G_LOG_DOMAIN "icy_server"
30 char*
31 icy_server_metadata_header(const char *name,
32 const char *genre, const char *url,
33 const char *content_type, int metaint)
35 return g_strdup_printf("ICY 200 OK\r\n"
36 "icy-notice1:<BR>This stream requires an audio player!<BR>\r\n" /* TODO */
37 "icy-notice2:MPD - The music player daemon<BR>\r\n"
38 "icy-name: %s\r\n" /* TODO */
39 "icy-genre: %s\r\n" /* TODO */
40 "icy-url: %s\r\n" /* TODO */
41 "icy-pub:1\r\n"
42 "icy-metaint:%d\r\n"
43 /* TODO "icy-br:%d\r\n" */
44 "Content-Type: %s\r\n"
45 "Connection: close\r\n"
46 "Pragma: no-cache\r\n"
47 "Cache-Control: no-cache, no-store\r\n"
48 "\r\n",
49 name,
50 genre,
51 url,
52 metaint,
53 /* bitrate, */
54 content_type);
57 static char *
58 icy_server_metadata_string(const char *stream_title, const char* stream_url)
60 gchar *icy_metadata;
61 guint meta_length;
63 // The leading n is a placeholder for the length information
64 icy_metadata = g_strdup_printf("nStreamTitle='%s';"
65 "StreamUrl='%s';",
66 stream_title,
67 stream_url);
69 g_return_val_if_fail(icy_metadata, NULL);
71 meta_length = strlen(icy_metadata);
73 meta_length--; // substract placeholder
75 meta_length = ((int)meta_length / 16) + 1;
77 icy_metadata[0] = meta_length;
79 if (meta_length > 255) {
80 g_free(icy_metadata);
81 return NULL;
84 return icy_metadata;
87 struct page*
88 icy_server_metadata_page(const struct tag *tag, ...)
90 va_list args;
91 const gchar *tag_items[TAG_NUM_OF_ITEM_TYPES];
92 gint last_item, item;
93 guint position;
94 gchar *icy_string;
95 struct page *icy_metadata;
96 gchar stream_title[(1 + 255 - 28) * 16]; // Length + Metadata -
97 // "StreamTitle='';StreamUrl='';"
98 // = 4081 - 28
100 last_item = -1;
102 va_start(args, tag);
103 while (1) {
104 enum tag_type type;
105 const gchar *tag_item;
107 type = va_arg(args, enum tag_type);
109 if (type == TAG_NUM_OF_ITEM_TYPES)
110 break;
112 tag_item = tag_get_value(tag, type);
114 if (tag_item)
115 tag_items[++last_item] = tag_item;
117 va_end(args);
119 position = item = 0;
120 while (position < sizeof(stream_title) && item <= last_item) {
121 gint length = 0;
123 length = g_strlcpy(stream_title + position,
124 tag_items[item++],
125 sizeof(stream_title) - position);
127 position += length;
129 if (item <= last_item) {
130 length = g_strlcpy(stream_title + position,
131 " - ",
132 sizeof(stream_title) - position);
134 position += length;
138 icy_string = icy_server_metadata_string(stream_title, "");
140 if (icy_string == NULL)
141 return NULL;
143 icy_metadata = page_new_copy(icy_string, (icy_string[0] * 16) + 1);
145 g_free(icy_string);
147 return icy_metadata;