#include "audio_encoder.h" bool Encoder::Open(const Param& audioParma, AVFormatContext* fmtCtx) { Close(); _isOpen = false; __CheckBool(_Init(audioParma, fmtCtx)); __CheckBool(avcodec_open2(_codecCtx, _codec, nullptr) >= 0); _isOpen = true; return true; } void Encoder::Close() { if (_codecCtx != nullptr) { avcodec_free_context(&_codecCtx); } Free(_codecCtx, [this] { avcodec_free_context(&_codecCtx); }); } bool Encoder::_Init(const Param& audioParam, AVFormatContext* fmtCtx) { // codec __CheckBool(_codec = avcodec_find_encoder(AV_CODEC_ID_AAC)); // codeccontext __CheckBool(_codecCtx = avcodec_alloc_context3(_codec)); _codecCtx->sample_fmt = AV_SAMPLE_FMT_FLTP; _codecCtx->bit_rate = audioParam.bitRate; _codecCtx->sample_rate = AUDIO_SAMPLE_RATE; AVChannelLayout layout; layout.order = AV_CHANNEL_ORDER_NATIVE; layout.nb_channels = 1; layout.u.mask = AV_CH_LAYOUT_MONO; av_channel_layout_copy(&_codecCtx->ch_layout, &layout); if (fmtCtx->oformat->flags & AVFMT_GLOBALHEADER) { _codecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; } return true; } bool Encoder::PushFrame(AVFrame* frame, bool isEnd, uint64_t pts) { if (!isEnd) { __CheckBool(frame); } else { frame = nullptr; } if (frame != nullptr) { frame->pts = pts; } __CheckBool(avcodec_send_frame(_codecCtx, frame) >= 0); return true; }