[英]How to write N bits to QByteArray
我的任務是解碼 RTP 數據包並提取該數據包的有效負載(音頻數據)。 為了繞過所有 RTP header 字段,我有這個 function:
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;
}
作為 arguments,function 獲取將從中進行讀取的 position、位數以及應從中進行讀取的緩沖區。 感謝這個 function,我可以讀取所有 RTP header 字段。 使用此 function 的示例:
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,我不知道該怎么做,我們只知道,例如,4 個字段的值(V.P、X、CC ) 的 RTP 數據包必須寫入緩沖區的第一個字節。
我想要一個示例 function,喜歡閱讀,以便您可以輕松使用它。 function 的示例:
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
並意識到我之前的錯誤。 因此,我在QByteArray
中編寫了正確的RTP
數據包條目的示例版本:
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;
}
這種方式既更加安全,又能保證固定RTP
包header的所有字段都填寫正確。 如果您需要填充QByteArray
和payload ,那么在用 package header 填充數組之后,我們編寫 payload 本身。
PS如果有任何與代碼相關的問題或意見,我會很樂意回答並接受批評。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.