input/CdioParanoia: remove loop from Read()

The Read() method is not required to fill the whole buffer.  By
returning as soon as at least one byte was read, we allow faster
cancellation.
This commit is contained in:
Max Kellermann 2022-07-08 12:26:52 +02:00
parent 5573e78364
commit c32dceb4d4
2 changed files with 38 additions and 47 deletions

1
NEWS
View File

@ -3,6 +3,7 @@ ver 0.23.8 (not yet released)
- curl: fix crash if web server does not understand WebDAV - curl: fix crash if web server does not understand WebDAV
* input * input
- cdio_paranoia: fix crash if no drive was found - cdio_paranoia: fix crash if no drive was found
- cdio_paranoia: faster cancellation
* output * output
- pipewire: fix crash with PipeWire 0.3.53 - pipewire: fix crash with PipeWire 0.3.53
* mixer * mixer

View File

@ -288,62 +288,52 @@ size_t
CdioParanoiaInputStream::Read(std::unique_lock<Mutex> &, CdioParanoiaInputStream::Read(std::unique_lock<Mutex> &,
void *ptr, size_t length) void *ptr, size_t length)
{ {
size_t nbytes = 0; /* end of track ? */
char *wptr = (char *) ptr; if (IsEOF())
return 0;
while (length > 0) { //current sector was changed ?
/* end of track ? */ const int16_t *rbuf;
if (IsEOF())
break;
//current sector was changed ? const int32_t lsn_relofs = offset / CDIO_CD_FRAMESIZE_RAW;
const int16_t *rbuf; if (lsn_relofs != buffer_lsn) {
const ScopeUnlock unlock(mutex);
const int32_t lsn_relofs = offset / CDIO_CD_FRAMESIZE_RAW; try {
if (lsn_relofs != buffer_lsn) { rbuf = para.Read().data;
const ScopeUnlock unlock(mutex); } catch (...) {
char *s_err = cdio_cddap_errors(drv);
try { if (s_err) {
rbuf = para.Read().data; FmtError(cdio_domain,
} catch (...) { "paranoia_read: {}", s_err);
char *s_err = cdio_cddap_errors(drv); cdio_cddap_free_messages(s_err);
if (s_err) {
FmtError(cdio_domain,
"paranoia_read: {}", s_err);
cdio_cddap_free_messages(s_err);
}
throw;
} }
//store current buffer throw;
memcpy(buffer, rbuf, CDIO_CD_FRAMESIZE_RAW);
buffer_lsn = lsn_relofs;
} else {
//use cached sector
rbuf = (const int16_t *)buffer;
} }
//correct offset //store current buffer
const int diff = offset - lsn_relofs * CDIO_CD_FRAMESIZE_RAW; memcpy(buffer, rbuf, CDIO_CD_FRAMESIZE_RAW);
buffer_lsn = lsn_relofs;
assert(diff >= 0 && diff < CDIO_CD_FRAMESIZE_RAW); } else {
//use cached sector
const size_t maxwrite = CDIO_CD_FRAMESIZE_RAW - diff; //# of bytes pending in current buffer rbuf = (const int16_t *)buffer;
const size_t len = std::min(length, maxwrite);
//skip diff bytes from this lsn
memcpy(wptr, ((const char *)rbuf) + diff, len);
//update pointer
wptr += len;
nbytes += len;
//update offset
offset += len;
//update length
length -= len;
} }
//correct offset
const int diff = offset - lsn_relofs * CDIO_CD_FRAMESIZE_RAW;
assert(diff >= 0 && diff < CDIO_CD_FRAMESIZE_RAW);
const size_t maxwrite = CDIO_CD_FRAMESIZE_RAW - diff; //# of bytes pending in current buffer
const std::size_t nbytes = std::min(length, maxwrite);
//skip diff bytes from this lsn
memcpy(ptr, ((const char *)rbuf) + diff, nbytes);
//update offset
offset += nbytes;
return nbytes; return nbytes;
} }