Finally success. I have clear voice transmission via udp and can filter the packets by the string I put in the buffer. The while loop seemed to be a mistake, as packets got sent multiple times.
I will add the transmitted ip and a package counter to the string later to make sure late packets won't be played. Thank you Chris, you were a big help.
Any further suggestions are still appreciated. Here is my working class:
#include "audio_send.h"
#include "config_manager.h"
#include <QIODevice>
#include <QUdpSocket>
#include <QAudioFormat>
#include <QAudioDeviceInfo>
#include <QAudioInput>
#define BUFFERSIZE 1024
AudioSend::AudioSend()
{
QAudioFormat format;
// Set up the desired format, for example:
format.setSampleRate(16000);
format.setChannelCount(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
if (!info.isFormatSupported(format))
format = info.nearestFormat(format);
input = new QAudioInput(format, this);
//input->setBufferSize(262144); //TODO: find best buffersize
input->setVolume(1.0);
socket
->connectToHost
(QHostAddress::LocalHost, ConfigManager
::getInstance()->getPort
());
}
AudioSend::~AudioSend()
{
delete input;
delete socket;
delete device;
}
void AudioSend::transmit()
{
qDebug("transmit()");
if (!input)
return;
qint64 len = input->bytesReady();
if (len > BUFFERSIZE)
len = BUFFERSIZE;
if (len > 0)
{
buffer.append("f:100000000;"); // 12 chars == 12 byte
buffer.append(device->read(len));
socket->write(buffer.constData());
}
}
void AudioSend::setRecording(bool state)
{
if (state)
{
qDebug("setRecording()");
device = input->start();
if (device != NULL)
connect(device, SIGNAL(readyRead()), this, SLOT(transmit()));
}
else
{
input->reset();
input->stop();
}
}
#include "audio_send.h"
#include "config_manager.h"
#include <QIODevice>
#include <QUdpSocket>
#include <QAudioFormat>
#include <QAudioDeviceInfo>
#include <QAudioInput>
#define BUFFERSIZE 1024
AudioSend::AudioSend()
{
QAudioFormat format;
// Set up the desired format, for example:
format.setSampleRate(16000);
format.setChannelCount(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
if (!info.isFormatSupported(format))
format = info.nearestFormat(format);
input = new QAudioInput(format, this);
//input->setBufferSize(262144); //TODO: find best buffersize
input->setVolume(1.0);
socket = new QUdpSocket(this);
socket->connectToHost(QHostAddress::LocalHost, ConfigManager::getInstance()->getPort());
}
AudioSend::~AudioSend()
{
delete input;
delete socket;
delete device;
}
void AudioSend::transmit()
{
qDebug("transmit()");
if (!input)
return;
qint64 len = input->bytesReady();
if (len > BUFFERSIZE)
len = BUFFERSIZE;
if (len > 0)
{
QByteArray buffer;
buffer.append("f:100000000;"); // 12 chars == 12 byte
buffer.append(device->read(len));
socket->write(buffer.constData());
}
}
void AudioSend::setRecording(bool state)
{
if (state)
{
qDebug("setRecording()");
device = input->start();
if (device != NULL)
connect(device, SIGNAL(readyRead()), this, SLOT(transmit()));
}
else
{
input->reset();
input->stop();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks