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>
#define RING_SIZE 44100*2*2
#define RING_SIZE 11025
AudioInputProxy::AudioInputProxy(QObject *parent) :
QIODevice(parent),
callback(nullptr),
ring_buffer(new MemRing<int16_t>(RING_SIZE))
{
open(QIODevice::ReadWrite | QIODevice::Unbuffered);
open(QIODevice::WriteOnly | QIODevice::Unbuffered);
qDebug() << "AudioInputProxy::AudioInputProxy";
}

View File

@ -13,11 +13,16 @@ public:
virtual ~AudioInputProxy();
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:
qint64 readData(char *data, qint64 maxlen);
qint64 writeData(const char *data, qint64 len);
qint64 bytesAvailable() const;
bool isSequential() const;
private:

View File

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

View File

@ -11,6 +11,11 @@ public:
explicit AudioOutputProxy(QObject *parent = 0);
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:
qint64 readData(char *data, qint64 maxlen);
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].videoEnabled = videoEnabled;
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];
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)
return;
calls[callId].audioOutputProxy->write((char*)data, length*2);
calls[callId].audioOutputProxy->push(data, length);
// TODO: we can subscribes
int state = calls[callId].audioOutput->state();
@ -1359,27 +1359,29 @@ void Core::sendCallAudio(int callId, ToxAv* toxav)
if (!calls[callId].active)
return;
auto bytesReady = calls[callId].audioInputProxy->bytesAvailable();
if (bytesReady < calls[callId].framesize*2) {
qDebug() << "not enough samples" << bytesReady << "of" << calls[callId].framesize*2;
return;
}
while (true) {
auto samplesReady = calls[callId].audioInputProxy->readSpace();
if (samplesReady < calls[callId].framesize) {
qDebug() << "not enough samples ready" << samplesReady << "of" << calls[callId].framesize;
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,
(uint8_t*)calls[callId].audio_packet_data,
calls[callId].framesize*2,
(int16_t*)calls[callId].audio_packet_samples,
calls[callId].framesize);
if (result < 0) {
qWarning() << QString("Core: Unable to prepare audio frame, error %1").arg(result);
return;
}
int result = toxav_prepare_audio_frame(toxav, callId,
(uint8_t*)calls[callId].audio_packet_data,
calls[callId].framesize*2,
calls[callId].audio_packet_samples,
calls[callId].framesize);
if (result < 0) {
qWarning() << QString("Core: Unable to prepare audio frame, error %1").arg(result);
return;
}
result = toxav_send_audio(toxav, callId, (uint8_t*)calls[callId].audio_packet_data, result);
if (result < 0) {
qWarning() << QString("Core: Unable to send audio frame, error %1").arg(result);
result = toxav_send_audio(toxav, callId, (uint8_t*)calls[callId].audio_packet_data, result);
if (result < 0) {
qWarning() << QString("Core: Unable to send audio frame, error %1").arg(result);
}
}
}

2
core.h
View File

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

View File

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