【问题标题】:How to write N bits to QByteArray如何将 N 位写入 QByteArray
【发布时间】:2020-12-08 04:25:55
【问题描述】:

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


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 标头字段。使用此功能的示例:


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)
{
   // ...
}

我尝试将所有值写入标头,然后将它们写入 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 字节标头的这些结果:

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

【问题讨论】:

    标签: c++ qt rtp


    【解决方案1】:

    经过一番实验,我找到了一个更正确的方法,在QByteArray中写入RTP数据包,并意识到我之前的错误。因此,我在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;
    }
    
    

    这种方法既安全又保证fixedRTP包头的所有字段都填写正确。如果你需要填写QByteArraypayload,那么在用包头填充数组之后,我们自己编写payload。

    附:如果有任何与代码相关的问题或cmets,我很乐意回答并接受批评。

    【讨论】:

      猜你喜欢
      • 2019-05-26
      • 2015-01-27
      • 2012-10-10
      • 1970-01-01
      • 2012-06-24
      • 2014-03-07
      • 2016-04-05
      • 1970-01-01
      • 2016-05-26
      相关资源
      最近更新 更多