Commit 2e822a57 authored by Max Kellermann's avatar Max Kellermann

wavpack: moved code to wavpack_open_wvc()

Move everything related to finding and initializing the WVC stream to wavpack_open_wvc(). This greatly simplifies its error handling and the function wavpack_streamdecode().
parent af58de65
...@@ -434,98 +434,87 @@ static unsigned int wavpack_trydecode(InputStream *is) ...@@ -434,98 +434,87 @@ static unsigned int wavpack_trydecode(InputStream *is)
return 1; return 1;
} }
/* static int wavpack_open_wvc(struct decoder *decoder,
* Decodes a stream. InputStream *is_wvc)
*/
static int wavpack_streamdecode(struct decoder * decoder, InputStream *is)
{ {
char error[ERRORLEN]; char tmp[MPD_PATH_MAX];
WavpackContext *wpc; const char *utf8url;
InputStream is_wvc; size_t len;
int open_flags = OPEN_2CH_MAX | OPEN_NORMALIZE /*| OPEN_STREAMING*/;
char *wvc_url = NULL; char *wvc_url = NULL;
int err; int ret;
InputStreamPlus isp, isp_wvc;
int canseek; /*
* As we use dc->utf8url, this function will be bad for
* single files. utf8url is not absolute file path :/
*/
utf8url = decoder_get_url(decoder, tmp);
if (utf8url == NULL)
return 0;
/* Try to find wvc */ len = strlen(utf8url);
do { if (!len)
char tmp[MPD_PATH_MAX]; return 0;
const char *utf8url;
size_t len;
err = 1;
/*
* As we use dc.utf8url, this function will be bad for
* single files. utf8url is not absolute file path :/
*/
utf8url = decoder_get_url(decoder, tmp);
if (utf8url == NULL) {
break;
}
len = strlen(utf8url); wvc_url = (char *)xmalloc(len + 2); /* +2: 'c' and EOS */
if (!len) { if (wvc_url == NULL)
break; return 0;
}
wvc_url = (char *)xmalloc(len + 2); /* +2: 'c' and EOS */ memcpy(wvc_url, utf8url, len);
if (wvc_url == NULL) { wvc_url[len] = 'c';
break; wvc_url[len + 1] = '\0';
}
memcpy(wvc_url, utf8url, len); ret = openInputStream(is_wvc, wvc_url);
wvc_url[len] = 'c'; free(wvc_url);
wvc_url[len + 1] = '\0';
if (openInputStream(&is_wvc, wvc_url)) { if (ret)
break; return 0;
/*
* And we try to buffer in order to get know
* about a possible 404 error.
*/
for (;;) {
if (inputStreamAtEOF(is_wvc)) {
/*
* EOF is reached even without
* a single byte is read...
* So, this is not good :/
*/
closeInputStream(is_wvc);
return 0;
} }
/* if (bufferInputStream(is_wvc) >= 0)
* And we try to buffer in order to get know return 1;
* about a possible 404 error.
*/
for (;;) {
if (inputStreamAtEOF(&is_wvc)) {
/*
* EOF is reached even without
* a single byte is read...
* So, this is not good :/
*/
break;
}
if (bufferInputStream(&is_wvc) >= 0) { if (decoder_get_command(decoder) == DECODE_COMMAND_STOP) {
err = 0; closeInputStream(is_wvc);
break; return 0;
} }
if (decoder_get_command(decoder) == DECODE_COMMAND_STOP) { /* Save some CPU */
break; my_usleep(1000);
} }
}
/* Save some CPU */ /*
my_usleep(1000); * Decodes a stream.
} */
if (err) { static int wavpack_streamdecode(struct decoder * decoder, InputStream *is)
closeInputStream(&is_wvc); {
break; char error[ERRORLEN];
} WavpackContext *wpc;
InputStream is_wvc;
int open_flags = OPEN_2CH_MAX | OPEN_NORMALIZE /*| OPEN_STREAMING*/;
InputStreamPlus isp, isp_wvc;
int canseek;
if (wavpack_open_wvc(decoder, &is_wvc)) {
initInputStreamPlus(&isp_wvc, decoder, &is_wvc);
open_flags |= OPEN_WVC; open_flags |= OPEN_WVC;
}
} while (0);
canseek = can_seek(&isp); canseek = can_seek(&isp);
if (wvc_url != NULL) {
if (err) {
free(wvc_url);
wvc_url = NULL;
} else {
initInputStreamPlus(&isp_wvc, decoder, &is_wvc);
}
}
initInputStreamPlus(&isp, decoder, is); initInputStreamPlus(&isp, decoder, is);
wpc = WavpackOpenFileInputEx(&mpd_is_reader, &isp, &isp_wvc, error, wpc = WavpackOpenFileInputEx(&mpd_is_reader, &isp, &isp_wvc, error,
...@@ -539,10 +528,8 @@ static int wavpack_streamdecode(struct decoder * decoder, InputStream *is) ...@@ -539,10 +528,8 @@ static int wavpack_streamdecode(struct decoder * decoder, InputStream *is)
wavpack_decode(decoder, wpc, canseek, NULL); wavpack_decode(decoder, wpc, canseek, NULL);
WavpackCloseFile(wpc); WavpackCloseFile(wpc);
if (wvc_url != NULL) { if (open_flags & OPEN_WVC)
closeInputStream(&is_wvc); closeInputStream(&is_wvc);
free(wvc_url);
}
closeInputStream(is); closeInputStream(is);
return 0; return 0;
......
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