H264Rtmp.cpp 7.82 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
5
 *
xiongziliang committed
6 7 8
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
9
 */
10

xiongziliang committed
11
#include "H264Rtmp.h"
xiongziliang committed
12
namespace mediakit{
13 14

H264RtmpDecoder::H264RtmpDecoder() {
15
    _h264frame = obtainFrame();
16 17
}

18 19
H264Frame::Ptr H264RtmpDecoder::obtainFrame() {
    auto frame = FrameImp::create<H264Frame>();
20
    frame->_prefix_size = 4;
21 22 23
    return frame;
}

xiongziliang committed
24 25 26 27 28 29 30 31 32 33 34 35
/**
 * 返回不带0x00 00 00 01头的sps
 * @return
 */
static string getH264SPS(const RtmpPacket &thiz) {
    string ret;
    if (thiz.getMediaType() != FLV_CODEC_H264) {
        return ret;
    }
    if (!thiz.isCfgFrame()) {
        return ret;
    }
xiongziliang committed
36
    if (thiz.buffer.size() < 13) {
xiongziliang committed
37 38 39 40
        WarnL << "bad H264 cfg!";
        return ret;
    }
    uint16_t sps_size ;
xiongziliang committed
41
    memcpy(&sps_size, thiz.buffer.data() + 11, 2);
xiongziliang committed
42
    sps_size = ntohs(sps_size);
xiongziliang committed
43
    if ((int) thiz.buffer.size() < 13 + sps_size) {
xiongziliang committed
44 45 46
        WarnL << "bad H264 cfg!";
        return ret;
    }
xiongziliang committed
47
    ret.assign(thiz.buffer.data() + 13, sps_size);
xiongziliang committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    return ret;
}

/**
 * 返回不带0x00 00 00 01头的pps
 * @return
 */
static string getH264PPS(const RtmpPacket &thiz) {
    string ret;
    if (thiz.getMediaType() != FLV_CODEC_H264) {
        return ret;
    }
    if (!thiz.isCfgFrame()) {
        return ret;
    }
xiongziliang committed
63
    if (thiz.buffer.size() < 13) {
xiongziliang committed
64 65 66 67
        WarnL << "bad H264 cfg!";
        return ret;
    }
    uint16_t sps_size ;
xiongziliang committed
68
    memcpy(&sps_size, thiz.buffer.data() + 11, 2);
xiongziliang committed
69 70
    sps_size = ntohs(sps_size);

xiongziliang committed
71
    if ((int) thiz.buffer.size() < 13 + sps_size + 1 + 2) {
xiongziliang committed
72 73 74 75
        WarnL << "bad H264 cfg!";
        return ret;
    }
    uint16_t pps_size ;
xiongziliang committed
76
    memcpy(&pps_size, thiz.buffer.data() + 13 + sps_size + 1, 2);
xiongziliang committed
77 78
    pps_size = ntohs(pps_size);

xiongziliang committed
79
    if ((int) thiz.buffer.size() < 13 + sps_size + 1 + 2 + pps_size) {
xiongziliang committed
80 81 82
        WarnL << "bad H264 cfg!";
        return ret;
    }
xiongziliang committed
83
    ret.assign(thiz.buffer.data() + 13 + sps_size + 1 + 2, pps_size);
xiongziliang committed
84 85 86
    return ret;
}

87
void H264RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
88 89
    if (pkt->isCfgFrame()) {
        //缓存sps pps,后续插入到I帧之前
xiongziliang committed
90 91
        _sps = getH264SPS(*pkt);
        _pps  = getH264PPS(*pkt);
xiongziliang committed
92 93
        onGetH264(_sps.data(), _sps.size(), pkt->time_stamp , pkt->time_stamp);
        onGetH264(_pps.data(), _pps.size(), pkt->time_stamp , pkt->time_stamp);
94
        return;
95 96
    }

xiongziliang committed
97
    if (pkt->buffer.size() > 9) {
98 99
        auto iTotalLen = pkt->buffer.size();
        size_t iOffset = 5;
xiongziliang committed
100
        uint8_t *cts_ptr = (uint8_t *) (pkt->buffer.data() + 2);
xiongziliang committed
101
        int32_t cts = (((cts_ptr[0] << 16) | (cts_ptr[1] << 8) | (cts_ptr[2])) + 0xff800000) ^ 0xff800000;
xiongziliang committed
102
        auto pts = pkt->time_stamp + cts;
xiongziliang committed
103

104 105
        while(iOffset + 4 < iTotalLen){
            uint32_t iFrameLen;
xiongziliang committed
106
            memcpy(&iFrameLen, pkt->buffer.data() + iOffset, 4);
107 108 109 110 111
            iFrameLen = ntohl(iFrameLen);
            iOffset += 4;
            if(iFrameLen + iOffset > iTotalLen){
                break;
            }
xiongziliang committed
112
            onGetH264(pkt->buffer.data() + iOffset, iFrameLen, pkt->time_stamp , pts);
113 114 115 116 117
            iOffset += iFrameLen;
        }
    }
}

