kwave  18.07.70
Kwave::PlayBackQt::Buffer Class Reference
Inheritance diagram for Kwave::PlayBackQt::Buffer:
Inheritance graph
Collaboration diagram for Kwave::PlayBackQt::Buffer:
Collaboration graph

Public Member Functions

 Buffer ()
 
virtual ~Buffer () Q_DECL_OVERRIDE
 
void start (unsigned int buf_size, int timeout)
 
void setTimeout (int timeout)
 
void drain (QByteArray &padding)
 
void stop ()
 
virtual qint64 readData (char *data, qint64 len) Q_DECL_OVERRIDE
 
virtual qint64 writeData (const char *data, qint64 len) Q_DECL_OVERRIDE
 
virtual qint64 bytesAvailable () const Q_DECL_OVERRIDE
 

Private Attributes

QMutex m_lock
 
QSemaphore m_sem_free
 
QSemaphore m_sem_filled
 
QQueue< char > m_raw_buffer
 
int m_timeout
 
QByteArray m_pad_data
 
int m_pad_ofs
 

Detailed Description

Definition at line 140 of file PlayBack-Qt.h.

Constructor & Destructor Documentation

◆ Buffer()

Kwave::PlayBackQt::Buffer::Buffer ( )

constructor

Definition at line 464 of file PlayBack-Qt.cpp.

465  :QIODevice(),
466  m_lock(QMutex::Recursive),
467  m_sem_free(0),
468  m_sem_filled(0),
469  m_raw_buffer(),
470  m_timeout(1000),
471  m_pad_data(),
472  m_pad_ofs(0)
473 {
474 }
QQueue< char > m_raw_buffer
Definition: PlayBack-Qt.h:206

◆ ~Buffer()

Kwave::PlayBackQt::Buffer::~Buffer ( )
virtual

destructor

Definition at line 477 of file PlayBack-Qt.cpp.

478 {
479 }

Member Function Documentation

◆ bytesAvailable()

qint64 Kwave::PlayBackQt::Buffer::bytesAvailable ( ) const
virtual

returns the number of bytes available for reading

Definition at line 584 of file PlayBack-Qt.cpp.

References m_pad_data, m_pad_ofs, and m_sem_filled.

Referenced by Kwave::PlayBackQt::close().

585 {
586  return QIODevice::bytesAvailable() +
587  m_sem_filled.available() +
588  m_pad_data.size() -
589  m_pad_ofs;
590 }
Here is the caller graph for this function:

◆ drain()

void Kwave::PlayBackQt::Buffer::drain ( QByteArray &  padding)

drain the sink, at the end of playback: provide padding to provide data for a full period

Parameters
paddingarray of bytes used for padding

Definition at line 504 of file PlayBack-Qt.cpp.

References m_pad_data, and m_pad_ofs.

Referenced by Kwave::PlayBackQt::close().

505 {
506  m_pad_data = padding;
507  m_pad_ofs = 0;
508 }
Here is the caller graph for this function:

◆ readData()

qint64 Kwave::PlayBackQt::Buffer::readData ( char *  data,
qint64  len 
)
virtual

read data out from the buffer, called from the Qt audio device side

Parameters
datapointer to a buffer (of bytes) to receive the data
lennumber of bytes to read
Returns
number of bytes that have been read

Definition at line 517 of file PlayBack-Qt.cpp.

References m_lock, m_pad_data, m_pad_ofs, m_raw_buffer, m_sem_filled, m_sem_free, m_timeout, and Kwave::toInt().

518 {
519  qint64 read_bytes = -1;
520  qint64 requested = len;
521 
522 // qDebug("Kwave::PlayBackQt::Buffer::readData(..., len=%lld", requested);
523  if (len == 0) return 0;
524  if (len < 0) return -1;
525 
526  while (len > 0) {
527  int count = qMin<int>(
528  qMax(m_sem_filled.available(), 1),
529  Kwave::toInt(len)
530  );
531  if (Q_LIKELY(m_sem_filled.tryAcquire(count, m_timeout))) {
532 // qDebug(" read: locking...");
533  QMutexLocker _lock(&m_lock); // context: qt streaming engine
534 // qDebug(" read: locked, count=%lld", len);
535  m_sem_free.release(count);
536  if (read_bytes < 0) read_bytes = 0;
537  read_bytes += count;
538  len -= count;
539  while (count--)
540  *(data++) = m_raw_buffer.dequeue();
541  } else break;
542  }
543 
544  // if we are at the end of the stream: do some padding to satisfy Qt
545  while ( (read_bytes < requested) &&
546  !m_pad_data.isEmpty() && (m_pad_ofs < m_pad_data.size()) )
547  {
548  *(data++) = 0;
549  read_bytes++;
550  m_pad_ofs++;
551  }
552 
553  if (read_bytes != requested)
554  qDebug("Kwave::PlayBackQt::Buffer::readData(...) -> read=%lld/%lld",
555  read_bytes, requested);
556 
557  return read_bytes;
558 }
int toInt(T x)
Definition: Utils.h:127
QQueue< char > m_raw_buffer
Definition: PlayBack-Qt.h:206
Here is the call graph for this function:

◆ setTimeout()

