format_kate.c 4.96 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
 */


/* Ogg codec handler for kate logical streams */

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

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

25
#include "compat.h"
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "refbuf.h"
#include "format_ogg.h"
#include "format_kate.h"
#include "client.h"
#include "stats.h"

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


typedef struct _kate_codec_tag
{
    int             headers_done;
39
    size_t          num_headers;
40
41
42
43
44
45
46
47
48
    ogg_int64_t     last_iframe;
    ogg_int64_t     prev_granulepos;
} kate_codec_t;


static void kate_codec_free (ogg_state_t *ogg_info, ogg_codec_t *codec)
{
    kate_codec_t *kate = codec->specific;

49
    ICECAST_LOG_DEBUG("freeing kate codec");
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    /* TODO: should i replace with something or just remove
    stats_event (ogg_info->mount, "video_bitrate", NULL);
    stats_event (ogg_info->mount, "video_quality", NULL);
    stats_event (ogg_info->mount, "frame_rate", NULL);
    stats_event (ogg_info->mount, "frame_size", NULL);
    */
    ogg_stream_clear (&codec->os);
    free (kate);
    free (codec);
}


/* kate pages are not rebuilt, so here we just for headers and then
 * pass them straight through to the the queue
 */
65
static refbuf_t *process_kate_page (ogg_state_t *ogg_info, ogg_codec_t *codec, ogg_page *page, format_plugin_t *plugin)
66
67
68
69
70
71
72
73
74
75
76
77
{
    kate_codec_t *kate = codec->specific;
    ogg_packet packet;
    int header_page = 0;
    refbuf_t *refbuf = NULL;
    ogg_int64_t granulepos;

    if (ogg_stream_pagein (&codec->os, page) < 0)
    {
        ogg_info->error = 1;
        return NULL;
    }
78
    granulepos = ogg_page_granulepos(page);
79
80
81
82
83

    while (ogg_stream_packetout (&codec->os, &packet) > 0)
    {
        if (!kate->headers_done)
        {
84
            header_page = (packet.bytes > 0 && (packet.packet[0] & 0x80));
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
            if (!header_page)
                break;
            codec->headers++;
            if (packet.packet[0]==0x80)
            {
                if (packet.bytes<64) return NULL;
                /* we peek for the number of headers to expect */
                kate->num_headers = packet.packet[11];
            }
            continue;
        }

        if (codec->headers < kate->num_headers)
        {
            ogg_info->error = 1;
100
            ICECAST_LOG_ERROR("Not enough header packets");
101
102
103
104
105
106
107
108
109
110
            return NULL;
        }
    }
    if (header_page)
    {
        format_ogg_attach_header (ogg_info, page);
        return NULL;
    }

    refbuf = make_refbuf_with_page (page);
111
    /* ICECAST_LOG_DEBUG("refbuf %p has pageno %ld, %llu", refbuf, ogg_page_pageno (page), (uint64_t)granulepos); */
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

    if (codec->possible_start)
    {
        /* we don't bother trying to know where we can start, we'll just
           start whenever we have to, video's more important and in the majority
           of the cases it's ok if we lose an event we're seeking in the middle
           of, as we won't have display artifacts as we'd have with video */
        codec->possible_start->sync_point = 1;
        refbuf_release (codec->possible_start);
        codec->possible_start = NULL;
    }
    if (granulepos != kate->prev_granulepos || granulepos == 0)
    {
        if (codec->possible_start)
            refbuf_release (codec->possible_start);
        refbuf_addref (refbuf);
        codec->possible_start = refbuf;
    }
    kate->prev_granulepos = granulepos;

    return refbuf;
}


/* Check if specified BOS page is the start of a kate stream and
 * if so, create a codec structure for handling it
 */
139
ogg_codec_t *initial_kate_page(format_plugin_t *plugin, ogg_page *page)
140
141
{
    ogg_state_t *ogg_info = plugin->_state;
142
    ogg_codec_t *codec = calloc(1, sizeof(ogg_codec_t));
143
144
    ogg_packet packet;

145
    kate_codec_t *kate_codec = calloc(1, sizeof(kate_codec_t));
146

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

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

152
    ICECAST_LOG_DEBUG("checking for kate codec");
153
154
155
156
157
158
159
160
161
    /* we don't have libkate, so we examine the packet magic by hand */
    if ((packet.bytes<9) || memcmp(packet.packet, "\x80kate\0\0\0\0", 9))
    {
        ogg_stream_clear (&codec->os);
        free (kate_codec);
        free (codec);
        return NULL;
    }

162
    ICECAST_LOG_INFO("seen initial kate header");
163
164
165
166
167
168
    codec->specific = kate_codec;
    codec->process_page = process_kate_page;
    codec->codec_free = kate_codec_free;
    codec->headers = 1;
    codec->name = "Kate";

169
    format_ogg_attach_header(ogg_info, page);
170
171
172
173
    ogg_info->codec_sync = codec;
    return codec;
}