我的目标是通过 LAN 网络传输 *.wav 文件,没有延迟或最少。
我们还分部分读取服务器机器上的文件,均为 320 字节。在此之后,我们通过 UDP 发送数据包并在 jitter-buffer 中写入接收。jitter-buffer 的大小是 10。我应该在定时器上设置什么延迟才能获得清晰的声音?
这是发件人:
void MainWindow::on_start_tx_triggered()
{
timer1 = new QTimer (this);
udpSocketout = new QUdpSocket(this);
qDebug()<<"Start";
for (int i = 0; i < playlist.size(); ++i)
{
inputFile.setFileName(playlist.at(i));
qDebug()<<inputFile.fileName();
if (!inputFile.open(QIODevice::ReadOnly))
{
qDebug()<< "file not found;";
}
}
connect(timer1, SIGNAL(timeout()), this, SLOT(writeDatagrams()));
timer1->start(5);
}
void MainWindow::writeDatagrams()
{
if(!inputFile.atEnd()){
payloadout = inputFile.read(320);
}
qDebug()<<payloadout;
QDataStream out(&datagramout, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out << qint64(0);
out << payloadout;
out.device()->seek(qint64(0));
out << qint64(datagramout.size() - sizeof(qint64));
qint64 writtenBytes = udpSocketout->writeDatagram(datagramout, remoteHOST, remotePORT);
qDebug() << "Sent " << writtenBytes << " bytes.";
}
这是接收器和播放器:
void MainWindow::on_start_rx_triggered()
{
udpSocketin = new QUdpSocket(this);
udpSocketin->bind(localHOST, localPORT);
connect(udpSocketin, SIGNAL(readyRead()),
this, SLOT(readDatagrams()));
QDataStream out(&datagramout, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
timer2 = new QTimer (this);
connect(timer2, SIGNAL(timeout()), this, SLOT(playbuff()));
timer2->start(50);
audioout = new QAudioOutput(format, this);
}
void MainWindow::readDatagrams()
{
datagramin.resize(udpSocketin->pendingDatagramSize());
qint64 receiveBytes = udpSocketin->readDatagram(datagramin.data(), datagramin.size());
qDebug() << "Receive " << receiveBytes << " bytes.";
QDataStream in(&datagramin, QIODevice::ReadOnly);
in.setVersion(QDataStream::Qt_4_7);
quint64 size = 0;
if(in.device()->size() > sizeof(quint64))
{
in >> size;
}
else
return;
if(in.device()->size() < size)
return;
in >> payloadin;
qDebug() << payloadin.size();
emit jitterbuff();
}
void MainWindow::jitterbuff()
{
if (buff_pos < SIZE_OF_BUF)
{
QDataStream out(&buffered, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out << payloadin;
buff_pos++;
}
else
buff_pos = 0;
}
void MainWindow::playbuff()
{
qDebug() << "YES!!!";
buffer = new QBuffer(&buffered);
buffer->open(QIODevice::ReadOnly);
audioout->start(buffer);
QEventLoop loop;
QTimer::singleShot(50, &loop, SLOT(quit()));
loop.exec();
buffer->close();
}