Skip to content

Commit

Permalink
RTC: Fix FFmpeg opus audio noisy issue. v5.0.195 (#3845)
Browse files Browse the repository at this point in the history
Follow the example in FFmpeg's doc, before calling the API
`avcodec_send_frame`, always use `av_frame_alloc` to create a new frame.

---------

Co-authored-by: Haibo Chen <[email protected]>
  • Loading branch information
2 people authored and winlinvip committed Oct 25, 2023
1 parent d51cabb commit 268bac5
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 21 deletions.
46 changes: 46 additions & 0 deletions trunk/conf/rtc.tcp.only.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# WebRTC streaming config for SRS.
# @see full.conf for detail config.

listen 1935;
max_connections 1000;
daemon off;
srs_log_tank console;

http_server {
enabled on;
listen 8080;
dir ./objs/nginx/html;
}

http_api {
enabled on;
listen 1985;
}
stats {
network 0;
}
rtc_server {
enabled on;
tcp {
enabled on;
listen 8000;
}
protocol tcp;
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#config-candidate
candidate $CANDIDATE;
}

vhost __defaultVhost__ {
rtc {
enabled on;
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#rtmp-to-rtc
rtmp_to_rtc off;
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#rtc-to-rtmp
rtc_to_rtmp off;
}
http_remux {
enabled on;
mount [vhost]/[app]/[stream].flv;
}
}

47 changes: 47 additions & 0 deletions trunk/conf/rtc.tcp.udp.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# WebRTC streaming config for SRS.
# @see full.conf for detail config.

listen 1935;
max_connections 1000;
daemon off;
srs_log_tank console;

http_server {
enabled on;
listen 8080;
dir ./objs/nginx/html;
}

http_api {
enabled on;
listen 1985;
}
stats {
network 0;
}
rtc_server {
enabled on;
listen 8000; # UDP port
tcp {
enabled on;
listen 8000;
}
protocol all;
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#config-candidate
candidate $CANDIDATE;
}

vhost __defaultVhost__ {
rtc {
enabled on;
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#rtmp-to-rtc
rtmp_to_rtc off;
# @see https://ossrs.net/lts/zh-cn/docs/v4/doc/webrtc#rtc-to-rtmp
rtc_to_rtmp off;
}
http_remux {
enabled on;
mount [vhost]/[app]/[stream].flv;
}
}

1 change: 1 addition & 0 deletions trunk/doc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The changelog for SRS.
<a name="v5-changes"></a>

## SRS 5.0 Changelog
* v5.0, 2023-10-25, Merge [#3845](https://github.com/ossrs/srs/pull/3845): RTC: Fix FFmpeg opus audio noisy issue. v5.0.195 (#3845)
* v5.0, 2023-10-21, Merge [#3847](https://github.com/ossrs/srs/pull/3847): WebRTC: TCP transport should use read_fully instead of read. v5.0.194 (#3847)
* v5.0, 2023-10-20, Merge [#3846](https://github.com/ossrs/srs/pull/3846): Added system library option for ffmpeg, srtp, srt libraries. v5.0.193 (#3846)
* v5.0, 2023-10-17, Merge [#3840](https://github.com/ossrs/srs/pull/3840): Disable asan by default. v5.0.192 (#3840)
Expand Down
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
36 changes: 20 additions & 16 deletions trunk/src/app/srs_app_rtc_codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ srs_error_t SrsAudioTranscoder::init_enc(SrsAudioCodecId dst_codec, int dst_chan
enc_->channel_layout = av_get_default_channel_layout(dst_channels);
enc_->bit_rate = dst_bit_rate;
enc_->sample_fmt = codec->sample_fmts[0];
enc_->time_base.num = 1; enc_->time_base.den = 1000; // {1, 1000}
enc_->time_base.num = 1; enc_->time_base.den = dst_samplerate; // {1, dst_samplerate}
if (dst_codec == SrsAudioCodecIdOpus) {
//TODO: for more level setting
enc_->compression_level = 1;
Expand All @@ -261,14 +261,6 @@ srs_error_t SrsAudioTranscoder::init_enc(SrsAudioCodecId dst_codec, int dst_chan
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;

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

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 @@ -380,25 +372,35 @@ srs_error_t SrsAudioTranscoder::encode(std::vector<SrsAudioFrame*> &pkts)
if (next_out_pts_ == AV_NOPTS_VALUE) {
next_out_pts_ = new_pkt_pts_;
} else {
int64_t diff = llabs(new_pkt_pts_ - next_out_pts_);
int64_t diff = llabs(new_pkt_pts_ - av_rescale(next_out_pts_, 1000, enc_->time_base.den));
if (diff > 1000) {
srs_trace("time diff to large=%lld, next out=%lld, new pkt=%lld, set to new pkt",
diff, next_out_pts_, new_pkt_pts_);
next_out_pts_ = new_pkt_pts_;
next_out_pts_ = av_rescale(new_pkt_pts_, enc_->time_base.den, 1000);
}
}

int frame_cnt = 0;
while (av_audio_fifo_size(fifo_) >= enc_->frame_size) {
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_);
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_);
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_ + av_rescale(enc_->frame_size * frame_cnt, 1000, enc_->sample_rate);
++frame_cnt;
enc_frame_->pts = next_out_pts_;
next_out_pts_ += enc_->frame_size;
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 @@ -419,6 +421,10 @@ srs_error_t SrsAudioTranscoder::encode(std::vector<SrsAudioFrame*> &pkts)
av_make_error_string(err_buf, AV_ERROR_MAX_STRING_SIZE, error));
}

// 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);

SrsAudioFrame *out_frame = new SrsAudioFrame;
char *buf = new char[enc_packet_->size];
memcpy(buf, enc_packet_->data, enc_packet_->size);
Expand All @@ -429,8 +435,6 @@ srs_error_t SrsAudioTranscoder::encode(std::vector<SrsAudioFrame*> &pkts)
}
}

next_out_pts_ += av_rescale(enc_->frame_size * frame_cnt, 1000, enc_->sample_rate);

return srs_success;
}

Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2733,7 +2733,7 @@ srs_error_t SrsLiveSource::consumer_dumps(SrsLiveConsumer* consumer, bool ds, bo

// print status.
if (dg) {
srs_trace("create consumer, active=%d, queue_size=%.2f, jitter=%d", hub->active(), queue_size, jitter_algorithm);
srs_trace("create consumer, active=%d, queue_size=%dms, jitter=%d", hub->active(), srsu2msi(queue_size), jitter_algorithm);
} else {
srs_trace("create consumer, active=%d, ignore gop cache, jitter=%d", hub->active(), jitter_algorithm);
}
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/core/srs_core_version5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

#define VERSION_MAJOR 5
#define VERSION_MINOR 0
#define VERSION_REVISION 194
#define VERSION_REVISION 195

#endif
2 changes: 0 additions & 2 deletions trunk/src/protocol/srs_protocol_rtmp_handshake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,6 @@ namespace srs_internal
{
srs_error_t err = srs_success;

int32_t bits_count = 1024;

close();

//1. Create the DH
Expand Down

0 comments on commit 268bac5

Please sign in to comment.