Commit fcb92d19 authored by Michael Smith's avatar Michael Smith
Browse files

Implement ability to save all streams on output, so you can actually save

what was being streamed out, instead of the significantly less useful saving
of what was fed in.

svn path=/trunk/ices/; revision=2080
parent a952e3cd
......@@ -44,7 +44,7 @@ typedef struct _instance_tag
int channels;
/* private */
FILE *savefile;
int serial;
int buffer_failures;
int died;
......
......@@ -231,10 +231,11 @@ void input_loop(void)
stream_description *arg = calloc(1, sizeof(stream_description));
arg->stream = instance;
arg->input = inmod;
/*
if(instance->savefilename != NULL)
thread_create("savefile", savefile_stream, arg, 1);
else
thread_create("stream", ices_instance_stream, arg, 1);
*/
thread_create("stream", ices_instance_stream, arg, 1);
instance = instance->next;
}
......
......@@ -7,6 +7,8 @@
* Public License, version 2. You may use, modify, and redistribute
* it under the terms of this license. A copy should be included
* with this source.
*
* NOTE: Not currently actually used.
*/
#include <stdio.h>
......
......@@ -44,7 +44,6 @@
void *ices_instance_stream(void *arg)
{
int ret;
int errors=0;
ref_buffer *buffer;
stream_description *sdsc = arg;
instance_t *stream = sdsc->stream;
......@@ -88,6 +87,16 @@ void *ices_instance_stream(void *arg)
else if(reencoding)
sdsc->reenc = reencode_init(stream);
if(stream->savefilename != NULL)
{
stream->savefile = fopen(stream->savefilename, "wb");
if(!stream->savefile)
LOG_ERROR2("Failed to open stream save file %s: %s",
stream->savefilename, strerror(errno));
else
LOG_INFO1("Saving stream to file %s", stream->savefilename);
}
if(shout_connect(&sdsc->conn))
{
LOG_INFO3("Connected to server: %s:%d%s",
......@@ -95,7 +104,7 @@ void *ices_instance_stream(void *arg)
while(1)
{
if(errors > MAX_ERRORS)
if(stream->buffer_failures > MAX_ERRORS)
{
LOG_WARN0("Too many errors, shutting down");
break;
......@@ -117,19 +126,22 @@ void *ices_instance_stream(void *arg)
if(!buffer->buf || !buffer->len)
{
LOG_WARN0("Bad buffer dequeued!");
errors++;
stream->buffer_failures++;
continue;
}
ret = process_and_send_buffer(sdsc, buffer);
/* No data produced */
if(ret == -1)
continue;
/* Fatal error */
else if(ret == -2)
{
errors = MAX_ERRORS+1;
stream->buffer_failures = MAX_ERRORS+1;
continue;
}
/* Non-fatal shout error */
else if(ret == 0)
{
LOG_ERROR1("Send error: %s",
......@@ -172,7 +184,8 @@ void *ices_instance_stream(void *arg)
{
LOG_ERROR0("Reconnect failed too many times, "
"giving up.");
errors = MAX_ERRORS+1; /* We want to die now */
/* We want to die now */
stream->buffer_failures = MAX_ERRORS+1;
}
else /* Don't try again too soon */
sleep(stream->reconnect_delay);
......@@ -180,13 +193,9 @@ void *ices_instance_stream(void *arg)
}
stream->skip = 0;
}
errors++;
stream->buffer_failures++;
}
else
errors=0;
stream_release_buffer(buffer);
}
}
else
......@@ -198,6 +207,9 @@ void *ices_instance_stream(void *arg)
shout_disconnect(&sdsc->conn);
if(stream->savefile != NULL)
fclose(stream->savefile);
free(sdsc->conn.ip);
encode_clear(sdsc->enc);
reencode_clear(sdsc->reenc);
......
......@@ -25,6 +25,19 @@
#define MODULE "stream-shared/"
#include "logging.h"
int stream_send_data(stream_description *s, unsigned char *buf,
unsigned long len)
{
if(s->stream->savefile)
{
int ret = fwrite(buf, 1, len, s->stream->savefile);
if(ret != len)
LOG_ERROR1("Failed to write %d bytes to savefile", len);
}
return shout_send_data(&s->conn, buf, len);
}
void stream_release_buffer(ref_buffer *buf)
{
thread_mutex_lock(&ices_config->refcount_lock);
......@@ -95,7 +108,7 @@ int process_and_send_buffer(stream_description *sdsc, ref_buffer *buffer)
ret = reencode_page(sdsc->reenc, buffer, &buf, &buflen);
if(ret > 0)
{
ret = shout_send_data(&sdsc->conn, buf, buflen);
ret = stream_send_data(sdsc, buf, buflen);
free(buf);
return ret;
}
......@@ -119,8 +132,8 @@ int process_and_send_buffer(stream_description *sdsc, ref_buffer *buffer)
encode_finish(sdsc->enc);
while(encode_flush(sdsc->enc, &og) != 0)
{
ret = shout_send_data(&sdsc->conn, og.header, og.header_len);
ret = shout_send_data(&sdsc->conn, og.body, og.body_len);
ret = stream_send_data(sdsc, og.header, og.header_len);
ret = stream_send_data(sdsc, og.body, og.body_len);
}
encode_clear(sdsc->enc);
......@@ -141,13 +154,13 @@ int process_and_send_buffer(stream_description *sdsc, ref_buffer *buffer)
while(encode_dataout(sdsc->enc, &og) > 0)
{
ret = shout_send_data(&sdsc->conn, og.header, og.header_len);
ret = shout_send_data(&sdsc->conn, og.body, og.body_len);
ret = stream_send_data(sdsc, og.header, og.header_len);
ret = stream_send_data(sdsc, og.body, og.body_len);
}
return ret;
}
else
return shout_send_data(&sdsc->conn, buffer->buf, buffer->len);
return stream_send_data(sdsc, buffer->buf, buffer->len);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment