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

13 14

/* Ogg codec handler for vorbis streams */
Jack Moffitt's avatar
Jack Moffitt committed
15

16 17 18 19
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
20 21 22
#include <stdlib.h>
#include <ogg/ogg.h>
#include <vorbis/codec.h>
23
#include <memory.h>
24
#include <string.h>
Jack Moffitt's avatar
Jack Moffitt committed
25 26

#include "refbuf.h"
27 28
#include "source.h"
#include "client.h"
Jack Moffitt's avatar
Jack Moffitt committed
29

30
#include "format_ogg.h"
Michael Smith's avatar
Michael Smith committed
31
#include "stats.h"
Jack Moffitt's avatar
Jack Moffitt committed
32 33
#include "format.h"

34 35 36
#define CATMODULE "format-vorbis"
#include "logging.h"

Michael Smith's avatar
Michael Smith committed
37

38
typedef struct vorbis_codec_tag
Jack Moffitt's avatar
Jack Moffitt committed
39
{
40
    vorbis_info    vi;
41

42 43
    int rebuild_comment;
    int stream_notify;
44
    int initial_audio_page;
Jack Moffitt's avatar
Jack Moffitt committed
45

46 47 48 49 50 51 52 53 54 55 56 57
    ogg_stream_state    new_os;
    int                 page_samples_trigger;
    ogg_int64_t         prev_granulepos;
    ogg_packet          *prev_packet;
    ogg_int64_t         granulepos;
    ogg_int64_t         initial_page_granulepos;
    ogg_int64_t         samples_in_page;
    int                 prev_window;
    int                 initial_audio_packet;

    ogg_page    bos_page;
    ogg_packet  *header[3];
58 59
    ogg_int64_t prev_page_samples;

60
    int (*process_packet)(ogg_state_t *ogg_info, ogg_codec_t *codec, format_plugin_t *plugin);
61
    refbuf_t *(*get_buffer_page)(ogg_state_t *ogg_info, ogg_codec_t *codec);
62

63
} vorbis_codec_t;
64

65
static int process_vorbis_headers (ogg_state_t *ogg_info, ogg_codec_t *codec, format_plugin_t *plugin);
66
static refbuf_t *process_vorbis_page (ogg_state_t *ogg_info,
67 68
                ogg_codec_t *codec, ogg_page *page, format_plugin_t *plugin);
static refbuf_t *process_vorbis (ogg_state_t *ogg_info, ogg_codec_t *codec, format_plugin_t *plugin);
69
static void vorbis_set_tag (format_plugin_t *plugin, const char *tag, const char *value, const char *charset);
Jack Moffitt's avatar
Jack Moffitt committed
70

71

72
static void free_ogg_packet (ogg_packet *packet)
Jack Moffitt's avatar
Jack Moffitt committed
73
{
74 75 76 77 78 79
    if (packet)
    {
        free (packet->packet);
        free (packet);
    }
}
Jack Moffitt's avatar
Jack Moffitt committed
80

81

82 83 84 85
static void vorbis_codec_free (ogg_state_t *ogg_info, ogg_codec_t *codec)
{
    vorbis_codec_t *vorbis = codec->specific;

86
    ICECAST_LOG_DEBUG("freeing vorbis codec");
87 88 89
    stats_event (ogg_info->mount, "audio_bitrate", NULL);
    stats_event (ogg_info->mount, "audio_channels", NULL);
    stats_event (ogg_info->mount, "audio_samplerate", NULL);
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    vorbis_info_clear (&vorbis->vi);
    ogg_stream_clear (&codec->os);
    ogg_stream_clear (&vorbis->new_os);
    free_ogg_packet (vorbis->header[0]);
    free_ogg_packet (vorbis->header[1]);
    free_ogg_packet (vorbis->header[2]);
    free_ogg_packet (vorbis->prev_packet);
    free (vorbis->bos_page.header);
    free (vorbis);
    free (codec);
}


static ogg_packet *copy_ogg_packet (ogg_packet *packet)
{
    ogg_packet *next;
    do
    {
        next = malloc (sizeof (ogg_packet));
        if (next == NULL)
            break;
        memcpy (next, packet, sizeof (ogg_packet));
        next->packet = malloc (next->bytes);
        if (next->packet == NULL)
            break;
        memcpy (next->packet, packet->packet, next->bytes);
        return next;
    } while (0);
Jack Moffitt's avatar
Jack Moffitt committed
118

119 120 121 122
    if (next)
        free (next);
    return NULL;
}
Jack Moffitt's avatar
Jack Moffitt committed
123 124


