format_mp3.c 19.5 KB
Newer Older
1
/* -*- c-basic-offset: 4; -*- */
2
3
4
5
6
7
8
9
10
11
12
13
/* 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).
 */

Michael Smith's avatar
Michael Smith committed
14
15
/* format_mp3.c
**
Michael Smith's avatar
Michael Smith committed
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"
Michael Smith's avatar
Michael Smith committed
32
33
#include "source.h"
#include "client.h"
Michael Smith's avatar
Michael Smith committed
34
35
36

#include "stats.h"
#include "format.h"
Michael Smith's avatar
Michael Smith committed
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

Michael Smith's avatar
Michael Smith committed
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?
 */
Michael Smith's avatar
Michael Smith committed
54
#define ICY_METADATA_INTERVAL 16000
Michael Smith's avatar
Michael Smith committed
55

56
static void format_mp3_free_plugin(format_plugin_t *self);
Karl Heyes's avatar
Karl Heyes committed
57
58
59
60
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);
61
static void free_mp3_client_data (client_t *client);
Karl Heyes's avatar
Karl Heyes committed
62
static int format_mp3_write_buf_to_client(format_plugin_t *self, client_t *client);
Michael Smith's avatar
Michael Smith committed
63
64
static void format_mp3_send_headers(format_plugin_t *self, 
        source_t *source, client_t *client);
Karl Heyes's avatar
Karl Heyes committed
65
static void write_mp3_to_file (struct source_tag *source, refbuf_t *refbuf);
66
67
static void mp3_set_tag (format_plugin_t *plugin, char *tag, char *value);
static void format_mp3_apply_settings(client_t *client, format_plugin_t *format, mount_proxy *mount);
Karl Heyes's avatar
Karl Heyes committed
68

Michael Smith's avatar
Michael Smith committed
69
70

typedef struct {
71
   unsigned int interval;
Michael Smith's avatar
Michael Smith committed
72
   int metadata_offset;
Karl Heyes's avatar
Karl Heyes committed
73
74
75
   unsigned int since_meta_block;
   int in_metadata;
   refbuf_t *associated;
Michael Smith's avatar
Michael Smith committed
76
} mp3_client_data;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
77

Karl Heyes's avatar
Karl Heyes committed
78
int format_mp3_get_plugin (source_t *source)
Michael Smith's avatar
Michael Smith committed
79
{
80
    char *metadata;
81
    format_plugin_t *plugin;
Michael Smith's avatar
Michael Smith committed
82
    mp3_state *state = calloc(1, sizeof(mp3_state));
Karl Heyes's avatar
Karl Heyes committed
83
    refbuf_t *meta;
Michael Smith's avatar
Michael Smith committed
84

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

87
    plugin->type = FORMAT_TYPE_GENERIC;
Karl Heyes's avatar
Karl Heyes committed
88
    plugin->get_buffer = mp3_get_no_meta;
Michael Smith's avatar
Michael Smith committed
89
    plugin->write_buf_to_client = format_mp3_write_buf_to_client;
Karl Heyes's avatar
Karl Heyes committed
90
    plugin->write_buf_to_file = write_mp3_to_file;
91
    plugin->create_client_data = format_mp3_create_client_data;
Michael Smith's avatar
Michael Smith committed
92
    plugin->client_send_headers = format_mp3_send_headers;
93
    plugin->free_plugin = format_mp3_free_plugin;
94
    plugin->set_tag = mp3_set_tag;
95
    plugin->apply_settings = format_mp3_apply_settings;
96
97
98
99
100
101

    plugin->contenttype = httpp_getvar (source->parser, "content-type");
    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
102

103
    plugin->_state = state;
Michael Smith's avatar
Michael Smith committed
104

105
106
107
108
109
    /* initial metadata needs to be blank for sending to clients and for
       comparing with new metadata */
    meta = refbuf_new (2);
    memcpy (meta->data, "\0\0", 2);
    meta->len = 1;
Karl Heyes's avatar
Karl Heyes committed
110
    state->metadata = meta;
111
    state->interval = -1;
Michael Smith's avatar
Michael Smith committed
112

Karl Heyes's avatar
Karl Heyes committed
113
114
115
116
    metadata = httpp_getvar (source->parser, "icy-metaint");
    if (metadata)
    {
        state->inline_metadata_interval = atoi (metadata);
117
118
119
120
        if (state->inline_metadata_interval > 0)
        {
            state->offset = 0;
            plugin->get_buffer = mp3_get_filter_meta;
121
            state->interval = state->inline_metadata_interval;
122
        }
Karl Heyes's avatar
Karl Heyes committed
123
124
125
    }
    source->format = plugin;
    thread_mutex_create (&state->url_lock);
126

Karl Heyes's avatar
Karl Heyes committed
127
    return 0;
Michael Smith's avatar
Michael Smith committed
128
129
}

