mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-03-25 07:20:53 +08:00
Merge branch 'master' into dev
This commit is contained in:
Submodule 3rdpart/ZLToolKit updated: e5535a7164...04d1c47d25
Submodule 3rdpart/media-server updated: a8a80e0738...8bc32a516b
6
AUTHORS
6
AUTHORS
@@ -98,3 +98,9 @@ WuPeng <wp@zafu.edu.cn>
|
||||
[zjx94](https://github.com/zjx94)
|
||||
[LeiZhi.Mai ](https://github.com/blueskiner)
|
||||
[JiaHao](https://github.com/nashiracn)
|
||||
[chdahuzi](https://github.com/chdahuzi)
|
||||
[snysmtx](https://github.com/snysmtx)
|
||||
[SetoKaiba](https://github.com/SetoKaiba)
|
||||
[sandro-qiang](https://github.com/sandro-qiang)
|
||||
[Paul Philippov](https://github.com/themactep)
|
||||
[张传峰](https://github.com/zhang-chuanfeng)
|
||||
|
||||
@@ -352,6 +352,12 @@ bash build_docker_images.sh
|
||||
[zjx94](https://github.com/zjx94)
|
||||
[LeiZhi.Mai ](https://github.com/blueskiner)
|
||||
[JiaHao](https://github.com/nashiracn)
|
||||
[chdahuzi](https://github.com/chdahuzi)
|
||||
[snysmtx](https://github.com/snysmtx)
|
||||
[SetoKaiba](https://github.com/SetoKaiba)
|
||||
[sandro-qiang](https://github.com/sandro-qiang)
|
||||
[Paul Philippov](https://github.com/themactep)
|
||||
[张传峰](https://github.com/zhang-chuanfeng)
|
||||
|
||||
同时感谢JetBrains对开源项目的支持,本项目使用CLion开发与调试:
|
||||
|
||||
|
||||
12
README_en.md
12
README_en.md
@@ -279,12 +279,12 @@ git submodule update --init
|
||||
return;
|
||||
}
|
||||
|
||||
auto viedoTrack = strongPlayer->getTrack(TrackVideo);
|
||||
if (!viedoTrack) {
|
||||
auto videoTrack = strongPlayer->getTrack(TrackVideo);
|
||||
if (!videoTrack) {
|
||||
WarnL << "No video Track!";
|
||||
return;
|
||||
}
|
||||
viedoTrack->addDelegate([](const Frame::Ptr &frame) {
|
||||
videoTrack->addDelegate([](const Frame::Ptr &frame) {
|
||||
//please decode video here
|
||||
});
|
||||
});
|
||||
@@ -510,6 +510,12 @@ Thanks to all those who have supported this project in various ways, including b
|
||||
[zjx94](https://github.com/zjx94)
|
||||
[LeiZhi.Mai ](https://github.com/blueskiner)
|
||||
[JiaHao](https://github.com/nashiracn)
|
||||
[chdahuzi](https://github.com/chdahuzi)
|
||||
[snysmtx](https://github.com/snysmtx)
|
||||
[SetoKaiba](https://github.com/SetoKaiba)
|
||||
[sandro-qiang](https://github.com/sandro-qiang)
|
||||
[Paul Philippov](https://github.com/themactep)
|
||||
[张传峰](https://github.com/zhang-chuanfeng)
|
||||
|
||||
Also thank to JetBrains for their support for open source project, we developed and debugged zlmediakit with CLion:
|
||||
|
||||
|
||||
@@ -177,6 +177,33 @@ typedef struct {
|
||||
*/
|
||||
void(API_CALL *on_mk_media_send_rtp_stop)(const char *vhost, const char *app, const char *stream, const char *ssrc, int err, const char *msg);
|
||||
|
||||
/**
|
||||
* rtc sctp连接中/完成/失败/关闭回调
|
||||
* @param rtc_transport 数据通道对象
|
||||
*/
|
||||
void(API_CALL *on_mk_rtc_sctp_connecting)(mk_rtc_transport rtc_transport);
|
||||
void(API_CALL *on_mk_rtc_sctp_connected)(mk_rtc_transport rtc_transport);
|
||||
void(API_CALL *on_mk_rtc_sctp_failed)(mk_rtc_transport rtc_transport);
|
||||
void(API_CALL *on_mk_rtc_sctp_closed)(mk_rtc_transport rtc_transport);
|
||||
|
||||
/**
|
||||
* rtc数据通道发送数据回调
|
||||
* @param rtc_transport 数据通道对象
|
||||
* @param msg 数据
|
||||
* @param len 数据长度
|
||||
*/
|
||||
void(API_CALL *on_mk_rtc_sctp_send)(mk_rtc_transport rtc_transport, const uint8_t *msg, size_t len);
|
||||
|
||||
/**
|
||||
* rtc数据通道接收数据回调
|
||||
* @param rtc_transport 数据通道对象
|
||||
* @param streamId 流id
|
||||
* @param ppid 协议id
|
||||
* @param msg 数据
|
||||
* @param len 数据长度
|
||||
*/
|
||||
void(API_CALL *on_mk_rtc_sctp_received)(mk_rtc_transport rtc_transport, uint16_t streamId, uint32_t ppid, const uint8_t *msg, size_t len);
|
||||
|
||||
} mk_events;
|
||||
|
||||
|
||||
|
||||
@@ -352,6 +352,20 @@ API_EXPORT mk_auth_invoker API_CALL mk_auth_invoker_clone(const mk_auth_invoker
|
||||
*/
|
||||
API_EXPORT void API_CALL mk_auth_invoker_clone_release(const mk_auth_invoker ctx);
|
||||
|
||||
///////////////////////////////////////////WebRtcTransport/////////////////////////////////////////////
|
||||
//WebRtcTransport对象的C映射
|
||||
typedef struct mk_rtc_transport_t *mk_rtc_transport;
|
||||
|
||||
/**
|
||||
* 发送rtc数据通道
|
||||
* @param ctx 数据通道对象
|
||||
* @param streamId 流id
|
||||
* @param ppid 协议id
|
||||
* @param msg 数据
|
||||
* @param len 数据长度
|
||||
*/
|
||||
API_EXPORT void API_CALL mk_rtc_send_datachannel(const mk_rtc_transport ctx, uint16_t streamId, uint32_t ppid, const char* msg, size_t len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#define MK_PROXY_PLAYER_H_
|
||||
|
||||
#include "mk_common.h"
|
||||
#include "mk_util.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -31,6 +32,17 @@ typedef struct mk_proxy_player_t *mk_proxy_player;
|
||||
*/
|
||||
API_EXPORT mk_proxy_player API_CALL mk_proxy_player_create(const char *vhost, const char *app, const char *stream, int hls_enabled, int mp4_enabled);
|
||||
|
||||
/**
|
||||
* 创建一个代理播放器
|
||||
* @param vhost 虚拟主机名,一般为__defaultVhost__
|
||||
* @param app 应用名
|
||||
* @param stream 流名
|
||||
* @param option ProtocolOption相关配置
|
||||
* @return 对象指针
|
||||
*/
|
||||
API_EXPORT mk_proxy_player API_CALL mk_proxy_player_create2(const char *vhost, const char *app, const char *stream, mk_ini option);
|
||||
|
||||
|
||||
/**
|
||||
* 销毁代理播放器
|
||||
* @param ctx 对象指针
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "Http/HttpSession.h"
|
||||
#include "Rtsp/RtspSession.h"
|
||||
#include "Record/MP4Recorder.h"
|
||||
#include "webrtc/WebRtcTransport.h"
|
||||
|
||||
using namespace toolkit;
|
||||
using namespace mediakit;
|
||||
@@ -167,6 +168,42 @@ API_EXPORT void API_CALL mk_events_listen(const mk_events *events){
|
||||
sender.getMediaTuple().stream.c_str(), ssrc.c_str(), ex.getErrCode(), ex.what());
|
||||
}
|
||||
});
|
||||
|
||||
NoticeCenter::Instance().addListener(&s_tag, Broadcast::kBroadcastRtcSctpConnecting,[](BroadcastRtcSctpConnectArgs){
|
||||
if (s_events.on_mk_rtc_sctp_connecting) {
|
||||
s_events.on_mk_rtc_sctp_connecting((mk_rtc_transport)&sender);
|
||||
}
|
||||
});
|
||||
|
||||
NoticeCenter::Instance().addListener(&s_tag, Broadcast::kBroadcastRtcSctpConnected,[](BroadcastRtcSctpConnectArgs){
|
||||
if (s_events.on_mk_rtc_sctp_connected) {
|
||||
s_events.on_mk_rtc_sctp_connected((mk_rtc_transport)&sender);
|
||||
}
|
||||
});
|
||||
|
||||
NoticeCenter::Instance().addListener(&s_tag, Broadcast::kBroadcastRtcSctpFailed,[](BroadcastRtcSctpConnectArgs){
|
||||
if (s_events.on_mk_rtc_sctp_failed) {
|
||||
s_events.on_mk_rtc_sctp_failed((mk_rtc_transport)&sender);
|
||||
}
|
||||
});
|
||||
|
||||
NoticeCenter::Instance().addListener(&s_tag, Broadcast::kBroadcastRtcSctpClosed,[](BroadcastRtcSctpConnectArgs){
|
||||
if (s_events.on_mk_rtc_sctp_closed) {
|
||||
s_events.on_mk_rtc_sctp_closed((mk_rtc_transport)&sender);
|
||||
}
|
||||
});
|
||||
|
||||
NoticeCenter::Instance().addListener(&s_tag, Broadcast::kBroadcastRtcSctpSend,[](BroadcastRtcSctpSendArgs){
|
||||
if (s_events.on_mk_rtc_sctp_send) {
|
||||
s_events.on_mk_rtc_sctp_send((mk_rtc_transport)&sender, data, len);
|
||||
}
|
||||
});
|
||||
|
||||
NoticeCenter::Instance().addListener(&s_tag, Broadcast::kBroadcastRtcSctpReceived,[](BroadcastRtcSctpReceivedArgs){
|
||||
if (s_events.on_mk_rtc_sctp_received) {
|
||||
s_events.on_mk_rtc_sctp_received((mk_rtc_transport)&sender, streamId, ppid, msg, len);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
#include "Http/HttpClient.h"
|
||||
#include "Rtsp/RtspSession.h"
|
||||
#include "webrtc/WebRtcTransport.h"
|
||||
|
||||
using namespace toolkit;
|
||||
using namespace mediakit;
|
||||
@@ -497,4 +498,22 @@ API_EXPORT void API_CALL mk_auth_invoker_clone_release(const mk_auth_invoker ctx
|
||||
assert(ctx);
|
||||
Broadcast::AuthInvoker *invoker = (Broadcast::AuthInvoker *)ctx;
|
||||
delete invoker;
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////WebRtcTransport/////////////////////////////////////////////
|
||||
API_EXPORT void API_CALL mk_rtc_sendDatachannel(const mk_rtc_transport ctx, uint16_t streamId, uint32_t ppid, const char *msg, size_t len) {
|
||||
#ifdef ENABLE_WEBRTC
|
||||
assert(ctx && msg);
|
||||
WebRtcTransport *transport = (WebRtcTransport *)ctx;
|
||||
std::string msg_str(msg, len);
|
||||
std::weak_ptr<WebRtcTransport> weak_trans = transport->shared_from_this();
|
||||
transport->getPoller()->async([streamId, ppid, msg_str, weak_trans]() {
|
||||
// 切换线程后再操作
|
||||
if (auto trans = weak_trans.lock()) {
|
||||
trans->sendDatachannel(streamId, ppid, msg_str.c_str(), msg_str.size());
|
||||
}
|
||||
});
|
||||
#else
|
||||
WarnL << "未启用webrtc功能, 编译时请开启ENABLE_WEBRTC";
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "mk_proxyplayer.h"
|
||||
#include "Player/PlayerProxy.h"
|
||||
#include "mk_util.h"
|
||||
|
||||
using namespace toolkit;
|
||||
using namespace mediakit;
|
||||
@@ -23,6 +24,14 @@ API_EXPORT mk_proxy_player API_CALL mk_proxy_player_create(const char *vhost, co
|
||||
return (mk_proxy_player) obj;
|
||||
}
|
||||
|
||||
API_EXPORT mk_proxy_player API_CALL mk_proxy_player_create2(const char *vhost, const char *app, const char *stream, mk_ini ini) {
|
||||
assert(vhost && app && stream);
|
||||
ProtocolOption option(*((mINI *)ini));
|
||||
PlayerProxy::Ptr *obj(new PlayerProxy::Ptr(new PlayerProxy(vhost, app, stream, option)));
|
||||
return (mk_proxy_player)obj;
|
||||
}
|
||||
|
||||
|
||||
API_EXPORT void API_CALL mk_proxy_player_release(mk_proxy_player ctx) {
|
||||
assert(ctx);
|
||||
PlayerProxy::Ptr *obj = (PlayerProxy::Ptr *) ctx;
|
||||
|
||||
@@ -24,7 +24,7 @@ bin=/usr/bin/ffmpeg
|
||||
#FFmpeg拉流再推流的命令模板,通过该模板可以设置再编码的一些参数
|
||||
cmd=%s -re -i %s -c:a aac -strict -2 -ar 44100 -ab 48k -c:v libx264 -f flv %s
|
||||
#FFmpeg生成截图的命令,可以通过修改该配置改变截图分辨率或质量
|
||||
snap=%s -i %s -y -f mjpeg -frames:v 1 %s
|
||||
snap=%s -i %s -y -f mjpeg -frames:v 1 -an %s
|
||||
#FFmpeg日志的路径,如果置空则不生成FFmpeg日志
|
||||
#可以为相对(相对于本可执行程序目录)或绝对路径
|
||||
log=./ffmpeg/ffmpeg.log
|
||||
@@ -329,6 +329,10 @@ opus_pt=100
|
||||
#如果不调用startSendRtp相关接口,可以置0节省内存
|
||||
gop_cache=1
|
||||
|
||||
#国标发送g711 rtp 打包时,每个包的语音时长是多少,默认是100 ms,范围为20~180ms (gb28181-2016,c.2.4规定),
|
||||
#最好为20 的倍数,程序自动向20的倍数取整
|
||||
rtp_g711_dur_ms = 100
|
||||
|
||||
[rtc]
|
||||
#rtc播放推流、播放超时时间
|
||||
timeoutSec=15
|
||||
|
||||
@@ -8,13 +8,27 @@ G711RtpEncoder::G711RtpEncoder(CodecId codec, uint32_t channels){
|
||||
_channels = channels;
|
||||
}
|
||||
|
||||
void G711RtpEncoder::setOpt(int opt, const toolkit::Any ¶m) {
|
||||
if (opt == RTP_ENCODER_PKT_DUR_MS) {
|
||||
if (param.is<uint32_t>()) {
|
||||
auto dur = param.get<uint32_t>();
|
||||
if (dur < 20 || dur > 180) {
|
||||
WarnL << "set g711 rtp encoder duration ms failed for " << dur;
|
||||
return;
|
||||
}
|
||||
// 向上 20ms 取整
|
||||
_pkt_dur_ms = (dur + 19) / 20 * 20;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool G711RtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
auto dur = (_cache_frame->size() - _cache_frame->prefixSize()) / (8 * _channels);
|
||||
auto next_pts = _cache_frame->pts() + dur;
|
||||
if (next_pts == 0) {
|
||||
_cache_frame->_pts = frame->pts();
|
||||
} else {
|
||||
if ((next_pts + 20) < frame->pts()) { // 有丢包超过20ms
|
||||
if ((next_pts + _pkt_dur_ms) < frame->pts()) { // 有丢包超过20ms
|
||||
_cache_frame->_pts = frame->pts() - dur;
|
||||
}
|
||||
}
|
||||
@@ -24,9 +38,9 @@ bool G711RtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
auto ptr = _cache_frame->data() + _cache_frame->prefixSize();
|
||||
auto len = _cache_frame->size() - _cache_frame->prefixSize();
|
||||
auto remain_size = len;
|
||||
auto max_size = 160 * _channels; // 20 ms per rtp
|
||||
int n = 0;
|
||||
bool mark = false;
|
||||
auto max_size = 160 * _channels * _pkt_dur_ms / 20; // 20 ms per 160 byte
|
||||
uint32_t n = 0;
|
||||
bool mark = true;
|
||||
while (remain_size >= max_size) {
|
||||
size_t rtp_size;
|
||||
if (remain_size >= max_size) {
|
||||
@@ -35,13 +49,13 @@ bool G711RtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
||||
break;
|
||||
}
|
||||
n++;
|
||||
stamp += 20;
|
||||
RtpCodec::inputRtp(getRtpInfo().makeRtp(TrackAudio, ptr, rtp_size, mark, stamp), false);
|
||||
stamp += _pkt_dur_ms;
|
||||
RtpCodec::inputRtp(getRtpInfo().makeRtp(TrackAudio, ptr, rtp_size, mark, stamp), true);
|
||||
ptr += rtp_size;
|
||||
remain_size -= rtp_size;
|
||||
}
|
||||
_cache_frame->_buffer.erase(0, n * max_size);
|
||||
_cache_frame->_pts += 20 * n;
|
||||
_cache_frame->_pts += (uint64_t)_pkt_dur_ms * n;
|
||||
return len > 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -36,8 +36,11 @@ public:
|
||||
*/
|
||||
bool inputFrame(const Frame::Ptr &frame) override;
|
||||
|
||||
void setOpt(int opt, const toolkit::Any ¶m) override;
|
||||
|
||||
private:
|
||||
uint32_t _channels = 1;
|
||||
uint32_t _pkt_dur_ms = 20;
|
||||
FrameImp::Ptr _cache_frame;
|
||||
};
|
||||
|
||||
|
||||
@@ -14,16 +14,24 @@
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
#define CHECK_RET(...) \
|
||||
try { \
|
||||
CHECK(__VA_ARGS__); \
|
||||
} catch (AssertFailedException & ex) { \
|
||||
WarnL << ex.what(); \
|
||||
return; \
|
||||
}
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
void H264RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
|
||||
if (pkt->isConfigFrame()) {
|
||||
CHECK(pkt->size() > 5);
|
||||
CHECK_RET(pkt->size() > 5);
|
||||
getTrack()->setExtraData((uint8_t *)pkt->data() + 5, pkt->size() - 5);
|
||||
return;
|
||||
}
|
||||
|
||||
CHECK(pkt->size() > 9);
|
||||
CHECK_RET(pkt->size() > 9);
|
||||
uint8_t *cts_ptr = (uint8_t *)(pkt->buffer.data() + 2);
|
||||
int32_t cts = (((cts_ptr[0] << 16) | (cts_ptr[1] << 8) | (cts_ptr[2])) + 0xff800000) ^ 0xff800000;
|
||||
auto pts = pkt->time_stamp + cts;
|
||||
|
||||
@@ -18,6 +18,14 @@
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
#define CHECK_RET(...) \
|
||||
try { \
|
||||
CHECK(__VA_ARGS__); \
|
||||
} catch (AssertFailedException & ex) { \
|
||||
WarnL << ex.what(); \
|
||||
return; \
|
||||
}
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
void H265RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
|
||||
@@ -44,7 +52,7 @@ void H265RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
|
||||
auto data = (uint8_t *)pkt->data() + RtmpPacketInfo::kEnhancedRtmpHeaderSize;
|
||||
auto size = pkt->size() - RtmpPacketInfo::kEnhancedRtmpHeaderSize;
|
||||
auto pts = pkt->time_stamp;
|
||||
CHECK(size > 3);
|
||||
CHECK_RET(size > 3);
|
||||
if (RtmpPacketType::PacketTypeCodedFrames == _info.video.pkt_type) {
|
||||
// SI24 = [CompositionTime Offset]
|
||||
int32_t cts = (((data[0] << 16) | (data[1] << 8) | (data[2])) + 0xff800000) ^ 0xff800000;
|
||||
@@ -52,7 +60,7 @@ void H265RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
|
||||
data += 3;
|
||||
size -= 3;
|
||||
}
|
||||
CHECK(size > 4);
|
||||
CHECK_RET(size > 4);
|
||||
splitFrame(data, size, pkt->time_stamp, pts);
|
||||
break;
|
||||
}
|
||||
@@ -63,12 +71,12 @@ void H265RtmpDecoder::inputRtmp(const RtmpPacket::Ptr &pkt) {
|
||||
|
||||
// 国内扩展(12) H265 rtmp
|
||||
if (pkt->isConfigFrame()) {
|
||||
CHECK(pkt->size() > 5);
|
||||
CHECK_RET(pkt->size() > 5);
|
||||
getTrack()->setExtraData((uint8_t *)pkt->data() + 5, pkt->size() - 5);
|
||||
return;
|
||||
}
|
||||
|
||||
CHECK(pkt->size() > 9);
|
||||
CHECK_RET(pkt->size() > 9);
|
||||
uint8_t *cts_ptr = (uint8_t *)(pkt->buffer.data() + 2);
|
||||
int32_t cts = (((cts_ptr[0] << 16) | (cts_ptr[1] << 8) | (cts_ptr[2])) + 0xff800000) ^ 0xff800000;
|
||||
auto pts = pkt->time_stamp + cts;
|
||||
|
||||
@@ -605,6 +605,7 @@ void JPEGRtpEncoder::rtpSendJpeg(const uint8_t *buf, int size, uint64_t pts, uin
|
||||
int i;
|
||||
int default_huffman_tables = 0;
|
||||
uint8_t *out = nullptr;
|
||||
uint16_t restart_interval = 0;
|
||||
|
||||
/* preparse the header for getting some info */
|
||||
for (i = 0; i < size; i++) {
|
||||
@@ -714,6 +715,9 @@ void JPEGRtpEncoder::rtpSendJpeg(const uint8_t *buf, int size, uint64_t pts, uin
|
||||
return;
|
||||
}
|
||||
break;
|
||||
} else if (buf[i + 1] == DRI) {
|
||||
type |= 0x40;
|
||||
restart_interval = AV_RB16(&buf[i + 4]);
|
||||
}
|
||||
}
|
||||
if (default_huffman_tables && default_huffman_tables != 31) {
|
||||
@@ -744,6 +748,9 @@ void JPEGRtpEncoder::rtpSendJpeg(const uint8_t *buf, int size, uint64_t pts, uin
|
||||
if (off == 0 && nb_qtables)
|
||||
hdr_size += 4 + 64 * nb_qtables;
|
||||
|
||||
if (type & 0x40)
|
||||
hdr_size += 4;
|
||||
|
||||
/* payload max in one packet */
|
||||
len = MIN(size, (int)getRtpInfo().getMaxSize() - hdr_size);
|
||||
|
||||
@@ -759,6 +766,13 @@ void JPEGRtpEncoder::rtpSendJpeg(const uint8_t *buf, int size, uint64_t pts, uin
|
||||
bytestream_put_byte(&p, w);
|
||||
bytestream_put_byte(&p, h);
|
||||
|
||||
/* set dri */
|
||||
if (type & 0x40) {
|
||||
bytestream_put_be16(&p, restart_interval);
|
||||
bytestream_put_byte(&p, 0xff);
|
||||
bytestream_put_byte(&p, 0xff);
|
||||
}
|
||||
|
||||
if (off == 0 && nb_qtables) {
|
||||
/* set quantization tables header */
|
||||
bytestream_put_byte(&p, 0);
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include <signal.h>
|
||||
#include "Util/logger.h"
|
||||
#include "Util/util.h"
|
||||
#include <iostream>
|
||||
#include "Common/config.h"
|
||||
#include "Rtsp/UDPServer.h"
|
||||
@@ -48,80 +49,76 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstanc, LPSTR lpCmdLine,
|
||||
int main(int argc, char *argv[]) {
|
||||
#endif
|
||||
static char *url = argv[1];
|
||||
//设置退出信号处理函数
|
||||
signal(SIGINT, [](int) { SDLDisplayerHelper::Instance().shutdown(); });
|
||||
//设置日志
|
||||
Logger::Instance().add(std::make_shared<ConsoleChannel>());
|
||||
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
||||
{
|
||||
// 设置退出信号处理函数
|
||||
signal(SIGINT, [](int) { SDLDisplayerHelper::Instance().shutdown(); });
|
||||
// 设置日志
|
||||
Logger::Instance().add(std::make_shared<ConsoleChannel>());
|
||||
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
||||
|
||||
if (argc < 3) {
|
||||
ErrorL << "\r\n测试方法:./test_player rtxp_url rtp_type\r\n"
|
||||
<< "例如:./test_player rtsp://admin:123456@127.0.0.1/live/0 0\r\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto player = std::make_shared<MediaPlayer>();
|
||||
//sdl要求在main线程初始化
|
||||
auto displayer = std::make_shared<YuvDisplayer>(nullptr, url);
|
||||
weak_ptr<MediaPlayer> weakPlayer = player;
|
||||
player->setOnPlayResult([weakPlayer, displayer](const SockException &ex) {
|
||||
InfoL << "OnPlayResult:" << ex.what();
|
||||
auto strongPlayer = weakPlayer.lock();
|
||||
if (ex || !strongPlayer) {
|
||||
return;
|
||||
if (argc < 3) {
|
||||
ErrorL << "\r\n测试方法:./test_player rtxp_url rtp_type\r\n"
|
||||
<< "例如:./test_player rtsp://admin:123456@127.0.0.1/live/0 0\r\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto videoTrack = dynamic_pointer_cast<VideoTrack>(strongPlayer->getTrack(TrackVideo, false));
|
||||
auto audioTrack = dynamic_pointer_cast<AudioTrack>(strongPlayer->getTrack(TrackAudio,false));
|
||||
auto player = std::make_shared<MediaPlayer>();
|
||||
// sdl要求在main线程初始化
|
||||
auto displayer = std::make_shared<YuvDisplayer>(nullptr, url);
|
||||
weak_ptr<MediaPlayer> weakPlayer = player;
|
||||
player->setOnPlayResult([weakPlayer, displayer](const SockException &ex) {
|
||||
InfoL << "OnPlayResult:" << ex.what();
|
||||
auto strongPlayer = weakPlayer.lock();
|
||||
if (ex || !strongPlayer) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (videoTrack) {
|
||||
auto decoder = std::make_shared<FFmpegDecoder>(videoTrack);
|
||||
decoder->setOnDecode([displayer](const FFmpegFrame::Ptr &yuv) {
|
||||
SDLDisplayerHelper::Instance().doTask([yuv, displayer]() {
|
||||
//sdl要求在main线程渲染
|
||||
displayer->displayYUV(yuv->get());
|
||||
return true;
|
||||
auto videoTrack = dynamic_pointer_cast<VideoTrack>(strongPlayer->getTrack(TrackVideo, false));
|
||||
auto audioTrack = dynamic_pointer_cast<AudioTrack>(strongPlayer->getTrack(TrackAudio, false));
|
||||
|
||||
if (videoTrack) {
|
||||
auto decoder = std::make_shared<FFmpegDecoder>(videoTrack);
|
||||
decoder->setOnDecode([displayer](const FFmpegFrame::Ptr &yuv) {
|
||||
SDLDisplayerHelper::Instance().doTask([yuv, displayer]() {
|
||||
// sdl要求在main线程渲染
|
||||
displayer->displayYUV(yuv->get());
|
||||
return true;
|
||||
});
|
||||
});
|
||||
});
|
||||
videoTrack->addDelegate([decoder](const Frame::Ptr &frame) {
|
||||
return decoder->inputFrame(frame, false, true);
|
||||
});
|
||||
videoTrack->addDelegate([decoder](const Frame::Ptr &frame) { return decoder->inputFrame(frame, false, true); });
|
||||
}
|
||||
|
||||
if (audioTrack) {
|
||||
auto decoder = std::make_shared<FFmpegDecoder>(audioTrack);
|
||||
auto audio_player = std::make_shared<AudioPlayer>();
|
||||
// FFmpeg解码时已经统一转换为16位整型pcm
|
||||
audio_player->setup(audioTrack->getAudioSampleRate(), audioTrack->getAudioChannel(), AUDIO_S16);
|
||||
FFmpegSwr::Ptr swr;
|
||||
|
||||
decoder->setOnDecode([audio_player, swr](const FFmpegFrame::Ptr &frame) mutable {
|
||||
if (!swr) {
|
||||
swr = std::make_shared<FFmpegSwr>(AV_SAMPLE_FMT_S16, frame->get()->channels, frame->get()->channel_layout, frame->get()->sample_rate);
|
||||
}
|
||||
auto pcm = swr->inputFrame(frame);
|
||||
auto len = pcm->get()->nb_samples * pcm->get()->channels * av_get_bytes_per_sample((enum AVSampleFormat)pcm->get()->format);
|
||||
audio_player->playPCM((const char *)(pcm->get()->data[0]), MIN(len, frame->get()->linesize[0]));
|
||||
});
|
||||
audioTrack->addDelegate([decoder](const Frame::Ptr &frame) { return decoder->inputFrame(frame, false, true); });
|
||||
}
|
||||
});
|
||||
|
||||
player->setOnShutdown([](const SockException &ex) { WarnL << "play shutdown: " << ex.what(); });
|
||||
|
||||
(*player)[Client::kRtpType] = atoi(argv[2]);
|
||||
// 不等待track ready再回调播放成功事件,这样可以加快秒开速度
|
||||
(*player)[Client::kWaitTrackReady] = false;
|
||||
if (argc > 3) {
|
||||
(*player)[Client::kPlayTrack] = atoi(argv[3]);
|
||||
}
|
||||
|
||||
if (audioTrack) {
|
||||
auto decoder = std::make_shared<FFmpegDecoder>(audioTrack);
|
||||
auto audio_player = std::make_shared<AudioPlayer>();
|
||||
//FFmpeg解码时已经统一转换为16位整型pcm
|
||||
audio_player->setup(audioTrack->getAudioSampleRate(), audioTrack->getAudioChannel(), AUDIO_S16);
|
||||
FFmpegSwr::Ptr swr;
|
||||
|
||||
decoder->setOnDecode([audio_player, swr](const FFmpegFrame::Ptr &frame) mutable{
|
||||
if (!swr) {
|
||||
swr = std::make_shared<FFmpegSwr>(AV_SAMPLE_FMT_S16, frame->get()->channels,
|
||||
frame->get()->channel_layout, frame->get()->sample_rate);
|
||||
}
|
||||
auto pcm = swr->inputFrame(frame);
|
||||
auto len = pcm->get()->nb_samples * pcm->get()->channels * av_get_bytes_per_sample((enum AVSampleFormat)pcm->get()->format);
|
||||
audio_player->playPCM((const char *) (pcm->get()->data[0]), MIN(len, frame->get()->linesize[0]));
|
||||
});
|
||||
audioTrack->addDelegate([decoder](const Frame::Ptr &frame) {
|
||||
return decoder->inputFrame(frame, false, true);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
player->setOnShutdown([](const SockException &ex){
|
||||
WarnL << "play shutdown: " << ex.what();
|
||||
});
|
||||
|
||||
(*player)[Client::kRtpType] = atoi(argv[2]);
|
||||
//不等待track ready再回调播放成功事件,这样可以加快秒开速度
|
||||
(*player)[Client::kWaitTrackReady] = false;
|
||||
if (argc > 3) {
|
||||
(*player)[Client::kPlayTrack] = atoi(argv[3]);
|
||||
player->play(argv[1]);
|
||||
SDLDisplayerHelper::Instance().runLoop();
|
||||
}
|
||||
player->play(argv[1]);
|
||||
SDLDisplayerHelper::Instance().runLoop();
|
||||
sleep(1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
{
|
||||
"info": {
|
||||
"_postman_id": "509e5f6b-728c-4d5f-b3e8-521d76b2cc7a",
|
||||
"_postman_id": "08e3bc35-5318-4949-81bb-90d854706194",
|
||||
"name": "ZLMediaKit",
|
||||
"description": "媒体服务器",
|
||||
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
|
||||
"_exporter_id": "29185956"
|
||||
"_exporter_id": "29185956",
|
||||
"_collection_link": "https://lively-station-598157.postman.co/workspace/%E6%B5%81%E5%AA%92%E4%BD%93%E6%9C%8D%E5%8A%A1~1e119172-45b0-4ed6-b1fc-8a15d0e2d5f8/collection/29185956-08e3bc35-5318-4949-81bb-90d854706194?action=share&source=collection_link&creator=29185956"
|
||||
},
|
||||
"item": [
|
||||
{
|
||||
@@ -1216,7 +1217,7 @@
|
||||
"method": "GET",
|
||||
"header": [],
|
||||
"url": {
|
||||
"raw": "{{ZLMediaKit_URL}}/index/api/seekRecordStamp?secret={{ZLMediaKit_secret}}&vhost={{defaultVhost}}&app=live&stream=obs&stamp",
|
||||
"raw": "{{ZLMediaKit_URL}}/index/api/seekRecordStamp?secret={{ZLMediaKit_secret}}&vhost={{defaultVhost}}&app=live&stream=obs&stamp=1000",
|
||||
"host": [
|
||||
"{{ZLMediaKit_URL}}"
|
||||
],
|
||||
@@ -1473,6 +1474,12 @@
|
||||
"value": "1",
|
||||
"description": "是否为单音频track,用于语音对讲",
|
||||
"disabled": true
|
||||
},
|
||||
{
|
||||
"key": "local_ip",
|
||||
"value": "::",
|
||||
"description": "指定创建RTP的本地ip,ipv4可填”0.0.0.0“,ipv6可填”::“,一般保持默认",
|
||||
"disabled": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1485,14 +1492,14 @@
|
||||
"method": "GET",
|
||||
"header": [],
|
||||
"url": {
|
||||
"raw": "{{ZLMediaKit_URL}}/index/api/openRtpServer?secret={{ZLMediaKit_secret}}&port=0&tcp_mode=1&stream_id=test",
|
||||
"raw": "{{ZLMediaKit_URL}}/index/api/openRtpServerMultiplex?secret={{ZLMediaKit_secret}}&port=0&tcp_mode=1&stream_id=test",
|
||||
"host": [
|
||||
"{{ZLMediaKit_URL}}"
|
||||
],
|
||||
"path": [
|
||||
"index",
|
||||
"api",
|
||||
"openRtpServer"
|
||||
"openRtpServerMultiplex"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
@@ -1520,6 +1527,12 @@
|
||||
"value": "0",
|
||||
"description": "是否为单音频track,用于语音对讲",
|
||||
"disabled": true
|
||||
},
|
||||
{
|
||||
"key": "local_ip",
|
||||
"value": "::",
|
||||
"description": "指定创建RTP的本地ip,ipv4可填”0.0.0.0“,ipv6可填”::“,一般保持默认",
|
||||
"disabled": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -2252,4 +2265,4 @@
|
||||
"value": "__defaultVhost__"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -40,7 +40,7 @@ onceToken token([]() {
|
||||
//ffmpeg日志保存路径
|
||||
mINI::Instance()[kLog] = "./ffmpeg/ffmpeg.log";
|
||||
mINI::Instance()[kCmd] = "%s -re -i %s -c:a aac -strict -2 -ar 44100 -ab 48k -c:v libx264 -f flv %s";
|
||||
mINI::Instance()[kSnap] = "%s -i %s -y -f mjpeg -frames:v 1 %s";
|
||||
mINI::Instance()[kSnap] = "%s -i %s -y -f mjpeg -frames:v 1 -an %s";
|
||||
mINI::Instance()[kRestartSec] = 0;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
#include <regex>
|
||||
#include "Util/MD5.h"
|
||||
#include "Util/util.h"
|
||||
#include "Util/File.h"
|
||||
@@ -203,7 +204,7 @@ static ApiArgsType getAllArgs(const Parser &parser) {
|
||||
if (parser["Content-Type"].find("application/x-www-form-urlencoded") == 0) {
|
||||
auto contentArgs = parser.parseArgs(parser.content());
|
||||
for (auto &pr : contentArgs) {
|
||||
allArgs[pr.first] = HttpSession::urlDecode(pr.second);
|
||||
allArgs[pr.first] = HttpSession::urlDecodeComponent(pr.second);
|
||||
}
|
||||
} else if (parser["Content-Type"].find("application/json") == 0) {
|
||||
try {
|
||||
@@ -1197,7 +1198,11 @@ void installWebApi() {
|
||||
//兼容老版本请求,新版本去除enable_tcp参数并新增tcp_mode参数
|
||||
tcp_mode = 1;
|
||||
}
|
||||
auto port = openRtpServer(allArgs["port"], stream_id, tcp_mode, "::", allArgs["re_use_port"].as<bool>(),
|
||||
std::string local_ip = "::";
|
||||
if (!allArgs["local_ip"].empty()) {
|
||||
local_ip = allArgs["local_ip"];
|
||||
}
|
||||
auto port = openRtpServer(allArgs["port"], stream_id, tcp_mode, local_ip, allArgs["re_use_port"].as<bool>(),
|
||||
allArgs["ssrc"].as<uint32_t>(), allArgs["only_audio"].as<bool>());
|
||||
if (port == 0) {
|
||||
throw InvalidArgsException("该stream_id已存在");
|
||||
@@ -1215,9 +1220,11 @@ void installWebApi() {
|
||||
// 兼容老版本请求,新版本去除enable_tcp参数并新增tcp_mode参数
|
||||
tcp_mode = 1;
|
||||
}
|
||||
|
||||
auto port = openRtpServer(
|
||||
allArgs["port"], stream_id, tcp_mode, "::", true, 0, allArgs["only_audio"].as<bool>(),true);
|
||||
std::string local_ip = "::";
|
||||
if (!allArgs["local_ip"].empty()) {
|
||||
local_ip = allArgs["local_ip"];
|
||||
}
|
||||
auto port = openRtpServer(allArgs["port"], stream_id, tcp_mode, local_ip, true, 0, allArgs["only_audio"].as<bool>(),true);
|
||||
if (port == 0) {
|
||||
throw InvalidArgsException("该stream_id已存在");
|
||||
}
|
||||
@@ -1282,7 +1289,10 @@ void installWebApi() {
|
||||
if (!src) {
|
||||
throw ApiRetException("can not find the source stream", API::NotFound);
|
||||
}
|
||||
|
||||
if (!allArgs["use_ps"].empty()) {
|
||||
// 兼容之前的use_ps参数
|
||||
allArgs["type"] = allArgs["use_ps"].as<int>();
|
||||
}
|
||||
MediaSourceEvent::SendRtpArgs args;
|
||||
args.passive = false;
|
||||
args.dst_url = allArgs["dst_url"];
|
||||
@@ -1292,11 +1302,11 @@ void installWebApi() {
|
||||
args.is_udp = allArgs["is_udp"];
|
||||
args.src_port = allArgs["src_port"];
|
||||
args.pt = allArgs["pt"].empty() ? 96 : allArgs["pt"].as<int>();
|
||||
args.use_ps = allArgs["use_ps"].empty() ? true : allArgs["use_ps"].as<bool>();
|
||||
args.type = (MediaSourceEvent::SendRtpArgs::Type)(allArgs["type"].as<int>());
|
||||
args.only_audio = allArgs["only_audio"].as<bool>();
|
||||
args.udp_rtcp_timeout = allArgs["udp_rtcp_timeout"];
|
||||
args.recv_stream_id = allArgs["recv_stream_id"];
|
||||
TraceL << "startSendRtp, pt " << int(args.pt) << " ps " << args.use_ps << " audio " << args.only_audio;
|
||||
TraceL << "startSendRtp, pt " << int(args.pt) << " rtp type " << args.type << " audio " << args.only_audio;
|
||||
|
||||
src->getOwnerPoller()->async([=]() mutable {
|
||||
src->startSendRtp(args, [val, headerOut, invoker](uint16_t local_port, const SockException &ex) mutable {
|
||||
@@ -1319,18 +1329,23 @@ void installWebApi() {
|
||||
throw ApiRetException("can not find the source stream", API::NotFound);
|
||||
}
|
||||
|
||||
if (!allArgs["use_ps"].empty()) {
|
||||
// 兼容之前的use_ps参数
|
||||
allArgs["type"] = allArgs["use_ps"].as<int>();
|
||||
}
|
||||
|
||||
MediaSourceEvent::SendRtpArgs args;
|
||||
args.passive = true;
|
||||
args.ssrc = allArgs["ssrc"];
|
||||
args.is_udp = false;
|
||||
args.src_port = allArgs["src_port"];
|
||||
args.pt = allArgs["pt"].empty() ? 96 : allArgs["pt"].as<int>();
|
||||
args.use_ps = allArgs["use_ps"].empty() ? true : allArgs["use_ps"].as<bool>();
|
||||
args.type = (MediaSourceEvent::SendRtpArgs::Type)(allArgs["type"].as<int>());
|
||||
args.only_audio = allArgs["only_audio"].as<bool>();
|
||||
args.recv_stream_id = allArgs["recv_stream_id"];
|
||||
//tcp被动服务器等待链接超时时间
|
||||
args.tcp_passive_close_delay_ms = allArgs["close_delay_ms"];
|
||||
TraceL << "startSendRtpPassive, pt " << int(args.pt) << " ps " << args.use_ps << " audio " << args.only_audio;
|
||||
TraceL << "startSendRtpPassive, pt " << int(args.pt) << " rtp type " << args.type << " audio " << args.only_audio;
|
||||
|
||||
src->getOwnerPoller()->async([=]() mutable {
|
||||
src->startSendRtp(args, [val, headerOut, invoker](uint16_t local_port, const SockException &ex) mutable {
|
||||
@@ -1740,16 +1755,26 @@ void installWebApi() {
|
||||
auto type = allArgs["type"];
|
||||
auto offer = allArgs.getArgs();
|
||||
CHECK(!offer.empty(), "http body(webrtc offer sdp) is empty");
|
||||
std::string host = allArgs.getParser()["Host"];
|
||||
std::string localIp = host.substr(0, host.find(':'));
|
||||
|
||||
auto isVaildIP = [](std::string ip)-> bool {
|
||||
int a,b,c,d;
|
||||
return sscanf(ip.c_str(),"%d.%d.%d.%d", &a, &b, &c, &d) == 4;
|
||||
};
|
||||
if (!isVaildIP(localIp) || localIp=="127.0.0.1") {
|
||||
localIp = "";
|
||||
}
|
||||
|
||||
auto args = std::make_shared<WebRtcArgsImp>(allArgs, sender.getIdentifier());
|
||||
WebRtcPluginManager::Instance().getAnswerSdp(static_cast<Session&>(sender), type, *args,
|
||||
[invoker, val, offer, headerOut](const WebRtcInterface &exchanger) mutable {
|
||||
WebRtcPluginManager::Instance().getAnswerSdp(static_cast<Session&>(sender), type, *args, [invoker, val, offer, headerOut, localIp](const WebRtcInterface &exchanger) mutable {
|
||||
//设置返回类型
|
||||
headerOut["Content-Type"] = HttpFileManager::getContentType(".json");
|
||||
//设置跨域
|
||||
headerOut["Access-Control-Allow-Origin"] = "*";
|
||||
|
||||
try {
|
||||
setLocalIp(exchanger,localIp);
|
||||
val["sdp"] = exchangeSdp(exchanger, offer);
|
||||
val["id"] = exchanger.getIdentifier();
|
||||
val["type"] = "answer";
|
||||
|
||||
@@ -37,6 +37,7 @@ bool MediaSink::addTrack(const Track::Ptr &track_in) {
|
||||
}
|
||||
// 克隆Track,只拷贝其数据,不拷贝其数据转发关系
|
||||
auto track = track_in->clone();
|
||||
CHECK(track, "Clone track failed: ", track_in->getCodecName());
|
||||
auto index = track->getIndex();
|
||||
if (!_track_map.emplace(index, std::make_pair(track, false)).second) {
|
||||
WarnL << "Already add a same track: " << track->getIndex() << ", codec: " << track->getCodecName();
|
||||
|
||||
@@ -92,10 +92,11 @@ public:
|
||||
|
||||
class SendRtpArgs {
|
||||
public:
|
||||
enum Type { kRtpRAW = 0, kRtpPS = 1, kRtpTS = 2 };
|
||||
// 是否采用udp方式发送rtp
|
||||
bool is_udp = true;
|
||||
// rtp采用ps还是es方式
|
||||
bool use_ps = true;
|
||||
// rtp类型
|
||||
Type type = kRtpPS;
|
||||
//发送es流时指定是否只发送纯音频流
|
||||
bool only_audio = false;
|
||||
//tcp被动方式
|
||||
|
||||
@@ -294,8 +294,8 @@ void RtspUrl::setup(bool is_ssl, const string &url, const string &user, const st
|
||||
splitUrl(ip, ip, port);
|
||||
|
||||
_url = std::move(url);
|
||||
_user = strCoding::UrlDecode(std::move(user));
|
||||
_passwd = strCoding::UrlDecode(std::move(passwd));
|
||||
_user = strCoding::UrlDecodeComponent(std::move(user));
|
||||
_passwd = strCoding::UrlDecodeComponent(std::move(passwd));
|
||||
_host = std::move(ip);
|
||||
_port = port;
|
||||
_is_ssl = is_ssl;
|
||||
|
||||
@@ -58,6 +58,12 @@ const string kBroadcastStreamNoneReader = "kBroadcastStreamNoneReader";
|
||||
const string kBroadcastHttpBeforeAccess = "kBroadcastHttpBeforeAccess";
|
||||
const string kBroadcastSendRtpStopped = "kBroadcastSendRtpStopped";
|
||||
const string kBroadcastRtpServerTimeout = "kBroadcastRtpServerTimeout";
|
||||
const string kBroadcastRtcSctpConnecting = "kBroadcastRtcSctpConnecting";
|
||||
const string kBroadcastRtcSctpConnected = "kBroadcastRtcSctpConnected";
|
||||
const string kBroadcastRtcSctpFailed = "kBroadcastRtcSctpFailed";
|
||||
const string kBroadcastRtcSctpClosed = "kBroadcastRtcSctpClosed";
|
||||
const string kBroadcastRtcSctpSend = "kBroadcastRtcSctpSend";
|
||||
const string kBroadcastRtcSctpReceived = "kBroadcastRtcSctpReceived";
|
||||
|
||||
} // namespace Broadcast
|
||||
|
||||
@@ -338,6 +344,7 @@ const string kH265PT = RTP_PROXY_FIELD "h265_pt";
|
||||
const string kPSPT = RTP_PROXY_FIELD "ps_pt";
|
||||
const string kOpusPT = RTP_PROXY_FIELD "opus_pt";
|
||||
const string kGopCache = RTP_PROXY_FIELD "gop_cache";
|
||||
const string kRtpG711DurMs = RTP_PROXY_FIELD "rtp_g711_dur_ms";
|
||||
|
||||
static onceToken token([]() {
|
||||
mINI::Instance()[kDumpDir] = "";
|
||||
@@ -348,6 +355,7 @@ static onceToken token([]() {
|
||||
mINI::Instance()[kPSPT] = 96;
|
||||
mINI::Instance()[kOpusPT] = 100;
|
||||
mINI::Instance()[kGopCache] = 1;
|
||||
mINI::Instance()[kRtpG711DurMs] = 100;
|
||||
});
|
||||
} // namespace RtpProxy
|
||||
|
||||
|
||||
@@ -109,6 +109,21 @@ extern const std::string kBroadcastReloadConfig;
|
||||
extern const std::string kBroadcastRtpServerTimeout;
|
||||
#define BroadcastRtpServerTimeoutArgs uint16_t &local_port, const string &stream_id,int &tcp_mode, bool &re_use_port, uint32_t &ssrc
|
||||
|
||||
// rtc transport sctp 连接状态
|
||||
extern const std::string kBroadcastRtcSctpConnecting;
|
||||
extern const std::string kBroadcastRtcSctpConnected;
|
||||
extern const std::string kBroadcastRtcSctpFailed;
|
||||
extern const std::string kBroadcastRtcSctpClosed;
|
||||
#define BroadcastRtcSctpConnectArgs WebRtcTransport& sender
|
||||
|
||||
// rtc transport sctp 发送数据
|
||||
extern const std::string kBroadcastRtcSctpSend;
|
||||
#define BroadcastRtcSctpSendArgs WebRtcTransport& sender, const uint8_t *&data, size_t& len
|
||||
|
||||
// rtc transport sctp 接收数据
|
||||
extern const std::string kBroadcastRtcSctpReceived;
|
||||
#define BroadcastRtcSctpReceivedArgs WebRtcTransport& sender, uint16_t &streamId, uint32_t &ppid, const uint8_t *&msg, size_t &len
|
||||
|
||||
#define ReloadConfigTag ((void *)(0xFF))
|
||||
#define RELOAD_KEY(arg, key) \
|
||||
do { \
|
||||
@@ -382,6 +397,9 @@ extern const std::string kPSPT;
|
||||
extern const std::string kOpusPT;
|
||||
// RtpSender相关功能是否提前开启gop缓存优化级联秒开体验,默认开启
|
||||
extern const std::string kGopCache;
|
||||
//国标发送g711 rtp 打包时,每个包的语音时长是多少,默认是100 ms,范围为20~180ms (gb28181-2016,c.2.4规定),
|
||||
//最好为20 的倍数,程序自动向20的倍数取整
|
||||
extern const std::string kRtpG711DurMs;
|
||||
} // namespace RtpProxy
|
||||
|
||||
/**
|
||||
|
||||
@@ -69,6 +69,40 @@ string strCoding::UrlEncode(const string &str) {
|
||||
return out;
|
||||
}
|
||||
|
||||
string strCoding::UrlEncodePath(const string &str) {
|
||||
const char *dont_escape = "!#&'*+:=?@/._-$,;~()";
|
||||
string out;
|
||||
size_t len = str.size();
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
char ch = str[i];
|
||||
if (isalnum((uint8_t) ch) || strchr(dont_escape, (uint8_t) ch) != NULL) {
|
||||
out.push_back(ch);
|
||||
} else {
|
||||
char buf[4];
|
||||
snprintf(buf, 4, "%%%X%X", (uint8_t) ch >> 4, (uint8_t) ch & 0x0F);
|
||||
out.append(buf);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
string strCoding::UrlEncodeComponent(const string &str) {
|
||||
const char *dont_escape = "!'()*-._~";
|
||||
string out;
|
||||
size_t len = str.size();
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
char ch = str[i];
|
||||
if (isalnum((uint8_t) ch) || strchr(dont_escape, (uint8_t) ch) != NULL) {
|
||||
out.push_back(ch);
|
||||
} else {
|
||||
char buf[4];
|
||||
snprintf(buf, 4, "%%%X%X", (uint8_t) ch >> 4, (uint8_t) ch & 0x0F);
|
||||
out.append(buf);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
string strCoding::UrlDecode(const string &str) {
|
||||
string output;
|
||||
size_t i = 0, len = str.length();
|
||||
@@ -95,6 +129,62 @@ string strCoding::UrlDecode(const string &str) {
|
||||
return output;
|
||||
}
|
||||
|
||||
string strCoding::UrlDecodePath(const string &str) {
|
||||
const char *dont_unescape = "#$&+,/:;=?@";
|
||||
string output;
|
||||
size_t i = 0, len = str.length();
|
||||
while (i < len) {
|
||||
if (str[i] == '%') {
|
||||
if (i + 3 > len) {
|
||||
// %后面必须还有两个字节才会反转义
|
||||
output.append(str, i, len - i);
|
||||
break;
|
||||
}
|
||||
char ch = HexStrToBin(&(str[i + 1]));
|
||||
if (ch == -1 || strchr(dont_unescape, (unsigned char)ch) != NULL) {
|
||||
// %后面两个字节不是16进制字符串,转义失败;或者转义出来可能会造成url包含非path部分,比如#?,说明提交的是非法拼接的url;直接拼接3个原始字符
|
||||
output.append(str, i, 3);
|
||||
} else {
|
||||
output += ch;
|
||||
}
|
||||
i += 3;
|
||||
} else {
|
||||
output += str[i];
|
||||
++i;
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
std::string strCoding::UrlDecodeComponent(const std::string &str) {
|
||||
string output;
|
||||
size_t i = 0, len = str.length();
|
||||
while (i < len) {
|
||||
if (str[i] == '%') {
|
||||
if (i + 3 > len) {
|
||||
// %后面必须还有两个字节才会反转义
|
||||
output.append(str, i, len - i);
|
||||
break;
|
||||
}
|
||||
char ch = HexStrToBin(&(str[i + 1]));
|
||||
if (ch == -1) {
|
||||
// %后面两个字节不是16进制字符串,转义失败;直接拼接3个原始字符
|
||||
output.append(str, i, 3);
|
||||
} else {
|
||||
output += ch;
|
||||
}
|
||||
i += 3;
|
||||
} else if (str[i] == '+') {
|
||||
output += ' ';
|
||||
++i;
|
||||
} else {
|
||||
output += str[i];
|
||||
++i;
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
#if 0
|
||||
#include "Util/onceToken.h"
|
||||
static toolkit::onceToken token([]() {
|
||||
|
||||
@@ -18,8 +18,12 @@ namespace mediakit {
|
||||
|
||||
class strCoding {
|
||||
public:
|
||||
static std::string UrlEncode(const std::string &str); //urlutf8 编码
|
||||
static std::string UrlDecode(const std::string &str); //urlutf8解码
|
||||
[[deprecated]] static std::string UrlEncode(const std::string &str); //url utf8编码, deprecated
|
||||
static std::string UrlEncodePath(const std::string &str); //url路径 utf8编码
|
||||
static std::string UrlEncodeComponent(const std::string &str); // url参数 utf8编码
|
||||
[[deprecated]] static std::string UrlDecode(const std::string &str); //url utf8解码, deprecated
|
||||
static std::string UrlDecodePath(const std::string &str); //url路径 utf8解码
|
||||
static std::string UrlDecodeComponent(const std::string &str); // url参数 utf8解码
|
||||
#if defined(_WIN32)
|
||||
static std::string UTF8ToGB2312(const std::string &str);//utf_8转为gb2312
|
||||
static std::string GB2312ToUTF8(const std::string &str); //gb2312 转utf_8
|
||||
|
||||
@@ -28,10 +28,6 @@
|
||||
#include "HttpClient.h"
|
||||
#include "Common/macros.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
#define ENABLE_MMAP
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
@@ -57,12 +53,25 @@ Buffer::Ptr HttpStringBody::readData(size_t size) {
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef ENABLE_MMAP
|
||||
|
||||
static mutex s_mtx;
|
||||
static unordered_map<string /*file_path*/, std::tuple<char */*ptr*/, int64_t /*size*/, weak_ptr<char> /*mmap*/ > > s_shared_mmap;
|
||||
|
||||
#if defined(_WIN32)
|
||||
static void mmap_close(HANDLE _hfile, HANDLE _hmapping, void *_addr) {
|
||||
if (_addr) {
|
||||
::UnmapViewOfFile(_addr);
|
||||
}
|
||||
|
||||
if (_hmapping) {
|
||||
::CloseHandle(_hmapping);
|
||||
}
|
||||
|
||||
if (_hfile != INVALID_HANDLE_VALUE) {
|
||||
::CloseHandle(_hfile);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
//删除mmap记录
|
||||
static void delSharedMmap(const string &file_path, char *ptr) {
|
||||
lock_guard<mutex> lck(s_mtx);
|
||||
@@ -97,24 +106,67 @@ static std::shared_ptr<char> getSharedMmap(const string &file_path, int64_t &fil
|
||||
file_size = -1;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
#if defined(_WIN32)
|
||||
auto fd = _fileno(fp.get());
|
||||
#else
|
||||
//获取文件大小
|
||||
file_size = File::fileSize(fp.get());
|
||||
auto fd = fileno(fp.get());
|
||||
#endif
|
||||
|
||||
int fd = fileno(fp.get());
|
||||
if (fd < 0) {
|
||||
WarnL << "fileno failed:" << get_uv_errmsg(false);
|
||||
return nullptr;
|
||||
}
|
||||
#ifndef _WIN32
|
||||
auto ptr = (char *)mmap(NULL, file_size, PROT_READ, MAP_SHARED, fd, 0);
|
||||
if (ptr == MAP_FAILED) {
|
||||
WarnL << "mmap " << file_path << " failed:" << get_uv_errmsg(false);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
std::shared_ptr<char> ret(ptr, [file_size, fp, file_path](char *ptr) {
|
||||
munmap(ptr, file_size);
|
||||
delSharedMmap(file_path, ptr);
|
||||
});
|
||||
|
||||
#else
|
||||
auto hfile = ::CreateFileA(file_path.data(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
|
||||
if (hfile == INVALID_HANDLE_VALUE) {
|
||||
WarnL << "CreateFileA() " << file_path << " failed:";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
file_size = ::GetFileSize(hfile, NULL);
|
||||
|
||||
auto hmapping = ::CreateFileMapping(hfile, NULL, PAGE_READONLY, 0, 0, NULL);
|
||||
|
||||
if (hmapping == NULL) {
|
||||
mmap_close(hfile, NULL, NULL);
|
||||
WarnL << "CreateFileMapping() " << file_path << " failed:";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto addr_ = ::MapViewOfFile(hmapping, FILE_MAP_READ, 0, 0, 0);
|
||||
|
||||
if (addr_ == nullptr) {
|
||||
mmap_close(hfile, hmapping, addr_);
|
||||
WarnL << "MapViewOfFile() " << file_path << " failed:";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<char> ret((char *)(addr_), [hfile, hmapping, file_path](char *addr_) {
|
||||
mmap_close(hfile, hmapping, addr_);
|
||||
delSharedMmap(file_path, addr_);
|
||||
});
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#if 0
|
||||
if (file_size < 10 * 1024 * 1024 && file_path.rfind(".ts") != string::npos) {
|
||||
//如果是小ts文件,那么尝试先加载到内存
|
||||
@@ -131,14 +183,12 @@ static std::shared_ptr<char> getSharedMmap(const string &file_path, int64_t &fil
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
HttpFileBody::HttpFileBody(const string &file_path, bool use_mmap) {
|
||||
#ifdef ENABLE_MMAP
|
||||
if (use_mmap ) {
|
||||
_map_addr = getSharedMmap(file_path, _read_to);
|
||||
_map_addr = getSharedMmap(file_path, _read_to);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!_map_addr && _read_to != -1) {
|
||||
//mmap失败(且不是由于文件不存在导致的)或未执行mmap时,才进入fread逻辑分支
|
||||
_fp.reset(fopen(file_path.data(), "rb"), [](FILE *fp) {
|
||||
|
||||
@@ -34,7 +34,7 @@ public:
|
||||
for (auto &pr : *this) {
|
||||
ret.append(pr.first);
|
||||
ret.append("=");
|
||||
ret.append(strCoding::UrlEncode(pr.second));
|
||||
ret.append(strCoding::UrlEncodeComponent(pr.second));
|
||||
ret.append("&");
|
||||
}
|
||||
if (ret.size()) {
|
||||
|
||||
@@ -228,7 +228,7 @@ static bool makeFolderMenu(const string &httpPath, const string &strFullPath, st
|
||||
multimap<string/*url name*/, std::pair<string/*note name*/, string/*file path*/> > file_map;
|
||||
File::scanDir(strPathPrefix, [&](const std::string &path, bool isDir) {
|
||||
auto name = fileName(strPathPrefix, path);
|
||||
file_map.emplace(strCoding::UrlEncode(name), std::make_pair(name, path));
|
||||
file_map.emplace(strCoding::UrlEncodePath(name), std::make_pair(name, path));
|
||||
return true;
|
||||
});
|
||||
//如果是root目录,添加虚拟目录
|
||||
|
||||
@@ -65,6 +65,7 @@ ssize_t HttpSession::onRecvHeader(const char *header, size_t len) {
|
||||
|
||||
_parser.parse(header, len);
|
||||
CHECK(_parser.url()[0] == '/');
|
||||
_origin = _parser["Origin"];
|
||||
|
||||
urlDecode(_parser);
|
||||
auto &cmd = _parser.method();
|
||||
@@ -606,8 +607,8 @@ void HttpSession::sendResponse(int code,
|
||||
headerOut.emplace("Connection", bClose ? "close" : "keep-alive");
|
||||
|
||||
GET_CONFIG(bool, allow_cross_domains, Http::kAllowCrossDomains);
|
||||
if (allow_cross_domains) {
|
||||
headerOut.emplace("Access-Control-Allow-Origin", "*");
|
||||
if (allow_cross_domains && !_origin.empty()) {
|
||||
headerOut.emplace("Access-Control-Allow-Origin", _origin);
|
||||
headerOut.emplace("Access-Control-Allow-Credentials", "true");
|
||||
}
|
||||
|
||||
@@ -694,10 +695,34 @@ string HttpSession::urlDecode(const string &str) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
string HttpSession::urlDecodePath(const string &str) {
|
||||
auto ret = strCoding::UrlDecodePath(str);
|
||||
#ifdef _WIN32
|
||||
GET_CONFIG(string, charSet, Http::kCharSet);
|
||||
bool isGb2312 = !strcasecmp(charSet.data(), "gb2312");
|
||||
if (isGb2312) {
|
||||
ret = strCoding::UTF8ToGB2312(ret);
|
||||
}
|
||||
#endif // _WIN32
|
||||
return ret;
|
||||
}
|
||||
|
||||
string HttpSession::urlDecodeComponent(const string &str) {
|
||||
auto ret = strCoding::UrlDecodeComponent(str);
|
||||
#ifdef _WIN32
|
||||
GET_CONFIG(string, charSet, Http::kCharSet);
|
||||
bool isGb2312 = !strcasecmp(charSet.data(), "gb2312");
|
||||
if (isGb2312) {
|
||||
ret = strCoding::UTF8ToGB2312(ret);
|
||||
}
|
||||
#endif // _WIN32
|
||||
return ret;
|
||||
}
|
||||
|
||||
void HttpSession::urlDecode(Parser &parser) {
|
||||
parser.setUrl(urlDecode(parser.url()));
|
||||
parser.setUrl(urlDecodePath(parser.url()));
|
||||
for (auto &pr : _parser.getUrlArgs()) {
|
||||
const_cast<string &>(pr.second) = urlDecode(pr.second);
|
||||
const_cast<string &>(pr.second) = urlDecodeComponent(pr.second);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -44,7 +44,9 @@ public:
|
||||
void onRecv(const toolkit::Buffer::Ptr &) override;
|
||||
void onError(const toolkit::SockException &err) override;
|
||||
void onManager() override;
|
||||
static std::string urlDecode(const std::string &str);
|
||||
[[deprecated]] static std::string urlDecode(const std::string &str);
|
||||
static std::string urlDecodePath(const std::string &str);
|
||||
static std::string urlDecodeComponent(const std::string &str);
|
||||
void setTimeoutSec(size_t second);
|
||||
void setMaxReqSize(size_t max_req_size);
|
||||
|
||||
@@ -136,6 +138,8 @@ private:
|
||||
size_t _max_req_size = 0;
|
||||
//消耗的总流量
|
||||
uint64_t _total_bytes_usage = 0;
|
||||
// http请求中的 Origin字段
|
||||
std::string _origin;
|
||||
Parser _parser;
|
||||
toolkit::Ticker _ticker;
|
||||
TSMediaSource::RingType::RingReader::Ptr _ts_reader;
|
||||
|
||||
@@ -23,11 +23,16 @@ namespace mediakit {
|
||||
|
||||
PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &in_poller, const string &url_in) {
|
||||
auto poller = in_poller ? in_poller : EventPollerPool::Instance().getPoller();
|
||||
static auto releasePlayer = [poller](PlayerBase *ptr) {
|
||||
poller->async([ptr]() {
|
||||
onceToken token(nullptr, [&]() { delete ptr; });
|
||||
ptr->teardown();
|
||||
});
|
||||
std::weak_ptr<EventPoller> weak_poller = poller;
|
||||
static auto release_func = [weak_poller](PlayerBase *ptr) {
|
||||
if (auto poller = weak_poller.lock()) {
|
||||
poller->async([ptr]() {
|
||||
onceToken token(nullptr, [&]() { delete ptr; });
|
||||
ptr->teardown();
|
||||
});
|
||||
} else {
|
||||
delete ptr;
|
||||
}
|
||||
};
|
||||
string url = url_in;
|
||||
string prefix = findSubString(url.data(), NULL, "://");
|
||||
@@ -38,29 +43,29 @@ PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &in_poller, cons
|
||||
}
|
||||
|
||||
if (strcasecmp("rtsps", prefix.data()) == 0) {
|
||||
return PlayerBase::Ptr(new TcpClientWithSSL<RtspPlayerImp>(poller), releasePlayer);
|
||||
return PlayerBase::Ptr(new TcpClientWithSSL<RtspPlayerImp>(poller), release_func);
|
||||
}
|
||||
|
||||
if (strcasecmp("rtsp", prefix.data()) == 0) {
|
||||
return PlayerBase::Ptr(new RtspPlayerImp(poller), releasePlayer);
|
||||
return PlayerBase::Ptr(new RtspPlayerImp(poller), release_func);
|
||||
}
|
||||
|
||||
if (strcasecmp("rtmps", prefix.data()) == 0) {
|
||||
return PlayerBase::Ptr(new TcpClientWithSSL<RtmpPlayerImp>(poller), releasePlayer);
|
||||
return PlayerBase::Ptr(new TcpClientWithSSL<RtmpPlayerImp>(poller), release_func);
|
||||
}
|
||||
|
||||
if (strcasecmp("rtmp", prefix.data()) == 0) {
|
||||
return PlayerBase::Ptr(new RtmpPlayerImp(poller), releasePlayer);
|
||||
return PlayerBase::Ptr(new RtmpPlayerImp(poller), release_func);
|
||||
}
|
||||
if ((strcasecmp("http", prefix.data()) == 0 || strcasecmp("https", prefix.data()) == 0)) {
|
||||
if (end_with(url, ".m3u8") || end_with(url_in, ".m3u8")) {
|
||||
return PlayerBase::Ptr(new HlsPlayerImp(poller), releasePlayer);
|
||||
return PlayerBase::Ptr(new HlsPlayerImp(poller), release_func);
|
||||
}
|
||||
if (end_with(url, ".ts") || end_with(url_in, ".ts")) {
|
||||
return PlayerBase::Ptr(new TsPlayerImp(poller), releasePlayer);
|
||||
return PlayerBase::Ptr(new TsPlayerImp(poller), release_func);
|
||||
}
|
||||
if (end_with(url, ".flv") || end_with(url_in, ".flv")) {
|
||||
return PlayerBase::Ptr(new FlvPlayerImp(poller), releasePlayer);
|
||||
return PlayerBase::Ptr(new FlvPlayerImp(poller), release_func);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,31 +17,37 @@ using namespace toolkit;
|
||||
|
||||
namespace mediakit {
|
||||
|
||||
PusherBase::Ptr PusherBase::createPusher(const EventPoller::Ptr &poller,
|
||||
PusherBase::Ptr PusherBase::createPusher(const EventPoller::Ptr &in_poller,
|
||||
const MediaSource::Ptr &src,
|
||||
const std::string & url) {
|
||||
static auto releasePusher = [](PusherBase *ptr){
|
||||
onceToken token(nullptr,[&](){
|
||||
delete ptr;
|
||||
});
|
||||
ptr->teardown();
|
||||
auto poller = in_poller ? in_poller : EventPollerPool::Instance().getPoller();
|
||||
std::weak_ptr<EventPoller> weak_poller = poller;
|
||||
static auto release_func = [weak_poller](PusherBase *ptr) {
|
||||
if (auto poller = weak_poller.lock()) {
|
||||
poller->async([ptr]() {
|
||||
onceToken token(nullptr, [&]() { delete ptr; });
|
||||
ptr->teardown();
|
||||
});
|
||||
} else {
|
||||
delete ptr;
|
||||
}
|
||||
};
|
||||
std::string prefix = findSubString(url.data(), NULL, "://");
|
||||
|
||||
if (strcasecmp("rtsps",prefix.data()) == 0) {
|
||||
return PusherBase::Ptr(new TcpClientWithSSL<RtspPusherImp>(poller, std::dynamic_pointer_cast<RtspMediaSource>(src)), releasePusher);
|
||||
return PusherBase::Ptr(new TcpClientWithSSL<RtspPusherImp>(poller, std::dynamic_pointer_cast<RtspMediaSource>(src)), release_func);
|
||||
}
|
||||
|
||||
if (strcasecmp("rtsp",prefix.data()) == 0) {
|
||||
return PusherBase::Ptr(new RtspPusherImp(poller, std::dynamic_pointer_cast<RtspMediaSource>(src)), releasePusher);
|
||||
return PusherBase::Ptr(new RtspPusherImp(poller, std::dynamic_pointer_cast<RtspMediaSource>(src)), release_func);
|
||||
}
|
||||
|
||||
if (strcasecmp("rtmps",prefix.data()) == 0) {
|
||||
return PusherBase::Ptr(new TcpClientWithSSL<RtmpPusherImp>(poller, std::dynamic_pointer_cast<RtmpMediaSource>(src)), releasePusher);
|
||||
return PusherBase::Ptr(new TcpClientWithSSL<RtmpPusherImp>(poller, std::dynamic_pointer_cast<RtmpMediaSource>(src)), release_func);
|
||||
}
|
||||
|
||||
if (strcasecmp("rtmp",prefix.data()) == 0) {
|
||||
return PusherBase::Ptr(new RtmpPusherImp(poller, std::dynamic_pointer_cast<RtmpMediaSource>(src)), releasePusher);
|
||||
return PusherBase::Ptr(new RtmpPusherImp(poller, std::dynamic_pointer_cast<RtmpMediaSource>(src)), release_func);
|
||||
}
|
||||
|
||||
throw std::invalid_argument("not supported push schema:" + url);
|
||||
|
||||
@@ -19,9 +19,17 @@ using namespace toolkit;
|
||||
|
||||
namespace mediakit{
|
||||
|
||||
PSEncoderImp::PSEncoderImp(uint32_t ssrc, uint8_t payload_type) : MpegMuxer(true) {
|
||||
GET_CONFIG(uint32_t,video_mtu,Rtp::kVideoMtuSize);
|
||||
PSEncoderImp::PSEncoderImp(uint32_t ssrc, uint8_t payload_type, bool ps_or_ts) : MpegMuxer(ps_or_ts) {
|
||||
GET_CONFIG(uint32_t, s_video_mtu, Rtp::kVideoMtuSize);
|
||||
_rtp_encoder = std::make_shared<CommonRtpEncoder>();
|
||||
auto video_mtu = s_video_mtu;
|
||||
if (!ps_or_ts) {
|
||||
// 确保ts rtp负载部分长度是188的倍数
|
||||
video_mtu = RtpPacket::kRtpHeaderSize + (s_video_mtu - (s_video_mtu % 188));
|
||||
if (video_mtu > s_video_mtu) {
|
||||
video_mtu -= 188;
|
||||
}
|
||||
}
|
||||
_rtp_encoder->setRtpInfo(ssrc, video_mtu, 90000, payload_type);
|
||||
auto ring = std::make_shared<RtpRing::RingType>();
|
||||
ring->setDelegate(std::make_shared<RingDelegateHelper>([this](RtpPacket::Ptr rtp, bool is_key) { onRTP(std::move(rtp), is_key); }));
|
||||
|
||||
@@ -16,11 +16,19 @@
|
||||
#include "Record/MPEG.h"
|
||||
#include "Common/MediaSink.h"
|
||||
|
||||
namespace mediakit{
|
||||
namespace mediakit {
|
||||
|
||||
class CommonRtpEncoder;
|
||||
class PSEncoderImp : public MpegMuxer{
|
||||
|
||||
class PSEncoderImp : public MpegMuxer {
|
||||
public:
|
||||
PSEncoderImp(uint32_t ssrc, uint8_t payload_type = 96);
|
||||
/**
|
||||
* 创建psh或ts rtp编码器
|
||||
* @param ssrc rtp的ssrc
|
||||
* @param payload_type rtp的pt
|
||||
* @param ps_or_ts true: ps, false: ts
|
||||
*/
|
||||
PSEncoderImp(uint32_t ssrc, uint8_t payload_type = 96, bool ps_or_ts = true);
|
||||
~PSEncoderImp() override;
|
||||
|
||||
protected:
|
||||
|
||||
@@ -34,6 +34,12 @@ bool RawEncoderImp::addTrack(const Track::Ptr &track) {
|
||||
auto ring = std::make_shared<RtpRing::RingType>();
|
||||
ring->setDelegate(std::make_shared<RingDelegateHelper>([this](RtpPacket::Ptr rtp, bool is_key) { onRTP(std::move(rtp), true); }));
|
||||
_rtp_encoder->setRtpRing(std::move(ring));
|
||||
if (track->getCodecId() == CodecG711A || track->getCodecId() == CodecG711U) {
|
||||
GET_CONFIG(uint32_t, dur_ms, RtpProxy::kRtpG711DurMs);
|
||||
Any param;
|
||||
param.set<uint32_t>(dur_ms);
|
||||
_rtp_encoder->setOpt(RtpCodec::RTP_ENCODER_PKT_DUR_MS, param);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +40,9 @@ private:
|
||||
|
||||
class RtpCachePS : public RtpCache, public PSEncoderImp {
|
||||
public:
|
||||
RtpCachePS(onFlushed cb, uint32_t ssrc, uint8_t payload_type = 96) : RtpCache(std::move(cb)), PSEncoderImp(ssrc, payload_type) {};
|
||||
RtpCachePS(onFlushed cb, uint32_t ssrc, uint8_t payload_type = 96, bool ps_or_ts = true) :
|
||||
RtpCache(std::move(cb)), PSEncoderImp(ssrc, ps_or_ts ? payload_type : Rtsp::PT_MP2T, ps_or_ts) {};
|
||||
|
||||
void flush() override;
|
||||
|
||||
protected:
|
||||
@@ -56,6 +58,7 @@ protected:
|
||||
void onRTP(toolkit::Buffer::Ptr rtp, bool is_key = false) override;
|
||||
};
|
||||
|
||||
}//namespace mediakit
|
||||
} //namespace mediakit
|
||||
|
||||
#endif//ENABLE_RTPPROXY
|
||||
#endif //ZLMEDIAKIT_RTPCACHE_H
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#if defined(ENABLE_RTPPROXY)
|
||||
#include "GB28181Process.h"
|
||||
#include "RtpProcess.h"
|
||||
#include "RtpSelector.h"
|
||||
#include "Http/HttpTSPlayer.h"
|
||||
#include "Util/File.h"
|
||||
#include "Common/config.h"
|
||||
@@ -255,6 +256,9 @@ void RtpProcess::emitOnPublish() {
|
||||
}
|
||||
if (err.empty()) {
|
||||
strong_self->_muxer = std::make_shared<MultiMediaSourceMuxer>(strong_self->_media_info, 0.0f, option);
|
||||
if (!option.stream_replace.empty()) {
|
||||
RtpSelector::Instance().addStreamReplace(strong_self->_media_info.stream, option.stream_replace);
|
||||
}
|
||||
if (strong_self->_only_audio) {
|
||||
strong_self->_muxer->setOnlyAudio();
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ INSTANCE_IMP(RtpSelector);
|
||||
void RtpSelector::clear(){
|
||||
lock_guard<decltype(_mtx_map)> lck(_mtx_map);
|
||||
_map_rtp_process.clear();
|
||||
_map_stream_replace.clear();
|
||||
}
|
||||
|
||||
bool RtpSelector::getSSRC(const char *data, size_t data_len, uint32_t &ssrc){
|
||||
@@ -36,17 +37,23 @@ bool RtpSelector::getSSRC(const char *data, size_t data_len, uint32_t &ssrc){
|
||||
|
||||
RtpProcess::Ptr RtpSelector::getProcess(const string &stream_id,bool makeNew) {
|
||||
lock_guard<decltype(_mtx_map)> lck(_mtx_map);
|
||||
auto it = _map_rtp_process.find(stream_id);
|
||||
string stream_id_origin = stream_id;
|
||||
auto it_replace = _map_stream_replace.find(stream_id);
|
||||
if (it_replace != _map_stream_replace.end()) {
|
||||
stream_id_origin = it_replace->second;
|
||||
}
|
||||
|
||||
auto it = _map_rtp_process.find(stream_id_origin);
|
||||
if (it == _map_rtp_process.end() && !makeNew) {
|
||||
return nullptr;
|
||||
}
|
||||
if (it != _map_rtp_process.end() && makeNew) {
|
||||
//已经被其他线程持有了,不得再被持有,否则会存在线程安全的问题
|
||||
throw ProcessExisted(StrPrinter << "RtpProcess(" << stream_id << ") already existed");
|
||||
throw ProcessExisted(StrPrinter << "RtpProcess(" << stream_id_origin << ") already existed");
|
||||
}
|
||||
RtpProcessHelper::Ptr &ref = _map_rtp_process[stream_id];
|
||||
RtpProcessHelper::Ptr &ref = _map_rtp_process[stream_id_origin];
|
||||
if (!ref) {
|
||||
ref = std::make_shared<RtpProcessHelper>(stream_id, shared_from_this());
|
||||
ref = std::make_shared<RtpProcessHelper>(stream_id_origin, shared_from_this());
|
||||
ref->attachEvent();
|
||||
createTimer();
|
||||
}
|
||||
@@ -81,10 +88,25 @@ void RtpSelector::delProcess(const string &stream_id,const RtpProcess *ptr) {
|
||||
}
|
||||
process = it->second->getProcess();
|
||||
_map_rtp_process.erase(it);
|
||||
delStreamReplace(stream_id);
|
||||
}
|
||||
process->onDetach();
|
||||
}
|
||||
|
||||
void RtpSelector::addStreamReplace(const string &stream_id, const std::string &stream_replace) {
|
||||
lock_guard<decltype(_mtx_map)> lck(_mtx_map);
|
||||
_map_stream_replace[stream_replace] = stream_id;
|
||||
}
|
||||
|
||||
void RtpSelector::delStreamReplace(const string &stream_id) {
|
||||
for (auto it = _map_stream_replace.begin(); it != _map_stream_replace.end(); ++it) {
|
||||
if (it->second == stream_id) {
|
||||
_map_stream_replace.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RtpSelector::onManager() {
|
||||
List<RtpProcess::Ptr> clear_list;
|
||||
{
|
||||
@@ -96,6 +118,7 @@ void RtpSelector::onManager() {
|
||||
}
|
||||
WarnL << "RtpProcess timeout:" << it->first;
|
||||
clear_list.emplace_back(it->second->getProcess());
|
||||
delStreamReplace(it->first);
|
||||
it = _map_rtp_process.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,14 +70,18 @@ public:
|
||||
*/
|
||||
void delProcess(const std::string &stream_id, const RtpProcess *ptr);
|
||||
|
||||
void addStreamReplace(const std::string &stream_id, const std::string &stream_replace);
|
||||
|
||||
private:
|
||||
void onManager();
|
||||
void createTimer();
|
||||
void delStreamReplace(const std::string &stream_id);
|
||||
|
||||
private:
|
||||
toolkit::Timer::Ptr _timer;
|
||||
std::recursive_mutex _mtx_map;
|
||||
std::unordered_map<std::string,RtpProcessHelper::Ptr> _map_rtp_process;
|
||||
std::unordered_map<std::string,std::string> _map_stream_replace;
|
||||
};
|
||||
|
||||
}//namespace mediakit
|
||||
|
||||
@@ -40,10 +40,11 @@ void RtpSender::startSend(const MediaSourceEvent::SendRtpArgs &args, const funct
|
||||
if (!_interface) {
|
||||
//重连时不重新创建对象
|
||||
auto lam = [this](std::shared_ptr<List<Buffer::Ptr>> list) { onFlushRtpList(std::move(list)); };
|
||||
if (args.use_ps) {
|
||||
_interface = std::make_shared<RtpCachePS>(lam, atoi(args.ssrc.data()), args.pt);
|
||||
} else {
|
||||
_interface = std::make_shared<RtpCacheRaw>(lam, atoi(args.ssrc.data()), args.pt, args.only_audio);
|
||||
switch (args.type) {
|
||||
case MediaSourceEvent::SendRtpArgs::kRtpPS: _interface = std::make_shared<RtpCachePS>(lam, atoi(args.ssrc.data()), args.pt, true); break;
|
||||
case MediaSourceEvent::SendRtpArgs::kRtpTS: _interface = std::make_shared<RtpCachePS>(lam, atoi(args.ssrc.data()), args.pt, false); break;
|
||||
case MediaSourceEvent::SendRtpArgs::kRtpRAW: _interface = std::make_shared<RtpCacheRaw>(lam, atoi(args.ssrc.data()), args.pt, args.only_audio); break;
|
||||
default: CHECK(0, "invalid rtp type:" + to_string(args.type)); break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -60,6 +60,7 @@ void RtpSession::onError(const SockException &err) {
|
||||
WarnP(this) << _stream_id << " " << err;
|
||||
if (_process) {
|
||||
RtpSelector::Instance().delProcess(_stream_id, _process.get());
|
||||
_process = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -272,4 +273,4 @@ const char *RtpSession::searchByPsHeaderFlag(const char *data, size_t len) {
|
||||
}
|
||||
|
||||
}//namespace mediakit
|
||||
#endif//defined(ENABLE_RTPPROXY)
|
||||
#endif//defined(ENABLE_RTPPROXY)
|
||||
|
||||
@@ -93,6 +93,17 @@ public:
|
||||
|
||||
RtpInfo &getRtpInfo() { return *_rtp_info; }
|
||||
|
||||
enum {
|
||||
RTP_ENCODER_PKT_DUR_MS = 1 // 主要应用于g711 rtp 打包器每个包的时间长度,option_value 为int*, option_len 为4
|
||||
};
|
||||
/**
|
||||
* @brief 设置rtp打包器与解包器的相关参数,主要应用与g711 rtp 打包器,使用方法类似setsockopt
|
||||
*
|
||||
* @param opt 设置的选项
|
||||
* @param param 设置的参数
|
||||
*/
|
||||
virtual void setOpt(int opt, const toolkit::Any ¶m) {};
|
||||
|
||||
private:
|
||||
std::unique_ptr<RtpInfo> _rtp_info;
|
||||
};
|
||||
|
||||
@@ -16,41 +16,17 @@ SrtSession::SrtSession(const Socket::Ptr &sock)
|
||||
// TraceL<<"after addr len "<<addr_len<<" family "<<_peer_addr.ss_family;
|
||||
}
|
||||
|
||||
EventPoller::Ptr SrtSession::queryPoller(const Buffer::Ptr &buffer) {
|
||||
uint8_t *data = (uint8_t *)buffer->data();
|
||||
size_t size = buffer->size();
|
||||
|
||||
if (DataPacket::isDataPacket(data, size)) {
|
||||
uint32_t socket_id = DataPacket::getSocketID(data, size);
|
||||
auto trans = SrtTransportManager::Instance().getItem(std::to_string(socket_id));
|
||||
return trans ? trans->getPoller() : nullptr;
|
||||
}
|
||||
|
||||
if (HandshakePacket::isHandshakePacket(data, size)) {
|
||||
auto type = HandshakePacket::getHandshakeType(data, size);
|
||||
if (type == HandshakePacket::HS_TYPE_INDUCTION) {
|
||||
// 握手第一阶段
|
||||
return nullptr;
|
||||
} else if (type == HandshakePacket::HS_TYPE_CONCLUSION) {
|
||||
// 握手第二阶段
|
||||
uint32_t sync_cookie = HandshakePacket::getSynCookie(data, size);
|
||||
auto trans = SrtTransportManager::Instance().getHandshakeItem(std::to_string(sync_cookie));
|
||||
return trans ? trans->getPoller() : nullptr;
|
||||
} else {
|
||||
WarnL << " not reach there";
|
||||
}
|
||||
} else {
|
||||
uint32_t socket_id = ControlPacket::getSocketID(data, size);
|
||||
auto trans = SrtTransportManager::Instance().getItem(std::to_string(socket_id));
|
||||
return trans ? trans->getPoller() : nullptr;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void SrtSession::attachServer(const toolkit::Server &server) {
|
||||
SockUtil::setRecvBuf(getSock()->rawFD(), 1024 * 1024);
|
||||
}
|
||||
|
||||
extern SrtTransport::Ptr querySrtTransport(uint8_t *data, size_t size, const EventPoller::Ptr& poller);
|
||||
|
||||
EventPoller::Ptr SrtSession::queryPoller(const Buffer::Ptr &buffer) {
|
||||
auto transport = querySrtTransport((uint8_t *)buffer->data(), buffer->size(), nullptr);
|
||||
return transport ? transport->getPoller() : nullptr;
|
||||
}
|
||||
|
||||
void SrtSession::onRecv(const Buffer::Ptr &buffer) {
|
||||
uint8_t *data = (uint8_t *)buffer->data();
|
||||
size_t size = buffer->size();
|
||||
@@ -58,45 +34,7 @@ void SrtSession::onRecv(const Buffer::Ptr &buffer) {
|
||||
if (_find_transport) {
|
||||
//只允许寻找一次transport
|
||||
_find_transport = false;
|
||||
|
||||
if (DataPacket::isDataPacket(data, size)) {
|
||||
uint32_t socket_id = DataPacket::getSocketID(data, size);
|
||||
auto trans = SrtTransportManager::Instance().getItem(std::to_string(socket_id));
|
||||
if (trans) {
|
||||
_transport = std::move(trans);
|
||||
} else {
|
||||
WarnL << " data packet not find transport ";
|
||||
}
|
||||
}
|
||||
|
||||
if (HandshakePacket::isHandshakePacket(data, size)) {
|
||||
auto type = HandshakePacket::getHandshakeType(data, size);
|
||||
if (type == HandshakePacket::HS_TYPE_INDUCTION) {
|
||||
// 握手第一阶段
|
||||
_transport = std::make_shared<SrtTransportImp>(getPoller());
|
||||
|
||||
} else if (type == HandshakePacket::HS_TYPE_CONCLUSION) {
|
||||
// 握手第二阶段
|
||||
uint32_t sync_cookie = HandshakePacket::getSynCookie(data, size);
|
||||
auto trans = SrtTransportManager::Instance().getHandshakeItem(std::to_string(sync_cookie));
|
||||
if (trans) {
|
||||
_transport = std::move(trans);
|
||||
} else {
|
||||
WarnL << " hanshake packet not find transport ";
|
||||
}
|
||||
} else {
|
||||
WarnL << " not reach there";
|
||||
}
|
||||
} else {
|
||||
uint32_t socket_id = ControlPacket::getSocketID(data, size);
|
||||
auto trans = SrtTransportManager::Instance().getItem(std::to_string(socket_id));
|
||||
if (trans) {
|
||||
_transport = std::move(trans);
|
||||
} else {
|
||||
WarnL << " not find transport";
|
||||
}
|
||||
}
|
||||
|
||||
_transport = querySrtTransport(data, size, getPoller());
|
||||
if (_transport) {
|
||||
_transport->setSession(static_pointer_cast<Session>(shared_from_this()));
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ void SrtTransport::switchToOtherTransport(uint8_t *buf, int len, uint32_t socket
|
||||
BufferRaw::Ptr tmp = BufferRaw::create();
|
||||
struct sockaddr_storage tmp_addr = *addr;
|
||||
tmp->assign((char *)buf, len);
|
||||
auto trans = SrtTransportManager::Instance().getItem(std::to_string(socketid));
|
||||
auto trans = SrtTransportManager::Instance().getItem(socketid);
|
||||
if (trans) {
|
||||
trans->getPoller()->async([tmp, tmp_addr, trans] {
|
||||
trans->inputSockData((uint8_t *)tmp->data(), tmp->size(), (struct sockaddr_storage *)&tmp_addr);
|
||||
@@ -700,30 +700,30 @@ void SrtTransport::sendPacket(Buffer::Ptr pkt, bool flush) {
|
||||
}
|
||||
}
|
||||
|
||||
std::string SrtTransport::getIdentifier() {
|
||||
std::string SrtTransport::getIdentifier() const {
|
||||
return _selected_session ? _selected_session->getIdentifier() : "";
|
||||
}
|
||||
|
||||
void SrtTransport::registerSelfHandshake() {
|
||||
SrtTransportManager::Instance().addHandshakeItem(std::to_string(_sync_cookie), shared_from_this());
|
||||
SrtTransportManager::Instance().addHandshakeItem(_sync_cookie, shared_from_this());
|
||||
}
|
||||
|
||||
void SrtTransport::unregisterSelfHandshake() {
|
||||
if (_sync_cookie == 0) {
|
||||
return;
|
||||
}
|
||||
SrtTransportManager::Instance().removeHandshakeItem(std::to_string(_sync_cookie));
|
||||
SrtTransportManager::Instance().removeHandshakeItem(_sync_cookie);
|
||||
}
|
||||
|
||||
void SrtTransport::registerSelf() {
|
||||
if (_socket_id == 0) {
|
||||
return;
|
||||
}
|
||||
SrtTransportManager::Instance().addItem(std::to_string(_socket_id), shared_from_this());
|
||||
SrtTransportManager::Instance().addItem(_socket_id, shared_from_this());
|
||||
}
|
||||
|
||||
void SrtTransport::unregisterSelf() {
|
||||
SrtTransportManager::Instance().removeItem(std::to_string(_socket_id));
|
||||
SrtTransportManager::Instance().removeItem(_socket_id);
|
||||
}
|
||||
|
||||
void SrtTransport::onShutdown(const SockException &ex) {
|
||||
@@ -739,7 +739,7 @@ void SrtTransport::onShutdown(const SockException &ex) {
|
||||
}
|
||||
}
|
||||
|
||||
size_t SrtTransport::getPayloadSize() {
|
||||
size_t SrtTransport::getPayloadSize() const {
|
||||
size_t ret = (_mtu - 28 - 16) / 188 * 188;
|
||||
return ret;
|
||||
}
|
||||
@@ -792,15 +792,13 @@ SrtTransportManager &SrtTransportManager::Instance() {
|
||||
return s_instance;
|
||||
}
|
||||
|
||||
void SrtTransportManager::addItem(const std::string &key, const SrtTransport::Ptr &ptr) {
|
||||
void SrtTransportManager::addItem(const uint32_t key, const SrtTransport::Ptr &ptr) {
|
||||
std::lock_guard<std::mutex> lck(_mtx);
|
||||
_map[key] = ptr;
|
||||
}
|
||||
|
||||
SrtTransport::Ptr SrtTransportManager::getItem(const std::string &key) {
|
||||
if (key.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
SrtTransport::Ptr SrtTransportManager::getItem(const uint32_t key) {
|
||||
assert(key > 0);
|
||||
std::lock_guard<std::mutex> lck(_mtx);
|
||||
auto it = _map.find(key);
|
||||
if (it == _map.end()) {
|
||||
@@ -809,25 +807,23 @@ SrtTransport::Ptr SrtTransportManager::getItem(const std::string &key) {
|
||||
return it->second.lock();
|
||||
}
|
||||
|
||||
void SrtTransportManager::removeItem(const std::string &key) {
|
||||
void SrtTransportManager::removeItem(const uint32_t key) {
|
||||
std::lock_guard<std::mutex> lck(_mtx);
|
||||
_map.erase(key);
|
||||
}
|
||||
|
||||
void SrtTransportManager::addHandshakeItem(const std::string &key, const SrtTransport::Ptr &ptr) {
|
||||
void SrtTransportManager::addHandshakeItem(const uint32_t key, const SrtTransport::Ptr &ptr) {
|
||||
std::lock_guard<std::mutex> lck(_handshake_mtx);
|
||||
_handshake_map[key] = ptr;
|
||||
}
|
||||
|
||||
void SrtTransportManager::removeHandshakeItem(const std::string &key) {
|
||||
void SrtTransportManager::removeHandshakeItem(const uint32_t key) {
|
||||
std::lock_guard<std::mutex> lck(_handshake_mtx);
|
||||
_handshake_map.erase(key);
|
||||
}
|
||||
|
||||
SrtTransport::Ptr SrtTransportManager::getHandshakeItem(const std::string &key) {
|
||||
if (key.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
SrtTransport::Ptr SrtTransportManager::getHandshakeItem(const uint32_t key) {
|
||||
assert(key > 0);
|
||||
std::lock_guard<std::mutex> lck(_handshake_mtx);
|
||||
auto it = _handshake_map.find(key);
|
||||
if (it == _handshake_map.end()) {
|
||||
|
||||
@@ -45,7 +45,7 @@ public:
|
||||
virtual void inputSockData(uint8_t *buf, int len, struct sockaddr_storage *addr);
|
||||
virtual void onSendTSData(const Buffer::Ptr &buffer, bool flush);
|
||||
|
||||
std::string getIdentifier();
|
||||
std::string getIdentifier() const;
|
||||
void unregisterSelf();
|
||||
void unregisterSelfHandshake();
|
||||
|
||||
@@ -89,7 +89,7 @@ private:
|
||||
void sendShutDown();
|
||||
void sendMsgDropReq(uint32_t first, uint32_t last);
|
||||
|
||||
size_t getPayloadSize();
|
||||
size_t getPayloadSize() const;
|
||||
|
||||
void createTimerForCheckAlive();
|
||||
|
||||
@@ -164,23 +164,23 @@ private:
|
||||
class SrtTransportManager {
|
||||
public:
|
||||
static SrtTransportManager &Instance();
|
||||
SrtTransport::Ptr getItem(const std::string &key);
|
||||
void addItem(const std::string &key, const SrtTransport::Ptr &ptr);
|
||||
void removeItem(const std::string &key);
|
||||
SrtTransport::Ptr getItem(const uint32_t key);
|
||||
void addItem(const uint32_t key, const SrtTransport::Ptr &ptr);
|
||||
void removeItem(const uint32_t key);
|
||||
|
||||
void addHandshakeItem(const std::string &key, const SrtTransport::Ptr &ptr);
|
||||
void removeHandshakeItem(const std::string &key);
|
||||
SrtTransport::Ptr getHandshakeItem(const std::string &key);
|
||||
void addHandshakeItem(const uint32_t key, const SrtTransport::Ptr &ptr);
|
||||
void removeHandshakeItem(const uint32_t key);
|
||||
SrtTransport::Ptr getHandshakeItem(const uint32_t key);
|
||||
|
||||
private:
|
||||
SrtTransportManager() = default;
|
||||
|
||||
private:
|
||||
std::mutex _mtx;
|
||||
std::unordered_map<std::string, std::weak_ptr<SrtTransport>> _map;
|
||||
std::unordered_map<uint32_t , std::weak_ptr<SrtTransport>> _map;
|
||||
|
||||
std::mutex _handshake_mtx;
|
||||
std::unordered_map<std::string, std::weak_ptr<SrtTransport>> _handshake_map;
|
||||
std::unordered_map<uint32_t, std::weak_ptr<SrtTransport>> _handshake_map;
|
||||
};
|
||||
|
||||
} // namespace SRT
|
||||
|
||||
@@ -24,6 +24,32 @@ SrtTransportImp::~SrtTransportImp() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
SrtTransport::Ptr querySrtTransport(uint8_t *data, size_t size, const EventPoller::Ptr& poller) {
|
||||
if (DataPacket::isDataPacket(data, size)) {
|
||||
uint32_t socket_id = DataPacket::getSocketID(data, size);
|
||||
return SrtTransportManager::Instance().getItem(socket_id);
|
||||
}
|
||||
|
||||
if (HandshakePacket::isHandshakePacket(data, size)) {
|
||||
auto type = HandshakePacket::getHandshakeType(data, size);
|
||||
if (type == HandshakePacket::HS_TYPE_INDUCTION) {
|
||||
// 握手第一阶段
|
||||
return poller ? std::make_shared<SrtTransportImp>(poller) : nullptr;
|
||||
}
|
||||
|
||||
if (type == HandshakePacket::HS_TYPE_CONCLUSION) {
|
||||
// 握手第二阶段
|
||||
uint32_t sync_cookie = HandshakePacket::getSynCookie(data, size);
|
||||
return SrtTransportManager::Instance().getHandshakeItem(sync_cookie);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t socket_id = ControlPacket::getSocketID(data, size);
|
||||
return SrtTransportManager::Instance().getItem(socket_id);
|
||||
}
|
||||
|
||||
|
||||
void SrtTransportImp::onHandShakeFinished(std::string &streamid, struct sockaddr_storage *addr) {
|
||||
SrtTransport::onHandShakeFinished(streamid,addr);
|
||||
// TODO parse stream id like this zlmediakit.com/live/test?token=1213444&type=push
|
||||
|
||||
@@ -55,6 +55,9 @@ const string kStartBitrate = RTC_FIELD "start_bitrate";
|
||||
const string kMaxBitrate = RTC_FIELD "max_bitrate";
|
||||
const string kMinBitrate = RTC_FIELD "min_bitrate";
|
||||
|
||||
// 数据通道设置
|
||||
const string kDataChannelEcho = RTC_FIELD "datachannel_echo";
|
||||
|
||||
static onceToken token([]() {
|
||||
mINI::Instance()[kTimeOutSec] = 15;
|
||||
mINI::Instance()[kExternIP] = "";
|
||||
@@ -65,6 +68,8 @@ static onceToken token([]() {
|
||||
mINI::Instance()[kStartBitrate] = 0;
|
||||
mINI::Instance()[kMaxBitrate] = 0;
|
||||
mINI::Instance()[kMinBitrate] = 0;
|
||||
|
||||
mINI::Instance()[kDataChannelEcho] = true;
|
||||
});
|
||||
|
||||
} // namespace RTC
|
||||
@@ -201,9 +206,26 @@ void WebRtcTransport::OnDtlsTransportConnected(
|
||||
onStartWebRTC();
|
||||
}
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct DtlsHeader {
|
||||
uint8_t content_type;
|
||||
uint16_t dtls_version;
|
||||
uint16_t epoch;
|
||||
uint8_t seq[6];
|
||||
uint16_t length;
|
||||
uint8_t payload[1];
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
void WebRtcTransport::OnDtlsTransportSendData(
|
||||
const RTC::DtlsTransport *dtlsTransport, const uint8_t *data, size_t len) {
|
||||
sendSockData((char *)data, len, nullptr);
|
||||
size_t offset = 0;
|
||||
while(offset < len) {
|
||||
auto *header = reinterpret_cast<const DtlsHeader *>(data + offset);
|
||||
auto length = ntohs(header->length) + offsetof(DtlsHeader, payload);
|
||||
sendSockData((char *)data + offset, length, nullptr);
|
||||
offset += length;
|
||||
}
|
||||
}
|
||||
|
||||
void WebRtcTransport::OnDtlsTransportConnecting(const RTC::DtlsTransport *dtlsTransport) {
|
||||
@@ -233,22 +255,47 @@ void WebRtcTransport::OnDtlsTransportApplicationDataReceived(
|
||||
#ifdef ENABLE_SCTP
|
||||
void WebRtcTransport::OnSctpAssociationConnecting(RTC::SctpAssociation *sctpAssociation) {
|
||||
TraceL << getIdentifier();
|
||||
try {
|
||||
NOTICE_EMIT(BroadcastRtcSctpConnectArgs, Broadcast::kBroadcastRtcSctpConnecting, *this);
|
||||
} catch (std::exception &ex) {
|
||||
WarnL << "Exception occurred: " << ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void WebRtcTransport::OnSctpAssociationConnected(RTC::SctpAssociation *sctpAssociation) {
|
||||
InfoL << getIdentifier();
|
||||
try {
|
||||
NOTICE_EMIT(BroadcastRtcSctpConnectArgs, Broadcast::kBroadcastRtcSctpConnected, *this);
|
||||
} catch (std::exception &ex) {
|
||||
WarnL << "Exception occurred: " << ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void WebRtcTransport::OnSctpAssociationFailed(RTC::SctpAssociation *sctpAssociation) {
|
||||
WarnL << getIdentifier();
|
||||
try {
|
||||
NOTICE_EMIT(BroadcastRtcSctpConnectArgs, Broadcast::kBroadcastRtcSctpFailed, *this);
|
||||
} catch (std::exception &ex) {
|
||||
WarnL << "Exception occurred: " << ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void WebRtcTransport::OnSctpAssociationClosed(RTC::SctpAssociation *sctpAssociation) {
|
||||
InfoL << getIdentifier();
|
||||
try {
|
||||
NOTICE_EMIT(BroadcastRtcSctpConnectArgs, Broadcast::kBroadcastRtcSctpClosed, *this);
|
||||
} catch (std::exception &ex) {
|
||||
WarnL << "Exception occurred: " << ex.what();
|
||||
}
|
||||
}
|
||||
|
||||
void WebRtcTransport::OnSctpAssociationSendData(
|
||||
RTC::SctpAssociation *sctpAssociation, const uint8_t *data, size_t len) {
|
||||
try {
|
||||
NOTICE_EMIT(BroadcastRtcSctpSendArgs, Broadcast::kBroadcastRtcSctpSend, *this, data, len);
|
||||
} catch (std::exception &ex) {
|
||||
WarnL << "Exception occurred: " << ex.what();
|
||||
}
|
||||
_dtls_transport->SendApplicationData(data, len);
|
||||
}
|
||||
|
||||
@@ -257,8 +304,18 @@ void WebRtcTransport::OnSctpAssociationMessageReceived(
|
||||
InfoL << getIdentifier() << " " << streamId << " " << ppid << " " << len << " " << string((char *)msg, len);
|
||||
RTC::SctpStreamParameters params;
|
||||
params.streamId = streamId;
|
||||
// 回显数据
|
||||
_sctp->SendSctpMessage(params, ppid, msg, len);
|
||||
|
||||
GET_CONFIG(bool, datachannel_echo, Rtc::kDataChannelEcho);
|
||||
if (datachannel_echo) {
|
||||
// 回显数据
|
||||
_sctp->SendSctpMessage(params, ppid, msg, len);
|
||||
}
|
||||
|
||||
try {
|
||||
NOTICE_EMIT(BroadcastRtcSctpReceivedArgs, Broadcast::kBroadcastRtcSctpReceived, *this, streamId, ppid, msg, len);
|
||||
} catch (std::exception &ex) {
|
||||
WarnL << "Exception occurred: " << ex.what();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -618,7 +675,7 @@ void WebRtcTransportImp::onCheckAnswer(RtcSession &sdp) {
|
||||
});
|
||||
for (auto &m : sdp.media) {
|
||||
m.addr.reset();
|
||||
m.addr.address = extern_ips.empty() ? SockUtil::get_local_ip() : extern_ips[0];
|
||||
m.addr.address = extern_ips.empty() ? _localIp.empty() ? SockUtil::get_local_ip() : _localIp : extern_ips[0];
|
||||
m.rtcp_addr.reset();
|
||||
m.rtcp_addr.address = m.addr.address;
|
||||
|
||||
@@ -713,7 +770,7 @@ void WebRtcTransportImp::onRtcConfigure(RtcConfigure &configure) const {
|
||||
return ret;
|
||||
});
|
||||
if (extern_ips.empty()) {
|
||||
std::string local_ip = SockUtil::get_local_ip();
|
||||
std::string local_ip = _localIp.empty() ? SockUtil::get_local_ip() : _localIp;
|
||||
if (local_udp_port) { configure.addCandidate(*makeIceCandidate(local_ip, local_udp_port, 120, "udp")); }
|
||||
if (local_tcp_port) { configure.addCandidate(*makeIceCandidate(local_ip, local_tcp_port, _preferred_tcp ? 125 : 115, "tcp")); }
|
||||
} else {
|
||||
@@ -731,6 +788,10 @@ void WebRtcTransportImp::setIceCandidate(vector<SdpAttrCandidate> cands) {
|
||||
_cands = std::move(cands);
|
||||
}
|
||||
|
||||
void WebRtcTransportImp::setLocalIp(const std::string &localIp) {
|
||||
_localIp = localIp;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
class RtpChannel : public RtpTrackImp, public std::enable_shared_from_this<RtpChannel> {
|
||||
@@ -1222,6 +1283,10 @@ std::string exchangeSdp(const WebRtcInterface &exchanger, const std::string& off
|
||||
return const_cast<WebRtcInterface &>(exchanger).getAnswerSdp(offer);
|
||||
}
|
||||
|
||||
void setLocalIp(const WebRtcInterface& exchanger, const std::string& localIp) {
|
||||
return const_cast<WebRtcInterface &>(exchanger).setLocalIp(localIp);
|
||||
}
|
||||
|
||||
void WebRtcPluginManager::setListener(Listener cb) {
|
||||
lock_guard<mutex> lck(_mtx_creator);
|
||||
_listener = std::move(cb);
|
||||
|
||||
@@ -42,10 +42,13 @@ public:
|
||||
virtual const std::string& getIdentifier() const = 0;
|
||||
virtual const std::string& deleteRandStr() const { static std::string s_null; return s_null; }
|
||||
virtual void setIceCandidate(std::vector<SdpAttrCandidate> cands) {}
|
||||
virtual void setLocalIp(const std::string &localIp) {}
|
||||
};
|
||||
|
||||
std::string exchangeSdp(const WebRtcInterface &exchanger, const std::string& offer);
|
||||
|
||||
void setLocalIp(const WebRtcInterface &exchanger, const std::string &localIp);
|
||||
|
||||
class WebRtcException : public WebRtcInterface {
|
||||
public:
|
||||
WebRtcException(const SockException &ex) : _ex(ex) {};
|
||||
@@ -253,6 +256,7 @@ public:
|
||||
void removeTuple(RTC::TransportTuple* tuple);
|
||||
void safeShutdown(const SockException &ex);
|
||||
|
||||
void setLocalIp(const std::string &localIp) override;
|
||||
protected:
|
||||
void OnIceServerSelectedTuple(const RTC::IceServer *iceServer, RTC::TransportTuple *tuple) override;
|
||||
WebRtcTransportImp(const EventPoller::Ptr &poller,bool preferred_tcp = false);
|
||||
@@ -306,6 +310,8 @@ private:
|
||||
//根据接收rtp的pt获取相关信息
|
||||
std::unordered_map<uint8_t/*pt*/, std::unique_ptr<WrappedMediaTrack>> _pt_to_track;
|
||||
std::vector<SdpAttrCandidate> _cands;
|
||||
//源访问的hostip
|
||||
std::string _localIp;
|
||||
};
|
||||
|
||||
class WebRtcTransportManager {
|
||||
|
||||
Reference in New Issue
Block a user