Merge branch 'master' into dev

This commit is contained in:
xiongguangjie 2023-11-10 13:46:13 +08:00
commit 5f8a77e6b1
52 changed files with 380 additions and 203 deletions

View File

@ -40,9 +40,7 @@ jobs:
# https://github.com/sigstore/cosign-installer
- name: Install cosign
if: github.event_name != 'pull_request'
uses: sigstore/cosign-installer@d6a3abf1bdea83574e28d40543793018b6035605
with:
cosign-release: 'v1.7.1'
uses: sigstore/cosign-installer@d572c9c13673d2e0a26fabf90b5748f36886883f
- name: Set up QEMU
uses: docker/setup-qemu-action@v2

3
.gitmodules vendored
View File

@ -7,3 +7,6 @@
[submodule "3rdpart/jsoncpp"]
path = 3rdpart/jsoncpp
url = https://gitee.com/mirrors/jsoncpp.git
[submodule "www/webassist"]
path = www/webassist
url = https://gitee.com/victor1002/zlm_webassist

View File

@ -7,3 +7,6 @@
[submodule "3rdpart/jsoncpp"]
path = 3rdpart/jsoncpp
url = https://github.com/open-source-parsers/jsoncpp.git
[submodule "www/webassist"]
path = www/webassist
url = https://github.com/1002victor/zlm_webassist

@ -1 +1 @@
Subproject commit b11582c38e8dbbb8d93ca9ce33c9a0b0cd58f59a
Subproject commit 97871cfa78fcd2fae164243a8c653e323385772d

@ -1 +1 @@
Subproject commit 8190e061bc2d95da37479a638aa2c9e483e58ec6
Subproject commit 69098a18b9af0c47549d9a271c054d13ca92b006

@ -1 +1 @@
Subproject commit cdbb3d6b9ea254f454c6e466c5962af5ace01199
Subproject commit 3dc623a899eee3810587fb267dbff770b626a55b

View File

