change srt streamid like srs srt streamid

This commit is contained in:
xiongguangjie 2022-06-11 21:37:41 +08:00
parent 4d6cff36b8
commit f8373302d0
5 changed files with 151 additions and 61 deletions

View File

@ -11,9 +11,9 @@
#ifndef ZLMEDIAKIT_MACROS_H #ifndef ZLMEDIAKIT_MACROS_H
#define ZLMEDIAKIT_MACROS_H #define ZLMEDIAKIT_MACROS_H
#include "Util/logger.h"
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include "Util/logger.h"
#if defined(__MACH__) #if defined(__MACH__)
#include <arpa/inet.h> #include <arpa/inet.h>
#include <machine/endian.h> #include <machine/endian.h>
@ -21,8 +21,8 @@
#define __BIG_ENDIAN BIG_ENDIAN #define __BIG_ENDIAN BIG_ENDIAN
#define __LITTLE_ENDIAN LITTLE_ENDIAN #define __LITTLE_ENDIAN LITTLE_ENDIAN
#elif defined(__linux__) #elif defined(__linux__)
#include <endian.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <endian.h>
#elif defined(_WIN32) #elif defined(_WIN32)
#define BIG_ENDIAN 1 #define BIG_ENDIAN 1
#define LITTLE_ENDIAN 0 #define LITTLE_ENDIAN 0
@ -53,7 +53,10 @@
#endif // MIN #endif // MIN
#ifndef CLEAR_ARR #ifndef CLEAR_ARR
#define CLEAR_ARR(arr) for(auto &item : arr){ item = 0;} #define CLEAR_ARR(arr) \
for (auto &item : arr) { \
item = 0; \
}
#endif // CLEAR_ARR #endif // CLEAR_ARR
#define VHOST_KEY "vhost" #define VHOST_KEY "vhost"
@ -64,6 +67,7 @@
#define HLS_SCHEMA "hls" #define HLS_SCHEMA "hls"
#define TS_SCHEMA "ts" #define TS_SCHEMA "ts"
#define FMP4_SCHEMA "fmp4" #define FMP4_SCHEMA "fmp4"
#define SRT_SCHEMA "srt"
#define DEFAULT_VHOST "__defaultVhost__" #define DEFAULT_VHOST "__defaultVhost__"
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -1,10 +1,11 @@
#include <memory> #include "Util/util.h"
#include "Util/util.h" #include <memory>
#include "SrtTransportImp.hpp" #include "SrtTransportImp.hpp"
namespace SRT { namespace SRT {
SrtTransportImp::SrtTransportImp(const EventPoller::Ptr &poller) : SrtTransport(poller) {} SrtTransportImp::SrtTransportImp(const EventPoller::Ptr &poller)
: SrtTransport(poller) {}
SrtTransportImp::~SrtTransportImp() { SrtTransportImp::~SrtTransportImp() {
InfoP(this); InfoP(this);
@ -28,15 +29,13 @@ void SrtTransportImp::onHandShakeFinished(std::string &streamid, struct sockaddr
} }
_is_pusher = false; _is_pusher = false;
TraceL << " stream id " << streamid; TraceL << " stream id " << streamid;
if (streamid.empty()) { if (!parseStreamid(streamid)) {
onShutdown(SockException(Err_shutdown, "stream id not empty")); onShutdown(SockException(Err_shutdown, "stream id not vaild"));
return; return;
} }
_media_info.parse("srt://" + streamid);
auto params = Parser::parseArgs(_media_info._param_strs); auto params = Parser::parseArgs(_media_info._param_strs);
if (params["type"] == "push") { if (params["m"] == "publish") {
_is_pusher = true; _is_pusher = true;
_decoder = DecoderImp::createDecoder(DecoderImp::decoder_ts, this); _decoder = DecoderImp::createDecoder(DecoderImp::decoder_ts, this);
emitOnPublish(); emitOnPublish();
@ -46,6 +45,56 @@ void SrtTransportImp::onHandShakeFinished(std::string &streamid, struct sockaddr
} }
} }
//
bool SrtTransportImp::parseStreamid(std::string &streamid) {
if (!toolkit::start_with(streamid, "#!::")) {
return false;
}
_media_info._schema = SRT_SCHEMA;
std::string real_streamid = streamid.substr(4);
std::string vhost, app, stream_name;
auto params = Parser::parseArgs(real_streamid, ",", "=");
for (auto it : params) {
if (it.first == "h") {
vhost = it.second;
} else if (it.first == "r") {
auto tmps = toolkit::split(it.second, "/");
if (tmps.size() < 2) {
continue;
}
app = tmps[0];
stream_name = tmps[1];
} else {
if (_media_info._param_strs.empty()) {
_media_info._param_strs = it.first + "=" + it.second;
} else {
_media_info._param_strs += "&" + it.first + "=" + it.second;
}
}
}
if (app == "" || stream_name == "") {
return false;
}
if (vhost != "") {
_media_info._vhost = vhost;
} else {
_media_info._vhost = DEFAULT_VHOST;
}
_media_info._app = app;
_media_info._streamid = stream_name;
TraceL << " vhost=" << _media_info._vhost << " app=" << _media_info._app << " streamid=" << _media_info._streamid
<< " params=" << _media_info._param_strs;
return true;
}
void SrtTransportImp::onSRTData(DataPacket::Ptr pkt) { void SrtTransportImp::onSRTData(DataPacket::Ptr pkt) {
if (!_is_pusher) { if (!_is_pusher) {
WarnP(this) << "this is a player data ignore"; WarnP(this) << "this is a player data ignore";
@ -66,10 +115,8 @@ bool SrtTransportImp::close(mediakit::MediaSource &sender, bool force) {
if (!force && totalReaderCount(sender)) { if (!force && totalReaderCount(sender)) {
return false; return false;
} }
std::string err = StrPrinter << "close media:" << sender.getSchema() << "/" std::string err = StrPrinter << "close media:" << sender.getSchema() << "/" << sender.getVhost() << "/"
<< sender.getVhost() << "/" << sender.getApp() << "/" << sender.getId() << " " << force;
<< sender.getApp() << "/"
<< sender.getId() << " " << force;
weak_ptr<SrtTransportImp> weak_self = static_pointer_cast<SrtTransportImp>(shared_from_this()); weak_ptr<SrtTransportImp> weak_self = static_pointer_cast<SrtTransportImp>(shared_from_this());
getPoller()->async([weak_self, err]() { getPoller()->async([weak_self, err]() {
auto strong_self = weak_self.lock(); auto strong_self = weak_self.lock();

View File

@ -1,11 +1,11 @@
#ifndef ZLMEDIAKIT_SRT_TRANSPORT_IMP_H #ifndef ZLMEDIAKIT_SRT_TRANSPORT_IMP_H
#define ZLMEDIAKIT_SRT_TRANSPORT_IMP_H #define ZLMEDIAKIT_SRT_TRANSPORT_IMP_H
#include <mutex> #include "Common/MultiMediaSourceMuxer.h"
#include "Rtp/Decoder.h" #include "Rtp/Decoder.h"
#include "SrtTransport.hpp" #include "SrtTransport.hpp"
#include "TS/TSMediaSource.h" #include "TS/TSMediaSource.h"
#include "Common/MultiMediaSourceMuxer.h" #include <mutex>
namespace SRT { namespace SRT {
@ -66,6 +66,7 @@ protected:
bool inputFrame(const Frame::Ptr &frame) override; bool inputFrame(const Frame::Ptr &frame) override;
private: private:
bool parseStreamid(std::string &streamid);
void emitOnPublish(); void emitOnPublish();
void emitOnPlay(); void emitOnPlay();

View File

@ -9,17 +9,36 @@
## 使用 ## 使用
zlm中的srt更加streamid 来确定是推流还是拉流来确定vhost,app,streamid(ZLM中的) zlm中的srt根据streamid 来确定是推流还是拉流来确定vhost,app,streamid(ZLM中的)、
srt中的streamid 为 `<vhost>/<app>/<streamid>?type=<push|play>& <other>=<other>`
srt中的streamid 为 `#!::key1=value1,key2=value2,key3=value4......`
h,r为特殊的key,来确定vhost,app,streamid,如果没有h则vhost为默认值
m 为特殊key来确定是推流还是拉流如果为publish 则为推流,否则为拉流 ,如果不存在m,则默认为拉流
其他key与m会作为webhook的鉴权参数
如:
#!::h=zlmediakit.com,r=live/test,m=publish
vhost = zlmediakit.com
app = live
streamid = test
是推流
- OBS 推流地址 - OBS 推流地址
`srt://192.168.1.105:9000?streamid=__defaultVhost__/live/test?type=push` `srt://192.168.1.105:9000?streamid=#!::r=live/test,m=publish`
- ffmpeg 推流 - ffmpeg 推流
`ffmpeg -re -stream_loop -1 -i test.ts -c:v copy -c:a copy -f mpegts srt://192.168.1.105:9000?streamid="__defaultVhost__/live/test?type=push"` `ffmpeg -re -stream_loop -1 -i test.ts -c:v copy -c:a copy -f mpegts srt://192.168.1.105:9000?streamid=#!::r=live/test,m=publish`
- ffplay 拉流 - ffplay 拉流
`ffplay -i srt://192.168.1.105:9000?streamid=__defaultVhost__/live/test` `ffplay -i srt://192.168.1.105:9000?streamid=#!::r=live/test`
- vlc 不支持,因为无法指定streamid[参考](https://github.com/Haivision/srt/issues/1015) - vlc 不支持,因为无法指定streamid[参考](https://github.com/Haivision/srt/issues/1015)

View File

@ -9,16 +9,35 @@
## usage ## usage
zlm get vhost,app,streamid and push or play by streamid of srt like this `<vhost>/<app>/<streamid>?type=<push|play>& <other>=<other>` zlm get vhost,app,streamid and push or play by streamid of srt like this
`#!::key1=value1,key2=value2,key3=value4......`
h and r is special key,to get vhost app streamid, if h not exist ,vhost is default value
m is special key, to judge is push or pull, if vaule is publish the mode is pushotherwise is play, if m not exist, mode is play
other key and m ,can use by webhook to auth for play or push
like
#!::h=zlmediakit.com,r=live/test,m=publish
vhost = zlmediakit.com
app = live
streamid = test
mode is push
- OBS push stream url - OBS push stream url
`srt://192.168.1.105:9000?streamid=__defaultVhost__/live/test?type=push` `srt://192.168.1.105:9000?streamid=#!::r=live/test,m=publish`
- ffmpeg push - ffmpeg push
`ffmpeg -re -stream_loop -1 -i test.ts -c:v copy -c:a copy -f mpegts srt://192.168.1.105:9000?streamid="__defaultVhost__/live/test?type=push"` `ffmpeg -re -stream_loop -1 -i test.ts -c:v copy -c:a copy -f mpegts srt://192.168.1.105:9000?streamid=#!::r=live/test,m=publish`
- ffplay pull - ffplay pull
`ffplay -i srt://192.168.1.105:9000?streamid=__defaultVhost__/live/test` `ffplay -i srt://192.168.1.105:9000?streamid=#!::r=live/test`
- vlc not support ,because can't set stream id [reference](https://github.com/Haivision/srt/issues/1015) - vlc not support ,because can't set stream id [reference](https://github.com/Haivision/srt/issues/1015)