AACRtmp.cpp 3.72 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 "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
static string getAacCfg(const RtmpPacket &thiz) {
    string ret;
    if (thiz.getMediaType() != FLV_CODEC_AAC) {
        return ret;
    }
    if (!thiz.isCfgFrame()) {
        return ret;
    }
xiongziliang committed
24
    if (thiz.buffer.size() < 4) {
xiongziliang committed
25 26 27
        WarnL << "bad aac cfg!";
        return ret;
    }
xiongziliang committed
28
    ret = thiz.buffer.substr(2);
xiongziliang committed
29 30 31
    return ret;
}

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

39
    if (!_aac_cfg.empty()) {
xiongziliang committed
40
        onGetAAC(pkt->buffer.data() + 2, pkt->buffer.size() - 2, pkt->time_stamp);
41 42 43
    }
}

44
void AACRtmpDecoder::onGetAAC(const char* data, size_t len, uint32_t stamp) {
45
    auto frame = FrameImp::create();
46 47
    frame->_codec_id = CodecAAC;

48 49 50 51 52 53 54 55 56 57 58
    //生成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
59
    if(len > 0){
60 61 62 63
        //追加负载数据
        frame->_buffer.append(data, len);
        frame->_dts = stamp;
    }
64

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

71 72
/////////////////////////////////////////////////////////////////////////////////////

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

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

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

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

    if(!_aac_cfg.empty()){
xia-chu committed
98
        auto rtmpPkt = RtmpPacket::create();
xiongziliang committed
99
        //header
xiongziliang committed
100
        uint8_t is_config = false;
xiongziliang committed
101 102
        rtmpPkt->buffer.push_back(_audio_flv_flags);
        rtmpPkt->buffer.push_back(!is_config);
xiongziliang committed
103 104

        //aac data
xiongziliang committed
105
        rtmpPkt->buffer.append(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());
xiongziliang committed
106

xiongziliang committed
107 108 109 110 111
        rtmpPkt->body_size = rtmpPkt->buffer.size();
        rtmpPkt->chunk_id = CHUNK_AUDIO;
        rtmpPkt->stream_index = STREAM_MEDIA;
        rtmpPkt->time_stamp = frame->dts();
        rtmpPkt->type_id = MSG_AUDIO;
112
        RtmpCodec::inputRtmp(rtmpPkt);
xiongziliang committed
113
    }
114 115 116
}

void AACRtmpEncoder::makeAudioConfigPkt() {
xiongziliang committed
117
    _audio_flv_flags = getAudioRtmpFlags(std::make_shared<AACTrack>(_aac_cfg));
xia-chu committed
118
    auto rtmpPkt = RtmpPacket::create();
119

xiongziliang committed
120
    //header
121
    uint8_t is_config = true;
xiongziliang committed
122 123
    rtmpPkt->buffer.push_back(_audio_flv_flags);
    rtmpPkt->buffer.push_back(!is_config);
xiongziliang committed
124
    //aac config
xiongziliang committed
125
    rtmpPkt->buffer.append(_aac_cfg);
126

xiongziliang committed
127 128 129 130 131
    rtmpPkt->body_size = rtmpPkt->buffer.size();
    rtmpPkt->chunk_id = CHUNK_AUDIO;
    rtmpPkt->stream_index = STREAM_MEDIA;
    rtmpPkt->time_stamp = 0;
    rtmpPkt->type_id = MSG_AUDIO;
132
    RtmpCodec::inputRtmp(rtmpPkt);
133 134
}

xiongziliang committed
135
}//namespace mediakit