From dc1cae21539fe59d6a130f91e36217c8071caa81 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sat, 23 Mar 2019 22:00:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96rtmp=E5=8F=91=E9=80=81?= =?UTF-8?q?=EF=BC=8C=E5=87=8F=E5=B0=91=E5=86=85=E5=AD=98=E6=8B=B7=E8=B4=9D?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E5=BC=80=E8=BE=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Rtmp/RtmpProtocol.cpp | 62 ++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/src/Rtmp/RtmpProtocol.cpp b/src/Rtmp/RtmpProtocol.cpp index 7d614007..b633337a 100644 --- a/src/Rtmp/RtmpProtocol.cpp +++ b/src/Rtmp/RtmpProtocol.cpp @@ -221,41 +221,55 @@ void RtmpProtocol::sendRtmp(uint8_t ui8Type, uint32_t ui32StreamId, auto strErr = StrPrinter << "不支持发送该类型的块流 ID:" << iChunkId << endl; throw std::runtime_error(strErr); } - + //是否有扩展时间戳 bool bExtStamp = ui32TimeStamp >= 0xFFFFFF; - RtmpHeader header; - header.flags = (iChunkId & 0x3f) | (0 << 6); - header.typeId = ui8Type; - set_be24(header.timeStamp, bExtStamp ? 0xFFFFFF : ui32TimeStamp); - set_be24(header.bodySize, buf->size()); - set_le32(header.streamId, ui32StreamId); - //估算rtmp包数据大小 - uint32_t totalSize = 0; - onSendRawData(obtainBuffer((char *) &header, sizeof(header))); - totalSize += sizeof(header); + //rtmp头 + BufferRaw::Ptr bufferHeader = obtainBuffer(); + bufferHeader->setCapacity(sizeof(RtmpHeader)); + bufferHeader->setSize(sizeof(RtmpHeader)); + //对rtmp头赋值,如果使用整形赋值,在arm android上可能由于数据对齐导致总线错误的问题 + RtmpHeader *header = (RtmpHeader*) bufferHeader->data(); + header->flags = (iChunkId & 0x3f) | (0 << 6); + header->typeId = ui8Type; + set_be24(header->timeStamp, bExtStamp ? 0xFFFFFF : ui32TimeStamp); + set_be24(header->bodySize, buf->size()); + set_le32(header->streamId, ui32StreamId); + //发送rtmp头 + onSendRawData(bufferHeader); - char acExtStamp[4]; + //扩展时间戳字段 + BufferRaw::Ptr bufferExtStamp; if (bExtStamp) { - //扩展时间戳 - set_be32(acExtStamp, ui32TimeStamp); - } - size_t pos = 0; - while (pos < buf->size()) { - if (pos) { - uint8_t flags = (iChunkId & 0x3f) | (3 << 6); - onSendRawData(obtainBuffer((char *) &flags, 1)); + //生成扩展时间戳 + bufferExtStamp = obtainBuffer(); + bufferExtStamp->setCapacity(4); + bufferExtStamp->setSize(4); + set_be32(bufferExtStamp->data(), ui32TimeStamp); + } + + //生成一个字节的flag,标明是什么chunkId + BufferRaw::Ptr bufferFlags = obtainBuffer(); + bufferFlags->setCapacity(1); + bufferFlags->setSize(1); + bufferFlags->data()[0] = (iChunkId & 0x3f) | (3 << 6); + + size_t offset = 0; + uint32_t totalSize = sizeof(RtmpHeader); + while (offset < buf->size()) { + if (offset) { + onSendRawData(bufferFlags); totalSize += 1; } if (bExtStamp) { //扩展时间戳 - onSendRawData(obtainBuffer(acExtStamp, 4)); + onSendRawData(bufferExtStamp); totalSize += 4; } - size_t chunk = min(_iChunkLenOut, buf->size() - pos); - onSendRawData(std::make_shared(buf,pos,chunk)); + size_t chunk = min(_iChunkLenOut, buf->size() - offset); + onSendRawData(std::make_shared(buf,offset,chunk)); totalSize += chunk; - pos += chunk; + offset += chunk; } _ui32ByteSent += totalSize; if (_ui32WinSize > 0 && _ui32ByteSent - _ui32LastSent >= _ui32WinSize) {