From 2e1a45895a5823d9e4643b174bf843389d5c380c Mon Sep 17 00:00:00 2001 From: bin-work <44047515+bin-work@users.noreply.github.com> Date: Thu, 7 Jul 2022 16:44:42 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9webrtc=E7=A0=81=E7=8E=87?= =?UTF-8?q?=E5=B8=A6=E5=AE=BD=E7=BA=A6=E6=9D=9F=E5=AD=97=E6=AE=B5=20(#1771?= =?UTF-8?q?,=20#1768)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webrtc/Sdp.cpp | 9 +++++---- webrtc/Sdp.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/webrtc/Sdp.cpp b/webrtc/Sdp.cpp index 4ce1c489..c48361f0 100644 --- a/webrtc/Sdp.cpp +++ b/webrtc/Sdp.cpp @@ -771,7 +771,6 @@ void RtcSession::loadFrom(const string &str) { session_name = sdp.getSessionName(); session_info = sdp.getSessionInfo(); connection = sdp.getConnection(); - bandwidth = sdp.getBandwidth(); time = sdp.getSessionTime(); msid_semantic = sdp.getItemClass('a', "msid-semantic"); for (auto &media : sdp.medias) { @@ -783,6 +782,7 @@ void RtcSession::loadFrom(const string &str) { rtc_media.type = mline.type; rtc_media.port = mline.port; rtc_media.addr = media.getItemClass('c'); + rtc_media.bandwidth = media.getItemClass('b'); rtc_media.ice_ufrag = media.getStringItem('a', "ice-ufrag"); rtc_media.ice_pwd = media.getStringItem('a', "ice-pwd"); rtc_media.role = media.getItemClass('a', "setup").role; @@ -1060,9 +1060,6 @@ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const{ if(connection.empty()){ sdp.addItem(std::make_shared(connection)); } - if (!bandwidth.empty()) { - sdp.addItem(std::make_shared(bandwidth)); - } sdp.addAttr(std::make_shared(group)); sdp.addAttr(std::make_shared(msid_semantic)); for (auto &m : media) { @@ -1080,6 +1077,9 @@ RtcSessionSdp::Ptr RtcSession::toRtcSessionSdp() const{ } sdp_media.addItem(std::move(mline)); sdp_media.addItem(std::make_shared(m.addr)); + if (!m.bandwidth.empty() && m.type != TrackAudio) { + sdp_media.addItem(std::make_shared(m.bandwidth)); + } if (!m.rtcp_addr.empty()) { sdp_media.addAttr(std::make_shared(m.rtcp_addr)); } @@ -1631,6 +1631,7 @@ RETRY: answer_media.proto = offer_media.proto; answer_media.port = offer_media.port; answer_media.addr = offer_media.addr; + answer_media.bandwidth = offer_media.bandwidth; answer_media.rtcp_addr = offer_media.rtcp_addr; answer_media.rtcp_mux = offer_media.rtcp_mux && configure.rtcp_mux; answer_media.rtcp_rsize = offer_media.rtcp_rsize && configure.rtcp_rsize; diff --git a/webrtc/Sdp.h b/webrtc/Sdp.h index df6ae557..41e1d5d3 100644 --- a/webrtc/Sdp.h +++ b/webrtc/Sdp.h @@ -612,6 +612,7 @@ public: std::string mid; uint16_t port{0}; SdpConnection addr; + SdpBandwidth bandwidth; std::string proto; RtpDirection direction{RtpDirection::invalid}; std::vector plan; @@ -666,7 +667,6 @@ public: std::string session_info; SdpTime time; SdpConnection connection; - SdpBandwidth bandwidth; SdpAttrMsidSemantic msid_semantic; std::vector media; SdpAttrGroup group; From c4988e7cfc8c6b1f16f3d9aaf1da5ced92bee737 Mon Sep 17 00:00:00 2001 From: Dw9 Date: Thu, 7 Jul 2022 16:48:35 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dmk=5Ftrack=5Fcreate?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E7=9A=84track=E6=97=A0=E6=B3=95=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E5=A4=8D=E7=94=A8=E5=99=A8=E7=9A=84bug(#1784?= =?UTF-8?q?,=20#1778=20)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/source/mk_media.cpp | 1 - api/source/mk_track.cpp | 13 ++++++++----- api/tests/h264_media_server.c | 6 +++++- src/Extension/Factory.cpp | 26 ++++++++++++++++++++++++++ src/Extension/Factory.h | 5 +++++ 5 files changed, 44 insertions(+), 7 deletions(-) diff --git a/api/source/mk_media.cpp b/api/source/mk_media.cpp index 9ee56fe6..0abe2f46 100755 --- a/api/source/mk_media.cpp +++ b/api/source/mk_media.cpp @@ -187,7 +187,6 @@ API_EXPORT int API_CALL mk_media_init_video(mk_media ctx, int codec_id, int widt info.iWidth = width; info.iHeight = height; info.iBitRate = bit_rate; - (*obj)->getChannel()->initVideo(info); return (*obj)->getChannel()->initVideo(info); } diff --git a/api/source/mk_track.cpp b/api/source/mk_track.cpp index 6d84ac70..b4df453d 100644 --- a/api/source/mk_track.cpp +++ b/api/source/mk_track.cpp @@ -10,12 +10,13 @@ #include "mk_track.h" #include "Extension/Track.h" +#include "Extension/Factory.h" using namespace std; using namespace toolkit; using namespace mediakit; -class VideoTrackForC : public VideoTrack { +class VideoTrackForC : public VideoTrack, public std::enable_shared_from_this { public: VideoTrackForC(int codec_id, codec_args *args) { _codec_id = (CodecId) codec_id; @@ -49,7 +50,8 @@ public: } Track::Ptr clone() override { - return std::make_shared::type>(*this); + auto track_in = std::shared_ptr(shared_from_this()); + return Factory::getTrackByAbstractTrack(track_in); } Sdp::Ptr getSdp() override { @@ -61,15 +63,16 @@ private: codec_args _args; }; -class AudioTrackForC : public AudioTrackImp { +class AudioTrackForC : public AudioTrackImp, public std::enable_shared_from_this { public: ~AudioTrackForC() override = default; AudioTrackForC(int codec_id, codec_args *args) : - AudioTrackImp((CodecId) codec_id, args->audio.sample_rate, args->audio.channels, 16) {} + AudioTrackImp((CodecId) codec_id, args->audio.sample_rate, args->audio.channels, 16) {} Track::Ptr clone() override { - return std::make_shared::type>(*this); + auto track_in = std::shared_ptr(shared_from_this()); + return Factory::getTrackByAbstractTrack(track_in); } Sdp::Ptr getSdp() override { diff --git a/api/tests/h264_media_server.c b/api/tests/h264_media_server.c index 3e0084f5..4ca99ffd 100644 --- a/api/tests/h264_media_server.c +++ b/api/tests/h264_media_server.c @@ -64,8 +64,12 @@ int main(int argc, char *argv[]) { mk_media media = mk_media_create("__defaultVhost__", "live", "test", 0, 0, 0); //h264的codec - mk_media_init_video(media, 0, 0, 0, 0, 2 * 104 * 1024); + //mk_media_init_video(media, 0, 0, 0, 0, 2 * 104 * 1024); + codec_args v_args={0}; + mk_track v_track = mk_track_create(MKCodecH264,&v_args); + mk_media_init_track(media,v_track); mk_media_init_complete(media); + mk_track_unref(v_track); //创建h264分帧器 mk_h264_splitter splitter = mk_h264_splitter_create(on_h264_frame, media); diff --git a/src/Extension/Factory.cpp b/src/Extension/Factory.cpp index d7757ecb..fae8dd8b 100644 --- a/src/Extension/Factory.cpp +++ b/src/Extension/Factory.cpp @@ -95,6 +95,32 @@ Track::Ptr Factory::getTrackBySdp(const SdpTrack::Ptr &track) { } } + +Track::Ptr Factory::getTrackByAbstractTrack(const Track::Ptr& track) { + auto codec = track->getCodecId(); + switch (codec) { + case CodecG711A: + case CodecG711U: { + auto audio_track = dynamic_pointer_cast(track); + return std::make_shared(codec, audio_track->getAudioSampleRate(), audio_track->getAudioChannel(), 16); + } + case CodecL16: { + auto audio_track = dynamic_pointer_cast(track); + return std::make_shared(audio_track->getAudioSampleRate(), audio_track->getAudioChannel()); + } + case CodecAAC : return std::make_shared(); + case CodecOpus : return std::make_shared(); + case CodecH265 : return std::make_shared(); + case CodecH264 : return std::make_shared(); + + default: { + //其他codec不支持 + WarnL << "暂不支持该该编码类型创建Track:" << track->getCodecName(); + return nullptr; + } + } +} + RtpCodec::Ptr Factory::getRtpEncoderBySdp(const Sdp::Ptr &sdp) { GET_CONFIG(uint32_t,audio_mtu,Rtp::kAudioMtuSize); GET_CONFIG(uint32_t,video_mtu,Rtp::kVideoMtuSize); diff --git a/src/Extension/Factory.h b/src/Extension/Factory.h index 3e5684f9..75c968ce 100644 --- a/src/Extension/Factory.h +++ b/src/Extension/Factory.h @@ -27,6 +27,11 @@ public: */ static Track::Ptr getTrackBySdp(const SdpTrack::Ptr &track); + /** + * 根据c api 抽象的Track生成具体Track对象 + */ + static Track::Ptr getTrackByAbstractTrack(const Track::Ptr& track); + /** * 根据sdp生成rtp编码器 * @param sdp sdp对象