解决编译错误以及运行期bug

This commit is contained in:
xiongziliang 2018-03-21 15:13:33 +08:00
parent 30aa71cb53
commit ee9b6b64da
5 changed files with 106 additions and 165 deletions

View File

@ -1,5 +1,7 @@
project(ZLMediaKit) project(ZLMediaKit)
cmake_minimum_required(VERSION 2.8) cmake_minimum_required(VERSION 2.8)
#使c++11
add_compile_options(-std=c++11)
# #
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake") set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")
@ -7,133 +9,88 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
# #
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
#
set(SUB_DIR_LIST "Codec" "Common" "Device" "H264" "Http" "MediaFile" "Player" "Rtmp" "RTP" "Rtsp" "Shell")
#使GOP #
add_definitions(-DENABLE_RING_USEBUF) set(ToolKit_Root ${CMAKE_SOURCE_DIR}/ZLToolKit/src)
set(MediaKit_Root ${CMAKE_SOURCE_DIR}/src)
#
INCLUDE_DIRECTORIES(${ToolKit_Root})
INCLUDE_DIRECTORIES(${MediaKit_Root})
# #
if(WIN32) file(GLOB ToolKit_src_list ${ToolKit_Root}/*/*.cpp ${ToolKit_Root}/*/*.h ${ToolKit_Root}/*/*.c)
set(INSTALL_PATH_LIB $ENV{HOME}/${CMAKE_PROJECT_NAME}/lib) file(GLOB MediaKit_src_list ${MediaKit_Root}/*/*.cpp ${MediaKit_Root}/*/*.h ${MediaKit_Root}/*/*.c)
set(INSTALL_PATH_INCLUDE $ENV{HOME}/${CMAKE_PROJECT_NAME}/include)
else()
set(INSTALL_PATH_LIB lib)
set(INSTALL_PATH_INCLUDE include)
endif()
#win32
if (NOT WIN32)
list(REMOVE_ITEM ToolKit_src_list ${ToolKit_Root}/win32/getopt.c)
endif ()
foreach(SUB_DIR ${SUB_DIR_LIST}) #
# set(LINK_LIB_LIST zlmediakit zltoolkit)
aux_source_directory(src/${SUB_DIR} SRC_LIST)
#
install(DIRECTORY src/${SUB_DIR} DESTINATION ${INSTALL_PATH_INCLUDE} FILES_MATCHING PATTERN "*.h")
endforeach()
set(LINK_LIB_LIST)
#openssl #openssl
find_package(OpenSSL QUIET) find_package(OpenSSL QUIET)
if(OPENSSL_FOUND) if (OPENSSL_FOUND)
message(STATUS "找到openssl库:\"${OPENSSL_INCLUDE_DIR}\",ENABLE_OPENSSL宏已打开") message(STATUS "found openssl library\"${OPENSSL_INCLUDE_DIR}\",ENABLE_OPENSSL enabled")
include_directories(${OPENSSL_INCLUDE_DIR}) include_directories(${OPENSSL_INCLUDE_DIR})
add_definitions(-DENABLE_OPENSSL) add_definitions(-DENABLE_OPENSSL)
list(APPEND LINK_LIB_LIST ${OPENSSL_LIBRARIES}) list(APPEND LINK_LIB_LIST ${OPENSSL_LIBRARIES})
endif() endif (OPENSSL_FOUND)
#mysql #mysql
find_package(MYSQL QUIET) find_package(MYSQL QUIET)
if(MYSQL_FOUND) if (MYSQL_FOUND)
message(STATUS "找到mysqlclient库:\"${MYSQL_INCLUDE_DIR}\",ENABLE_MYSQL宏已打开") message(STATUS "found mysqlclient library\"${MYSQL_INCLUDE_DIR}\",ENABLE_MYSQL enabled")
include_directories(${MYSQL_INCLUDE_DIR}) include_directories(${MYSQL_INCLUDE_DIR})
add_definitions(-DENABLE_MYSQL) add_definitions(-DENABLE_MYSQL)
list(APPEND LINK_LIB_LIST ${MYSQL_LIBRARIES}) list(APPEND LINK_LIB_LIST ${MYSQL_LIBRARIES})
endif() endif (MYSQL_FOUND)
#MP4V2
#mp4v2
find_package(MP4V2 QUIET) find_package(MP4V2 QUIET)
if(MP4V2_FOUND) if (MP4V2_FOUND)
message(STATUS "找到mp4v2库:\"${MP4V2_INCLUDE_DIR}\",ENABLE_MP4V2宏已打开")
include_directories(${MP4V2_INCLUDE_DIR}) include_directories(${MP4V2_INCLUDE_DIR})
list(APPEND LINK_LIB_LIST ${MP4V2_LIBRARY})
add_definitions(-DENABLE_MP4V2) add_definitions(-DENABLE_MP4V2)
list(APPEND LINK_LIB_LIST ${MP4V2_LIBRARIES}) message(STATUS "found MP4V2:${MP4V2_INCLUDE_DIR},${MP4V2_LIBRARY}")
endif() endif (MP4V2_FOUND)
#x264 #x264
find_package(X264 QUIET) find_package(X264 QUIET)
if(X264_FOUND) if (X264_FOUND)
message(STATUS "找到x264库:\"${X264_INCLUDE_DIRS}\",ENABLE_X264宏已打开") message(STATUS "found x264 library\"${X264_INCLUDE_DIRS}\",ENABLE_X264 enabled")
include_directories(${X264_INCLUDE_DIRS}) include_directories(${X264_INCLUDE_DIRS})
add_definitions(-DENABLE_X264) add_definitions(-DENABLE_X264)
list(APPEND LINK_LIB_LIST ${X264_LIBRARIES}) list(APPEND LINK_LIB_LIST ${X264_LIBRARIES})
endif() endif ()
#faac #faac
find_package(FAAC QUIET) find_package(FAAC QUIET)
if(FAAC_FOUND) if (FAAC_FOUND)
message(STATUS "找到faac库:\"${FAAC_INCLUDE_DIR}\",ENABLE_FAAC宏已打开") message(STATUS "found faac library\"${FAAC_INCLUDE_DIR}\",ENABLE_FAAC enabled")
include_directories(${FAAC_INCLUDE_DIR}) include_directories(${FAAC_INCLUDE_DIR})
add_definitions(-DENABLE_FAAC) add_definitions(-DENABLE_FAAC)
list(APPEND LINK_LIB_LIST ${FAAC_LIBRARIES}) list(APPEND LINK_LIB_LIST ${FAAC_LIBRARIES})
endif() endif ()
#ZLToolKit #使GOP
find_package(ZLTOOLKIT QUIET) add_definitions(-DENABLE_RING_USEBUF)
if(ZLTOOLKIT_FOUND) #
message(STATUS "找到ZLToolKit库:\"${ZLTOOLKIT_INCLUDE_DIR}\"") add_library(zltoolkit STATIC ${ToolKit_src_list})
include_directories(${ZLTOOLKIT_INCLUDE_DIR}) add_library(zlmediakit STATIC ${MediaKit_src_list})
list(APPEND LINK_LIB_LIST ${ZLTOOLKIT_LIBRARIES})
endif()
#JEMALLOC if (NOT WIN32)
find_package(JEMALLOC QUIET) list(APPEND LINK_LIB_LIST pthread)
if(JEMALLOC_FOUND) else()
message(STATUS "找到JEMALLOC库:\"${JEMALLOC_INCLUDE_DIR}\"") list(APPEND LINK_LIB_LIST WS2_32 Iphlpapi shlwapi)
include_directories(${JEMALLOC_INCLUDE_DIR}) endif ()
list(APPEND LINK_LIB_LIST ${JEMALLOC_LIBRARIES})
endif()
# message(STATUS "linked libraries:${LINK_LIB_LIST}")
message(STATUS "将链接依赖库:${LINK_LIB_LIST}")
#RTSP/RTMP,HLS
add_definitions(-DENABLE_RTMP2RTSP -DENABLE_RTSP2RTMP -DENABLE_HLS)
#
include_directories(${PROJECT_SOURCE_DIR}/src)
#使c++11
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
if(NOT WIN32)
#
add_compile_options(-Wno-deprecated-declarations)
#__FUNCTION__
add_compile_options(-Wno-predefined-identifier-outside-function)
endif()
if(WIN32)
list(APPEND LINK_LIB_LIST ws2_32)
endif()
#
if(NOT IOS AND NOT ANDROID)
add_library(${CMAKE_PROJECT_NAME}_shared SHARED ${SRC_LIST})
set_target_properties(${CMAKE_PROJECT_NAME}_shared PROPERTIES OUTPUT_NAME "${CMAKE_PROJECT_NAME}")
install(TARGETS ${CMAKE_PROJECT_NAME}_shared ARCHIVE DESTINATION ${INSTALL_PATH_LIB} LIBRARY DESTINATION ${INSTALL_PATH_LIB})
target_link_libraries(${CMAKE_PROJECT_NAME}_shared ${LINK_LIB_LIST})
endif()
#
add_library(${CMAKE_PROJECT_NAME}_static STATIC ${SRC_LIST})
set_target_properties(${CMAKE_PROJECT_NAME}_static PROPERTIES OUTPUT_NAME "${CMAKE_PROJECT_NAME}")
install(TARGETS ${CMAKE_PROJECT_NAME}_static ARCHIVE DESTINATION ${INSTALL_PATH_LIB})
# #
if(NOT IOS) add_subdirectory(tests)
add_subdirectory(tests)
endif()

