format_mp3.c 21.6 KB
Newer Older
1
/* -*- c-basic-offset: 4; -*- */
2 3 4 5 6
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
7
 * Copyright 2000-2004, Jack Moffitt <jack@xiph.org,
8 9 10 11
 *                      Michael Smith <msmith@xiph.org>,
 *                      oddsock <oddsock@xiph.org>,
 *                      Karl Heyes <karl@xiph.org>
 *                      and others (see AUTHORS for details).
Philipp Schafft's avatar
Philipp Schafft committed
12
 * Copyright 2011-2012, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
13 14
 */

Michael Smith's avatar
Michael Smith committed
15
/* format_mp3.c
16 17 18 19
 **
 ** format plugin for mp3
 **
 */
Michael Smith's avatar
Michael Smith committed
20

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

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

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

#include "stats.h"
#include "format.h"
Marvin Scholz's avatar
Marvin Scholz committed
38
#include "common/httpp/httpp.h"
39 40 41 42 43 44 45

#include "logging.h"

#include "format_mp3.h"

#define CATMODULE "format-mp3"

46 47 48
/* Note that this seems to be 8192 in shoutcast - perhaps we want to be the
 * same for compability with crappy clients?
 */
49
#define ICY_METADATA_INTERVAL 16000
Michael Smith's avatar
Michael Smith committed
50

51
static void format_mp3_free_plugin(format_plugin_t *self);
Karl Heyes's avatar
Karl Heyes committed
52 53 54 55
static refbuf_t *mp3_get_filter_meta (source_t *source);
static refbuf_t *mp3_get_no_meta (source_t *source);

static int  format_mp3_create_client_data (source_t *source, client_t *client);
56
static void free_mp3_client_data (client_t *client);
57
static int format_mp3_write_buf_to_client(client_t *client);
Karl Heyes's avatar
Karl Heyes committed
58
static void write_mp3_to_file (struct source_tag *source, refbuf_t *refbuf);
59
static void mp3_set_tag (format_plugin_t *plugin, const char *tag, const char *in_value, const char *charset);
60
static void format_mp3_apply_settings(client_t *client, format_plugin_t *format, mount_proxy *mount);
Karl Heyes's avatar
Karl Heyes committed
61

62 63

typedef struct {
64 65 66 67 68
    unsigned int interval;
    int metadata_offset;
    unsigned int since_meta_block;
    int in_metadata;
    refbuf_t *associated;
69
} mp3_client_data;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
70

71
int format_mp3_get_plugin(source_t *source)
Michael Smith's avatar
Michael Smith committed
72
{
73
    const char *metadata;
74
    format_plugin_t *plugin;
Michael Smith's avatar
Michael Smith committed
75
    mp3_state *state = calloc(1, sizeof(mp3_state));
Karl Heyes's avatar
Karl Heyes committed
76
    refbuf_t *meta;
Michael Smith's avatar
Michael Smith committed
77

78
    plugin = (format_plugin_t *) calloc(1, sizeof(format_plugin_t));
Michael Smith's avatar
Michael Smith committed
79

80
    plugin->type = FORMAT_TYPE_GENERIC;
Karl Heyes's avatar
Karl Heyes committed
81
    plugin->get_buffer = mp3_get_no_meta;
82
    plugin->write_buf_to_client = format_mp3_write_buf_to_client;
Karl Heyes's avatar
Karl Heyes committed
83
    plugin->write_buf_to_file = write_mp3_to_file;
84
    plugin->create_client_data = format_mp3_create_client_data;
85
    plugin->free_plugin = format_mp3_free_plugin;
86
    plugin->set_tag = mp3_set_tag;
87
    plugin->apply_settings = format_mp3_apply_settings;
88

89
    plugin->contenttype = httpp_getvar(source->parser, "content-type");
90 91 92 93
    if (plugin->contenttype == NULL) {
        /* We default to MP3 audio for old clients without content types */
        plugin->contenttype = "audio/mpeg";
    }
Michael Smith's avatar
Michael Smith committed
94

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

97 98
    /* initial metadata needs to be blank for sending to clients and for
       comparing with new metadata */
99 100
    meta = refbuf_new (17);
    memcpy (meta->data, "\001StreamTitle='';", 17);
Karl Heyes's avatar
Karl Heyes committed
101
    state->metadata = meta;
102
    state->interval = -1;
Michael Smith's avatar
Michael Smith committed
103

Karl Heyes's avatar
Karl Heyes committed
104 105 106 107
    metadata = httpp_getvar (source->parser, "icy-metaint");
    if (metadata)
    {
        state->inline_metadata_interval = atoi (metadata);
108 109 110 111
        if (state->inline_metadata_interval > 0)
        {
            state->offset = 0;
            plugin->get_buffer = mp3_get_filter_meta;
112
            state->interval = state->inline_metadata_interval;
113
        }
Karl Heyes's avatar
Karl Heyes committed
114
    }
115 116

    vorbis_comment_init(&plugin->vc);
Karl Heyes's avatar
Karl Heyes committed
117
    source->format = plugin;
118
    thread_mutex_create(&state->url_lock);
119

Karl Heyes's avatar
Karl Heyes committed
120
    return 0;
Michael Smith's avatar
Michael Smith committed
121 122
}

