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;