gpt4 book ai didi

c++ - 如何将N位写入QByteArray

转载 作者:行者123 更新时间:2023-12-02 09:59:21 26 4
gpt4 key购买 nike

我的任务是解码RTP数据包并提取该数据包的有效负载(音频数据)。为了绕过所有RTP header 字段,我具有以下功能:


quint32 readNBitsByRange(quint32 position, quint32 count, const QByteArray &array)
{
quint32 accuml = 0;
while (count != 0) {
const quint32 l = (8 - position % 8);
const quint32 u = (l < count ? l : count);
const quint32 f = (8 - u);
accuml <<= u;
accuml |= ((*(array.data() + position / 8) << (8 - l)) & (((1 << u) - 1) << f)) >> f;
position += u;
count -= u;
}
return accuml;
}

作为参数,该函数采用将从其进行读取的位置,位数和应从其进行读取的缓冲区。借助此功能,我可以读取所有RTP header 字段。使用此功能的示例:

int main()
{
// ... We get The RTP packet in binary form and write it to QByteArray ...
QByteArray array;
for (quint32 i = 0; i < rtpBinaryDataLength; ++i) {
array.push_back(rtpBinaryData[i]);
}

// 0 1 2 3
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |V=2|P|X| CC |M| PT | sequence number |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | timestamp |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | synchronization source (SSRC) identifier |
// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
// | contributing source (CSRC) identifiers |
// | .... |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

quint16 rtpVersion = readNBitsByRange(0x000, 0x002, array);
quint16 rtpPadding = readNBitsByRange(0x002, 0x001, array);
quint16 rtpExtension = readNBitsByRange(0x003, 0x001, array);

// .. And so on ...
}

一切正常!但这就是问题所在!现在,我有一个任务将RTP数据包值写入QByteArray。我不知道该怎么做!我们只知道,例如,必须将RTP数据包的4个字段(V,P,X,CC)的值写入缓冲区的第一个字节。
我想有一个示例功能,例如阅读功能,以便您可以轻松地使用它。函数示例:

void writeNBits(quint32 position, quint32 count, quint32 val, QByteArray &array)
{
// ...
}

我尝试将所有值写入 header ,然后将它们写入QByteArray:
struct RtpHeader
{
unsigned m_v :2;
unsigned m_p :1;
unsigned m_x :1;
unsigned m_cc:4;
unsigned m_m :1;
unsigned m_pt:7;
uint16_t m_sn;
uint32_t m_tm;
uint32_t m_ssrc;
};

int main(int argc, char *argv[])
{
RtpHeader hdr;

hdr.m_v = 2;
hdr.m_p = 0;
hdr.m_x = 0;
hdr.m_cc = 0;
hdr.m_m = 1;
hdr.m_pt = 8;
hdr.m_sn = 59133;
hdr.m_tm = 240;
hdr.m_ssrc = 0xDEE0EE8F;

QByteArray array(reinterpret_cast<const char*>(&hdr), sizeof(hdr));
for (quint8 i = 0; i < 240; i++) {
array.push_back(0xD5); // Silence
}

QFile file("./rawRtpPacket.bin");
file.open(QIODevice::WriteOnly);
file.write(array);
file.close();

return EXIT_SUCCESS;
}

但这不是我应该得到的!
例如,我应该收到以下12字节 header 的结果:
80 88 E6 FD  00 00 00 F0  DE E0 EE 8F
但是我得到了不同的结果:
02 11 FD E6  F0 00 00 00  8F EE E0 DE 
如果仔细观察,从4个字节到12个字节,我必须镜像这些值,但是以前两个字节为代价,我不明白为什么会得到一个完全不同的字节。
我写:V-2,P-0,X-0,CC-0,应该得到 10000000,但我得到 00000010

最佳答案

经过一些实验,我发现了一种更正确的方法来将RTP数据包写入QByteArray并实现了我以前的错误。因此,我在RTP中编写了正确的QByteArray数据包条目的样本版本:


class RtpHeader
{
public:

quint16 m_vp:0x02;
quint16 m_pf:0x01;
quint16 m_xf:0x01;
quint16 m_cc:0x04;
quint16 m_mb:0x01;
quint16 m_pt:0x07;
quint16 m_sn;
quint32 m_tm;
quint32 m_ss;

};

class RtpHeaderEncoder
{
public:

RtpHeaderEncoder(void) noexcept = delete;
RtpHeaderEncoder &operator=(const RtpHeaderEncoder &) noexcept = delete;
RtpHeaderEncoder &operator=(RtpHeaderEncoder &&) noexcept = delete;
RtpHeaderEncoder(const RtpHeaderEncoder &) noexcept = delete;
RtpHeaderEncoder(RtpHeaderEncoder &&) noexcept = delete;
~RtpHeaderEncoder(void) noexcept = delete;

static QByteArray encode(const RtpHeader &hdr) noexcept;

};

QByteArray RtpHeaderEncoder::encode(const RtpHeader &hdr) noexcept
{
QByteArray array;

if ((hdr.m_vp == 0x02) && (hdr.m_pf == 0x00) && (hdr.m_cc <= 0x0F) && (hdr.m_pt <= 0x12)) {

QDataStream stream(&array, QIODevice::WriteOnly);
stream << (((hdr.m_vp & 0x00003) << 0x01E)|
((hdr.m_pf & 0x00001) << 0x01D)|
((hdr.m_xf & 0x00001) << 0x01C)|
((hdr.m_cc & 0x0000F) << 0x018)|
((hdr.m_mb & 0x00001) << 0x017)|
((hdr.m_pt & 0x0007F) << 0x010)|
((hdr.m_sn & 0x0FFFF) << 0x000));

stream << hdr.m_tm << hdr.m_ss;
}
return array;
}

int main(int argc, char *argv[])
{
RtpHeader hdr;
hdr.m_vp = 2;
hdr.m_pf = 0;
hdr.m_xf = 0;
hdr.m_cc = 0;
hdr.m_mb = 0;
hdr.m_pt = 8;
hdr.m_sn = 1;
hdr.m_tm = 201452158;
hdr.m_ss = 111537764;

QFile file("./rawRtpHeader.bin");
file.open (QIODevice::WriteOnly);
file.write(RtpHeaderEncoder::encode(hdr));
file.close();

return EXIT_SUCCESS;
}

此方法既更安全,又可以确保 fixed RTP数据包头的所有字段均正确填写。如果您需要填充 QByteArraypayload,那么在用包头填充数组之后,我们就编写有效负载本身。
附言如果对代码有任何疑问或意见,我将很乐意回答并接受批评。

关于c++ - 如何将N位写入QByteArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63477872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com