void Kwave::PlayBackQt::Buffer::setTimeout ( int  timeout)

set a new read/write timeout

Note
does not influence currently waiting reads/writes
Parameters
timeouta new read/write timeout [ms]

Definition at line 496 of file PlayBack-Qt.cpp.

References m_lock, and m_timeout.

Referenced by Kwave::PlayBackQt::open().

497 {
498  QMutexLocker _lock(&m_lock); // context: main thread
499  m_timeout = timeout;
500  qDebug("Kwave::PlayBackQt::Buffer::setTimeout(%d)", timeout);
501 }
Here is the caller graph for this function:

◆ start()

void Kwave::PlayBackQt::Buffer::start ( unsigned int  buf_size,
int  timeout 
)

start filling the buffer

Parameters
buf_sizesize of the buffer in bytes
timeoutread/write timeout [ms]

Definition at line 482 of file PlayBack-Qt.cpp.

References m_pad_data, m_pad_ofs, m_raw_buffer, m_sem_filled, m_sem_free, m_timeout, and Kwave::PlayBackQt::open().

Referenced by Kwave::PlayBackQt::open().

483 {
484  m_raw_buffer.clear();
485  m_sem_filled.acquire(m_sem_filled.available());
486  m_sem_free.acquire(m_sem_free.available());
487  m_sem_free.release(buf_size);
488  m_timeout = timeout;
489  m_pad_data.clear();
490  m_pad_ofs = 0;
491 
492  open(QIODevice::ReadOnly);
493 }
virtual QString open(const QString &device, double rate, unsigned int channels, unsigned int bits, unsigned int bufbase) Q_DECL_OVERRIDE
QQueue< char > m_raw_buffer
Definition: PlayBack-Qt.h:206
Here is the call graph for this function:
Here is the caller graph for this function:

◆ stop()

void Kwave::PlayBackQt::Buffer::stop ( )

stop filling the buffer

Definition at line 511 of file PlayBack-Qt.cpp.

References Kwave::PlayBackQt::close().

Referenced by Kwave::PlayBackQt::close().

512 {
513  close();
514 }
virtual int close() Q_DECL_OVERRIDE
Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeData()

qint64 Kwave::PlayBackQt::Buffer::writeData ( const char *  data,
qint64  len 
)
virtual

write data into the buffer, called from our own worker thread

Parameters
datapointer to a buffer (of bytes) to write
lennumber of bytes to write
Returns
number of bytes written

Definition at line 561 of file PlayBack-Qt.cpp.

References m_lock, m_raw_buffer, m_sem_filled, m_sem_free, m_timeout, and Kwave::toInt().

Referenced by Kwave::PlayBackQt::write().

562 {
563 
564  qint64 written_bytes = 0;
565  while (len) {
566  int count = qMin<int>(
567  qMax(m_sem_free.available(), 1),
568  Kwave::toInt(len)
569  );
570  if (Q_LIKELY(m_sem_free.tryAcquire(count, m_timeout * 10))) {
571  QMutexLocker _lock(&m_lock); // context: kwave worker thread
572  m_sem_filled.release(count);
573  written_bytes += count;
574  len -= count;
575  while (count--)
576  m_raw_buffer.enqueue(*(data++));
577  } else break;
578  }
579 
580  return written_bytes;
581 }
int toInt(T x)
Definition: Utils.h:127
QQueue< char > m_raw_buffer
Definition: PlayBack-Qt.h:206
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_lock

QMutex Kwave::PlayBackQt::Buffer::m_lock
private

mutex for locking the queue

Definition at line 197 of file PlayBack-Qt.h.

Referenced by readData(), setTimeout(), and writeData().

◆ m_pad_data

QByteArray Kwave::PlayBackQt::Buffer::m_pad_data
private

buffer with padding data

Definition at line 212 of file PlayBack-Qt.h.

Referenced by bytesAvailable(), drain(), readData(), and start().

◆ m_pad_ofs

int Kwave::PlayBackQt::Buffer::m_pad_ofs
private

read pointer within m_pad_data

Definition at line 215 of file PlayBack-Qt.h.

Referenced by bytesAvailable(), drain(), readData(), and start().

◆ m_raw_buffer

QQueue<char> Kwave::PlayBackQt::Buffer::m_raw_buffer
private

raw buffer with audio data

Definition at line 206 of file PlayBack-Qt.h.

Referenced by readData(), start(), and writeData().

◆ m_sem_filled

QSemaphore Kwave::PlayBackQt::Buffer::m_sem_filled
private

semaphore with filled buffer space

Definition at line 203 of file PlayBack-Qt.h.

Referenced by bytesAvailable(), readData(), start(), and writeData().

◆ m_sem_free

QSemaphore Kwave::PlayBackQt::Buffer::m_sem_free
private

semaphore with free buffer space

Definition at line 200 of file PlayBack-Qt.h.

Referenced by readData(), start(), and writeData().

◆ m_timeout

int Kwave::PlayBackQt::Buffer::m_timeout
private

read timeout [ms]

Definition at line 209 of file PlayBack-Qt.h.

Referenced by readData(), setTimeout(), start(), and writeData().


The documentation for this class was generated from the following files: