如何将 N 位写入 QByteArray

How to write N bits to QByteArray

我的任务是解码 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)的值必须写入buffer的第1字节。

我想要一个示例功能,例如阅读功能,以便您可以轻松使用它。函数示例:


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;
}

这种方法既安全又能保证fixed RTP packet header are filled in correctly. If you need to fill QByteArray and payload的所有字段,然后在用包头填充数组后,我们自己写入payload。

P.S。如果有任何与代码相关的问题或意见,我将很乐意回答并接受批评。