diff --git a/src/Extension/G711.h b/src/Extension/G711.h index d1644201..e4929e7b 100644 --- a/src/Extension/G711.h +++ b/src/Extension/G711.h @@ -64,6 +64,19 @@ class G711FrameNoCacheAble : public FrameNoCacheAble { public: typedef std::shared_ptr Ptr; + //兼容通用接口 + G711FrameNoCacheAble(char *ptr,uint32_t size,uint32_t dts, uint32_t pts = 0,int prefixeSize = 0){ + _ptr = ptr; + _size = size; + _dts = dts; + _prefixSize = prefixeSize; + } + + //兼容通用接口 + void setCodec(CodecId codecId){ + _codecId = codecId; + } + G711FrameNoCacheAble(CodecId codecId, char *ptr,uint32_t size,uint32_t dts,int prefixeSize = 0){ _codecId = codecId; _ptr = ptr; diff --git a/src/Record/MP4Demuxer.cpp b/src/Record/MP4Demuxer.cpp index 94394e63..f157f9a5 100644 --- a/src/Record/MP4Demuxer.cpp +++ b/src/Record/MP4Demuxer.cpp @@ -14,6 +14,7 @@ #include "Extension/H265.h" #include "Extension/H264.h" #include "Extension/AAC.h" +#include "Extension/G711.h" using namespace toolkit; namespace mediakit { @@ -120,6 +121,12 @@ void MP4Demuxer::onAudioTrack(uint32_t track_id, uint8_t object, int channel_cou _track_to_codec.emplace(track_id, audio); } break; + case MOV_OBJECT_G711a: + case MOV_OBJECT_G711u:{ + auto audio = std::make_shared(object == MOV_OBJECT_G711a ? CodecG711A : CodecG711U, sample_rate, channel_count, bit_per_sample / channel_count ); + _track_to_codec.emplace(track_id, audio); + } + break; default: WarnL << "不支持该编码类型的MP4,已忽略:" << getObjectName(object); break; @@ -223,8 +230,16 @@ Frame::Ptr MP4Demuxer::makeFrame(uint32_t track_id, const Buffer::Ptr &buf, int6 } return std::make_shared >(buf, pts, dts, 4); } + case CodecAAC : return std::make_shared >(buf, pts, dts, 0); + + case CodecG711A: + case CodecG711U: { + auto frame = std::make_shared >(buf, pts, dts, 0); + frame->setCodec(codec); + return frame; + } default: return nullptr; } diff --git a/src/Record/MP4Muxer.cpp b/src/Record/MP4Muxer.cpp index 2eb5436a..64cfbc4e 100644 --- a/src/Record/MP4Muxer.cpp +++ b/src/Record/MP4Muxer.cpp @@ -124,22 +124,47 @@ void MP4Muxer::inputFrame(const Frame::Ptr &frame) { void MP4Muxer::addTrack(const Track::Ptr &track) { switch (track->getCodecId()) { + case CodecG711A: + case CodecG711U: { + auto audio_track = dynamic_pointer_cast(track); + if (!audio_track) { + WarnL << "不是G711 Track"; + return; + } + if (!audio_track->ready()) { + WarnL << "G711 Track未就绪"; + return; + } + auto track_id = mov_writer_add_audio(_mov_writter.get(), + track->getCodecId() == CodecG711A ? MOV_OBJECT_G711a : MOV_OBJECT_G711u, + audio_track->getAudioChannel(), + audio_track->getAudioSampleBit() * audio_track->getAudioChannel(), + audio_track->getAudioSampleRate(), + nullptr, 0); + if (track_id < 0) { + WarnL << "添加G711 Track失败:" << track_id; + return; + } + _codec_to_trackid[track->getCodecId()].track_id = track_id; + } + break; + case CodecAAC: { - auto aac_track = dynamic_pointer_cast(track); - if (!aac_track) { + auto audio_track = dynamic_pointer_cast(track); + if (!audio_track) { WarnL << "不是AAC Track"; return; } - if(!aac_track->ready()){ + if(!audio_track->ready()){ WarnL << "AAC Track未就绪"; return; } auto track_id = mov_writer_add_audio(_mov_writter.get(), MOV_OBJECT_AAC, - aac_track->getAudioChannel(), - aac_track->getAudioSampleBit() * aac_track->getAudioChannel(), - aac_track->getAudioSampleRate(), - aac_track->getAacCfg().data(), 2); + audio_track->getAudioChannel(), + audio_track->getAudioSampleBit() * audio_track->getAudioChannel(), + audio_track->getAudioSampleRate(), + audio_track->getAacCfg().data(), 2); if(track_id < 0){ WarnL << "添加AAC Track失败:" << track_id; return; diff --git a/src/Record/MP4Muxer.h b/src/Record/MP4Muxer.h index 7975bf2f..e5fd4807 100644 --- a/src/Record/MP4Muxer.h +++ b/src/Record/MP4Muxer.h @@ -15,6 +15,7 @@ #include "Common/MediaSink.h" #include "Extension/AAC.h" +#include "Extension/G711.h" #include "Extension/H264.h" #include "Extension/H265.h" #include "Common/Stamp.h" diff --git a/src/Record/TsMuxer.cpp b/src/Record/TsMuxer.cpp index 4da7eab9..9730eeec 100644 --- a/src/Record/TsMuxer.cpp +++ b/src/Record/TsMuxer.cpp @@ -39,6 +39,14 @@ void TsMuxer::addTrack(const Track::Ptr &track) { _codec_to_trackid[track->getCodecId()].track_id = mpeg_ts_add_stream(_context, PSI_STREAM_AAC, nullptr, 0); } break; + + case CodecG711A: + case CodecG711U: { + //todo 此处未区分G711a和G711u + _codec_to_trackid[track->getCodecId()].track_id = mpeg_ts_add_stream(_context, PSI_STREAM_AUDIO_G711, nullptr, 0); + } + break; + default: break; } diff --git a/src/Rtp/RtpProcess.cpp b/src/Rtp/RtpProcess.cpp index 2efcb722..50c6e8d7 100644 --- a/src/Rtp/RtpProcess.cpp +++ b/src/Rtp/RtpProcess.cpp @@ -14,6 +14,7 @@ #include "Util/File.h" #include "Extension/H265.h" #include "Extension/AAC.h" +#include "Extension/G711.h" namespace mediakit{ @@ -271,6 +272,27 @@ void RtpProcess::onDecode(int stream,int codecid,int flags,int64_t pts,int64_t d _muxer->inputFrame(std::make_shared((char *) data, bytes, dts, 0, 7)); break; } + + case STREAM_AUDIO_G711: { + _dts = dts; + //todo 等待陈大佬更新ts/ps解析库,现在暂时固定为G711A + auto codec = CodecG711A; + if (!_codecid_audio) { + //获取到音频 + _codecid_audio = codecid; + InfoL << "got audio track: G711"; + //G711传统只支持 8000/1/16的规格,FFmpeg貌似做了扩展,但是这里不管它了 + auto track = std::make_shared(codec, 8000, 1, 16); + _muxer->addTrack(track); + } + + if (codecid != _codecid_audio) { + WarnL << "audio track change to G711 from codecid:" << getCodecName(_codecid_audio); + return; + } + _muxer->inputFrame(std::make_shared(codec, (char *) data, bytes, dts)); + break; + } default: if(codecid != 0){ WarnL << "unsupported codec type:" << getCodecName(codecid);