130

131
static void mp3_set_tag (format_plugin_t *plugin, char *tag, char *value)
Michael Smith's avatar
Michael Smith committed
132
{
Karl Heyes's avatar
Karl Heyes committed
133
134
135
136
137
138
139
140
141
142
143
144
145
    mp3_state *source_mp3 = plugin->_state;
    unsigned int len;
    const char meta[] = "StreamTitle='";
    int size = sizeof (meta) + 1;

    if (tag==NULL || value == NULL)
        return;

    len = strlen (value)+1;
    size += len;
    /* protect against multiple updaters */
    thread_mutex_lock (&source_mp3->url_lock);
    if (strcmp (tag, "title") == 0 || strcmp (tag, "song") == 0)
146
    {
Karl Heyes's avatar
Karl Heyes committed
147
148
        char *p = strdup (value);
        if (p)
149
        {
Karl Heyes's avatar
Karl Heyes committed
150
151
152
153
154
            free (source_mp3->url_title);
            free (source_mp3->url_artist);
            source_mp3->url_artist = NULL;
            source_mp3->url_title = p;
            source_mp3->update_metadata = 1;
155
        }
Karl Heyes's avatar
Karl Heyes committed
156
157
158
159
160
161
162
163
164
165
166
167
168
    }
    else if (strcmp (tag, "artist") == 0)
    {
        char *p = strdup (value);
        if (p)
        {
            free (source_mp3->url_artist);
            source_mp3->url_artist = p;
            source_mp3->update_metadata = 1;
        }
    }
    thread_mutex_unlock (&source_mp3->url_lock);
}
169
170


Karl Heyes's avatar
Karl Heyes committed
171
172
173
174
175
176
static void filter_shoutcast_metadata (source_t *source, char *metadata, unsigned int meta_len)
{
    if (metadata)
    {
        char *end, *p;
        int len;
177

Karl Heyes's avatar
Karl Heyes committed
178
        do
179
        {
Karl Heyes's avatar
Karl Heyes committed
180
181
182
183
184
185
186
187
188
189
            metadata++;
            if (strncmp (metadata, "StreamTitle='", 13))
                break;
            if ((end = strstr (metadata, "\';")) == NULL)
                break;
            len = (end - metadata) - 13;
            p = calloc (1, len+1);
            if (p)
            {
                memcpy (p, metadata+13, len);
190
                logging_playlist (source->mount, p, source->listeners);
Karl Heyes's avatar
Karl Heyes committed
191
192
193
194
195
196
197
                stats_event (source->mount, "title", p);
                yp_touch (source->mount);
                free (p);
            }
        } while (0);
    }
}
198

Michael Smith's avatar
Michael Smith committed
199

200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
static void format_mp3_apply_settings (client_t *client, format_plugin_t *format, mount_proxy *mount)
{
    mp3_state *source_mp3 = format->_state;

    if (mount->mp3_meta_interval <= 0)
    {
        char *metadata = httpp_getvar (client->parser, "icy-metaint");
        source_mp3->interval = -1;
        if (metadata)
        {
            int interval = atoi (metadata);
            if (interval > 0)
                source_mp3->interval = interval;
        }
    }
    else
        source_mp3->interval = mount->mp3_meta_interval;
    DEBUG2 ("mp3 interval %d, %d", mount->mp3_meta_interval, source_mp3->interval);
}


