diff --git a/audiohandler.cpp b/audiohandler.cpp index 961b541..eee4a81 100644 --- a/audiohandler.cpp +++ b/audiohandler.cpp @@ -80,7 +80,6 @@ audioHandler::audioHandler(QObject* parent) : isInput(0), volume(1.0f) { - } audioHandler::~audioHandler() @@ -308,14 +307,13 @@ qint64 audioHandler::writeData(const char* data, qint64 len) { QMutexLocker locker(&mutex); - - if (buffer.length() > bufferSize) + if (buffer.length() > bufferSize * 4) { qWarning() << "writeData() Buffer overflow"; buffer.clear(); } - int chunkSize = 960; // Assume 8bit or uLaw. + //int chunkSize = 960; // Assume 8bit or uLaw. if (isUlaw) { for (int f = 0; f < len / 2; f++) { @@ -330,20 +328,12 @@ qint64 audioHandler::writeData(const char* data, qint64 len) } else if (radioSampleBits == 16) { buffer.append(QByteArray::fromRawData(data, len)); - chunkSize = 1920; + //chunkSize = 1920; } else { qWarning() << "Unsupported number of bits! :" << radioSampleBits; } - - - if (buffer.length() >= chunkSize) - { - qDebug() << "Sending haveAudioData() with " << chunkSize << " bytes " << " rxlen: " << len; - emit haveAudioData(buffer.mid(0, chunkSize)); - buffer.remove(0, chunkSize); - } - + return (len); // Always return the same number as we received } @@ -361,6 +351,9 @@ void audioHandler::notified() { } + + + void audioHandler::stateChanged(QAudio::State state) { if (state == QAudio::IdleState && audioOutput->error() == QAudio::UnderrunError) { @@ -412,16 +405,17 @@ void audioHandler::getBufferSize() emit sendBufferSize(audioOutput->bufferSize()); } -void audioHandler::getNextAudioChunk() +QByteArray audioHandler::getNextAudioChunk() { QMutexLocker locker(&mutex); quint16 numSamples = radioSampleBits * 120; + QByteArray ret; if (buffer.size() >= numSamples) { - emit haveAudioData(buffer.mid(0, numSamples)); + ret.append(buffer.mid(0, numSamples)); buffer.remove(0, numSamples); } - return; + return ret; } diff --git a/audiohandler.h b/audiohandler.h index 41c54f5..19e9c2e 100644 --- a/audiohandler.h +++ b/audiohandler.h @@ -40,6 +40,7 @@ public: qint64 writeData(const char* data, qint64 len); qint64 bytesAvailable() const; bool isSequential() const; + QByteArray getNextAudioChunk(void); public slots: bool init(const quint8 bits, const quint8 channels, const quint16 samplerate, const quint16 bufferSize, const bool isulaw, const bool isinput); @@ -49,7 +50,6 @@ public slots: private slots: void notified(); void stateChanged(QAudio::State state); - void getNextAudioChunk(void); signals: void audioMessage(QString message); diff --git a/udphandler.cpp b/udphandler.cpp index 720c255..6d46e4f 100644 --- a/udphandler.cpp +++ b/udphandler.cpp @@ -584,7 +584,7 @@ udpAudio::udpAudio(QHostAddress local, QHostAddress ip, quint16 aport, quint16 b rxaudio->moveToThread(rxAudioThread); connect(this, SIGNAL(setupRxAudio(quint8, quint8, quint16, quint16, bool, bool)), rxaudio, SLOT(init(quint8, quint8, quint16, quint16, bool, bool))); - connect(this, SIGNAL(haveAudioData(QByteArray)), rxaudio, SLOT(incomingAudio(QByteArray))); + //connect(this, SIGNAL(haveAudioData(QByteArray)), rxaudio, SLOT(incomingAudio(QByteArray))); connect(this, SIGNAL(haveChangeBufferSize(quint16)), rxaudio, SLOT(changeBufferSize(quint16))); connect(rxAudioThread, SIGNAL(finished()), rxaudio, SLOT(deleteLater())); @@ -601,7 +601,7 @@ udpAudio::udpAudio(QHostAddress local, QHostAddress ip, quint16 aport, quint16 b txaudio->moveToThread(txAudioThread); connect(this, SIGNAL(setupTxAudio(quint8, quint8, quint16, quint16, bool, bool)), txaudio, SLOT(init(quint8, quint8, quint16, quint16, bool, bool))); - connect(txaudio, SIGNAL(haveAudioData(QByteArray)), this, SLOT(sendTxAudio(QByteArray))); + //connect(txaudio, SIGNAL(haveAudioData(QByteArray)), this, SLOT(sendTxAudio(QByteArray))); connect(txAudioThread, SIGNAL(finished()), txaudio, SLOT(deleteLater())); rxAudioThread->start(); @@ -624,40 +624,44 @@ udpAudio::~udpAudio() txAudioThread->quit(); txAudioThread->wait(); } + if (txAudioTimer != Q_NULLPTR) + { + txAudioTimer->stop(); + delete txAudioTimer; + } } -void udpAudio::sendTxAudio(QByteArray audio) +void udpAudio::sendTxAudio() { quint8 p[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, static_cast(localSID >> 24 & 0xff), static_cast(localSID >> 16 & 0xff), static_cast(localSID >> 8 & 0xff), static_cast(localSID & 0xff), static_cast(remoteSID >> 24 & 0xff), static_cast(remoteSID >> 16 & 0xff), static_cast(remoteSID >> 8 & 0xff), static_cast(remoteSID & 0xff), 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; - int counter = 0; //if (((txCodec == 0x01 || txCodec == 0x02) && audio.length() != 960) || (txCodec == 0x04 && audio.length() != 1920)) { // qDebug() << "Unsupported TX audio length :" << audio.length() << " With codec: " << txCodec; //} - while (counter < audio.length()) - { - QByteArray tx = QByteArray::fromRawData((const char*)p, sizeof(p)); - QByteArray partial = audio.mid(counter, 1364); - tx.append(partial); - tx[0] = static_cast(tx.length() & 0xff); - tx[1] = static_cast(tx.length() >> 8 & 0xff); - tx[18] = static_cast(sendAudioSeq >> 8 & 0xff); - tx[19] = static_cast(sendAudioSeq & 0xff); - tx[22] = static_cast(partial.length() >> 8 & 0xff); - tx[23] = static_cast(partial.length() & 0xff); - counter = counter + partial.length(); - //qDebug() << "Sending audio packet length: " << tx.length(); - SendTrackedPacket(tx); - sendAudioSeq++; - } - - + QByteArray audio = txaudio->getNextAudioChunk(); + int counter = 0; + while (counter < audio.length() ) { + QByteArray tx = QByteArray::fromRawData((const char*)p, sizeof(p)); + QByteArray partial = audio.mid(counter, 1364); + tx.append(partial); + tx[0] = static_cast(tx.length() & 0xff); + tx[1] = static_cast(tx.length() >> 8 & 0xff); + tx[18] = static_cast(sendAudioSeq >> 8 & 0xff); + tx[19] = static_cast(sendAudioSeq & 0xff); + tx[22] = static_cast(partial.length() >> 8 & 0xff); + tx[23] = static_cast(partial.length() & 0xff); + counter = counter + partial.length(); + //qDebug() << "Sending audio packet length: " << tx.length(); + SendTrackedPacket(tx); + sendAudioSeq++; + QThread::msleep(5); + } } void udpAudio::changeBufferSize(quint16 value) @@ -688,6 +692,9 @@ void udpAudio::DataReceived() connect(pkt7Timer, &QTimer::timeout, this, &udpBase::SendPkt7Idle); pkt7Timer->start(3000); // send pkt7 idle packets every 3 seconds + txAudioTimer = new QTimer(this); + connect(txAudioTimer, &QTimer::timeout, this, &udpAudio::sendTxAudio); + txAudioTimer->start(10); // send pkt7 idle packets every 3 seconds } } break; @@ -719,7 +726,7 @@ void udpAudio::DataReceived() lastReceivedSeq = gotSeq; - emit haveAudioData(r.mid(24)); + rxaudio->incomingAudio(r.mid(24)); } break; } diff --git a/udphandler.h b/udphandler.h index b39023a..64f2294 100644 --- a/udphandler.h +++ b/udphandler.h @@ -132,10 +132,10 @@ signals: public slots: void changeBufferSize(quint16 value); - void sendTxAudio(QByteArray d); private: + void sendTxAudio(); void DataReceived(); QAudioFormat format; quint16 bufferSize; @@ -159,6 +159,8 @@ private: audioHandler* txaudio; QThread* txAudioThread; + QTimer* txAudioTimer; + };