Dynamic load codec ext library

This commit is contained in:
xia-chu 2023-12-10 11:13:26 +08:00
parent 25c99470ee
commit 3646f7f21c
5 changed files with 63 additions and 37 deletions

View File

@ -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)

View File

@ -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
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>"
${INCLUDE_DIRECTORIES})
update_cached_list(MK_LINK_LIBRARIES ZLMediaKit::ext-codec ${LINK_LIBRARIES})
# update_cached_list(MK_LINK_LIBRARIES ZLMediaKit::ext-codec ${LINK_LIBRARIES})

View File

@ -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

View File

@ -8,6 +8,7 @@
* may be found in the AUTHORS file in the root of the source tree.
*/
#include <dlfcn.h>
#include <signal.h>
#include <iostream>
#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];

View File

@ -17,29 +17,14 @@ using namespace toolkit;
namespace mediakit {
static std::unordered_map<int, const CodecPlugin *> 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<int, const CodecPlugin *> &getPluginRegister() {
static std::unordered_map<int, const CodecPlugin *> *s_plugins = new std::unordered_map<int, const CodecPlugin *>;
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;
}