处理转码和静音发生器冲突问题:

由于静音发生器产生的音频时间戳不连续,会导致转码出一大堆警告日志,并占用cpu
这变采用opus静音发生器来伪造rtc静音,而不是用转码:
This commit is contained in:
cqm 2022-11-17 12:43:30 +08:00
parent 607d8ba0e7
commit 3765873582
2 changed files with 30 additions and 16 deletions

View File

@ -139,10 +139,10 @@ MultiMediaSourceMuxer::MultiMediaSourceMuxer(const MediaTuple& tuple, float dur_
} }
if (option.audio_transcode) { if (option.audio_transcode) {
#if defined(ENABLE_FFMPEG) #if defined(ENABLE_FFMPEG)
_audio_transcode = option.audio_transcode;
InfoL << "enable audio_transcode"; InfoL << "enable audio_transcode";
#else #else
InfoL << "without ffmpeg disable audio_transcode"; InfoL << "without ffmpeg disable audio_transcode";
_option.audio_transcode = false;
#endif #endif
} }
@ -410,7 +410,7 @@ bool MultiMediaSourceMuxer::onTrackReady(const Track::Ptr &track) {
auto rtmp = _rtmp; auto rtmp = _rtmp;
auto rtc = _rtc; auto rtc = _rtc;
#if defined(ENABLE_FFMPEG) #if defined(ENABLE_FFMPEG)
if (_audio_transcode) { if (_option.audio_transcode) {
if (track->getCodecId() == CodecAAC) { if (track->getCodecId() == CodecAAC) {
if (rtmp) { if (rtmp) {
rtmp->addTrack(track); rtmp->addTrack(track);
@ -418,24 +418,34 @@ bool MultiMediaSourceMuxer::onTrackReady(const Track::Ptr &track) {
} }
_audio_dec = nullptr; _audio_dec = nullptr;
_audio_enc = nullptr; _audio_enc = nullptr;
_opus_mute_maker = nullptr;
if (rtc) { if (rtc) {
Track::Ptr newTrack(new OpusTrack()); Track::Ptr newTrack(new OpusTrack());
GET_CONFIG(int, bitrate, General::kOpusBitrate); GET_CONFIG(int, bitrate, General::kOpusBitrate);
newTrack->setBitRate(bitrate); newTrack->setBitRate(bitrate);
rtc->addTrack(newTrack); rtc->addTrack(newTrack);
rtc = nullptr; rtc = nullptr;
if (!hasMuteAudio()) {
// aac to opus // aac to opus
_audio_dec.reset(new FFmpegDecoder(track)); _audio_dec.reset(new FFmpegDecoder(track));
_audio_enc.reset(new FFmpegEncoder(newTrack)); _audio_enc.reset(new FFmpegEncoder(newTrack));
_audio_dec->setOnDecode([this](const FFmpegFrame::Ptr & frame) { _audio_dec->setOnDecode([this](const FFmpegFrame::Ptr & frame) {
_audio_enc->inputFrame(frame, false); _audio_enc->inputFrame(frame, false);
}); });
_audio_enc->setOnEncode([this](const Frame::Ptr& frame) { _audio_enc->setOnEncode([this](const Frame::Ptr& frame) {
// fill data to _rtc // fill data to _rtc
if (_rtc && _rtc->isEnabled()) if (_rtc && _rtc->isEnabled())
_rtc->inputFrame(frame); _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) { else if (track->getTrackType() == TrackAudio) {
@ -445,6 +455,7 @@ bool MultiMediaSourceMuxer::onTrackReady(const Track::Ptr &track) {
} }
_audio_dec = nullptr; _audio_dec = nullptr;
_audio_enc = nullptr; _audio_enc = nullptr;
_opus_mute_maker = nullptr;
if (rtmp) { if (rtmp) {
Track::Ptr newTrack(new AACTrack(44100, std::dynamic_pointer_cast<AudioTrack>(track)->getAudioChannel())); Track::Ptr newTrack(new AACTrack(44100, std::dynamic_pointer_cast<AudioTrack>(track)->getAudioChannel()));
GET_CONFIG(int, bitrate, General::kAacBitrate); GET_CONFIG(int, bitrate, General::kAacBitrate);
@ -571,6 +582,7 @@ void MultiMediaSourceMuxer::resetTracks() {
#if defined(ENABLE_FFMPEG) #if defined(ENABLE_FFMPEG)
_audio_dec = nullptr; _audio_dec = nullptr;
_audio_dec = nullptr; _audio_dec = nullptr;
_opus_mute_maker = nullptr;
#endif #endif
if (_fmp4) { if (_fmp4) {
_fmp4->resetTracks(); _fmp4->resetTracks();
@ -601,7 +613,7 @@ bool MultiMediaSourceMuxer::onTrackFrame(const Frame::Ptr &frame_in) {
if (_rtmp && _rtmp->isEnabled()) if (_rtmp && _rtmp->isEnabled())
rtmp = _rtmp; rtmp = _rtmp;
#if defined(ENABLE_FFMPEG) #if defined(ENABLE_FFMPEG)
if (_audio_transcode) { if (_option.audio_transcode) {
if (frame->getCodecId() == CodecAAC) { if (frame->getCodecId() == CodecAAC) {
if (rtc) { if (rtc) {
if (_audio_dec && rtc->readerCount()) if (_audio_dec && rtc->readerCount())
@ -615,6 +627,8 @@ bool MultiMediaSourceMuxer::onTrackFrame(const Frame::Ptr &frame_in) {
_audio_dec->inputFrame(frame, true, false, false); _audio_dec->inputFrame(frame, true, false, false);
rtmp = nullptr; rtmp = nullptr;
} }
} else if (_opus_mute_maker && rtc) {
_opus_mute_maker->inputFrame(frame);
} }
} }
#endif #endif

View File

@ -179,7 +179,7 @@ private:
std::shared_ptr<TSMediaSourceMuxer> _ts; std::shared_ptr<TSMediaSourceMuxer> _ts;
std::shared_ptr<RtspMediaSourceMuxer> _rtc; std::shared_ptr<RtspMediaSourceMuxer> _rtc;
#if defined(ENABLE_FFMPEG) #if defined(ENABLE_FFMPEG)
bool _audio_transcode = false; MuteAudioMaker::Ptr _opus_mute_maker;
std::shared_ptr<FFmpegDecoder> _audio_dec; std::shared_ptr<FFmpegDecoder> _audio_dec;
std::shared_ptr<FFmpegEncoder> _audio_enc; std::shared_ptr<FFmpegEncoder> _audio_enc;
#endif #endif