125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
static void add_audio_packet (vorbis_codec_t *source_vorbis, ogg_packet *packet)
{
    if (source_vorbis->initial_audio_packet)
    {
        packet->granulepos = 0;
        source_vorbis->initial_audio_packet = 0;
    }
    else
    {
        source_vorbis->samples_in_page +=
            (packet->granulepos - source_vorbis->prev_granulepos);
        source_vorbis->prev_granulepos = packet->granulepos;
        source_vorbis->granulepos += source_vorbis->prev_window;
    }
    ogg_stream_packetin (&source_vorbis->new_os, packet);
Jack Moffitt's avatar
Jack Moffitt committed
140 141
}

142 143

static refbuf_t *get_buffer_audio (ogg_state_t *ogg_info, ogg_codec_t *codec)
144
{
145 146 147 148
    refbuf_t *refbuf = NULL;
    ogg_page page;
    vorbis_codec_t *source_vorbis = codec->specific;
    int (*get_ogg_page)(ogg_stream_state*, ogg_page *) = ogg_stream_pageout;
149

150 151
    if (source_vorbis->samples_in_page > source_vorbis->page_samples_trigger)
        get_ogg_page = ogg_stream_flush;
152

153
    if (get_ogg_page (&source_vorbis->new_os, &page) > 0)
154
    {
155 156 157 158 159
        /* squeeze a page copy into a buffer */
        source_vorbis->samples_in_page -= (ogg_page_granulepos (&page) - source_vorbis->prev_page_samples);
        source_vorbis->prev_page_samples = ogg_page_granulepos (&page);

        refbuf = make_refbuf_with_page (&page);
160
    }
161 162 163
    return refbuf;
}

164

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
static refbuf_t *get_buffer_header (ogg_state_t *ogg_info, ogg_codec_t *codec)
{
    int headers_flushed = 0;
    ogg_page page;
    vorbis_codec_t *source_vorbis = codec->specific;

    while (ogg_stream_flush (&source_vorbis->new_os, &page) > 0)
    {
        format_ogg_attach_header (ogg_info, &page);
        headers_flushed = 1;
    }
    if (headers_flushed)
    {
        source_vorbis->get_buffer_page = get_buffer_audio;
    }
    return NULL;
181 182
}

183

184
static refbuf_t *get_buffer_finished(ogg_state_t *ogg_info, ogg_codec_t *codec)
Jack Moffitt's avatar
Jack Moffitt committed
185
{
186 187 188 189 190
    vorbis_codec_t *source_vorbis = codec->specific;
    ogg_page page;
    refbuf_t *refbuf;

    if (ogg_stream_flush (&source_vorbis->new_os, &page) > 0)
191
    {
192 193 194 195
        source_vorbis->samples_in_page -= (ogg_page_granulepos (&page) - source_vorbis->prev_page_samples);
        source_vorbis->prev_page_samples = ogg_page_granulepos (&page);

        refbuf = make_refbuf_with_page (&page);
196
        ICECAST_LOG_DEBUG("flushing page");
197
        return refbuf;
198
    }
199 200 201 202 203
    ogg_stream_clear (&source_vorbis->new_os);
    ogg_stream_init (&source_vorbis->new_os, rand());

    format_ogg_free_headers (ogg_info);
    source_vorbis->get_buffer_page = NULL;
204 205 206 207 208
    if (source_vorbis->prev_packet)
        source_vorbis->process_packet = process_vorbis_headers;
    else
        source_vorbis->process_packet = NULL;

209 210 211 212 213 214 215 216 217 218 219
    if (source_vorbis->initial_audio_packet == 0)
        source_vorbis->prev_window = 0;

    return NULL;
}


/* push last packet into stream marked with eos */
static void initiate_flush (vorbis_codec_t *source_vorbis)
{
    if (source_vorbis->prev_packet)
220
    {
221
        /* insert prev_packet with eos */
222
        ICECAST_LOG_DEBUG("adding EOS packet");
223 224 225
        source_vorbis->prev_packet->e_o_s = 1;
        add_audio_packet (source_vorbis, source_vorbis->prev_packet);
        source_vorbis->prev_packet->e_o_s = 0;
226
    }
227 228 229
    source_vorbis->get_buffer_page = get_buffer_finished;
    source_vorbis->initial_audio_packet = 1;
}
Jack Moffitt's avatar
Jack Moffitt committed
230