123

124
static void mp3_set_tag (format_plugin_t *plugin, const char *tag, const char *in_value, const char *charset)
Michael Smith's avatar
Michael Smith committed
125
{
Karl Heyes's avatar
Karl Heyes committed
126
    mp3_state *source_mp3 = plugin->_state;
127
    char *value = NULL;
Karl Heyes's avatar
Karl Heyes committed
128 129 130

    /* protect against multiple updaters */
    thread_mutex_lock (&source_mp3->url_lock);
131

132 133 134 135 136 137
    if (tag==NULL)
    {
        source_mp3->update_metadata = 1;
        thread_mutex_unlock (&source_mp3->url_lock);
        return;
    }
138

139 140 141 142 143 144
    if (in_value)
    {
        value = util_conv_string (in_value, charset, plugin->charset);
        if (value == NULL)
            value = strdup (in_value);
    }
145

146 147 148 149 150 151
    if (strcmp(tag, "title") == 0 || strcmp(tag, "song") == 0) {
        tag = MP3_METADATA_TITLE;
    } else if (strcmp(tag, "artist") == 0) {
        tag = MP3_METADATA_ARTIST;
    } else if (strcmp(tag, "url") == 0) {
        tag = MP3_METADATA_URL;
Karl Heyes's avatar
Karl Heyes committed
152
    }
153 154 155 156

    format_set_vorbiscomment(plugin, tag, value);
    free (value);

Karl Heyes's avatar
Karl Heyes committed
157 158
    thread_mutex_unlock (&source_mp3->url_lock);
}
159 160


Karl Heyes's avatar
Karl Heyes committed
161 162 163 164 165 166
static void filter_shoutcast_metadata (source_t *source, char *metadata, unsigned int meta_len)
{
    if (metadata)
    {
        char *end, *p;
        int len;
167

Karl Heyes's avatar
Karl Heyes committed
168
        do
169
        {
Karl Heyes's avatar
Karl Heyes committed
170 171 172
            metadata++;
            if (strncmp (metadata, "StreamTitle='", 13))
                break;
Karl Heyes's avatar
Karl Heyes committed
173
            if ((end = strstr (metadata+13, "\';")) == NULL)
Karl Heyes's avatar
Karl Heyes committed
174 175 176 177 178 179
                break;
            len = (end - metadata) - 13;
            p = calloc (1, len+1);
            if (p)
            {
                memcpy (p, metadata+13, len);
180
                logging_playlist (source->mount, p, source->listeners);
181
                stats_event_conv (source->mount, "title", p, source->format->charset);
Karl Heyes's avatar
Karl Heyes committed
182 183
                yp_touch (source->mount);
                free (p);
184
                playlist_push_track(source->history, &source->format->vc);
Karl Heyes's avatar
Karl Heyes committed
185 186 187 188
            }
        } while (0);
    }
}
189

