diff --git a/src/Codec/Transcode.cpp b/src/Codec/Transcode.cpp index 049a1fde..f753803c 100644 --- a/src/Codec/Transcode.cpp +++ b/src/Codec/Transcode.cpp @@ -18,17 +18,16 @@ #include #include "Transcode.h" #include "Common/config.h" -#include "Extension/Opus.h" -#include "Extension/G711.h" -#include "Extension/H264.h" -#include "Extension/H265.h" +#include "Extension/Factory.h" +#define ADTS_HEADER_LEN 7 #define MAX_DELAY_SECOND 3 using namespace std; using namespace toolkit; namespace mediakit { +extern int dumpAacConfig(const string &config, size_t length, uint8_t *out, size_t out_size); static string ffmpeg_err(int errnum) { char errbuf[AV_ERROR_MAX_STRING_SIZE]; @@ -1045,64 +1044,23 @@ void FFmpegEncoder::onEncode(AVPacket *packet) { // process frame if (!_cb) return; - switch (_codecId) { - case CodecH264: { - auto frame = FrameImp::create(); - frame->_dts = packet->dts; - frame->_pts = packet->pts; - frame->_buffer.assign((const char *)packet->data, packet->size); - frame->_prefix_size = prefixSize((const char *)packet->data, packet->size); - _cb(frame); - break; + if (_codecId == CodecAAC) { + auto frame = FrameImp::create<>(); + frame->_codec_id = _codecId; + frame->_dts = packet->dts; + frame->_pts = packet->pts; + frame->_buffer.reserve(ADTS_HEADER_LEN + packet->size); + if (_context && _context->extradata && _context->extradata_size) { + uint8_t adts[ADTS_HEADER_LEN]; + auto cfg = std::string((const char *)_context->extradata, _context->extradata_size); + dumpAacConfig(cfg, packet->size, adts, ADTS_HEADER_LEN); + frame->_prefix_size = ADTS_HEADER_LEN; + frame->_buffer.append((char*)adts, ADTS_HEADER_LEN); } - case CodecH265: { - auto frame = FrameImp::create(); - frame->_dts = packet->dts; - frame->_pts = packet->pts; - frame->_buffer.assign((const char *)packet->data, packet->size); - frame->_prefix_size = prefixSize((const char *)packet->data, packet->size); - _cb(frame); - break; - } - case CodecAAC: { - auto frame = FrameImp::create<>(); - frame->_codec_id = _codecId; - frame->_dts = packet->dts; - frame->_pts = packet->pts; - frame->_buffer.reserve(ADTS_HEADER_LEN + packet->size); - if (_context && _context->extradata && _context->extradata_size) { - uint8_t adts[ADTS_HEADER_LEN]; - auto cfg = std::string((const char *)_context->extradata, _context->extradata_size); - dumpAacConfig(cfg, packet->size, adts, ADTS_HEADER_LEN); - frame->_prefix_size = ADTS_HEADER_LEN; - frame->_buffer.append((char*)adts, ADTS_HEADER_LEN); - } - frame->_buffer.append((const char *)packet->data, packet->size); - _cb(frame); - break; - } - case CodecOpus: - case CodecG711A: - case CodecG711U: { - auto frame = FrameImp::create<>(); - frame->_codec_id = _codecId; - frame->_dts = packet->dts; - frame->_pts = packet->pts; - frame->_buffer.assign((const char *)packet->data, packet->size); - _cb(frame); - break; - } - case CodecVP8: - case CodecVP9: { - auto frame = FrameImp::create<>(); - frame->_codec_id = _codecId; - frame->_dts = packet->dts; - frame->_pts = packet->pts; - frame->_buffer.assign((const char *)packet->data, packet->size); - _cb(frame); - break; - } - default: break; + frame->_buffer.append((const char *)packet->data, packet->size); + _cb(frame); + } else { + _cb(Factory::getFrameFromPtr(_codecId, (const char*)packet->data, packet->size, packet->dts, packet->pts)); } } diff --git a/tests/test_audio_transcode.cpp b/tests/test_audio_transcode.cpp index 3b7fe87d..5cd1b478 100644 --- a/tests/test_audio_transcode.cpp +++ b/tests/test_audio_transcode.cpp @@ -1,34 +1,35 @@ #include "Codec/Transcode.h" #include "Record/MP4Demuxer.h" #include "Record/MP4Muxer.h" -#include "Extension/AAC.h" -#include "Extension/Opus.h" -#include "Extension/G711.h" #include "Util/logger.h" + using namespace mediakit; struct TransCtx { using Ptr = std::shared_ptr; TransCtx(const char *prefix, CodecId codec) { char path[256]; - Track::Ptr track; + int samplerate; + int channel = 1; + int bits = 16; switch (codec) { case CodecAAC: - track.reset(new AACTrack(44100, 1)); + samplerate = 44100; sprintf(path, "%s_aac.mp4", prefix); break; case CodecOpus: - track.reset(new OpusTrack()); + samplerate = 48000; sprintf(path, "%s_opus.mp4", prefix); break; case CodecG711A: case CodecG711U: - track.reset(new G711Track(codec, 8000, 1, 16)); + samplerate = 8000; sprintf(path, "%s_711%c.mp4", prefix, codec == CodecG711A ? 'A' : 'U'); break; default: return; break; } + Track::Ptr track = std::make_shared(codec, samplerate, channel, bits); file.reset(new MP4Muxer()); file->openMP4(path); file->addTrack(track); diff --git a/webrtc/RtcMediaSource.cpp b/webrtc/RtcMediaSource.cpp index d25c8d0b..d547350a 100644 --- a/webrtc/RtcMediaSource.cpp +++ b/webrtc/RtcMediaSource.cpp @@ -68,17 +68,11 @@ bool RtcMediaSourceMuxer::inputFrame(const Frame::Ptr &frame) switch (frame->getCodecId()) { case CodecAAC: - if (frame->prefixSize()) { - std::string cfg = makeAacConfig((uint8_t *)(frame->data()), frame->prefixSize()); - track = std::make_shared(cfg); - } - else { - track = std::make_shared(44100, 2); - } + track = Factory::getTrackByCodecId(CodecAAC, 44100, 2, 16); break; case CodecG711A: case CodecG711U: - track.reset(new G711Track(frame->getCodecId())); + track = Factory::getTrackByCodecId(frame->getCodecId()); break; default: break; @@ -116,7 +110,7 @@ bool RtcMediaSourceMuxer::addTrack(const Track::Ptr & track) { Track::Ptr newTrack = track; if (_option.audio_transcode && needTransToOpus(track->getCodecId())) { - newTrack = std::make_shared(); + newTrack = Factory::getTrackByCodecId(CodecOpus); GET_CONFIG(int, bitrate, General::kOpusBitrate); newTrack->setBitRate(bitrate); _audio_dec.reset(new FFmpegDecoder(track)); @@ -149,7 +143,7 @@ bool RtcMediaSourceImp::addTrack(const Track::Ptr &track) if (_muxer) { Track::Ptr newTrack = track; if (_option.audio_transcode && needTransToAac(track->getCodecId())) { - newTrack.reset(new AACTrack(44100, 2)); + newTrack = Factory::getTrackByCodecId(CodecAAC, 44100, std::dynamic_pointer_cast(track)->getAudioChannel(), 16); GET_CONFIG(int, bitrate, General::kAacBitrate); newTrack->setBitRate(bitrate); _audio_dec.reset(new FFmpegDecoder(track));