231

232
/* process the vorbis audio packets. Here we just take each packet out
233 234 235
 * and add them into the new stream, flushing after so many samples. We
 * also check if an new headers are requested after each processed page
 */
236
static int process_vorbis_audio(ogg_state_t *ogg_info, ogg_codec_t *codec, format_plugin_t *plugin)
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
{
    vorbis_codec_t *source_vorbis = codec->specific;

    while (1)
    {
        int window;
        ogg_packet packet;

        /* now, lets extract what packets we can */
        if (ogg_stream_packetout (&codec->os, &packet) <= 0)
            break;

        /* calculate granulepos for the packet */
        window = vorbis_packet_blocksize (&source_vorbis->vi, &packet) / 4;

        source_vorbis->granulepos += window;
        if (source_vorbis->prev_packet)
        {
            ogg_packet *prev_packet = source_vorbis->prev_packet;

            add_audio_packet (source_vorbis, prev_packet);
            free_ogg_packet (prev_packet);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

            /* check for short values on first initial page */
            if (packet . packetno == 4)
            {
                if (source_vorbis->initial_page_granulepos < source_vorbis->granulepos)
                {
                    source_vorbis->granulepos -= source_vorbis->initial_page_granulepos;
                    source_vorbis->samples_in_page = source_vorbis->page_samples_trigger;
                }
            }
            /* check for long values on first page */
            if (packet.granulepos == source_vorbis->initial_page_granulepos)
            {
                if (source_vorbis->initial_page_granulepos > source_vorbis->granulepos)
                    source_vorbis->granulepos = source_vorbis->initial_page_granulepos;
            }

            if (packet.e_o_s == 0)
                packet . granulepos = source_vorbis->granulepos;
278
        }
279
        else
280
        {
281
            packet . granulepos = 0;
282
        }
Jack Moffitt's avatar
Jack Moffitt committed
283

284 285 286 287 288 289
        /* store the current packet details */
        source_vorbis->prev_window = window;
        source_vorbis->prev_packet = copy_ogg_packet (&packet);
        if (packet.e_o_s)
        {
            initiate_flush (source_vorbis);
290 291
            free_ogg_packet (source_vorbis->prev_packet);
            source_vorbis->prev_packet = NULL;
292 293
            return 1;
        }
Jack Moffitt's avatar
Jack Moffitt committed
294

295 296 297 298 299 300 301 302
        /* allow for pages to be flushed if there's over a certain number of samples */
        if (source_vorbis->samples_in_page > source_vorbis->page_samples_trigger)
            return 1;
    }
    if (source_vorbis->stream_notify)
    {
        initiate_flush (source_vorbis);
        source_vorbis->stream_notify = 0;
303
        return 1;
304 305
    }
    return -1;
Jack Moffitt's avatar
Jack Moffitt committed
306 307
}

308 309 310 311

/* This handles the headers at the backend, here we insert the header packets
 * we want for the queue.
 */
312
static int process_vorbis_headers (ogg_state_t *ogg_info, ogg_codec_t *codec, format_plugin_t *plugin)
313
{
314 315 316 317
    vorbis_codec_t *source_vorbis = codec->specific;

    if (source_vorbis->header [0] == NULL)
        return 0;
318

319
    ICECAST_LOG_DEBUG("Adding the 3 header packets");
320 321 322
    ogg_stream_packetin (&source_vorbis->new_os, source_vorbis->header [0]);
    /* NOTE: we could build a separate comment packet each time */
    if (source_vorbis->rebuild_comment)
323
    {
324
        ogg_packet header;
325
        ice_config_t *config;
326

327
        config = config_get_config();
328
        format_set_vorbiscomment(plugin, "server", config->server_id);
329
        config_release_config();
330
        vorbis_commentheader_out (&plugin->vc, &header);
331 332 333

        ogg_stream_packetin (&source_vorbis->new_os, &header);
        ogg_packet_clear (&header);
334
    }
335 336 337 338 339 340 341 342 343 344 345
    else
        ogg_stream_packetin (&source_vorbis->new_os, source_vorbis->header [1]);
    ogg_stream_packetin (&source_vorbis->new_os, source_vorbis->header [2]);
    source_vorbis->rebuild_comment = 0;

    ogg_info->log_metadata = 1;
    source_vorbis->get_buffer_page = get_buffer_header;
    source_vorbis->process_packet = process_vorbis_audio;
    source_vorbis->granulepos = source_vorbis->prev_window;
    source_vorbis->initial_audio_packet = 1;
    return 1;
346
}
Jack Moffitt's avatar
Jack Moffitt committed
347

