In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to achieve video transmission UDP version of Qt", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to achieve video transmission UDP version of Qt"!
I. Preface
Qt network communication class, we usually use three: QTcpSocket client class, QTcpServer server class, QUdpSocket communication class, why there is no QUdpServer class? In fact, UDP is connectionless communication, occupies very little resources, it can be either a client or a server, if you want to serve as a server, you can specify the port to call the bind method. This program supports both TCP mode and UDP mode. After the actual test, it is recommended to use TCP mode. Because UDP mode sends a large number of packets in a short time without connection, it is found that packets will be lost, and the size of the packet is limited, which is 65507 bytes, about 64K, so the resolution of the picture transmitted in real time in UDP mode can not be too large. The measured 64000480 video file is still quite good. 720p is basically a bit miserable, with a lot of packet loss. It may need to be improved on the agreement at a later stage.
This procedure and the agreement of the picture using base64 coding transmission, received after the base64 string will be decoded to generate pictures, QByteArray built-in class toBase64 method into base64-encoded string, QByteArray::fromBase64 method to restore the base64 string into data. After many experiments, the statistical data show that the speed of encoding and decoding is OK, including 720p picture coding 25ms-30ms, decoding 15msLay 20ms1080p picture coding 35ms-40ms, decoding 25ms-30ms. Generally speaking, there is no problem in transmitting 25-30 pictures per second and decoding 25-30 pictures. It is just CPU encoding and decoding. If there are a large number of channels open, it still consumes a lot of CPU, but it is still easy to cope with some simple application scenarios without any pressure.
Communication protocol:
TCP persistent connection and UDP protocol are optional and the default communication port is 6000.
The custom xml communication protocol is adopted.
All transmissions plus 20-byte headers: IIMAGE:0000000000000,IIMAGE: a fixed header, followed by a string of 13 bytes of content (including 20 header lengths).
The header byte is omitted in the following protocol.
The uuid in the data returned by the server is the uuid corresponding to the received message.
The server returns with the current time each time, which can be used for client calibration.
The client sends the heartbeat and the server receives the heartbeat and returns the base64 encoded string of the picture sent by the Ok. The client sends the heartbeat back to the base64 encoded string / 9jUnip 4AQAQSkZJRgABAQEAYABgAAD2wDAAgGBgGBgGBgGBgJCJC4nICIsIxwcKDcpLDNDQ0Hyc5PTgyPC4zNDLLG2wBDAkJCKDcpLDQ0HycNDCNDLG2wBDAkJCJCLDBgNDRgyIRMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjyMjyMjyMjIyMjIyMjIyMjIyMjIyMjMjyMjyMjyMjyMjyMjyMjMjyMjIyMjIyMjIyMjyMjyIyMjMjyMjyMjMjyMjyMjMjyMjyMjyMjyMjyMjyIyMjMjyMjyMjyMjyMjyMjyMjyMjyMjyMjyMjyMjyMjyMjMjMjyMjMjMjyMjMjIyMjMjIyMjMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjMjMjMjyMjMjMjyMjMjIyMjIyMjIyMjIyMjIyMjIyMjMjMjyMjMjMjMjMjMjMjIyMjMjIyMjMjIyMjMjIyMjMjIyMjMjIyMjMjIyMjMjIyMjMjIyMjMjMjMjMjM
Multi-threaded sending and receiving picture data and parsing picture data, do not card the main interface.
Both TCP and UDP modes are supported, and client-side classes and server-side classes of TCP mode and UDP mode are encapsulated.
The picture transmission client supports sending to multiple servers at the same time, which can be sent to multiple student machines on the same screen as a teacher.
At the same time, multiple clients are supported to send pictures to the server at the same time, and each connection on the server will automatically open a thread to send and receive and parse the picture data.
Custom label control signal slot mechanism to draw pictures, do not card the main interface.
With its own heartbeat mechanism to judge offline, automatically reconnect the server, you can set the timeout.
Each message has a unique message ID uuid. After receiving it, the server will return the corresponding uuid message to indicate receipt. The client can determine that the server parsing is successful based on this return message, and there is no need to send it again. This ensures that the outgoing data server receives and parses successfully.
Each message has a unique picture ID flag, which is equivalent to the ID number, according to which interface it needs to be parsed and displayed.
The picture is sent in the string format of base64, and the receiving end receives the picture data of the base64 string and regenerates the picture after decoding.
All data transceivers have signals to be sent out to facilitate output and viewing.
All provide singleton classes to facilitate direct use without new when there is only one.
Using the custom xml protocol, you are free to expand other attribute fields, such as bringing image content, etc.
Third, effect picture
4. Core code # include "udpimageclient.h" # include "devicefun.h" QScopedPointer UdpImageClient::self;UdpImageClient * UdpImageClient::Instance () {if (self.isNull ()) {static QMutex mutex; QMutexLocker locker (& mutex); if (self.isNull ()) {self.reset (new UdpImageClient);}} return self.data () } UdpImageClient::UdpImageClient (QObject * parent): QThread (parent) {/ / if it is a public network, please adjust this value by yourself. The public network needs to adjust packageSize = 10000; flag = "SHJC00000001"; serverIP = "127.0.0.1"; serverPort = 6000; stopped = false; / / UDP communication object udpSocket = new QUdpSocket (this); connect (udpSocket, SIGNAL (readyRead ()), this, SLOT (readData () / / the timer parses the received data and can adjust the interval timerData = new QTimer (this); connect (timerData, SIGNAL (timeout ()), this, SLOT (checkData (); timerData- > setInterval (100); / / start the timer connect (this, SIGNAL (started ()), this, SLOT (started ()) after the binding signal is started) / bind sending data signal slot connect (this, SIGNAL (readyWrite (QString)), this, SLOT (sendImage (QString));} UdpImageClient::~UdpImageClient () {this- > stop () } void UdpImageClient::run () {while (! stopped) {/ / Thread is used here, which can actually be handled with a timer. After all, the write of tcp is asynchronous, and the operating system automatically schedules / / for later expansion, such as whether it is sent successfully or not, it needs synchronous processing. Therefore, it takes time for the changed thread to process / / convert the picture data into base64-encoded data. The main time-consuming is transcoding / / taking out the data and sending it. It needs to be locked to avoid inserting data if (images.count () > 0) {QMutexLocker locker (& mutexImage). QImage image = images.takeFirst (); QString imageData = DeviceFun::getImageData (image); emit readyWrite (imageData);} / / take a little rest, otherwise CPU will be occupied by msleep (1);} stopped = false;} void UdpImageClient::readData () {QHostAddress host; quint16 port; QByteArray data While (udpSocket- > hasPendingDatagrams ()) {data.resize (udpSocket- > pendingDatagramSize ()); udpSocket- > readDatagram (data.data (), data.size (), & host, & port); / / received data stored in buffer requires locking QMutexLocker locker (& mutexData); buffer.append (data); emit receiveData (data) }} void UdpImageClient::checkData () {if (buffer.length () = = 0) {return;} / / data processing needs to be locked to prevent data QMutexLocker locker (& mutexData) from being inserted at this time; QDomDocument dom; if (! DeviceFun::getReceiveXmlData (buffer, dom, "IIMAGE:", 11, true)) {return } / / check data QDomElement element = dom.documentElement (); if (element.tagName () = = "ImageServer") {QString uuid = element.attribute ("Uuid"); QDomNode childNode = element.firstChild (); QString name = childNode.nodeName (); QString value = element.text (); / / qDebug () wait (); udpSocket- > disconnectFromHost () If (timerData- > isActive ()) {timerData- > stop ();}} void UdpImageClient::setPackageSize (int packageSize) {if (packageSize packageSize = packageSize;}} void UdpImageClient::setFlag (const QString & flag) {this- > flag = flag;} void UdpImageClient::setServerIP (const QString & serverIP) {this- > serverIP = serverIP;} void UdpImageClient::setServerPort (int serverPort) {this- > serverPort = serverPort } void UdpImageClient::writeData (const QString & body) {/ / build xml string QStringList list; list.append (QString (") .arg (DeviceFun::getUuid ()) .arg (flag)); list.append (body); list.append ("); / / call general methods to form complete data according to the protocol QString data = DeviceFun::getSendXmlData (list.join (")," IIMAGE: "); QByteArray buffer = data.toUtf8 () / / udp can only send a maximum of 65507 bytes of data. If it exceeds 64K, it will fail / / so you need to subpackage manually. If the packet in the public network is smaller, if (packageSize = = 65500) {udpSocket- > writeDatagram (buffer, QHostAddress (serverIP), serverPort);} else {int len = buffer.length (); int count = len / packageSize + 1; for (int I = 0; I)
< count; i++) { QByteArray temp = buffer.mid(i * packageSize, packageSize); udpSocket->WriteDatagram (temp, QHostAddress (serverIP), serverPort);}} emit sendData (buffer);} void UdpImageClient::sendImage (const QString & body) {writeData (QString ("% 1") .arg (body));} void UdpImageClient::append (const QImage & image) {/ / need to be locked here to avoid fetching data QMutexLocker locker (& mutexImage) / / limit the maximum number of messages in the queue to avoid frantically inserting if (this- > isRunning () & & images.count () < 10) {images when offline.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.