format_vorbis.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).
 */

Jack Moffitt's avatar
Jack Moffitt committed
13 14 15 16 17 18
/* format_vorbis.c
**
** format plugin for vorbis
**
*/

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

Jack Moffitt's avatar
Jack Moffitt committed
23 24 25 26 27 28 29 30
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <ogg/ogg.h>
#include <vorbis/codec.h>

#include "refbuf.h"
31 32
#include "source.h"
#include "client.h"
Jack Moffitt's avatar
Jack Moffitt committed
33

Michael Smith's avatar
Michael Smith committed
34
#include "stats.h"
Jack Moffitt's avatar
Jack Moffitt committed
35 36
#include "format.h"

37 38 39
#define CATMODULE "format-vorbis"
#include "logging.h"

Michael Smith's avatar
Michael Smith committed
40 41
#define MAX_HEADER_PAGES 10

Jack Moffitt's avatar
Jack Moffitt committed
42 43
typedef struct _vstate_tag
{
44 45 46 47 48 49 50 51
    ogg_sync_state oy;
    ogg_stream_state os;
    vorbis_info vi;
    vorbis_comment vc;

    ogg_page og;
    unsigned long serialno;
    int header;
Karl Heyes's avatar
Karl Heyes committed
52 53 54
    refbuf_t *file_headers;
    refbuf_t *header_pages;
    refbuf_t *header_pages_tail;
55
    int packets;
Jack Moffitt's avatar
Jack Moffitt committed
56 57
} vstate_t;

Karl Heyes's avatar
Karl Heyes committed
58 59 60 61 62 63 64 65 66
struct client_vorbis
{
    refbuf_t *headers;
    refbuf_t *header_page;
    unsigned int pos;
    int processing_headers;
};


67
static void format_vorbis_free_plugin(format_plugin_t *self);
Karl Heyes's avatar
Karl Heyes committed
68 69
static refbuf_t *format_vorbis_get_buffer (source_t *source);
static int format_vorbis_create_client_data (source_t *source, client_t *client);
70 71
static void format_vorbis_send_headers(format_plugin_t *self,
        source_t *source, client_t *client);
Karl Heyes's avatar
Karl Heyes committed
72 73
static int write_buf_to_client (format_plugin_t *self, client_t *client);
static void write_ogg_to_file (struct source_tag *source, refbuf_t *refbuf);
Jack Moffitt's avatar
Jack Moffitt committed
74

Karl Heyes's avatar
Karl Heyes committed
75 76

int format_vorbis_get_plugin(source_t *source)
Jack Moffitt's avatar
Jack Moffitt committed
77
{
78 79
    format_plugin_t *plugin;
    vstate_t *state;
Jack Moffitt's avatar
Jack Moffitt committed
80

81
    plugin = (format_plugin_t *)malloc(sizeof(format_plugin_t));
82

83
    plugin->type = FORMAT_TYPE_VORBIS;
Karl Heyes's avatar
Karl Heyes committed
84
    plugin->write_buf_to_file = write_ogg_to_file;
85
    plugin->get_buffer = format_vorbis_get_buffer;
Karl Heyes's avatar
Karl Heyes committed
86
    plugin->write_buf_to_client = write_buf_to_client;
87
    plugin->create_client_data = format_vorbis_create_client_data;
88
    plugin->client_send_headers = format_vorbis_send_headers;
89
    plugin->free_plugin = format_vorbis_free_plugin;
90
    plugin->contenttype = "application/ogg";
Jack Moffitt's avatar
Jack Moffitt committed
91

92 93
    state = (vstate_t *)calloc(1, sizeof(vstate_t));
    ogg_sync_init(&state->oy);
Jack Moffitt's avatar
Jack Moffitt committed
94

95
    plugin->_state = (void *)state;
Karl Heyes's avatar
Karl Heyes committed
96
    source->format = plugin;
Jack Moffitt's avatar
Jack Moffitt committed
97

Karl Heyes's avatar
Karl Heyes committed
98
    return 0;
Jack Moffitt's avatar
Jack Moffitt committed
99 100
}

