format_mp3.c 12.3 KB
Newer Older
1
/* -*- c-basic-offset: 4; -*- */
Michael Smith's avatar
Michael Smith committed
2
3
/* format_mp3.c
**
Michael Smith's avatar
Michael Smith committed
4
** format plugin for mp3
Michael Smith's avatar
Michael Smith committed
5
6
7
**
*/

8
9
10
11
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Michael Smith's avatar
Michael Smith committed
12
13
14
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Karl Heyes's avatar
Karl Heyes committed
15
16
17
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
Michael Smith's avatar
Michael Smith committed
18
19

#include "refbuf.h"
Michael Smith's avatar
Michael Smith committed
20
21
#include "source.h"
#include "client.h"
Michael Smith's avatar
Michael Smith committed
22
23
24

#include "stats.h"
#include "format.h"
Michael Smith's avatar
Michael Smith committed
25
26
27
28
29
30
#include "httpp/httpp.h"

#include "logging.h"

#include "format_mp3.h"

Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
31
32
33
#ifdef WIN32
#define strcasecmp stricmp
#define strncasecmp strnicmp
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
34
#define snprintf _snprintf
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
35
36
#endif

Michael Smith's avatar
Michael Smith committed
37
38
#define CATMODULE "format-mp3"

39
40
41
/* 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
42
#define ICY_METADATA_INTERVAL 16000
Michael Smith's avatar
Michael Smith committed
43

44
45
46
47
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);
Michael Smith's avatar
Michael Smith committed
48
49
50
51
52
53
54
55
static void *format_mp3_create_client_data(format_plugin_t *self,
        source_t *source, client_t *client);
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
56
   int use_metadata;
Michael Smith's avatar
Michael Smith committed
57
58
   int interval;
   int offset;
Michael Smith's avatar
Michael Smith committed
59
60
   int metadata_age;
   int metadata_offset;
Michael Smith's avatar
Michael Smith committed
61
} mp3_client_data;
Ed "oddsock" Zaleski's avatar
Ed "oddsock" Zaleski committed
62

63
format_plugin_t *format_mp3_get_plugin(http_parser_t *parser)
Michael Smith's avatar
Michael Smith committed
64
{
65
    char *metadata;
66
    format_plugin_t *plugin;
Michael Smith's avatar
Michael Smith committed
67
    mp3_state *state = calloc(1, sizeof(mp3_state));
Michael Smith's avatar
Michael Smith committed
68

69
    plugin = (format_plugin_t *)malloc(sizeof(format_plugin_t));
Michael Smith's avatar
Michael Smith committed
70

71
72
73
74
    plugin->type = FORMAT_TYPE_MP3;
    plugin->has_predata = 0;
    plugin->get_buffer = format_mp3_get_buffer;
    plugin->get_predata = format_mp3_get_predata;
Michael Smith's avatar
Michael Smith committed
75
    plugin->write_buf_to_client = format_mp3_write_buf_to_client;
76
    plugin->create_client_data = format_mp3_create_client_data;
Michael Smith's avatar
Michael Smith committed
77
    plugin->client_send_headers = format_mp3_send_headers;
78
    plugin->free_plugin = format_mp3_free_plugin;
79
    plugin->format_description = "MP3 audio";
Michael Smith's avatar
Michael Smith committed
80

81
    plugin->_state = state;
Michael Smith's avatar
Michael Smith committed
82

Michael Smith's avatar
Michael Smith committed
83
84
    state->metadata_age = 0;
    state->metadata = strdup("");
Michael Smith's avatar
Michael Smith committed
85
    thread_mutex_create(&(state->lock));
Michael Smith's avatar
Michael Smith committed
86

87
88
89
90
    metadata = httpp_getvar(parser, "icy-metaint");
    if(metadata)
        state->inline_metadata_interval = atoi(metadata);

91
    return plugin;
Michael Smith's avatar
Michael Smith committed
92
93
}

Michael Smith's avatar
Michael Smith committed
94
95
96
static int send_metadata(client_t *client, mp3_client_data *client_state,
        mp3_state *source_state)
{
97
    int free_meta = 0;
Michael Smith's avatar
Michael Smith committed
98
99
    int len_byte;
    int len;
100
    int ret = -1;
Michael Smith's avatar
Michael Smith committed
101
102
    unsigned char *buf;
    int source_age;
103
104
105
    char *fullmetadata = NULL;
    int  fullmetadata_size = 0;
    const char meta_fmt[] = "StreamTitle='';"; 
Michael Smith's avatar
Michael Smith committed
106

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
    do 
    {
        thread_mutex_lock (&(source_state->lock));
        if (source_state->metadata == NULL)
            break; /* Shouldn't be possible */

        if (source_state->metadata_raw)
        {
            fullmetadata_size = strlen (source_state->metadata);
            fullmetadata = source_state->metadata;
            if (fullmetadata_size > 4080)
            {
                fullmetadata_size = 4080;
            }
        }
        else
        {
            fullmetadata_size = strlen (source_state->metadata) + 
                sizeof (meta_fmt);

            if (fullmetadata_size > 4080)
            {
                fullmetadata_size = 4080;
            }
            fullmetadata = malloc (fullmetadata_size);
            if (fullmetadata == NULL)
                break;
134

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

140
        source_age = source_state->metadata_age;
141

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
        if (fullmetadata_size > 0 && source_age != client_state->metadata_age)
        {
            len_byte = (fullmetadata_size-1)/16 + 1; /* to give 1-255 */
            client_state->metadata_offset = 0;
        }
        else
            len_byte = 0;
        len = 1 + len_byte*16;
        buf = malloc (len);
        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
160

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

163
164
        /* 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
165

166
167
168
169
170
171
172
173
174
175
176
177
        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);
        if (free_meta)
            free (fullmetadata);
        return ret;
Michael Smith's avatar
Michael Smith committed
178

179
    } while (0);
Michael Smith's avatar
Michael Smith committed
180
181

    thread_mutex_unlock(&(source_state->lock));
182
183
184
    if (free_meta)
        free (fullmetadata);
    return -1;
Michael Smith's avatar
Michael Smith committed
185
186
}

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

Michael Smith's avatar
Michael Smith committed
198
199
200
201
202
203
204
205
206
207
208
        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;
        }
209
        else {
Michael Smith's avatar
Michael Smith committed
210
            ret = send_metadata(client, state, self->_state);
211
212
213
214
            if(ret > 0)
                client->con->sent_bytes += ret;
            ret = 0;
        }
215

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

    if(ret < 0) {
222
        if(sock_recoverable(sock_error())) {
Michael Smith's avatar
Michael Smith committed
223
224
225
226
227
228
229
230
231
232
233
            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
234
{
235
    /* free the plugin instance */
Michael Smith's avatar
Michael Smith committed
236
237
    mp3_state *state = self->_state;
    thread_mutex_destroy(&(state->lock));
Michael Smith's avatar
Michael Smith committed
238
239

    free(state->metadata);
Michael Smith's avatar
Michael Smith committed
240
    free(state);
241
    free(self);
Michael Smith's avatar
Michael Smith committed
242
243
}

