From db3f0147be39b74c849e64f2f75cbf1629001072 Mon Sep 17 00:00:00 2001 From: alexliyu7352 Date: Mon, 9 Oct 2023 19:37:04 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E5=8D=87=E7=BA=A7jemalloc=E5=88=B05.3?= =?UTF-8?q?=E7=89=88=E6=9C=AC=20(#2884)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 升级jemalloc到5.3版本,相比5.2.1内存稳定许多,去除--without-export编译参数, 解决类似glibc中分配内存却在jemalloc中释放导致崩溃问题. 比如: toolkit::LoggerWrapper::printLogV中使用的vasprintf --- CMakeLists.txt | 13 ++++++++++++- cmake/Jemalloc.cmake | 23 ++++++++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cb85679b..39c6c517 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,7 @@ option(ENABLE_FAAC "Enable FAAC" OFF) option(ENABLE_FFMPEG "Enable FFmpeg" OFF) option(ENABLE_HLS "Enable HLS" ON) option(ENABLE_JEMALLOC_STATIC "Enable static linking to the jemalloc library" OFF) +option(ENABLE_JEMALLOC_DUMP "Enable jemalloc to dump malloc statistics" OFF) option(ENABLE_MEM_DEBUG "Enable Memory Debug" OFF) option(ENABLE_MP4 "Enable MP4" ON) option(ENABLE_HLS_FMP4 "Enable HLS-FMP4" ON) @@ -335,7 +336,11 @@ if(ENABLE_JEMALLOC_STATIC) if(NOT EXISTS ${DEP_ROOT_DIR}) file(MAKE_DIRECTORY ${DEP_ROOT_DIR}) endif() - + if (ENABLE_JEMALLOC_DUMP) + set(ENABLE_JEMALLOC_STAT ON) + else () + set(ENABLE_JEMALLOC_STAT OFF) + endif () include(Jemalloc) include_directories(SYSTEM ${DEP_ROOT_DIR}/${JEMALLOC_NAME}/include/jemalloc) link_directories(${DEP_ROOT_DIR}/${JEMALLOC_NAME}/lib) @@ -349,6 +354,12 @@ if(JEMALLOC_FOUND) message(STATUS "found library: ${JEMALLOC_LIBRARIES}") include_directories(${JEMALLOC_INCLUDE_DIR}) update_cached_list(MK_LINK_LIBRARIES ${JEMALLOC_LIBRARIES}) + add_definitions(-DUSE_JEMALLOC) + message(STATUS "jemalloc will be used to avoid memory fragmentation") + if (ENABLE_JEMALLOC_DUMP) + add_definitions(-DENABLE_JEMALLOC_DUMP) + message(STATUS "jemalloc will save memory usage statistics when the program exits") + endif () endif() # 查找 openssl 是否安装 diff --git a/cmake/Jemalloc.cmake b/cmake/Jemalloc.cmake index 94d46b64..38847e58 100644 --- a/cmake/Jemalloc.cmake +++ b/cmake/Jemalloc.cmake @@ -1,21 +1,34 @@ # Download and build Jemalloc -set(JEMALLOC_VERSION 5.2.1) +set(JEMALLOC_VERSION 5.3.0) set(JEMALLOC_NAME jemalloc-${JEMALLOC_VERSION}) set(JEMALLOC_TAR_PATH ${DEP_ROOT_DIR}/${JEMALLOC_NAME}.tar.bz2) list(APPEND jemalloc_CONFIG_ARGS --disable-initial-exec-tls) -list(APPEND jemalloc_CONFIG_ARGS --without-export) +#list(APPEND jemalloc_CONFIG_ARGS --without-export) +if (ENABLE_JEMALLOC_STAT) + list(APPEND jemalloc_CONFIG_ARGS --enable-stats) + message(STATUS "Jemalloc stats enabled") +else () list(APPEND jemalloc_CONFIG_ARGS --disable-stats) + message(STATUS "Jemalloc stats disabled") +endif () list(APPEND jemalloc_CONFIG_ARGS --disable-libdl) #list(APPEND jemalloc_CONFIG_ARGS --disable-cxx) #list(APPEND jemalloc_CONFIG_ARGS --with-jemalloc-prefix=je_) #list(APPEND jemalloc_CONFIG_ARGS --enable-debug) if(NOT EXISTS ${JEMALLOC_TAR_PATH}) - message(STATUS "Downloading ${JEMALLOC_NAME}...") - file(DOWNLOAD https://github.com/jemalloc/jemalloc/releases/download/${JEMALLOC_VERSION}/${JEMALLOC_NAME}.tar.bz2 - ${JEMALLOC_TAR_PATH}) + set(JEMALLOC_URL https://github.com/jemalloc/jemalloc/releases/download/${JEMALLOC_VERSION}/${JEMALLOC_NAME}.tar.bz2) + message(STATUS "Downloading ${JEMALLOC_NAME} from ${JEMALLOC_URL}") + file(DOWNLOAD ${JEMALLOC_URL} ${JEMALLOC_TAR_PATH} SHOW_PROGRESS STATUS JEMALLOC_DOWNLOAD_STATUS LOG JEMALLOC_DOWNLOAD_LOG) + list(GET JEMALLOC_DOWNLOAD_STATUS 0 JEMALLOC_DOWNLOAD_STATUS_CODE) + if(NOT JEMALLOC_DOWNLOAD_STATUS_CODE EQUAL 0) + file(REMOVE ${JEMALLOC_TAR_PATH}) + message(STATUS "${JEMALLOC_DOWNLOAD_LOG}") + message(FATAL_ERROR "${JEMALLOC_NAME} download failed! error is ${JEMALLOC_DOWNLOAD_STATUS}") + return() + endif () endif() SET( DIR_CONTAINING_JEMALLOC ${DEP_ROOT_DIR}/${JEMALLOC_NAME} ) From fdc00d5a02dffb76d4c9b80cf8dc313c42571d76 Mon Sep 17 00:00:00 2001 From: alexliyu7352 Date: Tue, 10 Oct 2023 11:48:56 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0jemalloc=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB,=20=E5=A2=9E=E5=8A=A0jemalloc=E5=86=85?= =?UTF-8?q?=E5=AD=98=E7=BB=9F=E8=AE=A1=E5=88=86=E6=9E=90=20(#2885)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加jemalloc工具类, 增加jemalloc内存统计分析 --- server/System.cpp | 21 ++++++++++- src/Common/JemallocUtil.cpp | 74 +++++++++++++++++++++++++++++++++++++ src/Common/JemallocUtil.h | 30 +++++++++++++++ 3 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 src/Common/JemallocUtil.cpp create mode 100644 src/Common/JemallocUtil.h diff --git a/server/System.cpp b/server/System.cpp index bc93ed3c..c06d34e1 100644 --- a/server/System.cpp +++ b/server/System.cpp @@ -22,10 +22,11 @@ #include #include +#include "Common/JemallocUtil.h" +#include "Common/macros.h" +#include "System.h" #include "Util/logger.h" #include "Util/uv_errno.h" -#include "System.h" -#include "Common/macros.h" using namespace std; using namespace toolkit; @@ -55,6 +56,16 @@ string System::execute(const string &cmd) { static constexpr int MAX_STACK_FRAMES = 128; +static void save_jemalloc_stats() { + string jemalloc_status = JemallocUtil::get_malloc_stats(); + if (jemalloc_status.empty()) { + return; + } + ofstream out(StrPrinter << exeDir() << "/jemalloc.json", ios::out | ios::binary | ios::trunc); + out << jemalloc_status; + out.flush(); +} + static void sig_crash(int sig) { signal(sig, SIG_DFL); void *array[MAX_STACK_FRAMES]; @@ -149,6 +160,12 @@ void System::startDaemon(bool &kill_parent_if_failed) { } void System::systemSetup(){ + +#ifdef ENABLE_JEMALLOC_DUMP + //Save memory report when program exits + atexit(save_jemalloc_stats); +#endif //ENABLE_JEMALLOC_DUMP + #if !defined(_WIN32) struct rlimit rlim,rlim_new; if (getrlimit(RLIMIT_CORE, &rlim)==0) { diff --git a/src/Common/JemallocUtil.cpp b/src/Common/JemallocUtil.cpp new file mode 100644 index 00000000..fa2de64a --- /dev/null +++ b/src/Common/JemallocUtil.cpp @@ -0,0 +1,74 @@ +/* +* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. +* +* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit). +* +* Use of this source code is governed by MIT license that can be found in the +* LICENSE file in the root of the source tree. All contributing project authors +* may be found in the AUTHORS file in the root of the source tree. +*/ + + +#include "JemallocUtil.h" +#include "Util/logger.h" +#ifdef USE_JEMALLOC +#include +#include +#endif + +namespace mediakit { + +void set_profile_active(bool active) { +#ifdef USE_JEMALLOC + int err = mallctl("prof.active", nullptr, nullptr, (void *)&active, sizeof(active)); + if (err != 0) { + WarnL << "mallctl failed with: " << err; + } +#endif +} + +void JemallocUtil::enable_profiling() { + set_profile_active(true); +} +void JemallocUtil::disable_profiling() { + set_profile_active(false); +} +void JemallocUtil::dump(const std::string &file_name) { +#ifdef USE_JEMALLOC + auto *c_str = file_name.c_str(); + int err = mallctl("prof.dump", nullptr, nullptr, &c_str, sizeof(const char *)); + if (err != 0) { + std::cerr << "mallctl failed with: " << err << std::endl; + } +#endif +} +std::string JemallocUtil::get_malloc_stats() { +#ifdef USE_JEMALLOC + std::string res; + malloc_stats_print([](void *opaque, const char *s) { ((std::string *)opaque)->append(s); }, &res, "J"); + return res; +#else + return ""; +#endif +} + +void JemallocUtil::some_malloc_stats(const std::function &fn) { +#ifdef USE_JEMALLOC + constexpr std::array STATS = { + "stats.allocated", "stats.active", "stats.metadata", "stats.metadata_thp", + "stats.resident", "stats.mapped", "stats.retained", "stats.zero_reallocs", + }; + + for (const char *stat : STATS) { + size_t value; + size_t len = sizeof(value); + auto err = mallctl(stat, &value, &len, nullptr, 0); + if (err != 0) { + ErrorL << "Failed reading " << stat << ": " << err; + continue; + } + fn(stat, value); + } +#endif +} +} // namespace mediakit \ No newline at end of file diff --git a/src/Common/JemallocUtil.h b/src/Common/JemallocUtil.h new file mode 100644 index 00000000..796a58bb --- /dev/null +++ b/src/Common/JemallocUtil.h @@ -0,0 +1,30 @@ +/* +* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved. +* +* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit). +* +* Use of this source code is governed by MIT license that can be found in the +* LICENSE file in the root of the source tree. All contributing project authors +* may be found in the AUTHORS file in the root of the source tree. +*/ + +#ifndef ZLMEDIAKIT_JEMALLOCUTIL_H +#define ZLMEDIAKIT_JEMALLOCUTIL_H +#include +#include +namespace mediakit { +class JemallocUtil { +public: + JemallocUtil() = default; + ~JemallocUtil() = default; + + static void enable_profiling(); + + static void disable_profiling(); + + static void dump(const std::string &file_name); + static std::string get_malloc_stats(); + static void some_malloc_stats(const std::function &fn); +}; +} // namespace mediakit +#endif // ZLMEDIAKIT_JEMALLOCUTIL_H From c25e76311ff0a203f0103564f2b1baa6d4077355 Mon Sep 17 00:00:00 2001 From: fruit Juice <2317232721@qq.com> Date: Wed, 11 Oct 2023 14:52:16 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=9B=B8=E5=AF=B9?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=88=B3=E6=A8=A1=E5=BC=8F=E4=B8=8B=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E6=88=B3=E5=9B=9E=E9=80=80=E5=AF=BC=E8=87=B4=E9=9F=B3?= =?UTF-8?q?=E8=A7=86=E9=A2=91=E4=B8=8D=E5=90=8C=E6=AD=A5=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#2894=20#2877)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 加大时间戳回退容忍度,最大回退阈值改成与跳变最大幅度一致 --- src/Common/Stamp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Common/Stamp.cpp b/src/Common/Stamp.cpp index 87282c39..2656a176 100644 --- a/src/Common/Stamp.cpp +++ b/src/Common/Stamp.cpp @@ -50,7 +50,7 @@ int64_t DeltaStamp::deltaStamp(int64_t stamp) { _last_stamp = stamp; // 如果时间戳回退不多,那么返回负值,否则返回加1 - return -ret < MAX_CTS ? ret : 1; + return -ret < MAX_DELTA_STAMP ? ret : 1; } void Stamp::setPlayBack(bool playback) { From 6348e64cdf52abe94e3573d04d7275fd49361ed6 Mon Sep 17 00:00:00 2001 From: PioLing <964472638@qq.com> Date: Thu, 12 Oct 2023 11:05:41 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E7=A1=AE=E4=BF=9Dhls=E6=92=AD=E6=94=BE?= =?UTF-8?q?=E5=99=A8=E6=8C=81=E7=BB=AD=E6=92=AD=E6=94=BE=20(#2896)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在hls注销后,hls cookie会继续存活60秒,在此期间,如果hls流重新注册,将导致无法继续播放; 通过此修改,在hls注销后每3秒查询一次MediaSource,可以在性能和功能间保持平衡。 --- src/Http/HttpCookieManager.cpp | 5 ++--- src/Http/HttpCookieManager.h | 8 ++++---- src/Http/HttpFileManager.cpp | 25 +++++++++++++++++-------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/Http/HttpCookieManager.cpp b/src/Http/HttpCookieManager.cpp index b846a3b9..e829423d 100644 --- a/src/Http/HttpCookieManager.cpp +++ b/src/Http/HttpCookieManager.cpp @@ -61,7 +61,7 @@ bool HttpServerCookie::isExpired() { return _ticker.elapsedTime() > _max_elapsed * 1000; } -void HttpServerCookie::setAttach(std::shared_ptr attach) { +void HttpServerCookie::setAttach(toolkit::Any attach) { _attach = std::move(attach); } @@ -114,8 +114,7 @@ void HttpCookieManager::onManager() { } } -HttpServerCookie::Ptr HttpCookieManager::addCookie(const string &cookie_name, const string &uid_in, - uint64_t max_elapsed, std::shared_ptr attach, int max_client) { +HttpServerCookie::Ptr HttpCookieManager::addCookie(const string &cookie_name, const string &uid_in, uint64_t max_elapsed, toolkit::Any attach, int max_client) { lock_guard lck(_mtx_cookie); auto cookie = _generator.obtain(); auto uid = uid_in.empty() ? cookie : uid_in; diff --git a/src/Http/HttpCookieManager.h b/src/Http/HttpCookieManager.h index e55c2086..53775b79 100644 --- a/src/Http/HttpCookieManager.h +++ b/src/Http/HttpCookieManager.h @@ -85,14 +85,14 @@ public: /** * 设置附加数据 */ - void setAttach(std::shared_ptr attach); + void setAttach(toolkit::Any attach); /* * 获取附加数据 */ template T& getAttach() { - return *static_cast(_attach.get()); + return _attach.get(); } private: @@ -104,7 +104,7 @@ private: std::string _cookie_uuid; uint64_t _max_elapsed; toolkit::Ticker _ticker; - std::shared_ptr _attach; + toolkit::Any _attach; std::weak_ptr _manager; }; @@ -163,7 +163,7 @@ public: */ HttpServerCookie::Ptr addCookie( const std::string &cookie_name, const std::string &uid, uint64_t max_elapsed = COOKIE_DEFAULT_LIFE, - std::shared_ptr attach = nullptr, + toolkit::Any = toolkit::Any{}, int max_client = 1); /** diff --git a/src/Http/HttpFileManager.cpp b/src/Http/HttpFileManager.cpp index c8996d1c..1eaa9ec1 100644 --- a/src/Http/HttpFileManager.cpp +++ b/src/Http/HttpFileManager.cpp @@ -31,13 +31,16 @@ namespace mediakit { // 每次访问一次该cookie,那么将重新刷新cookie有效期 // 假如播放器在60秒内都未访问该cookie,那么将重新触发hls播放鉴权 static int kHlsCookieSecond = 60; +static int kFindSrcIntervalSecond = 3; static const string kCookieName = "ZL_COOKIE"; static const string kHlsSuffix = "/hls.m3u8"; static const string kHlsFMP4Suffix = "/hls.fmp4.m3u8"; struct HttpCookieAttachment { - //是否已经查找到过MediaSource + // 是否已经查找到过MediaSource bool _find_src = false; + // 查找MediaSource计时 + Ticker _find_src_ticker; //cookie生效作用域,本cookie只对该目录下的文件生效 string _path; //上次鉴权失败信息,为空则上次鉴权成功 @@ -414,7 +417,9 @@ static void canAccessPath(Session &sender, const Parser &parser, const MediaInfo // hls相关信息 attach->_hls_data = std::make_shared(media_info, info); } - callback(err_msg, HttpCookieManager::Instance().addCookie(kCookieName, uid, life_second, attach)); + toolkit::Any any; + any.set(std::move(attach)); + callback(err_msg, HttpCookieManager::Instance().addCookie(kCookieName, uid, life_second, std::move(any))); } else { callback(err_msg, nullptr); } @@ -532,14 +537,15 @@ static void accessFile(Session &sender, const Parser &parser, const MediaInfo &m return; } - auto src = cookie->getAttach()._hls_data->getMediaSource(); + auto &attach = cookie->getAttach(); + auto src = attach._hls_data->getMediaSource(); if (src) { - //直接从内存获取m3u8索引文件(而不是从文件系统) + // 直接从内存获取m3u8索引文件(而不是从文件系统) response_file(cookie, cb, file_path, parser, src->getIndexFile()); return; } - if (cookie->getAttach()._find_src) { - //查找过MediaSource,但是流已经注销了,不用再查找 + if (attach._find_src && attach._find_src_ticker.elapsedTime() < kFindSrcIntervalSecond * 1000) { + // 最近已经查找过MediaSource了,为了防止频繁查找导致占用全局互斥锁的问题,我们尝试直接从磁盘返回hls索引文件 response_file(cookie, cb, file_path, parser); return; } @@ -555,11 +561,14 @@ static void accessFile(Session &sender, const Parser &parser, const MediaInfo &m auto &attach = cookie->getAttach(); attach._hls_data->setMediaSource(hls); - //添加HlsMediaSource的观看人数(HLS是按需生成的,这样可以触发HLS文件的生成) + // 添加HlsMediaSource的观看人数(HLS是按需生成的,这样可以触发HLS文件的生成) attach._hls_data->addByteUsage(0); - //标记找到MediaSource + // 标记找到MediaSource attach._find_src = true; + // 重置查找MediaSource计时 + attach._find_src_ticker.resetTime(); + // m3u8文件可能不存在, 等待m3u8索引文件按需生成 hls->getIndexFile([response_file, file_path, cookie, cb, parser](const string &file) { response_file(cookie, cb, file_path, parser, file); From 67bc0273b456c8b25ad0fc8c010e0f318651adc8 Mon Sep 17 00:00:00 2001 From: Deepslient <1154547394@qq.com> Date: Thu, 12 Oct 2023 14:38:45 +0800 Subject: [PATCH 5/7] Modify the readme description (#2859) --- tests/README.md | 2 +- www/webrtc/index.html | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/README.md b/tests/README.md index 448ae9ec..81516128 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1,6 +1,6 @@ 此目录下的所有.cpp文件将被编译成可执行程序(不包含此目录下的子目录). 子目录DeviceHK为海康IPC的适配程序,需要先下载海康的SDK才能编译, -由于操作麻烦,所以仅把源码放在这仅供参考. +由于操作麻烦,所以仅把源码放在这里仅供参考. - test_benchmark.cpp diff --git a/www/webrtc/index.html b/www/webrtc/index.html index a29629bd..8f9b60e6 100644 --- a/www/webrtc/index.html +++ b/www/webrtc/index.html @@ -93,8 +93,8 @@ var recvOnly = true; var resArr = []; - var ishttps = 'https:' == document.location.protocol ? true : false; - var isLocal = "file:" == document.location.protocol ? true : false; + var ishttps = 'https:' === document.location.protocol; + var isLocal = "file:" === document.location.protocol; const searchParams = new URL(document.location.href).searchParams; let type = searchParams.get('type'); @@ -105,7 +105,7 @@ const apiPath = `/index/api/webrtc?app=${searchParams.get('app') ?? 'live'}&stream=${searchParams.get('stream') ?? 'test'}&type=${type}`; if(!ishttps && !isLocal){ - alert('本demo需要在https的网站访问 ,如果你要推流的话(this demo must access in site of https if you want push stream)'); + alert('本demo需要在https的网站访问, 如果你要推流的话(this demo must access in site of https if you want to push stream)'); } const apiHost = isLocal ? "http://127.0.0.1" : `${document.location.protocol}//${window.location.host}`; From eedf262059d8f00ab8f3f077ab201f55e28ff7e0 Mon Sep 17 00:00:00 2001 From: imp_rayjay <32894715+rayjay214@users.noreply.github.com> Date: Thu, 12 Oct 2023 17:26:53 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81mjpeg?= =?UTF-8?q?=E8=B4=9F=E8=BD=BD=E7=9A=84mp4=E6=96=87=E4=BB=B6=E7=82=B9?= =?UTF-8?q?=E6=92=AD=20(#2898)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Record/MP4Demuxer.cpp | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Record/MP4Demuxer.cpp b/src/Record/MP4Demuxer.cpp index 02c66976..87936137 100644 --- a/src/Record/MP4Demuxer.cpp +++ b/src/Record/MP4Demuxer.cpp @@ -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((char *)config, size, 0, 0,4)); } } - } break; + } + case MOV_OBJECT_HEVC: { auto video = std::make_shared(); _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((char *) config, size, 0, 0,4)); } } + break; } + + case MOV_OBJECT_JPEG: { + auto video = std::make_shared(); + _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(buf, (uint64_t)dts, 0, DATA_OFFSET); + break; + } + case CodecAAC: { AACTrack::Ptr track = dynamic_pointer_cast(it->second); assert(track); @@ -283,4 +296,4 @@ uint64_t MP4Demuxer::getDurationMS() const { } }//namespace mediakit -#endif// ENABLE_MP4 \ No newline at end of file +#endif// ENABLE_MP4 From b8cb8957e4e4f91a62acc831ae37cced3656a389 Mon Sep 17 00:00:00 2001 From: Armstrong Date: Fri, 13 Oct 2023 15:26:31 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E4=BF=AE=E5=A4=8DWebApi=E9=80=80=E5=87=BA?= =?UTF-8?q?=E6=B8=85=E7=90=86=E6=97=B6=E9=87=8D=E5=A4=8D=E6=9E=90=E6=9E=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20(#2900)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. s_???Map.clear()会触发key/value的析构,先执行析构再移除map成员。析构执行完之前map成员仍然有可见性。 2. s_???Map的成员析构时,根据当前状态,可能触发回调,如播放终止回调。 3. 在状态变更的回调函数中,通过s_???Map.erase(key)的方式解注册,此时也会触发一次析构。 两次析构导致double free:a) map.erase, b) map.clear Signed-off-by: ArmstrongCN --- server/WebApi.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index e8081756..7c8e35c0 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -1911,24 +1911,28 @@ void installWebApi() { void unInstallWebApi(){ { lock_guard lck(s_proxyMapMtx); - s_proxyMap.clear(); + auto proxyMap(std::move(s_proxyMap)); + proxyMap.clear(); } { lock_guard lck(s_ffmpegMapMtx); - s_ffmpegMap.clear(); + auto ffmpegMap(std::move(s_ffmpegMap)); + ffmpegMap.clear(); } { lock_guard lck(s_proxyPusherMapMtx); - s_proxyPusherMap.clear(); + auto proxyPusherMap(std::move(s_proxyPusherMap)); + proxyPusherMap.clear(); } { #if defined(ENABLE_RTPPROXY) RtpSelector::Instance().clear(); lock_guard lck(s_rtpServerMapMtx); - s_rtpServerMap.clear(); + auto rtpServerMap(std::move(s_rtpServerMap)); + rtpServerMap.clear(); #endif } NoticeCenter::Instance().delListener(&web_api_tag);