101 102
void format_vorbis_free_plugin(format_plugin_t *self)
{
103
    vstate_t *state = (vstate_t *)self->_state;
Karl Heyes's avatar
Karl Heyes committed
104
    refbuf_t *header = state->header_pages;
105 106 107 108

    /* free memory associated with this plugin instance */

    /* free state memory */
Karl Heyes's avatar
Karl Heyes committed
109 110 111 112 113 114
    while (header)
    {
        refbuf_t *to_release = header;
        header = header->next;
        refbuf_release (to_release);
    }
115 116 117 118 119 120 121 122 123
    ogg_sync_clear(&state->oy);
    ogg_stream_clear(&state->os);
    vorbis_comment_clear(&state->vc);
    vorbis_info_clear(&state->vi);
    
    free(state);

    /* free the plugin instance */
    free(self);
124 125
}

Karl Heyes's avatar
Karl Heyes committed
126
static refbuf_t *format_vorbis_get_buffer (source_t *source)
Jack Moffitt's avatar
Jack Moffitt committed
127
{
Karl Heyes's avatar
Karl Heyes committed
128
    int result;
129
    ogg_packet op;
130 131 132 133
    char *title_tag;
    char *artist_tag;
    char *metadata = NULL;
    int   metadata_len = 0;
Karl Heyes's avatar
Karl Heyes committed
134 135 136 137
    refbuf_t *refbuf, *header;
    char *data;
    format_plugin_t *self = source->format;
    int bytes;
138
    vstate_t *state = (vstate_t *)self->_state;
Jack Moffitt's avatar
Jack Moffitt committed
139

Karl Heyes's avatar
Karl Heyes committed
140 141 142 143 144 145 146 147 148 149 150
    data = ogg_sync_buffer (&state->oy, 4096);

    bytes = sock_read_bytes (source->con->sock, data, 4096);
    if (bytes < 0)
    {
        if (sock_recoverable (sock_error()))
            return NULL;
        WARN0 ("source connection has died");
        ogg_sync_wrote (&state->oy, 0);
        source->running = 0;
        return NULL;
151
    }
Karl Heyes's avatar
Karl Heyes committed
152 153 154 155 156 157 158 159
    if (bytes == 0)
    {
        INFO1 ("End of Stream %s", source->mount);
        ogg_sync_wrote (&state->oy, 0);
        source->running = 0;
        return NULL;
    }
    ogg_sync_wrote (&state->oy, bytes);
160 161 162 163 164 165 166 167

    refbuf = NULL;
    if (ogg_sync_pageout(&state->oy, &state->og) == 1) {
        refbuf = refbuf_new(state->og.header_len + state->og.body_len);
        memcpy(refbuf->data, state->og.header, state->og.header_len);
        memcpy(&refbuf->data[state->og.header_len], state->og.body, state->og.body_len);

        if (state->serialno != ogg_page_serialno(&state->og)) {
Karl Heyes's avatar
Karl Heyes committed
168
            DEBUG0("new stream");
169 170 171 172
            /* this is a new logical bitstream */
            state->header = 0;
            state->packets = 0;

173
            /* Clear old stuff. Rarely but occasionally needed. */
Karl Heyes's avatar
Karl Heyes committed
174 175 176 177 178 179 180 181
            header = state->header_pages;
            while (header)
            {
                refbuf_t *to_release = header;
                DEBUG0 ("clearing out header page");
                header = header->next;
                refbuf_release (to_release);
            }
182 183 184
            ogg_stream_clear(&state->os);
            vorbis_comment_clear(&state->vc);
            vorbis_info_clear(&state->vi);
Karl Heyes's avatar
Karl Heyes committed
185 186
            state->header_pages = NULL;
            state->header_pages_tail = NULL;
Jack Moffitt's avatar
Jack Moffitt committed
187

188 189 190 191 192
            state->serialno = ogg_page_serialno(&state->og);
            ogg_stream_init(&state->os, state->serialno);
            vorbis_info_init(&state->vi);
            vorbis_comment_init(&state->vc);
        }
Jack Moffitt's avatar
Jack Moffitt committed
193

194
        if (state->header >= 0) {
Michael Smith's avatar
Michael Smith committed
195 196 197 198
            /* FIXME: In some streams (non-vorbis ogg streams), this could get
             * extras pages beyond the header. We need to collect the pages
             * here anyway, but they may have to be discarded later.
             */
Karl Heyes's avatar
Karl Heyes committed
199
            DEBUG1 ("header %d", state->header);
200 201 202 203 204 205
            if (ogg_page_granulepos(&state->og) <= 0) {
                state->header++;
            } else {
                /* we're done caching headers */
                state->header = -1;

Karl Heyes's avatar
Karl Heyes committed
206
                DEBUG0 ("doing stats");
207
                /* put known comments in the stats */
208 209
                title_tag = vorbis_comment_query(&state->vc, "TITLE", 0);
                if (title_tag) stats_event(source->mount, "title", title_tag);
Karl Heyes's avatar
Karl Heyes committed
210
                else stats_event(source->mount, "title", "unknown");
211 212
                artist_tag = vorbis_comment_query(&state->vc, "ARTIST", 0);
                if (artist_tag) stats_event(source->mount, "artist", artist_tag);
Karl Heyes's avatar
Karl Heyes committed
213
                else stats_event(source->mount, "artist", "unknown");
214

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
                metadata = NULL;
                if (artist_tag) {
                    if (title_tag) {
                        metadata_len = strlen(artist_tag) + strlen(title_tag) +
                                       strlen(" - ") + 1;
                        metadata = (char *)calloc(1, metadata_len);
                        sprintf(metadata, "%s - %s", artist_tag, title_tag);
                    }
                    else {
                        metadata_len = strlen(artist_tag) + 1;
                        metadata = (char *)calloc(1, metadata_len);
                        sprintf(metadata, "%s", artist_tag);
                    }
                }
                else {
                    if (title_tag) {
                        metadata_len = strlen(title_tag) + 1;
                        metadata = (char *)calloc(1, metadata_len);
                        sprintf(metadata, "%s", title_tag);
                    }
                }
                if (metadata) {
                    logging_playlist(source->mount, metadata, source->listeners);
                    free(metadata);
                    metadata = NULL;
                }
241 242 243 244
                /* don't need these now */
                ogg_stream_clear(&state->os);
                vorbis_comment_clear(&state->vc);
                vorbis_info_clear(&state->vi);
245

Karl Heyes's avatar
Karl Heyes committed
246
                yp_touch (source->mount);
247 248 249 250 251
            }
        }

        /* cache header pages */
        if (state->header > 0 && state->packets < 3) {
Karl Heyes's avatar
Karl Heyes committed
252 253 254 255
            /* build a list of headers pages for attaching */
            if (state->header_pages_tail)
                state->header_pages_tail->next = refbuf;
            state->header_pages_tail = refbuf;
256

Karl Heyes's avatar
Karl Heyes committed
257 258
            if (state->header_pages == NULL)
                state->header_pages = refbuf;
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

            if (state->packets >= 0 && state->packets < 3) {
                ogg_stream_pagein(&state->os, &state->og);
                while (state->packets < 3) {
                    result = ogg_stream_packetout(&state->os, &op);
                    if (result == 0) break; /* need more data */
                    if (result < 0) {
                        state->packets = -1;
                        break;
                    }

                    state->packets++;

                    if (vorbis_synthesis_headerin(&state->vi, &state->vc, &op) < 0) {
                        state->packets = -1;
                        break;
                    }
                }
            }
Karl Heyes's avatar
Karl Heyes committed
278 279 280 281 282 283 284 285 286
            /* we do not place ogg headers on the main queue */
            return NULL;
        }
        /* increase ref counts on each header page going out */
        header = state->header_pages;
        while (header)
        {
            refbuf_addref (header);
            header = header->next;
287
        }
Karl Heyes's avatar
Karl Heyes committed
288
        refbuf->associated = state->header_pages;
289
    }
Jack Moffitt's avatar
Jack Moffitt committed
290

Karl Heyes's avatar
Karl Heyes committed
291
    return refbuf;
Jack Moffitt's avatar
Jack Moffitt committed
292 293
}

