From ee9b6b64da58793fb9b92c96f043387fbaaf48b2 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 21 Mar 2018 15:13:33 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E7=BC=96=E8=AF=91=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E4=BB=A5=E5=8F=8A=E8=BF=90=E8=A1=8C=E6=9C=9Fbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 135 ++++++++++++----------------------- ZLToolKit | 1 + tests/CMakeLists.txt | 22 ++---- tests/test_rtmpPusher.cpp | 102 +++++++++++++------------- tests/test_rtmpPusherMp4.cpp | 11 +-- 5 files changed, 106 insertions(+), 165 deletions(-) create mode 160000 ZLToolKit diff --git a/CMakeLists.txt b/CMakeLists.txt index 1189dfc4..5a58498b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,7 @@ project(ZLMediaKit) 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") @@ -7,133 +9,88 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake") set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) #设置可执行程序路径 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) - set(INSTALL_PATH_LIB $ENV{HOME}/${CMAKE_PROJECT_NAME}/lib) - set(INSTALL_PATH_INCLUDE $ENV{HOME}/${CMAKE_PROJECT_NAME}/include) -else() - set(INSTALL_PATH_LIB lib) - set(INSTALL_PATH_INCLUDE include) -endif() +#收集源代码 +file(GLOB ToolKit_src_list ${ToolKit_Root}/*/*.cpp ${ToolKit_Root}/*/*.h ${ToolKit_Root}/*/*.c) +file(GLOB MediaKit_src_list ${MediaKit_Root}/*/*.cpp ${MediaKit_Root}/*/*.h ${MediaKit_Root}/*/*.c) +#去除win32的适配代码 +if (NOT WIN32) + list(REMOVE_ITEM ToolKit_src_list ${ToolKit_Root}/win32/getopt.c) +endif () -foreach(SUB_DIR ${SUB_DIR_LIST}) - #遍历源文件 - 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) +#添加两个静态库 +set(LINK_LIB_LIST zlmediakit zltoolkit) #查找openssl是否安装 find_package(OpenSSL QUIET) -if(OPENSSL_FOUND) - message(STATUS "找到openssl库:\"${OPENSSL_INCLUDE_DIR}\",ENABLE_OPENSSL宏已打开") +if (OPENSSL_FOUND) + message(STATUS "found openssl library\"${OPENSSL_INCLUDE_DIR}\",ENABLE_OPENSSL enabled") include_directories(${OPENSSL_INCLUDE_DIR}) add_definitions(-DENABLE_OPENSSL) list(APPEND LINK_LIB_LIST ${OPENSSL_LIBRARIES}) -endif() +endif (OPENSSL_FOUND) #查找mysql是否安装 find_package(MYSQL QUIET) -if(MYSQL_FOUND) - message(STATUS "找到mysqlclient库:\"${MYSQL_INCLUDE_DIR}\",ENABLE_MYSQL宏已打开") +if (MYSQL_FOUND) + message(STATUS "found mysqlclient library\"${MYSQL_INCLUDE_DIR}\",ENABLE_MYSQL enabled") include_directories(${MYSQL_INCLUDE_DIR}) add_definitions(-DENABLE_MYSQL) list(APPEND LINK_LIB_LIST ${MYSQL_LIBRARIES}) -endif() +endif (MYSQL_FOUND) - -#查找mp4v2是否安装 +#查找MP4V2是否安装 find_package(MP4V2 QUIET) -if(MP4V2_FOUND) - message(STATUS "找到mp4v2库:\"${MP4V2_INCLUDE_DIR}\",ENABLE_MP4V2宏已打开") +if (MP4V2_FOUND) include_directories(${MP4V2_INCLUDE_DIR}) + list(APPEND LINK_LIB_LIST ${MP4V2_LIBRARY}) add_definitions(-DENABLE_MP4V2) - list(APPEND LINK_LIB_LIST ${MP4V2_LIBRARIES}) -endif() + message(STATUS "found MP4V2:${MP4V2_INCLUDE_DIR},${MP4V2_LIBRARY}") +endif (MP4V2_FOUND) #查找x264是否安装 find_package(X264 QUIET) -if(X264_FOUND) - message(STATUS "找到x264库:\"${X264_INCLUDE_DIRS}\",ENABLE_X264宏已打开") +if (X264_FOUND) + message(STATUS "found x264 library\"${X264_INCLUDE_DIRS}\",ENABLE_X264 enabled") include_directories(${X264_INCLUDE_DIRS}) add_definitions(-DENABLE_X264) list(APPEND LINK_LIB_LIST ${X264_LIBRARIES}) -endif() +endif () #查找faac是否安装 find_package(FAAC QUIET) -if(FAAC_FOUND) - message(STATUS "找到faac库:\"${FAAC_INCLUDE_DIR}\",ENABLE_FAAC宏已打开") +if (FAAC_FOUND) + message(STATUS "found faac library\"${FAAC_INCLUDE_DIR}\",ENABLE_FAAC enabled") include_directories(${FAAC_INCLUDE_DIR}) add_definitions(-DENABLE_FAAC) list(APPEND LINK_LIB_LIST ${FAAC_LIBRARIES}) -endif() +endif () -#查找ZLToolKit是否安装 -find_package(ZLTOOLKIT QUIET) -if(ZLTOOLKIT_FOUND) - message(STATUS "找到ZLToolKit库:\"${ZLTOOLKIT_INCLUDE_DIR}\"") - include_directories(${ZLTOOLKIT_INCLUDE_DIR}) - list(APPEND LINK_LIB_LIST ${ZLTOOLKIT_LIBRARIES}) -endif() +#使能GOP缓存 +add_definitions(-DENABLE_RING_USEBUF) +#添加库 +add_library(zltoolkit STATIC ${ToolKit_src_list}) +add_library(zlmediakit STATIC ${MediaKit_src_list}) -#查找JEMALLOC是否安装 -find_package(JEMALLOC QUIET) -if(JEMALLOC_FOUND) - message(STATUS "找到JEMALLOC库:\"${JEMALLOC_INCLUDE_DIR}\"") - include_directories(${JEMALLOC_INCLUDE_DIR}) - list(APPEND LINK_LIB_LIST ${JEMALLOC_LIBRARIES}) -endif() +if (NOT WIN32) + list(APPEND LINK_LIB_LIST pthread) +else() + list(APPEND LINK_LIB_LIST WS2_32 Iphlpapi shlwapi) +endif () -#打印库文件 -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}) +message(STATUS "linked libraries:${LINK_LIB_LIST}") #测试程序 -if(NOT IOS) - add_subdirectory(tests) -endif() +add_subdirectory(tests) diff --git a/ZLToolKit b/ZLToolKit new file mode 160000 index 00000000..378e0503 --- /dev/null +++ b/ZLToolKit @@ -0,0 +1 @@ +Subproject commit 378e05030f5faef37612d9cb69adb70a222b0a9d diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8ac62a62..f4fe9618 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -3,7 +3,7 @@ find_package(SDL QUIET) if(SDL_FOUND) include_directories(${SDL_INCLUDE_DIR}) list(APPEND LINK_LIB_LIST ${SDL_LIBRARY}) - message(STATUS "找到SDL") + message(STATUS " found SDL") endif() #查找ffmpeg/libutil是否安装 @@ -11,7 +11,7 @@ find_package(AVUTIL QUIET) if(AVUTIL_FOUND) include_directories(${AVUTIL_INCLUDE_DIR}) list(APPEND LINK_LIB_LIST ${AVUTIL_LIBRARIES}) - message(STATUS "找到libutil") + message(STATUS " found libutil") endif() #查找ffmpeg/libavcodec是否安装 @@ -19,31 +19,23 @@ find_package(AVCODEC QUIET) if(AVCODEC_FOUND) include_directories(${AVCODEC_INCLUDE_DIR}) list(APPEND LINK_LIB_LIST ${AVCODEC_LIBRARIES}) - message(STATUS "找到libavcodec") + message(STATUS " found libavcodec") endif() aux_source_directory(. TEST_SRC_LIST) #如果ffmpeg/libavcodec ffmpeg/libavcodec SDL 都安装了则编译 test_player if(SDL_FOUND AND AVCODEC_FOUND AND AVUTIL_FOUND) - message(STATUS "test_player被编译") + message(STATUS "test_player will be compiled") 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) endif() foreach(TEST_SRC ${TEST_SRC_LIST}) 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}) - - 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() - + target_link_libraries(${TEST_EXE_NAME} ${LINK_LIB_LIST}) endforeach() diff --git a/tests/test_rtmpPusher.cpp b/tests/test_rtmpPusher.cpp index 8a8eeb2b..3043e195 100644 --- a/tests/test_rtmpPusher.cpp +++ b/tests/test_rtmpPusher.cpp @@ -42,26 +42,27 @@ using namespace ZL::DEV; //推流器,保持强引用 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 - 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(); //重试 - 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) { WarnL << "Publish fail:" << ex.getErrCode() << " " << ex.what(); //如果发布失败,就重试 - rePushDelay(app,stream, url); - }else { + rePushDelay(app, stream, url); + } else { InfoL << "Publish success,Please play with player:" << url; } }); @@ -69,61 +70,58 @@ void createPusher(const string &app,const string &stream,const string &url){ } //推流失败或断开延迟2秒后重试推流 -void rePushDelay(const string &app,const string &stream,const string &url){ - //上次延时两秒的任务可能还没执行,所以我们要先取消上次任务 - AsyncTaskThread::Instance().CancelTask(0); - //2秒后执行重新推流的任务 - AsyncTaskThread::Instance().DoTaskDelay(0, 2000, [app, stream,url]() { - InfoL << "Re-Publishing..."; - //重新推流 - createPusher(app,stream,url); - //此任务不重复 +void rePushDelay(const string &app, const string &stream, const string &url) { + //上次延时两秒的任务可能还没执行,所以我们要先取消上次任务 + AsyncTaskThread::Instance().CancelTask(0); + //2秒后执行重新推流的任务 + AsyncTaskThread::Instance().DoTaskDelay(0, 2000, [app, stream, url]() { + InfoL << "Re-Publishing..."; + //重新推流 + createPusher(app, stream, url); + //此任务不重复 return false; - }); + }); } //这里才是真正执行main函数,你可以把函数名(domain)改成main,然后就可以输入自定义url了 -int domain(int argc, const char *argv[]) { - //设置退出信号处理函数 - signal(SIGINT, [](int){EventPoller::Instance().shutdown();}); - //设置日志 - Logger::Instance().add(std::make_shared("stdout", LTrace)); - Logger::Instance().setWriter(std::make_shared()); +int domain(const string &playUrl, const string &pushUrl) { + //设置退出信号处理函数 + signal(SIGINT, [](int) { EventPoller::Instance().shutdown(); }); + //设置日志 + Logger::Instance().add(std::make_shared("stdout", LTrace)); + Logger::Instance().setWriter(std::make_shared()); - 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,比如说MP4文件(请查看test_rtmpPusherMp4.cpp代码) - PlayerProxy::Ptr player(new PlayerProxy(DEFAULT_VHOST,"app", "stream")); - player->play(playUrl.data()); + //监听RtmpMediaSource注册事件,在PlayerProxy播放成功后触发 + NoticeCenter::Instance().addListener(nullptr, Config::Broadcast::kBroadcastMediaChanged, + [pushUrl](BroadcastMediaChangedArgs) { + //媒体源"app/stream"已经注册,这时方可新建一个RtmpPusher对象并绑定该媒体源 + if(bRegist && schema == RTMP_SCHEMA){ + createPusher(app, stream, pushUrl); + } + }); - //监听RtmpMediaSource注册事件,在PlayerProxy播放成功后触发 - NoticeCenter::Instance().addListener(nullptr, Config::Broadcast::kBroadcastMediaChanged, [pushUrl](BroadcastMediaChangedArgs) { - //媒体源"app/stream"已经注册,这时方可新建一个RtmpPusher对象并绑定该媒体源 - createPusher(app,stream,pushUrl); - }); + //事件轮询 + EventPoller::Instance().runLoop(); + } + //删除事件监听 + NoticeCenter::Instance().delListener(nullptr); - //事件轮询 - EventPoller::Instance().runLoop(); - //删除事件监听 - NoticeCenter::Instance().delListener(nullptr); - //销毁代理播放器、推流器 - player.reset(); - pusher.reset(); - - //清理程序 - EventPoller::Destory(); - AsyncTaskThread::Destory(); - Logger::Destory(); - return 0; + //清理程序 + EventPoller::Destory(); + AsyncTaskThread::Destory(); + Logger::Destory(); + return 0; } - -int main(int argc,char *argv[]){ - const char *argList[] = {argv[0],"rtmp://live.hkstv.hk.lxdns.com/live/hks",argv[1]}; - return domain(3,argList); +int main(int argc, char *argv[]) { + return domain("rtmp://live.hkstv.hk.lxdns.com/live/hks", "rtmp://jizan.iok.la/live/stream"); } diff --git a/tests/test_rtmpPusherMp4.cpp b/tests/test_rtmpPusherMp4.cpp index d85b176a..ccc696ff 100644 --- a/tests/test_rtmpPusherMp4.cpp +++ b/tests/test_rtmpPusherMp4.cpp @@ -48,7 +48,6 @@ RtmpPusher::Ptr pusher; 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){ auto rtmpSrc = dynamic_pointer_cast(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了 -int domain(int argc,const char *argv[]){ +int domain(const string & filePath,const string & pushUrl){ //设置退出信号处理函数 signal(SIGINT, [](int){EventPoller::Instance().shutdown();}); //设置日志 Logger::Instance().add(std::make_shared("stdout", LTrace)); Logger::Instance().setWriter(std::make_shared()); - //filePath同时也是流id - string filePath = argv[1]; - //推流地址 - string pushUrl = argv[2]; - //录像应用名称默认为record string appName = mINI::Instance()[Config::Record::kAppName]; //app必须record,filePath(流id)为相对于httpRoot/record的路径,否则MediaReader会找到不该文件 @@ -131,8 +125,7 @@ int domain(int argc,const char *argv[]){ int main(int argc,char *argv[]){ //MP4文件需要放置在 httpRoot/record目录下,文件负载必须为h264+aac //可以使用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(3,argList); + return domain("app/stream/2017-09-30/12-55-38.mp4","rtmp://jizan.iok.la/live/test"); }