H265Rtmp.cpp 7.09 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) {
ziyue committed
76 77
        auto total_len = pkt->buffer.size();
        size_t offset = 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;
ziyue committed
81 82 83 84 85 86
        while (offset + 4 < total_len) {
            uint32_t frame_len;
            memcpy(&frame_len, pkt->buffer.data() + offset, 4);
            frame_len = ntohl(frame_len);
            offset += 4;
            if (frame_len + offset > total_len) {
xiongziliang committed
87 88
                break;
            }
ziyue committed
89 90
            onGetH265(pkt->buffer.data() + offset, frame_len, pkt->time_stamp, pts);
            offset += frame_len;
xiongziliang committed
91 92 93 94
        }
    }
}

95
inline void H265RtmpDecoder::onGetH265(const char* pcData, size_t iLen, uint32_t dts,uint32_t pts) {
xiongziliang committed
96 97 98 99 100 101
    if(iLen == 0){
        return;
    }
#if 1
    _h265frame->_dts = dts;
    _h265frame->_pts = pts;
102
    _h265frame->_buffer.assign("\x00\x00\x00\x01", 4);  //添加265头
xiongziliang committed
103 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
    _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();
ziyue committed
132
        _got_config_frame = true;
xiongziliang committed
133 134 135 136
    }
}

void H265RtmpEncoder::inputFrame(const Frame::Ptr &frame) {
ziyue committed
137 138
    auto data = frame->data() + frame->prefixSize();
    auto len = frame->size() - frame->prefixSize();
139
    auto type = H265_TYPE(data[0]);
ziyue committed
140 141 142 143
    switch (type) {
        case H265Frame::NAL_SPS: {
            if (!_got_config_frame) {
                _sps = string(data, len);
xiongziliang committed
144 145
                makeConfigPacket();
            }
ziyue committed
146 147 148 149 150
            break;
        }
        case H265Frame::NAL_PPS: {
            if (!_got_config_frame) {
                _pps = string(data, len);
xiongziliang committed
151 152
                makeConfigPacket();
            }
ziyue committed
153 154 155 156 157
            break;
        }
        case H265Frame::NAL_VPS: {
            if (!_got_config_frame) {
                _vps = string(data, len);
xiongziliang committed
158 159
                makeConfigPacket();
            }
160
            break;
xiongziliang committed
161
        }
ziyue committed
162
        default: break;
xiongziliang committed
163 164
    }

ziyue committed
165
    if (!_rtmp_packet) {
xiongziliang committed
166 167 168 169
        //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);
ziyue committed
170
        _rtmp_packet = RtmpPacket::create();
171
        _rtmp_packet->buffer.clear();
ziyue committed
172 173 174 175 176 177
        _rtmp_packet->buffer.push_back(flags);
        _rtmp_packet->buffer.push_back(!is_config);
        int32_t cts = frame->pts() - frame->dts();
        if (cts < 0) {
            cts = 0;
        }
xiongziliang committed
178
        cts = htonl(cts);
ziyue committed
179 180 181 182 183 184
        _rtmp_packet->buffer.append((char *) &cts + 1, 3);
        _rtmp_packet->chunk_id = CHUNK_VIDEO;
        _rtmp_packet->stream_index = STREAM_MEDIA;
        _rtmp_packet->time_stamp = frame->dts();
        _rtmp_packet->type_id = MSG_VIDEO;
    }
185 186 187 188 189 190 191

    _merger.inputFrame(frame, [&](uint32_t, uint32_t, const Buffer::Ptr &, bool) {
        //输出rtmp packet
        _rtmp_packet->body_size = _rtmp_packet->buffer.size();
        RtmpCodec::inputRtmp(_rtmp_packet);
        _rtmp_packet = nullptr;
    }, &_rtmp_packet->buffer);
xiongziliang committed
192 193 194 195 196 197 198
}

void H265RtmpEncoder::makeVideoConfigPkt() {
#ifdef ENABLE_MP4
    int8_t flags = FLV_CODEC_H265;
    flags |= (FLV_KEY_FRAME << 4);
    bool is_config = true;
xia-chu committed
199
    auto rtmpPkt = RtmpPacket::create();
xiongziliang committed
200
    //header
xiongziliang committed
201 202
    rtmpPkt->buffer.push_back(flags);
    rtmpPkt->buffer.push_back(!is_config);
xiongziliang committed
203
    //cts
xiongziliang committed
204
    rtmpPkt->buffer.append("\x0\x0\x0", 3);
xiongziliang committed
205 206 207 208 209

    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;
210
    h265_annexbtomp4(&hevc, vps_sps_pps.data(), (int)vps_sps_pps.size(), NULL, 0, NULL, NULL);
xiongziliang committed
211 212 213 214 215 216 217
    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
218 219 220 221 222 223
    rtmpPkt->buffer.append((char *)extra_data, extra_data_size);
    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;
224
    RtmpCodec::inputRtmp(rtmpPkt);
xiongziliang committed
225
#else
xiongziliang committed
226
    WarnL << "请开启MP4相关功能并使能\"ENABLE_MP4\",否则对H265-RTMP支持不完善";
xiongziliang committed
227 228 229 230
#endif
}

}//namespace mediakit