1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/audioinputproxy.cpp

51 lines
1.1 KiB
C++
Raw Normal View History

#include "audioinputproxy.h"
#include <QDebug>
#define RING_SIZE 44100*2*2
AudioInputProxy::AudioInputProxy(QObject *parent) :
QIODevice(parent),
callback(nullptr),
2014-07-14 21:16:44 +08:00
ring_buffer(new MemRing<int16_t>(RING_SIZE))
{
2014-07-14 21:16:44 +08:00
open(QIODevice::ReadWrite | QIODevice::Unbuffered);
qDebug() << "AudioInputProxy::AudioInputProxy";
}
AudioInputProxy::~AudioInputProxy()
{
if (ring_buffer) {
delete ring_buffer;
ring_buffer = 0;
}
2014-07-14 21:16:44 +08:00
close();
qDebug() << "AudioInputProxy::~AudioInputProxy";
}
qint64 AudioInputProxy::readData(char *data, qint64 len)
{
2014-07-14 21:16:44 +08:00
qDebug() << "AudioInputProxy::read" << len;
return ring_buffer->pull((int16_t*)data, len/2)*2;
}
qint64 AudioInputProxy::writeData(const char* data, qint64 len)
{
2014-07-14 21:16:44 +08:00
qDebug() << "AudioInputProxy::write" << len;
auto ret = ring_buffer->push((int16_t*)data, len/2)*2;
if (callback != nullptr) {
callback();
}
2014-07-14 21:16:44 +08:00
return ret;
}
qint64 AudioInputProxy::bytesAvailable() const
{
return ring_buffer->readSpace() + QIODevice::bytesAvailable();
}
2014-07-14 21:16:44 +08:00
bool AudioInputProxy::isSequential() const
{
return true;
}