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