2012-05-18 13:33:17 -04:00
|
|
|
/* Icecast
|
|
|
|
*
|
2012-05-24 12:10:20 -04:00
|
|
|
* This program is distributed under the GNU General Public License,
|
2012-05-24 14:32:06 -04:00
|
|
|
* version 2. A copy of this license is included with this source.
|
|
|
|
* At your option, this specific source file can also be distributed
|
|
|
|
* under the GNU GPL version 3.
|
2012-05-18 13:33:17 -04:00
|
|
|
*
|
2012-05-24 12:10:20 -04:00
|
|
|
* Copyright 2012, David Richards, Mozilla Foundation,
|
2012-05-18 13:33:17 -04:00
|
|
|
* and others (see AUTHORS for details).
|
2014-11-29 07:41:19 -05:00
|
|
|
* Copyright 2014, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>.
|
2012-05-18 13:33:17 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* format_ebml.c
|
|
|
|
*
|
2012-06-13 17:24:23 -04:00
|
|
|
* format plugin for WebM/EBML
|
2012-05-18 13:33:17 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "refbuf.h"
|
|
|
|
#include "source.h"
|
|
|
|
#include "client.h"
|
|
|
|
|
|
|
|
#include "stats.h"
|
|
|
|
#include "format.h"
|
|
|
|
#include "format_ebml.h"
|
|
|
|
|
|
|
|
#define CATMODULE "format-ebml"
|
|
|
|
|
|
|
|
#include "logging.h"
|
|
|
|
|
|
|
|
#define EBML_HEADER_MAX_SIZE 131072
|
|
|
|
#define EBML_SLICE_SIZE 4096
|
|
|
|
|
2015-11-21 03:12:33 -05:00
|
|
|
typedef enum ebml_read_mode {
|
|
|
|
EBML_STATE_READING_HEADER = 0,
|
|
|
|
EBML_STATE_READING_CLUSTERS
|
|
|
|
} ebml_read_mode;
|
2012-05-18 13:33:17 -04:00
|
|
|
|
2015-11-21 03:16:07 -05:00
|
|
|
|
|
|
|
typedef enum ebml_chunk_type {
|
|
|
|
EBML_CHUNK_HEADER = 0,
|
|
|
|
EBML_CHUNK_CLUSTER_START,
|
|
|
|
EBML_CHUNK_CLUSTER_CONTINUE
|
|
|
|
} ebml_chunk_type;
|
|
|
|
|
2012-05-18 13:33:17 -04:00
|
|
|
typedef struct ebml_client_data_st ebml_client_data_t;
|
|
|
|
|
|
|
|
struct ebml_client_data_st {
|
|
|
|
|
|
|
|
refbuf_t *header;
|
|
|
|
int header_pos;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ebml_st {
|
|
|
|
|
2015-11-21 03:12:33 -05:00
|
|
|
ebml_read_mode output_state;
|
|
|
|
|
2012-05-18 13:33:17 -04:00
|
|
|
char *cluster_id;
|
|
|
|
int cluster_start;
|
2014-11-30 15:32:30 -05:00
|
|
|
|
2012-05-18 13:33:17 -04:00
|
|
|
int position;
|
|
|
|
unsigned char *input_buffer;
|
|
|
|
unsigned char *buffer;
|
2015-11-21 03:12:33 -05:00
|
|
|
|
2012-05-18 13:33:17 -04:00
|
|
|
int header_size;
|
|
|
|
int header_position;
|
|
|
|
int header_read_position;
|
|
|
|
unsigned char *header;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
static void ebml_free_plugin(format_plugin_t *plugin);
|
|
|
|
static refbuf_t *ebml_get_buffer(source_t *source);
|
|
|
|
static int ebml_write_buf_to_client(client_t *client);
|
|
|
|
static void ebml_write_buf_to_file(source_t *source, refbuf_t *refbuf);
|
|
|
|
static int ebml_create_client_data(source_t *source, client_t *client);
|
|
|
|
static void ebml_free_client_data(client_t *client);
|
2012-05-18 13:33:17 -04:00
|
|
|
|
|
|
|
static ebml_t *ebml_create();
|
|
|
|
static void ebml_destroy(ebml_t *ebml);
|
|
|
|
static int ebml_read_space(ebml_t *ebml);
|
2015-11-21 03:16:07 -05:00
|
|
|
static int ebml_read(ebml_t *ebml, char *buffer, int len, ebml_chunk_type *chunk_type);
|
2012-05-18 13:33:17 -04:00
|
|
|
static char *ebml_write_buffer(ebml_t *ebml, int len);
|
|
|
|
static int ebml_wrote(ebml_t *ebml, int len);
|
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
int format_ebml_get_plugin(source_t *source)
|
2012-05-18 13:33:17 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
ebml_source_state_t *ebml_source_state = calloc(1, sizeof(ebml_source_state_t));
|
|
|
|
format_plugin_t *plugin = calloc(1, sizeof(format_plugin_t));
|
|
|
|
|
|
|
|
plugin->get_buffer = ebml_get_buffer;
|
|
|
|
plugin->write_buf_to_client = ebml_write_buf_to_client;
|
|
|
|
plugin->create_client_data = ebml_create_client_data;
|
|
|
|
plugin->free_plugin = ebml_free_plugin;
|
|
|
|
plugin->write_buf_to_file = ebml_write_buf_to_file;
|
|
|
|
plugin->set_tag = NULL;
|
|
|
|
plugin->apply_settings = NULL;
|
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
plugin->contenttype = httpp_getvar(source->parser, "content-type");
|
2012-05-18 13:33:17 -04:00
|
|
|
|
|
|
|
plugin->_state = ebml_source_state;
|
2014-12-09 11:08:27 -05:00
|
|
|
vorbis_comment_init(&plugin->vc);
|
2012-05-18 13:33:17 -04:00
|
|
|
source->format = plugin;
|
|
|
|
|
|
|
|
ebml_source_state->ebml = ebml_create();
|
2015-11-13 17:42:27 -05:00
|
|
|
|
2012-05-18 13:33:17 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
static void ebml_free_plugin(format_plugin_t *plugin)
|
2012-05-18 13:33:17 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
ebml_source_state_t *ebml_source_state = plugin->_state;
|
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
refbuf_release(ebml_source_state->header);
|
2012-05-18 13:33:17 -04:00
|
|
|
ebml_destroy(ebml_source_state->ebml);
|
2014-11-30 15:32:30 -05:00
|
|
|
free(ebml_source_state);
|
2014-12-09 11:08:27 -05:00
|
|
|
vorbis_comment_clear(&plugin->vc);
|
2014-11-30 15:32:30 -05:00
|
|
|
free(plugin);
|
2012-05-18 13:33:17 -04:00
|
|
|
}
|
|
|
|
|
2015-11-13 17:42:27 -05:00
|
|
|
/* Write to a client from the header buffer.
|
|
|
|
*/
|
2014-11-30 15:32:30 -05:00
|
|
|
static int send_ebml_header(client_t *client)
|
2012-05-18 13:33:17 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
ebml_client_data_t *ebml_client_data = client->format_data;
|
|
|
|
int len = EBML_SLICE_SIZE;
|
|
|
|
int ret;
|
|
|
|
|
2015-01-10 13:53:44 -05:00
|
|
|
if (ebml_client_data->header->len - ebml_client_data->header_pos < len)
|
2012-05-18 13:33:17 -04:00
|
|
|
{
|
|
|
|
len = ebml_client_data->header->len - ebml_client_data->header_pos;
|
|
|
|
}
|
2015-01-10 13:53:44 -05:00
|
|
|
ret = client_send_bytes (client,
|
2012-05-18 13:33:17 -04:00
|
|
|
ebml_client_data->header->data + ebml_client_data->header_pos,
|
|
|
|
len);
|
|
|
|
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
ebml_client_data->header_pos += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-13 17:42:27 -05:00
|
|
|
/* Initial write-to-client function.
|
|
|
|
*/
|
2012-05-18 13:33:17 -04:00
|
|
|
static int ebml_write_buf_to_client (client_t *client)
|
|
|
|
{
|
|
|
|
|
|
|
|
ebml_client_data_t *ebml_client_data = client->format_data;
|
|
|
|
|
|
|
|
if (ebml_client_data->header_pos != ebml_client_data->header->len)
|
|
|
|
{
|
|
|
|
return send_ebml_header (client);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-13 17:42:27 -05:00
|
|
|
/* Now that the header's sent, short-circuit to the generic
|
|
|
|
* write-refbufs function. */
|
2012-05-18 13:33:17 -04:00
|
|
|
client->write_to_client = format_generic_write_to_client;
|
|
|
|
return client->write_to_client(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-13 17:42:27 -05:00
|
|
|
/* Return a refbuf to add to the queue.
|
|
|
|
*/
|
2014-11-30 15:32:30 -05:00
|
|
|
static refbuf_t *ebml_get_buffer(source_t *source)
|
2012-05-18 13:33:17 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
ebml_source_state_t *ebml_source_state = source->format->_state;
|
|
|
|
format_plugin_t *format = source->format;
|
|
|
|
char *data = NULL;
|
|
|
|
int bytes = 0;
|
2015-11-21 03:16:07 -05:00
|
|
|
ebml_chunk_type chunk_type;
|
2012-05-18 13:33:17 -04:00
|
|
|
refbuf_t *refbuf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
|
|
|
|
if ((bytes = ebml_read_space(ebml_source_state->ebml)) > 0)
|
|
|
|
{
|
2015-11-13 17:42:27 -05:00
|
|
|
/* A chunk is available for reading */
|
2012-05-18 13:33:17 -04:00
|
|
|
refbuf = refbuf_new(bytes);
|
2015-11-21 03:16:07 -05:00
|
|
|
ebml_read(ebml_source_state->ebml, refbuf->data, bytes, &chunk_type);
|
2012-05-18 13:33:17 -04:00
|
|
|
|
|
|
|
if (ebml_source_state->header == NULL)
|
|
|
|
{
|
2015-11-13 17:42:27 -05:00
|
|
|
/* Capture header before adding clusters to the queue */
|
2012-05-18 13:33:17 -04:00
|
|
|
ebml_source_state->header = refbuf;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-11-21 03:16:07 -05:00
|
|
|
/* ICECAST_LOG_DEBUG("EBML: generated refbuf, size %i : %hhi %hhi %hhi",
|
|
|
|
* bytes, refbuf->data[0], refbuf->data[1], refbuf->data[2]);
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (chunk_type == EBML_CHUNK_CLUSTER_START)
|
2012-05-18 13:33:17 -04:00
|
|
|
{
|
|
|
|
refbuf->sync_point = 1;
|
2015-11-21 03:16:07 -05:00
|
|
|
/* ICECAST_LOG_DEBUG("EBML: ^ was sync point"); */
|
2012-05-18 13:33:17 -04:00
|
|
|
}
|
|
|
|
return refbuf;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-13 17:42:27 -05:00
|
|
|
/* Feed more bytes into the parser */
|
2012-05-18 13:33:17 -04:00
|
|
|
data = ebml_write_buffer(ebml_source_state->ebml, EBML_SLICE_SIZE);
|
|
|
|
bytes = client_read_bytes (source->client, data, EBML_SLICE_SIZE);
|
|
|
|
if (bytes <= 0)
|
|
|
|
{
|
|
|
|
ebml_wrote (ebml_source_state->ebml, 0);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
format->read_bytes += bytes;
|
|
|
|
ret = ebml_wrote (ebml_source_state->ebml, bytes);
|
|
|
|
if (ret != bytes) {
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_ERROR("Problem processing stream");
|
2012-05-18 13:33:17 -04:00
|
|
|
source->running = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 17:42:27 -05:00
|
|
|
/* Initialize client state.
|
|
|
|
*/
|
2014-11-30 15:32:30 -05:00
|
|
|
static int ebml_create_client_data(source_t *source, client_t *client)
|
2012-05-18 13:33:17 -04:00
|
|
|
{
|
2015-04-08 05:36:59 -04:00
|
|
|
ebml_client_data_t *ebml_client_data;
|
2012-05-18 13:33:17 -04:00
|
|
|
ebml_source_state_t *ebml_source_state = source->format->_state;
|
|
|
|
|
2015-04-08 05:36:59 -04:00
|
|
|
if (!ebml_source_state->header)
|
|
|
|
return -1;
|
2012-05-18 13:33:17 -04:00
|
|
|
|
2015-04-08 05:36:59 -04:00
|
|
|
ebml_client_data = calloc(1, sizeof(ebml_client_data_t));
|
|
|
|
if (!ebml_client_data)
|
|
|
|
return -1;
|
2012-05-18 13:33:17 -04:00
|
|
|
|
2015-04-08 05:36:59 -04:00
|
|
|
ebml_client_data->header = ebml_source_state->header;
|
|
|
|
refbuf_addref(ebml_client_data->header);
|
|
|
|
client->format_data = ebml_client_data;
|
|
|
|
client->free_client_data = ebml_free_client_data;
|
|
|
|
return 0;
|
2012-05-18 13:33:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void ebml_free_client_data (client_t *client)
|
|
|
|
{
|
|
|
|
|
|
|
|
ebml_client_data_t *ebml_client_data = client->format_data;
|
|
|
|
|
|
|
|
refbuf_release (ebml_client_data->header);
|
|
|
|
free (client->format_data);
|
|
|
|
client->format_data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void ebml_write_buf_to_file_fail (source_t *source)
|
|
|
|
{
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_WARN("Write to dump file failed, disabling");
|
2012-05-18 13:33:17 -04:00
|
|
|
fclose (source->dumpfile);
|
|
|
|
source->dumpfile = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void ebml_write_buf_to_file (source_t *source, refbuf_t *refbuf)
|
|
|
|
{
|
|
|
|
|
|
|
|
ebml_source_state_t *ebml_source_state = source->format->_state;
|
|
|
|
|
|
|
|
if (ebml_source_state->file_headers_written == 0)
|
|
|
|
{
|
|
|
|
if (fwrite (ebml_source_state->header->data, 1,
|
2015-01-10 13:53:44 -05:00
|
|
|
ebml_source_state->header->len,
|
2012-05-18 13:33:17 -04:00
|
|
|
source->dumpfile) != ebml_source_state->header->len)
|
|
|
|
ebml_write_buf_to_file_fail(source);
|
|
|
|
else
|
|
|
|
ebml_source_state->file_headers_written = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fwrite (refbuf->data, 1, refbuf->len, source->dumpfile) != refbuf->len)
|
|
|
|
{
|
|
|
|
ebml_write_buf_to_file_fail(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* internal ebml parsing */
|
|
|
|
|
|
|
|
static void ebml_destroy(ebml_t *ebml)
|
|
|
|
{
|
|
|
|
|
|
|
|
free(ebml->header);
|
|
|
|
free(ebml->input_buffer);
|
|
|
|
free(ebml->buffer);
|
|
|
|
free(ebml);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static ebml_t *ebml_create()
|
|
|
|
{
|
|
|
|
|
|
|
|
ebml_t *ebml = calloc(1, sizeof(ebml_t));
|
|
|
|
|
2015-11-21 03:12:33 -05:00
|
|
|
ebml->output_state = EBML_STATE_READING_HEADER;
|
|
|
|
|
2012-05-18 13:33:17 -04:00
|
|
|
ebml->header = calloc(1, EBML_HEADER_MAX_SIZE);
|
|
|
|
ebml->buffer = calloc(1, EBML_SLICE_SIZE * 4);
|
|
|
|
ebml->input_buffer = calloc(1, EBML_SLICE_SIZE);
|
|
|
|
|
|
|
|
ebml->cluster_id = "\x1F\x43\xB6\x75";
|
|
|
|
|
2015-11-21 03:16:07 -05:00
|
|
|
ebml->cluster_start = -1;
|
2012-05-18 13:33:17 -04:00
|
|
|
|
|
|
|
return ebml;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-13 17:42:27 -05:00
|
|
|
/* Return the size of a buffer needed to store the next
|
|
|
|
* chunk that ebml_read can yield.
|
|
|
|
*/
|
2012-05-18 13:33:17 -04:00
|
|
|
static int ebml_read_space(ebml_t *ebml)
|
|
|
|
{
|
|
|
|
|
|
|
|
int read_space;
|
|
|
|
|
2015-11-21 03:12:33 -05:00
|
|
|
switch (ebml->output_state) {
|
|
|
|
case EBML_STATE_READING_HEADER:
|
|
|
|
|
|
|
|
if (ebml->header_size != 0) {
|
|
|
|
/* The header can be read */
|
|
|
|
return ebml->header_size;
|
|
|
|
} else {
|
|
|
|
/* The header's not ready yet */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EBML_STATE_READING_CLUSTERS:
|
|
|
|
|
|
|
|
if (ebml->cluster_start > 0) {
|
|
|
|
/* return up until just before a new cluster starts */
|
|
|
|
read_space = ebml->cluster_start;
|
|
|
|
} else {
|
2015-11-21 03:16:07 -05:00
|
|
|
/* return what we have */
|
|
|
|
read_space = ebml->position;
|
2015-11-21 03:12:33 -05:00
|
|
|
}
|
2014-11-30 15:32:30 -05:00
|
|
|
|
2015-11-21 03:12:33 -05:00
|
|
|
return read_space;
|
2012-05-18 13:33:17 -04:00
|
|
|
}
|
2015-11-21 03:12:33 -05:00
|
|
|
|
|
|
|
ICECAST_LOG_ERROR("EBML: Invalid parser read state");
|
|
|
|
return 0;
|
2012-05-18 13:33:17 -04:00
|
|
|
}
|
|
|
|
|
2015-11-13 17:42:27 -05:00
|
|
|
/* Return a chunk of the EBML/MKV/WebM stream.
|
2015-11-21 03:12:33 -05:00
|
|
|
* The header will be buffered until it can be returned as one chunk.
|
2015-11-13 17:42:27 -05:00
|
|
|
* A cluster element's opening tag will always start a new chunk.
|
2015-11-21 03:16:07 -05:00
|
|
|
*
|
|
|
|
* chunk_type will be set to indicate if the chunk is the header,
|
|
|
|
* the start of a cluster, or continuing the current cluster.
|
2015-11-13 17:42:27 -05:00
|
|
|
*/
|
2015-11-21 03:16:07 -05:00
|
|
|
static int ebml_read(ebml_t *ebml, char *buffer, int len, ebml_chunk_type *chunk_type)
|
2012-05-18 13:33:17 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
int read_space;
|
|
|
|
int to_read;
|
2015-11-21 03:16:07 -05:00
|
|
|
|
|
|
|
*chunk_type = EBML_CHUNK_HEADER;
|
2012-05-18 13:33:17 -04:00
|
|
|
|
2015-11-13 17:42:27 -05:00
|
|
|
if (len < 1) {
|
2012-05-18 13:33:17 -04:00
|
|
|
return 0;
|
2015-11-13 17:42:27 -05:00
|
|
|
}
|
2012-05-18 13:33:17 -04:00
|
|
|
|
2015-11-21 03:12:33 -05:00
|
|
|
switch (ebml->output_state) {
|
|
|
|
case EBML_STATE_READING_HEADER:
|
|
|
|
|
|
|
|
if (ebml->header_size != 0)
|
|
|
|
{
|
|
|
|
/* Can read a chunk of the header */
|
|
|
|
read_space = ebml->header_size - ebml->header_read_position;
|
2012-05-18 13:33:17 -04:00
|
|
|
|
2015-11-21 03:12:33 -05:00
|
|
|
if (read_space >= len) {
|
|
|
|
to_read = len;
|
|
|
|
} else {
|
|
|
|
to_read = read_space;
|
|
|
|
}
|
2012-05-18 13:33:17 -04:00
|
|
|
|
2015-11-21 03:12:33 -05:00
|
|
|
memcpy(buffer, ebml->header, to_read);
|
|
|
|
ebml->header_read_position += to_read;
|
2015-11-21 03:16:07 -05:00
|
|
|
|
|
|
|
*chunk_type = EBML_CHUNK_HEADER;
|
|
|
|
|
2015-11-21 03:12:33 -05:00
|
|
|
if (ebml->header_read_position == ebml->header_size) {
|
|
|
|
ebml->output_state = EBML_STATE_READING_CLUSTERS;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* The header's not ready yet */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EBML_STATE_READING_CLUSTERS:
|
2015-11-13 17:42:27 -05:00
|
|
|
|
2015-11-21 03:16:07 -05:00
|
|
|
*chunk_type = EBML_CHUNK_CLUSTER_CONTINUE;
|
|
|
|
read_space = ebml->position;
|
|
|
|
|
|
|
|
if (ebml->cluster_start == 0) {
|
|
|
|
/* new cluster is starting now */
|
|
|
|
*chunk_type = EBML_CHUNK_CLUSTER_START;
|
|
|
|
|
|
|
|
/* mark end of cluster */
|
|
|
|
ebml->cluster_start = -1;
|
|
|
|
} else if (ebml->cluster_start > 0) {
|
2015-11-21 03:12:33 -05:00
|
|
|
/* return up until just before a new cluster starts */
|
|
|
|
read_space = ebml->cluster_start;
|
|
|
|
}
|
2014-11-30 15:32:30 -05:00
|
|
|
|
2015-11-21 03:12:33 -05:00
|
|
|
if (read_space < 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-05-18 13:33:17 -04:00
|
|
|
|
2015-11-21 03:12:33 -05:00
|
|
|
if (read_space >= len ) {
|
2012-05-18 13:33:17 -04:00
|
|
|
to_read = len;
|
2015-11-13 17:42:27 -05:00
|
|
|
} else {
|
2012-05-18 13:33:17 -04:00
|
|
|
to_read = read_space;
|
2015-11-13 17:42:27 -05:00
|
|
|
}
|
2012-05-18 13:33:17 -04:00
|
|
|
|
2015-11-21 03:12:33 -05:00
|
|
|
memcpy(buffer, ebml->buffer, to_read);
|
|
|
|
|
|
|
|
/* Shift unread data down to the start of the buffer */
|
|
|
|
memmove(ebml->buffer, ebml->buffer + to_read, ebml->position - to_read);
|
|
|
|
ebml->position -= to_read;
|
2012-05-18 13:33:17 -04:00
|
|
|
|
2015-11-21 03:12:33 -05:00
|
|
|
if (ebml->cluster_start > 0) {
|
|
|
|
ebml->cluster_start -= to_read;
|
2015-11-13 17:42:27 -05:00
|
|
|
}
|
2015-11-21 03:12:33 -05:00
|
|
|
|
|
|
|
break;
|
2012-05-18 13:33:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return to_read;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *ebml_write_buffer(ebml_t *ebml, int len)
|
|
|
|
{
|
|
|
|
|
2014-11-30 15:32:30 -05:00
|
|
|
return (char *) ebml->input_buffer;
|
2012-05-18 13:33:17 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-13 17:42:27 -05:00
|
|
|
/* Process data that has been written to the EBML parser's input buffer.
|
|
|
|
*/
|
2012-05-18 13:33:17 -04:00
|
|
|
static int ebml_wrote(ebml_t *ebml, int len)
|
|
|
|
{
|
|
|
|
|
|
|
|
int b;
|
|
|
|
|
2015-11-13 17:42:27 -05:00
|
|
|
if (ebml->header_size == 0) {
|
|
|
|
/* Still reading header */
|
|
|
|
if ((ebml->header_position + len) > EBML_HEADER_MAX_SIZE) {
|
2014-10-31 04:46:58 -04:00
|
|
|
ICECAST_LOG_ERROR("EBML Header too large, failing");
|
2012-05-18 13:33:17 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2014-11-29 07:41:19 -05:00
|
|
|
|
2015-01-10 13:53:44 -05:00
|
|
|
/*
|
|
|
|
ICECAST_LOG_DEBUG("EBML: Adding to header, ofset is %d size is %d adding %d",
|
2014-11-29 07:41:19 -05:00
|
|
|
ebml->header_size, ebml->header_position, len);
|
|
|
|
*/
|
2015-01-10 13:53:44 -05:00
|
|
|
|
2012-05-18 13:33:17 -04:00
|
|
|
memcpy(ebml->header + ebml->header_position, ebml->input_buffer, len);
|
|
|
|
ebml->header_position += len;
|
2015-11-13 17:42:27 -05:00
|
|
|
} else {
|
|
|
|
/* Header's already been determined, read into data buffer */
|
2012-05-18 13:33:17 -04:00
|
|
|
memcpy(ebml->buffer + ebml->position, ebml->input_buffer, len);
|
|
|
|
}
|
2015-01-10 13:53:44 -05:00
|
|
|
|
2015-11-21 03:16:07 -05:00
|
|
|
for (b = 0; b <= len - 4; b++)
|
2012-05-18 13:33:17 -04:00
|
|
|
{
|
2015-11-13 17:42:27 -05:00
|
|
|
/* Scan for cluster start marker.
|
|
|
|
* False positives are possible, but unlikely, and only
|
|
|
|
* permanently corrupt a stream if they occur while scanning
|
|
|
|
* the initial header. Else, a client can reconnect in a few
|
|
|
|
* seconds and get a real sync point.
|
|
|
|
*/
|
2012-05-18 13:33:17 -04:00
|
|
|
if (!memcmp(ebml->input_buffer + b, ebml->cluster_id, 4))
|
|
|
|
{
|
2014-11-29 07:41:19 -05:00
|
|
|
/*
|
|
|
|
ICECAST_LOG_DEBUG("EBML: found cluster");
|
|
|
|
*/
|
2015-01-10 13:53:44 -05:00
|
|
|
|
2012-05-18 13:33:17 -04:00
|
|
|
if (ebml->header_size == 0)
|
|
|
|
{
|
2015-11-13 17:42:27 -05:00
|
|
|
/* We were looking for the header end; now we've found it */
|
2012-05-18 13:33:17 -04:00
|
|
|
ebml->header_size = ebml->header_position - len + b;
|
2015-11-13 17:42:27 -05:00
|
|
|
|
|
|
|
/* Shift data after the header into the data buffer */
|
2012-05-18 13:33:17 -04:00
|
|
|
memcpy(ebml->buffer, ebml->input_buffer + b, len - b);
|
|
|
|
ebml->position = len - b;
|
2015-11-13 17:42:27 -05:00
|
|
|
|
|
|
|
/* Mark the start of the data as the first sync point */
|
2015-11-21 03:16:07 -05:00
|
|
|
ebml->cluster_start = 0;
|
2012-05-18 13:33:17 -04:00
|
|
|
return len;
|
2015-11-13 17:42:27 -05:00
|
|
|
} else {
|
|
|
|
/* We've located a sync point in the data stream */
|
2012-05-18 13:33:17 -04:00
|
|
|
ebml->cluster_start = ebml->position + b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-30 15:32:30 -05:00
|
|
|
|
2012-05-18 13:33:17 -04:00
|
|
|
ebml->position += len;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
|
|
|
|
}
|