Michael Smith's avatar
Michael Smith committed
190

191 192 193 194
static void format_mp3_apply_settings (client_t *client, format_plugin_t *format, mount_proxy *mount)
{
    mp3_state *source_mp3 = format->_state;

195 196 197 198 199 200
    source_mp3->interval = -1;
    free (format->charset);
    format->charset = NULL;

    if (mount)
    {
201
        if (mount->mp3_meta_interval >= 0)
202 203 204 205
            source_mp3->interval = mount->mp3_meta_interval;
        if (mount->charset)
            format->charset = strdup (mount->charset);
    }
206
    if (source_mp3->interval < 0)
207
    {
208
        const char *metadata = httpp_getvar (client->parser, "icy-metaint");
209
        source_mp3->interval = ICY_METADATA_INTERVAL;
210 211 212 213 214 215 216
        if (metadata)
        {
            int interval = atoi (metadata);
            if (interval > 0)
                source_mp3->interval = interval;
        }
    }
217 218 219 220

    if (format->charset == NULL)
        format->charset = strdup ("ISO8859-1");

221 222
    ICECAST_LOG_DEBUG("sending metadata interval %d", source_mp3->interval);
    ICECAST_LOG_DEBUG("charset %s", format->charset);
223 224 225
}


Karl Heyes's avatar
Karl Heyes committed
226 227 228
/* called from the source thread when the metadata has been updated.
 * The artist title are checked and made ready for clients to send
 */