Michael Smith's avatar
Michael Smith committed
244
245
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
246
{
247
    refbuf_t *refbuf;
248
249
    mp3_state *state = self->_state;

250
251
252
253
    /* 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
254
        return 0;
255

256
    if(state->inline_metadata_interval) {
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
290
291
292
        /* 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;
293

294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
                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);

319
320
                state->metadata_offset += readable;

321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
                data += readable;
                len -= readable;

                if(state->metadata_offset == state->metadata_length)
                {
                    if(state->metadata_length)
                    {
                        thread_mutex_lock(&(state->lock));
                        free(state->metadata);
                        state->metadata = state->metadata_buffer;
                        state->metadata_buffer = NULL;
                        state->metadata_age++;
                        state->metadata_raw = 1;
                        thread_mutex_unlock(&(state->lock));
                    }
336
337
338

                    state->offset = 0;
                    state->metadata_length = 0;
339
340
341
342
343
344
345
346
                }
            }
        }

        /* 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...
         */
347
348
349
        return 0;
    }
    else {
350
        /* Simple case - no metadata, just dump data directly to a buffer */
351
        refbuf = refbuf_new(len);
Michael Smith's avatar
Michael Smith committed
352

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

355
        *buffer = refbuf;
356
        return 0;
357
    }
Michael Smith's avatar
Michael Smith committed
358
359
}

