修复adts符合包相关bug

This commit is contained in:
xiongziliang 2020-08-08 12:19:04 +08:00
parent 873f8bc5ac
commit b3f048f194
3 changed files with 47 additions and 3 deletions

View File

@ -92,6 +92,16 @@ static void parseAacConfig(const string &config, AdtsHeader &adts) {
adts.no_raw_data_blocks_in_frame = 0;
}
int getAacFrameLength(const uint8_t *data, int bytes) {
uint16_t len;
if (bytes < 7) return -1;
if (0xFF != data[0] || 0xF0 != (data[1] & 0xF0)) {
return -1;
}
len = ((uint16_t) (data[3] & 0x03) << 11) | ((uint16_t) data[4] << 3) | ((uint16_t) (data[5] >> 5) & 0x07);
return len;
}
string makeAacConfig(const uint8_t *hex, int length){
#ifndef ENABLE_MP4
if (!(hex[0] == 0xFF && (hex[1] & 0xF0) == 0xF0)) {

View File

@ -18,6 +18,7 @@
namespace mediakit{
string makeAacConfig(const uint8_t *hex, int length);
int getAacFrameLength(const uint8_t *hex, int length);
int dumpAacConfig(const string &config, int length, uint8_t *out, int out_size);
bool parseAacConfig(const string &config, int &samplerate, int &channels);
@ -100,6 +101,28 @@ public:
* @param frame
*/
void inputFrame(const Frame::Ptr &frame) override{
if (frame->prefixSize()) {
//有adts头尝试分帧
auto ptr = frame->data();
auto end = frame->data() + frame->size();
while (ptr < end) {
auto frame_len = getAacFrameLength((uint8_t *) ptr, end - ptr);
if (frame_len < ADTS_HEADER_LEN) {
break;
}
auto sub_frame = std::make_shared<FrameInternal<FrameFromPtr> >(frame, (char *) ptr, frame_len, ADTS_HEADER_LEN);
ptr += frame_len;
sub_frame->setCodecId(CodecAAC);
inputFrame_l(sub_frame);
}
} else {
inputFrame_l(frame);
}
}
private:
void inputFrame_l(const Frame::Ptr &frame) {
if (_cfg.empty()) {
//未获取到aac_cfg信息
if (frame->prefixSize()) {
@ -116,7 +139,6 @@ public:
AudioTrack::inputFrame(frame);
}
}
private:
/**
* 2aac配置
*/

View File

@ -314,8 +314,13 @@ private:
class FrameFromPtr : public Frame{
public:
typedef std::shared_ptr<FrameFromPtr> Ptr;
FrameFromPtr(CodecId codec_id, char *ptr, uint32_t size, uint32_t dts, uint32_t pts = 0, int prefix_size = 0){
FrameFromPtr(CodecId codec_id, char *ptr, uint32_t size, uint32_t dts, uint32_t pts = 0, int prefix_size = 0)
: FrameFromPtr(ptr, size, dts, pts, prefix_size) {
_codec_id = codec_id;
}
FrameFromPtr(char *ptr, uint32_t size, uint32_t dts, uint32_t pts = 0, int prefix_size = 0){
_ptr = ptr;
_size = size;
_dts = dts;
@ -347,10 +352,17 @@ public:
return false;
}
CodecId getCodecId() const override{
CodecId getCodecId() const override {
if (_codec_id == CodecInvalid) {
throw std::invalid_argument("FrameFromPtr对象未设置codec类型");
}
return _codec_id;
}
void setCodecId(CodecId codec_id) {
_codec_id = codec_id;
}
bool keyFrame() const override {
return false;
}