118
inline void H264RtmpDecoder::onGetH264(const char* pcData, size_t iLen, uint32_t dts,uint32_t pts) {
xiongziliang committed
119 120 121
    if(iLen == 0){
        return;
    }
122
#if 1
123 124
    _h264frame->_dts = dts;
    _h264frame->_pts = pts;
125
    _h264frame->_buffer.assign("\x00\x00\x00\x01", 4);  //添加264头
126
    _h264frame->_buffer.append(pcData, iLen);
127 128

    //写入环形缓存
129 130
    RtmpCodec::inputFrame(_h264frame);
    _h264frame = obtainFrame();
131 132 133 134 135
#else
    //防止内存拷贝,这样产生的264帧不会有0x00 00 01头
    auto frame = std::make_shared<H264FrameNoCacheAble>((char *)pcData,iLen,dts,pts,0);
    RtmpCodec::inputFrame(frame);
#endif
136 137 138 139
}

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

140 141
H264RtmpEncoder::H264RtmpEncoder(const Track::Ptr &track) {
    _track = dynamic_pointer_cast<H264Track>(track);
142 143 144 145 146 147 148 149
}

void H264RtmpEncoder::makeConfigPacket(){
    if (_track && _track->ready()) {
        //尝试从track中获取sps pps信息
        _sps = _track->getSps();
        _pps = _track->getPps();
    }
xiongziliang committed
150

151 152 153 154 155
    if (!_sps.empty() && !_pps.empty()) {
        //获取到sps/pps
        makeVideoConfigPkt();
        _gotSpsPps = true;
    }
156 157 158 159 160
}

void H264RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
    auto pcData = frame->data() + frame->prefixSize();
    auto iLen = frame->size() - frame->prefixSize();
xiongziliang committed
161
    auto type = H264_TYPE(((uint8_t*)pcData)[0]);
162 163 164
    if(type == H264Frame::NAL_SEI){
        return;
    }
165

166
    if (!_gotSpsPps) {
xiongziliang committed
167
        //尝试从frame中获取sps pps
168 169
        switch (type) {
            case H264Frame::NAL_SPS: {
xiongziliang committed
170
                //sps
171 172
                _sps = string(pcData, iLen);
                makeConfigPacket();
xiongziliang committed
173
                break;
174
            }
175 176 177 178
            case H264Frame::NAL_PPS: {
                //pps
                _pps = string(pcData, iLen);
                makeConfigPacket();
xiongziliang committed
179
                break;
180
            }
xiongziliang committed
181 182
            default:
                break;
183
        }
184 185
    }

186
    if(_lastPacket && (_lastPacket->time_stamp != frame->dts() || pcData[1]&0x80 != 0)) {
187
        RtmpCodec::inputRtmp(_lastPacket);
188 189
        _lastPacket = nullptr;
    }
190

