QIODevice::bytesAvailable in non sequential mode has a bug as follows
Unsolved
General and Desktop
-
Re: How to minimise audio output latency?
I found some references to a bug in this quite old post online. I was wondering if others came across this bug where the bytesAvailable is double. I tracked the bug down to virtual size() call in the QIODevice::bytesAvaiable superclass when not in sequential mode.
/** * Return the number of bytes available. * * <p>Note that there may be an issue with this when the * IOIDevice is not sequential due to the requirement * that the we must call QIODevice::bytesAvailable which * in turn calls size(). * * @return number of bytes available. */ qint64 IODeviceSink::bytesAvailable() const { const qint64 my_size = mAudioData.size(); const qint64 builtin_size = QIODevice::bytesAvailable(); return (my_size + builtin_size); } /** * Gets the Audio buffer Size. * * @return size of the audio buffer */ qint64 IODeviceSink::size() const { return mAudioData.size(); }
The QIODevice::bytesAvailable documentation indicates that
Subclasses that reimplement this function must call the base implementation in order to include the size of the buffer of QIODevice. Example:
The problem with calling the base class' bytesAvailable is that it in turn calls a virtual size() function if the device is opened in default (non sequential mode). Per:
/*! Returns the number of bytes that are available for reading. This function is commonly used with sequential devices to determine the number of bytes to allocate in a buffer before reading. Subclasses that reimplement this function must call the base implementation in order to include the size of the buffer of QIODevice. Example: \snippet code/src_corelib_io_qiodevice.cpp 1 \sa bytesToWrite(), readyRead(), isSequential() */ qint64 QIODevice::bytesAvailable() const { Q_D(const QIODevice); if (!d->isSequential()) return qMax(size() - d->pos, qint64(0)); <<<< size() is virtual and calls the subclass return d->buffer.size() - d->transactionPos; }