From 34365a2f8f703594491adb24031e0179f29301ad Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Sat, 16 Oct 2021 10:52:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ewebrtc=20echo=20test=E5=8F=8C?= =?UTF-8?q?=E5=90=91=E4=BC=9A=E8=AF=9D=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 9 +++++++++ webrtc/WebRtcEchoTest.cpp | 38 ++++++++++++++++++++++++++++++++++++++ webrtc/WebRtcEchoTest.h | 38 ++++++++++++++++++++++++++++++++++++++ webrtc/WebRtcPlayer.h | 2 +- webrtc/WebRtcTransport.cpp | 8 ++++++++ webrtc/WebRtcTransport.h | 1 + 6 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 webrtc/WebRtcEchoTest.cpp create mode 100644 webrtc/WebRtcEchoTest.h diff --git a/server/WebApi.cpp b/server/WebApi.cpp index eb78f713..d5bb071d 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -40,6 +40,7 @@ #ifdef ENABLE_WEBRTC #include "../webrtc/WebRtcPlayer.h" #include "../webrtc/WebRtcPusher.h" +#include "../webrtc/WebRtcEchoTest.h" #endif using namespace toolkit; @@ -1271,6 +1272,14 @@ void installWebApi() { return; } + if (!strcasecmp(type.data(), "echo")) { + auto rtc = WebRtcEchoTest::create(EventPollerPool::Instance().getPoller()); + val["sdp"] = rtc->getAnswerSdp(offer_sdp); + val["type"] = "answer"; + invoker(200, headerOut, val.toStyledString()); + return; + } + throw ApiRetException("不支持该类型", API::InvalidArgs); }); #endif diff --git a/webrtc/WebRtcEchoTest.cpp b/webrtc/WebRtcEchoTest.cpp new file mode 100644 index 00000000..9293d369 --- /dev/null +++ b/webrtc/WebRtcEchoTest.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. + * + * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit). + * + * Use of this source code is governed by MIT license that can be found in the + * LICENSE file in the root of the source tree. All contributing project authors + * may be found in the AUTHORS file in the root of the source tree. + */ + +#include "WebRtcEchoTest.h" + + +WebRtcEchoTest::Ptr WebRtcEchoTest::create(const EventPoller::Ptr &poller) { + WebRtcEchoTest::Ptr ret(new WebRtcEchoTest(poller), [](WebRtcEchoTest *ptr) { + ptr->onDestory(); + delete ptr; + }); + ret->onCreate(); + return ret; +} + +WebRtcEchoTest::WebRtcEchoTest(const EventPoller::Ptr &poller) : WebRtcTransportImp(poller) {} + +void WebRtcEchoTest::onRtcConfigure(RtcConfigure &configure) const { + WebRtcTransportImp::onRtcConfigure(configure); + configure.audio.direction = configure.video.direction = RtpDirection::sendrecv; +} + +void WebRtcEchoTest::onRtp(const char *buf, size_t len, uint64_t stamp_ms) { + updateTicker(); + sendRtpPacket(buf, len, true, nullptr); +} + +void WebRtcEchoTest::onRtcp(const char *buf, size_t len) { + sendRtcpPacket(buf, len, true, nullptr); +} + diff --git a/webrtc/WebRtcEchoTest.h b/webrtc/WebRtcEchoTest.h new file mode 100644 index 00000000..06ca2dee --- /dev/null +++ b/webrtc/WebRtcEchoTest.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. + * + * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit). + * + * Use of this source code is governed by MIT license that can be found in the + * LICENSE file in the root of the source tree. All contributing project authors + * may be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef ZLMEDIAKIT_WEBRTCECHOTEST_H +#define ZLMEDIAKIT_WEBRTCECHOTEST_H + +#include "WebRtcTransport.h" + +class WebRtcEchoTest : public WebRtcTransportImp { +public: + using Ptr = std::shared_ptr; + ~WebRtcEchoTest() override = default; + static Ptr create(const EventPoller::Ptr &poller); + +protected: + ///////WebRtcTransportImp override/////// + void onRtcConfigure(RtcConfigure &configure) const override; + void onRtp(const char *buf, size_t len, uint64_t stamp_ms) override; + void onRtcp(const char *buf, size_t len) override; + + void onRecvRtp(MediaTrack &track, const string &rid, RtpPacket::Ptr rtp) override {}; + void onBeforeEncryptRtp(const char *buf, int &len, void *ctx) override {}; + void onBeforeEncryptRtcp(const char *buf, int &len, void *ctx) override {}; + +private: + WebRtcEchoTest(const EventPoller::Ptr &poller); + +}; + + +#endif //ZLMEDIAKIT_WEBRTCECHOTEST_H diff --git a/webrtc/WebRtcPlayer.h b/webrtc/WebRtcPlayer.h index 4ca5eb75..ade67388 100644 --- a/webrtc/WebRtcPlayer.h +++ b/webrtc/WebRtcPlayer.h @@ -24,7 +24,7 @@ protected: void onStartWebRTC() override; void onDestory() override; void onRtcConfigure(RtcConfigure &configure) const override; - void onRecvRtp(MediaTrack &track, const string &rid, RtpPacket::Ptr rtp) {}; + void onRecvRtp(MediaTrack &track, const string &rid, RtpPacket::Ptr rtp) override {}; private: WebRtcPlayer(const EventPoller::Ptr &poller, const RtspMediaSource::Ptr &src, const MediaInfo &info); diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 130ff0f3..38cc1aff 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -435,6 +435,10 @@ void WebRtcTransportImp::onCheckAnswer(RtcSession &sdp) { if (m.type == TrackApplication) { continue; } + if (!m.rtp_rtx_ssrc.empty()) { + //已经生成了ssrc + continue; + } //添加answer sdp的ssrc信息 m.rtp_rtx_ssrc.emplace_back(); auto &ssrc = m.rtp_rtx_ssrc.back(); @@ -668,6 +672,10 @@ void WebRtcTransportImp::createRtpChannel(const string &rid, uint32_t ssrc, Medi InfoL << "create rtp receiver of ssrc:" << ssrc << ", rid:" << rid << ", codec:" << track.plan_rtp->codec; } +void WebRtcTransportImp::updateTicker() { + _alive_ticker.resetTime(); +} + void WebRtcTransportImp::onRtp(const char *buf, size_t len, uint64_t stamp_ms) { _bytes_usage += len; _alive_ticker.resetTime(); diff --git a/webrtc/WebRtcTransport.h b/webrtc/WebRtcTransport.h index 02c4cb0d..f5153e1f 100644 --- a/webrtc/WebRtcTransport.h +++ b/webrtc/WebRtcTransport.h @@ -189,6 +189,7 @@ protected: void onDestory() override; void onShutdown(const SockException &ex) override; virtual void onRecvRtp(MediaTrack &track, const string &rid, RtpPacket::Ptr rtp) = 0; + void updateTicker(); private: SdpAttrCandidate::Ptr getIceCandidate() const;