c sdk 新增webrtc相关函数 (#4473)
Some checks failed
Android / build (push) Has been cancelled
CodeQL / Analyze (cpp) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Docker / build (push) Has been cancelled
Linux / build (push) Has been cancelled
macOS / build (push) Has been cancelled
Windows / build (push) Has been cancelled

另外调整函数位置,whip、whep请求设置Content-Type为application/sdp
This commit is contained in:
Lidaofu 2025-09-24 17:40:43 +08:00 committed by GitHub
parent a3eb07adfc
commit 493714bc7d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 355 additions and 57 deletions

View File

@ -259,31 +259,24 @@ API_EXPORT uint16_t API_CALL mk_rtp_server_start(uint16_t port);
*/
API_EXPORT uint16_t API_CALL mk_rtc_server_start(uint16_t port);
// 获取webrtc answer sdp回调函数 [AUTO-TRANSLATED:10c93fa9]
// Get webrtc answer sdp callback function
typedef void(API_CALL *on_mk_webrtc_get_answer_sdp)(void *user_data, const char *answer, const char *err);
/**
* webrtc交换sdpoffer sdp生成answer sdp
* @param user_data
* @param cb
* @param type webrtc插件类型echo,play,push
* @param offer webrtc offer sdp
* @param url rtc url, rtc://__defaultVhost/app/stream?key1=val1&key2=val2
* webrtc exchange sdp, generate answer sdp based on offer sdp
* @param user_data Callback user pointer
* @param cb Callback function
* @param type webrtc plugin type, supports echo, play, push
* @param offer webrtc offer sdp
* @param url rtc url, for example rtc://__defaultVhost/app/stream?key1=val1&key2=val2
* [AUTO-TRANSLATED:ea79659b]
* websocket[s]
* @param port websocket监听端口
* @param ssl ssl类型服务器
* @return 0:,0:
*
*/
API_EXPORT void API_CALL mk_webrtc_get_answer_sdp(void *user_data, on_mk_webrtc_get_answer_sdp cb, const char *type,
const char *offer, const char *url);
API_EXPORT uint16_t API_CALL mk_signaling_server_start(uint16_t port, int ssl);
/**
* webrtc-ice[s]
* @param port websocket监听端口
* @return 0:,0:
*
*/
API_EXPORT uint16_t API_CALL mk_ice_server_start(uint16_t port);
API_EXPORT void API_CALL mk_webrtc_get_answer_sdp2(void *user_data, on_user_data_free user_data_free, on_mk_webrtc_get_answer_sdp cb, const char *type,
const char *offer, const char *url);
/**
* srt服务器

View File

@ -27,5 +27,6 @@
#include "mk_frame.h"
#include "mk_track.h"
#include "mk_transcode.h"
#include "mk_webrtc.h"
#endif /* MK_API_H_ */

111
api/include/mk_webrtc.h Normal file
View File

@ -0,0 +1,111 @@
/*
* 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.
*/
#ifndef MK_WEBRTC_H
#define MK_WEBRTC_H
#include "mk_common.h"
#include "mk_proxyplayer.h"
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
// 获取webrtc answer sdp回调函数 [AUTO-TRANSLATED:10c93fa9]
// Get webrtc answer sdp callback function
typedef void(API_CALL *on_mk_webrtc_get_answer_sdp)(void *user_data, const char *answer, const char *err);
// 获取webrtc proxy player信息回调函数
typedef void(API_CALL *on_mk_webrtc_get_proxy_player_info_cb)(const char *info_json, const char *err);
//WebRTC-注册到信令服务器、WebRTC-从信令服务器注销回调函数
typedef void(API_CALL *on_mk_webrtc_room_keeper_info_cb)(void *user_data, const char *room_key, const char *err);
//获取WebRTC-Peer查看注册信息、WebRTC-信令服务器查看注册信息回调函数
typedef void(API_CALL *on_mk_webrtc_room_keeper_data_cb)(const char *data);
/**
* webrtc交换sdpoffer sdp生成answer sdp
* @param user_data
* @param cb
* @param type webrtc插件类型echo,play,push
* @param offer webrtc offer sdp
* @param url rtc url, rtc://__defaultVhost/app/stream?key1=val1&key2=val2
* webrtc exchange sdp, generate answer sdp based on offer sdp
* @param user_data Callback user pointer
* @param cb Callback function
* @param type webrtc plugin type, supports echo, play, push
* @param offer webrtc offer sdp
* @param url rtc url, for example rtc://__defaultVhost/app/stream?key1=val1&key2=val2
* [AUTO-TRANSLATED:ea79659b]
*/
API_EXPORT void API_CALL mk_webrtc_get_answer_sdp(void *user_data, on_mk_webrtc_get_answer_sdp cb, const char *type, const char *offer, const char *url);
API_EXPORT void API_CALL mk_webrtc_get_answer_sdp2(
void *user_data, on_user_data_free user_data_free, on_mk_webrtc_get_answer_sdp cb, const char *type, const char *offer, const char *url);
/**
* webrtc proxy player信息
* @param mk_proxy_player
* @param cb
*/
API_EXPORT void API_CALL mk_webrtc_get_proxy_player_info(mk_proxy_player ctx, on_mk_webrtc_get_proxy_player_info_cb cb);
/**
* WebRTC-
* @param server_host host
* @param server_port port
* @param room_id id
* @param ssl ssl
* @param cb
* @param user_data
*/
API_EXPORT void API_CALL
mk_webrtc_add_room_keeper(const char *room_id, const char *server_host, uint16_t server_port, int ssl, on_mk_webrtc_room_keeper_info_cb cb, void *user_data);
API_EXPORT void API_CALL mk_webrtc_add_room_keeper2(
const char *room_id, const char *server_host, uint16_t server_port, int ssl, on_mk_webrtc_room_keeper_info_cb cb, void *user_data,
on_user_data_free user_data_free);
/**
* WebRTC-
* @param room_key key
* @param cb
* @param user_data
*/
API_EXPORT void API_CALL mk_webrtc_del_room_keeper(const char *room_key, on_mk_webrtc_room_keeper_info_cb cb, void *user_data);
API_EXPORT void API_CALL
mk_webrtc_del_room_keeper2(const char *room_key, on_mk_webrtc_room_keeper_info_cb cb, void *user_data, on_user_data_free user_data_free);
/**
* WebRTC-Peer查看注册信息
* @param cb
*/
API_EXPORT void API_CALL mk_webrtc_list_room_keeper(on_mk_webrtc_room_keeper_data_cb cb);
/**
* WebRTC-
* @param cb
*/
API_EXPORT void API_CALL mk_webrtc_list_rooms(on_mk_webrtc_room_keeper_data_cb cb);
#ifdef __cplusplus
}
#endif
#endif /* MK_WEBRTC_H */

