~ubuntu-branches/ubuntu/utopic/mpd/utopic-proposed

« back to all changes in this revision

Viewing changes to src/icy_server.c

  • Committer: Package Import Robot
  • Author(s): Steve Kowalik
  • Date: 2013-11-12 18:17:40 UTC
  • mfrom: (2.2.36 sid)
  • Revision ID: package-import@ubuntu.com-20131112181740-72aa4zihehoobedp
Tags: 0.18.3-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Add libmp3lame-dev to Build-Depends, and enable LAME.
  - Read the user for the daemon from the config file in the init script.
  - Move avahi-daemon from Suggests to Recommends.
  - Added apport hook to include user configuration file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2003-2011 The Music Player Daemon Project
3
 
 * http://www.musicpd.org
4
 
 *
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.
9
 
 *
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.
14
 
 *
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.
18
 
 */
19
 
 
20
 
#include "config.h"
21
 
#include "icy_server.h"
22
 
 
23
 
#include <glib.h>
24
 
 
25
 
#include <assert.h>
26
 
 
27
 
#undef G_LOG_DOMAIN
28
 
#define G_LOG_DOMAIN "icy_server"
29
 
 
30
 
char*
31
 
icy_server_metadata_header(const char *name,
32
 
                           const char *genre, const char *url,
33
 
                           const char *content_type, int metaint)
34
 
{
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);
55
 
}
56
 
 
57
 
static char *
58
 
icy_server_metadata_string(const char *stream_title, const char* stream_url)
59
 
{
60
 
        gchar *icy_metadata;
61
 
        guint meta_length;
62
 
 
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);
68
 
 
69
 
        g_return_val_if_fail(icy_metadata, NULL);
70
 
 
71
 
        meta_length = strlen(icy_metadata);
72
 
 
73
 
        meta_length--; // subtract placeholder
74
 
 
75
 
        meta_length = ((int)meta_length / 16) + 1;
76
 
 
77
 
        icy_metadata[0] = meta_length;
78
 
 
79
 
        if (meta_length > 255) {
80
 
                g_free(icy_metadata);
81
 
                return NULL;
82
 
        }
83
 
 
84
 
        return icy_metadata;
85
 
}
86
 
 
87
 
struct page*
88
 
icy_server_metadata_page(const struct tag *tag, ...)
89
 
{
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
99
 
        stream_title[0] =  '\0';
100
 
 
101
 
        last_item = -1;
102
 
 
103
 
        va_start(args, tag);
104
 
        while (1) {
105
 
                enum tag_type type;
106
 
                const gchar *tag_item;
107
 
 
108
 
                type = va_arg(args, enum tag_type);
109
 
 
110
 
                if (type == TAG_NUM_OF_ITEM_TYPES)
111
 
                        break;
112
 
 
113
 
                tag_item = tag_get_value(tag, type);
114
 
 
115
 
                if (tag_item)
116
 
                        tag_items[++last_item] = tag_item;
117
 
        }
118
 
        va_end(args);
119
 
 
120
 
        position = item = 0;
121
 
        while (position < sizeof(stream_title) && item <= last_item) {
122
 
                gint length = 0;
123
 
 
124
 
                length = g_strlcpy(stream_title + position,
125
 
                                   tag_items[item++],
126
 
                                   sizeof(stream_title) - position);
127
 
 
128
 
                position += length;
129
 
 
130
 
                if (item <= last_item) {
131
 
                        length = g_strlcpy(stream_title + position,
132
 
                                           " - ",
133
 
                                           sizeof(stream_title) - position);
134
 
 
135
 
                        position += length;
136
 
                }
137
 
        }
138
 
 
139
 
        icy_string = icy_server_metadata_string(stream_title, "");
140
 
 
141
 
        if (icy_string == NULL)
142
 
                return NULL;
143
 
 
144
 
        icy_metadata = page_new_copy(icy_string, (icy_string[0] * 16) + 1);
145
 
 
146
 
        g_free(icy_string);
147
 
 
148
 
        return icy_metadata;
149
 
}