229
static void mp3_set_title(source_t *source)
Karl Heyes's avatar
Karl Heyes committed
230
{
231 232
    const char streamtitle[] = "StreamTitle='";
    const char streamurl[] = "StreamUrl='";
233 234 235
    const char *url_artist = vorbis_comment_query(&source->format->vc, MP3_METADATA_ARTIST, 0);
    const char *url_title = vorbis_comment_query(&source->format->vc, MP3_METADATA_TITLE, 0);
    const char *url = vorbis_comment_query(&source->format->vc, MP3_METADATA_URL, 0);
236
    size_t size;
Karl Heyes's avatar
Karl Heyes committed
237 238
    unsigned char len_byte;
    refbuf_t *p;
239
    unsigned int len = sizeof(streamtitle) + 2; /* the StreamTitle, quotes, ; and null */
Karl Heyes's avatar
Karl Heyes committed
240 241 242 243 244 245
    mp3_state *source_mp3 = source->format->_state;

    /* make sure the url data does not disappear from under us */
    thread_mutex_lock (&source_mp3->url_lock);

    /* work out message length */
246 247 248 249 250
    if (url_artist)
        len += strlen (url_artist);
    if (url_title)
        len += strlen (url_title);
    if (url_artist && url_title)
Karl Heyes's avatar
Karl Heyes committed
251
        len += 3;
252 253 254 255 256 257
    if (source_mp3->inline_url)
    {
        char *end = strstr (source_mp3->inline_url, "';");
        if (end)
            len += end - source_mp3->inline_url+2;
    }
258 259
    else if (url)
        len += strlen (url) + strlen (streamurl) + 2;
Karl Heyes's avatar
Karl Heyes committed
260 261 262 263
#define MAX_META_LEN 255*16
    if (len > MAX_META_LEN)
    {
        thread_mutex_unlock (&source_mp3->url_lock);
264
        ICECAST_LOG_WARN("Metadata too long at %d chars", len);
Karl Heyes's avatar
Karl Heyes committed
265 266 267 268
        return;
    }
    /* work out the metadata len byte */
    len_byte = (len-1) / 16 + 1;
Michael Smith's avatar
Michael Smith committed
269

Karl Heyes's avatar
Karl Heyes committed
270 271
    /* now we know how much space to allocate, +1 for the len byte */
    size = len_byte * 16 + 1;
Michael Smith's avatar
Michael Smith committed
272

Karl Heyes's avatar
Karl Heyes committed
273 274 275 276
    p = refbuf_new (size);
    if (p)
    {
        mp3_state *source_mp3 = source->format->_state;
277
        int r;
Michael Smith's avatar
Michael Smith committed
278

Karl Heyes's avatar
Karl Heyes committed
279
        memset (p->data, '\0', size);
280
        if (url_artist && url_title)
281
            r = snprintf (p->data, size, "%c%s%s - %s';", len_byte, streamtitle,
282
                    url_artist, url_title);
Karl Heyes's avatar
Karl Heyes committed
283
        else
284
            r = snprintf (p->data, size, "%c%s%s';", len_byte, streamtitle,
285
                    url_title);
286 287 288 289 290
        if (r > 0)
        {
            if (source_mp3->inline_url)
            {
                char *end = strstr (source_mp3->inline_url, "';");
291
                ssize_t urllen = size;
292
                if (end) urllen = end - source_mp3->inline_url + 2;
293
                if ((ssize_t)(size-r) > urllen)
294 295
                    snprintf (p->data+r, size-r, "StreamUrl='%s';", source_mp3->inline_url+11);
            }
296 297
            else if (url)
                snprintf (p->data+r, size-r, "StreamUrl='%s';", url);
298
        }
299
        ICECAST_LOG_DEBUG("shoutcast metadata block setup with %s", p->data+1);
Karl Heyes's avatar
Karl Heyes committed
300
        filter_shoutcast_metadata (source, p->data, size);
Michael Smith's avatar
Michael Smith committed
301

Karl Heyes's avatar
Karl Heyes committed
302 303 304 305
        refbuf_release (source_mp3->metadata);
        source_mp3->metadata = p;
    }
    thread_mutex_unlock (&source_mp3->url_lock);
Michael Smith's avatar
Michael Smith committed
306 307
}

Karl Heyes's avatar
Karl Heyes committed
308 309 310 311 312

/* send the appropriate metadata, and return the number of bytes written
 * which is 0 or greater.  Check the client in_metadata value afterwards
 * to see if all metadata has been sent
 */
313
static int send_stream_metadata (client_t *client, refbuf_t *associated)
314
{
315
    int ret = 0;
316
    char *metadata;
Karl Heyes's avatar
Karl Heyes committed
317 318 319 320 321 322
    int meta_len;
    mp3_client_data *client_mp3 = client->format_data;

    /* If there is a change in metadata then send it else
     * send a single zero value byte in its place
     */
323
    if (associated && associated != client_mp3->associated)
Karl Heyes's avatar
Karl Heyes committed
324
    {
325 326
        metadata = associated->data + client_mp3->metadata_offset;
        meta_len = associated->len - client_mp3->metadata_offset;
Karl Heyes's avatar
Karl Heyes committed
327 328 329
    }
    else
    {
330 331 332 333 334 335 336 337 338 339 340
        if (associated)
        {
            metadata = "\0";
            meta_len = 1;
        }
        else
        {
            char *meta = "\001StreamTitle='';";
            metadata = meta + client_mp3->metadata_offset;
            meta_len = 17 - client_mp3->metadata_offset;
        }
Karl Heyes's avatar
Karl Heyes committed
341
    }
342
    ret = client_send_bytes(client, metadata, meta_len);
Karl Heyes's avatar
Karl Heyes committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

    if (ret == meta_len)
    {
        client_mp3->associated = associated;
        client_mp3->metadata_offset = 0;
        client_mp3->in_metadata = 0;
        client_mp3->since_meta_block = 0;
        return ret;
    }
    if (ret > 0)
        client_mp3->metadata_offset += ret;
    else
        ret = 0;
    client_mp3->in_metadata = 1;

    return ret;
}


