H264Rtp.cpp 9.54 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
 */
xiongziliang committed
10

xiongziliang committed
11
#include "H264Rtp.h"
xiongziliang committed
12

xiongziliang committed
13
namespace mediakit{
14

xia-chu committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#if defined(_WIN32)
#pragma pack(push, 1)
#endif // defined(_WIN32)

class FuFlags {
public:
#if __BYTE_ORDER == __BIG_ENDIAN
    unsigned start_bit: 1;
    unsigned end_bit: 1;
    unsigned reserved: 1;
    unsigned nal_type: 5;
#else
    unsigned nal_type: 5;
    unsigned reserved: 1;
    unsigned end_bit: 1;
    unsigned start_bit: 1;
#endif
} PACKED;

#if defined(_WIN32)
#pragma pack(pop)
#endif // defined(_WIN32)
xiongziliang committed
37

38
H264RtpDecoder::H264RtpDecoder() {
xia-chu committed
39
    _frame = obtainFrame();
40 41
}

42 43
H264Frame::Ptr H264RtpDecoder::obtainFrame() {
    auto frame = FrameImp::create<H264Frame>();
44
    frame->_prefix_size = 4;
45 46 47
    return frame;
}

xiongziliang committed
48
bool H264RtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
49 50 51 52 53 54 55 56
    auto seq = rtp->getSeq();
    auto ret = decodeRtp(rtp);
    if (!_gop_dropped && seq != (uint16_t) (_last_seq + 1) && _last_seq) {
        _gop_dropped = true;
        WarnL << "start drop h264 gop, last seq:" << _last_seq << ", rtp:\r\n" << rtp->dumpString();
    }
    _last_seq = seq;
    return ret;
xiongziliang committed
57 58
}

xia-chu committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
/*
RTF3984 5.2节  Common Structure of the RTP Payload Format
Table 1.  Summary of NAL unit types and their payload structures

   Type   Packet    Type name                        Section
   ---------------------------------------------------------
   0      undefined                                    -
   1-23   NAL unit  Single NAL unit packet per H.264   5.6
   24     STAP-A    Single-time aggregation packet     5.7.1
   25     STAP-B    Single-time aggregation packet     5.7.1
   26     MTAP16    Multi-time aggregation packet      5.7.2
   27     MTAP24    Multi-time aggregation packet      5.7.2
   28     FU-A      Fragmentation unit                 5.8
   29     FU-B      Fragmentation unit                 5.8
   30-31  undefined                                    -
*/

76 77 78 79 80 81 82 83
bool H264RtpDecoder::singleFrame(const RtpPacket::Ptr &rtp, const uint8_t *ptr, ssize_t size, uint32_t stamp){
    _frame->_buffer.assign("\x00\x00\x00\x01", 4);
    _frame->_buffer.append((char *) ptr, size);
    _frame->_pts = stamp;
    auto key = _frame->keyFrame();
    outputFrame(rtp, _frame);
    return key;
}
xiongziliang committed
84

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
bool H264RtpDecoder::unpackStapA(const RtpPacket::Ptr &rtp, const uint8_t *ptr, ssize_t size, uint32_t stamp) {
    //STAP-A 单一时间的组合包
    auto have_key_frame = false;
    auto end = ptr + size;
    while (ptr + 2 < end) {
        uint16_t len = (ptr[0] << 8) | ptr[1];
        if (!len || ptr + len > end) {
            WarnL << "invalid rtp data size:" << len << ",rtp:\r\n" << rtp->dumpString();
            _gop_dropped = true;
            break;
        }
        ptr += 2;
        if (singleFrame(rtp, ptr, len, stamp)) {
            have_key_frame = true;
        }
        ptr += len;
    }
    return have_key_frame;
}

