Files
ZLMediaKit/webrtc/WebRtcProxyPusher.cpp
baigao-X 3fb43c5fef feat: 增加webrtc代理拉流 (#4389)
- 增加客户端模式,支持主动拉流、推流:
   - addStreamProxy接口新增支持whep主动拉流,拉流地址目前只兼容zlm的whep url。
   - addStreamPusherProxy接口新增支持whip主动推流,推流地址目前只兼容zlm的whip url。
   - 以上推流url格式为webrtc[s]://server_host:server_port/app/stream_id?key=value, 内部会自动转换为http[s]://server_host:server_port/index/api/[whip/whep]?app=app&stream=stream_id&key=value。

- 增加WebRtc p2p 模式:
  - 增加 ICE FULL模式。
  - 增加STUN/TURN 服务器。
  - 增加websocket 信令。
  - 增加P2P代理拉流。

---------

Co-authored-by: xia-chu <771730766@qq.com>
Co-authored-by: mtdxc <mtdxc@126.com>
Co-authored-by: cqm <cqm@97kid.com>
2025-09-20 16:23:30 +08:00

93 lines
2.6 KiB
C++
Executable File

/*
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
*
* Use of this source code is governed by MIT-like 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 "WebRtcProxyPusher.h"
#include "Common/config.h"
#include "Http/HlsPlayer.h"
#include "Rtsp/RtspMediaSourceImp.h"
#include "WebRtcPlayer.h"
using namespace toolkit;
using namespace std;
namespace mediakit {
WebRtcProxyPusher::WebRtcProxyPusher(const EventPoller::Ptr &poller, const RtspMediaSource::Ptr &src)
: WebRtcClient(poller) {
_push_src = src;
DebugL;
}
WebRtcProxyPusher::~WebRtcProxyPusher(void) {
teardown();
DebugL;
}
void WebRtcProxyPusher::publish(const string &strUrl) {
DebugL;
try {
_url.parse(strUrl, isPlayer());
} catch (std::exception &ex) {
onResult(SockException(Err_other, StrPrinter << "illegal webrtc url:" << ex.what()));
return;
}
startConnect();
}
void WebRtcProxyPusher::teardown() {
DebugL;
_transport = nullptr;
}
void WebRtcProxyPusher::onResult(const SockException &ex) {
DebugL << ex;
if (!ex) {
onPublishResult(ex);
} else {
if (!_is_negotiate_finished) {
onPublishResult(ex);
} else {
onShutdown(ex);
}
}
}
float WebRtcProxyPusher::getTimeOutSec() {
auto timeoutMS = (*this)[Client::kTimeoutMS].as<uint64_t>();
return (float)timeoutMS / (float)1000;
}
void WebRtcProxyPusher::startConnect() {
DebugL;
MediaInfo info(_url._full_url);
info.schema = "rtc";
auto src = _push_src.lock();
if (!src) {
onResult(SockException(Err_other, "media source released"));
return;
}
std::weak_ptr<WebRtcProxyPusher> weak_self = std::static_pointer_cast<WebRtcProxyPusher>(shared_from_this());
_transport = WebRtcPlayer::create(getPoller(), src, info, WebRtcTransport::Role::CLIENT, _url._signaling_protocols);
_transport->setOnShutdown([weak_self](const SockException &ex) {
if (auto strong_self = weak_self.lock()) {
strong_self->onResult(ex);
}
});
_transport->setOnStartWebRTC([weak_self]() {
if (auto strong_self = weak_self.lock()) {
strong_self->onResult(SockException());
}
});
WebRtcClient::startConnect();
}
} /* namespace mediakit */