Skip to content

Commit

Permalink
WebRTC: Support config the bitrate of transcoding AAC to Opus.(ossrs#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chundonglinlin committed Apr 15, 2023
1 parent 26aabe4 commit 2ac3325
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
10 changes: 10 additions & 0 deletions trunk/conf/full.conf
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,11 @@ vhost rtc.vhost.srs.com {
# Overwrite by env SRS_VHOST_RTC_KEEP_BFRAME for all vhosts.
# default: off
keep_bframe off;
# The transcode audio bitrate, for RTMP to RTC.
# Overwrite by env SRS_VHOST_RTC_AUDIO_BITRATE for all vhosts.
# [8000, 192000]
# default: 48000
opus_bitrate 48000;
###############################################################
# Whether enable transmuxing RTC to RTMP.
# Overwrite by env SRS_VHOST_RTC_RTC_TO_RTMP for all vhosts.
Expand All @@ -560,6 +565,11 @@ vhost rtc.vhost.srs.com {
# Overwrite by env SRS_VHOST_RTC_PLI_FOR_RTMP for all vhosts.
# Default: 6.0
pli_for_rtmp 6.0;
# The transcode audio bitrate, for RTC to RTMP.
# Overwrite by env SRS_VHOST_RTC_AUDIO_BITRATE for all vhosts.
# [8000, 192000]
# default: 48000
aac_bitrate 48000;
}
###############################################################
# For transmuxing RTMP to RTC, it will impact the default values if RTC is on.
Expand Down
38 changes: 38 additions & 0 deletions trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4641,6 +4641,44 @@ bool SrsConfig::get_rtc_twcc_enabled(string vhost)
return SRS_CONF_PERFER_TRUE(conf->arg0());
}

int SrsConfig::get_rtc_opus_bitrate(string vhost)
{
SRS_OVERWRITE_BY_ENV_INT("srs.vhost.rtc.opus_bitrate"); // SRS_VHOST_RTC_OPUS_BITRATE

static int DEFAULT = 48000;

SrsConfDirective* conf = get_rtc(vhost);
if (!conf) {
return DEFAULT;
}

conf = conf->get("opus_bitrate");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}

return ::atoi(conf->arg0().c_str());
}

int SrsConfig::get_rtc_aac_bitrate(string vhost)
{
SRS_OVERWRITE_BY_ENV_INT("srs.vhost.rtc.aac_bitrate"); // SRS_VHOST_RTC_AAC_BITRATE

static int DEFAULT = 48000;

SrsConfDirective* conf = get_rtc(vhost);
if (!conf) {
return DEFAULT;
}

conf = conf->get("aac_bitrate");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}

return ::atoi(conf->arg0().c_str());
}

SrsConfDirective* SrsConfig::get_vhost(string vhost, bool try_default_vhost)
{
srs_assert(root);
Expand Down
2 changes: 2 additions & 0 deletions trunk/src/app/srs_app_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,8 @@ class SrsConfig
bool get_rtc_nack_enabled(std::string vhost);
bool get_rtc_nack_no_copy(std::string vhost);
bool get_rtc_twcc_enabled(std::string vhost);
int get_rtc_opus_bitrate(std::string vhost);
int get_rtc_aac_bitrate(std::string vhost);

// vhost specified section
public:
Expand Down
4 changes: 2 additions & 2 deletions trunk/src/app/srs_app_rtc_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ srs_error_t SrsRtcRtpBuilder::init_codec(SrsAudioCodecId codec)
codec_ = new SrsAudioTranscoder();

// Initialize the codec according to the codec in stream.
int bitrate = 48000; // The output bitrate in bps.
int bitrate = _srs_config->get_rtc_opus_bitrate(req->vhost);// The output bitrate in bps.
if ((err = codec_->initialize(codec, SrsAudioCodecIdOpus, kAudioChannel, kAudioSamplerate, bitrate)) != srs_success) {
return srs_error_wrap(err, "init codec=%d", codec);
}
Expand Down Expand Up @@ -1334,7 +1334,7 @@ srs_error_t SrsRtcFrameBuilder::initialize(SrsRequest* r)
SrsAudioCodecId to = SrsAudioCodecIdAAC; // The output audio codec.
int channels = 2; // The output audio channels.
int sample_rate = 48000; // The output audio sample rate in HZ.
int bitrate = 48000; // The output audio bitrate in bps.
int bitrate = _srs_config->get_rtc_aac_bitrate(r->vhost); // The output audio bitrate in bps.
if ((err = codec_->initialize(from, to, channels, sample_rate, bitrate)) != srs_success) {
return srs_error_wrap(err, "bridge initialize");
}
Expand Down

0 comments on commit 2ac3325

Please sign in to comment.