format_flac.c 3.2 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
 *                      Michael Smith <msmith@xiph.org>,
 *                      oddsock <oddsock@xiph.org>,
 *                      Karl Heyes <karl@xiph.org>
 *                      and others (see AUTHORS for details).
11
 * Copyright 2014-2018, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 */


/* Ogg codec handler for FLAC logical streams */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <ogg/ogg.h>
#include <string.h>

#include "refbuf.h"
#include "format_ogg.h"
#include "client.h"
#include "stats.h"

#define CATMODULE "format-flac"
#include "logging.h"


static void flac_codec_free (ogg_state_t *ogg_info, ogg_codec_t *codec)
{
36
    ICECAST_LOG_DEBUG("freeing FLAC codec");
37
38
39
40
41
42
43
    stats_event (ogg_info->mount, "FLAC_version", NULL);
    ogg_stream_clear (&codec->os);
    free (codec);
}


/* Here, we just verify the page is ok and then add it to the queue */
44
static refbuf_t *process_flac_page (ogg_state_t *ogg_info, ogg_codec_t *codec, ogg_page *page, format_plugin_t *plugin)
45
46
47
48
49
50
{
    refbuf_t * refbuf;

    if (codec->headers)
    {
        ogg_packet packet;
51
52
53
54
55
        if (ogg_stream_pagein (&codec->os, page) < 0)
        {
            ogg_info->error = 1;
            return NULL;
        }
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
        while (ogg_stream_packetout (&codec->os, &packet))
        {
            int type = packet.packet[0];
            if (type == 0xFF)
            {
                codec->headers = 0;
                break;
            }
            if (type >= 1 && type <= 0x7E)
                continue;
            if (type >= 0x81 && type <= 0xFE)
                continue;
            ogg_info->error = 1;
            return NULL;
        }
        if (codec->headers)
        {
            format_ogg_attach_header (ogg_info, page);
            return NULL;
        }
    }
    refbuf = make_refbuf_with_page (page);
    return refbuf;
}


/* Check for flac header in logical stream */

ogg_codec_t *initial_flac_page (format_plugin_t *plugin, ogg_page *page)
{
    ogg_state_t *ogg_info = plugin->_state;
    ogg_codec_t *codec = calloc (1, sizeof (ogg_codec_t));
    ogg_packet packet;

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

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

95
    ICECAST_LOG_DEBUG("checking for FLAC codec");
96
97
98
99
100
101
102
103
104
    do
    {
        unsigned char *parse = packet.packet;

        if (page->header_len + page->body_len != 79)
            break;
        if (*parse != 0x7F)
            break;
        parse++;
105
        if (memcmp(parse, "FLAC", 4) != 0)
106
107
            break;

108
        ICECAST_LOG_INFO("seen initial FLAC header");
109
110
111
112
113
114
115
116

        parse += 4;
        stats_event_args (ogg_info->mount, "FLAC_version", "%d.%d",  parse[0], parse[1]);
        codec->process_page = process_flac_page;
        codec->codec_free = flac_codec_free;
        codec->headers = 1;
        codec->name = "FLAC";

117
        format_ogg_attach_header(ogg_info, page);
118
119
120
        return codec;
    } while (0);

121
122
    ogg_stream_clear(&codec->os);
    free(codec);
123
124
125
    return NULL;
}