0

I am in the process of developing a forwarder application for VLC and have run into the following problem:

The forwarder is supposed to receive a UDP stream and forward it to another middleware, which will then put the packets on a time triggered network. My issue lies with the irregularity of the packets sent by VLC. The target network processes the messages in a periodic manner, causing it to drop frames, once VLC starts sending many packets in quick succession (usually during scene changes for example). I need to enforce a sporadic behaviour for these bursts of data, so they may arrive as they please, but get forwarded with a minimum interrival time.

So far, my forwarder works as follows:

I put the whole thing in a QThread and connect the readyRead() signal to my receivePacket() slot which handles reception, processing and sending the data again by calling a second function sendPacket()

My first (naive) idea was to simply have the thread sleep for, say, 100 microseconds after each QUdpSocket::writeDatagram call, but this seems like a bad approach, since I will be suspending the whole thread and thus (I am not sure about this) lose packets from VLC which arrive during that time.

Another solution I thought of was creating a singleshot Qtimer, which would delay the writeDatagram() call. But here, I am not sure what will happen if another packet arrives and triggers the receivePacket() slot again and thus overwriting the message I am trying to delay sending.

Is there an easy way to solve this problem? The only way I could imagine possible would be to introduce an internal buffer to the sender, but this sounds like a lot of work and since I am on a deadline, I would like to avoid that.

Thank you in advance!

4

1 回答 1

0

据我了解,该sendPacket()方法被快速/经常调用。这意味着您将需要创建一个缓冲区,但这很容易(如果在多个线程中运行 receivePacket 和 sendPacket,还要在访问缓冲区之前/之后添加一个 QMutex::lock()/unlock())

在 Class.h 中:

QTimer *timer;  // Timer for periodically calling a sendPacket
QList<MyPacketClass*> buffer; // List containing the packets to be sent

在 Class.cpp Constructor 中创建带有连接的计时器:

timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(slotSendPacket()));
timer->start(1000); // Every second

在 receivePacket() 中,只需将数据添加到列表中:

buffer.append(new MyPacketClass(data));

在 class.cpp 中,为连接和从列表中弹出数据创建一个套接字:

void SomeClass::slotSendPacket()
{
  if(buffer.size() > 0)
  {
    sendPacket(buffer.takeAt(0));
  }
}

在方法 sendPacket(MyPacketClass* inData) 中释放分配的内存:

Do_whatever_needs_to_be_done_and_sent(); // Here is writeDatagram();
delete inData; // Free the memory for Packetclass
于 2013-10-10T09:26:06.993 回答