AACRtmp.cpp 3.87 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
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 "AACRtmp.h"
xiongziliang committed
12
#include "Rtmp/Rtmp.h"
13

xiongziliang committed
14
namespace mediakit{
15

xiongziliang committed
16 17 18 19 20 21 22 23 24 25 26 27
static string getAacCfg(const RtmpPacket &thiz) {
    string ret;
    if (thiz.getMediaType() != FLV_CODEC_AAC) {
        return ret;
    }
    if (!thiz.isCfgFrame()) {
        return ret;
    }
    if (thiz.strBuf.size() < 4) {
        WarnL << "bad aac cfg!";
        return ret;
    }
28
    ret = thiz.strBuf.substr(2);
xiongziliang committed
29 30 31
    return ret;
}

xiongziliang committed
32
bool AACRtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt, bool) {
33
    if (pkt->isCfgFrame()) {
xiongziliang committed
34
        _aac_cfg = getAacCfg(*pkt);
35 36
        onGetAAC(nullptr, 0, 0);
        return false;
37
    }
38

39 40 41 42 43 44
    if (!_aac_cfg.empty()) {
        onGetAAC(pkt->strBuf.data() + 2, pkt->strBuf.size() - 2, pkt->timeStamp);
    }
    return false;
}

xiongziliang committed
45
void AACRtmpDecoder::onGetAAC(const char* data, int len, uint32_t stamp) {
46 47 48 49 50 51 52 53 54 55 56 57
    auto frame = ResourcePoolHelper<AACFrame>::obtainObj();
    //生成adts头
    char adts_header[32] = {0};
    auto size = dumpAacConfig(_aac_cfg, len, (uint8_t *) adts_header, sizeof(adts_header));
    if (size > 0) {
        frame->_buffer.assign(adts_header, size);
        frame->_prefix_size = size;
    } else {
        frame->_buffer.clear();
        frame->_prefix_size = 0;
    }

xiongziliang committed
58
    if(len > 0){
59 60 61 62
        //追加负载数据
        frame->_buffer.append(data, len);
        frame->_dts = stamp;
    }
63

xiongziliang committed
64
    if(size > 0 || len > 0){
65 66 67
        //有adts头或者实际aac负载
        RtmpCodec::inputFrame(frame);
    }
68
}
xiongziliang committed
69

70 71
/////////////////////////////////////////////////////////////////////////////////////

72
AACRtmpEncoder::AACRtmpEncoder(const Track::Ptr &track) {
73 74 75
    _track = dynamic_pointer_cast<AACTrack>(track);
}

76 77 78 79 80 81 82 83 84 85 86
void AACRtmpEncoder::makeConfigPacket() {
    if (_track && _track->ready()) {
        //从track中和获取aac配置信息
        _aac_cfg = _track->getAacCfg();
    }

    if (!_aac_cfg.empty()) {
        makeAudioConfigPkt();
    }
}

87
void AACRtmpEncoder::inputFrame(const Frame::Ptr &frame) {
88
    if (_aac_cfg.empty()) {
89
        if (frame->prefixSize()) {
90
            //包含adts头,从adts头获取aac配置信息
91
            _aac_cfg = makeAacConfig((uint8_t *) (frame->data()), frame->prefixSize());
92
        }
93
        makeConfigPacket();
94
    }
xiongziliang committed
95 96 97

    if(!_aac_cfg.empty()){
        RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
98 99
        rtmpPkt->strBuf.clear();

xiongziliang committed
100
        //header
xiongziliang committed
101
        uint8_t is_config = false;
xiongziliang committed
102
        rtmpPkt->strBuf.push_back(_audio_flv_flags);
xiongziliang committed
103
        rtmpPkt->strBuf.push_back(!is_config);
xiongziliang committed
104 105

        //aac data
xiongziliang committed
106 107 108 109 110
        rtmpPkt->strBuf.append(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());

        rtmpPkt->bodySize = rtmpPkt->strBuf.size();
        rtmpPkt->chunkId = CHUNK_AUDIO;
        rtmpPkt->streamId = STREAM_MEDIA;
111
        rtmpPkt->timeStamp = frame->dts();
xiongziliang committed
112
        rtmpPkt->typeId = MSG_AUDIO;
113
        RtmpCodec::inputRtmp(rtmpPkt, false);
xiongziliang committed
114
    }
115 116 117
}

void AACRtmpEncoder::makeAudioConfigPkt() {
xiongziliang committed
118
    _audio_flv_flags = getAudioRtmpFlags(std::make_shared<AACTrack>(_aac_cfg));
119
    RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
120 121
    rtmpPkt->strBuf.clear();

xiongziliang committed
122
    //header
123
    uint8_t is_config = true;
xiongziliang committed
124
    rtmpPkt->strBuf.push_back(_audio_flv_flags);
125
    rtmpPkt->strBuf.push_back(!is_config);
xiongziliang committed
126
    //aac config
127 128 129 130 131 132 133
    rtmpPkt->strBuf.append(_aac_cfg);

    rtmpPkt->bodySize = rtmpPkt->strBuf.size();
    rtmpPkt->chunkId = CHUNK_AUDIO;
    rtmpPkt->streamId = STREAM_MEDIA;
    rtmpPkt->timeStamp = 0;
    rtmpPkt->typeId = MSG_AUDIO;
134
    RtmpCodec::inputRtmp(rtmpPkt, false);
135 136
}

xiongziliang committed
137
}//namespace mediakit