Karl Heyes's avatar
Karl Heyes committed
221
222
223
/* called from the source thread when the metadata has been updated.
 * The artist title are checked and made ready for clients to send
 */
224
static void mp3_set_title (source_t *source)
Karl Heyes's avatar
Karl Heyes committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
{
    const char meta[] = "StreamTitle='";
    int size;
    unsigned char len_byte;
    refbuf_t *p;
    unsigned int len = sizeof(meta) + 2; /* the StreamTitle, quotes, ; and null */
    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 */
    if (source_mp3->url_artist)
        len += strlen (source_mp3->url_artist);
    if (source_mp3->url_title)
        len += strlen (source_mp3->url_title);
    if (source_mp3->url_artist && source_mp3->url_title)
        len += 3;
#define MAX_META_LEN 255*16
    if (len > MAX_META_LEN)
    {
        thread_mutex_unlock (&source_mp3->url_lock);
        WARN1 ("Metadata too long at %d chars", len);
        return;
    }
    /* work out the metadata len byte */
    len_byte = (len-1) / 16 + 1;
Michael Smith's avatar
Michael Smith committed
252

Karl Heyes's avatar
Karl Heyes committed
253
254
    /* 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
255

Karl Heyes's avatar
Karl Heyes committed
256
257
258
259
    p = refbuf_new (size);
    if (p)
    {
        mp3_state *source_mp3 = source->format->_state;
Michael Smith's avatar
Michael Smith committed
260

Karl Heyes's avatar
Karl Heyes committed
261
262
263
264
265
266
267
268
        memset (p->data, '\0', size);
        if (source_mp3->url_artist && source_mp3->url_title)
            snprintf (p->data, size, "%c%s%s - %s';", len_byte, meta,
                    source_mp3->url_artist, source_mp3->url_title);
        else
            snprintf (p->data, size, "%c%s%s';", len_byte, meta,
                    source_mp3->url_title);
        filter_shoutcast_metadata (source, p->data, size);
Michael Smith's avatar
Michael Smith committed
269

Karl Heyes's avatar
Karl Heyes committed
270
271
272
273
        refbuf_release (source_mp3->metadata);
        source_mp3->metadata = p;
    }
    thread_mutex_unlock (&source_mp3->url_lock);
Michael Smith's avatar
Michael Smith committed
274
275
}

Karl Heyes's avatar
Karl Heyes committed
276
277
278
279
280
281

/* 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
 */
static int send_mp3_metadata (client_t *client, refbuf_t *associated)
Michael Smith's avatar
Michael Smith committed
282
{
283
    int ret = 0;
Karl Heyes's avatar
Karl Heyes committed
284
285
286
287
288
289
290
    unsigned char *metadata;
    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
     */
291
    if (associated && associated != client_mp3->associated)
Karl Heyes's avatar
Karl Heyes committed
292
    {
293
294
        metadata = associated->data + client_mp3->metadata_offset;
        meta_len = associated->len - client_mp3->metadata_offset;
Karl Heyes's avatar
Karl Heyes committed
295
296
297
    }
    else
    {
298
299
        metadata = "\0";
        meta_len = 1;
Karl Heyes's avatar
Karl Heyes committed
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
    }
    ret = client_send_bytes (client, metadata, meta_len);

    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
 */
static int format_mp3_write_buf_to_client (format_plugin_t *self, client_t *client) 
{
    int ret, written = 0;
    mp3_client_data *client_mp3 = client->format_data;
    refbuf_t *refbuf = client->refbuf;
    char *buf;
    unsigned int len;

    if (refbuf->next == NULL && client->pos == refbuf->len)
        return 0;

    /* move to the next buffer if we have finished with the current one */
    if (refbuf->next && client->pos == refbuf->len)
Michael Smith's avatar
Michael Smith committed
337
    {
Karl Heyes's avatar
Karl Heyes committed
338
339
340
        client_set_queue (client, refbuf->next);
        refbuf = client->refbuf;
    }
Michael Smith's avatar
Michael Smith committed
341

Karl Heyes's avatar
Karl Heyes committed
342
343
    buf = refbuf->data + client->pos;
    len = refbuf->len - client->pos;
Michael Smith's avatar
Michael Smith committed
344

Karl Heyes's avatar
Karl Heyes committed
345
346
347
348
349
350
351
    do
    {
        /* send any unwritten metadata to the client */
        if (client_mp3->in_metadata)
        {
            refbuf_t *associated = refbuf->associated;
            ret = send_mp3_metadata (client, associated);
Michael Smith's avatar
Michael Smith committed
352

Karl Heyes's avatar
Karl Heyes committed
353
354
355
            if (client_mp3->in_metadata)
                break;
            written += ret;
Michael Smith's avatar
Michael Smith committed
356
        }
Karl Heyes's avatar
Karl Heyes committed
357
        /* see if we need to send the current metadata to the client */
358
        if (client_mp3->interval)
Karl Heyes's avatar
Karl Heyes committed
359
        {
360
            unsigned int remaining = client_mp3->interval -
Karl Heyes's avatar
Karl Heyes committed
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
                client_mp3->since_meta_block;

            /* sending the metadata block */
            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;
                }
                ret = send_mp3_metadata (client, refbuf->associated);
                if (client_mp3->in_metadata)
                    break;
                written += ret;
                /* change buf and len */
                buf += remaining;
                len -= remaining;
            }
388
        }
Karl Heyes's avatar
Karl Heyes committed
389
390
391
392
        /* write any mp3, maybe after the metadata block */
        if (len)
        {
            ret = client_send_bytes (client, buf, len);
393

Karl Heyes's avatar
Karl Heyes committed
394
395
396
397
398
399
400
401
402
403
404
            if (ret > 0)
            {
                client_mp3->since_meta_block += ret;
                client->pos += ret;
            }
            if (ret < (int)len)
                break;
            written += ret;
        }
        ret = 0;
    } while (0);
Michael Smith's avatar
Michael Smith committed
405

Karl Heyes's avatar
Karl Heyes committed
406
407
408
    if (ret > 0)
        written += ret;
    return written;
Michael Smith's avatar
Michael Smith committed
409
410
411
}