191 192
    if(!_lastPacket) {
        //I or P or B frame
xiongziliang committed
193
        int8_t flags = FLV_CODEC_H264;
194
        bool is_config = false;
195
        flags |= (((frame->configFrame() || frame->keyFrame()) ? FLV_KEY_FRAME : FLV_INTER_FRAME) << 4);
196

xia-chu committed
197
        _lastPacket = RtmpPacket::create();
xiongziliang committed
198 199
        _lastPacket->buffer.push_back(flags);
        _lastPacket->buffer.push_back(!is_config);
200 201 202 203
        int32_t cts = frame->pts() - frame->dts();
        if (cts < 0) {
            cts = 0;
        }
204
        cts = htonl(cts);
xiongziliang committed
205
        _lastPacket->buffer.append((char *)&cts + 1, 3);
206

xiongziliang committed
207 208 209 210
        _lastPacket->chunk_id = CHUNK_VIDEO;
        _lastPacket->stream_index = STREAM_MEDIA;
        _lastPacket->time_stamp = frame->dts();
        _lastPacket->type_id = MSG_VIDEO;
211 212

    }
213
    uint32_t size = htonl((uint32_t)iLen);
xiongziliang committed
214 215 216
    _lastPacket->buffer.append((char *) &size, 4);
    _lastPacket->buffer.append(pcData, iLen);
    _lastPacket->body_size = _lastPacket->buffer.size();
217
    /*
218 219 220 221
    if (type == H264Frame::NAL_B_P) {
        RtmpCodec::inputRtmp(_lastPacket);
        _lastPacket = nullptr;
    }
222
    */
223 224 225
}

void H264RtmpEncoder::makeVideoConfigPkt() {
226 227 228 229
    if (_sps.size() < 4) {
        WarnL << "sps长度不足4字节";
        return;
    }
xiongziliang committed
230
    int8_t flags = FLV_CODEC_H264;
231 232 233
    flags |= (FLV_KEY_FRAME << 4);
    bool is_config = true;

xia-chu committed
234
    auto rtmpPkt = RtmpPacket::create();
xiongziliang committed
235
    //header
xiongziliang committed
236 237
    rtmpPkt->buffer.push_back(flags);
    rtmpPkt->buffer.push_back(!is_config);
xiongziliang committed
238
    //cts
xiongziliang committed
239
    rtmpPkt->buffer.append("\x0\x0\x0", 3);
240

xiongziliang committed
241
    //AVCDecoderConfigurationRecord start
xiongziliang committed
242 243 244 245
    rtmpPkt->buffer.push_back(1); // version
    rtmpPkt->buffer.push_back(_sps[1]); // profile
    rtmpPkt->buffer.push_back(_sps[2]); // compat
    rtmpPkt->buffer.push_back(_sps[3]); // level
246 247
    rtmpPkt->buffer.push_back((char)0xff); // 6 bits reserved + 2 bits nal size length - 1 (11)
    rtmpPkt->buffer.push_back((char)0xe1); // 3 bits reserved + 5 bits number of sps (00001)
xiongziliang committed
248
    //sps
249
    uint16_t size = (uint16_t)_sps.size();
250
    size = htons(size);
xiongziliang committed
251 252
    rtmpPkt->buffer.append((char *) &size, 2);
    rtmpPkt->buffer.append(_sps);
xiongziliang committed
253
    //pps
xiongziliang committed
254
    rtmpPkt->buffer.push_back(1); // version
255
    size = (uint16_t)_pps.size();
256
    size = htons(size);
xiongziliang committed
257 258
    rtmpPkt->buffer.append((char *) &size, 2);
    rtmpPkt->buffer.append(_pps);
259

xiongziliang committed
260 261 262 263 264
    rtmpPkt->body_size = rtmpPkt->buffer.size();
    rtmpPkt->chunk_id = CHUNK_VIDEO;
    rtmpPkt->stream_index = STREAM_MEDIA;
    rtmpPkt->time_stamp = 0;
    rtmpPkt->type_id = MSG_VIDEO;
265
    RtmpCodec::inputRtmp(rtmpPkt);
266 267
}

xiongziliang committed
268
}//namespace mediakit