This repository has been archived by the owner on Oct 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use subthreads to process data, avoid lags in GUI.
- Loading branch information
Showing
19 changed files
with
787 additions
and
351 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#include "audiopacksender.h" | ||
|
||
AudioPackSender::AudioPackSender(char *ap) | ||
{ | ||
this->ap = new AudioPack; | ||
memcpy(this->ap, ap, sizeof(*(this->ap))); | ||
setAutoDelete(true); | ||
} | ||
|
||
AudioPackSender::~AudioPackSender() | ||
{ | ||
delete audio_socket; | ||
delete ap; | ||
} | ||
|
||
void AudioPackSender::run() | ||
{ | ||
groupAddress = QHostAddress("239.0.0.1"); | ||
audio_port = 8889; | ||
|
||
audio_socket = new QUdpSocket; | ||
audio_socket->setSocketOption(QAbstractSocket::MulticastTtlOption, 1); // 设置套接字属性 | ||
audio_socket->setSocketOption(QAbstractSocket::SendBufferSizeSocketOption, 1024 * 64); // 缓冲区最大存储 4个 数据包(单个 16K) | ||
|
||
qint32 res; | ||
qint32 dataLength = ap->len; | ||
uchar *dataBuffer = (uchar *)ap->data; | ||
|
||
qint32 packetNum = dataLength / UDP_MAX_SIZE; | ||
qint32 lastPaketSize = dataLength % UDP_MAX_SIZE; | ||
qint32 currentPacketIndex = 0; | ||
if(lastPaketSize != 0) | ||
{ | ||
packetNum++; | ||
} | ||
|
||
PackageHeader packageHead; | ||
packageHead.uTransPackageHdrSize = sizeof(packageHead); | ||
packageHead.uDataSize = dataLength; | ||
packageHead.uDataPackageNum = packetNum; | ||
|
||
uchar frameBuffer[sizeof(packageHead) + UDP_MAX_SIZE]; | ||
memset(frameBuffer, 0, sizeof(packageHead) + UDP_MAX_SIZE); | ||
|
||
packageHead.uTransPackageSize = packageHead.uTransPackageHdrSize + UDP_MAX_SIZE; | ||
packageHead.uDataPackageCurrIndex = 0; | ||
packageHead.uDataPackageOffset = 0; | ||
memcpy(frameBuffer, &packageHead, packageHead.uTransPackageHdrSize); | ||
res = audio_socket->writeDatagram( | ||
(const char *)frameBuffer, packageHead.uTransPackageSize, | ||
groupAddress, audio_port); | ||
if(res < 0) | ||
{ | ||
qDebug() << "audio_socket: Audio Pack Size Send Failed!"; | ||
} | ||
|
||
while(currentPacketIndex < packetNum) | ||
{ | ||
if(currentPacketIndex < (packetNum - 1)) | ||
{ | ||
packageHead.uTransPackageSize = packageHead.uTransPackageHdrSize + UDP_MAX_SIZE; | ||
packageHead.uDataPackageCurrIndex = currentPacketIndex + 1; | ||
packageHead.uDataPackageOffset = currentPacketIndex * UDP_MAX_SIZE; | ||
memcpy(frameBuffer, &packageHead, packageHead.uTransPackageHdrSize); | ||
memcpy(frameBuffer + packageHead.uTransPackageHdrSize, dataBuffer + packageHead.uDataPackageOffset, UDP_MAX_SIZE); | ||
|
||
res = audio_socket->writeDatagram( | ||
(const char *)frameBuffer, packageHead.uTransPackageSize, | ||
groupAddress, audio_port); | ||
|
||
if(res < 0) | ||
{ | ||
qDebug() << "audio_socket: Packet Send Failed!"; | ||
} | ||
|
||
currentPacketIndex++; | ||
} | ||
else | ||
{ | ||
packageHead.uTransPackageSize = packageHead.uTransPackageHdrSize + (dataLength - currentPacketIndex * UDP_MAX_SIZE); | ||
packageHead.uDataPackageCurrIndex = currentPacketIndex + 1; | ||
packageHead.uDataPackageOffset = currentPacketIndex * UDP_MAX_SIZE; | ||
memcpy(frameBuffer, &packageHead, packageHead.uTransPackageHdrSize); | ||
memcpy(frameBuffer + packageHead.uTransPackageHdrSize, dataBuffer + packageHead.uDataPackageOffset, dataLength - currentPacketIndex * UDP_MAX_SIZE); | ||
|
||
res = audio_socket->writeDatagram( | ||
(const char *)frameBuffer, packageHead.uTransPackageSize, | ||
groupAddress, audio_port); | ||
|
||
if(res < 0) | ||
{ | ||
qDebug() << "video_socket: Packet Send Failed!"; | ||
} | ||
|
||
currentPacketIndex++; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#ifndef AUDIOPACKSENDER_H | ||
#define AUDIOPACKSENDER_H | ||
|
||
#include <QRunnable> | ||
#include <QUdpSocket> | ||
|
||
#define UDP_MAX_SIZE 1200 // UDP 数据包最大长度 * MTU = 1500,故数据包大小 1500 - 20(IP头)- 8(UDP头) | ||
|
||
class AudioPackSender : public QRunnable | ||
{ | ||
public: | ||
explicit AudioPackSender(char *ap); | ||
~AudioPackSender(); | ||
|
||
protected: | ||
void run() override; | ||
|
||
private: | ||
QUdpSocket *audio_socket; | ||
QHostAddress groupAddress; | ||
quint16 audio_port; | ||
|
||
struct PackageHeader | ||
{ | ||
quint32 uTransPackageHdrSize; | ||
quint32 uTransPackageSize; | ||
quint32 uDataSize; | ||
quint32 uDataPackageNum; | ||
quint32 uDataPackageCurrIndex; | ||
quint32 uDataPackageOffset; | ||
}; | ||
|
||
struct AudioPack | ||
{ | ||
char data[1024 * 16]; // 单个音频数据包大小设为 16K,音质 44K/128Kbps(?) | ||
int len; | ||
} *ap; | ||
|
||
}; | ||
|
||
#endif // AUDIOPACKSENDER_H |
Oops, something went wrong.