Karl Heyes's avatar
Karl Heyes committed
294
static void free_ogg_client_data (client_t *client)
Jack Moffitt's avatar
Jack Moffitt committed
295
{
Karl Heyes's avatar
Karl Heyes committed
296 297
    free (client->format_data);
    client->format_data = NULL;
Jack Moffitt's avatar
Jack Moffitt committed
298 299
}

Karl Heyes's avatar
Karl Heyes committed
300
static int format_vorbis_create_client_data (source_t *source, client_t *client)
301
{
Karl Heyes's avatar
Karl Heyes committed
302 303 304 305 306 307 308 309 310 311
    struct client_vorbis *client_data = calloc (1, sizeof (struct client_vorbis));
    int ret = -1;

    if (client_data)
    {
        client->format_data = client_data;
        client->free_client_data = free_ogg_client_data;
        ret = 0;
    }
    return ret;
312
}
Jack Moffitt's avatar
Jack Moffitt committed
313

314 315 316 317 318 319 320 321 322
static void format_vorbis_send_headers(format_plugin_t *self,
        source_t *source, client_t *client)
{
    int bytes;
    
    client->respcode = 200;
    bytes = sock_write(client->con->sock, 
            "HTTP/1.0 200 OK\r\n" 
            "Content-Type: %s\r\n", 
323
            source->format->contenttype);
324 325 326 327 328 329

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

    format_send_general_headers(self, source, client);
}

