重构 audio_transcode 代码:

- 独立出 RtcMediaSource,并只对rtc开放
- 增加Rtc g711转码开关
- 修改说明
This commit is contained in:
cqm
2022-11-29 20:38:59 +08:00
parent 2adc12c4ab
commit f4b2fd9c05
8 changed files with 281 additions and 135 deletions

View File

@@ -11,9 +11,6 @@
#include <math.h>
#include "Common/config.h"
#include "MultiMediaSourceMuxer.h"
#include "Extension/AAC.h"
#include "Extension/Opus.h"
#include "Extension/G711.h"
#include "Rtp/RtpSender.h"
#include "Record/HlsRecorder.h"
#include "Record/HlsMediaSource.h"
@@ -21,9 +18,8 @@
#include "Rtmp/RtmpMediaSourceMuxer.h"
#include "TS/TSMediaSourceMuxer.h"
#include "FMP4/FMP4MediaSourceMuxer.h"
#ifdef ENABLE_FFMPEG
#include "Codec/Transcode.h"
#if defined(ENABLE_WEBRTC)
#include "webrtc/RtcMediaSource.h"
#endif
using namespace std;
using namespace toolkit;
@@ -122,7 +118,7 @@ MultiMediaSourceMuxer::MultiMediaSourceMuxer(const MediaTuple& tuple, float dur_
}
if (option.enable_rtc) {
#if defined(ENABLE_WEBRTC)
_rtc = std::make_shared<RtspMediaSourceMuxer>(_tuple, option, std::make_shared<TitleSdp>(dur_sec), RTC_SCHEMA);
_rtc = std::make_shared<RtcMediaSourceMuxer>(_tuple, option, std::make_shared<TitleSdp>(dur_sec));
#endif
}
if (option.enable_hls) {
@@ -137,14 +133,6 @@ MultiMediaSourceMuxer::MultiMediaSourceMuxer(const MediaTuple& tuple, float dur_
if (option.enable_ts) {
_ts = dynamic_pointer_cast<TSMediaSourceMuxer>(Recorder::createRecorder(Recorder::type_ts, _tuple, option));
}
if (option.audio_transcode) {
#if defined(ENABLE_FFMPEG)
InfoL << "enable audio_transcode";
#else
InfoL << "without ffmpeg disable audio_transcode";
_option.audio_transcode = false;
#endif
}
if (option.enable_fmp4) {
_fmp4 = dynamic_pointer_cast<FMP4MediaSourceMuxer>(Recorder::createRecorder(Recorder::type_fmp4, _tuple, option));
@@ -407,79 +395,9 @@ std::shared_ptr<MultiMediaSourceMuxer> MultiMediaSourceMuxer::getMuxer(MediaSour
bool MultiMediaSourceMuxer::onTrackReady(const Track::Ptr &track) {
bool ret = false;
auto rtmp = _rtmp;
auto rtc = _rtc;
#if defined(ENABLE_FFMPEG)
if (_option.audio_transcode) {
if (track->getCodecId() == CodecAAC) {
if (rtmp) {
rtmp->addTrack(track);
rtmp = nullptr;
}
_audio_dec = nullptr;
_audio_enc = nullptr;
_opus_mute_maker = nullptr;
if (rtc) {
Track::Ptr newTrack(new OpusTrack());
GET_CONFIG(int, bitrate, General::kOpusBitrate);
newTrack->setBitRate(bitrate);
rtc->addTrack(newTrack);
rtc = nullptr;
if (!hasMuteAudio()) {
// aac to opus
_audio_dec.reset(new FFmpegDecoder(track));
_audio_enc.reset(new FFmpegEncoder(newTrack));
_audio_dec->setOnDecode([this](const FFmpegFrame::Ptr & frame) {
_audio_enc->inputFrame(frame, false);
});
_audio_enc->setOnEncode([this](const Frame::Ptr& frame) {
// fill data to _rtc
if (_rtc && _rtc->isEnabled())
_rtc->inputFrame(frame);
});
}
else {
_opus_mute_maker = std::make_shared<MuteAudioMaker>(CodecOpus);
_opus_mute_maker->addDelegate([this](const Frame::Ptr &frame) {
if (_rtc && _rtc->isEnabled())
_rtc->inputFrame(frame);
return true;
});
}
}
}
else if (track->getTrackType() == TrackAudio) {
if (rtc) {
rtc->addTrack(track);
rtc = nullptr;
}
_audio_dec = nullptr;
_audio_enc = nullptr;
_opus_mute_maker = nullptr;
if (rtmp) {
Track::Ptr newTrack(new AACTrack(44100, std::dynamic_pointer_cast<AudioTrack>(track)->getAudioChannel()));
GET_CONFIG(int, bitrate, General::kAacBitrate);
newTrack->setBitRate(bitrate);
rtmp->addTrack(newTrack);
rtmp = nullptr;
_audio_dec.reset(new FFmpegDecoder(track));
_audio_enc.reset(new FFmpegEncoder(newTrack));
_audio_dec->setOnDecode([this](const FFmpegFrame::Ptr & frame) {
_audio_enc->inputFrame(frame, false);
});
_audio_enc->setOnEncode([this](const Frame::Ptr& frame) {
// fill aac frame to rtmp
if (_rtmp && _rtmp->isEnabled())
_rtmp->inputFrame(frame);
});
}
}
}
#endif
if (rtc && rtc->addTrack(track))
if (_rtc && _rtc->addTrack(track))
ret = true;
if (rtmp && rtmp->addTrack(track))
if (_rtmp && _rtmp->addTrack(track))
ret = true;
if (_rtsp && _rtsp->addTrack(track))
ret = true;
@@ -579,11 +497,6 @@ void MultiMediaSourceMuxer::resetTracks() {
if (_rtc) {
_rtc->resetTracks();
}
#if defined(ENABLE_FFMPEG)
_audio_dec = nullptr;
_audio_dec = nullptr;
_opus_mute_maker = nullptr;
#endif
if (_fmp4) {
_fmp4->resetTracks();
}
@@ -606,36 +519,10 @@ bool MultiMediaSourceMuxer::onTrackFrame(const Frame::Ptr &frame_in) {
}
bool ret = false;
RtspMediaSourceMuxer::Ptr rtc;
RtmpMediaSourceMuxer::Ptr rtmp;
if (_rtc && _rtc->isEnabled())
rtc = _rtc;
if (_rtmp && _rtmp->isEnabled())
rtmp = _rtmp;
#if defined(ENABLE_FFMPEG)
if (_option.audio_transcode) {
if (frame->getCodecId() == CodecAAC) {
if (rtc) {
if (_audio_dec && rtc->readerCount())
_audio_dec->inputFrame(frame, true, false, false);
rtc = nullptr;
}
}
else if (frame->getTrackType() == TrackAudio) {
if (rtmp) {
if (_audio_dec && (rtmp->readerCount() || !rtmp->isRegisted()))
_audio_dec->inputFrame(frame, true, false, false);
rtmp = nullptr;
}
} else if (_opus_mute_maker && rtc) {
_opus_mute_maker->inputFrame(frame);
}
}
#endif
if (rtc && rtc->inputFrame(frame))
if (_rtc && _rtc->inputFrame(frame))
ret = true;
if (rtmp && rtmp->inputFrame(frame))
if (_rtmp && _rtmp->inputFrame(frame))
ret = true;
if (_rtsp && _rtsp->inputFrame(frame))

View File

@@ -24,10 +24,6 @@ class RtmpMediaSourceMuxer;
class TSMediaSourceMuxer;
class FMP4MediaSourceMuxer;
class RtpSender;
#ifdef ENABLE_FFMPEG
class FFmpegDecoder;
class FFmpegEncoder;
#endif
class MultiMediaSourceMuxer : public MediaSourceEventInterceptor, public MediaSink, public std::enable_shared_from_this<MultiMediaSourceMuxer>{
public:
@@ -178,11 +174,6 @@ private:
std::shared_ptr<RtspMediaSourceMuxer> _rtsp;
std::shared_ptr<TSMediaSourceMuxer> _ts;
std::shared_ptr<RtspMediaSourceMuxer> _rtc;
#if defined(ENABLE_FFMPEG)
MuteAudioMaker::Ptr _opus_mute_maker;
std::shared_ptr<FFmpegDecoder> _audio_dec;
std::shared_ptr<FFmpegEncoder> _audio_enc;
#endif
MediaSinkInterface::Ptr _mp4;
std::shared_ptr<HlsRecorder> _hls;
std::shared_ptr<HlsFMP4Recorder> _hls_fmp4;