static void format_mp3_free_plugin(format_plugin_t *self)
Michael Smith's avatar
Michael Smith committed
412
{
413
    /* free the plugin instance */
Michael Smith's avatar
Michael Smith committed
414
    mp3_state *state = self->_state;
Michael Smith's avatar
Michael Smith committed
415

Karl Heyes's avatar
Karl Heyes committed
416
417
418
419
    thread_mutex_destroy (&state->url_lock);
    free (state->url_artist);
    free (state->url_title);
    refbuf_release (state->metadata);
Michael Smith's avatar
Michael Smith committed
420
    free(state);
421
    free(self);
Michael Smith's avatar
Michael Smith committed
422
423
}

Karl Heyes's avatar
Karl Heyes committed
424
425
426

/* read an mp3 stream which does not have shoutcast style metadata */
static refbuf_t *mp3_get_no_meta (source_t *source)
Michael Smith's avatar
Michael Smith committed
427
{
Karl Heyes's avatar
Karl Heyes committed
428
    int bytes;
429
    refbuf_t *refbuf;
Karl Heyes's avatar
Karl Heyes committed
430
    mp3_state *source_mp3 = source->format->_state;
431
    format_plugin_t *format = source->format;
432

Karl Heyes's avatar
Karl Heyes committed
433
434
    if ((refbuf = refbuf_new (2048)) == NULL)
        return NULL;
435

436
437
    bytes = client_read_bytes (source->client, refbuf->data, 2048);
    if (bytes < 0)
Karl Heyes's avatar
Karl Heyes committed
438
439
440
441
    {
        refbuf_release (refbuf);
        return NULL;
    }
442
    format->read_bytes += bytes;
Karl Heyes's avatar
Karl Heyes committed
443
444
445
446
447
448
449
450
451
452
    if (source_mp3->update_metadata)
    {
        mp3_set_title (source);
        source_mp3->update_metadata = 0;
    }
    if (bytes > 0)
    {
        refbuf->len  = bytes;
        refbuf->associated = source_mp3->metadata;
        refbuf_addref (source_mp3->metadata);
453
        refbuf->sync_point = 1;
Karl Heyes's avatar
Karl Heyes committed
454
455
456
        return refbuf;
    }
    refbuf_release (refbuf);
457

Karl Heyes's avatar
Karl Heyes committed
458
459
    return NULL;
}
460
461


