In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Qt how to achieve video transmission TCP version", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Qt how to achieve video transmission TCP version" bar!
I. Preface
When developing audio and video, you will encounter the need to re-forward the audio and video. Of course, the ultimate way is to push and forward, and there are some simple scenarios where you can directly customize the protocol to send the video out. The speed of the LAN is still good. A similar project was done many years ago, it is nothing more than uploading local pictures to the server, it is as simple as that, the practical post upload of http is relatively simple and easy, and the binary data can be set up directly without a custom protocol, while if you use TCP or UDP communication, you must customize the protocol, because you do not know when the data is finished receiving the complete picture data, and you may be sending a lot of picture data at the same time. Moreover, it is not possible to distinguish which client sent the received picture. For a TCP long connection, a heartbeat is needed to detect the connection, so a set of protocols must be customized to support the communication. This protocol adopts the communication protocol format of the Shanghai regulatory platform, which is relatively extensible. The header information includes the type + the data length of the current complete packet. This type is the identification of the communication protocol. In this way, the next time we have another type of building intercom, it can be called IDOOR. According to this identity, the server can know which parsing algorithm is used to process the following data. The data length of the current complete packet can be used to process the received data. Only the data of this length means that a complete picture data has been received and then decoded. When the transmitted pictures reach a certain speed, such as transmitting 20 pictures per second, it is actually equivalent to transmitting video. The average person's naked eye sees 20 pictures per second and basically knows that it is a video.
In theory, TCP is a stable connection, it will not lose packets, nor will any packet be inserted into the middle of a packet. It can certainly ensure the integrity of a packet. There are also two kinds of TCP connections. One is a long connection, which communicates all the time once connected. It is mainly used in frequent communication scenarios such as real-time upload, and there is also a short connection, in which the client disconnects immediately after sending data or receiving data from the server. It is mainly used in infrequent communication scenarios such as alarm uploading. after all, alarm rarely occurs in a day, so it is better to use a short connection, which can save a lot of system overhead. Qt's communication to TCP is also easy to encapsulate. In some projects with only a few dozen connections with small concurrency, the efficiency is still OK. It is said that the QNetwork component of Qt5 has been rewritten at the bottom, and the efficiency is higher than that of Qt4. I do not need to check the corresponding source code in detail, just heard.
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 "tcpimagesocket.h" # include "devicefun.h" TcpImageSocket::TcpImageSocket (QObject * parent): QThread (parent) {stopped = false; tcpSocket = 0; / / timer parses the received data and can adjust the interval timerData = new QTimer (this); connect (timerData, SIGNAL (timeout ()), this, SLOT (checkData (); timerData- > start (30) } TcpImageSocket::~TcpImageSocket () {} void TcpImageSocket::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 to be processed synchronously. Therefore, it takes time for a changed thread to process / / base64 encoded data to transfer image data. The main time consuming is transcoding / / taking out data to send. Lock is needed here to avoid inserting data if (imageFlags.count () > 0) {QMutexLocker locker (& mutexImage). QString imageFlag = imageFlags.takeFirst (); QString imageData = imageDatas.takeFirst (); QImage image = DeviceFun::getImage (imageData); emit receiveImage (imageFlag, image);} / / take a break, otherwise CPU will always be occupied by msleep (1);} stopped = false } void TcpImageSocket::readData () {/ / received data stored in buffer needs to be locked QMutexLocker locker (& mutexData); / / received data is stored in queue and queued processing QByteArray data = tcpSocket- > readAll (); buffer.append (data); emit receiveData (data);} void TcpImageSocket::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 () one by one If (element.tagName () = = "ImageClient") {QString uuid = element.attribute ("Uuid"); QString flag = element.attribute ("Flag"); QDomNode childNode = element.firstChild (); QString name = childNode.nodeName (); QString value = element.text (); / / qDebug () tcpSocket = tcpSocket; connect (tcpSocket, SIGNAL (disconnected ()), this, SLOT (stop () Connect (tcpSocket, SIGNAL (error (QAbstractSocket::SocketError)), this, SLOT (stop ()); connect (tcpSocket, SIGNAL (readyRead ()), this, SLOT (readData ();} void TcpImageSocket::writeData (const QString & body) {QString data = DeviceFun::getSendXmlData (body, "IIMAGE:"); QByteArray buffer = data.toUtf8 (); tcpSocket- > write (buffer); tcpSocket- > flush (); emit sendData (buffer) } void TcpImageSocket::sendHeart (const QString & uuid) {/ / build xml string QStringList list; list.append (QString ("") .arg (uuid) .arg (DATETIME)); list.append ("Ok"); list.append (""); writeData (list.join (""));} void TcpImageSocket::sendAck (const QString & uuid) {/ / build xml string QStringList list List.append (QString ("") .arg (uuid) .arg (DATETIME)); list.append ("Ack"); list.append (""); writeData (list.join (")) } Thank you for your reading. the above is the content of "how to achieve video transmission TCP version of Qt". After the study of this article, I believe you have a deeper understanding of how to achieve video transmission TCP version of Qt, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.