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

cleanup io, replace C-style memory allocation with C++ one

This commit is contained in:
Alexandr Kutuzov 2014-07-19 01:36:02 +09:00
parent 1e01ee8a9e
commit b71589175e
7 changed files with 40 additions and 28 deletions

View File

@ -2,14 +2,14 @@
#include <QDebug> #include <QDebug>
#define RING_SIZE 44100*2*2 #define RING_SIZE 11025
AudioInputProxy::AudioInputProxy(QObject *parent) : AudioInputProxy::AudioInputProxy(QObject *parent) :
QIODevice(parent), QIODevice(parent),
callback(nullptr), callback(nullptr),
ring_buffer(new MemRing<int16_t>(RING_SIZE)) ring_buffer(new MemRing<int16_t>(RING_SIZE))
{ {
open(QIODevice::ReadWrite | QIODevice::Unbuffered); open(QIODevice::WriteOnly | QIODevice::Unbuffered);
qDebug() << "AudioInputProxy::AudioInputProxy"; qDebug() << "AudioInputProxy::AudioInputProxy";
} }

View File

@ -13,11 +13,16 @@ public:
virtual ~AudioInputProxy(); virtual ~AudioInputProxy();
std::function< void() > callback; std::function< void() > callback;
qint64 bytesAvailable() const;
inline size_t pull(int16_t *data, size_t len) { return ring_buffer->pull(data, len); }
inline size_t push(int16_t *data, size_t len) { return ring_buffer->push(data, len); }
inline size_t readSpace() { return ring_buffer->readSpace(); }
inline size_t writeSpace() { return ring_buffer->writeSpace(); }
protected: protected:
qint64 readData(char *data, qint64 maxlen); qint64 readData(char *data, qint64 maxlen);
qint64 writeData(const char *data, qint64 len); qint64 writeData(const char *data, qint64 len);
qint64 bytesAvailable() const;
bool isSequential() const; bool isSequential() const;
private: private:

View File

@ -2,13 +2,13 @@
#include <QDebug> #include <QDebug>
#define RING_SIZE 44100*2*2 #define RING_SIZE 11025
AudioOutputProxy::AudioOutputProxy(QObject *parent) : AudioOutputProxy::AudioOutputProxy(QObject *parent) :
QIODevice(parent), QIODevice(parent),
ring_buffer(new MemRing<int16_t>(RING_SIZE)) ring_buffer(new MemRing<int16_t>(RING_SIZE))
{ {
open(QIODevice::ReadWrite | QIODevice::Unbuffered); open(QIODevice::ReadOnly | QIODevice::Unbuffered);
qDebug() << "AudioOutputProxy::AudioOutputProxy"; qDebug() << "AudioOutputProxy::AudioOutputProxy";
} }

View File

@ -11,6 +11,11 @@ public:
explicit AudioOutputProxy(QObject *parent = 0); explicit AudioOutputProxy(QObject *parent = 0);
virtual ~AudioOutputProxy(); virtual ~AudioOutputProxy();
inline size_t pull(int16_t *data, size_t len) { return ring_buffer->pull(data, len); }
inline size_t push(int16_t *data, size_t len) { return ring_buffer->push(data, len); }
inline size_t readSpace() { return ring_buffer->readSpace(); }
inline size_t writeSpace() { return ring_buffer->writeSpace(); }
protected: protected:
qint64 readData(char *data, qint64 maxlen); qint64 readData(char *data, qint64 maxlen);
qint64 writeData(const char *data, qint64 len); qint64 writeData(const char *data, qint64 len);

View File