/* Handler for writing mp3 data to a client, taking into account whether
 * client has requested shoutcast style metadata updates
 */
365
static int format_mp3_write_buf_to_client(client_t *client)
Karl Heyes's avatar
Karl Heyes committed
366 367 368 369
{
    int ret, written = 0;
    mp3_client_data *client_mp3 = client->format_data;
    refbuf_t *refbuf = client->refbuf;
Karl Heyes's avatar
Karl Heyes committed
370 371
    char *buf = refbuf->data + client->pos;
    unsigned int len = refbuf->len - client->pos;
Michael Smith's avatar
Michael Smith committed
372

Karl Heyes's avatar
Karl Heyes committed
373 374 375 376 377 378
    do
    {
        /* send any unwritten metadata to the client */
        if (client_mp3->in_metadata)
        {
            refbuf_t *associated = refbuf->associated;
379
            ret = send_stream_metadata(client, associated);
Michael Smith's avatar
Michael Smith committed
380

Karl Heyes's avatar
Karl Heyes committed
381 382 383
            if (client_mp3->in_metadata)
                break;
            written += ret;
Michael Smith's avatar
Michael Smith committed
384
        }
Karl Heyes's avatar
Karl Heyes committed
385
        /* see if we need to send the current metadata to the client */
386
        if (client_mp3->interval)
Karl Heyes's avatar
Karl Heyes committed
387
        {
388
            unsigned int remaining = client_mp3->interval -
Karl Heyes's avatar
Karl Heyes committed
389 390
                client_mp3->since_meta_block;

391
            /* leading up to sending the metadata block */
Karl Heyes's avatar
Karl Heyes committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
            if (remaining <= len)
            {
                /* send any mp3 before the metadata block */
                if (remaining)
                {
                    ret = client_send_bytes (client, buf, remaining);

                    if (ret > 0)
                    {
                        client_mp3->since_meta_block += ret;
                        client->pos += ret;
                    }
                    if (ret < (int)remaining)
                        break;
                    written += ret;
                }
408
                ret = send_stream_metadata (client, refbuf->associated);
Karl Heyes's avatar
Karl Heyes committed
409 410 411 412 413
                if (client_mp3->in_metadata)
                    break;
                written += ret;
                buf += remaining;
                len -= remaining;
414 415 416
                /* limit how much mp3 we send if using small intervals */
                if (len > client_mp3->interval)
                    len = client_mp3->interval;
Karl Heyes's avatar
Karl Heyes committed
417
            }
418
        }
Karl Heyes's avatar
Karl Heyes committed
419 420 421 422
        /* write any mp3, maybe after the metadata block */
        if (len)
        {
            ret = client_send_bytes (client, buf, len);
423

Karl Heyes's avatar
Karl Heyes committed
424 425 426 427 428 429 430 431 432 433 434
            if (ret > 0)
            {
                client_mp3->since_meta_block += ret;
                client->pos += ret;
            }
            if (ret < (int)len)
                break;
            written += ret;
        }
        ret = 0;
    } while (0);
435

Karl Heyes's avatar
Karl Heyes committed
436 437 438
    if (ret > 0)
        written += ret;
    return written;
439 440 441
}

static void format_mp3_free_plugin(format_plugin_t *self)
Michael Smith's avatar
Michael Smith committed
442
{
443
    /* free the plugin instance */
Michael Smith's avatar
Michael Smith committed
444
    mp3_state *state = self->_state;
Michael Smith's avatar
Michael Smith committed
445

446 447 448 449
    thread_mutex_destroy(&state->url_lock);
    free(self->charset);
    refbuf_release(state->metadata);
    refbuf_release(state->read_data);
Michael Smith's avatar
Michael Smith committed
450
    free(state);
451
    vorbis_comment_clear(&self->vc);
452
    free(self);
Michael Smith's avatar
Michael Smith committed
453 454
}