Karl Heyes's avatar
Karl Heyes committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 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 388 389 390 391 392 393 394 395 396 397 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 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
static int send_ogg_headers (client_t *client, refbuf_t *headers)
{
    struct client_vorbis *client_data = client->format_data;
    refbuf_t *refbuf;
    int written = 0;

    if (client_data->processing_headers == 0)
    {
        client_data->header_page = headers;
        client_data->pos = 0;
        client_data->processing_headers = 1;
    }
    refbuf = client_data->header_page;
    while (refbuf)
    {
        char *data = refbuf->data + client_data->pos;
        unsigned int len = refbuf->len - client_data->pos;
        int ret;

        ret = client_send_bytes (client, data, len);
        if (ret > 0)
        {
           written += ret;
           client_data->pos += ret;
        }
        if (ret < (int)len)
            return written;
        if (client_data->pos == refbuf->len)
        {
            refbuf = refbuf->next;
            client_data->header_page = refbuf;
            client_data->pos = 0;
        }
    }
    /* update client info on headers sent */
    client_data->processing_headers = 0;
    client_data->headers = headers;
    return written;
}

static int write_buf_to_client (format_plugin_t *self, client_t *client)
{
    refbuf_t *refbuf = client->refbuf;
    char *buf;
    unsigned int len;
    struct client_vorbis *client_data = client->format_data;
    int ret, written = 0;

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

    if (refbuf->next && client->pos == refbuf->len)
    {
        client_set_queue (client, refbuf->next);
        refbuf = client->refbuf;
    }
    do
    {
        if (client_data->headers != refbuf->associated)
        {
            /* different headers seen so send the new ones */
            ret = send_ogg_headers (client, refbuf->associated);
            if (client_data->processing_headers)
                break;
            written += ret;
        }
        buf = refbuf->data + client->pos;
        len = refbuf->len - client->pos;
        ret = client_send_bytes (client, buf, len);

        if (ret > 0)
            client->pos += ret;
    
        if (ret < (int)len) 
            break;
        written += ret;
        /* we have now written the page(s) */
        ret = 0;
    } while (0);

    if (ret > 0)
       written += ret;
    return written;
}

static int write_ogg_data (struct source_tag *source, refbuf_t *refbuf)
{   
    int ret = 1;

    if (fwrite (refbuf->data, 1, refbuf->len, source->dumpfile) != refbuf->len)
    {   
        WARN0 ("Write to dump file failed, disabling");
        fclose (source->dumpfile);
        source->dumpfile = NULL;
        ret = 0;
    }
    return ret;
}


static void write_ogg_to_file (struct source_tag *source, refbuf_t *refbuf)
{   
    vstate_t *state = (vstate_t *)source->format->_state;


    if (state->file_headers != refbuf->associated)
    {
        refbuf_t *header = refbuf->associated;
        while (header) 
        {
            if (write_ogg_data (source, header) == 0)
                return;
            header = header->next;
        }
        state->file_headers = refbuf->associated;
    }
    write_ogg_data (source, refbuf);
}
Jack Moffitt's avatar
Jack Moffitt committed
448