Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTC: Fix FFmpeg opus audio noisy issue. v5.0.195 v6.0.95 #3845

Merged
merged 8 commits into from
Oct 25, 2023
Prev Previous commit
Next Next commit
Optimize the code.
  • Loading branch information
duiniuluantanqin committed Oct 22, 2023
commit 0c568a70d7c439bc2809bb53c3533279d004abd2
1 change: 0 additions & 1 deletion trunk/ide/srs_clion/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ set(DEPS_LIBS ${SRS_DIR}/objs/st/libst.a
${SRS_DIR}/objs/srtp2/lib/libsrtp2.a
${SRS_DIR}/objs/ffmpeg/lib/libavcodec.a
${SRS_DIR}/objs/ffmpeg/lib/libavutil.a
${SRS_DIR}/objs/opus/lib/libopus.a
${SRS_DIR}/objs/ffmpeg/lib/libswresample.a
${SRS_DIR}/objs/srt/lib/libsrt.a)
foreach(DEPS_LIB ${DEPS_LIBS})
Expand Down
37 changes: 21 additions & 16 deletions trunk/src/app/srs_app_rtc_codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ SrsAudioTranscoder::SrsAudioTranscoder()
dec_frame_ = NULL;
dec_packet_ = NULL;
enc_ = NULL;
enc_frame_ = NULL;
enc_packet_ = NULL;
swr_ = NULL;
swr_data_ = NULL;
Expand Down Expand Up @@ -122,6 +123,10 @@ SrsAudioTranscoder::~SrsAudioTranscoder()
avcodec_free_context(&enc_);
}

if (enc_frame_) {
av_frame_free(&enc_frame_);
}

if (enc_packet_) {
av_packet_free(&enc_packet_);
}
Expand Down Expand Up @@ -251,6 +256,11 @@ srs_error_t SrsAudioTranscoder::init_enc(SrsAudioCodecId dst_codec, int dst_chan
return srs_error_new(ERROR_RTC_RTP_MUXER, "Could not open codec");
}

enc_frame_ = av_frame_alloc();
if (!enc_frame_) {
return srs_error_new(ERROR_RTC_RTP_MUXER, "Could not allocate audio encode in frame");
}

enc_packet_ = av_packet_alloc();
if (!enc_packet_) {
return srs_error_new(ERROR_RTC_RTP_MUXER, "Could not allocate audio encode out packet");
Expand Down Expand Up @@ -371,31 +381,26 @@ srs_error_t SrsAudioTranscoder::encode(std::vector<SrsAudioFrame*> &pkts)
}

while (av_audio_fifo_size(fifo_) >= enc_->frame_size) {
AVFrame* enc_frame = av_frame_alloc();
if (!enc_frame) {
return srs_error_new(ERROR_RTC_RTP_MUXER, "Could not allocate audio encode in frame");
}

enc_frame->format = enc_->sample_fmt;
enc_frame->nb_samples = enc_->frame_size;
enc_frame->channel_layout = enc_->channel_layout;
enc_frame_->format = enc_->sample_fmt;
enc_frame_->nb_samples = enc_->frame_size;
enc_frame_->channel_layout = enc_->channel_layout;

if (av_frame_get_buffer(enc_frame, 0) < 0) {
av_frame_free(&enc_frame);
if (av_frame_get_buffer(enc_frame_, 0) < 0) {
av_frame_free(&enc_frame_);
return srs_error_new(ERROR_RTC_RTP_MUXER, "Could not get audio frame buffer");
}

/* Read as many samples from the FIFO buffer as required to fill the frame.
* The samples are stored in the frame temporarily. */
if (av_audio_fifo_read(fifo_, (void **)enc_frame->data, enc_->frame_size) < enc_->frame_size) {
av_frame_free(&enc_frame);
if (av_audio_fifo_read(fifo_, (void **)enc_frame_->data, enc_->frame_size) < enc_->frame_size) {
av_frame_free(&enc_frame_);
return srs_error_new(ERROR_RTC_RTP_MUXER, "Could not read data from FIFO");
}
/* send the frame for encoding */
enc_frame->pts = next_out_pts_;
enc_frame_->pts = next_out_pts_;
next_out_pts_ += enc_->frame_size;
int error = avcodec_send_frame(enc_, enc_frame);
av_frame_free(&enc_frame);
int error = avcodec_send_frame(enc_, enc_frame_);
av_frame_unref(enc_frame_);
if (error < 0) {
return srs_error_new(ERROR_RTC_RTP_MUXER, "Error sending the frame to the encoder(%d,%s)", error,
av_make_error_string(err_buf, AV_ERROR_MAX_STRING_SIZE, error));
Expand All @@ -418,7 +423,7 @@ srs_error_t SrsAudioTranscoder::encode(std::vector<SrsAudioFrame*> &pkts)

// rescale time base from sample_rate 1000.
enc_packet_->dts = av_rescale(enc_packet_->dts, 1000, enc_->time_base.den);
enc_packet_->pts = av_rescale(enc_packet_->pts, 1000, enc_->time_base.den);
enc_packet_->pts = av_rescale(enc_packet_->pts, 1000, enc_->time_base.den);

SrsAudioFrame *out_frame = new SrsAudioFrame;
char *buf = new char[enc_packet_->size];
Expand Down
1 change: 1 addition & 0 deletions trunk/src/app/srs_app_rtc_codec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class SrsAudioTranscoder
AVPacket *dec_packet_;

AVCodecContext *enc_;
AVFrame *enc_frame_;
AVPacket *enc_packet_;

SwrContext *swr_;
Expand Down
Loading