Skip to content

Commit 1c8ed1c

Browse files
Add srt caller mode and stream encryption support. (ZLMediaKit#4088)
Add srt caller mode and stream encryption support. 1. Support srt caller mode, realize srt proxy pull stream proxy push stream; url parameter format such as: srt://127.0.0.1:9000?streamid=#! ::r=live/test11 2. Support srt stream encrypted transmission in caller and listener mode. --------- Co-authored-by: xiongguangjie <xiong_panda@163.com>
1 parent cb4db80 commit 1c8ed1c

27 files changed

+3002
-17
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ if(ENABLE_VIDEOSTACK)
463463
endif ()
464464
endif ()
465465

466+
if(ENABLE_SRT)
467+
update_cached_list(MK_COMPILE_DEFINITIONS ENABLE_SRT)
468+
endif()
469+
466470
# ----------------------------------------------------------------------------
467471
# Solution folders:
468472
# ----------------------------------------------------------------------------

conf/config.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ port=9000
400400
latencyMul=4
401401
#包缓存的大小
402402
pktBufSize=8192
403+
#srt udp服务器的密码,为空表示不加密
404+
passPhrase=
403405

404406

405407
[rtsp]

postman/ZLMediaKit.postman_collection.json

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@
522522
"response": []
523523
},
524524
{
525-
"name": "添加rtsp/rtmp/hls拉流代理(addStreamProxy)",
525+
"name": "添加rtsp/rtmp/hls/srt拉流代理(addStreamProxy)",
526526
"request": {
527527
"method": "GET",
528528
"header": [],
@@ -663,7 +663,19 @@
663663
"value": null,
664664
"description": "无人观看时,是否直接关闭(而不是通过on_none_reader hook返回close)",
665665
"disabled": true
666-
}
666+
},
667+
{
668+
"key": "latency",
669+
"value": null,
670+
"description": "srt延时, 单位毫秒",
671+
"disabled": true
672+
},
673+
{
674+
"key": "passphrase",
675+
"value": null,
676+
"description": "srt拉流的密码",
677+
"disabled": true
678+
}
667679
]
668680
}
669681
},
@@ -753,7 +765,7 @@
753765
"response": []
754766
},
755767
{
756-
"name": "添加rtsp/rtmp推流(addStreamPusherProxy)",
768+
"name": "添加rtsp/rtmp/srt推流(addStreamPusherProxy)",
757769
"request": {
758770
"method": "GET",
759771
"header": [],
@@ -815,7 +827,20 @@
815827
"value": null,
816828
"description": "推流重试次数,不传此参数或传值<=0时,则无限重试",
817829
"disabled": true
818-
}
830+
},
831+
{
832+
"key": "latency",
833+
"value": null,
834+
"description": "srt延时, 单位毫秒",
835+
"disabled": true
836+
},
837+
{
838+
"key": "passphrase",
839+
"value": null,
840+
"description": "srt推流的密码",
841+
"disabled": true
842+
}
843+
819844
]
820845
}
821846
},
@@ -2610,4 +2635,4 @@
26102635
"value": "__defaultVhost__"
26112636
}
26122637
]
2613-
}
2638+
}

