diff --git a/CMakeLists.txt b/CMakeLists.txt index b9dbfa64..bc375c11 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,9 @@ project(ZLMediaKit LANGUAGES C CXX) # Enable C++11 set(CMAKE_CXX_STANDARD 11) +# 主程序链接符号可以给外部动态库调用 +add_link_options("LINKER:-export_dynamic") + option(ENABLE_API "Enable C API SDK" ON) option(ENABLE_API_STATIC_LIB "Enable mk_api static lib" OFF) option(ENABLE_ASAN "Enable Address Sanitize" OFF) diff --git a/ext-codec/CMakeLists.txt b/ext-codec/CMakeLists.txt index 23190fb8..818bf812 100644 --- a/ext-codec/CMakeLists.txt +++ b/ext-codec/CMakeLists.txt @@ -30,8 +30,9 @@ file(GLOB EXT_SRC_LIST ${CMAKE_CURRENT_SOURCE_DIR}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp) -add_library(ext-codec STATIC ${EXT_SRC_LIST}) -add_library(ZLMediaKit::ext-codec ALIAS ext-codec) +add_library(ext-codec SHARED ${EXT_SRC_LIST}) +# add_library(ZLMediaKit::ext-codec ALIAS ext-codec) +target_link_options(ext-codec PRIVATE -Wl,-undefined -Wl,dynamic_lookup) target_compile_options(ext-codec PRIVATE ${COMPILE_OPTIONS_DEFAULT}) target_compile_definitions(ext-codec PUBLIC ${COMPILE_DEFINITIONS}) @@ -45,4 +46,4 @@ target_include_directories(ext-codec "$" ${INCLUDE_DIRECTORIES}) -update_cached_list(MK_LINK_LIBRARIES ZLMediaKit::ext-codec ${LINK_LIBRARIES}) +# update_cached_list(MK_LINK_LIBRARIES ZLMediaKit::ext-codec ${LINK_LIBRARIES}) diff --git a/ext-codec/auto_register.cpp b/ext-codec/auto_register.cpp new file mode 100644 index 00000000..dfaa84df --- /dev/null +++ b/ext-codec/auto_register.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved. + * + * This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit). + * + * Use of this source code is governed by MIT-like 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 "Extension/Factory.h" + + +namespace mediakit { + +extern CodecPlugin h264_plugin; +extern CodecPlugin h265_plugin; +extern CodecPlugin jpeg_plugin; +extern CodecPlugin aac_plugin; +extern CodecPlugin opus_plugin; +extern CodecPlugin g711a_plugin; +extern CodecPlugin g711u_plugin; +extern CodecPlugin l16_plugin; + +REGISTER_CODEC(h264_plugin); +REGISTER_CODEC(h265_plugin); +REGISTER_CODEC(jpeg_plugin); +REGISTER_CODEC(aac_plugin); +REGISTER_CODEC(opus_plugin); +REGISTER_CODEC(g711a_plugin) +REGISTER_CODEC(g711u_plugin); +REGISTER_CODEC(l16_plugin); + +}//namespace mediakit \ No newline at end of file diff --git a/server/main.cpp b/server/main.cpp index 1b2f4b1a..1463f35c 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -8,6 +8,7 @@ * may be found in the AUTHORS file in the root of the source tree. */ +#include #include #include #include "Util/File.h" @@ -272,6 +273,8 @@ int start_main(int argc,char *argv[]) { }); } + dlopen("libext-codec.dylib", RTLD_LAZY); + uint16_t shellPort = mINI::Instance()[Shell::kPort]; uint16_t rtspPort = mINI::Instance()[Rtsp::kPort]; uint16_t rtspsPort = mINI::Instance()[Rtsp::kSSLPort]; diff --git a/src/Extension/Factory.cpp b/src/Extension/Factory.cpp index f69eead7..f78315bf 100644 --- a/src/Extension/Factory.cpp +++ b/src/Extension/Factory.cpp @@ -17,29 +17,14 @@ using namespace toolkit; namespace mediakit { -static std::unordered_map s_plugins; - -extern CodecPlugin h264_plugin; -extern CodecPlugin h265_plugin; -extern CodecPlugin jpeg_plugin; -extern CodecPlugin aac_plugin; -extern CodecPlugin opus_plugin; -extern CodecPlugin g711a_plugin; -extern CodecPlugin g711u_plugin; -extern CodecPlugin l16_plugin; - -REGISTER_CODEC(h264_plugin); -REGISTER_CODEC(h265_plugin); -REGISTER_CODEC(jpeg_plugin); -REGISTER_CODEC(aac_plugin); -REGISTER_CODEC(opus_plugin); -REGISTER_CODEC(g711a_plugin) -REGISTER_CODEC(g711u_plugin); -REGISTER_CODEC(l16_plugin); +static std::unordered_map &getPluginRegister() { + static std::unordered_map *s_plugins = new std::unordered_map; + return *s_plugins; +} void Factory::registerPlugin(const CodecPlugin &plugin) { InfoL << "Load codec: " << getCodecName(plugin.getCodec()); - s_plugins[(int)(plugin.getCodec())] = &plugin; + getPluginRegister()[(int)(plugin.getCodec())] = &plugin; } Track::Ptr Factory::getTrackBySdp(const SdpTrack::Ptr &track) { @@ -48,8 +33,8 @@ Track::Ptr Factory::getTrackBySdp(const SdpTrack::Ptr &track) { // 根据传统的payload type 获取编码类型以及采样率等信息 codec = RtpPayload::getCodecId(track->_pt); } - auto it = s_plugins.find(codec); - if (it == s_plugins.end()) { + auto it = getPluginRegister().find(codec); + if (it == getPluginRegister().end()) { WarnL << "Unsupported codec: " << track->getName(); return nullptr; } @@ -66,8 +51,8 @@ Track::Ptr Factory::getTrackByAbstractTrack(const Track::Ptr &track) { } RtpCodec::Ptr Factory::getRtpEncoderByCodecId(CodecId codec, uint8_t pt) { - auto it = s_plugins.find(codec); - if (it == s_plugins.end()) { + auto it = getPluginRegister().find(codec); + if (it == getPluginRegister().end()) { WarnL << "Unsupported codec: " << getCodecName(codec); return nullptr; } @@ -75,8 +60,8 @@ RtpCodec::Ptr Factory::getRtpEncoderByCodecId(CodecId codec, uint8_t pt) { } RtpCodec::Ptr Factory::getRtpDecoderByCodecId(CodecId codec) { - auto it = s_plugins.find(codec); - if (it == s_plugins.end()) { + auto it = getPluginRegister().find(codec); + if (it == getPluginRegister().end()) { WarnL << "Unsupported codec: " << getCodecName(codec); return nullptr; } @@ -113,8 +98,8 @@ static CodecId getVideoCodecIdByAmf(const AMFValue &val){ } Track::Ptr Factory::getTrackByCodecId(CodecId codec, int sample_rate, int channels, int sample_bit) { - auto it = s_plugins.find(codec); - if (it == s_plugins.end()) { + auto it = getPluginRegister().find(codec); + if (it == getPluginRegister().end()) { WarnL << "Unsupported codec: " << getCodecName(codec); return nullptr; } @@ -162,8 +147,8 @@ Track::Ptr Factory::getAudioTrackByAmf(const AMFValue& amf, int sample_rate, int } RtmpCodec::Ptr Factory::getRtmpDecoderByTrack(const Track::Ptr &track) { - auto it = s_plugins.find(track->getCodecId()); - if (it == s_plugins.end()) { + auto it = getPluginRegister().find(track->getCodecId()); + if (it == getPluginRegister().end()) { WarnL << "Unsupported codec: " << track->getCodecName(); return nullptr; } @@ -171,8 +156,8 @@ RtmpCodec::Ptr Factory::getRtmpDecoderByTrack(const Track::Ptr &track) { } RtmpCodec::Ptr Factory::getRtmpEncoderByTrack(const Track::Ptr &track) { - auto it = s_plugins.find(track->getCodecId()); - if (it == s_plugins.end()) { + auto it = getPluginRegister().find(track->getCodecId()); + if (it == getPluginRegister().end()) { WarnL << "Unsupported codec: " << track->getCodecName(); return nullptr; } @@ -195,8 +180,8 @@ AMFValue Factory::getAmfByCodecId(CodecId codecId) { } Frame::Ptr Factory::getFrameFromPtr(CodecId codec, const char *data, size_t bytes, uint64_t dts, uint64_t pts) { - auto it = s_plugins.find(codec); - if (it == s_plugins.end()) { + auto it = getPluginRegister().find(codec); + if (it == getPluginRegister().end()) { WarnL << "Unsupported codec: " << getCodecName(codec); return nullptr; }