format_mp3.c 13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
 * Copyright 2000-2004, Jack Moffitt <jack@xiph.org, 
 *                      Michael Smith <msmith@xiph.org>,
 *                      oddsock <oddsock@xiph.org>,
 *                      Karl Heyes <karl@xiph.org>
 *                      and others (see AUTHORS for details).
 */

13
/* -*- c-basic-offset: 4; -*- */
Michael Smith's avatar
Michael Smith committed
14 15
/* format_mp3.c
**
16
** format plugin for mp3
Michael Smith's avatar
Michael Smith committed
17 18 19
**
*/

20 21 22 23
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Michael Smith's avatar
Michael Smith committed
24 25 26
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Karl Heyes's avatar
Karl Heyes committed
27 28 29
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
Michael Smith's avatar
Michael Smith committed
30 31

#include "refbuf.h"
32 33
#include "source.h"
#include "client.h"
Michael Smith's avatar
Michael Smith committed
34 35 36

#include "stats.h"
#include "format.h"
37 38 39 40 41 42
#include "httpp/httpp.h"

#include "logging.h"

#include "format_mp3.h"

Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
43 44 45
#ifdef WIN32
#define strcasecmp stricmp
#define strncasecmp strnicmp
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
46
#define snprintf _snprintf
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
47 48
#endif

49 50
#define CATMODULE "format-mp3"

51 52 53
/* Note that this seems to be 8192 in shoutcast - perhaps we want to be the
 * same for compability with crappy clients?
 */
54
#define ICY_METADATA_INTERVAL 16000
Michael Smith's avatar
Michael Smith committed
55

56 57 58 59
static void format_mp3_free_plugin(format_plugin_t *self);
static int format_mp3_get_buffer(format_plugin_t *self, char *data, 
        unsigned long len, refbuf_t **buffer);
static refbuf_queue_t *format_mp3_get_predata(format_plugin_t *self);
60 61
static void *format_mp3_create_client_data(format_plugin_t *self,
        source_t *source, client_t *client);
62
static void free_mp3_client_data (client_t *client);
63 64 65 66 67 68
static int format_mp3_write_buf_to_client(format_plugin_t *self,
        client_t *client, unsigned char *buf, int len);
static void format_mp3_send_headers(format_plugin_t *self, 
        source_t *source, client_t *client);

typedef struct {
Michael Smith's avatar
Michael Smith committed
69
   int use_metadata;
70 71
   int interval;
   int offset;
Michael Smith's avatar
Michael Smith committed
72 73
   int metadata_age;
   int metadata_offset;
74
} mp3_client_data;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
75

76
format_plugin_t *format_mp3_get_plugin(http_parser_t *parser)
Michael Smith's avatar
Michael Smith committed
77
{
78
    char *metadata;
79
    format_plugin_t *plugin;
Michael Smith's avatar
Michael Smith committed
80
    mp3_state *state = calloc(1, sizeof(mp3_state));
Michael Smith's avatar
Michael Smith committed
81

82
    plugin = (format_plugin_t *)malloc(sizeof(format_plugin_t));
Michael Smith's avatar
Michael Smith committed
83

84 85 86 87
    plugin->type = FORMAT_TYPE_MP3;
    plugin->has_predata = 0;
    plugin->get_buffer = format_mp3_get_buffer;
    plugin->get_predata = format_mp3_get_predata;
88
    plugin->write_buf_to_client = format_mp3_write_buf_to_client;
89
    plugin->create_client_data = format_mp3_create_client_data;
90
    plugin->client_send_headers = format_mp3_send_headers;
91
    plugin->free_plugin = format_mp3_free_plugin;
92
    plugin->format_description = "MP3 audio";
Michael Smith's avatar
Michael Smith committed
93

94
    plugin->_state = state;
Michael Smith's avatar
Michael Smith committed
95

Michael Smith's avatar
Michael Smith committed
96 97
    state->metadata_age = 0;
    state->metadata = strdup("");
Michael Smith's avatar
Michael Smith committed
98
    thread_mutex_create(&(state->lock));
Michael Smith's avatar
Michael Smith committed
99

100 101 102 103
    metadata = httpp_getvar(parser, "icy-metaint");
    if(metadata)
        state->inline_metadata_interval = atoi(metadata);

104
    return plugin;
Michael Smith's avatar
Michael Smith committed
105 106
}

