H265Rtmp.cpp 7.22 KB
Newer Older
xiongziliang committed
1 2 3
/*
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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.
 */

#include "H265Rtmp.h"
#ifdef ENABLE_MP4
#include "mpeg4-hevc.h"
#endif//ENABLE_MP4

namespace mediakit{

H265RtmpDecoder::H265RtmpDecoder() {
    _h265frame = obtainFrame();
}

22 23
H265Frame::Ptr H265RtmpDecoder::obtainFrame() {
    auto frame = FrameImp::create<H265Frame>();
xiongziliang committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    frame->_prefix_size = 4;
    return frame;
}

#ifdef ENABLE_MP4
/**
 * 返回不带0x00 00 00 01头的sps
 * @return
 */
static bool getH265ConfigFrame(const RtmpPacket &thiz,string &frame) {
    if (thiz.getMediaType() != FLV_CODEC_H265) {
        return false;
    }
    if (!thiz.isCfgFrame()) {
        return false;
    }
xiongziliang committed
40
    if (thiz.buffer.size() < 6) {
xiongziliang committed
41 42 43 44
        WarnL << "bad H265 cfg!";
        return false;
    }

xiongziliang committed
45 46
    auto extra = thiz.buffer.data() + 5;
    auto bytes = thiz.buffer.size() - 5;
xiongziliang committed
47 48 49

    struct mpeg4_hevc_t hevc = {0};
    if (mpeg4_hevc_decoder_configuration_record_load((uint8_t *) extra, bytes, &hevc) > 0) {
50 51
        uint8_t *config = new uint8_t[bytes * 2];
        int size = mpeg4_hevc_to_nalu(&hevc, config, bytes * 2);
xiongziliang committed
52 53
        if (size > 4) {
            frame.assign((char *) config + 4, size - 4);
xiongziliang committed
54
        }
55 56
        delete [] config;
        return size > 4;
xiongziliang committed
57 58 59 60 61
    }
    return false;
}
#endif

62
void H265RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
xiongziliang committed
63 64 65 66
    if (pkt->isCfgFrame()) {
#ifdef ENABLE_MP4
        string config;
        if(getH265ConfigFrame(*pkt,config)){
xiongziliang committed
67
            onGetH265(config.data(), config.size(), pkt->time_stamp , pkt->time_stamp);
xiongziliang committed
68 69
        }
#else
xiongziliang committed
70
        WarnL << "请开启MP4相关功能并使能\"ENABLE_MP4\",否则对H265-RTMP支持不完善";
xiongziliang committed
71
#endif
72
        return;
xiongziliang committed
73 74
    }

xiongziliang committed
75
    if (pkt->buffer.size() > 9) {
76 77
        auto iTotalLen = pkt->buffer.size();
        size_t iOffset = 5;
xiongziliang committed
78
        uint8_t *cts_ptr = (uint8_t *) (pkt->buffer.data() + 2);
xiongziliang committed
79
        int32_t cts = (((cts_ptr[0] << 16) | (cts_ptr[1] << 8) | (cts_ptr[2])) + 0xff800000) ^ 0xff800000;
xiongziliang committed
80
        auto pts = pkt->time_stamp + cts;
xiongziliang committed
81 82 83

        while(iOffset + 4 < iTotalLen){
            uint32_t iFrameLen;
xiongziliang committed
84
            memcpy(&iFrameLen, pkt->buffer.data() + iOffset, 4);
xiongziliang committed
85 86 87 88 89
            iFrameLen = ntohl(iFrameLen);
            iOffset += 4;
            if(iFrameLen + iOffset > iTotalLen){
                break;
            }
xiongziliang committed
90
            onGetH265(pkt->buffer.data() + iOffset, iFrameLen, pkt->time_stamp , pts);
xiongziliang committed
91 92 93 94 95
            iOffset += iFrameLen;
        }
    }
}

96
inline void H265RtmpDecoder::onGetH265(const char* pcData, size_t iLen, uint32_t dts,uint32_t pts) {
xiongziliang committed
97 98 99 100 101 102
    if(iLen == 0){
        return;
    }
#if 1
    _h265frame->_dts = dts;
    _h265frame->_pts = pts;
103
    _h265frame->_buffer.assign("\x00\x00\x00\x01", 4);  //添加265头
xiongziliang committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    _h265frame->_buffer.append(pcData, iLen);

    //写入环形缓存
    RtmpCodec::inputFrame(_h265frame);
    _h265frame = obtainFrame();
#else
    //防止内存拷贝,这样产生的265帧不会有0x00 00 01头
    auto frame = std::make_shared<H265FrameNoCacheAble>((char *)pcData,iLen,dts,pts,0);
    RtmpCodec::inputFrame(frame);
#endif
}

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