348 349 350 351 352

/* check if the provided BOS page is the start of a vorbis stream. If so
 * then setup a structure so it can be used
 */
ogg_codec_t *initial_vorbis_page (format_plugin_t *plugin, ogg_page *page)
353
{
354 355 356 357 358 359 360 361 362 363 364 365
    ogg_codec_t *codec = calloc (1, sizeof (ogg_codec_t));
    ogg_packet packet;

    vorbis_codec_t *vorbis = calloc (1, sizeof (vorbis_codec_t));

    ogg_stream_init (&codec->os, ogg_page_serialno (page));
    ogg_stream_pagein (&codec->os, page);

    vorbis_info_init (&vorbis->vi);

    ogg_stream_packetout (&codec->os, &packet);

366 367 368
    vorbis_comment_clear(&plugin->vc);
    vorbis_comment_init(&plugin->vc);

369
    ICECAST_LOG_DEBUG("checking for vorbis codec");
370
    if (vorbis_synthesis_headerin (&vorbis->vi, &plugin->vc, &packet) < 0)
371 372 373
    {
        ogg_stream_clear (&codec->os);
        vorbis_info_clear (&vorbis->vi);
374
        vorbis_comment_clear (&plugin->vc);
375 376 377 378
        free (vorbis);
        free (codec);
        return NULL;
    }
379
    ICECAST_LOG_INFO("seen initial vorbis header");
380 381 382
    codec->specific = vorbis;
    codec->codec_free = vorbis_codec_free;
    codec->headers = 1;
383
    codec->name = "Vorbis";
384

385 386 387 388 389 390
    free_ogg_packet(vorbis->header[0]);
    free_ogg_packet(vorbis->header[1]);
    free_ogg_packet(vorbis->header[2]);
    memset(vorbis->header, 0, sizeof(vorbis->header));
    vorbis->header[0] = copy_ogg_packet(&packet);
    ogg_stream_init(&vorbis->new_os, rand());
391 392 393 394 395

    codec->process_page = process_vorbis_page;
    codec->process = process_vorbis;
    plugin->set_tag = vorbis_set_tag;

396 397 398
    vorbis->bos_page.header = malloc(page->header_len + page->body_len);

    memcpy(vorbis->bos_page.header, page->header, page->header_len);
399
    vorbis->bos_page.header_len = page->header_len;
400

401
    vorbis->bos_page.body = vorbis->bos_page.header + page->header_len;
402
    memcpy(vorbis->bos_page.body, page->body, page->body_len);
403
    vorbis->bos_page.body_len = page->body_len;
404

405
    return codec;
406 407
}

408

409 410 411
/* called from the admin interface, here we update the artist/title info
 * and schedule a new set of header pages
 */
412
static void vorbis_set_tag (format_plugin_t *plugin, const char *tag, const char *in_value, const char *charset)
413
{
414 415 416
    ogg_state_t *ogg_info = plugin->_state;
    ogg_codec_t *codec = ogg_info->codecs;
    vorbis_codec_t *source_vorbis;
417
    char *value;
418 419 420 421 422 423 424

    /* avoid updating if multiple codecs in use */
    if (codec && codec->next == NULL)
        source_vorbis = codec->specific;
    else
        return;

425 426 427 428 429 430 431
    if (tag == NULL)
    {
        source_vorbis->stream_notify = 1;
        source_vorbis->rebuild_comment = 1;
        return;
    }

Karl Heyes's avatar
Karl Heyes committed
432
    value = util_conv_string (in_value, charset, "UTF-8");
433 434 435
    if (value == NULL)
        value = strdup (in_value);

436 437 438 439 440
    if (strcmp(tag, "song") == 0)
        tag = "title";

    format_set_vorbiscomment(plugin, tag, value);
    free (value);
441 442 443
}


444 445 446
/* main backend routine when rebuilding streams. Here we loop until we either
 * have a refbuf to add onto the queue, or we want more data to process.
 */