107

Michael Smith's avatar
Michael Smith committed
108 109 110 111 112
static int send_metadata(client_t *client, mp3_client_data *client_state,
        mp3_state *source_state)
{
    int len_byte;
    int len;
113
    int ret = -1;
Michael Smith's avatar
Michael Smith committed
114 115
    unsigned char *buf;
    int source_age;
116 117 118
    char *fullmetadata = NULL;
    int  fullmetadata_size = 0;
    const char meta_fmt[] = "StreamTitle='';"; 
Michael Smith's avatar
Michael Smith committed
119

120 121 122 123 124 125
    do 
    {
        thread_mutex_lock (&(source_state->lock));
        if (source_state->metadata == NULL)
            break; /* Shouldn't be possible */

126 127
        fullmetadata_size = strlen (source_state->metadata) + sizeof (meta_fmt);

128
        if (fullmetadata_size > 4079)
129
        {
130
            fullmetadata_size = 4079;
131
        }
132 133 134
        fullmetadata = malloc (fullmetadata_size);
        if (fullmetadata == NULL)
            break;
135

136
        fullmetadata_size = snprintf (fullmetadata, fullmetadata_size,
137
                "StreamTitle='%.*s';", fullmetadata_size-(sizeof (meta_fmt)-1), source_state->metadata);
138

139
        source_age = source_state->metadata_age;
140

141 142
        if (fullmetadata_size > 0 && source_age != client_state->metadata_age)
        {
143
            len_byte = (fullmetadata_size)/16 + 1; /* to give 1-255 */
144 145 146 147 148
            client_state->metadata_offset = 0;
        }
        else
            len_byte = 0;
        len = 1 + len_byte*16;
149
        buf = calloc (1, len);
150 151 152 153 154 155 156 157 158
        if (buf == NULL)
            break;

        buf[0] = len_byte;

        if (len > 1) {
            strncpy (buf+1, fullmetadata, len-1);
            buf[len-1] = '\0';
        }
Michael Smith's avatar
Michael Smith committed
159

160
        thread_mutex_unlock (&(source_state->lock));
Michael Smith's avatar
Michael Smith committed
161

162 163
        /* only write what hasn't been written already */
        ret = sock_write_bytes (client->con->sock, buf+client_state->metadata_offset, len-client_state->metadata_offset);
Michael Smith's avatar
Michael Smith committed
164

165 166 167 168 169 170 171 172 173
        if (ret > 0 && ret < len) {
            client_state->metadata_offset += ret;
        }
        else if (ret == len) {
            client_state->metadata_age = source_age;
            client_state->offset = 0;
            client_state->metadata_offset = 0;
        }
        free (buf);
174
        free (fullmetadata);
175
        return ret;
Michael Smith's avatar
Michael Smith committed
176

177
    } while (0);
Michael Smith's avatar
Michael Smith committed
178 179

    thread_mutex_unlock(&(source_state->lock));
180
    free (fullmetadata);
181
    return -1;
Michael Smith's avatar
Michael Smith committed
182 183
}

184 185 186 187
static int format_mp3_write_buf_to_client(format_plugin_t *self, 
    client_t *client, unsigned char *buf, int len) 
{
    int ret;
188
    mp3_client_data *mp3data = client->format_data;
Michael Smith's avatar
Michael Smith committed
189
    
190
    if(((mp3_state *)self->_state)->metadata && mp3data->use_metadata)
Michael Smith's avatar
Michael Smith committed
191 192 193
    {
        mp3_client_data *state = client->format_data;
        int max = state->interval - state->offset;
194

Michael Smith's avatar
Michael Smith committed
195 196 197 198 199 200 201 202 203 204 205
        if(len == 0) /* Shouldn't happen */
            return 0;

        if(max > len)
            max = len;

        if(max > 0) {
            ret = sock_write_bytes(client->con->sock, buf, max);
            if(ret > 0)
                state->offset += ret;
        }
206
        else {
Michael Smith's avatar
Michael Smith committed
207
            ret = send_metadata(client, state, self->_state);
208 209 210 211
            if(ret > 0)
                client->con->sent_bytes += ret;
            ret = 0;
        }
212

Michael Smith's avatar
Michael Smith committed
213 214 215 216
    }
    else {
        ret = sock_write_bytes(client->con->sock, buf, len);
    }
217 218

    if(ret < 0) {
219
        if(sock_recoverable(sock_error())) {
220 221 222 223 224 225 226 227 228 229 230
            DEBUG1("Client had recoverable error %ld", ret);
            ret = 0;
        }
    }
    else
        client->con->sent_bytes += ret;

    return ret;
}

