AACRtmp.cpp 5.2 KB
Newer Older
1 2 3
/*
 * MIT License
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
xiongziliang committed
26
#include "AACRtmp.h"
27

xiongziliang committed
28
namespace mediakit{
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

AACRtmpDecoder::AACRtmpDecoder() {
    _adts = obtainFrame();
}

AACFrame::Ptr AACRtmpDecoder::obtainFrame() {
    //从缓存池重新申请对象,防止覆盖已经写入环形缓存的对象
    auto frame = ResourcePoolHelper<AACFrame>::obtainObj();
    frame->aac_frame_length = 7;
    frame->iPrefixSize = 7;
    return frame;
}

bool AACRtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt, bool key_pos) {
    RtmpCodec::inputRtmp(pkt, false);

    if (pkt->isCfgFrame()) {
        _aac_cfg = pkt->getAacCfg();
        return false;
    }
    if (!_aac_cfg.empty()) {
        onGetAAC(pkt->strBuf.data() + 2, pkt->strBuf.size() - 2, pkt->timeStamp);
    }
    return false;
}

void AACRtmpDecoder::onGetAAC(const char* pcData, int iLen, uint32_t ui32TimeStamp) {
    if(iLen + 7 > sizeof(_adts->buffer)){
        WarnL << "Illegal adts data, exceeding the length limit.";
        return;
    }
    //写adts结构头
    makeAdtsHeader(_aac_cfg,*_adts);

    //拷贝aac负载
    memcpy(_adts->buffer + 7, pcData, iLen);
    _adts->aac_frame_length = 7 + iLen;
    _adts->timeStamp = ui32TimeStamp;

    //adts结构头转成头7个字节
    writeAdtsHeader(*_adts, _adts->buffer);

    //写入环形缓存
    RtmpCodec::inputFrame(_adts);
    _adts = obtainFrame();
}
/////////////////////////////////////////////////////////////////////////////////////

77 78 79 80
AACRtmpEncoder::AACRtmpEncoder(const Track::Ptr &track) {
    _track = dynamic_pointer_cast<AACTrack>(track);
}

81 82
void AACRtmpEncoder::inputFrame(const Frame::Ptr &frame) {
    RtmpCodec::inputFrame(frame);
83 84 85 86 87 88 89 90 91 92 93

    if(_aac_cfg.empty()){
        if(frame->prefixSize() >= 7){
            //包含adts头,从adts头获取aac配置信息
            _aac_cfg = makeAdtsConfig(reinterpret_cast<const uint8_t *>(frame->data()));
            makeAudioConfigPkt();
        } else if(_track && _track->ready()){
            //从track中和获取aac配置信息
            _aac_cfg = _track->getAacCfg();
            makeAudioConfigPkt();
        }
94
    }
xiongziliang committed
95 96 97

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

xiongziliang committed
100 101
        //////////header
        uint8_t is_config = false;
102
        rtmpPkt->strBuf.push_back(_ui8AudioFlags);
xiongziliang committed
103 104 105 106 107 108 109 110
        rtmpPkt->strBuf.push_back(!is_config);
        rtmpPkt->strBuf.append(frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize());

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

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
}

void AACRtmpEncoder::makeAudioConfigPkt() {
    makeAdtsHeader(_aac_cfg,*_adts);
    int iSampleRate , iChannel , iSampleBit = 16;
    getAACInfo(*_adts,iSampleRate,iChannel);

    uint8_t flvStereoOrMono = (iChannel > 1);
    uint8_t flvSampleRate;
    switch (iSampleRate) {
        case 48000:
        case 44100:
            flvSampleRate = 3;
            break;
        case 24000:
        case 22050:
            flvSampleRate = 2;
            break;
        case 12000:
        case 11025:
            flvSampleRate = 1;
            break;
        default:
            flvSampleRate = 0;
            break;
    }
    uint8_t flvSampleBit = iSampleBit == 16;
    uint8_t flvAudioType = 10; //aac

143
    _ui8AudioFlags = (flvAudioType << 4) | (flvSampleRate << 2) | (flvSampleBit << 1) | flvStereoOrMono;
144 145

    RtmpPacket::Ptr rtmpPkt = ResourcePoolHelper<RtmpPacket>::obtainObj();
146 147
    rtmpPkt->strBuf.clear();

148 149
    //////////header
    uint8_t is_config = true;
150
    rtmpPkt->strBuf.push_back(_ui8AudioFlags);
151 152 153 154 155 156 157 158
    rtmpPkt->strBuf.push_back(!is_config);
    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;
159
    RtmpCodec::inputRtmp(rtmpPkt, false);
160 161
}

xiongziliang committed
162
}//namespace mediakit