AACRtp.cpp 4.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 "AACRtp.h"
12

xiongziliang committed
13 14
namespace mediakit{

xiongziliang committed
15 16 17
AACRtpEncoder::AACRtpEncoder(uint32_t ui32Ssrc,
                             uint32_t ui32MtuSize,
                             uint32_t ui32SampleRate,
xiongziliang committed
18
                             uint8_t ui8PayloadType,
xiongziliang committed
19
                             uint8_t ui8Interleaved) :
20 21 22
        RtpInfo(ui32Ssrc,
                ui32MtuSize,
                ui32SampleRate,
xiongziliang committed
23
                ui8PayloadType,
24
                ui8Interleaved){
xiongziliang committed
25 26
}

27
void AACRtpEncoder::inputFrame(const Frame::Ptr &frame) {
xiongziliang committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41
    auto stamp = frame->dts();
    auto data = frame->data() + frame->prefixSize();
    auto len = frame->size() - frame->prefixSize();
    auto ptr = (char *) data;
    auto remain_size = len;
    auto max_size = getMaxSize() - 4;
    while (remain_size > 0) {
        if (remain_size <= max_size) {
            _section_buf[0] = 0;
            _section_buf[1] = 16;
            _section_buf[2] = (len >> 5) & 0xFF;
            _section_buf[3] = ((len & 0x1F) << 3) & 0xFF;
            memcpy(_section_buf + 4, ptr, remain_size);
            makeAACRtp(_section_buf, remain_size + 4, true, stamp);
42 43
            break;
        }
xiongziliang committed
44 45 46 47 48 49 50 51
        _section_buf[0] = 0;
        _section_buf[1] = 16;
        _section_buf[2] = ((len) >> 5) & 0xFF;
        _section_buf[3] = ((len & 0x1F) << 3) & 0xFF;
        memcpy(_section_buf + 4, ptr, max_size);
        makeAACRtp(_section_buf, max_size + 4, false, stamp);
        ptr += max_size;
        remain_size -= max_size;
52 53 54
    }
}

55
void AACRtpEncoder::makeAACRtp(const void *data, size_t len, bool mark, uint32_t uiStamp) {
56
    RtpCodec::inputRtp(makeRtp(getTrackType(), data, len, mark, uiStamp), false);
57 58 59
}

/////////////////////////////////////////////////////////////////////////////////////
60

61
AACRtpDecoder::AACRtpDecoder(const Track::Ptr &track) {
62
    auto aacTrack = dynamic_pointer_cast<AACTrack>(track);
63
    if (!aacTrack || !aacTrack->ready()) {
64
        WarnL << "该aac track无效!";
65
    } else {
66 67
        _aac_cfg = aacTrack->getAacCfg();
    }
68
    obtainFrame();
69
}
70

71
AACRtpDecoder::AACRtpDecoder() {
72
    obtainFrame();
73 74
}

75
void AACRtpDecoder::obtainFrame() {
76
    //从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
77
    _frame = FrameImp::create();
78
    _frame->_codec_id = CodecAAC;
79
}
80

xiongziliang committed
81 82
bool AACRtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
    auto stamp = rtp->getStampMS();
83
    //rtp数据开始部分
xiongziliang committed
84
    auto ptr = rtp->getPayload();
85
    //rtp数据末尾
xiongziliang committed
86
    auto end = ptr + rtp->getPayloadSize();
87
    //首2字节表示Au-Header的个数,单位bit,所以除以16得到Au-Header个数
xiongziliang committed
88
    auto au_header_count = ((ptr[0] << 8) | ptr[1]) >> 4;
89
    //记录au_header起始指针
xiongziliang committed
90
    auto au_header_ptr = ptr + 2;
91 92 93 94 95
    ptr = au_header_ptr +  au_header_count * 2;

    if (end < ptr) {
        //数据不够
        return false;
96 97
    }

xiongziliang committed
98 99
    if (!_last_dts) {
        //记录第一个时间戳
xiongziliang committed
100
        _last_dts = stamp;
xiongziliang committed
101 102
    }

103
    //每个audio unit时间戳增量
xiongziliang committed
104
    auto dts_inc = (stamp - _last_dts) / au_header_count;
xiongziliang committed
105 106
    if (dts_inc < 0 && dts_inc > 100) {
        //时间戳增量异常,忽略
107 108 109 110 111 112 113 114 115 116
        dts_inc = 0;
    }

    for (int i = 0; i < au_header_count; ++i) {
        // 之后的2字节是AU_HEADER,其中高13位表示一帧AAC负载的字节长度,低3位无用
        uint16_t size = ((au_header_ptr[0] << 8) | au_header_ptr[1]) >> 3;
        if (ptr + size > end) {
            //数据不够
            break;
        }
xiongziliang committed
117 118 119 120 121 122 123 124 125 126

        if (size) {
            //设置aac数据
            _frame->_buffer.assign((char *) ptr, size);
            //设置当前audio unit时间戳
            _frame->_dts = _last_dts + i * dts_inc;
            ptr += size;
            au_header_ptr += 2;
            flushData();
        }
127
    }
128
    //记录上次时间戳
xiongziliang committed
129
    _last_dts = stamp;
xiongziliang committed
130
    return false;
131 132
}

133
void AACRtpDecoder::flushData() {
134 135 136 137 138 139 140 141 142
    //插入adts头
    char adts_header[32] = {0};
    auto size = dumpAacConfig(_aac_cfg, _frame->_buffer.size(), (uint8_t *) adts_header, sizeof(adts_header));
    if (size > 0) {
        //插入adts头
        _frame->_buffer.insert(0, adts_header, size);
        _frame->_prefix_size = size;
    }
    RtpCodec::inputFrame(_frame);
143
    obtainFrame();
144
}
xiongziliang committed
145

146
}//namespace mediakit