Karl Heyes's avatar
Karl Heyes committed
455

456 457
/* This does the actual reading, making sure the read data is packaged in
 * blocks of 1400 bytes (near the common MTU size). This is because many
458
 * incoming streams come in small packets which could waste a lot of
459 460
 * bandwidth with many listeners due to headers and such like.
 */
461
static int complete_read(source_t *source)
Michael Smith's avatar
Michael Smith committed
462
{
Karl Heyes's avatar
Karl Heyes committed
463
    int bytes;
464
    format_plugin_t *format = source->format;
465 466 467
    mp3_state *source_mp3 = format->_state;
    char *buf;
    refbuf_t *refbuf;
468

469 470 471 472
#define REFBUF_SIZE 1400

    if (source_mp3->read_data == NULL)
    {
473
        source_mp3->read_data = refbuf_new (REFBUF_SIZE);
474 475 476
        source_mp3->read_count = 0;
    }
    buf = source_mp3->read_data->data + source_mp3->read_count;
477

478
    bytes = client_read_bytes (source->client, buf, REFBUF_SIZE-source_mp3->read_count);
479
    if (bytes < 0)
Karl Heyes's avatar
Karl Heyes committed
480
    {
481 482 483 484 485 486
        if (source->client->con->error)
        {
            refbuf_release (source_mp3->read_data);
            source_mp3->read_data = NULL;
        }
        return 0;
Karl Heyes's avatar
Karl Heyes committed
487
    }
488 489 490
    source_mp3->read_count += bytes;
    refbuf = source_mp3->read_data;
    refbuf->len = source_mp3->read_count;
491
    format->read_bytes += bytes;
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517

    if (source_mp3->read_count < REFBUF_SIZE)
    {
        if (source_mp3->read_count == 0)
        {
            refbuf_release (source_mp3->read_data);
            source_mp3->read_data = NULL;
        }
        return 0;
    }
    return 1;
}


/* read an mp3 stream which does not have shoutcast style metadata */
static refbuf_t *mp3_get_no_meta (source_t *source)
{
    refbuf_t *refbuf;
    mp3_state *source_mp3 = source->format->_state;

    if (complete_read (source) == 0)
        return NULL;

    refbuf = source_mp3->read_data;
    source_mp3->read_data = NULL;

Karl Heyes's avatar
Karl Heyes committed
518 519 520 521 522
    if (source_mp3->update_metadata)
    {
        mp3_set_title (source);
        source_mp3->update_metadata = 0;
    }
523 524 525 526
    refbuf->associated = source_mp3->metadata;
    refbuf_addref (source_mp3->metadata);
    refbuf->sync_point = 1;
    return refbuf;
Karl Heyes's avatar
Karl Heyes committed
527
}
528 529


Karl Heyes's avatar
Karl Heyes committed
530 531 532 533
/* read mp3 data with inlined metadata from the source. Filter out the
 * metadata so that the mp3 data itself is store on the queue and the
 * metadata is is associated with it
 */
