2004-12-07 16:06:26 -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.
|
|
|
|
*
|
2015-01-10 13:53:44 -05:00
|
|
|
* Copyright 2000-2004, Jack Moffitt <jack@xiph.org,
|
2004-12-07 16:06:26 -05:00
|
|
|
* Michael Smith <msmith@xiph.org>,
|
|
|
|
* oddsock <oddsock@xiph.org>,
|
|
|
|
* Karl Heyes <karl@xiph.org>
|
|
|
|
* and others (see AUTHORS for details).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* format_ogg.c
|
|
|
|
*
|
|
|
|
* format plugin for Ogg
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <ogg/ogg.h>
|
|
|
|
|
|
|
|
#include "refbuf.h"
|
|
|
|
#include "source.h"
|
|
|
|
#include "client.h"
|
|
|
|
|
|
|
|
#include "stats.h"
|
2015-03-28 12:15:49 -04:00
|
|
|
#include "playlist.h"
|
2004-12-07 16:06:26 -05:00
|
|
|
#include "format.h"
|
|
|
|
#include "format_ogg.h"
|
|
|
|
#include "format_vorbis.h"
|
|
|
|
#ifdef HAVE_THEORA
|
|
|
|
#include "format_theora.h"
|
|
|
|
#endif
|
2005-05-07 07:01:35 -04:00
|
|
|
#ifdef HAVE_SPEEX
|
|
|
|
#include "format_speex.h"
|
|
|
|
#endif
|
2012-05-24 12:04:27 -04:00
|
|
|
#include "format_opus.h"
|
2005-05-07 07:01:35 -04:00
|
|
|
#include "format_midi.h"
|
|
|
|
#include "format_flac.h"
|
2008-04-18 22:11:37 -04:00
|
|
|
#include "format_kate.h"
|
|
|
|
#include "format_skeleton.h"
|
2007-11-20 21:55:11 -05:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
2004-12-07 16:06:26 -05:00
|
|
|
|
|
|
|
#define CATMODULE "format-ogg"
|
|
|
|
#include "logging.h"
|
|
|
|
|
|
|
|
struct _ogg_state_tag;
|
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
static void format_ogg_free_plugin(format_plugin_t *plugin);
|
|
|
|
static int create_ogg_client_data(source_t *source, client_t *client);
|
|
|
|
static void free_ogg_client_data(client_t *client);
|
2004-12-07 16:06:26 -05:00
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
static void write_ogg_to_file(struct source_tag *source, refbuf_t *refbuf);
|
|
|
|
static refbuf_t *ogg_get_buffer(source_t *source);
|
|
|
|
static int write_buf_to_client(client_t *client);
|
2004-12-07 16:06:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
struct ogg_client
|
|
|
|
{
|
|
|
|
refbuf_t *headers;
|
|
|
|
refbuf_t *header_page;
|
|
|
|
unsigned pos;
|
|
|
|
int headers_sent;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
refbuf_t *make_refbuf_with_page (ogg_page *page)
|
|
|
|
{
|
|
|
|
refbuf_t *refbuf = refbuf_new (page->header_len + page->body_len);
|
|
|
|
|
|
|
|
memcpy (refbuf->data, page->header, page->header_len);
|
|
|
|
memcpy (refbuf->data+page->header_len, page->body, page->body_len);
|
|
|
|
return refbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* routine for taking the provided page (should be a header page) and
|
|
|
|
* placing it on the collection of header pages
|
|
|
|
*/
|
|
|
|
void format_ogg_attach_header (ogg_state_t *ogg_info, ogg_page *page)
|
|
|
|
{
|
|
|
|
refbuf_t *refbuf = make_refbuf_with_page (page);
|
|
|
|
|
|
|
|
if (ogg_page_bos (page))
|
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("attaching BOS page");
|
2004-12-07 16:06:26 -05:00
|
|
|
if (*ogg_info->bos_end == NULL)
|
|
|
|
ogg_info->header_pages_tail = refbuf;
|
|
|
|
refbuf->next = *ogg_info->bos_end;
|
|
|
|
*ogg_info->bos_end = refbuf;
|
|
|
|
ogg_info->bos_end = &refbuf->next;
|
|
|
|
return;
|
|
|
|
}
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("attaching header page");
|
2004-12-07 16:06:26 -05:00
|
|
|
if (ogg_info->header_pages_tail)
|
|
|
|
ogg_info->header_pages_tail->next = refbuf;
|
|
|
|
ogg_info->header_pages_tail = refbuf;
|
|
|
|
|
|
|
|
if (ogg_info->header_pages == NULL)
|
|
|
|
ogg_info->header_pages = refbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
void format_ogg_free_headers(ogg_state_t *ogg_info)
|
2004-12-07 16:06:26 -05:00
|
|
|
{
|
|
|
|
refbuf_t *header;
|
|
|
|
|
|
|
|
/* release the header pages first */
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("releasing header pages");
|
2004-12-07 16:06:26 -05:00
|
|
|
header = ogg_info->header_pages;
|
|
|
|
while (header)
|
|
|
|
{
|
|
|
|
refbuf_t *to_release = header;
|
|
|
|
header = header->next;
|
2014-11-30 15:32:30 -05:00
|
|
|
refbuf_release(to_release);
|
2004-12-07 16:06:26 -05:00
|
|
|
}
|
|
|
|
ogg_info->header_pages = NULL;
|
|
|
|
ogg_info->header_pages_tail = NULL;
|
|
|
|
ogg_info->bos_end = &ogg_info->header_pages;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* release the memory used for the codec and header pages from the module */
|
2014-11-30 15:32:30 -05:00
|
|
|
static void free_ogg_codecs(ogg_state_t *ogg_info)
|
2004-12-07 16:06:26 -05:00
|
|
|
{
|
|
|
|
ogg_codec_t *codec;
|
|
|
|
|
|
|
|
if (ogg_info == NULL)
|
|
|
|
return;
|
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
format_ogg_free_headers(ogg_info);
|
2004-12-07 16:06:26 -05:00
|
|
|
|
|
|
|
/* now free the codecs */
|
|
|
|
codec = ogg_info->codecs;
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_DEBUG("freeing codecs");
|
2004-12-07 16:06:26 -05:00
|
|
|
while (codec)
|
|
|
|
{
|
|
|
|
ogg_codec_t *next = codec->next;
|
2005-04-18 10:32:26 -04:00
|
|
|
if (codec->possible_start)
|
2014-11-30 15:32:30 -05:00
|
|
|
refbuf_release(codec->possible_start);
|
|
|
|
codec->codec_free(ogg_info, codec);
|
2004-12-07 16:06:26 -05:00
|
|
|
codec = next;
|
|
|
|
}
|
|
|
|
ogg_info->codecs = NULL;
|
|
|
|
ogg_info->current = NULL;
|
|
|
|
ogg_info->bos_completed = 0;
|
2005-08-07 10:50:59 -04:00
|
|
|
ogg_info->codec_count = 0;
|
2004-12-07 16:06:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
int format_ogg_get_plugin(source_t *source)
|
2004-12-07 16:06:26 -05:00
|
|
|
{
|
|
|
|
format_plugin_t *plugin;
|
2014-11-30 15:32:30 -05:00
|
|
|
ogg_state_t *state = calloc(1, sizeof(ogg_state_t));
|
2004-12-07 16:06:26 -05:00
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
plugin = (format_plugin_t *) calloc(1, sizeof(format_plugin_t));
|
2004-12-07 16:06:26 -05:00
|
|
|
|
|
|
|
plugin->type = FORMAT_TYPE_OGG;
|
|
|
|
plugin->get_buffer = ogg_get_buffer;
|
|
|
|
plugin->write_buf_to_client = write_buf_to_client;
|
|
|
|
plugin->write_buf_to_file = write_ogg_to_file;
|
|
|
|
plugin->create_client_data = create_ogg_client_data;
|
|
|
|
plugin->free_plugin = format_ogg_free_plugin;
|
|
|
|
plugin->set_tag = NULL;
|
2008-04-22 20:32:20 -04:00
|
|
|
if (strcmp (httpp_getvar (source->parser, "content-type"), "application/x-ogg") == 0)
|
|
|
|
httpp_setvar (source->parser, "content-type", "application/ogg");
|
|
|
|
plugin->contenttype = httpp_getvar (source->parser, "content-type");
|
2004-12-07 16:06:26 -05:00
|
|
|
|
|
|
|
ogg_sync_init (&state->oy);
|
2014-12-09 11:08:27 -05:00
|
|
|
vorbis_comment_init(&plugin->vc);
|
2004-12-07 16:06:26 -05:00
|
|
|
|
|
|
|
plugin->_state = state;
|
|
|
|
source->format = plugin;
|
|
|
|
state->mount = source->mount;
|
|
|
|
state->bos_end = &state->header_pages;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-17 07:15:09 -05:00
|
|
|
static void format_ogg_free_plugin (format_plugin_t *plugin)
|
2004-12-07 16:06:26 -05:00
|
|
|
{
|
|
|
|
ogg_state_t *state = plugin->_state;
|
|
|
|
|
|
|
|
/* free memory associated with this plugin instance */
|
|
|
|
free_ogg_codecs (state);
|
|
|
|
|
|
|
|
ogg_sync_clear (&state->oy);
|
2014-12-09 11:08:27 -05:00
|
|
|
|
2004-12-07 16:06:26 -05:00
|
|
|
free (state);
|
|
|
|
|
2014-12-09 11:08:27 -05:00
|
|
|
vorbis_comment_clear(&plugin->vc);
|
|
|
|
|
2004-12-07 16:06:26 -05:00
|
|
|
free (plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* a new BOS page has been seen so check which codec it is */
|
|
|
|
static int process_initial_page (format_plugin_t *plugin, ogg_page *page)
|
|
|
|
{
|
|
|
|
ogg_state_t *ogg_info = plugin->_state;
|
|
|
|
ogg_codec_t *codec;
|
|
|
|
|
|
|
|
if (ogg_info->bos_completed)
|
|
|
|
{
|
|
|
|
ogg_info->bitrate = 0;
|
|
|
|
ogg_info->codec_sync = NULL;
|
|
|
|
/* need to zap old list of codecs when next group of BOS pages appear */
|
2014-11-30 15:32:30 -05:00
|
|
|
free_ogg_codecs(ogg_info);
|
2004-12-07 16:06:26 -05:00
|
|
|
}
|
|
|
|
do
|
|
|
|
{
|
2005-08-07 10:50:59 -04:00
|
|
|
if (ogg_info->codec_count > 10)
|
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_ERROR("many codecs in stream, playing safe, dropping source");
|
2005-08-07 10:50:59 -04:00
|
|
|
ogg_info->error = 1;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-30 15:32:30 -05:00
|
|
|
codec = initial_vorbis_page(plugin, page);
|
2004-12-07 16:06:26 -05:00
|
|
|
if (codec)
|
|
|
|
break;
|
|
|
|
#ifdef HAVE_THEORA
|
|
|
|
codec = initial_theora_page (plugin, page);
|
|
|
|
if (codec)
|
|
|
|
break;
|
|
|
|
#endif
|
2014-11-30 15:32:30 -05:00
|
|
|
codec = initial_midi_page(plugin, page);
|
2005-05-07 07:01:35 -04:00
|
|
|
if (codec)
|
|
|
|
break;
|
2014-11-30 15:32:30 -05:00
|
|
|
codec = initial_flac_page(plugin, page);
|
2005-05-07 07:01:35 -04:00
|
|
|
if (codec)
|
|
|
|
break;
|
|
|
|
#ifdef HAVE_SPEEX
|
|
|
|
codec = initial_speex_page (plugin, page);
|
|
|
|
if (codec)
|
|
|
|
break;
|
|
|
|
#endif
|
2014-11-30 15:32:30 -05:00
|
|
|
codec = initial_kate_page(plugin, page);
|
2008-04-18 22:11:37 -04:00
|
|
|
if (codec)
|
|
|
|
break;
|
2014-11-30 15:32:30 -05:00
|
|
|
codec = initial_skeleton_page(plugin, page);
|
2012-05-24 12:04:27 -04:00
|
|
|
if (codec)
|
|
|
|
break;
|
2014-11-30 15:32:30 -05:00
|
|
|
codec = initial_opus_page(plugin, page);
|
2008-04-18 22:11:37 -04:00
|
|
|
if (codec)
|
|
|
|
break;
|
2005-05-07 07:01:35 -04:00
|
|
|
|
2004-12-07 16:06:26 -05:00
|
|
|
/* any others */
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_ERROR("Seen BOS page with unknown type");
|
2006-04-20 12:51:27 -04:00
|
|
|
ogg_info->error = 1;
|
2004-12-07 16:06:26 -05:00
|
|
|
return -1;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
if (codec)
|
|
|
|
{
|
|
|
|
/* add codec to list */
|
|
|
|
codec->next = ogg_info->codecs;
|
|
|
|
ogg_info->codecs = codec;
|
2005-08-07 10:50:59 -04:00
|
|
|
ogg_info->codec_count++;
|
2004-12-07 16:06:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This is called when there has been a change in the metadata. Usually
|
|
|
|
* artist and title are provided separately so here we update the stats
|
|
|
|
* and write log entry if required.
|
|
|
|
*/
|
2014-11-30 15:32:30 -05:00
|
|
|
static void update_comments(source_t *source)
|
2004-12-07 16:06:26 -05:00
|
|
|
{
|
|
|
|
ogg_state_t *ogg_info = source->format->_state;
|
2014-12-09 11:08:27 -05:00
|
|
|
char *title = vorbis_comment_query(&source->format->vc, "TITLE", 0);
|
|
|
|
char *artist = vorbis_comment_query(&source->format->vc, "ARTIST", 0);
|
2004-12-07 16:06:26 -05:00
|
|
|
char *metadata = NULL;
|
2005-01-03 12:48:54 -05:00
|
|
|
unsigned int len = 1; /* space for the nul byte at least */
|
2004-12-09 19:11:16 -05:00
|
|
|
ogg_codec_t *codec;
|
|
|
|
char codec_names [100] = "";
|
2004-12-07 16:06:26 -05:00
|
|
|
|
2014-12-09 11:08:27 -05:00
|
|
|
if (artist)
|
2004-12-07 16:06:26 -05:00
|
|
|
{
|
|
|
|
if (title)
|
|
|
|
{
|
|
|
|
len += strlen(artist) + strlen(title) + 3;
|
|
|
|
metadata = calloc (1, len);
|
|
|
|
snprintf (metadata, len, "%s - %s", artist, title);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len += strlen(artist);
|
|
|
|
metadata = calloc (1, len);
|
|
|
|
snprintf (metadata, len, "%s", artist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (title)
|
|
|
|
{
|
|
|
|
len += strlen (title);
|
|
|
|
metadata = calloc (1, len);
|
|
|
|
snprintf (metadata, len, "%s", title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (metadata)
|
|
|
|
{
|
|
|
|
logging_playlist (source->mount, metadata, source->listeners);
|
|
|
|
free (metadata);
|
|
|
|
}
|
|
|
|
stats_event (source->mount, "artist", artist);
|
|
|
|
stats_event (source->mount, "title", title);
|
2004-12-09 19:11:16 -05:00
|
|
|
|
2015-03-28 12:15:49 -04:00
|
|
|
playlist_push_track(source->history, &source->format->vc);
|
|
|
|
|
2004-12-09 19:11:16 -05:00
|
|
|
codec = ogg_info->codecs;
|
|
|
|
while (codec)
|
|
|
|
{
|
|
|
|
if (codec->name)
|
|
|
|
{
|
|
|
|
int len = strlen (codec_names);
|
|
|
|
int remaining = sizeof (codec_names) - len;
|
|
|
|
char *where = codec_names + len;
|
2004-12-17 15:03:26 -05:00
|
|
|
char *separator = "/";
|
2004-12-09 19:11:16 -05:00
|
|
|
if (len == 0)
|
|
|
|
separator = "";
|
|
|
|
snprintf (where, remaining, "%s%s", separator, codec->name);
|
|
|
|
}
|
|
|
|
codec = codec->next;
|
|
|
|
}
|
|
|
|
stats_event (source->mount, "subtype", codec_names);
|
2004-12-07 16:06:26 -05:00
|
|
|
yp_touch (source->mount);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* called when preparing a refbuf with audio data to be passed
|
|
|
|
* back for queueing
|
|
|
|
*/
|
|
|
|
static refbuf_t *complete_buffer (source_t *source, refbuf_t *refbuf)
|
|
|
|
{
|
|
|
|
ogg_state_t *ogg_info = source->format->_state;
|
|
|
|
refbuf_t *header = ogg_info->header_pages;
|
|
|
|
|
|
|
|
while (header)
|
|
|
|
{
|
|
|
|
refbuf_addref (header);
|
|
|
|
header = header->next;
|
|
|
|
}
|
|
|
|
refbuf->associated = ogg_info->header_pages;
|
|
|
|
|
|
|
|
if (ogg_info->log_metadata)
|
|
|
|
{
|
|
|
|
update_comments (source);
|
|
|
|
ogg_info->log_metadata = 0;
|
|
|
|
}
|
|
|
|
/* listeners can start anywhere unless the codecs themselves are
|
|
|
|
* marking starting points */
|
|
|
|
if (ogg_info->codec_sync == NULL)
|
|
|
|
refbuf->sync_point = 1;
|
|
|
|
return refbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* process the incoming page. this requires searching through the
|
|
|
|
* currently known codecs that have been seen in the stream
|
|
|
|
*/
|
2014-12-09 11:08:27 -05:00
|
|
|
static refbuf_t *process_ogg_page(ogg_state_t *ogg_info, ogg_page *page, format_plugin_t *plugin)
|
2004-12-07 16:06:26 -05:00
|
|
|
{
|
|
|
|
ogg_codec_t *codec = ogg_info->codecs;
|
|
|
|
refbuf_t *refbuf = NULL;
|
|
|
|
|
|
|
|
while (codec)
|
|
|
|
{
|
|
|
|
if (ogg_page_serialno (page) == codec->os.serialno)
|
|
|
|
{
|
|
|
|
if (codec->process_page)
|
2014-12-09 11:08:27 -05:00
|
|
|
refbuf = codec->process_page(ogg_info, codec, page, plugin);
|
2004-12-07 16:06:26 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
codec = codec->next;
|
|
|
|
}
|
|
|
|
ogg_info->current = codec;
|
|
|
|
return refbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* main plugin handler for getting a buffer for the queue. In here we
|
|
|
|
* just add an incoming page to the codecs and process it until either
|
|
|
|
* more data is needed or we prodice a buffer for the queue.
|
|
|
|
*/
|
2014-11-30 15:32:30 -05:00
|
|
|
static refbuf_t *ogg_get_buffer(source_t *source)
|
2004-12-07 16:06:26 -05:00
|
|
|
{
|
|
|
|
ogg_state_t *ogg_info = source->format->_state;
|
2005-05-08 09:51:05 -04:00
|
|
|
format_plugin_t *format = source->format;
|
2004-12-07 16:06:26 -05:00
|
|
|
char *data = NULL;
|
2005-08-07 10:50:59 -04:00
|
|
|
int bytes = 0;
|
2004-12-07 16:06:26 -05:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
ogg_page page;
|
2005-08-07 10:50:59 -04:00
|
|
|
refbuf_t *refbuf = NULL;
|
2004-12-07 16:06:26 -05:00
|
|
|
ogg_codec_t *codec = ogg_info->current;
|
|
|
|
|
|
|
|
/* if a codec has just been given a page then process it */
|
|
|
|
if (codec && codec->process)
|
|
|
|
{
|
2014-12-09 11:08:27 -05:00
|
|
|
refbuf = codec->process (ogg_info, codec, source->format);
|
2004-12-07 16:06:26 -05:00
|
|
|
if (refbuf)
|
|
|
|
return complete_buffer (source, refbuf);
|
|
|
|
|
|
|
|
ogg_info->current = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ogg_sync_pageout (&ogg_info->oy, &page) > 0)
|
|
|
|
{
|
|
|
|
if (ogg_page_bos (&page))
|
|
|
|
{
|
|
|
|
process_initial_page (source->format, &page);
|
|
|
|
}
|
2005-08-07 10:50:59 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ogg_info->bos_completed = 1;
|
2014-12-09 11:08:27 -05:00
|
|
|
refbuf = process_ogg_page (ogg_info, &page, source->format);
|
2005-08-07 10:50:59 -04:00
|
|
|
}
|
2004-12-07 16:06:26 -05:00
|
|
|
if (ogg_info->error)
|
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_ERROR("Problem processing stream");
|
2004-12-07 16:06:26 -05:00
|
|
|
source->running = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (refbuf)
|
|
|
|
return complete_buffer (source, refbuf);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* need more stream data */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* we need more data to continue getting pages */
|
|
|
|
data = ogg_sync_buffer (&ogg_info->oy, 4096);
|
|
|
|
|
2005-05-08 09:51:05 -04:00
|
|
|
bytes = client_read_bytes (source->client, data, 4096);
|
2005-08-07 10:50:59 -04:00
|
|
|
if (bytes <= 0)
|
2004-12-07 16:06:26 -05:00
|
|
|
{
|
|
|
|
ogg_sync_wrote (&ogg_info->oy, 0);
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-05-08 09:51:05 -04:00
|
|
|
format->read_bytes += bytes;
|
2004-12-07 16:06:26 -05:00
|
|
|
ogg_sync_wrote (&ogg_info->oy, bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-10 13:53:44 -05:00
|
|
|
static int create_ogg_client_data (source_t *source, client_t *client)
|
2004-12-07 16:06:26 -05:00
|
|
|
{
|
|
|
|
struct ogg_client *client_data = calloc (1, sizeof (struct ogg_client));
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (client_data)
|
|
|
|
{
|
|
|
|
client_data->headers_sent = 1;
|
|
|
|
client->format_data = client_data;
|
|
|
|
client->free_client_data = free_ogg_client_data;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void free_ogg_client_data (client_t *client)
|
|
|
|
{
|
|
|
|
free (client->format_data);
|
|
|
|
client->format_data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* send out the header pages. These are for all codecs but are
|
|
|
|
* in the order for the stream, ie BOS pages first
|
|
|
|
*/
|
|
|
|
static int send_ogg_headers (client_t *client, refbuf_t *headers)
|
|
|
|
{
|
|
|
|
struct ogg_client *client_data = client->format_data;
|
|
|
|
refbuf_t *refbuf;
|
|
|
|
int written = 0;
|
|
|
|
|
|
|
|
if (client_data->headers_sent)
|
|
|
|
{
|
|
|
|
client_data->header_page = headers;
|
|
|
|
client_data->pos = 0;
|
|
|
|
client_data->headers_sent = 0;
|
|
|
|
}
|
|
|
|
refbuf = client_data->header_page;
|
|
|
|
while (refbuf)
|
|
|
|
{
|
|
|
|
char *data = refbuf->data + client_data->pos;
|
|
|
|
unsigned len = refbuf->len - client_data->pos;
|
|
|
|
int ret;
|
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
ret = client_send_bytes(client, data, len);
|
2004-12-07 16:06:26 -05:00
|
|
|
if (ret > 0)
|
2014-11-30 15:32:30 -05:00
|
|
|
written += ret;
|
|
|
|
if (ret < (int) len)
|
2004-12-07 16:06:26 -05:00
|
|
|
return written ? written : -1;
|
|
|
|
client_data->pos += ret;
|
|
|
|
if (client_data->pos == refbuf->len)
|
|
|
|
{
|
|
|
|
refbuf = refbuf->next;
|
|
|
|
client_data->header_page = refbuf;
|
|
|
|
client_data->pos = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
client_data->headers_sent = 1;
|
|
|
|
client_data->headers = headers;
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* main client write routine for sending ogg data. Each refbuf has a
|
|
|
|
* single page so we only need to determine if there are new headers
|
|
|
|
*/
|
2014-11-30 15:32:30 -05:00
|
|
|
static int write_buf_to_client(client_t *client)
|
2004-12-07 16:06:26 -05:00
|
|
|
{
|
|
|
|
refbuf_t *refbuf = client->refbuf;
|
2005-06-03 11:35:52 -04:00
|
|
|
char *buf = refbuf->data + client->pos;
|
|
|
|
unsigned len = refbuf->len - client->pos;
|
2004-12-07 16:06:26 -05:00
|
|
|
struct ogg_client *client_data = client->format_data;
|
|
|
|
int ret, written = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (client_data->headers != refbuf->associated)
|
|
|
|
{
|
|
|
|
ret = send_ogg_headers (client, refbuf->associated);
|
|
|
|
if (client_data->headers_sent == 0)
|
|
|
|
break;
|
|
|
|
written += ret;
|
|
|
|
}
|
|
|
|
ret = client_send_bytes (client, buf, len);
|
|
|
|
|
|
|
|
if (ret > 0)
|
|
|
|
client->pos += ret;
|
|
|
|
|
|
|
|
if (ret < (int)len)
|
|
|
|
break;
|
|
|
|
written += ret;
|
|
|
|
/* we have now written the page(s) */
|
|
|
|
ret = 0;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
if (ret > 0)
|
|
|
|
written += ret;
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int write_ogg_data (struct source_tag *source, refbuf_t *refbuf)
|
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
|
|
|
|
if (fwrite (refbuf->data, 1, refbuf->len, source->dumpfile) != refbuf->len)
|
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_WARN("Write to dump file failed, disabling");
|
2004-12-07 16:06:26 -05:00
|
|
|
fclose (source->dumpfile);
|
|
|
|
source->dumpfile = NULL;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void write_ogg_to_file (struct source_tag *source, refbuf_t *refbuf)
|
|
|
|
{
|
|
|
|
ogg_state_t *ogg_info = source->format->_state;
|
|
|
|
|
|
|
|
if (ogg_info->file_headers != refbuf->associated)
|
|
|
|
{
|
|
|
|
refbuf_t *header = refbuf->associated;
|
|
|
|
while (header)
|
|
|
|
{
|
|
|
|
if (write_ogg_data (source, header) == 0)
|
|
|
|
return;
|
|
|
|
header = header->next;
|
|
|
|
}
|
|
|
|
ogg_info->file_headers = refbuf->associated;
|
|
|
|
}
|
|
|
|
write_ogg_data (source, refbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|