server/WebApi.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ void addStreamPusherProxy(const string &schema,
685685
int retry_count,
686686
int rtp_type,
687687
float timeout_sec,
688+
const mINI &args,
688689
const function<void(const SockException &ex, const string &key)> &cb) {
689690
auto key = getPusherKey(schema, vhost, app, stream, url);
690691
auto src = MediaSource::find(schema, vhost, app, stream);
@@ -703,14 +704,20 @@ void addStreamPusherProxy(const string &schema,
703704
// Add push stream proxy
704705
auto pusher = s_pusher_proxy.make(key, src, retry_count);
705706

707+
// 先透传拷贝参数 [AUTO-TRANSLATED:22b5605e]
708+
// First pass-through copy parameters
709+
for (auto &pr : args) {
710+
(*pusher)[pr.first] = pr.second;
711+
}
712+
706713
// 指定RTP over TCP(播放rtsp时有效) [AUTO-TRANSLATED:1a062656]
707714
// Specify RTP over TCP (effective when playing RTSP)
708-
pusher->emplace(Client::kRtpType, rtp_type);
715+
(*pusher)[Client::kRtpType] = rtp_type;
709716

710717
if (timeout_sec > 0.1f) {
711718
// 推流握手超时时间 [AUTO-TRANSLATED:00762fc1]
712719
// Push stream handshake timeout
713-
pusher->emplace(Client::kTimeoutMS, timeout_sec * 1000);
720+
(*pusher)[Client::kTimeoutMS] = timeout_sec * 1000;
714721
}
715722

716723
// 开始推流,如果推流失败或者推流中止,将会自动重试若干次,默认一直重试 [AUTO-TRANSLATED:c8b95088]
@@ -1174,6 +1181,12 @@ void installWebApi() {
11741181
api_regist("/index/api/addStreamPusherProxy", [](API_ARGS_MAP_ASYNC) {
11751182
CHECK_SECRET();
11761183
CHECK_ARGS("schema", "vhost", "app", "stream", "dst_url");
1184+
1185+
mINI args;
1186+
for (auto &pr : allArgs.args) {
1187+
args.emplace(pr.first, pr.second);
1188+
}
1189+
11771190
auto dst_url = allArgs["dst_url"];
11781191
auto retry_count = allArgs["retry_count"].empty() ? -1 : allArgs["retry_count"].as<int>();
11791192
addStreamPusherProxy(allArgs["schema"],
@@ -1184,6 +1197,7 @@ void installWebApi() {
11841197
retry_count,
11851198
allArgs["rtp_type"],
11861199
allArgs["timeout_sec"],
1200+
args,
11871201
[invoker, val, headerOut, dst_url](const SockException &ex, const string &key) mutable {
11881202
if (ex) {
11891203
val["code"] = API::OtherFailed;

src/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ file(GLOB MediaKit_SRC_LIST
2626
${CMAKE_CURRENT_SOURCE_DIR}/*/*.cpp
2727
${CMAKE_CURRENT_SOURCE_DIR}/*/*.h)
2828

29+
if(NOT ENABLE_SRT)
30+
file(GLOB SRT_SRC_LIST
31+
${CMAKE_CURRENT_SOURCE_DIR}/Srt/*.c
32+
${CMAKE_CURRENT_SOURCE_DIR}/Srt/*.cpp
33+
${CMAKE_CURRENT_SOURCE_DIR}/Srt/*.h)
34+
list(REMOVE_ITEM MediaKit_SRC_LIST ${SRT_SRC_LIST})
35+
endif()
36+
2937
if(USE_SOLUTION_FOLDERS AND (NOT GROUP_BY_EXPLORER))
3038
# 在 IDE 中对文件进行分组, 源文件和头文件分开
3139
set_file_group("${CMAKE_CURRENT_SOURCE_DIR}" ${MediaKit_SRC_LIST})
@@ -49,6 +57,7 @@ target_link_libraries(zlmediakit
4957
target_include_directories(zlmediakit
5058
PRIVATE
5159
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
60+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>"
5261
PUBLIC
5362
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>")
5463

src/Common/config.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ const string kWaitTrackReady = "wait_track_ready";
396396
const string kPlayTrack = "play_track";
397397
const string kProxyUrl = "proxy_url";
398398
const string kRtspSpeed = "rtsp_speed";
399+
const string kLatency = "latency";
400+
const string kPassPhrase = "passPhrase";
399401
} // namespace Client
400402

401403
} // namespace mediakit

src/Common/config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,10 @@ extern const std::string kProxyUrl;
624624
// 设置开始rtsp倍速播放 [AUTO-TRANSLATED:5db03cad]
625625
// Set the start RTSP playback speed
626626
extern const std::string kRtspSpeed;
627+
// Set SRT delay
628+
extern const std::string kLatency;
629+
// Set SRT PassPhrase
630+
extern const std::string kPassPhrase;
627631
} // namespace Client
628632
} // namespace mediakit
629633

src/Player/PlayerBase.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include "Rtmp/FlvPlayer.h"
1616
#include "Http/HlsPlayer.h"
1717
#include "Http/TsPlayerImp.h"
18+
#ifdef ENABLE_SRT
19+
#include "Srt/SrtPlayerImp.h"
20+
#endif // ENABLE_SRT
1821

1922
using namespace std;
2023
using namespace toolkit;
@@ -70,6 +73,12 @@ PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &in_poller, cons
7073
}
7174
}
7275

76+
#ifdef ENABLE_SRT
77+
if (strcasecmp("srt", prefix.data()) == 0) {
78+
return PlayerBase::Ptr(new SrtPlayerImp(poller), release_func);
79+
}
80+
#endif//ENABLE_SRT
81+
7382
throw std::invalid_argument("not supported play schema:" + url_in);
7483
}
7584

@@ -78,6 +87,8 @@ PlayerBase::PlayerBase() {
7887
this->mINI::operator[](Client::kMediaTimeoutMS) = 5000;
7988
this->mINI::operator[](Client::kBeatIntervalMS) = 5000;
8089
this->mINI::operator[](Client::kWaitTrackReady) = true;
90+
this->mINI::operator[](Client::kLatency) = 0;
91+
this->mINI::operator[](Client::kPassPhrase) = "";
8192
}
8293

8394
} /* namespace mediakit */

src/Pusher/PusherBase.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include "PusherBase.h"
1313
#include "Rtsp/RtspPusher.h"
1414
#include "Rtmp/RtmpPusher.h"
15+
#ifdef ENABLE_SRT
16+
#include "Srt/SrtPusher.h"
17+
#endif // ENABLE_SRT
1518

1619
using namespace toolkit;
1720

@@ -50,6 +53,13 @@ PusherBase::Ptr PusherBase::createPusher(const EventPoller::Ptr &in_poller,
5053
return PusherBase::Ptr(new RtmpPusherImp(poller, std::dynamic_pointer_cast<RtmpMediaSource>(src)), release_func);
5154
}
5255

56+
#ifdef ENABLE_SRT
57+
if (strcasecmp("srt", prefix.data()) == 0) {
58+
return PusherBase::Ptr(new SrtPusherImp(poller, std::dynamic_pointer_cast<TSMediaSource>(src)), release_func);
59+
}
60+
#endif//ENABLE_SRT
61+
62+
5363
throw std::invalid_argument("not supported push schema:" + url);
5464
}
5565

0 commit comments

Comments
 (0)