bool H264RtpDecoder::mergeFu(const RtpPacket::Ptr &rtp, const uint8_t *ptr, ssize_t size, uint32_t stamp, uint16_t seq){
    auto nal_suffix = *ptr & (~0x1F);
    FuFlags *fu = (FuFlags *) (ptr + 1);
    if (fu->start_bit) {
        //该帧的第一个rtp包
xia-chu committed
110
        _frame->_buffer.assign("\x00\x00\x00\x01", 4);
111
        _frame->_buffer.push_back(nal_suffix | fu->nal_type);
xia-chu committed
112
        _frame->_pts = stamp;
113
        _fu_dropped = false;
xiongziliang committed
114 115
    }

116 117 118 119
    if (_fu_dropped) {
        //该帧不完整
        return false;
    }
xiongziliang committed
120

121 122 123 124 125 126
    if (!fu->start_bit && seq != (uint16_t) (_last_seq + 1)) {
        //中间的或末尾的rtp包,其seq必须连续,否则说明rtp丢包,那么该帧不完整,必须得丢弃
        _fu_dropped = true;
        _frame->_buffer.clear();
        return false;
    }
127

128 129
    //后面追加数据
    _frame->_buffer.append((char *) ptr + 2, size - 2);
130

131 132 133 134
    if (!fu->end_bit) {
        //非末尾包
        return fu->start_bit ? _frame->keyFrame() : false;
    }
135

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    //确保下一次fu必须收到第一个包
    _fu_dropped = true;
    //该帧最后一个rtp包,输出frame
    outputFrame(rtp, _frame);
    return false;
}

bool H264RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtp) {
    auto frame = rtp->getPayload();
    auto length = rtp->getPayloadSize();
    auto stamp = rtp->getStampMS();
    auto seq = rtp->getSeq();
    int nal = H264_TYPE(frame[0]);

    switch (nal) {
        case 24:
            // 24 STAP-A Single-time aggregation packet 5.7.1
            return unpackStapA(rtp, frame + 1, length - 1, stamp);

        case 28:
            // 28 FU-A Fragmentation unit
            return mergeFu(rtp, frame, length, stamp, seq);
158

159
        default: {
160 161 162 163 164 165
            if (nal < 24) {
                //Single NAL Unit Packets
                return singleFrame(rtp, frame, length, stamp);
            }
            _gop_dropped = true;
            WarnL << "不支持该类型的264 RTP包, nal type:" << nal << ", rtp:\r\n" << rtp->dumpString();
166
            return false;
xiongziliang committed
167 168 169 170
        }
    }
}

171
void H264RtpDecoder::outputFrame(const RtpPacket::Ptr &rtp, const H264Frame::Ptr &frame) {
172 173 174 175 176 177 178
    if (frame->dropAble()) {
        //不参与dts生成
        frame->_dts = frame->_pts;
    } else {
        //rtsp没有dts,那么根据pts排序算法生成dts
        _dts_generator.getDts(frame->_pts, frame->_dts);
    }
179 180 181 182 183 184 185 186

    if (frame->keyFrame() && _gop_dropped) {
        _gop_dropped = false;
        InfoL << "new gop received, rtp:\r\n" << rtp->dumpString();
    }
    if (!_gop_dropped) {
        RtpCodec::inputFrame(frame);
    }
xia-chu committed
187
    _frame = obtainFrame();
xiongziliang committed
188
}
xiongziliang committed
189 190 191

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

xia-chu committed
192 193
H264RtpEncoder::H264RtpEncoder(uint32_t ssrc, uint32_t mtu, uint32_t sample_rate, uint8_t pt, uint8_t interleaved)
        : RtpInfo(ssrc, mtu, sample_rate, pt, interleaved) {
xiongziliang committed
194 195
}

xia-chu committed
196
void H264RtpEncoder::insertConfigFrame(uint32_t pts){
xia-chu committed
197
    if (!_sps || !_pps) {
198 199
        return;
    }
xia-chu committed
200
    //gop缓存从sps开始,sps、pps后面还有时间戳相同的关键帧,所以mark bit为false
xia-chu committed
201 202
    packRtp(_sps->data() + _sps->prefixSize(), _sps->size() - _sps->prefixSize(), pts, false, true);
    packRtp(_pps->data() + _pps->prefixSize(), _pps->size() - _pps->prefixSize(), pts, false, false);
xia-chu committed
203
}
204

xia-chu committed
205 206 207 208 209 210 211
void H264RtpEncoder::packRtp(const char *ptr, size_t len, uint32_t pts, bool is_mark, bool gop_pos){
    if (len + 3 <= getMaxSize()) {
        //STAP-A模式打包小于MTU
        packRtpStapA(ptr, len, pts, is_mark, gop_pos);
    } else {
        //STAP-A模式打包会大于MTU,所以采用FU-A模式
        packRtpFu(ptr, len, pts, is_mark, gop_pos);
212
    }
xia-chu committed
213
}
214

