新增webrtc echo test双向会话示例

This commit is contained in:
ziyue 2021-10-16 10:52:28 +08:00
parent f0e896a5e2
commit 34365a2f8f
6 changed files with 95 additions and 1 deletions

View File

@ -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

38
webrtc/WebRtcEchoTest.cpp Normal file
View File

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

38
webrtc/WebRtcEchoTest.h Normal file
View File

@ -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>;
~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

View File

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

View File

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

View File

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