From 3c5cecb828cdf9618cd4ec14a15f8cc59e313db8 Mon Sep 17 00:00:00 2001 From: "J. Alexander Treuman" Date: Tue, 12 Jun 2007 17:58:17 +0000 Subject: Redoing remiss's shout patch. This time, just block on open() instead of pretending to play while we wait for the connection to timeout. This removes the need for timers, and thus removes the now unnecessary timer_get_runtime_* function(s) from the timer code. The changes made compared to the pre-patch shout plugin are: * Block while connecting, timing out after 2 seconds. * Close the device, and not just the connection, if play returns -1. * Remove sd->last_err (it's always assigned before use). * Some minor cleanups. git-svn-id: https://svn.musicpd.org/mpd/trunk@6555 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- src/audioOutputs/audioOutput_shout.c | 125 ++++++++--------------------------- src/timer.c | 8 +-- src/timer.h | 3 - 3 files changed, 30 insertions(+), 106 deletions(-) diff --git a/src/audioOutputs/audioOutput_shout.c b/src/audioOutputs/audioOutput_shout.c index 1b235abe6..41e19a450 100644 --- a/src/audioOutputs/audioOutput_shout.c +++ b/src/audioOutputs/audioOutput_shout.c @@ -25,7 +25,6 @@ #include "../conf.h" #include "../log.h" #include "../pcm_utils.h" -#include "../timer.h" #include #include @@ -33,8 +32,8 @@ #include #include -#define CONN_ATTEMPT_INTERVAL 60 -#define SHOUT_MAX_CONNTIME_US 10000000 +#define CONN_ATTEMPT_INTERVAL 60 +#define CONN_TIMEOUT 2 static int shoutInitCount; @@ -66,9 +65,6 @@ typedef struct _ShoutData { int connAttempts; time_t lastAttempt; - int last_err; - - Timer *timer; /* just a pointer to audioOutput->outAudioFormat */ AudioFormat *audioFormat; @@ -87,8 +83,6 @@ static ShoutData *newShoutData(void) ret->connAttempts = 0; ret->lastAttempt = 0; ret->audioFormat = NULL; - ret->last_err = SHOUTERR_UNCONNECTED; - ret->timer = NULL; return ret; } @@ -100,31 +94,9 @@ static void freeShoutData(ShoutData * sd) if (sd->tag) freeMpdTag(sd->tag); - if(sd->timer) - timer_free(sd->timer); - free(sd); } -static void myShout_startTimer(AudioOutput *audioOutput) -{ - ShoutData *sd; - sd = audioOutput->data; - if(sd->timer != NULL) - ERROR("sd->timer is not null\n"); - - sd->timer = timer_new(&audioOutput->inAudioFormat); // <<--? - timer_start(sd->timer); -} - -static void myShout_endTimer(ShoutData *sd) -{ - if(sd->timer) { - timer_free(sd->timer); - sd->timer = NULL; - } -} - #define checkBlockParam(name) { \ blockParam = getBlockParam(param, name); \ if (!blockParam) { \ @@ -296,14 +268,14 @@ static int myShout_handleError(ShoutData * sd, int err) break; case SHOUTERR_UNCONNECTED: case SHOUTERR_SOCKET: - ERROR("Lost shout connection to %s:%i : %s\n", + ERROR("Lost shout connection to %s:%i: %s\n", shout_get_host(sd->shoutConn), shout_get_port(sd->shoutConn), shout_get_error(sd->shoutConn)); sd->shoutError = 1; return -1; default: - ERROR("shout: connection to %s:%i error : %s\n", + ERROR("shout: connection to %s:%i error: %s\n", shout_get_host(sd->shoutConn), shout_get_port(sd->shoutConn), shout_get_error(sd->shoutConn)); @@ -318,7 +290,6 @@ static int write_page(ShoutData * sd) { int err = 0; - /*DEBUG("shout_delay: %i\n", shout_delay(sd->shoutConn)); */ shout_sync(sd->shoutConn); err = shout_send(sd->shoutConn, sd->og.header, sd->og.header_len); if (myShout_handleError(sd, err) < 0) @@ -374,11 +345,6 @@ static void myShout_closeShoutConn(ShoutData * sd) } } - if(sd->timer) { - myShout_endTimer(sd); - } - - sd->last_err = SHOUTERR_UNCONNECTED; sd->opened = 0; } @@ -473,45 +439,42 @@ static int myShout_openShoutConn(AudioOutput * audioOutput) { ShoutData *sd = (ShoutData *) audioOutput->data; time_t t = time(NULL); - - if(sd->last_err == SHOUTERR_BUSY) - sd->last_err = shout_get_connected(sd->shoutConn); - if(sd->last_err == SHOUTERR_BUSY) - return 1; + int state; if (sd->connAttempts != 0 && - sd->last_err != SHOUTERR_CONNECTED && - sd->last_err != SHOUTERR_SUCCESS && (t - sd->lastAttempt) < CONN_ATTEMPT_INTERVAL) { return -1; } - sd->last_err = shout_get_connected(sd->shoutConn); - - if (sd->last_err == SHOUTERR_UNCONNECTED) { - sd->last_err = shout_open(sd->shoutConn); - sd->lastAttempt = t; - /* start timer */ - myShout_startTimer(audioOutput); - DEBUG("SHOUT: opening connection to shout server\n"); - } - sd->connAttempts++; + sd->lastAttempt = t; + + state = shout_open(sd->shoutConn); - switch (sd->last_err) { + while (state == SHOUTERR_BUSY && (t - sd->lastAttempt) < CONN_TIMEOUT) { + my_usleep(10000); + state = shout_get_connected(sd->shoutConn); + t = time(NULL); + } + + switch (state) { case SHOUTERR_SUCCESS: case SHOUTERR_CONNECTED: - DEBUG("SHOUT: connected!\n"); break; case SHOUTERR_BUSY: - return 1; + ERROR("timeout connecting to shout server %s:%i " + "(attempt %i)\n", + shout_get_host(sd->shoutConn), + shout_get_port(sd->shoutConn), + sd->connAttempts); + shout_close(sd->shoutConn); + return -1; default: ERROR("problem opening connection to shout server %s:%i " "(attempt %i): %s\n", shout_get_host(sd->shoutConn), shout_get_port(sd->shoutConn), sd->connAttempts, shout_get_error(sd->shoutConn)); - DEBUG("SHOUT: connection failed: %s\n", shout_get_error(sd->shoutConn)); return -1; } @@ -551,15 +514,10 @@ static int myShout_openDevice(AudioOutput * audioOutput) { ShoutData *sd = (ShoutData *) audioOutput->data; - audioOutput->open = 1; - - if (sd->opened) - return 0; - - if (myShout_openShoutConn(audioOutput) < 0) { - audioOutput->open = 0; + if (!sd->opened && myShout_openShoutConn(audioOutput) < 0) return -1; - } + + audioOutput->open = 1; return 0; } @@ -606,34 +564,9 @@ static int myShout_play(AudioOutput * audioOutput, char *playChunk, int size) if (sd->opened && sd->tagToSend) myShout_sendMetadata(sd); - if (!sd->opened) { - i = myShout_openShoutConn(audioOutput); - if (i == 0) { - myShout_endTimer(sd); - DEBUG("SHOUT: should be connected, ending timer\n"); - } - else if(i == 1) { - if(!sd->timer) { - myShout_startTimer(audioOutput); - DEBUG("SHOUT: starting timer\n"); - } - - if(timer_get_runtime_us(sd->timer) > SHOUT_MAX_CONNTIME_US) { - DEBUG("SHOUT: giving up on getting a connection after 10s\n"); - audioOutput->open = 0; - myShout_endTimer(sd); - return -1; - } - - timer_add(sd->timer, size); - timer_sync(sd->timer); - return 0; - } - else { - DEBUG("SHOUT: giving up on trying to get a connection\n"); - audioOutput->open = 0; - return -1; - } + if (!sd->opened && myShout_openShoutConn(audioOutput) < 0) { + myShout_closeDevice(audioOutput); + return -1; } samples = size / (bytes * sd->audioFormat->channels); @@ -662,7 +595,7 @@ static int myShout_play(AudioOutput * audioOutput, char *playChunk, int size) while (ogg_stream_pageout(&(sd->os), &(sd->og)) != 0) { if (write_page(sd) < 0) { - myShout_closeShoutConn(sd); + myShout_closeDevice(audioOutput); return -1; } } diff --git a/src/timer.c b/src/timer.c index f9dfb1ab6..0406a945e 100644 --- a/src/timer.c +++ b/src/timer.c @@ -51,7 +51,7 @@ void timer_free(Timer *timer) void timer_start(Timer *timer) { - timer->start_time = timer->time = now(); + timer->time = now(); timer->started = 1; } @@ -59,7 +59,6 @@ void timer_reset(Timer *timer) { timer->time = 0; timer->started = 0; - timer->start_time = 0; } void timer_add(Timer *timer, int size) @@ -79,8 +78,3 @@ void timer_sync(Timer *timer) if (sleep > 0) my_usleep(sleep); } - -uint64_t timer_get_runtime_us(Timer *timer) -{ - return now() - timer->start_time; -} diff --git a/src/timer.h b/src/timer.h index 7e84d9071..c8018e260 100644 --- a/src/timer.h +++ b/src/timer.h @@ -23,7 +23,6 @@ #include "mpd_types.h" typedef struct _Timer { - uint64_t start_time; uint64_t time; int started; int rate; @@ -41,6 +40,4 @@ void timer_add(Timer *timer, int size); void timer_sync(Timer *timer); -uint64_t timer_get_runtime_us(Timer *timer); - #endif -- cgit v1.2.3