static void format_mp3_free_plugin(format_plugin_t *self)
Michael Smith's avatar
Michael Smith committed
231
{
232
    /* free the plugin instance */
Michael Smith's avatar
Michael Smith committed
233 234
    mp3_state *state = self->_state;
    thread_mutex_destroy(&(state->lock));
Michael Smith's avatar
Michael Smith committed
235 236

    free(state->metadata);
Michael Smith's avatar
Michael Smith committed
237
    free(state);
238
    free(self);
Michael Smith's avatar
Michael Smith committed
239 240
}

241 242
static int format_mp3_get_buffer(format_plugin_t *self, char *data, 
    unsigned long len, refbuf_t **buffer)
Michael Smith's avatar
Michael Smith committed
243
{
244
    refbuf_t *refbuf;
245 246
    mp3_state *state = self->_state;

247 248 249 250
    /* Set this to NULL in case it doesn't get set to a valid buffer later */
    *buffer = NULL;

    if(!data)
Michael Smith's avatar
Michael Smith committed
251
        return 0;
252

253
    if(state->inline_metadata_interval) {
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
        /* Source is sending metadata, handle it... */

        while(len > 0) {
            int to_read = state->inline_metadata_interval - state->offset;
            if(to_read > 0) {
                refbuf_t *old_refbuf = *buffer;

                if(to_read > len)
                    to_read = len;

                if(old_refbuf) {
                    refbuf = refbuf_new(to_read + old_refbuf->len);
                    memcpy(refbuf->data, old_refbuf->data, old_refbuf->len);
                    memcpy(refbuf->data+old_refbuf->len, data, to_read);

                    refbuf_release(old_refbuf);
                }
                else {
                    refbuf = refbuf_new(to_read);
                    memcpy(refbuf->data, data, to_read);
                }

                *buffer = refbuf;

                state->offset += to_read;
                data += to_read;
                len -= to_read;
            }
            else if(!state->metadata_length) {
                /* Next up is the metadata byte... */
                unsigned char byte = data[0];
                data++;
                len--;

                /* According to the "spec"... this byte * 16 */
                state->metadata_length = byte * 16;
290

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
                if(state->metadata_length) {
                    state->metadata_buffer = 
                        calloc(state->metadata_length + 1, 1);

                    /* Ensure we have a null-terminator even if the source
                     * stream is invalid.
                     */
                    state->metadata_buffer[state->metadata_length] = 0;
                }
                else {
                    state->offset = 0;
                }

                state->metadata_offset = 0;
            }
            else {
                /* Metadata to read! */
                int readable = state->metadata_length - state->metadata_offset;

                if(readable > len)
                    readable = len;

                memcpy(state->metadata_buffer + state->metadata_offset, 
                        data, readable);

316 317
                state->metadata_offset += readable;

318 319 320 321 322 323 324 325 326
                data += readable;
                len -= readable;

                if(state->metadata_offset == state->metadata_length)
                {
                    if(state->metadata_length)
                    {
                        thread_mutex_lock(&(state->lock));
                        free(state->metadata);
327 328 329 330
                        /* Now, reformat state->metadata_buffer to strip off
                           StreamTitle=' and the closing '; (but only if there's
                           enough data for it to be correctly formatted) */
                        if(state->metadata_length >= 15) {
331 332 333 334 335 336 337
                            /* This is overly complex because the 
                               metadata_length is the length of the actual raw
                               data, but the (null-terminated) string is going
                               to be shorter than this, and we can't trust that
                               the raw data doesn't have other embedded-nulls */
                            int stringlength;
                            
338
                            state->metadata = malloc(state->metadata_length -
Michael Smith's avatar
Michael Smith committed
339
                                    12);
340 341
                            memcpy(state->metadata, 
                                    state->metadata_buffer + 13, 
Michael Smith's avatar
Michael Smith committed
342 343 344 345 346 347
                                    state->metadata_length - 13);
                            /* Make sure we've got a null-terminator of some
                               sort */
                            state->metadata[state->metadata_length - 13] = 0;

                            /* Now figure out the _right_ one */
348 349 350
                            stringlength = strlen(state->metadata);
                            if(stringlength > 2)
                                state->metadata[stringlength - 2] = 0;
351 352 353 354 355 356
                            free(state->metadata_buffer);
                        }
                        else
                            state->metadata = state->metadata_buffer;

                        stats_event(self->mount, "title", state->metadata);
357 358 359
                        state->metadata_buffer = NULL;
                        state->metadata_age++;
                        thread_mutex_unlock(&(state->lock));
360
                        yp_touch (self->mount);
361
                    }
362 363 364

                    state->offset = 0;
                    state->metadata_length = 0;
365 366 367 368 369 370 371 372
                }
            }
        }

        /* Either we got a buffer above (in which case it can be used), or
         * we set *buffer to NULL in the prologue, so the return value is
         * correct anyway...
         */
373 374 375
        return 0;
    }
    else {
376
        /* Simple case - no metadata, just dump data directly to a buffer */
377
        refbuf = refbuf_new(len);
Michael Smith's avatar
Michael Smith committed
378

379
        memcpy(refbuf->data, data, len);
Michael Smith's avatar
Michael Smith committed
380

381
        *buffer = refbuf;
382
        return 0;
383
    }
Michael Smith's avatar
Michael Smith committed
384 385
}

