2004-01-28 20:02:12 -05:00
|
|
|
/* 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).
|
|
|
|
*/
|
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
/* format_vorbis.c
|
|
|
|
**
|
|
|
|
** format plugin for vorbis
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2003-07-20 21:58:54 -04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <ogg/ogg.h>
|
|
|
|
#include <vorbis/codec.h>
|
|
|
|
|
|
|
|
#include "refbuf.h"
|
2002-12-30 02:55:56 -05:00
|
|
|
#include "source.h"
|
|
|
|
#include "client.h"
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2002-02-11 04:11:18 -05:00
|
|
|
#include "stats.h"
|
2001-09-09 22:21:46 -04:00
|
|
|
#include "format.h"
|
|
|
|
|
2002-08-10 00:51:11 -04:00
|
|
|
#define CATMODULE "format-vorbis"
|
|
|
|
#include "logging.h"
|
|
|
|
|
2002-04-05 04:28:26 -05:00
|
|
|
#define MAX_HEADER_PAGES 10
|
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
typedef struct _vstate_tag
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
ogg_sync_state oy;
|
|
|
|
ogg_stream_state os;
|
|
|
|
vorbis_info vi;
|
|
|
|
vorbis_comment vc;
|
|
|
|
|
|
|
|
ogg_page og;
|
|
|
|
unsigned long serialno;
|
|
|
|
int header;
|
|
|
|
refbuf_t *headbuf[MAX_HEADER_PAGES];
|
|
|
|
int packets;
|
2001-09-09 22:21:46 -04:00
|
|
|
} vstate_t;
|
|
|
|
|
2002-12-29 03:10:10 -05:00
|
|
|
static void format_vorbis_free_plugin(format_plugin_t *self);
|
|
|
|
static int format_vorbis_get_buffer(format_plugin_t *self, char *data,
|
|
|
|
unsigned long len, refbuf_t **buffer);
|
|
|
|
static refbuf_queue_t *format_vorbis_get_predata(format_plugin_t *self);
|
2002-12-30 02:55:56 -05:00
|
|
|
static void *format_vorbis_create_client_data(format_plugin_t *self,
|
|
|
|
source_t *source, client_t *client);
|
|
|
|
static void format_vorbis_send_headers(format_plugin_t *self,
|
|
|
|
source_t *source, client_t *client);
|
2001-09-09 22:21:46 -04:00
|
|
|
|
|
|
|
format_plugin_t *format_vorbis_get_plugin(void)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
format_plugin_t *plugin;
|
|
|
|
vstate_t *state;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
plugin = (format_plugin_t *)malloc(sizeof(format_plugin_t));
|
2002-01-04 19:29:27 -05:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
plugin->type = FORMAT_TYPE_VORBIS;
|
|
|
|
plugin->has_predata = 1;
|
|
|
|
plugin->get_buffer = format_vorbis_get_buffer;
|
|
|
|
plugin->get_predata = format_vorbis_get_predata;
|
2002-12-29 03:10:10 -05:00
|
|
|
plugin->write_buf_to_client = format_generic_write_buf_to_client;
|
|
|
|
plugin->create_client_data = format_vorbis_create_client_data;
|
2002-12-30 02:55:56 -05:00
|
|
|
plugin->client_send_headers = format_vorbis_send_headers;
|
2003-03-14 21:10:19 -05:00
|
|
|
plugin->free_plugin = format_vorbis_free_plugin;
|
2002-08-10 04:01:56 -04:00
|
|
|
plugin->format_description = "Ogg Vorbis";
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
state = (vstate_t *)calloc(1, sizeof(vstate_t));
|
|
|
|
ogg_sync_init(&state->oy);
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
plugin->_state = (void *)state;
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
return plugin;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2002-01-04 19:29:27 -05:00
|
|
|
void format_vorbis_free_plugin(format_plugin_t *self)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
int i;
|
|
|
|
vstate_t *state = (vstate_t *)self->_state;
|
|
|
|
|
|
|
|
/* free memory associated with this plugin instance */
|
|
|
|
|
|
|
|
/* free state memory */
|
|
|
|
ogg_sync_clear(&state->oy);
|
|
|
|
ogg_stream_clear(&state->os);
|
|
|
|
vorbis_comment_clear(&state->vc);
|
|
|
|
vorbis_info_clear(&state->vi);
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_HEADER_PAGES; i++) {
|
|
|
|
if (state->headbuf[i]) {
|
|
|
|
refbuf_release(state->headbuf[i]);
|
|
|
|
state->headbuf[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(state);
|
|
|
|
|
|
|
|
/* free the plugin instance */
|
|
|
|
free(self);
|
2002-01-04 19:29:27 -05:00
|
|
|
}
|
|
|
|
|
2002-04-05 04:28:26 -05:00
|
|
|
int format_vorbis_get_buffer(format_plugin_t *self, char *data, unsigned long len, refbuf_t **buffer)
|
2001-09-09 22:21:46 -04:00
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
char *buf;
|
|
|
|
int i, result;
|
|
|
|
ogg_packet op;
|
|
|
|
char *tag;
|
2002-04-05 04:28:26 -05:00
|
|
|
refbuf_t *refbuf;
|
2003-03-14 21:10:19 -05:00
|
|
|
vstate_t *state = (vstate_t *)self->_state;
|
2003-06-26 09:31:38 -04:00
|
|
|
#ifdef USE_YP
|
|
|
|
source_t *source;
|
|
|
|
time_t current_time;
|
|
|
|
#endif
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (data) {
|
|
|
|
/* write the data to the buffer */
|
|
|
|
buf = ogg_sync_buffer(&state->oy, len);
|
2002-04-05 04:28:26 -05:00
|
|
|
memcpy(buf, data, len);
|
2003-03-14 21:10:19 -05:00
|
|
|
ogg_sync_wrote(&state->oy, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
/* this is a new logical bitstream */
|
|
|
|
state->header = 0;
|
|
|
|
state->packets = 0;
|
|
|
|
|
|
|
|
/* release old headers, stream state, vorbis data */
|
|
|
|
for (i = 0; i < MAX_HEADER_PAGES; i++) {
|
|
|
|
if (state->headbuf[i]) {
|
|
|
|
refbuf_release(state->headbuf[i]);
|
|
|
|
state->headbuf[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
2002-05-03 11:04:56 -04:00
|
|
|
/* Clear old stuff. Rarely but occasionally needed. */
|
2003-03-14 21:10:19 -05:00
|
|
|
ogg_stream_clear(&state->os);
|
|
|
|
vorbis_comment_clear(&state->vc);
|
|
|
|
vorbis_info_clear(&state->vi);
|
2002-02-04 02:08:52 -05:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
state->serialno = ogg_page_serialno(&state->og);
|
|
|
|
ogg_stream_init(&state->os, state->serialno);
|
|
|
|
vorbis_info_init(&state->vi);
|
|
|
|
vorbis_comment_init(&state->vc);
|
|
|
|
}
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
if (state->header >= 0) {
|
2002-02-11 04:11:18 -05:00
|
|
|
/* 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.
|
|
|
|
*/
|
2003-03-14 21:10:19 -05:00
|
|
|
if (ogg_page_granulepos(&state->og) <= 0) {
|
|
|
|
state->header++;
|
|
|
|
} else {
|
|
|
|
/* we're done caching headers */
|
|
|
|
state->header = -1;
|
|
|
|
|
|
|
|
/* put known comments in the stats */
|
|
|
|
tag = vorbis_comment_query(&state->vc, "TITLE", 0);
|
|
|
|
if (tag) stats_event(self->mount, "title", tag);
|
|
|
|
else stats_event(self->mount, "title", "unknown");
|
|
|
|
tag = vorbis_comment_query(&state->vc, "ARTIST", 0);
|
|
|
|
if (tag) stats_event(self->mount, "artist", tag);
|
|
|
|
else stats_event(self->mount, "artist", "unknown");
|
|
|
|
|
|
|
|
/* don't need these now */
|
|
|
|
ogg_stream_clear(&state->os);
|
|
|
|
vorbis_comment_clear(&state->vc);
|
|
|
|
vorbis_info_clear(&state->vi);
|
2003-06-26 09:31:38 -04:00
|
|
|
#ifdef USE_YP
|
|
|
|
/* If we get an update on the mountpoint, force a
|
|
|
|
yp touch */
|
|
|
|
avl_tree_rlock(global.source_tree);
|
2004-01-14 20:01:09 -05:00
|
|
|
source = source_find_mount_raw(self->mount);
|
2003-06-26 09:31:38 -04:00
|
|
|
avl_tree_unlock(global.source_tree);
|
|
|
|
|
|
|
|
if (source) {
|
|
|
|
/* If we get an update on the mountpoint, force a
|
|
|
|
yp touch */
|
|
|
|
current_time = time(NULL);
|
|
|
|
for (i=0; i<source->num_yp_directories; i++) {
|
|
|
|
source->ypdata[i]->yp_last_touch = current_time -
|
|
|
|
source->ypdata[i]->yp_touch_interval + 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-03-14 21:10:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cache header pages */
|
|
|
|
if (state->header > 0 && state->packets < 3) {
|
2002-04-05 04:28:26 -05:00
|
|
|
if(state->header > MAX_HEADER_PAGES) {
|
|
|
|
refbuf_release(refbuf);
|
2002-08-10 00:51:11 -04:00
|
|
|
ERROR1("Bad vorbis input: header is more than %d pages long", MAX_HEADER_PAGES);
|
|
|
|
|
2002-04-05 04:28:26 -05:00
|
|
|
return -1;
|
|
|
|
}
|
2003-03-14 21:10:19 -05:00
|
|
|
refbuf_addref(refbuf);
|
|
|
|
state->headbuf[state->header - 1] = refbuf;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2002-04-05 04:28:26 -05:00
|
|
|
*buffer = refbuf;
|
2003-03-14 21:10:19 -05:00
|
|
|
return 0;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
refbuf_queue_t *format_vorbis_get_predata(format_plugin_t *self)
|
|
|
|
{
|
2003-03-14 21:10:19 -05:00
|
|
|
refbuf_queue_t *queue;
|
|
|
|
int i;
|
|
|
|
vstate_t *state = (vstate_t *)self->_state;
|
|
|
|
|
|
|
|
queue = NULL;
|
|
|
|
for (i = 0; i < MAX_HEADER_PAGES; i++) {
|
|
|
|
if (state->headbuf[i]) {
|
|
|
|
refbuf_addref(state->headbuf[i]);
|
|
|
|
refbuf_queue_add(&queue, state->headbuf[i]);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return queue;
|
2001-09-09 22:21:46 -04:00
|
|
|
}
|
|
|
|
|
2002-12-30 02:55:56 -05:00
|
|
|
static void *format_vorbis_create_client_data(format_plugin_t *self,
|
|
|
|
source_t *source, client_t *client)
|
|
|
|
{
|
2002-12-29 03:10:10 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-09-09 22:21:46 -04:00
|
|
|
|
2002-12-30 02:55:56 -05:00
|
|
|
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",
|
|
|
|
format_get_mimetype(source->format->type));
|
|
|
|
|
|
|
|
if(bytes > 0) client->con->sent_bytes += bytes;
|
|
|
|
|
|
|
|
format_send_general_headers(self, source, client);
|
|
|
|
}
|
|
|
|
|
2001-09-09 22:21:46 -04:00
|
|
|
|