@ -85,3 +85,6 @@ WuPeng <wp@zafu.edu.cn>
[linxiaoyan87](https://github.com/linxiaoyan)
[waken](https://github.com/mc373906408)
[Deepslient](https://github.com/Deepslient)
[imp_rayjay](https://github.com/rayjay214)
[ArmstrongCN](https://github.com/ArmstrongCN)
[leibnewton](https://github.com/leibnewton)

View File

@ -184,6 +184,7 @@ bash build_docker_images.sh
- [BXC_gb28181Player](https://github.com/any12345com/BXC_gb28181Player) C++开发的支持国标GB28181协议的视频流播放器
- WEB管理网站
- [zlm_webassist](https://github.com/1002victor/zlm_webassist) 本项目配套的前后端分离web管理项目
- [AKStreamNVR](https://github.com/langmansh/AKStreamNVR) 前后端分离web项目,支持webrtc播放
- SDK
@ -329,6 +330,10 @@ bash build_docker_images.sh
[linxiaoyan87](https://github.com/linxiaoyan)
[waken](https://github.com/mc373906408)
[Deepslient](https://github.com/Deepslient)
[imp_rayjay](https://github.com/rayjay214)
[ArmstrongCN](https://github.com/ArmstrongCN)
[leibnewton](https://github.com/leibnewton)
[1002victor](https://github.com/1002victor)
同时感谢JetBrains对开源项目的支持本项目使用CLion开发与调试

View File

@ -342,6 +342,7 @@ bash build_docker_images.sh
## Collaborative Projects
- Visual management website
- [A backend management website for this project](https://github.com/1002victor/zlm_webassist)
- [The latest web project with front-end and back-end separation, supporting webrtc playback](https://github.com/langmansh/AKStreamNVR)
- [Management web site based on ZLMediaKit master branch](https://gitee.com/kkkkk5G/MediaServerUI)
- [Management web site based on ZLMediaKit branch](https://github.com/chenxiaolei/ZLMediaKit_NVR_UI)
@ -493,6 +494,10 @@ Thanks to all those who have supported this project in various ways, including b
[linxiaoyan87](https://github.com/linxiaoyan)
[waken](https://github.com/mc373906408)
[Deepslient](https://github.com/Deepslient)
[imp_rayjay](https://github.com/rayjay214)
[ArmstrongCN](https://github.com/ArmstrongCN)
[leibnewton](https://github.com/leibnewton)
[1002victor](https://github.com/1002victor)
Also thank to JetBrains for their support for open source project, we developed and debugged zlmediakit with CLion:

View File

@ -1,9 +1,10 @@
{
"info": {
"_postman_id": "39e8a1df-cc8e-4e3f-bf5e-197c86e7bf0f",
"_postman_id": "509e5f6b-728c-4d5f-b3e8-521d76b2cc7a",
"name": "ZLMediaKit",
"description": "媒体服务器",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "29185956"
},
"item": [
{
@ -918,7 +919,7 @@
"method": "GET",
"header": [],
"url": {
"raw": "{{ZLMediaKit_URL}}/index/api/broadcastMessage?secret={{ZLMediaKit_secret}}&schema=rtsp&vhost={{defaultVhost}}&app=live&stream=test&msg=Hello zlmediakit123",
"raw": "{{ZLMediaKit_URL}}/index/api/broadcastMessage?secret={{ZLMediaKit_secret}}&schema=rtsp&vhost={{defaultVhost}}&app=live&stream=test&msg=Hello ZLMediakit",
"host": [
"{{ZLMediaKit_URL}}"
],
@ -1247,7 +1248,7 @@
},
{
"key": "stamp",
"value": 1000,
"value": "1000",
"description": "要设置的录像播放位置"
}
]
@ -1478,6 +1479,53 @@
},
"response": []
},
{
"name": "创建多路复用RTP服务器(openRtpServerMultiplex)",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{ZLMediaKit_URL}}/index/api/openRtpServer?secret={{ZLMediaKit_secret}}&port=0&tcp_mode=1&stream_id=test",
"host": [
"{{ZLMediaKit_URL}}"
],
"path": [
"index",
"api",
"openRtpServer"
],
"query": [
{
"key": "secret",
"value": "{{ZLMediaKit_secret}}",
"description": "api操作密钥(配置文件配置)"
},
{
"key": "port",
"value": "0",
"description": "绑定的端口0时为随机端口"
},
{
"key": "tcp_mode",
"value": "1",
"description": "tcp模式0时为不启用tcp监听1时为启用tcp监听"
},
{
"key": "stream_id",
"value": "test",
"description": "该端口绑定的流id\n"
},
{
"key": "only_audio",
"value": "0",
"description": "是否为单音频track用于语音对讲",
"disabled": true
}
]
}
},
"response": []
},
{
"name": "连接RTP服务器(connectRtpServer)",
"request": {
@ -1710,10 +1758,16 @@
"value": "obs",
"description": "流id例如 obs"
},
{
"key": "ssrc_multi_send",
"value": "0",
"description": "是否支持同ssrc推流到多个上级服务器,该参数非必选参数 默认false",
"disabled": true
},
{
"key": "ssrc",
"value": "1",
"description": "rtp推流的ssrcssrc不同时可以推流到多个上级服务器"
"description": "rtp推流的ssrc"
},
{
"key": "dst_url",

View File

@ -370,6 +370,7 @@ Value makeMediaSourceJson(MediaSource &media){
obj["loss"] = loss;
}
obj["frames"] = track->getFrames();
obj["duration"] = track->getDuration();
switch(codec_type){
case TrackAudio : {
auto audio_track = dynamic_pointer_cast<AudioTrack>(track);
@ -403,7 +404,7 @@ Value makeMediaSourceJson(MediaSource &media){
}
#if defined(ENABLE_RTPPROXY)
uint16_t openRtpServer(uint16_t local_port, const string &stream_id, int tcp_mode, const string &local_ip, bool re_use_port, uint32_t ssrc, bool only_audio) {
uint16_t openRtpServer(uint16_t local_port, const string &stream_id, int tcp_mode, const string &local_ip, bool re_use_port, uint32_t ssrc, bool only_audio, bool multiplex) {
lock_guard<recursive_mutex> lck(s_rtpServerMapMtx);
if (s_rtpServerMap.find(stream_id) != s_rtpServerMap.end()) {
//为了防止RtpProcess所有权限混乱的问题不允许重复添加相同的stream_id
@ -411,7 +412,7 @@ uint16_t openRtpServer(uint16_t local_port, const string &stream_id, int tcp_mod
}
RtpServer::Ptr server = std::make_shared<RtpServer>();
server->start(local_port, stream_id, (RtpServer::TcpMode)tcp_mode, local_ip.c_str(), re_use_port, ssrc, only_audio);
server->start(local_port, stream_id, (RtpServer::TcpMode)tcp_mode, local_ip.c_str(), re_use_port, ssrc, only_audio, multiplex);
server->setOnDetach([stream_id]() {
//设置rtp超时移除事件
lock_guard<recursive_mutex> lck(s_rtpServerMapMtx);
@ -1182,6 +1183,25 @@ void installWebApi() {
val["port"] = port;
});
api_regist("/index/api/openRtpServerMultiplex", [](API_ARGS_MAP) {
CHECK_SECRET();
CHECK_ARGS("port", "stream_id");
auto stream_id = allArgs["stream_id"];
auto tcp_mode = allArgs["tcp_mode"].as<int>();
if (allArgs["enable_tcp"].as<int>() && !tcp_mode) {
// 兼容老版本请求新版本去除enable_tcp参数并新增tcp_mode参数
tcp_mode = 1;
}
auto port = openRtpServer(
allArgs["port"], stream_id, tcp_mode, "::", true, 0, allArgs["only_audio"].as<bool>(),true);
if (port == 0) {
throw InvalidArgsException("该stream_id已存在");
}
// 回复json
val["port"] = port;
});
api_regist("/index/api/connectRtpServer", [](API_ARGS_MAP_ASYNC) {
CHECK_SECRET();
CHECK_ARGS("stream_id", "dst_url", "dst_port");
@ -1244,6 +1264,7 @@ void installWebApi() {
args.passive = false;
args.dst_url = allArgs["dst_url"];
args.dst_port = allArgs["dst_port"];
args.ssrc_multi_send = allArgs["ssrc_multi_send"].empty() ? false : allArgs["ssrc_multi_send"].as<bool>();
args.ssrc = allArgs["ssrc"];
args.is_udp = allArgs["is_udp"];
args.src_port = allArgs["src_port"];
@ -1491,11 +1512,15 @@ void installWebApi() {
// http://127.0.0.1/index/api/deleteRecordDirectroy?vhost=__defaultVhost__&app=live&stream=ss&period=2020-01-01
api_regist("/index/api/deleteRecordDirectory", [](API_ARGS_MAP) {
CHECK_SECRET();
CHECK_ARGS("vhost", "app", "stream");
CHECK_ARGS("vhost", "app", "stream", "period");
auto tuple = MediaTuple{allArgs["vhost"], allArgs["app"], allArgs["stream"]};
auto record_path = Recorder::getRecordPath(Recorder::type_mp4, tuple, allArgs["customized_path"]);
auto period = allArgs["period"];
record_path = record_path + period + "/";
auto name = allArgs["name"];
if (!name.empty()) {
record_path += name;
}
int result = File::delete_file(record_path.data());
if (result) {
// 不等于0时代表失败
@ -1911,24 +1936,28 @@ void installWebApi() {
void unInstallWebApi(){
{
lock_guard<recursive_mutex> lck(s_proxyMapMtx);
s_proxyMap.clear();
auto proxyMap(std::move(s_proxyMap));
proxyMap.clear();
}
{
lock_guard<recursive_mutex> lck(s_ffmpegMapMtx);
s_ffmpegMap.clear();
auto ffmpegMap(std::move(s_ffmpegMap));
ffmpegMap.clear();
}
{
lock_guard<recursive_mutex> lck(s_proxyPusherMapMtx);
s_proxyPusherMap.clear();
auto proxyPusherMap(std::move(s_proxyPusherMap));
proxyPusherMap.clear();
}
{
#if defined(ENABLE_RTPPROXY)
RtpSelector::Instance().clear();
lock_guard<recursive_mutex> lck(s_rtpServerMapMtx);
s_rtpServerMap.clear();
auto rtpServerMap(std::move(s_rtpServerMap));
rtpServerMap.clear();
#endif
}
NoticeCenter::Instance().delListener(&web_api_tag);

View File

@ -239,7 +239,7 @@ void installWebApi();
void unInstallWebApi();
#if defined(ENABLE_RTPPROXY)
uint16_t openRtpServer(uint16_t local_port, const std::string &stream_id, int tcp_mode, const std::string &local_ip, bool re_use_port, uint32_t ssrc, bool only_audio);
uint16_t openRtpServer(uint16_t local_port, const std::string &stream_id, int tcp_mode, const std::string &local_ip, bool re_use_port, uint32_t ssrc, bool only_audio, bool multiplex=false);
void connectRtpServer(const std::string &stream_id, const std::string &dst_url, uint16_t dst_port, const std::function<void(const toolkit::SockException &ex)> &cb);
bool closeRtpServer(const std::string &stream_id);
#endif

View File

@ -674,7 +674,7 @@ void installWebHook() {
});
});
NoticeCenter::Instance().addListener(&web_hook_tag, Broadcast::KBroadcastRtpServerTimeout, [](BroadcastRtpServerTimeoutArgs) {
NoticeCenter::Instance().addListener(&web_hook_tag, Broadcast::kBroadcastRtpServerTimeout, [](BroadcastRtpServerTimeoutArgs) {
GET_CONFIG(string, rtp_server_timeout, Hook::kOnRtpServerTimeout);
if (!hook_enable || rtp_server_timeout.empty()) {
return;

View File

@ -12,6 +12,7 @@
#include "JemallocUtil.h"
#include "Util/logger.h"
#ifdef USE_JEMALLOC
#include <array>
#include <iostream>
#include <jemalloc/jemalloc.h>
#endif

View File

@ -99,11 +99,13 @@ public:
// rtp采用ps还是es方式
bool use_ps = true;
//发送es流时指定是否只发送纯音频流
bool only_audio = true;
bool only_audio = false;
//tcp被动方式
bool passive = false;
// rtp payload type
uint8_t pt = 96;
//是否支持同ssrc多服务器发送
bool ssrc_multi_send = false;
// 指定rtp ssrc
std::string ssrc;
// 指定本地发送端口

View File

@ -45,6 +45,7 @@ static string getTrackInfoStr(const TrackSource *track_src){
_StrPrinter codec_info;
auto tracks = track_src->getTracks(true);
for (auto &track : tracks) {
track->update();
auto codec_type = track->getTrackType();
codec_info << track->getCodecName();
switch (codec_type) {
@ -294,12 +295,14 @@ void MultiMediaSourceMuxer::startSendRtp(MediaSource &sender, const MediaSourceE
auto ring = _ring;
auto ssrc = args.ssrc;
auto ssrc_multi_send = args.ssrc_multi_send;
auto tracks = getTracks(false);
auto poller = getOwnerPoller(sender);
auto rtp_sender = std::make_shared<RtpSender>(poller);
weak_ptr<MultiMediaSourceMuxer> weak_self = shared_from_this();
rtp_sender->startSend(args, [ssrc, weak_self, rtp_sender, cb, tracks, ring, poller](uint16_t local_port, const SockException &ex) mutable {
rtp_sender->startSend(args, [ssrc,ssrc_multi_send, weak_self, rtp_sender, cb, tracks, ring, poller](uint16_t local_port, const SockException &ex) mutable {
cb(local_port, ex);
auto strong_self = weak_self.lock();
if (!strong_self || ex) {
@ -328,7 +331,10 @@ void MultiMediaSourceMuxer::startSendRtp(MediaSource &sender, const MediaSourceE
// 可能归属线程发生变更
strong_self->getOwnerPoller(MediaSource::NullMediaSource())->async([=]() {
strong_self->_rtp_sender[ssrc] = std::move(reader);
if(!ssrc_multi_send) {
strong_self->_rtp_sender.erase(ssrc);
}
strong_self->_rtp_sender.emplace(ssrc,reader);
});
});
#else

View File

@ -173,7 +173,7 @@ private:
toolkit::Ticker _last_check;
Stamp _stamp[2];
std::weak_ptr<Listener> _track_listener;
std::unordered_map<std::string, RingType::RingReader::Ptr> _rtp_sender;
std::unordered_multimap<std::string, RingType::RingReader::Ptr> _rtp_sender;
FMP4MediaSourceMuxer::Ptr _fmp4;
RtmpMediaSourceMuxer::Ptr _rtmp;
RtspMediaSourceMuxer::Ptr _rtsp;

View File

@ -57,7 +57,7 @@ const string kBroadcastNotFoundStream = "kBroadcastNotFoundStream";
const string kBroadcastStreamNoneReader = "kBroadcastStreamNoneReader";
const string kBroadcastHttpBeforeAccess = "kBroadcastHttpBeforeAccess";
const string kBroadcastSendRtpStopped = "kBroadcastSendRtpStopped";
const string KBroadcastRtpServerTimeout = "KBroadcastRtpServerTimeout";
const string kBroadcastRtpServerTimeout = "kBroadcastRtpServerTimeout";
} // namespace Broadcast

View File

@ -106,7 +106,7 @@ extern const std::string kBroadcastReloadConfig;
#define BroadcastReloadConfigArgs void
// rtp server 超时
extern const std::string KBroadcastRtpServerTimeout;
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
#define ReloadConfigTag ((void *)(0xFF))

View File

@ -36,20 +36,21 @@ void UnicodeToUTF8(char *pOut, const wchar_t *pText) {
return;
}
char CharToInt(char ch) {
char HexCharToBin(char ch) {
if (ch >= '0' && ch <= '9') return (char)(ch - '0');
if (ch >= 'a' && ch <= 'f') return (char)(ch - 'a' + 10);
if (ch >= 'A' && ch <= 'F') return (char)(ch - 'A' + 10);
return -1;
}
char StrToBin(const char *str) {
char tempWord[2];
char chn;
tempWord[0] = CharToInt(str[0]); //make the B to 11 -- 00001011
tempWord[1] = CharToInt(str[1]); //make the 0 to 0 -- 00000000
chn = (tempWord[0] << 4) | tempWord[1]; //to change the BO to 10110000
return chn;
char HexStrToBin(const char *str) {
auto high = HexCharToBin(str[0]);
auto low = HexCharToBin(str[1]);
if (high == -1 || low == -1) {
// 无法把16进制字符串转换为二进制
return -1;
}
return (high << 4) | low;
}
string strCoding::UrlEncode(const string &str) {
@ -70,26 +71,51 @@ string strCoding::UrlEncode(const string &str) {
string strCoding::UrlDecode(const string &str) {
string output;
char tmp[2];
size_t i = 0, len = str.length();
while (i < len) {
if (str[i] == '%') {
if (i > len - 3) {
//防止内存溢出
if (i + 3 > len) {
// %后面必须还有两个字节才会反转义
output.append(str, i, len - i);
break;
}
tmp[0] = str[i + 1];
tmp[1] = str[i + 2];
output += StrToBin(tmp);
i = i + 3;
char ch = HexStrToBin(&(str[i + 1]));
if (ch == -1) {
// %后面两个字节不是16进制字符串转义失败直接拼接3个原始字符
output.append(str, i, 3);
} else {
output += ch;
}
i += 3;
} else {
output += str[i];
i++;
++i;
}
}
return output;
}
#if 0
#include "Util/onceToken.h"
static toolkit::onceToken token([]() {
auto str0 = strCoding::UrlDecode(
"rtsp%3A%2F%2Fadmin%3AJm13317934%25jm%40111.47.84.69%3A554%2FStreaming%2FChannels%2F101%3Ftransportmode%3Dunicast%26amp%3Bprofile%3DProfile_1");
auto str1 = strCoding::UrlDecode("%j1"); // 测试%后面两个字节不是16进制字符串
auto str2 = strCoding::UrlDecode("%a"); // 测试%后面字节数不够
auto str3 = strCoding::UrlDecode("%"); // 测试只有%
auto str4 = strCoding::UrlDecode("%%%"); // 测试多个%
auto str5 = strCoding::UrlDecode("%%%%40"); // 测试多个非法%后恢复正常解析
auto str6 = strCoding::UrlDecode("Jm13317934%jm"); // 测试多个非法%后恢复正常解析
cout << str0 << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
cout << str4 << endl;
cout << str5 << endl;
cout << str6 << endl;
});
#endif
///////////////////////////////windows专用///////////////////////////////////
#if defined(_WIN32)
void UnicodeToGB2312(char* pOut, wchar_t uData)

View File

@ -67,7 +67,10 @@ static void dumpAdtsHeader(const AdtsHeader &hed, uint8_t *out) {
out[6] |= (hed.no_raw_data_blocks_in_frame & 0x03); // 2 bit
}
static void parseAacConfig(const string &config, AdtsHeader &adts) {
static bool parseAacConfig(const string &config, AdtsHeader &adts) {
if (config.size() < 2) {
return false;
}
uint8_t cfg1 = config[0];
uint8_t cfg2 = config[1];
@ -94,6 +97,7 @@ static void parseAacConfig(const string &config, AdtsHeader &adts) {
adts.aac_frame_length = 7;
adts.adts_buffer_fullness = 2047;
adts.no_raw_data_blocks_in_frame = 0;
return true;
}
#endif// ENABLE_MP4
@ -171,7 +175,9 @@ int dumpAacConfig(const string &config, size_t length, uint8_t *out, size_t out_
bool parseAacConfig(const string &config, int &samplerate, int &channels) {
#ifndef ENABLE_MP4
AdtsHeader header;
parseAacConfig(config, header);
if (!parseAacConfig(config, header)) {
return false;
}
samplerate = samplingFrequencyTable[header.sf_index];
channels = header.channel_configuration;
return true;
@ -326,11 +332,14 @@ bool AACTrack::inputFrame_l(const Frame::Ptr &frame) {
return false;
}
void AACTrack::onReady() {
if (_cfg.size() < 2) {
return;
bool AACTrack::update() {
return parseAacConfig(_cfg, _sampleRate, _channel);
}
void AACTrack::onReady() {
if (!parseAacConfig(_cfg, _sampleRate, _channel)) {
_cfg.clear();
}
parseAacConfig(_cfg, _sampleRate, _channel);
}
Track::Ptr AACTrack::clone() {
@ -342,6 +351,7 @@ Sdp::Ptr AACTrack::getSdp() {
WarnL << getCodecName() << " Track未准备好";
return nullptr;
}
update();
return std::make_shared<AACSdp>(getConfig(), getAudioSampleRate(), getAudioChannel(), getBitRate() / 1024);
}

View File

@ -52,6 +52,7 @@ public:
int getAudioSampleRate() const override;
int getAudioSampleBit() const override;
bool inputFrame(const Frame::Ptr &frame) override;
bool update() override;
private:
void onReady();

View File

@ -16,6 +16,7 @@
#include <functional>
#include "Util/List.h"
#include "Util/TimeTicker.h"
#include "Common/Stamp.h"
#include "Network/Buffer.h"
namespace mediakit {
@ -361,11 +362,18 @@ public:
return _gop_interval_ms;
}
int64_t getDuration() const {
std::lock_guard<std::recursive_mutex> lck(_mtx);
return _stamp.getRelativeStamp();
}
private:
void doStatistics(const Frame::Ptr &frame) {
if (!frame->configFrame() && !frame->dropAble()) {
// 忽略配置帧与可丢弃的帧
++_frames;
int64_t out;
_stamp.revise(frame->dts(), frame->pts(), out, out);
if (frame->keyFrame() && frame->getTrackType() == TrackVideo) {
// 遇视频关键帧时统计
++_video_key_frames;
@ -384,6 +392,7 @@ private:
uint64_t _last_frames = 0;
uint64_t _frames = 0;
uint64_t _video_key_frames = 0;
Stamp _stamp;
mutable std::recursive_mutex _mtx;
std::map<void *, FrameWriterInterface::Ptr> _delegates;
};

View File

@ -168,6 +168,10 @@ bool H264Track::inputFrame(const Frame::Ptr &frame) {
return ret;
}
bool H264Track::update() {
return getAVCInfo(_sps, _width, _height, _fps);
}
void H264Track::onReady() {
if (!getAVCInfo(_sps, _width, _height, _fps)) {
_sps.clear();

View File

@ -128,6 +128,7 @@ public:
int getVideoWidth() const override;
float getVideoFps() const override;
bool inputFrame(const Frame::Ptr &frame) override;
bool update() override;
private:
void onReady();

View File

@ -44,13 +44,15 @@ H264Frame::Ptr H264RtpDecoder::obtainFrame() {
bool H264RtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
auto seq = rtp->getSeq();
auto ret = decodeRtp(rtp);
auto last_is_gop = _is_gop;
_is_gop = decodeRtp(rtp);
if (!_gop_dropped && seq != (uint16_t)(_last_seq + 1) && _last_seq) {
_gop_dropped = true;
WarnL << "start drop h264 gop, last seq:" << _last_seq << ", rtp:\r\n" << rtp->dumpString();
}
_last_seq = seq;
return ret;
// 确保有sps rtp的时候gop从sps开始否则从关键帧开始
return _is_gop && !last_is_gop;
}
/*
@ -74,7 +76,7 @@ bool H264RtpDecoder::singleFrame(const RtpPacket::Ptr &rtp, const uint8_t *ptr,
_frame->_buffer.assign("\x00\x00\x00\x01", 4);
_frame->_buffer.append((char *) ptr, size);
_frame->_pts = stamp;
auto key = _frame->keyFrame();
auto key = _frame->keyFrame() || _frame->configFrame();
outputFrame(rtp, _frame);
return key;
}
@ -127,7 +129,7 @@ bool H264RtpDecoder::mergeFu(const RtpPacket::Ptr &rtp, const uint8_t *ptr, ssiz
if (!fu->end_bit) {
//非末尾包
return fu->start_bit ? _frame->keyFrame() : false;
return fu->start_bit ? (_frame->keyFrame() || _frame->configFrame()) : false;
}
//确保下一次fu必须收到第一个包

View File

@ -51,6 +51,7 @@ private:
void outputFrame(const RtpPacket::Ptr &rtp, const H264Frame::Ptr &frame);
private:
bool _is_gop = false;
bool _gop_dropped = false;
bool _fu_dropped = true;
uint16_t _last_seq = 0;

View File

@ -144,6 +144,10 @@ bool H265Track::inputFrame_l(const Frame::Ptr &frame) {
return ret;
}
bool H265Track::update() {
return getHEVCInfo(_vps, _sps, _width, _height, _fps);
}
void H265Track::onReady() {
if (!getHEVCInfo(_vps, _sps, _width, _height, _fps)) {
_vps.clear();

View File

@ -150,6 +150,7 @@ public:
int getVideoHeight() const override;
float getVideoFps() const override;
bool inputFrame(const Frame::Ptr &frame) override;
bool update() override;
private:
void onReady();

View File

@ -163,7 +163,7 @@ bool H265RtpDecoder::mergeFu(const RtpPacket::Ptr &rtp, const uint8_t *ptr, ssiz
if (!e_bit) {
//非末尾包
return s_bit ? _frame->keyFrame() : false;
return s_bit ? (_frame->keyFrame() || _frame->configFrame()) : false;
}
//确保下一次fu必须收到第一个包
@ -175,13 +175,15 @@ bool H265RtpDecoder::mergeFu(const RtpPacket::Ptr &rtp, const uint8_t *ptr, ssiz
bool H265RtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool) {
auto seq = rtp->getSeq();
auto ret = decodeRtp(rtp);
auto last_is_gop = _is_gop;
_is_gop = decodeRtp(rtp);
if (!_gop_dropped && seq != (uint16_t) (_last_seq + 1) && _last_seq) {
_gop_dropped = true;
WarnL << "start drop h265 gop, last seq:" << _last_seq << ", rtp:\r\n" << rtp->dumpString();
}
_last_seq = seq;
return ret;
// 确保有sps rtp的时候gop从sps开始否则从关键帧开始
return _is_gop && !last_is_gop;
}
bool H265RtpDecoder::decodeRtp(const RtpPacket::Ptr &rtp) {
@ -220,7 +222,7 @@ bool H265RtpDecoder::singleFrame(const RtpPacket::Ptr &rtp, const uint8_t *ptr,
_frame->_buffer.assign("\x00\x00\x00\x01", 4);
_frame->_buffer.append((char *) ptr, size);
_frame->_pts = stamp;
auto key = _frame->keyFrame();
auto key = _frame->keyFrame() || _frame->configFrame();
outputFrame(rtp, _frame);
return key;
}

View File

@ -51,6 +51,7 @@ private:
void outputFrame(const RtpPacket::Ptr &rtp, const H265Frame::Ptr &frame);
private:
bool _is_gop = false;
bool _using_donl_field = false;
bool _gop_dropped = false;
bool _fu_dropped = true;

View File

@ -39,6 +39,11 @@ public:
*/
virtual Track::Ptr clone() = 0;
/**
* track信息sps/pps解析
*/
virtual bool update() { return false; }
/**
* sdp
* @return sdp对象

View File

@ -80,7 +80,7 @@ void HttpClient::sendRequest(const string &url) {
}
if (!alive() || host_changed) {
startConnect(host, port, _wait_header_ms);
startConnect(host, port, _wait_header_ms / 1000.0f);
} else {
SockException ex;
onConnect_l(ex);

View File

@ -166,11 +166,11 @@ void HttpSession::onError(const SockException &err) {
if (_is_live_stream) {
// flv/ts播放器
uint64_t duration = _ticker.createdTime() / 1000;
WarnP(this) << "FLV/TS/FMP4播放器(" << _mediaInfo.shortUrl() << ")断开:" << err << ",耗时(s):" << duration;
WarnP(this) << "FLV/TS/FMP4播放器(" << _media_info.shortUrl() << ")断开:" << err << ",耗时(s):" << duration;
GET_CONFIG(uint32_t, iFlowThreshold, General::kFlowThreshold);
if (_total_bytes_usage >= iFlowThreshold * 1024) {
NOTICE_EMIT(BroadcastFlowReportArgs, Broadcast::kBroadcastFlowReport, _mediaInfo, _total_bytes_usage, duration, true, *this);
NOTICE_EMIT(BroadcastFlowReportArgs, Broadcast::kBroadcastFlowReport, _media_info, _total_bytes_usage, duration, true, *this);
}
return;
}
@ -263,9 +263,9 @@ bool HttpSession::checkLiveStream(const string &schema, const string &url_suffix
}
// 解析带上协议+参数完整的url
_mediaInfo.parse(schema + "://" + _parser["Host"] + url);
_media_info.parse(schema + "://" + _parser["Host"] + url);
if (_mediaInfo.app.empty() || _mediaInfo.stream.empty()) {
if (_media_info.app.empty() || _media_info.stream.empty()) {
// url不合法
return false;
}
@ -288,7 +288,7 @@ bool HttpSession::checkLiveStream(const string &schema, const string &url_suffix
}
// 异步查找直播流
MediaSource::findAsync(strong_self->_mediaInfo, strong_self, [weak_self, close_flag, cb](const MediaSource::Ptr &src) {
MediaSource::findAsync(strong_self->_media_info, strong_self, [weak_self, close_flag, cb](const MediaSource::Ptr &src) {
auto strong_self = weak_self.lock();
if (!strong_self) {
// 本对象已经销毁
@ -311,7 +311,7 @@ bool HttpSession::checkLiveStream(const string &schema, const string &url_suffix
}
};
auto flag = NOTICE_EMIT(BroadcastMediaPlayedArgs, Broadcast::kBroadcastMediaPlayed, _mediaInfo, invoker, *this);
auto flag = NOTICE_EMIT(BroadcastMediaPlayedArgs, Broadcast::kBroadcastMediaPlayed, _media_info, invoker, *this);
if (!flag) {
// 该事件无人监听,默认不鉴权
onRes("");

View File

@ -124,7 +124,7 @@ private:
void setSocketFlags();
protected:
MediaInfo _mediaInfo;
MediaInfo _media_info;
private:
bool _is_live_stream = false;

View File

@ -69,7 +69,7 @@ template <typename SessionType>
class SessionCreator {
public:
//返回的Session必须派生于SendInterceptor可以返回null
toolkit::Session::Ptr operator()(const mediakit::Parser &header, const mediakit::HttpSession &parent, const toolkit::Socket::Ptr &pSock){
toolkit::Session::Ptr operator()(const mediakit::Parser &header, const mediakit::HttpSession &parent, const toolkit::Socket::Ptr &pSock, mediakit::WebSocketHeader::Type &data_type){
return std::make_shared<SessionTypeImp<SessionType> >(header,parent,pSock);
}
};
@ -128,7 +128,8 @@ protected:
*/
bool onWebSocketConnect(const mediakit::Parser &header) override{
//创建websocket session类
_session = _creator(header, *this, HttpSessionType::getSock());
auto data_type = DataType;
_session = _creator(header, *this, HttpSessionType::getSock(), data_type);
if (!_session) {
// 此url不允许创建websocket连接
return false;
@ -140,13 +141,13 @@ protected:
//此处截取数据并进行websocket协议打包
std::weak_ptr<WebSocketSessionBase> weakSelf = std::static_pointer_cast<WebSocketSessionBase>(HttpSessionType::shared_from_this());
std::dynamic_pointer_cast<SendInterceptor>(_session)->setOnBeforeSendCB([weakSelf](const toolkit::Buffer::Ptr &buf) {
std::dynamic_pointer_cast<SendInterceptor>(_session)->setOnBeforeSendCB([weakSelf, data_type](const toolkit::Buffer::Ptr &buf) {
auto strongSelf = weakSelf.lock();
if (strongSelf) {
mediakit::WebSocketHeader header;
header._fin = true;
header._reserved = 0;
header._opcode = DataType;
header._opcode = data_type;
header._mask_flag = false;
strongSelf->HttpSessionType::encode(header, buf);
}

View File

@ -70,6 +70,7 @@ void PlayerProxy::setTranslationInfo()
_transtalion_info.stream_info.clear();
auto tracks = _muxer->getTracks();
for (auto &track : tracks) {
track->update();
_transtalion_info.stream_info.emplace_back();
auto &back = _transtalion_info.stream_info.back();
back.bitrate = track->getBitRate();

View File

@ -16,8 +16,10 @@
#include "Extension/AAC.h"
#include "Extension/G711.h"
#include "Extension/Opus.h"
using namespace toolkit;
#include "Extension/JPEG.h"
using namespace std;
using namespace toolkit;
namespace mediakit {
@ -105,8 +107,9 @@ void MP4Demuxer::onVideoTrack(uint32_t track, uint8_t object, int width, int hei
video->inputFrame(std::make_shared<H264FrameNoCacheAble>((char *)config, size, 0, 0,4));
}
}
}
break;
}
case MOV_OBJECT_HEVC: {
auto video = std::make_shared<H265Track>();
_track_to_codec.emplace(track,video);
@ -120,11 +123,16 @@ void MP4Demuxer::onVideoTrack(uint32_t track, uint8_t object, int width, int hei
video->inputFrame(std::make_shared<H265FrameNoCacheAble>((char *) config, size, 0, 0,4));
}
}
break;
}
case MOV_OBJECT_JPEG: {
auto video = std::make_shared<JPEGTrack>();
_track_to_codec.emplace(track,video);
break;
default:
WarnL << "不支持该编码类型的MP4,已忽略:" << getObjectName(object);
break;
}
default: WarnL << "不支持该编码类型的MP4,已忽略:" << getObjectName(object); break;
}
}
@ -243,6 +251,11 @@ Frame::Ptr MP4Demuxer::makeFrame(uint32_t track_id, const Buffer::Ptr &buf, int6
break;
}
case CodecJPEG: {
ret = std::make_shared<JPEGFrame>(buf, (uint64_t)dts, 0, DATA_OFFSET);
break;
}
case CodecAAC: {
AACTrack::Ptr track = dynamic_pointer_cast<AACTrack>(it->second);
assert(track);

View File

@ -198,6 +198,7 @@ bool MP4MuxerInterface::addTrack(const Track::Ptr &track) {
return false;
}
track->update();
switch (track->getCodecId()) {
case CodecG711A:
case CodecG711U:

View File

@ -34,8 +34,12 @@ MP4Recorder::MP4Recorder(const string &path, const string &vhost, const string &
}
MP4Recorder::~MP4Recorder() {
try {
flush();
closeFile();
} catch (std::exception &ex) {
WarnL << ex.what();
}
}
void MP4Recorder::createFile() {

View File

@ -57,6 +57,7 @@ AudioMeta::AudioMeta(const AudioTrack::Ptr &audio) {
}
uint8_t getAudioRtmpFlags(const Track::Ptr &track) {
track->update();
switch (track->getTrackType()) {
case TrackAudio: {
auto audioTrack = std::dynamic_pointer_cast<AudioTrack>(track);
@ -115,6 +116,7 @@ uint8_t getAudioRtmpFlags(const Track::Ptr &track) {
void Metadata::addTrack(AMFValue &metadata, const Track::Ptr &track) {
Metadata::Ptr new_metadata;
track->update();
switch (track->getTrackType()) {
case TrackVideo: {
new_metadata = std::make_shared<VideoMeta>(std::dynamic_pointer_cast<VideoTrack>(track));

View File

@ -52,17 +52,18 @@ void RtmpPlayer::teardown() {
void RtmpPlayer::play(const string &url) {
teardown();
string host_url = findSubString(url.data(), "://", "/");
{
auto pos = url.find_last_of('/');
if (pos != string::npos) {
_stream_id = url.substr(pos + 1);
auto schema = findSubString(url.data(), nullptr, "://");
auto host_url = findSubString(url.data(), "://", "/");
_app = findSubString(url.data(), (host_url + "/").data(), "/");
_stream_id = findSubString(url.data(), (host_url + "/" + _app + "/").data(), NULL);
auto app_second = findSubString(_stream_id.data(), nullptr, "/");
if (!app_second.empty() && app_second.find('?') == std::string::npos) {
// _stream_id存在多级不包含'?', 说明分割符'/'不是url参数的一部分
_app += "/" + app_second;
_stream_id.erase(0, app_second.size() + 1);
}
}
_app = findSubString(url.data(), (host_url + "/").data(), ("/" + _stream_id).data());
_tc_url = string("rtmp://") + host_url + "/" + _app;
if (!_app.size() || !_stream_id.size()) {
_tc_url = schema + "://" + host_url + "/" + _app;
if (_app.empty() || _stream_id.empty()) {
onPlayResult_l(SockException(Err_other, "rtmp url非法"), false);
return;
}

View File

@ -65,12 +65,18 @@ void RtmpPusher::onPublishResult_l(const SockException &ex, bool handshake_done)
void RtmpPusher::publish(const string &url) {
teardown();
string host_url = findSubString(url.data(), "://", "/");
auto schema = findSubString(url.data(), nullptr, "://");
auto host_url = findSubString(url.data(), "://", "/");
_app = findSubString(url.data(), (host_url + "/").data(), "/");
_stream_id = findSubString(url.data(), (host_url + "/" + _app + "/").data(), NULL);
_tc_url = string("rtmp://") + host_url + "/" + _app;
if (!_app.size() || !_stream_id.size()) {
auto app_second = findSubString(_stream_id.data(), nullptr, "/");
if (!app_second.empty() && app_second.find('?') == std::string::npos) {
// _stream_id存在多级不包含'?', 说明分割符'/'不是url参数的一部分
_app += "/" + app_second;
_stream_id.erase(0, app_second.size() + 1);
}
_tc_url = schema + "://" + host_url + "/" + _app;
if (_app.empty() || _stream_id.empty()) {
onPublishResult_l(SockException(Err_other, "rtmp url非法"), false);
return;
}

View File

@ -140,7 +140,7 @@ void RtpSender::startSend(const MediaSourceEvent::SendRtpArgs &args, const funct
cb(0, SockException(Err_other, ex.what()));
return;
}
strong_self->_socket_rtp->bindPeerAddr((struct sockaddr *)&addr);
strong_self->_socket_rtp->bindPeerAddr((struct sockaddr *)&addr, 0, true);
strong_self->onConnect();
cb(strong_self->_socket_rtp->get_local_port(), SockException());
});
@ -182,7 +182,7 @@ void RtpSender::createRtcpSocket() {
case AF_INET6: ((sockaddr_in6 *)&addr)->sin6_port = htons(ntohs(((sockaddr_in6 *)&addr)->sin6_port) + 1); break;
default: assert(0); break;
}
_socket_rtcp->bindPeerAddr((struct sockaddr *)&addr);
_socket_rtcp->bindPeerAddr((struct sockaddr *)&addr, 0, true);
_rtcp_context = std::make_shared<RtcpContextForSend>();
weak_ptr<RtpSender> weak_self = shared_from_this();

View File

@ -89,6 +89,8 @@ public:
for (auto &rtcp : rtcps) {
strong_self->_process->onRtcp(rtcp);
}
// 收到sr rtcp后驱动返回rr rtcp
strong_self->sendRtcp(strong_self->_ssrc, (struct sockaddr *)(strong_self->_rtcp_addr.get()));
});
GET_CONFIG(uint64_t, timeoutSec, RtpProxy::kTimeoutSec);
@ -102,7 +104,7 @@ public:
process->setOnDetach(std::move(strong_self->_on_detach));
}
if (!process) { // process 未创建触发rtp server 超时事件
NOTICE_EMIT(BroadcastRtpServerTimeoutArgs, Broadcast::KBroadcastRtpServerTimeout, strong_self->_local_port, strong_self->_stream_id,
NOTICE_EMIT(BroadcastRtpServerTimeoutArgs, Broadcast::kBroadcastRtpServerTimeout, strong_self->_local_port, strong_self->_stream_id,
(int)strong_self->_tcp_mode, strong_self->_re_use_port, strong_self->_ssrc);
}
}
@ -154,7 +156,7 @@ private:
EventPoller::DelayTask::Ptr _delay_task;
};
void RtpServer::start(uint16_t local_port, const string &stream_id, TcpMode tcp_mode, const char *local_ip, bool re_use_port, uint32_t ssrc, bool only_audio) {
void RtpServer::start(uint16_t local_port, const string &stream_id, TcpMode tcp_mode, const char *local_ip, bool re_use_port, uint32_t ssrc, bool only_audio, bool multiplex) {
//创建udp服务器
Socket::Ptr rtp_socket = Socket::createSocket(nullptr, true);
Socket::Ptr rtcp_socket = Socket::createSocket(nullptr, true);
@ -193,7 +195,8 @@ void RtpServer::start(uint16_t local_port, const string &stream_id, TcpMode tcp_
//创建udp服务器
UdpServer::Ptr udp_server;
RtcpHelper::Ptr helper;
if (!stream_id.empty()) {
//增加了多路复用判断如果多路复用为true就走else逻辑同时保留了原来stream_id为空走else逻辑
if (!stream_id.empty() && !multiplex) {
//指定了流id那么一个端口一个流(不管是否包含多个ssrc的多个流绑定rtp源后会筛选掉ip端口不匹配的流)
helper = std::make_shared<RtcpHelper>(std::move(rtcp_socket), stream_id);
helper->startRtcp();

View File

@ -42,9 +42,10 @@ public:
* @param local_ip ip
* @param re_use_port socket为re_use属性
* @param ssrc ssrc
* @param multiplex
*/
void start(uint16_t local_port, const std::string &stream_id = "", TcpMode tcp_mode = PASSIVE,
const char *local_ip = "::", bool re_use_port = true, uint32_t ssrc = 0, bool only_audio = false);
const char *local_ip = "::", bool re_use_port = true, uint32_t ssrc = 0, bool only_audio = false, bool multiplex = false);
/**
* tcp服务(tcp主动模式)

View File

@ -26,7 +26,13 @@ public:
_media_src = std::make_shared<TSMediaSource>(tuple);
}
~TSMediaSourceMuxer() override { MpegMuxer::flush(); };
~TSMediaSourceMuxer() override {
try {
MpegMuxer::flush();
} catch (std::exception &ex) {
WarnL << ex.what();
}
};
void setListener(const std::weak_ptr<MediaSourceEvent> &listener){
setDelegate(listener);

View File

@ -81,9 +81,11 @@ public:
*/
struct EchoSessionCreator {
//返回的Session必须派生于SendInterceptor可以返回null(拒绝连接)
Session::Ptr operator()(const Parser &header, const HttpSession &parent, const Socket::Ptr &pSock) {
Session::Ptr operator()(const Parser &header, const HttpSession &parent, const Socket::Ptr &pSock, mediakit::WebSocketHeader::Type &type) {
// return nullptr;
if (header.url() == "/") {
// 可以指定传输方式
// type = mediakit::WebSocketHeader::BINARY;
return std::make_shared<SessionTypeImp<EchoSession> >(header, parent, pSock);
}
return std::make_shared<SessionTypeImp<EchoSessionWithUrl> >(header, parent, pSock);

View File

@ -29,6 +29,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <cstdio> // std::sprintf(), std::fopen()
#include <cstring> // std::memcpy(), std::strcmp()
#include "Util/util.h"
#include "Util/SSLBox.h"
#include "Util/SSLUtil.h"
using namespace std;
@ -129,16 +131,10 @@ namespace RTC
MS_TRACE();
// Generate a X509 certificate and private key (unless PEM files are provided).
if (true /*
Settings::configuration.dtlsCertificateFile.empty() ||
Settings::configuration.dtlsPrivateKeyFile.empty()*/)
{
auto ssl = toolkit::SSL_Initor::Instance().getSSLCtx("", true);
if (!ssl || !ReadCertificateAndPrivateKeyFromContext(ssl.get())) {
GenerateCertificateAndPrivateKey();
}
else
{
ReadCertificateAndPrivateKeyFromFiles();
}
// Create a global SSL_CTX.
CreateSslCtx();
@ -297,59 +293,22 @@ namespace RTC
MS_THROW_ERROR("DTLS certificate and private key generation failed");
}
void DtlsTransport::DtlsEnvironment::ReadCertificateAndPrivateKeyFromFiles()
bool DtlsTransport::DtlsEnvironment::ReadCertificateAndPrivateKeyFromContext(SSL_CTX *ctx)
{
#if 0
MS_TRACE();
FILE* file{ nullptr };
file = fopen(Settings::configuration.dtlsCertificateFile.c_str(), "r");
if (!file)
{
MS_ERROR("error reading DTLS certificate file: %s", std::strerror(errno));
goto error;
certificate = SSL_CTX_get0_certificate(ctx);
if (!certificate) {
return false;
}
X509_up_ref(certificate);
certificate = PEM_read_X509(file, nullptr, nullptr, nullptr);
if (!certificate)
{
LOG_OPENSSL_ERROR("PEM_read_X509() failed");
goto error;
privateKey = SSL_CTX_get0_privatekey(ctx);
if (!privateKey) {
return false;
}
fclose(file);
file = fopen(Settings::configuration.dtlsPrivateKeyFile.c_str(), "r");
if (!file)
{
MS_ERROR("error reading DTLS private key file: %s", std::strerror(errno));
goto error;
}
privateKey = PEM_read_PrivateKey(file, nullptr, nullptr, nullptr);
if (!privateKey)
{
LOG_OPENSSL_ERROR("PEM_read_PrivateKey() failed");
goto error;
}
fclose(file);
return;
error:
MS_THROW_ERROR("error reading DTLS certificate and private key PEM files");
#endif
EVP_PKEY_up_ref(privateKey);
InfoL << "Load webrtc dtls certificate: " << toolkit::SSLUtil::getServerName(certificate);
return true;
}
void DtlsTransport::DtlsEnvironment::CreateSslCtx()

View File

@ -88,7 +88,7 @@ namespace RTC
private:
DtlsEnvironment();
void GenerateCertificateAndPrivateKey();
void ReadCertificateAndPrivateKeyFromFiles();
bool ReadCertificateAndPrivateKeyFromContext(SSL_CTX *ctx);
void CreateSslCtx();
void GenerateFingerprints();

View File

@ -251,7 +251,7 @@ void WebRtcTransport::sendSockData(const char *buf, size_t len, RTC::TransportTu
}
Session::Ptr WebRtcTransport::getSession() const {
auto tuple = _ice_server->GetSelectedTuple(true);
auto tuple = _ice_server ? _ice_server->GetSelectedTuple(true) : nullptr;
return tuple ? static_pointer_cast<Session>(tuple->shared_from_this()) : nullptr;
}

1
www/webassist Submodule

@ -0,0 +1 @@
Subproject commit b02d2a4c1abf95db45e50bb77d789defa0fcc4b7