xia-chu committed
215
void H264RtpEncoder::packRtpFu(const char *ptr, size_t len, uint32_t pts, bool is_mark, bool gop_pos){
216 217
    auto packet_size = getMaxSize() - 2;
    if (len <= packet_size + 1) {
218
        //小于FU-A打包最小字节长度要求,采用STAP-A模式
xia-chu committed
219
        packRtpStapA(ptr, len, pts, is_mark, gop_pos);
220 221
        return;
    }
xia-chu committed
222 223 224

    //末尾5bit为nalu type,固定为28(FU-A)
    auto fu_char_0 = (ptr[0] & (~0x1F)) | 28;
xia-chu committed
225
    auto fu_char_1 = H264_TYPE(ptr[0]);
xia-chu committed
226 227
    FuFlags *fu_flags = (FuFlags *) (&fu_char_1);
    fu_flags->start_bit = 1;
xiongziliang committed
228

xia-chu committed
229 230 231 232 233 234
    size_t offset = 1;
    while (!fu_flags->end_bit) {
        if (!fu_flags->start_bit && len <= offset + packet_size) {
            //FU-A end
            packet_size = len - offset;
            fu_flags->end_bit = 1;
xiongziliang committed
235
        }
xia-chu committed
236 237 238 239

        //传入nullptr先不做payload的内存拷贝
        auto rtp = makeRtp(getTrackType(), nullptr, packet_size + 2, fu_flags->end_bit && is_mark, pts);
        //rtp payload 负载部分
240
        uint8_t *payload = rtp->getPayload();
xia-chu committed
241 242 243 244 245 246 247 248 249 250 251
        //FU-A 第1个字节
        payload[0] = fu_char_0;
        //FU-A 第2个字节
        payload[1] = fu_char_1;
        //H264 数据
        memcpy(payload + 2, (uint8_t *) ptr + offset, packet_size);
        //输入到rtp环形缓存
        RtpCodec::inputRtp(rtp, gop_pos);

        offset += packet_size;
        fu_flags->start_bit = 0;
xiongziliang committed
252
    }
xia-chu committed
253
}
254

xia-chu committed
255 256 257 258 259 260 261 262 263 264 265
void H264RtpEncoder::packRtpStapA(const char *ptr, size_t len, uint32_t pts, bool is_mark, bool gop_pos){
    //如果帧长度不超过mtu,为了兼容性 webrtc,采用STAP-A模式打包
    auto rtp = makeRtp(getTrackType(), nullptr, len + 3, is_mark, pts);
    uint8_t *payload = rtp->getPayload();
    //STAP-A
    payload[0] = (ptr[0] & (~0x1F)) | 24;
    payload[1] = (len >> 8) & 0xFF;
    payload[2] = len & 0xff;
    memcpy(payload + 3, (uint8_t *) ptr, len);

    RtpCodec::inputRtp(rtp, gop_pos);
xiongziliang committed
266 267
}

268
bool H264RtpEncoder::inputFrame(const Frame::Ptr &frame) {
xia-chu committed
269 270 271
    auto ptr = frame->data() + frame->prefixSize();
    switch (H264_TYPE(ptr[0])) {
        case H264Frame::NAL_SPS: {
xia-chu committed
272
            _sps = Frame::getCacheAbleFrame(frame);
273
            return true;
xia-chu committed
274 275
        }
        case H264Frame::NAL_PPS: {
xia-chu committed
276
            _pps = Frame::getCacheAbleFrame(frame);
277
            return true;
xia-chu committed
278 279 280 281 282 283 284 285 286
        }
        default: break;
    }

    if (_last_frame) {
        //如果时间戳发生了变化,那么markbit才置true
        inputFrame_l(_last_frame, _last_frame->pts() != frame->pts());
    }
    _last_frame = Frame::getCacheAbleFrame(frame);
287
    return true;
xia-chu committed
288 289
}

290
bool H264RtpEncoder::inputFrame_l(const Frame::Ptr &frame, bool is_mark){
xia-chu committed
291 292 293 294 295
    if (frame->keyFrame()) {
        //保证每一个关键帧前都有SPS与PPS
        insertConfigFrame(frame->pts());
    }
    packRtp(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize(), frame->pts(), is_mark, false);
296
    return true;
xiongziliang committed
297 298
}

299
}//namespace mediakit