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
This commit is contained in:
J. Alexander Treuman 2007-06-12 17:58:17 +00:00
parent bd0620ff72
commit 3c5cecb828
3 changed files with 30 additions and 106 deletions

View File

@ -25,7 +25,6 @@
#include "../conf.h"
#include "../log.h"
#include "../pcm_utils.h"
#include "../timer.h"
#include <string.h>
#include <time.h>
@ -34,7 +33,7 @@
#include <vorbis/vorbisenc.h>
#define CONN_ATTEMPT_INTERVAL 60
#define SHOUT_MAX_CONNTIME_US 10000000
#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) { \
@ -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->connAttempts++;
sd->lastAttempt = t;
/* start timer */
myShout_startTimer(audioOutput);
DEBUG("SHOUT: opening connection to shout server\n");
state = shout_open(sd->shoutConn);
while (state == SHOUTERR_BUSY && (t - sd->lastAttempt) < CONN_TIMEOUT) {
my_usleep(10000);
state = shout_get_connected(sd->shoutConn);
t = time(NULL);
}
sd->connAttempts++;
switch (sd->last_err) {
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,36 +564,11 @@ 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);
if (!sd->opened && myShout_openShoutConn(audioOutput) < 0) {
myShout_closeDevice(audioOutput);
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;
}
}
samples = size / (bytes * sd->audioFormat->channels);
/* this is for only 16-bit audio */
@ -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;
}
}

View File

@ -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;
}

View File

@ -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