decoder/audiofile: use decoder_read()

.. instead of InputStream::LockRead(). The former is cancellable.
This commit is contained in:
Max Kellermann 2014-07-09 18:46:58 +02:00
parent d4bd947bf5
commit bc6472bb9e
2 changed files with 26 additions and 15 deletions

2
NEWS
View File

@ -2,6 +2,8 @@ ver 0.18.12 (not yet released)
* database * database
- proxy: fix build failure with libmpdclient 2.2 - proxy: fix build failure with libmpdclient 2.2
- proxy: fix add/search and other commands with libmpdclient < 2.9 - proxy: fix add/search and other commands with libmpdclient < 2.9
* decoder
- audiofile: improve responsiveness
ver 0.18.11 (2014/05/12) ver 0.18.11 (2014/05/12)
* decoder * decoder

View File

@ -38,6 +38,15 @@
static constexpr Domain audiofile_domain("audiofile"); static constexpr Domain audiofile_domain("audiofile");
struct AudioFileInputStream {
Decoder *const decoder;
InputStream &is;
size_t Read(void *buffer, size_t size) {
return decoder_read(decoder, is, buffer, size);
}
};
static int audiofile_get_duration(const char *file) static int audiofile_get_duration(const char *file)
{ {
int total_time; int total_time;
@ -55,29 +64,26 @@ static int audiofile_get_duration(const char *file)
static ssize_t static ssize_t
audiofile_file_read(AFvirtualfile *vfile, void *data, size_t length) audiofile_file_read(AFvirtualfile *vfile, void *data, size_t length)
{ {
InputStream &is = *(InputStream *)vfile->closure; AudioFileInputStream &afis = *(AudioFileInputStream *)vfile->closure;
Error error; return afis.Read(data, length);
size_t nbytes = is.LockRead(data, length, error);
if (nbytes == 0 && error.IsDefined()) {
LogError(error);
return -1;
}
return nbytes;
} }
static AFfileoffset static AFfileoffset
audiofile_file_length(AFvirtualfile *vfile) audiofile_file_length(AFvirtualfile *vfile)
{ {
InputStream &is = *(InputStream *)vfile->closure; AudioFileInputStream &afis = *(AudioFileInputStream *)vfile->closure;
InputStream &is = afis.is;
return is.GetSize(); return is.GetSize();
} }
static AFfileoffset static AFfileoffset
audiofile_file_tell(AFvirtualfile *vfile) audiofile_file_tell(AFvirtualfile *vfile)
{ {
InputStream &is = *(InputStream *)vfile->closure; AudioFileInputStream &afis = *(AudioFileInputStream *)vfile->closure;
InputStream &is = afis.is;
return is.GetOffset(); return is.GetOffset();
} }
@ -92,7 +98,9 @@ audiofile_file_destroy(AFvirtualfile *vfile)
static AFfileoffset static AFfileoffset
audiofile_file_seek(AFvirtualfile *vfile, AFfileoffset offset, int is_relative) audiofile_file_seek(AFvirtualfile *vfile, AFfileoffset offset, int is_relative)
{ {
InputStream &is = *(InputStream *)vfile->closure; AudioFileInputStream &afis = *(AudioFileInputStream *)vfile->closure;
InputStream &is = afis.is;
int whence = (is_relative ? SEEK_CUR : SEEK_SET); int whence = (is_relative ? SEEK_CUR : SEEK_SET);
Error error; Error error;
@ -104,10 +112,10 @@ audiofile_file_seek(AFvirtualfile *vfile, AFfileoffset offset, int is_relative)
} }
static AFvirtualfile * static AFvirtualfile *
setup_virtual_fops(InputStream &stream) setup_virtual_fops(AudioFileInputStream &afis)
{ {
AFvirtualfile *vf = new AFvirtualfile(); AFvirtualfile *vf = new AFvirtualfile();
vf->closure = &stream; vf->closure = &afis;
vf->write = nullptr; vf->write = nullptr;
vf->read = audiofile_file_read; vf->read = audiofile_file_read;
vf->length = audiofile_file_length; vf->length = audiofile_file_length;
@ -174,7 +182,8 @@ audiofile_stream_decode(Decoder &decoder, InputStream &is)
return; return;
} }
vf = setup_virtual_fops(is); AudioFileInputStream afis{&decoder, is};
vf = setup_virtual_fops(afis);
af_fp = afOpenVirtualFile(vf, "r", nullptr); af_fp = afOpenVirtualFile(vf, "r", nullptr);
if (af_fp == AF_NULL_FILEHANDLE) { if (af_fp == AF_NULL_FILEHANDLE) {