534
static refbuf_t *mp3_get_filter_meta(source_t *source)
Karl Heyes's avatar
Karl Heyes committed
535 536 537 538 539 540
{
    refbuf_t *refbuf;
    format_plugin_t *plugin = source->format;
    mp3_state *source_mp3 = plugin->_state;
    unsigned char *src;
    unsigned int bytes, mp3_block;
541

542 543 544 545 546
    if (complete_read (source) == 0)
        return NULL;

    refbuf = source_mp3->read_data;
    source_mp3->read_data = NULL;
547
    src = (unsigned char *)refbuf->data;
548

Karl Heyes's avatar
Karl Heyes committed
549 550 551 552 553 554
    if (source_mp3->update_metadata)
    {
        mp3_set_title (source);
        source_mp3->update_metadata = 0;
    }
    /* fill the buffer with the read data */
555
    bytes = source_mp3->read_count;
Karl Heyes's avatar
Karl Heyes committed
556 557 558 559
    refbuf->len = 0;
    while (bytes > 0)
    {
        unsigned int metadata_remaining;
560

Karl Heyes's avatar
Karl Heyes committed
561
        mp3_block = source_mp3->inline_metadata_interval - source_mp3->offset;
562

Karl Heyes's avatar
Karl Heyes committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
        /* is there only enough to account for mp3 data */
        if (bytes <= mp3_block)
        {
            refbuf->len += bytes;
            source_mp3->offset += bytes;
            break;
        }
        /* we have enough data to get to the metadata
         * block, but only transfer upto it */
        if (mp3_block)
        {
            src += mp3_block;
            bytes -= mp3_block;
            refbuf->len += mp3_block;
            source_mp3->offset += mp3_block;
            continue;
        }
580

Karl Heyes's avatar
Karl Heyes committed
581 582 583 584 585 586 587 588
        /* process the inline metadata, len == 0 indicates not seen any yet */
        if (source_mp3->build_metadata_len == 0)
        {
            memset (source_mp3->build_metadata, 0,
                    sizeof (source_mp3->build_metadata));
            source_mp3->build_metadata_offset = 0;
            source_mp3->build_metadata_len = 1 + (*src * 16);
        }
589

Karl Heyes's avatar
Karl Heyes committed
590 591 592 593 594 595 596 597 598 599
        /* do we have all of the metatdata block */
        metadata_remaining = source_mp3->build_metadata_len -
            source_mp3->build_metadata_offset;
        if (bytes < metadata_remaining)
        {
            memcpy (source_mp3->build_metadata +
                    source_mp3->build_metadata_offset, src, bytes);
            source_mp3->build_metadata_offset += bytes;
            break;
        }
600
        /* copy all bytes except the last one, that way we
Karl Heyes's avatar
Karl Heyes committed
601 602 603
         * know a null byte terminates the message */
        memcpy (source_mp3->build_metadata + source_mp3->build_metadata_offset,
                src, metadata_remaining-1);
604

Karl Heyes's avatar
Karl Heyes committed
605 606 607
        /* overwrite metadata in the buffer */
        bytes -= metadata_remaining;
        memmove (src, src+metadata_remaining, bytes);
608

609 610 611
        /* assign metadata if it's greater than 1 byte, and the text has changed */
        if (source_mp3->build_metadata_len > 1 &&
                strcmp (source_mp3->build_metadata+1, source_mp3->metadata->data+1) != 0)
Karl Heyes's avatar
Karl Heyes committed
612 613 614 615 616
        {
            refbuf_t *meta = refbuf_new (source_mp3->build_metadata_len);
            memcpy (meta->data, source_mp3->build_metadata,
                    source_mp3->build_metadata_len);

617
            ICECAST_LOG_DEBUG("shoutcast metadata %.*s", 4080, meta->data+1);
Karl Heyes's avatar
Karl Heyes committed
618 619 620 621 622 623
            if (strncmp (meta->data+1, "StreamTitle=", 12) == 0)
            {
                filter_shoutcast_metadata (source, source_mp3->build_metadata,
                        source_mp3->build_metadata_len);
                refbuf_release (source_mp3->metadata);
                source_mp3->metadata = meta;
624
                source_mp3->inline_url = strstr (meta->data+1, "StreamUrl='");
Karl Heyes's avatar
Karl Heyes committed
625 626 627
            }
            else
            {
628
                ICECAST_LOG_ERROR("Incorrect metadata format, ending stream");
Karl Heyes's avatar
Karl Heyes committed
629 630
                source->running = 0;
                refbuf_release (refbuf);
631
                refbuf_release (meta);
Karl Heyes's avatar
Karl Heyes committed
632
                return NULL;
633 634
            }
        }
Karl Heyes's avatar
Karl Heyes committed
635 636
        source_mp3->offset = 0;
        source_mp3->build_metadata_len = 0;
637
    }
Karl Heyes's avatar
Karl Heyes committed
638 639 640 641 642
    /* the data we have just read may of just been metadata */
    if (refbuf->len == 0)
    {
        refbuf_release (refbuf);
        return NULL;
643
    }
Karl Heyes's avatar
Karl Heyes committed
644 645
    refbuf->associated = source_mp3->metadata;
    refbuf_addref (source_mp3->metadata);
646
    refbuf->sync_point = 1;
Michael Smith's avatar
Michael Smith committed
647

Karl Heyes's avatar
Karl Heyes committed
648
    return refbuf;
Michael Smith's avatar
Michael Smith committed
649 650
}

