From 2ead272187b3d25b3c50011850e7f0bc10cf1464 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Fri, 21 Jun 2024 20:46:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dwebrtc=E6=92=AD=E6=94=BE?= =?UTF-8?q?=E6=97=B6=E7=94=9F=E6=88=90config=20rtp=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E5=AE=89=E5=85=A8=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit rtsp直接代理时会从config frame生成rtp,在获取VideoTrack配置帧列表时存在线程安全风险; 同时简化getConfigFrames函数代码,去除缓存逻辑。 --- ext-codec/H264.cpp | 12 ++++++++---- ext-codec/H264.h | 3 +-- ext-codec/H265.cpp | 14 +++++++++----- ext-codec/H265.h | 3 +-- webrtc/WebRtcPlayer.cpp | 12 ++++++++---- 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/ext-codec/H264.cpp b/ext-codec/H264.cpp index 7f796ea0..d542a8aa 100644 --- a/ext-codec/H264.cpp +++ b/ext-codec/H264.cpp @@ -235,13 +235,17 @@ void H264Track::setExtraData(const uint8_t *data, size_t bytes) { } bool H264Track::update() { - _config_frames = std::vector{ - createConfigFrame(_sps, 0, getIndex()), - createConfigFrame(_pps, 0, getIndex()) - }; return getAVCInfo(_sps, _width, _height, _fps); } +std::vector H264Track::getConfigFrames() const { + if (!ready()) { + return {}; + } + return { createConfigFrame(_sps, 0, getIndex()), + createConfigFrame(_pps, 0, getIndex()) }; +} + Track::Ptr H264Track::clone() const { return std::make_shared(*this); } diff --git a/ext-codec/H264.h b/ext-codec/H264.h index 9d662037..e76263a9 100644 --- a/ext-codec/H264.h +++ b/ext-codec/H264.h @@ -115,7 +115,7 @@ public: toolkit::Buffer::Ptr getExtraData() const override; void setExtraData(const uint8_t *data, size_t size) override; bool update() override; - std::vector getConfigFrames() const override { return _config_frames; } + std::vector getConfigFrames() const override; private: Sdp::Ptr getSdp(uint8_t payload_type) const override; @@ -130,7 +130,6 @@ private: float _fps = 0; std::string _sps; std::string _pps; - std::vector _config_frames; }; template diff --git a/ext-codec/H265.cpp b/ext-codec/H265.cpp index 4bf3e2ff..ae649b1d 100644 --- a/ext-codec/H265.cpp +++ b/ext-codec/H265.cpp @@ -182,14 +182,18 @@ void H265Track::setExtraData(const uint8_t *data, size_t bytes) { } bool H265Track::update() { - _config_frames = std::vector{ - createConfigFrame(_vps, 0, getIndex()), - createConfigFrame(_sps, 0, getIndex()), - createConfigFrame(_pps, 0, getIndex()) - }; return getHEVCInfo(_vps, _sps, _width, _height, _fps); } +std::vector H265Track::getConfigFrames() const { + if (!ready()) { + return {}; + } + return { createConfigFrame(_vps, 0, getIndex()), + createConfigFrame(_sps, 0, getIndex()), + createConfigFrame(_pps, 0, getIndex()) }; +} + Track::Ptr H265Track::clone() const { return std::make_shared(*this); } diff --git a/ext-codec/H265.h b/ext-codec/H265.h index 8132f444..b95f72e8 100644 --- a/ext-codec/H265.h +++ b/ext-codec/H265.h @@ -142,7 +142,7 @@ public: toolkit::Buffer::Ptr getExtraData() const override; void setExtraData(const uint8_t *data, size_t size) override; bool update() override; - std::vector getConfigFrames() const override { return _config_frames; } + std::vector getConfigFrames() const override; private: Sdp::Ptr getSdp(uint8_t payload_type) const override; @@ -158,7 +158,6 @@ private: std::string _vps; std::string _sps; std::string _pps; - std::vector _config_frames; }; }//namespace mediakit diff --git a/webrtc/WebRtcPlayer.cpp b/webrtc/WebRtcPlayer.cpp index b410a154..83ff5830 100644 --- a/webrtc/WebRtcPlayer.cpp +++ b/webrtc/WebRtcPlayer.cpp @@ -129,7 +129,12 @@ void WebRtcPlayer::sendConfigFrames(uint32_t before_seq, uint32_t sample_rate, u if (!play_src) { return; } - auto video_track = std::dynamic_pointer_cast(play_src->getTrack(mediakit::TrackVideo)); + SdpParser parser(play_src->getSdp()); + auto video_sdp = parser.getTrack(TrackVideo); + if (!video_sdp) { + return; + } + auto video_track = dynamic_pointer_cast(Factory::getTrackBySdp(video_sdp)); if (!video_track) { return; } @@ -146,9 +151,8 @@ void WebRtcPlayer::sendConfigFrames(uint32_t before_seq, uint32_t sample_rate, u encoder->setRtpInfo(0, video_mtu, sample_rate, 0, 0, 0); auto seq = before_seq - frames.size(); - for (const auto &frame : video_track->getConfigFrames()) { - auto rtp = encoder->getRtpInfo().makeRtp( - TrackVideo, frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize(), false, 0); + for (const auto &frame : frames) { + auto rtp = encoder->getRtpInfo().makeRtp(TrackVideo, frame->data() + frame->prefixSize(), frame->size() - frame->prefixSize(), false, 0); auto header = rtp->getHeader(); header->seq = htons(seq++); header->stamp = htonl(timestamp);