View File

@ -29,6 +29,7 @@ using namespace mediakit;
static TcpServer::Ptr rtsp_server[2];
static TcpServer::Ptr rtmp_server[2];
static TcpServer::Ptr http_server[2];
static TcpServer::Ptr signaling_server[2];
static TcpServer::Ptr shell_server;
#ifdef ENABLE_RTPPROXY
@ -37,9 +38,14 @@ static RtpServer::Ptr rtpServer;
#endif
#ifdef ENABLE_WEBRTC
#include "../webrtc/WebRtcSession.h"
#include "webrtc/WebRtcSession.h"
#include "webrtc/IceSession.hpp"
#include "webrtc/WebRtcSignalingSession.h"
#include "webrtc/WebRtcTransport.h"
static UdpServer::Ptr rtcServer_udp;
static TcpServer::Ptr rtcServer_tcp;
static UdpServer::Ptr iceServer_udp;
static TcpServer::Ptr iceServer_tcp;
#endif
#if defined(ENABLE_SRT)
@ -288,46 +294,45 @@ API_EXPORT uint16_t API_CALL mk_rtc_server_start(uint16_t port) {
#endif
}
#ifdef ENABLE_WEBRTC
class WebRtcArgsUrl : public mediakit::WebRtcArgs {
public:
WebRtcArgsUrl(std::string url) { _url = std::move(url); }
toolkit::variant operator[](const std::string &key) const override {
if (key == "url") {
return _url;
API_EXPORT uint16_t API_CALL mk_signaling_server_start(uint16_t port, int ssl) {
#ifdef ENABLE_WEBRTC
ssl = MAX(0, MIN(ssl, 1));
try {
signaling_server[ssl] = std::make_shared<TcpServer>();
if (ssl) {
signaling_server[ssl]->start<WebRtcWebcosktSignalSslSession>(port);
} else {
signaling_server[ssl]->start<WebRtcWebcosktSignalingSession>(port);
}
return "";
return signaling_server[ssl]->getPort();
} catch (std::exception &ex) {
signaling_server[ssl] = nullptr;
WarnL << ex.what();
return 0;
}
private:
std::string _url;
};
#endif
API_EXPORT void API_CALL mk_webrtc_get_answer_sdp(void *user_data, on_mk_webrtc_get_answer_sdp cb, const char *type,
const char *offer, const char *url) {
mk_webrtc_get_answer_sdp2(user_data, nullptr, cb, type, offer, url);
}
API_EXPORT void API_CALL mk_webrtc_get_answer_sdp2(void *user_data, on_user_data_free user_data_free, on_mk_webrtc_get_answer_sdp cb, const char *type,
const char *offer, const char *url) {
#ifdef ENABLE_WEBRTC
assert(type && offer && url && cb);
auto session = std::make_shared<HttpSession>(Socket::createSocket());
std::string offer_str = offer;
std::shared_ptr<void> ptr(user_data, user_data_free ? user_data_free : [](void *) {});
auto args = std::make_shared<WebRtcArgsUrl>(url);
WebRtcPluginManager::Instance().negotiateSdp(*session, type, *args, [offer_str, session, ptr, cb](const WebRtcInterface &exchanger) mutable {
auto &handler = const_cast<WebRtcInterface &>(exchanger);
try {
auto sdp_answer = handler.getAnswerSdp(offer_str);
cb(ptr.get(), sdp_answer.data(), nullptr);
} catch (std::exception &ex) {
cb(ptr.get(), nullptr, ex.what());
}
});
#else
WarnL << "未启用webrtc功能, 编译时请开启ENABLE_WEBRTC";
return 0;
#endif
}
API_EXPORT uint16_t API_CALL mk_ice_server_start(uint16_t port){
#ifdef ENABLE_WEBRTC
try {
iceServer_tcp = std::make_shared<TcpServer>();
iceServer_udp = std::make_shared<UdpServer>();
iceServer_udp->start<IceSession>(port);
iceServer_tcp->start<IceSession>(port);
} catch (std::exception &ex) {
iceServer_udp = nullptr;
iceServer_tcp = nullptr;
WarnL << ex.what();
return 0;
}
#else
WarnL << "未启用webrtc功能, 编译时请开启ENABLE_WEBRTC";
return 0;
#endif
}

187
api/source/mk_webrtc.cpp Normal file
View File

@ -0,0 +1,187 @@
/*
* 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 "mk_webrtc.h"
#include "mk_util.h"
#include <stdarg.h>
#include <unordered_map>
#include "Util/logger.h"
#include "Util/SSLBox.h"
#include "Util/File.h"
#include "Network/TcpServer.h"
#include "Network/UdpServer.h"
#include "Thread/WorkThreadPool.h"
#include "Rtsp/RtspSession.h"
#include "Rtmp/RtmpSession.h"
#include "Http/HttpSession.h"
#include "Shell/ShellSession.h"
#include "Player/PlayerProxy.h"
#include "webrtc/WebRtcProxyPlayer.h"
#include "webrtc/WebRtcProxyPlayerImp.h"
#include "../webrtc/WebRtcSignalingPeer.h"
#include "../webrtc/WebRtcSignalingSession.h"
using namespace std;
using namespace toolkit;
using namespace mediakit;
#ifdef ENABLE_WEBRTC
#include "../webrtc/WebRtcSession.h"
static UdpServer::Ptr rtcServer_udp;
static TcpServer::Ptr rtcServer_tcp;
class WebRtcArgsUrl : public mediakit::WebRtcArgs {
public:
WebRtcArgsUrl(std::string url) { _url = std::move(url); }
toolkit::variant operator[](const std::string &key) const override {
if (key == "url") {
return _url;
}
return "";
}
private:
std::string _url;
};
#endif
API_EXPORT void API_CALL mk_webrtc_get_answer_sdp(void *user_data, on_mk_webrtc_get_answer_sdp cb, const char *type, const char *offer, const char *url) {
mk_webrtc_get_answer_sdp2(user_data, nullptr, cb, type, offer, url);
}
API_EXPORT void API_CALL mk_webrtc_get_answer_sdp2(
void *user_data, on_user_data_free user_data_free, on_mk_webrtc_get_answer_sdp cb, const char *type, const char *offer, const char *url) {
#ifdef ENABLE_WEBRTC
assert(type && offer && url && cb);
auto session = std::make_shared<HttpSession>(Socket::createSocket());
std::string offer_str = offer;
std::shared_ptr<void> ptr(user_data, user_data_free ? user_data_free : [](void *) {});
auto args = std::make_shared<WebRtcArgsUrl>(url);
WebRtcPluginManager::Instance().negotiateSdp(*session, type, *args, [offer_str, session, ptr, cb](const WebRtcInterface &exchanger) mutable {
auto &handler = const_cast<WebRtcInterface &>(exchanger);
try {
auto sdp_answer = handler.getAnswerSdp(offer_str);
cb(ptr.get(), sdp_answer.data(), nullptr);
} catch (std::exception &ex) {
cb(ptr.get(), nullptr, ex.what());
}
});
#else
WarnL << "未启用webrtc功能, 编译时请开启ENABLE_WEBRTC";
#endif
}
API_EXPORT void API_CALL mk_webrtc_get_proxy_player_info(mk_proxy_player ctx, on_mk_webrtc_get_proxy_player_info_cb cb) {
#ifdef ENABLE_WEBRTC
assert(ctx && cb);
PlayerProxy::Ptr *obj = (PlayerProxy::Ptr *)ctx;
auto media_player = obj->get()->getDelegate();
if (!media_player) {
cb(nullptr, "Media player not found");
return;
}
auto webrtc_player_imp = std::dynamic_pointer_cast<WebRtcProxyPlayerImp>(media_player);
if (!webrtc_player_imp) {
cb(nullptr, "Stream proxy is not WebRTC type");
return;
}
auto webrtc_transport = webrtc_player_imp->getWebRtcTransport();
if (!webrtc_transport) {
cb(nullptr, "WebRTC transport not available");
return;
}
webrtc_transport->getTransportInfo([cb](Json::Value transport_info) mutable {
if (transport_info.isMember("error")) {
cb(nullptr, strdup(transport_info["error"].asCString()));
return;
}
cb(strdup(transport_info.toStyledString().c_str()), "");
});
#else
WarnL << "未启用webrtc功能, 编译时请开启ENABLE_WEBRTC";
#endif
}
API_EXPORT void API_CALL mk_webrtc_add_room_keeper(
const char *room_id, const char *server_host, uint16_t server_port, int ssl, on_mk_webrtc_room_keeper_info_cb cb, void *user_data) {
mk_webrtc_add_room_keeper2(room_id, server_host, server_port, ssl, cb, user_data, nullptr);
}
API_EXPORT void API_CALL mk_webrtc_add_room_keeper2(
const char *room_id, const char *server_host, uint16_t server_port, int ssl, on_mk_webrtc_room_keeper_info_cb cb, void *user_data,
on_user_data_free user_data_free) {
#ifdef ENABLE_WEBRTC
assert(server_host && server_port && room_id && cb);
// server_host: 信令服务器host
// server_post: 信令服务器host
// room_id: 注册的id,信令服务器会对该id进行唯一性检查
std::string server_host_str(server_host), room_id_str(room_id);
std::shared_ptr<void> ptr(user_data, user_data_free ? user_data_free : [](void *) {});
addWebrtcRoomKeeper(server_host_str, server_port, room_id_str, ssl, [ptr,cb](const SockException &ex, const string &key) mutable {
if (ex) {
cb(ptr.get(), nullptr, ex.what());
} else {
cb(ptr.get(), key.c_str(), nullptr);
}
});
#else
WarnL << "未启用webrtc功能, 编译时请开启ENABLE_WEBRTC";
#endif
}
API_EXPORT void API_CALL mk_webrtc_del_room_keeper(const char *room_key, on_mk_webrtc_room_keeper_info_cb cb, void *user_data) {
mk_webrtc_del_room_keeper2(room_key,cb,user_data,nullptr);
}
API_EXPORT void API_CALL
mk_webrtc_del_room_keeper2(const char *room_key, on_mk_webrtc_room_keeper_info_cb cb, void *user_data, on_user_data_free user_data_free) {
#ifdef ENABLE_WEBRTC
assert(room_key && cb);
std::string room_key_str(room_key);
std::shared_ptr<void> ptr(user_data, user_data_free ? user_data_free : [](void *) {});
delWebrtcRoomKeeper(room_key_str, [room_key_str, ptr, cb](const SockException &ex) mutable {
if (ex) {
cb(ptr.get(), room_key_str.c_str(), ex.what());
}
cb(ptr.get(), room_key_str.c_str(), nullptr);
});
#else
WarnL << "未启用webrtc功能, 编译时请开启ENABLE_WEBRTC";
#endif
}
API_EXPORT void API_CALL mk_webrtc_list_room_keeper(on_mk_webrtc_room_keeper_data_cb cb) {
#ifdef ENABLE_WEBRTC
assert(cb);
listWebrtcRoomKeepers([cb](const std::string &key, const WebRtcSignalingPeer::Ptr &p) {
Json::Value item = ToJson(p);
item["room_key"] = key;
cb(strdup(item.toStyledString().c_str()));
});
#else
WarnL << "未启用webrtc功能, 编译时请开启ENABLE_WEBRTC";
#endif
}
API_EXPORT void API_CALL mk_webrtc_list_rooms(on_mk_webrtc_room_keeper_data_cb cb){
#ifdef ENABLE_WEBRTC
assert(cb);
listWebrtcRooms([cb](const std::string &key, const WebRtcSignalingSession::Ptr &p) {
Json::Value item = ToJson(p);
item["room_id"] = key;
cb(strdup(item.toStyledString().c_str()));
});
#else
WarnL << "未启用webrtc功能, 编译时请开启ENABLE_WEBRTC";
#endif
}

View File

@ -154,6 +154,7 @@ void WebRtcClient::doNegotiateWhepOrWhip() {
_negotiate = make_shared<HttpRequester>();
_negotiate->setMethod("POST");
_negotiate->addHeader("Content-Type", "application/sdp");
_negotiate->setBody(std::move(offer_sdp));
_negotiate->startRequester(_url._negotiate_url, [weak_self](const toolkit::SockException &ex, const Parser &response) {
auto strong_self = weak_self.lock();