447
static refbuf_t *process_vorbis (ogg_state_t *ogg_info, ogg_codec_t *codec, format_plugin_t *plugin)
448 449 450
{
    vorbis_codec_t *source_vorbis = codec->specific;
    refbuf_t *refbuf;
451

452
    while (1)
453
    {
454
        if (source_vorbis->get_buffer_page)
455
        {
456 457 458
            refbuf = source_vorbis->get_buffer_page (ogg_info, codec);
            if (refbuf)
                return refbuf;
459 460
        }

461
        if (source_vorbis->process_packet &&
462
                source_vorbis->process_packet (ogg_info, codec, plugin) > 0)
463 464 465
            continue;
        return NULL;
    }
466 467
}

468 469 470 471 472

/* no processing of pages, just wrap them up in a refbuf and pass
 * back for adding to the queue
 */
static refbuf_t *process_vorbis_passthru_page (ogg_state_t *ogg_info,
473
        ogg_codec_t *codec, ogg_page *page, format_plugin_t *plugin)
474 475
{
    return make_refbuf_with_page (page);
476 477 478
}


479 480 481 482
/* handle incoming page. as the stream is being rebuilt, we need to
 * add all pages from the stream before processing packets
 */
static refbuf_t *process_vorbis_page (ogg_state_t *ogg_info,
483
        ogg_codec_t *codec, ogg_page *page, format_plugin_t *plugin)
484 485 486
{
    ogg_packet header;
    vorbis_codec_t *source_vorbis = codec->specific;
487

488 489 490 491 492 493
    if (ogg_stream_pagein (&codec->os, page) < 0)
    {
        ogg_info->error = 1;
        return NULL;
    }
    if (codec->headers == 3)
494 495 496 497 498 499
    {
        if (source_vorbis->initial_audio_page)
        {
            source_vorbis->initial_page_granulepos = ogg_page_granulepos (page);
            source_vorbis->initial_audio_page = 0;
        }
500
        return NULL;
501
    }
502

503
    while (codec->headers < 3)
504
    {
505
        /* now, lets extract the packets */
506
        ICECAST_LOG_DEBUG("processing incoming header packet (%d)", codec->headers);
507 508 509 510

        if (ogg_stream_packetout (&codec->os, &header) <= 0)
        {
            if (ogg_info->codecs->next)
511
                format_ogg_attach_header(ogg_info, page);
512 513 514
            return NULL;
        }

515
        if (vorbis_synthesis_headerin (&source_vorbis->vi, &plugin->vc, &header) < 0)
516
        {
517
            ogg_info->error = 1;
518
            ICECAST_LOG_WARN("Problem parsing ogg vorbis header");
519
            return NULL;
520
        }
521
        header.granulepos = 0;
522
        source_vorbis->header[codec->headers] = copy_ogg_packet(&header);
523 524
        codec->headers++;
    }
525
    ICECAST_LOG_DEBUG("we have the header packets now");
526 527 528 529 530 531 532

    /* if vorbis is the only codec then allow rebuilding of the streams */
    if (ogg_info->codecs->next == NULL)
    {
        /* set queued vorbis pages to contain about 1/2 of a second worth of samples */
        source_vorbis->page_samples_trigger = source_vorbis->vi.rate / 2;
        source_vorbis->process_packet = process_vorbis_headers;
533
        source_vorbis->initial_audio_page = 1;
534 535 536 537 538 539
    }
    else
    {
        format_ogg_attach_header (ogg_info, &source_vorbis->bos_page);
        format_ogg_attach_header (ogg_info, page);
        codec->process_page = process_vorbis_passthru_page;
540
    }
541 542 543

    ogg_info->log_metadata = 1;

544 545 546
    stats_event_args (ogg_info->mount, "audio_samplerate", "%ld", (long)source_vorbis->vi.rate);
    stats_event_args (ogg_info->mount, "audio_channels", "%ld", (long)source_vorbis->vi.channels);
    stats_event_args (ogg_info->mount, "audio_bitrate", "%ld", (long)source_vorbis->vi.bitrate_nominal);
547 548 549
    stats_event_args (ogg_info->mount, "ice-bitrate", "%ld", (long)source_vorbis->vi.bitrate_nominal/1000);

    return NULL;
550
}
Jack Moffitt's avatar
Jack Moffitt committed
551