Commit c32dceb4 authored by Max Kellermann's avatar Max Kellermann

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.
parent 5573e783
......@@ -3,6 +3,7 @@ ver 0.23.8 (not yet released)
- curl: fix crash if web server does not understand WebDAV
* input
- cdio_paranoia: fix crash if no drive was found
- cdio_paranoia: faster cancellation
* output
- pipewire: fix crash with PipeWire 0.3.53
* mixer
......
......@@ -288,61 +288,51 @@ size_t
CdioParanoiaInputStream::Read(std::unique_lock<Mutex> &,
void *ptr, size_t length)
{
size_t nbytes = 0;
char *wptr = (char *) ptr;
while (length > 0) {
/* end of track ? */
if (IsEOF())
break;
//current sector was changed ?
const int16_t *rbuf;
const int32_t lsn_relofs = offset / CDIO_CD_FRAMESIZE_RAW;
if (lsn_relofs != buffer_lsn) {
const ScopeUnlock unlock(mutex);
try {
rbuf = para.Read().data;
} catch (...) {
char *s_err = cdio_cddap_errors(drv);
if (s_err) {
FmtError(cdio_domain,
"paranoia_read: {}", s_err);
cdio_cddap_free_messages(s_err);
}
throw;
/* end of track ? */
if (IsEOF())
return 0;
//current sector was changed ?
const int16_t *rbuf;
const int32_t lsn_relofs = offset / CDIO_CD_FRAMESIZE_RAW;
if (lsn_relofs != buffer_lsn) {
const ScopeUnlock unlock(mutex);
try {
rbuf = para.Read().data;
} catch (...) {
char *s_err = cdio_cddap_errors(drv);
if (s_err) {
FmtError(cdio_domain,
"paranoia_read: {}", s_err);
cdio_cddap_free_messages(s_err);
}
//store current buffer
memcpy(buffer, rbuf, CDIO_CD_FRAMESIZE_RAW);
buffer_lsn = lsn_relofs;
} else {
//use cached sector
rbuf = (const int16_t *)buffer;
throw;
}
//correct offset
const int diff = offset - lsn_relofs * CDIO_CD_FRAMESIZE_RAW;
//store current buffer
memcpy(buffer, rbuf, CDIO_CD_FRAMESIZE_RAW);
buffer_lsn = lsn_relofs;
} else {
//use cached sector
rbuf = (const int16_t *)buffer;
}
assert(diff >= 0 && diff < CDIO_CD_FRAMESIZE_RAW);
//correct offset
const int diff = offset - lsn_relofs * CDIO_CD_FRAMESIZE_RAW;
const size_t maxwrite = CDIO_CD_FRAMESIZE_RAW - diff; //# of bytes pending in current buffer
const size_t len = std::min(length, maxwrite);
assert(diff >= 0 && diff < CDIO_CD_FRAMESIZE_RAW);
//skip diff bytes from this lsn
memcpy(wptr, ((const char *)rbuf) + diff, len);
//update pointer
wptr += len;
nbytes += len;
const size_t maxwrite = CDIO_CD_FRAMESIZE_RAW - diff; //# of bytes pending in current buffer
const std::size_t nbytes = std::min(length, maxwrite);
//update offset
offset += len;
//update length
length -= len;
}
//skip diff bytes from this lsn
memcpy(ptr, ((const char *)rbuf) + diff, nbytes);
//update offset
offset += nbytes;
return nbytes;
}
......
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