Karl Heyes's avatar
Karl Heyes committed
651 652

static int format_mp3_create_client_data(source_t *source, client_t *client)
653
{
654
    mp3_client_data *client_mp3 = calloc(1, sizeof(mp3_client_data));
655
    mp3_state *source_mp3 = source->format->_state;
656
    const char *metadata;
657 658 659 660
    /* the +-2 is for overwriting the last set of \r\n */
    unsigned remaining = 4096 - client->refbuf->len + 2;
    char *ptr = client->refbuf->data + client->refbuf->len - 2;
    int bytes;
661
    const char *useragent;
662

663
    if (client_mp3 == NULL)
Karl Heyes's avatar
Karl Heyes committed
664
        return -1;
665

666 667 668 669 670
    /* hack for flash player, it wants a length.  It has also been reported that the useragent
     * appears as MSIE if run in internet explorer */
    useragent = httpp_getvar (client->parser, "user-agent");
    if (httpp_getvar(client->parser, "x-flash-version") ||
            (useragent && strstr(useragent, "MSIE")))
671
    {
672
        bytes = snprintf (ptr, remaining, "Content-Length: 221183499\r\n");
673 674 675 676
        remaining -= bytes;
        ptr += bytes;
    }

677
    client->format_data = client_mp3;
Karl Heyes's avatar
Karl Heyes committed
678
    client->free_client_data = free_mp3_client_data;
679
    metadata = httpp_getvar(client->parser, "icy-metadata");
680 681
    if (metadata && atoi(metadata))
    {
682
        if (source_mp3->interval >= 0)
683 684 685
            client_mp3->interval = source_mp3->interval;
        else
            client_mp3->interval = ICY_METADATA_INTERVAL;
686
        if (client_mp3->interval)
687
        {
688 689 690 691 692 693 694
            bytes = snprintf (ptr, remaining, "icy-metaint:%u\r\n",
                    client_mp3->interval);
            if (bytes > 0)
            {
                remaining -= bytes;
                ptr += bytes;
            }
695
        }
696
    }
697 698 699
    bytes = snprintf (ptr, remaining, "\r\n");
    remaining -= bytes;
    ptr += bytes;
700

701
    client->refbuf->len = 4096 - remaining;
702

Karl Heyes's avatar
Karl Heyes committed
703
    return 0;
704
}
Michael Smith's avatar
Michael Smith committed
705

706 707 708 709 710 711 712 713

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


Karl Heyes's avatar
Karl Heyes committed
714 715 716 717 718 719
static void write_mp3_to_file (struct source_tag *source, refbuf_t *refbuf)
{
    if (refbuf->len == 0)
        return;
    if (fwrite (refbuf->data, 1, refbuf->len, source->dumpfile) < (size_t)refbuf->len)
    {
720
        ICECAST_LOG_WARN("Write to dump file failed, disabling");
Karl Heyes's avatar
Karl Heyes committed
721 722 723 724 725
        fclose (source->dumpfile);
        source->dumpfile = NULL;
    }
}