Karl Heyes's avatar
Karl Heyes committed
462
463
464
465
466
467
468
469
470
/* 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
 */
static refbuf_t *mp3_get_filter_meta (source_t *source)
{
    refbuf_t *refbuf;
    format_plugin_t *plugin = source->format;
    mp3_state *source_mp3 = plugin->_state;
471
    format_plugin_t *format = source->format;
Karl Heyes's avatar
Karl Heyes committed
472
473
474
    unsigned char *src;
    unsigned int bytes, mp3_block;
    int ret;
475

Karl Heyes's avatar
Karl Heyes committed
476
477
    refbuf = refbuf_new (2048);
    src = refbuf->data;
478

479
480
    ret = client_read_bytes (source->client, refbuf->data, 2048);
    if (ret < 0)
Karl Heyes's avatar
Karl Heyes committed
481
482
483
484
    {
        refbuf_release (refbuf);
        return NULL;
    }
485
    format->read_bytes += ret;
Karl Heyes's avatar
Karl Heyes committed
486
487
488
489
490
491
492
493
494
495
496
    if (source_mp3->update_metadata)
    {
        mp3_set_title (source);
        source_mp3->update_metadata = 0;
    }
    /* fill the buffer with the read data */
    bytes = (unsigned int)ret;
    refbuf->len = 0;
    while (bytes > 0)
    {
        unsigned int metadata_remaining;
497

Karl Heyes's avatar
Karl Heyes committed
498
        mp3_block = source_mp3->inline_metadata_interval - source_mp3->offset;
499

Karl Heyes's avatar
Karl Heyes committed
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
        /* 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;
        }
517

Karl Heyes's avatar
Karl Heyes committed
518
519
520
521
522
523
524
525
        /* 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);
        }
526

Karl Heyes's avatar
Karl Heyes committed
527
528
529
530
531
532
533
534
535
536
537
538
539
540
        /* 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;
        }
        /* copy all bytes except the last one, that way we 
         * know a null byte terminates the message */
        memcpy (source_mp3->build_metadata + source_mp3->build_metadata_offset,
                src, metadata_remaining-1);
541

Karl Heyes's avatar
Karl Heyes committed
542
543
544
        /* overwrite metadata in the buffer */
        bytes -= metadata_remaining;
        memmove (src, src+metadata_remaining, bytes);
545

546
547
548
        /* 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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
        {
            refbuf_t *meta = refbuf_new (source_mp3->build_metadata_len);
            memcpy (meta->data, source_mp3->build_metadata,
                    source_mp3->build_metadata_len);

            DEBUG1("shoutcast metadata %.4080s", meta->data+1);
            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;
            }
            else
            {
                ERROR0 ("Incorrect metadata format, ending stream");
                source->running = 0;
                refbuf_release (refbuf);
                return NULL;
568
569
            }
        }
Karl Heyes's avatar
Karl Heyes committed
570
571
        source_mp3->offset = 0;
        source_mp3->build_metadata_len = 0;
572
    }
Karl Heyes's avatar
Karl Heyes committed
573
574
575
576
577
    /* the data we have just read may of just been metadata */
    if (refbuf->len == 0)
    {
        refbuf_release (refbuf);
        return NULL;
578
    }
Karl Heyes's avatar
Karl Heyes committed
579
580
    refbuf->associated = source_mp3->metadata;
    refbuf_addref (source_mp3->metadata);
581
    refbuf->sync_point = 1;
Michael Smith's avatar
Michael Smith committed
582

Karl Heyes's avatar
Karl Heyes committed
583
    return refbuf;
Michael Smith's avatar
Michael Smith committed
584
585
}

Karl Heyes's avatar
Karl Heyes committed
586
587

static int format_mp3_create_client_data(source_t *source, client_t *client)
Michael Smith's avatar
Michael Smith committed
588
{
589
    mp3_client_data *client_mp3 = calloc(1,sizeof(mp3_client_data));
Michael Smith's avatar
Michael Smith committed
590
    char *metadata;
591
    mp3_state *source_mp3 = source->format->_state;
Michael Smith's avatar
Michael Smith committed
592

593
    if (client_mp3 == NULL)
Karl Heyes's avatar
Karl Heyes committed
594
        return -1;
Michael Smith's avatar
Michael Smith committed
595

596
    client->format_data = client_mp3;
Karl Heyes's avatar
Karl Heyes committed
597
    client->free_client_data = free_mp3_client_data;
598
    metadata = httpp_getvar(client->parser, "icy-metadata");
599
600
601
602
603
604
605
606
    if (metadata && atoi(metadata))
    {
        if (source_mp3->interval > 0)
            client_mp3->interval = source_mp3->interval;
        else
            client_mp3->interval = ICY_METADATA_INTERVAL;
    }

Michael Smith's avatar
Michael Smith committed
607

Karl Heyes's avatar
Karl Heyes committed
608
    return 0;
609
}
Michael Smith's avatar
Michael Smith committed
610

611
612
613
614
615
616
617
618

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


Michael Smith's avatar
Michael Smith committed
619
620
621
622
static void format_mp3_send_headers(format_plugin_t *self,
        source_t *source, client_t *client)
{
    int bytes;
623
624
    char *content_length;

625
    mp3_client_data *mp3data = client->format_data;
Michael Smith's avatar
Michael Smith committed
626
627
    
    client->respcode = 200;
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644

    /* This little bit of code is for compatability with
       flash mp3 streaming. Flash requires a content-length
       in order for it to stream mp3s, and so based off a
       trial and error effort, the following number was derived.
       It is the largest content-length that we can send, anything
       larger causes flash streaming not to work. Note that it
       is also possible that other flash-based players may not
       send this request header (x-flash-version), but given the
       sampleset I had access to, this should suffice. */
    if (httpp_getvar(client->parser, "x-flash-version")) {
        content_length = "Content-Length: 347122319\r\n";
    }
    else {
        content_length = "";
    }

Michael Smith's avatar
Michael Smith committed
645
    /* TODO: This may need to be ICY/1.0 for shoutcast-compatibility? */
Michael Smith's avatar
Michael Smith committed
646
647
    bytes = sock_write(client->con->sock, 
            "HTTP/1.0 200 OK\r\n" 
648
649
            "Content-Type: %s\r\n"
            "%s", 
650
            source->format->contenttype,
651
            content_length);
Michael Smith's avatar
Michael Smith committed
652

Karl Heyes's avatar
Karl Heyes committed
653
654
    if (bytes > 0)
        client->con->sent_bytes += bytes;
Michael Smith's avatar
Michael Smith committed
655

656
    if (mp3data->interval)
Karl Heyes's avatar
Karl Heyes committed
657
    {
658
        int bytes = sock_write(client->con->sock, "icy-metaint:%d\r\n", 
659
                mp3data->interval);
Michael Smith's avatar
Michael Smith committed
660
661
662
        if(bytes > 0)
            client->con->sent_bytes += bytes;
    }
Karl Heyes's avatar
Karl Heyes committed
663
    format_send_general_headers(self, source, client);
Michael Smith's avatar
Michael Smith committed
664
665
}

Karl Heyes's avatar
Karl Heyes committed
666
667
668
669
670
671
672
673
674
675
676
677
678

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)
    {
        WARN0 ("Write to dump file failed, disabling");
        fclose (source->dumpfile);
        source->dumpfile = NULL;
    }
}