How I can play streaming audio over Ethernet in Qt?

后端 未结 1 1590
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-28 10:10

My goal is to transmit *.wav file over LAN network without delay or with minimal one.

Also we read a file on a server machine by parts, 320 bytes both. After this we

相关标签:
1条回答
  • 2020-12-28 10:30

    This problem was solved. QAdioOutput has two modes; there are "push" and "pull". I give a pointer to a QIODevice, and write data directly to this. Solution:

    Reading UDP socket:

    void MainWindow::on_start_rx_triggered()
    {
        udpSocketin = new QUdpSocket(this);
        udpSocketin->bind(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->setInterval(15*9);
        audioout = new QAudioOutput(format, this);
        input = audioout->start();
    }
    
    void MainWindow::readDatagrams()
    {
        if (udpSocketin->hasPendingDatagrams()){
        datagramin.resize(udpSocketin->pendingDatagramSize());
        qint64 receiveBytes = udpSocketin->readDatagram(datagramin.data(), datagramin.size());
        if (receiveBytes <= 0)
        {
            msg.warning(this, "File ERROR", "The end!", QMessageBox::Ok);
            emit on_stop_rx_triggered();
        }
        QDataStream in(&datagramin, QIODevice::ReadOnly);
        in.setVersion(QDataStream::Qt_4_7);
        quint64 size = 0;
        if(in.device()->size() > sizeof(quint64))
        {
            in >> size;
        }
        else return;
        in >> rxfilename;
        in >> name;
        in >> payloadin;
        emit jitterbuff();
    }
    
    void MainWindow::jitterbuff()
    {
        if (buff_pos < SIZE_OF_BUF)
        {
            buffered.append(payloadin);
            buff_pos++;
        }
        else
        {
            timer2->start();
            buffered.clear();
            buff_pos = 0;
        }
    }
    
    void MainWindow::playbuff()
    {
        if (!buffered.isEmpty())
        {
            buffer = new QBuffer(&buffered);
            buffer->open(QIODevice::ReadOnly);
            input->write(buffered);
            buffer->close();
        }
    }
    

    Writing to UDP socket:

    void MainWindow::on_start_tx_triggered()
    {
        timer1 = new QTimer (this);
        udpSocketout = new QUdpSocket(this);
        inputFile.setFileName(playlist.at(playIDfile));
        if (!inputFile.open(QIODevice::ReadOnly))
        {
            msg.warning(this, "File ERROR", "File not found!", QMessageBox::Ok);
            return;
        }
        fileinfo = new QFileInfo (inputFile);
        txfilename = fileinfo->fileName();
        ui->playedFile->setText("Now played: " + txfilename);
        connect(timer1, SIGNAL(timeout()), this, SLOT(writeDatagrams()));
        timer1->start(15);
    }
    void  MainWindow::writeDatagrams()
    {
    
        if(!inputFile.atEnd()){
            payloadout = inputFile.read(SIZE_OF_SOUND);
            QDataStream out(&datagramout, QIODevice::WriteOnly);
            out.setVersion(QDataStream::Qt_4_7);
            out << qint64(0);
            out << txfilename;
            out << name;
            out << payloadout;
            out.device()->seek(qint64(0));
            out << qint64(datagramout.size() - sizeof(qint64));
            qint64 writtenBytes = udpSocketout->writeDatagram(datagramout, remoteHOST, remotePORT);
        }
    }
    

    If somebody will have seems problem, I will try to help him/her.

    0 讨论(0)
提交回复
热议问题