H265RtmpEncoder::H265RtmpEncoder(const Track::Ptr &track) {
    _track = dynamic_pointer_cast<H265Track>(track);
}

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

    if (!_sps.empty() && !_pps.empty() && !_vps.empty()) {
        //获取到sps/pps
        makeVideoConfigPkt();
        _gotSpsPps = true;
    }
}

void H265RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
    auto pcData = frame->data() + frame->prefixSize();
    auto iLen = frame->size() - frame->prefixSize();
    auto type = H265_TYPE(((uint8_t*)pcData)[0]);

    if (!_gotSpsPps) {
        //尝试从frame中获取sps pps
        switch (type) {
            case H265Frame::NAL_SPS: {
                //sps
                _sps = string(pcData, iLen);
                makeConfigPacket();
                break;
            }
            case H265Frame::NAL_PPS: {
                //pps
                _pps = string(pcData, iLen);
                makeConfigPacket();
                break;
            }
            case H265Frame::NAL_VPS: {
                //vps
                _vps = string(pcData, iLen);
                makeConfigPacket();
                break;
            }
            default:
                break;
        }
    }

    if(type == H265Frame::NAL_SEI_PREFIX || type == H265Frame::NAL_SEI_SUFFIX){
        return;
    }

xiongziliang committed
172
    if(_lastPacket && _lastPacket->time_stamp != frame->dts()) {
173
        RtmpCodec::inputRtmp(_lastPacket);
xiongziliang committed
174 175 176 177 178 179 180 181 182
        _lastPacket = nullptr;
    }

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

xia-chu committed
183
        _lastPacket = RtmpPacket::create();
xiongziliang committed
184 185
        _lastPacket->buffer.push_back(flags);
        _lastPacket->buffer.push_back(!is_config);
xiongziliang committed
186 187
        auto cts = frame->pts() - frame->dts();
        cts = htonl(cts);
xiongziliang committed
188
        _lastPacket->buffer.append((char *)&cts + 1, 3);
xiongziliang committed
189

xiongziliang committed
190 191 192 193
        _lastPacket->chunk_id = CHUNK_VIDEO;
        _lastPacket->stream_index = STREAM_MEDIA;
        _lastPacket->time_stamp = frame->dts();
        _lastPacket->type_id = MSG_VIDEO;
xiongziliang committed
194 195

    }
196
    uint32_t size = htonl((uint32_t)iLen);
xiongziliang committed
197 198 199
    _lastPacket->buffer.append((char *) &size, 4);
    _lastPacket->buffer.append(pcData, iLen);
    _lastPacket->body_size = _lastPacket->buffer.size();
xiongziliang committed
200 201 202 203 204 205 206 207
}

void H265RtmpEncoder::makeVideoConfigPkt() {
#ifdef ENABLE_MP4
    int8_t flags = FLV_CODEC_H265;
    flags |= (FLV_KEY_FRAME << 4);
    bool is_config = true;

xia-chu committed
208
    auto rtmpPkt = RtmpPacket::create();
xiongziliang committed
209
    //header
xiongziliang committed
210 211
    rtmpPkt->buffer.push_back(flags);
    rtmpPkt->buffer.push_back(!is_config);
xiongziliang committed
212
    //cts
xiongziliang committed
213
    rtmpPkt->buffer.append("\x0\x0\x0", 3);
xiongziliang committed
214 215 216 217 218

    struct mpeg4_hevc_t hevc = {0};
    string vps_sps_pps = string("\x00\x00\x00\x01", 4) + _vps +
                         string("\x00\x00\x00\x01", 4) + _sps +
                         string("\x00\x00\x00\x01", 4) + _pps;
219
    h265_annexbtomp4(&hevc, vps_sps_pps.data(), (int)vps_sps_pps.size(), NULL, 0, NULL, NULL);
xiongziliang committed
220 221 222 223 224 225 226 227
    uint8_t extra_data[1024];
    int extra_data_size = mpeg4_hevc_decoder_configuration_record_save(&hevc, extra_data, sizeof(extra_data));
    if (extra_data_size == -1) {
        WarnL << "生成H265 extra_data 失败";
        return;
    }

    //HEVCDecoderConfigurationRecord
xiongziliang committed
228
    rtmpPkt->buffer.append((char *)extra_data, extra_data_size);
xiongziliang committed
229

xiongziliang committed
230 231 232 233 234
    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;
235
    RtmpCodec::inputRtmp(rtmpPkt);
xiongziliang committed
236
#else
xiongziliang committed
237
    WarnL << "请开启MP4相关功能并使能\"ENABLE_MP4\",否则对H265-RTMP支持不完善";
xiongziliang committed
238 239 240 241
#endif
}

}//namespace mediakit