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