Michael Smith's avatar
Michael Smith committed
360
static refbuf_queue_t *format_mp3_get_predata(format_plugin_t *self)
Michael Smith's avatar
Michael Smith committed
361
362
363
364
{
    return NULL;
}

Michael Smith's avatar
Michael Smith committed
365
366
367
368
369
370
371
372
373
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;

374
    metadata = httpp_getvar(client->parser, "icy-metadata");
Michael Smith's avatar
Michael Smith committed
375
    if(metadata)
Michael Smith's avatar
Michael Smith committed
376
        data->use_metadata = atoi(metadata)>0?1:0;
Michael Smith's avatar
Michael Smith committed
377
378

    return data;
379
}
Michael Smith's avatar
Michael Smith committed
380

Michael Smith's avatar
Michael Smith committed
381
382
383
static void format_mp3_send_headers(format_plugin_t *self,
        source_t *source, client_t *client)
{
384
385
    http_var_t *var;
    avl_node *node;
Michael Smith's avatar
Michael Smith committed
386
    int bytes;
387
    mp3_client_data *mp3data = client->format_data;
Michael Smith's avatar
Michael Smith committed
388
389
    
    client->respcode = 200;
Michael Smith's avatar
Michael Smith committed
390
    /* TODO: This may need to be ICY/1.0 for shoutcast-compatibility? */
Michael Smith's avatar
Michael Smith committed
391
392
393
394
395
396
397
    bytes = sock_write(client->con->sock, 
            "HTTP/1.0 200 OK\r\n" 
            "Content-Type: %s\r\n", 
            format_get_mimetype(source->format->type));

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

398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
    /* iterate through source http headers and send to client */
    avl_tree_rlock(source->parser->vars);
    node = avl_get_first(source->parser->vars);
    while (node) {
        var = (http_var_t *)node->key;
	if (!strcasecmp(var->name, "ice-audio-info")) {
	    /* convert ice-audio-info to icy-br */
	    char *brfield;
	    unsigned int bitrate;

	    brfield = strstr(var->value, "bitrate=");
	    if (brfield && sscanf(var->value, "bitrate=%u", &bitrate)) {
		bytes = sock_write(client->con->sock, "icy-br:%u\r\n", bitrate);
		if (bytes > 0)
		    client->con->sent_bytes += bytes;
	    }
	} else if (strcasecmp(var->name, "ice-password") &&
	    strcasecmp(var->name, "icy-metaint") &&
	    (!strncasecmp("ice-", var->name, 4) ||
	     !strncasecmp("icy-", var->name, 4))) {
	    bytes = sock_write(client->con->sock, "icy%s:%s\r\n",
			       var->name + 3, var->value);
            if (bytes > 0)
		client->con->sent_bytes += bytes;
        }
        node = avl_get_next(node);
    }
    avl_tree_unlock(source->parser->vars);
Michael Smith's avatar
Michael Smith committed
426

427
    if (mp3data->use_metadata) {
428
        int bytes = sock_write(client->con->sock, "icy-metaint:%d\r\n", 
Michael Smith's avatar
Michael Smith committed
429
430
431
432
                ICY_METADATA_INTERVAL);
        if(bytes > 0)
            client->con->sent_bytes += bytes;
    }
433
434
435
436

    bytes = sock_write(client->con->sock,
		       "Server: %s\r\n", ICECAST_VERSION_STRING);
    if (bytes > 0)
437
        client->con->sent_bytes += bytes;
Michael Smith's avatar
Michael Smith committed
438
439
}