-/*
- * Copyright (C) 2004 Andre Noll <maan@tuebingen.mpg.de>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 2004 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
/** \file ogg_afh_common.c Functions common to all ogg/ codecs. */
/* Taken from decoder_example.c of libvorbis-1.2.3. */
static int process_packets_2_and_3(ogg_sync_state *oss,
ogg_stream_state *stream, struct afh_info *afhi,
- struct ogg_afh_callback_info *ci)
+ struct oac_callback_info *ci)
{
ogg_page page;
ogg_packet packet;
}
static int process_ogg_packets(ogg_sync_state *oss, struct afh_info *afhi,
- struct ogg_afh_callback_info *ci)
+ struct oac_callback_info *ci)
{
ogg_packet packet;
ogg_stream_state stream;
*
* \return Standard.
*/
-int ogg_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
- struct ogg_afh_callback_info *ci)
+int oac_get_file_info(char *map, size_t numbytes, struct afh_info *afhi,
+ struct oac_callback_info *ci)
{
ogg_sync_state oss;
ogg_page op;
- long len = numbytes;
char *buf;
- int ret, i, j, frames_per_chunk, ct_size;
- long long unsigned num_frames = 0;
+ int ret, i, j, frames_per_chunk, ct_size, prev_pageno = 0;
+ long long unsigned granule_skip = 0, num_frames = 0;
+ int64_t granule = 0, prev_granule = 0;
ogg_sync_init(&oss);
ret = -E_OGG_SYNC;
- buf = ogg_sync_buffer(&oss, len);
+ buf = ogg_sync_buffer(&oss, numbytes);
if (!buf)
goto out;
- memcpy(buf, map, len);
+ memcpy(buf, map, numbytes);
ret = -E_OGG_SYNC;
- if (ogg_sync_wrote(&oss, len) < 0)
+ if (ogg_sync_wrote(&oss, numbytes) < 0)
goto out;
ret = process_ogg_packets(&oss, afhi, ci);
if (ret < 0)
oss.returned = 0;
oss.fill = numbytes;
/* count ogg pages and get duration of the file */
- for (i = 0; ogg_sync_pageseek(&oss, &op) > 0; i++)
- num_frames = ogg_page_granulepos(&op);
+ for (i = 0; ogg_sync_pageseek(&oss, &op) > 0; i++) {
+ int this_pageno = ogg_page_pageno(&op);
+ int64_t this_granule = ogg_page_granulepos(&op);
+ if (this_granule >= 0)
+ granule = this_granule;
+ if (i > 0 && this_pageno != prev_pageno + 1) /* hole */
+ granule_skip += granule - prev_granule;
+ prev_pageno = this_pageno;
+ prev_granule = granule;
+ }
+ num_frames = granule - granule_skip;
PARA_INFO_LOG("%d pages, %llu frames\n", i, num_frames);
ret = -E_OGG_EMPTY;
if (i == 0)
afhi->seconds_total = num_frames / afhi->frequency;
/* use roughly one page per chunk */
frames_per_chunk = num_frames / i;
- PARA_INFO_LOG("%" PRIu32 "seconds, %d frames/chunk\n",
+ PARA_INFO_LOG("%" PRIu32 " seconds, %d frames/chunk\n",
afhi->seconds_total, frames_per_chunk);
ct_size = 250;
- afhi->chunk_table = para_malloc(ct_size * sizeof(uint32_t));
+ afhi->chunk_table = arr_alloc(ct_size, sizeof(uint32_t));
afhi->chunk_table[0] = 0;
afhi->chunk_table[1] = afhi->header_len;
oss.returned = afhi->header_len;
oss.fill = numbytes;
for (j = 1; ogg_sync_pageseek(&oss, &op) > 0; /* nothing */) {
- int granule = ogg_page_granulepos(&op);
+ granule = ogg_page_granulepos(&op);
while (granule >= (j + 1) * frames_per_chunk) {
j++;
if (j >= ct_size) {
ct_size *= 2;
- afhi->chunk_table = para_realloc(
+ afhi->chunk_table = arr_realloc(
afhi->chunk_table,
- ct_size * sizeof(uint32_t));
+ ct_size, sizeof(uint32_t));
}
afhi->chunk_table[j] = oss.returned;
}
}
afhi->chunks_total = j;
+ set_max_chunk_size(afhi);
set_chunk_tv(frames_per_chunk, afhi->frequency, &afhi->chunk_tv);
ret = 0;
out:
{
int ret;
- PARA_DEBUG_LOG("header/body: %lu/%lu\n", op->header_len, op->body_len);
+ PARA_DEBUG_LOG("header/body: %li/%li\n", op->header_len, op->body_len);
ret = xwrite(fd, (const char *)op->header, op->header_len);
if (ret < 0)
return ret;
*
* \return Standard.
*/
-int ogg_rewrite_tags(const char *map, size_t map_sz, int fd,
+int oac_rewrite_tags(const char *map, size_t map_sz, int fd,
char *meta_packet, size_t meta_sz)
{
ogg_sync_state oss_in, oss_out;
goto out;
continue;
}
- PARA_DEBUG_LOG("packet: bytes: %d, granule: %d, packetno: %u\n",
+ PARA_DEBUG_LOG("packet: bytes: %d, granule: %d, packetno: %d\n",
(int)packet.bytes, (int)packet.granulepos,
(int)packet.packetno);
/* ignore meta data packet which we replaced */
#endif
if (ret <= 0)
break;
- PARA_DEBUG_LOG("writing page (%lu bytes)\n",
+ PARA_DEBUG_LOG("writing page (%li bytes)\n",
op.header_len + op.body_len);
ret = write_ogg_page(fd, &op);
if (ret < 0)
ogg_stream_clear(so);
return ret;
}
+
+/* Structure for providing custom headers for streaming. */
+struct oac_custom_header {
+ char *buf;
+ size_t len;
+ ogg_stream_state oss;
+};
+
+/**
+ * Allocate and return a custom header structure.
+ *
+ * For some audio codecs which employ the ogg container format, the server side
+ * wants to replace the meta tags at the beginning of the file because they are
+ * not needed for streaming and can be arbitrary large. The structure returned
+ * by this function is typically used as the ->private field of struct \ref
+ * oac_callback_info for \ref oac_get_file_info(). This allows the audio format
+ * handler to set up a custom header which is identical to the original header,
+ * but with the meta data part replaced by fixed length dummy contents.
+ *
+ * \return The returned memory must be initialized with the serial number of
+ * the ogg stream before ogg packets can be submitted to it. This is not done
+ * here because the header structure is allocated before \ref
+ * oac_get_file_info() is called, and the serial number is not known at this
+ * point.
+ *
+ * \sa \ref oac_custom_header_init().
+ */
+struct oac_custom_header *oac_custom_header_new(void)
+{
+ return zalloc(sizeof(struct oac_custom_header));
+}
+
+/**
+ * Set the serial number of an allocated header structure.
+ *
+ * \param serial Passed to the callback function.
+ * \param h As returned from \ref oac_custom_header_new().
+ *
+ * This function must be called before any packets are submitted.
+ */
+void oac_custom_header_init(int serial, struct oac_custom_header *h)
+{
+ ogg_stream_init(&h->oss, serial);
+}
+
+/**
+ * Submit an ogg packet to a custom header structure.
+ *
+ * \param op The packet to append.
+ * \param h Must be initialized.
+ *
+ * The packet may be the one which was passed to the callback, or a completely
+ * different one, like a dummy metadata packet.
+ *
+ * \return Standard.
+ */
+int oac_custom_header_append(ogg_packet *op, struct oac_custom_header *h)
+{
+ return ogg_stream_packetin(&h->oss, op) < 0? -E_OGG_PACKET_IN : 1;
+}
+
+/**
+ * Force remaining packets into an ogg page.
+ *
+ * \param h Should contain submitted but not yet flushed packets.
+ *
+ * This is called after the first packet has been submitted with \ref
+ * oac_custom_header_append() to make sure the first ogg page contains only
+ * this packet. Also when header processing is complete, the callbacks call
+ * this to force the previously submitted packets into a page.
+ */
+void oac_custom_header_flush(struct oac_custom_header *h)
+{
+ ogg_page og;
+
+ while (ogg_stream_flush(&h->oss, &og)) {
+ size_t len = og.header_len + og.body_len;
+ h->buf = para_realloc(h->buf, h->len + len);
+ memcpy(h->buf + h->len, og.header, og.header_len);
+ memcpy(h->buf + h->len + og.header_len, og.body, og.body_len);
+ h->len += len;
+ }
+}
+
+/**
+ * Return the custom header buffer and deallocate resources.
+ *
+ * This is called after the ogg packets which comprise the header have been
+ * submitted and flushed.
+ *
+ * \param buf Result pointer.
+ * \param h Must not be used any more after the call.
+ *
+ * \return The size of the header. This is the sum of the sizes of all ogg
+ * pages that have been flushed out.
+ */
+size_t oac_custom_header_get(char **buf, struct oac_custom_header *h)
+{
+ size_t ret = h->len;
+
+ *buf = h->buf;
+ ogg_stream_clear(&h->oss);
+ free(h);
+ return ret;
+}