386
static refbuf_queue_t *format_mp3_get_predata(format_plugin_t *self)
Michael Smith's avatar
Michael Smith committed
387 388 389 390
{
    return NULL;
}

391 392 393 394 395 396 397 398
static void *format_mp3_create_client_data(format_plugin_t *self, 
        source_t *source, client_t *client) 
{
    mp3_client_data *data = calloc(1,sizeof(mp3_client_data));
    char *metadata;

    data->interval = ICY_METADATA_INTERVAL;
    data->offset = 0;
399
    client->free_client_data = free_mp3_client_data;
400

401
    metadata = httpp_getvar(client->parser, "icy-metadata");
402
    if(metadata)
Michael Smith's avatar
Michael Smith committed
403
        data->use_metadata = atoi(metadata)>0?1:0;
404 405

    return data;
406
}
Michael Smith's avatar
Michael Smith committed
407

408 409 410 411 412 413 414 415

static void free_mp3_client_data (client_t *client)
{
    free (client->format_data);
    client->format_data = NULL;
}


416 417 418 419
static void format_mp3_send_headers(format_plugin_t *self,
        source_t *source, client_t *client)
{
    int bytes;
420
    mp3_client_data *mp3data = client->format_data;
421 422
    
    client->respcode = 200;
Michael Smith's avatar
Michael Smith committed
423
    /* TODO: This may need to be ICY/1.0 for shoutcast-compatibility? */
424 425 426 427 428
    bytes = sock_write(client->con->sock, 
            "HTTP/1.0 200 OK\r\n" 
            "Content-Type: %s\r\n", 
            format_get_mimetype(source->format->type));

429 430
    if (bytes > 0)
        client->con->sent_bytes += bytes;
431

432 433
    if (mp3data->use_metadata)
    {
434
        int bytes = sock_write(client->con->sock, "icy-metaint:%d\r\n", 
435 436 437 438
                ICY_METADATA_INTERVAL);
        if(bytes > 0)
            client->con->sent_bytes += bytes;
    }
439
    format_send_general_headers(self, source, client);
440 441
}