@ -1230,7 +1230,7 @@ void Core::prepareCall(int friendId, int callId, ToxAv* toxav, bool videoEnabled
calls[callId].codecSettings.max_video_height = TOXAV_MAX_VIDEO_HEIGHT; calls[callId].codecSettings.max_video_height = TOXAV_MAX_VIDEO_HEIGHT;
calls[callId].videoEnabled = videoEnabled; calls[callId].videoEnabled = videoEnabled;
calls[callId].framesize = (calls[callId].codecSettings.audio_frame_duration * calls[callId].codecSettings.audio_sample_rate) / 1000;; calls[callId].framesize = (calls[callId].codecSettings.audio_frame_duration * calls[callId].codecSettings.audio_sample_rate) / 1000;;
calls[callId].audio_packet_samples = new char[calls[callId].framesize * 2]; calls[callId].audio_packet_samples = new int16_t[calls[callId].framesize];
calls[callId].audio_packet_data = new char[calls[callId].framesize * 2]; calls[callId].audio_packet_data = new char[calls[callId].framesize * 2];
toxav_prepare_transmission(toxav, callId, &calls[callId].codecSettings, videoEnabled); toxav_prepare_transmission(toxav, callId, &calls[callId].codecSettings, videoEnabled);
@ -1321,7 +1321,7 @@ void Core::playCallAudio(ToxAv*, int32_t callId, int16_t *data, int length)
if (!calls[callId].active) if (!calls[callId].active)
return; return;
calls[callId].audioOutputProxy->write((char*)data, length*2); calls[callId].audioOutputProxy->push(data, length);
// TODO: we can subscribes // TODO: we can subscribes
int state = calls[callId].audioOutput->state(); int state = calls[callId].audioOutput->state();
@ -1359,18 +1359,19 @@ void Core::sendCallAudio(int callId, ToxAv* toxav)
if (!calls[callId].active) if (!calls[callId].active)
return; return;
auto bytesReady = calls[callId].audioInputProxy->bytesAvailable(); while (true) {
if (bytesReady < calls[callId].framesize*2) { auto samplesReady = calls[callId].audioInputProxy->readSpace();
qDebug() << "not enough samples" << bytesReady << "of" << calls[callId].framesize*2; if (samplesReady < calls[callId].framesize) {
qDebug() << "not enough samples ready" << samplesReady << "of" << calls[callId].framesize;
return; return;
} }
calls[callId].audioInputProxy->read(calls[callId].audio_packet_samples, calls[callId].framesize*2); calls[callId].audioInputProxy->pull(calls[callId].audio_packet_samples, calls[callId].framesize);
int result = toxav_prepare_audio_frame(toxav, callId, int result = toxav_prepare_audio_frame(toxav, callId,
(uint8_t*)calls[callId].audio_packet_data, (uint8_t*)calls[callId].audio_packet_data,
calls[callId].framesize*2, calls[callId].framesize*2,
(int16_t*)calls[callId].audio_packet_samples, calls[callId].audio_packet_samples,
calls[callId].framesize); calls[callId].framesize);
if (result < 0) { if (result < 0) {
qWarning() << QString("Core: Unable to prepare audio frame, error %1").arg(result); qWarning() << QString("Core: Unable to prepare audio frame, error %1").arg(result);
@ -1382,6 +1383,7 @@ void Core::sendCallAudio(int callId, ToxAv* toxav)
qWarning() << QString("Core: Unable to send audio frame, error %1").arg(result); qWarning() << QString("Core: Unable to send audio frame, error %1").arg(result);
} }
} }
}
void Core::playCallVideo(ToxAv*, int32_t callId, vpx_image_t* img) void Core::playCallVideo(ToxAv*, int32_t callId, vpx_image_t* img)
{ {

2
core.h
View File

@ -106,7 +106,7 @@ public:
int callId; int callId;
int friendId; int friendId;
int framesize; int framesize;
char *audio_packet_samples; int16_t *audio_packet_samples;
char *audio_packet_data; char *audio_packet_data;
bool videoEnabled; bool videoEnabled;
bool active; bool active;

View File

@ -21,7 +21,7 @@ public:
_size = _size_mask = 1 << power_of_two; _size = _size_mask = 1 << power_of_two;
_size_mask -= 1; _size_mask -= 1;
_buff = reinterpret_cast<T*>(malloc(_size * sizeof(T))); _buff = new T[_size];
if (!_buff) if (!_buff)
throw std::exception(); throw std::exception();
@ -29,7 +29,7 @@ public:
~MemRing() { ~MemRing() {
if (_buff) if (_buff)
free(_buff); delete [] _buff;
} }
size_t readSpace() size_t readSpace()