1
ZLToolKit Submodule

@ -0,0 +1 @@
Subproject commit 378e05030f5faef37612d9cb69adb70a222b0a9d

View File

@ -3,7 +3,7 @@ find_package(SDL QUIET)
if(SDL_FOUND) if(SDL_FOUND)
include_directories(${SDL_INCLUDE_DIR}) include_directories(${SDL_INCLUDE_DIR})
list(APPEND LINK_LIB_LIST ${SDL_LIBRARY}) list(APPEND LINK_LIB_LIST ${SDL_LIBRARY})
message(STATUS "找到SDL") message(STATUS " found SDL")
endif() endif()
#ffmpeg/libutil #ffmpeg/libutil
@ -11,7 +11,7 @@ find_package(AVUTIL QUIET)
if(AVUTIL_FOUND) if(AVUTIL_FOUND)
include_directories(${AVUTIL_INCLUDE_DIR}) include_directories(${AVUTIL_INCLUDE_DIR})
list(APPEND LINK_LIB_LIST ${AVUTIL_LIBRARIES}) list(APPEND LINK_LIB_LIST ${AVUTIL_LIBRARIES})
message(STATUS "找到libutil") message(STATUS " found libutil")
endif() endif()
#ffmpeg/libavcodec #ffmpeg/libavcodec
@ -19,31 +19,23 @@ find_package(AVCODEC QUIET)
if(AVCODEC_FOUND) if(AVCODEC_FOUND)
include_directories(${AVCODEC_INCLUDE_DIR}) include_directories(${AVCODEC_INCLUDE_DIR})
list(APPEND LINK_LIB_LIST ${AVCODEC_LIBRARIES}) list(APPEND LINK_LIB_LIST ${AVCODEC_LIBRARIES})
message(STATUS "找到libavcodec") message(STATUS " found libavcodec")
endif() endif()
aux_source_directory(. TEST_SRC_LIST) aux_source_directory(. TEST_SRC_LIST)
#ffmpeg/libavcodec ffmpeg/libavcodec SDL test_player #ffmpeg/libavcodec ffmpeg/libavcodec SDL test_player
if(SDL_FOUND AND AVCODEC_FOUND AND AVUTIL_FOUND) if(SDL_FOUND AND AVCODEC_FOUND AND AVUTIL_FOUND)
message(STATUS "test_player被编译") message(STATUS "test_player will be compiled")
else() else()
message(STATUS "test_player被忽略,如需编译,请先安装sdl ffmpeg/libavcodec ffmpeg/libavcodec") message(STATUS "test_player ingored, please install sdl ffmpeg/libavcodec ffmpeg/libavcodec")
list(REMOVE_ITEM TEST_SRC_LIST ./test_player.cpp) list(REMOVE_ITEM TEST_SRC_LIST ./test_player.cpp)
endif() endif()
foreach(TEST_SRC ${TEST_SRC_LIST}) foreach(TEST_SRC ${TEST_SRC_LIST})
STRING(REGEX REPLACE "^\\./|\\.c[a-zA-Z0-9_]*$" "" TEST_EXE_NAME ${TEST_SRC}) STRING(REGEX REPLACE "^\\./|\\.c[a-zA-Z0-9_]*$" "" TEST_EXE_NAME ${TEST_SRC})
message(STATUS "添加测试程序:${TEST_EXE_NAME}") message(STATUS "add test:${TEST_EXE_NAME}")
add_executable(${TEST_EXE_NAME} ${TEST_SRC}) add_executable(${TEST_EXE_NAME} ${TEST_SRC})
target_link_libraries(${TEST_EXE_NAME} ${LINK_LIB_LIST})
if(ANDROID)
target_link_libraries(${TEST_EXE_NAME} ${CMAKE_PROJECT_NAME}_static ${LINK_LIB_LIST})
elseif(WIN32)
target_link_libraries(${TEST_EXE_NAME} ${CMAKE_PROJECT_NAME}_shared ${LINK_LIB_LIST} )
else()
target_link_libraries(${TEST_EXE_NAME} ${CMAKE_PROJECT_NAME}_shared ${LINK_LIB_LIST} pthread)
endif()
endforeach() endforeach()

