From a7d6e2ba38f4db61faa91076c00cbb3f260d2b1d Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Mon, 15 Nov 2021 10:32:54 +0800 Subject: [PATCH] =?UTF-8?q?RTC:=20webrtc=E5=81=8F=E5=A5=BD=E9=9F=B3?= =?UTF-8?q?=E8=A7=86=E9=A2=91codec=E5=8F=AF=E9=85=8D=E7=BD=AE(#1214)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/config.ini | 6 ++++++ webrtc/Sdp.cpp | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/conf/config.ini b/conf/config.ini index 5bd8e56e..42017efc 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -234,6 +234,12 @@ port=8000 #设置remb比特率,非0时关闭twcc并开启remb。该设置在rtc推流时有效,可以控制推流画质 #目前已经实现twcc自动调整码率,关闭remb根据真实网络状况调整码率 rembBitRate=0 +#rtc支持的音频codec类型,在前面的优先级更高 +#以下范例为所有支持的音频codec +preferredCodecA=PCMU,PCMA,opus,mpeg4-generic +#rtc支持的视频codec类型,在前面的优先级更高 +#以下范例为所有支持的视频codec +preferredCodecV=H264,H265,AV1X,VP9,VP8 [rtsp] #rtsp专有鉴权方式是采用base64还是md5方式 diff --git a/webrtc/Sdp.cpp b/webrtc/Sdp.cpp index 68e6f2b7..b9717a40 100644 --- a/webrtc/Sdp.cpp +++ b/webrtc/Sdp.cpp @@ -13,6 +13,16 @@ #include using namespace mediakit; +namespace RTC { +#define RTC_FIELD "rtc." +const string kPreferredCodecA = RTC_FIELD"preferredCodecA"; +const string kPreferredCodecV = RTC_FIELD"preferredCodecV"; +static onceToken token([]() { + mINI::Instance()[kPreferredCodecA] = "PCMU,PCMA,opus,mpeg4-generic"; + mINI::Instance()[kPreferredCodecV] = "H264,H265,AV1X,VP9,VP8"; +}); +} + using onCreateSdpItem = function; static map sdpItemCreator; @@ -1372,6 +1382,18 @@ void RtcConfigure::RtcTrackConfigure::enableREMB(bool enable){ } } +static vector toCodecArray(const string &str){ + vector ret; + auto vec = split(str, ","); + for (auto &s : vec) { + auto codec = getCodecId(trim(s)); + if (codec != CodecInvalid) { + ret.emplace_back(codec); + } + } + return ret; +} + void RtcConfigure::RtcTrackConfigure::setDefaultSetting(TrackType type){ rtcp_mux = true; rtcp_rsize = false; @@ -1385,7 +1407,10 @@ void RtcConfigure::RtcTrackConfigure::setDefaultSetting(TrackType type){ switch (type) { case TrackAudio: { //此处调整偏好的编码格式优先级 - preferred_codec = {CodecAAC, CodecG711U, CodecG711A, CodecOpus}; + GET_CONFIG_FUNC(vector, s_preferred_codec, RTC::kPreferredCodecA, toCodecArray); + CHECK(!s_preferred_codec.empty(), "rtc音频偏好codec不能为空"); + preferred_codec = s_preferred_codec; + rtcp_fb = {SdpConst::kTWCCRtcpFb, SdpConst::kRembRtcpFb}; extmap = { {RtpExtType::ssrc_audio_level, RtpDirection::sendrecv}, @@ -1401,7 +1426,10 @@ void RtcConfigure::RtcTrackConfigure::setDefaultSetting(TrackType type){ } case TrackVideo: { //此处调整偏好的编码格式优先级 - preferred_codec = {CodecH264, CodecH265, CodecAV1}; + GET_CONFIG_FUNC(vector, s_preferred_codec, RTC::kPreferredCodecV, toCodecArray); + CHECK(!s_preferred_codec.empty(), "rtc视频偏好codec不能为空"); + preferred_codec = s_preferred_codec; + rtcp_fb = {SdpConst::kTWCCRtcpFb, SdpConst::kRembRtcpFb, "nack", "ccm fir", "nack pli"}; extmap = { {RtpExtType::abs_send_time, RtpDirection::sendrecv},