H264Rtmp.cpp 8.07 KB
Newer Older
1 2 3
/*
 * MIT License
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
26

xiongziliang committed
27
#include "H264Rtmp.h"
28

xiongziliang committed
29
namespace mediakit{
30 31

H264RtmpDecoder::H264RtmpDecoder() {
32
    _h264frame = obtainFrame();
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
}

H264Frame::Ptr  H264RtmpDecoder::obtainFrame() {
    //从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
    auto frame = obtainObj();
    frame->buffer.clear();
    frame->iPrefixSize = 4;
    return frame;
}

bool H264RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &rtmp, bool key_pos) {
    key_pos = decodeRtmp(rtmp);
    RtmpCodec::inputRtmp(rtmp, key_pos);
    return key_pos;
}

bool H264RtmpDecoder::decodeRtmp(const RtmpPacket::Ptr &pkt) {
    if (pkt->isCfgFrame()) {
        //缓存sps pps,后续插入到I帧之前
52 53
        _sps = pkt->getH264SPS();
        _pps  = pkt->getH264PPS();
54 55 56
        return false;
    }

57
    if (pkt->strBuf.size() > 9) {
58 59
        uint32_t iTotalLen = pkt->strBuf.size();
        uint32_t iOffset = 5;
xiongziliang committed
60 61 62 63
        uint8_t *cts_ptr = (uint8_t *) (pkt->strBuf.data() + 2);
        int32_t cts = (((cts_ptr[0] << 16) | (cts_ptr[1] << 8) | (cts_ptr[2])) + 0xff800000) ^ 0xff800000;
        auto pts = pkt->timeStamp + cts;

64 65 66 67 68 69 70 71
        while(iOffset + 4 < iTotalLen){
            uint32_t iFrameLen;
            memcpy(&iFrameLen, pkt->strBuf.data() + iOffset, 4);
            iFrameLen = ntohl(iFrameLen);
            iOffset += 4;
            if(iFrameLen + iOffset > iTotalLen){
                break;
            }
xiongziliang committed
72
            onGetH264_l(pkt->strBuf.data() + iOffset, iFrameLen, pkt->timeStamp , pts);
73 74 75 76 77 78 79
            iOffset += iFrameLen;
        }
    }
    return  pkt->isVideoKeyFrame();
}


xiongziliang committed
80
inline void H264RtmpDecoder::onGetH264_l(const char* pcData, int iLen, uint32_t dts,uint32_t pts) {
xiongziliang committed
81 82
    switch (H264_TYPE(pcData[0])) {
        case H264Frame::NAL_IDR: {
83
            //I frame
84 85 86 87 88 89 90
            if(_sps.length()){
                onGetH264(_sps.data(), _sps.length(), dts , pts);
            }
            if(_pps.length()){
                onGetH264(_pps.data(), _pps.length(), dts , pts);
            }
            onGetH264(pcData, iLen, dts , pts);
91
        }
92
            break;
xiongziliang committed
93
        case H264Frame::NAL_B_P: {
94
            //I or P or B frame
xiongziliang committed
95
            onGetH264(pcData, iLen, dts , pts);
96 97
        }
            break;
98 99 100 101 102 103 104 105
        case H264Frame::NAL_SPS: {
            _sps.assign(pcData, iLen);
        }
            break;
        case H264Frame::NAL_PPS:{
            _pps.assign(pcData, iLen);
        }
            break;
106 107 108 109
        default:
            break;
    }
}
xiongziliang committed
110
inline void H264RtmpDecoder::onGetH264(const char* pcData, int iLen, uint32_t dts,uint32_t pts) {
xiongziliang committed
111
    _h264frame->type = H264_TYPE(pcData[0]);
xiongziliang committed
112 113
    _h264frame->timeStamp = dts;
    _h264frame->ptsStamp = pts;
114 115
    _h264frame->buffer.assign("\x0\x0\x0\x1", 4);  //添加264头
    _h264frame->buffer.append(pcData, iLen);
116 117

    //写入环形缓存
118 119
    RtmpCodec::inputFrame(_h264frame);
    _h264frame = obtainFrame();
120 121 122 123 124 125
}



////////////////////////////////////////////////////////////////////////

126 127
H264RtmpEncoder::H264RtmpEncoder(const Track::Ptr &track) {
    _track = dynamic_pointer_cast<H264Track>(track);
xiongziliang committed
128

129 130 131 132 133 134 135
}

void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
    RtmpCodec::inputFrame(frame);

    auto pcData = frame->data() + frame->prefixSize();
    auto iLen = frame->size() - frame->prefixSize();
xiongziliang committed
136
    auto type = H264_TYPE(((uint8_t*)pcData)[0]);
137

xiongziliang committed
138 139 140
    if(!_gotSpsPps){
        //尝试从frame中获取sps pps
        switch (type){
xiongziliang committed
141
            case H264Frame::NAL_SPS:{
xiongziliang committed
142 143 144
                //sps
                if(_sps.empty()){
                    _sps = string(pcData,iLen);
145 146
                }
            }
xiongziliang committed
147
                break;
xiongziliang committed
148
            case H264Frame::NAL_PPS:{
xiongziliang committed
149 150 151
                //pps
                if(_pps.empty()){
                    _pps = string(pcData,iLen);
152 153
                }
            }
xiongziliang committed
154 155 156
                break;
            default:
                break;
157
        }
158

xiongziliang committed
159 160
        if(_track && _track->ready()){
            //尝试从track中获取sps pps信息
xiongziliang committed
161 162
            _sps = _track->getSps();
            _pps = _track->getPps();
xiongziliang committed
163 164 165 166
        }

        if(!_sps.empty() && !_pps.empty()){
            _gotSpsPps = true;
xiongziliang committed
167 168
            makeVideoConfigPkt();
        }
169 170 171
    }

    switch (type){
xiongziliang committed
172 173
        case H264Frame::NAL_IDR:
        case H264Frame::NAL_B_P:{
174 175 176 177 178 179 180 181 182 183
            if(_lastPacket && _lastPacket->timeStamp != frame->stamp()) {
                RtmpCodec::inputRtmp(_lastPacket, _lastPacket->isVideoKeyFrame());
                _lastPacket = nullptr;
            }

            if(!_lastPacket) {
                //I or P or B frame
                int8_t flags = 7; //h.264
                bool is_config = false;
                flags |= ((frame->keyFrame() ? FLV_KEY_FRAME : FLV_INTER_FRAME) << 4);
184

185 186 187 188
                _lastPacket = ResourcePoolHelper<RtmpPacket>::obtainObj();
                _lastPacket->strBuf.clear();
                _lastPacket->strBuf.push_back(flags);
                _lastPacket->strBuf.push_back(!is_config);
xiongziliang committed
189 190 191
                auto cts = frame->pts() - frame->dts();
                cts = htonl(cts);
                _lastPacket->strBuf.append((char *)&cts + 1, 3);
192

193 194 195 196 197 198
                _lastPacket->chunkId = CHUNK_VIDEO;
                _lastPacket->streamId = STREAM_MEDIA;
                _lastPacket->timeStamp = frame->stamp();
                _lastPacket->typeId = MSG_VIDEO;

            }
199
            auto size = htonl(iLen);
200 201 202
            _lastPacket->strBuf.append((char *) &size, 4);
            _lastPacket->strBuf.append(pcData, iLen);
            _lastPacket->bodySize = _lastPacket->strBuf.size();
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
        }
            break;

        default:
            break;
    }
}


void H264RtmpEncoder::makeVideoConfigPkt() {
    int8_t flags = 7; //h.264
    flags |= (FLV_KEY_FRAME << 4);
    bool is_config = true;

    RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
218 219
    rtmpPkt->strBuf.clear();

220 221 222 223 224 225 226 227
    //////////header
    rtmpPkt->strBuf.push_back(flags);
    rtmpPkt->strBuf.push_back(!is_config);
    rtmpPkt->strBuf.append("\x0\x0\x0", 3);

    ////////////sps
    rtmpPkt->strBuf.push_back(1); // version

228 229 230 231
    //DebugL<<hexdump(_sps.data(), _sps.size());
    rtmpPkt->strBuf.push_back(_sps[1]); // profile
    rtmpPkt->strBuf.push_back(_sps[2]); // compat
    rtmpPkt->strBuf.push_back(_sps[3]); // level
232 233
    rtmpPkt->strBuf.push_back(0xff); // 6 bits reserved + 2 bits nal size length - 1 (11)
    rtmpPkt->strBuf.push_back(0xe1); // 3 bits reserved + 5 bits number of sps (00001)
234
    uint16_t size = _sps.size();
235 236
    size = htons(size);
    rtmpPkt->strBuf.append((char *) &size, 2);
237
    rtmpPkt->strBuf.append(_sps);
238 239 240

    /////////////pps
    rtmpPkt->strBuf.push_back(1); // version
241
    size = _pps.size();
242 243
    size = htons(size);
    rtmpPkt->strBuf.append((char *) &size, 2);
244
    rtmpPkt->strBuf.append(_pps);
245 246 247 248 249 250

    rtmpPkt->bodySize = rtmpPkt->strBuf.size();
    rtmpPkt->chunkId = CHUNK_VIDEO;
    rtmpPkt->streamId = STREAM_MEDIA;
    rtmpPkt->timeStamp = 0;
    rtmpPkt->typeId = MSG_VIDEO;
251
    RtmpCodec::inputRtmp(rtmpPkt, false);
252 253 254
}


xiongziliang committed
255
}//namespace mediakit
256 257 258 259 260