View File

@ -42,26 +42,27 @@ using namespace ZL::DEV;
//推流器,保持强引用 //推流器,保持强引用
RtmpPusher::Ptr pusher; RtmpPusher::Ptr pusher;
//声明函数 //声明函数
void rePushDelay(const string &app,const string &stream,const string &url); void rePushDelay(const string &app, const string &stream, const string &url);
//创建推流器并开始推流 //创建推流器并开始推流
void createPusher(const string &app,const string &stream,const string &url){ void createPusher(const string &app, const string &stream, const string &url) {
//创建推流器并绑定一个RtmpMediaSource //创建推流器并绑定一个RtmpMediaSource
pusher.reset(new RtmpPusher(DEFAULT_VHOST,app.data(), stream.data())); pusher.reset(new RtmpPusher(DEFAULT_VHOST, app.data(), stream.data()));
//设置推流中断处理逻辑 //设置推流中断处理逻辑
pusher->setOnShutdown([app,stream, url](const SockException &ex) { pusher->setOnShutdown([app, stream, url](const SockException &ex) {
WarnL << "Server connection is closed:" << ex.getErrCode() << " " << ex.what(); WarnL << "Server connection is closed:" << ex.getErrCode() << " " << ex.what();
//重试 //重试
rePushDelay(app,stream, url); rePushDelay(app, stream, url);
}); });
//设置发布结果处理逻辑 //设置发布结果处理逻辑
pusher->setOnPublished([app,stream, url](const SockException &ex) { pusher->setOnPublished([app, stream, url](const SockException &ex) {
if (ex) { if (ex) {
WarnL << "Publish fail:" << ex.getErrCode() << " " << ex.what(); WarnL << "Publish fail:" << ex.getErrCode() << " " << ex.what();
//如果发布失败,就重试 //如果发布失败,就重试
rePushDelay(app,stream, url); rePushDelay(app, stream, url);
}else { } else {
InfoL << "Publish success,Please play with player:" << url; InfoL << "Publish success,Please play with player:" << url;
} }
}); });
@ -69,61 +70,58 @@ void createPusher(const string &app,const string &stream,const string &url){
} }
//推流失败或断开延迟2秒后重试推流 //推流失败或断开延迟2秒后重试推流
void rePushDelay(const string &app,const string &stream,const string &url){ void rePushDelay(const string &app, const string &stream, const string &url) {
//上次延时两秒的任务可能还没执行,所以我们要先取消上次任务 //上次延时两秒的任务可能还没执行,所以我们要先取消上次任务
AsyncTaskThread::Instance().CancelTask(0); AsyncTaskThread::Instance().CancelTask(0);
//2秒后执行重新推流的任务 //2秒后执行重新推流的任务
AsyncTaskThread::Instance().DoTaskDelay(0, 2000, [app, stream,url]() { AsyncTaskThread::Instance().DoTaskDelay(0, 2000, [app, stream, url]() {
InfoL << "Re-Publishing..."; InfoL << "Re-Publishing...";
//重新推流 //重新推流
createPusher(app,stream,url); createPusher(app, stream, url);
//此任务不重复 //此任务不重复
return false; return false;
}); });
} }
//这里才是真正执行main函数你可以把函数名(domain)改成main然后就可以输入自定义url了 //这里才是真正执行main函数你可以把函数名(domain)改成main然后就可以输入自定义url了
int domain(int argc, const char *argv[]) { int domain(const string &playUrl, const string &pushUrl) {
//设置退出信号处理函数 //设置退出信号处理函数
signal(SIGINT, [](int){EventPoller::Instance().shutdown();}); signal(SIGINT, [](int) { EventPoller::Instance().shutdown(); });
//设置日志 //设置日志
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace)); Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>()); Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
string playUrl = argv[1]; {
string pushUrl = argv[2]; //拉一个流生成一个RtmpMediaSource源的名称是"app/stream"
//你也可以以其他方式生成RtmpMediaSource比如说MP4文件请查看test_rtmpPusherMp4.cpp代码
PlayerProxy::Ptr player(new PlayerProxy(DEFAULT_VHOST, "app", "stream"));
player->play(playUrl.data());
//拉一个流生成一个RtmpMediaSource源的名称是"app/stream" //监听RtmpMediaSource注册事件,在PlayerProxy播放成功后触发
//你也可以以其他方式生成RtmpMediaSource比如说MP4文件请查看test_rtmpPusherMp4.cpp代码 NoticeCenter::Instance().addListener(nullptr, Config::Broadcast::kBroadcastMediaChanged,
PlayerProxy::Ptr player(new PlayerProxy(DEFAULT_VHOST,"app", "stream")); [pushUrl](BroadcastMediaChangedArgs) {
player->play(playUrl.data()); //媒体源"app/stream"已经注册这时方可新建一个RtmpPusher对象并绑定该媒体源
if(bRegist && schema == RTMP_SCHEMA){
createPusher(app, stream, pushUrl);
}
});
//监听RtmpMediaSource注册事件,在PlayerProxy播放成功后触发 //事件轮询
NoticeCenter::Instance().addListener(nullptr, Config::Broadcast::kBroadcastMediaChanged, [pushUrl](BroadcastMediaChangedArgs) { EventPoller::Instance().runLoop();
//媒体源"app/stream"已经注册这时方可新建一个RtmpPusher对象并绑定该媒体源 }
createPusher(app,stream,pushUrl); //删除事件监听
}); NoticeCenter::Instance().delListener(nullptr);
//事件轮询 //清理程序
EventPoller::Instance().runLoop(); EventPoller::Destory();
//删除事件监听 AsyncTaskThread::Destory();
NoticeCenter::Instance().delListener(nullptr); Logger::Destory();
//销毁代理播放器、推流器 return 0;
player.reset();
pusher.reset();
//清理程序
EventPoller::Destory();
AsyncTaskThread::Destory();
Logger::Destory();
return 0;
} }
int main(int argc, char *argv[]) {
int main(int argc,char *argv[]){ return domain("rtmp://live.hkstv.hk.lxdns.com/live/hks", "rtmp://jizan.iok.la/live/stream");
const char *argList[] = {argv[0],"rtmp://live.hkstv.hk.lxdns.com/live/hks",argv[1]};
return domain(3,argList);
} }

View File

@ -48,7 +48,6 @@ RtmpPusher::Ptr pusher;
void rePushDelay(const string &app,const string &stream,const string &url); void rePushDelay(const string &app,const string &stream,const string &url);
void createPusher(const string &app,const string &stream,const string &url); void createPusher(const string &app,const string &stream,const string &url);
//创建推流器并开始推流 //创建推流器并开始推流
void createPusher(const string &app,const string &stream,const string &url){ void createPusher(const string &app,const string &stream,const string &url){
auto rtmpSrc = dynamic_pointer_cast<RtmpMediaSource>(MediaReader::onMakeMediaSource(RTMP_SCHEMA,DEFAULT_VHOST,app,stream)); auto rtmpSrc = dynamic_pointer_cast<RtmpMediaSource>(MediaReader::onMakeMediaSource(RTMP_SCHEMA,DEFAULT_VHOST,app,stream));
@ -94,18 +93,13 @@ void rePushDelay(const string &app,const string &stream,const string &url){
} }
//这里才是真正执行main函数你可以把函数名(domain)改成main然后就可以输入自定义url了 //这里才是真正执行main函数你可以把函数名(domain)改成main然后就可以输入自定义url了
int domain(int argc,const char *argv[]){ int domain(const string & filePath,const string & pushUrl){
//设置退出信号处理函数 //设置退出信号处理函数
signal(SIGINT, [](int){EventPoller::Instance().shutdown();}); signal(SIGINT, [](int){EventPoller::Instance().shutdown();});
//设置日志 //设置日志
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace)); Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>()); Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
//filePath同时也是流id
string filePath = argv[1];
//推流地址
string pushUrl = argv[2];
//录像应用名称默认为record //录像应用名称默认为record
string appName = mINI::Instance()[Config::Record::kAppName]; string appName = mINI::Instance()[Config::Record::kAppName];
//app必须recordfilePath(流id)为相对于httpRoot/record的路径否则MediaReader会找到不该文件 //app必须recordfilePath(流id)为相对于httpRoot/record的路径否则MediaReader会找到不该文件
@ -131,8 +125,7 @@ int domain(int argc,const char *argv[]){
int main(int argc,char *argv[]){ int main(int argc,char *argv[]){
//MP4文件需要放置在 httpRoot/record目录下,文件负载必须为h264+aac //MP4文件需要放置在 httpRoot/record目录下,文件负载必须为h264+aac
//可以使用test_server生成的mp4文件 //可以使用test_server生成的mp4文件
const char *argList[] = {argv[0],"app/stream/2017-09-30/12-55-38.mp4","rtmp://jizan.iok.la/live/test"}; return domain("app/stream/2017-09-30/12-55-38.mp4","rtmp://jizan.iok.la/live/test");
return domain(3,argList);
} }