From 87afd616168307558dae8911222e14feef0b9096 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Thu, 4 Apr 2019 11:30:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E7=BC=96=E8=AF=91=E6=9F=90=E7=89=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 58 ++++++++------ cmake/FindMYSQL.cmake | 2 - src/MediaFile/MediaRecorder.cpp | 20 +++-- src/MediaFile/MediaRecorder.h | 13 ++-- src/MediaFile/TsMuxer.cpp | 7 +- src/MediaFile/TsMuxer.h | 132 -------------------------------- tests/CMakeLists.txt | 7 +- 7 files changed, 62 insertions(+), 177 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b62f4bb..685c4162 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,6 @@ set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) #设置工程源码根目录 set(ToolKit_Root ${CMAKE_SOURCE_DIR}/3rdpart/ZLToolKit/src) -set(MediaServer_Root ${CMAKE_SOURCE_DIR}/3rdpart/media-server) set(MediaKit_Root ${CMAKE_SOURCE_DIR}/src) #设置头文件目录 @@ -31,40 +30,54 @@ else() add_definitions(-DWIN32_LEAN_AND_MEAN -DMP4V2_NO_STDINT_DEFS) endif () +set(ENABLE_HLS true) +set(ENABLE_OPENSSL true) +set(ENABLE_MYSQL true) +set(ENABLE_MP4V2 true) +set(ENABLE_FAAC true) +set(ENABLE_X264 true) + #添加两个静态库 -set(LINK_LIB_LIST zlmediakit zltoolkit mpeg) +if(ENABLE_HLS) + set(LINK_LIB_LIST zlmediakit zltoolkit mpeg) + set(MediaServer_Root ${CMAKE_SOURCE_DIR}/3rdpart/media-server) + add_definitions(-DENABLE_HLS) + message(STATUS "ENABLE_HLS defined") +else() + set(LINK_LIB_LIST zlmediakit zltoolkit) +endif() #查找openssl是否安装 find_package(OpenSSL QUIET) -if (OPENSSL_FOUND) - message(STATUS "found openssl library\"${OPENSSL_INCLUDE_DIR}\",ENABLE_OPENSSL enabled") +if (OPENSSL_FOUND AND ENABLE_OPENSSL) + message(STATUS "found library:${OPENSSL_LIBRARIES},ENABLE_OPENSSL defined") include_directories(${OPENSSL_INCLUDE_DIR}) add_definitions(-DENABLE_OPENSSL) list(APPEND LINK_LIB_LIST ${OPENSSL_LIBRARIES}) -endif (OPENSSL_FOUND) +endif () #查找mysql是否安装 find_package(MYSQL QUIET) -if (MYSQL_FOUND) - message(STATUS "found mysqlclient library\"${MYSQL_INCLUDE_DIR}\",ENABLE_MYSQL enabled") +if (MYSQL_FOUND AND ENABLE_MYSQL) + message(STATUS "found library:${MYSQL_LIBRARIES},ENABLE_MYSQL defined") include_directories(${MYSQL_INCLUDE_DIR}) add_definitions(-DENABLE_MYSQL) list(APPEND LINK_LIB_LIST ${MYSQL_LIBRARIES}) -endif (MYSQL_FOUND) +endif () #查找MP4V2是否安装 find_package(MP4V2 QUIET) -if (MP4V2_FOUND) +if (MP4V2_FOUND AND ENABLE_MP4V2) include_directories(${MP4V2_INCLUDE_DIR}) list(APPEND LINK_LIB_LIST ${MP4V2_LIBRARY}) add_definitions(-DENABLE_MP4V2) - message(STATUS "found MP4V2:${MP4V2_INCLUDE_DIR},${MP4V2_LIBRARY}") -endif (MP4V2_FOUND) + message(STATUS "found library:${MP4V2_LIBRARY},ENABLE_MP4V2 defined") +endif () #查找x264是否安装 find_package(X264 QUIET) -if (X264_FOUND) - message(STATUS "found x264 library\"${X264_INCLUDE_DIRS}\",ENABLE_X264 enabled") +if (X264_FOUND AND ENABLE_X264) + message(STATUS "found library:${X264_LIBRARIES},ENABLE_X264 defined") include_directories(${X264_INCLUDE_DIRS}) add_definitions(-DENABLE_X264) list(APPEND LINK_LIB_LIST ${X264_LIBRARIES}) @@ -72,25 +85,24 @@ endif () #查找faac是否安装 find_package(FAAC QUIET) -if (FAAC_FOUND) - message(STATUS "found faac library\"${FAAC_INCLUDE_DIR}\",ENABLE_FAAC enabled") +if (FAAC_FOUND AND ENABLE_FAAC) + message(STATUS "found library:${FAAC_LIBRARIES},ENABLE_FAAC defined") include_directories(${FAAC_INCLUDE_DIR}) add_definitions(-DENABLE_FAAC) list(APPEND LINK_LIB_LIST ${FAAC_LIBRARIES}) endif () -#使能GOP缓存 -add_definitions(-DENABLE_RING_USEBUF) #添加库 add_library(zltoolkit STATIC ${ToolKit_src_list}) add_library(zlmediakit STATIC ${MediaKit_src_list}) #libmpeg -aux_source_directory(${MediaServer_Root}/libmpeg/include src_mpeg) -aux_source_directory(${MediaServer_Root}/libmpeg/source src_mpeg) -include_directories(${MediaServer_Root}/libmpeg/include) -add_library(mpeg STATIC ${src_mpeg}) - +if(ENABLE_HLS) + aux_source_directory(${MediaServer_Root}/libmpeg/include src_mpeg) + aux_source_directory(${MediaServer_Root}/libmpeg/source src_mpeg) + include_directories(${MediaServer_Root}/libmpeg/include) + add_library(mpeg STATIC ${src_mpeg}) +endif() if (WIN32) list(APPEND LINK_LIB_LIST WS2_32 Iphlpapi shlwapi) @@ -98,8 +110,6 @@ elseif(NOT ANDROID OR IOS) list(APPEND LINK_LIB_LIST pthread) endif () -message(STATUS "linked libraries:${LINK_LIB_LIST}") - #测试程序 add_subdirectory(tests) diff --git a/cmake/FindMYSQL.cmake b/cmake/FindMYSQL.cmake index 3828ed3e..fff04296 100644 --- a/cmake/FindMYSQL.cmake +++ b/cmake/FindMYSQL.cmake @@ -113,7 +113,6 @@ check_cxx_source_compiles( "#include \nint main() { int i = MYSQL_OPT_U if(MYSQL_INCLUDE_DIR AND MYSQL_LIBRARIES) set(MYSQL_FOUND TRUE) - message(STATUS "Found MySQL: ${MYSQL_INCLUDE_DIR}, ${MYSQL_LIBRARIES}") else(MYSQL_INCLUDE_DIR AND MYSQL_LIBRARIES) set(MYSQL_FOUND FALSE) message(STATUS "MySQL not found.") @@ -124,7 +123,6 @@ if(MYSQL_INCLUDE_DIR AND MYSQL_EMBEDDED_LIBRARIES AND HAVE_MYSQL_OPT_EMBEDDED_CO message(STATUS "Found MySQL Embedded: ${MYSQL_INCLUDE_DIR}, ${MYSQL_EMBEDDED_LIBRARIES}") else(MYSQL_INCLUDE_DIR AND MYSQL_EMBEDDED_LIBRARIES AND HAVE_MYSQL_OPT_EMBEDDED_CONNECTION) set(MYSQL_EMBEDDED_FOUND FALSE) - message(STATUS "MySQL Embedded not found.") endif(MYSQL_INCLUDE_DIR AND MYSQL_EMBEDDED_LIBRARIES AND HAVE_MYSQL_OPT_EMBEDDED_CONNECTION) mark_as_advanced(MYSQL_INCLUDE_DIR MYSQL_LIBRARIES MYSQL_EMBEDDED_LIBRARIES) diff --git a/src/MediaFile/MediaRecorder.cpp b/src/MediaFile/MediaRecorder.cpp index a5d977e7..35617267 100644 --- a/src/MediaFile/MediaRecorder.cpp +++ b/src/MediaFile/MediaRecorder.cpp @@ -52,12 +52,14 @@ MediaRecorder::MediaRecorder(const string &strVhost_tmp, strVhost = DEFAULT_VHOST; } +#if defined(ENABLE_HLS) if(enableHls) { auto m3u8FilePath = hlsPath + "/" + strVhost + "/" + strApp + "/" + strId + "/hls.m3u8"; _hlsMaker.reset(new HlsRecorder(m3u8FilePath,string(VHOST_KEY) + "=" + strVhost ,hlsBufSize, hlsDuration, hlsNum)); } +#endif //defined(ENABLE_HLS) -#ifdef ENABLE_MP4V2 +#if defined(ENABLE_MP4V2) GET_CONFIG_AND_REGISTER(string,recordPath,Record::kFilePath); GET_CONFIG_AND_REGISTER(string,recordAppName,Record::kAppName); @@ -65,32 +67,38 @@ MediaRecorder::MediaRecorder(const string &strVhost_tmp, auto mp4FilePath = recordPath + "/" + strVhost + "/" + recordAppName + "/" + strApp + "/" + strId + "/"; _mp4Maker.reset(new Mp4Maker(mp4FilePath,strVhost,strApp,strId)); } -#endif //ENABLE_MP4V2 +#endif //defined(ENABLE_MP4V2) } MediaRecorder::~MediaRecorder() { } void MediaRecorder::inputFrame(const Frame::Ptr &frame) { +#if defined(ENABLE_HLS) if (_hlsMaker) { _hlsMaker->inputFrame(frame); } -#ifdef ENABLE_MP4V2 +#endif //defined(ENABLE_HLS) + +#if defined(ENABLE_MP4V2) if (_mp4Maker) { _mp4Maker->inputFrame(frame); } -#endif //ENABLE_MP4V2 +#endif //defined(ENABLE_MP4V2) } void MediaRecorder::addTrack(const Track::Ptr &track) { +#if defined(ENABLE_HLS) if (_hlsMaker) { _hlsMaker->addTrack(track); } -#ifdef ENABLE_MP4V2 +#endif //defined(ENABLE_HLS) + +#if defined(ENABLE_MP4V2) if (_mp4Maker) { _mp4Maker->addTrack(track); } -#endif //ENABLE_MP4V2 +#endif //defined(ENABLE_MP4V2) } } /* namespace mediakit */ diff --git a/src/MediaFile/MediaRecorder.h b/src/MediaFile/MediaRecorder.h index ceabec2d..f2f09117 100644 --- a/src/MediaFile/MediaRecorder.h +++ b/src/MediaFile/MediaRecorder.h @@ -30,12 +30,9 @@ #include #include "Player/PlayerBase.h" #include "Common/MediaSink.h" - -#ifdef ENABLE_MP4V2 #include "Mp4Maker.h" -#endif //ENABLE_MP4V2 - #include "HlsRecorder.h" + using namespace toolkit; namespace mediakit { @@ -63,11 +60,13 @@ public: */ void addTrack(const Track::Ptr & track) override; private: +#if defined(ENABLE_HLS) std::shared_ptr _hlsMaker; -#ifdef ENABLE_MP4V2 - std::shared_ptr _mp4Maker; -#endif //ENABLE_MP4V2 +#endif //defined(ENABLE_HLS) +#if defined(ENABLE_MP4V2) + std::shared_ptr _mp4Maker; +#endif //defined(ENABLE_MP4V2) }; } /* namespace mediakit */ diff --git a/src/MediaFile/TsMuxer.cpp b/src/MediaFile/TsMuxer.cpp index 999d11ea..eeac65bc 100644 --- a/src/MediaFile/TsMuxer.cpp +++ b/src/MediaFile/TsMuxer.cpp @@ -25,8 +25,9 @@ */ #include "TsMuxer.h" -#include +#if defined(ENABLE_HLS) #include "mpeg-ts-proto.h" +#include "mpeg-ts.h" namespace mediakit { @@ -121,4 +122,6 @@ void TsMuxer::uninit() { _codecid_to_stream_id.clear(); } -}//namespace mediakit \ No newline at end of file +}//namespace mediakit + +#endif// defined(ENABLE_HLS) \ No newline at end of file diff --git a/src/MediaFile/TsMuxer.h b/src/MediaFile/TsMuxer.h index 188de897..3b02e704 100644 --- a/src/MediaFile/TsMuxer.h +++ b/src/MediaFile/TsMuxer.h @@ -28,7 +28,6 @@ #define TSMUXER_H #include -#include "mpeg-ts.h" #include "Extension/Frame.h" #include "Extension/Track.h" #include "Util/File.h" @@ -56,136 +55,5 @@ private: List _frameCached; }; - - -class TsMuxer2{ -public: - typedef function onTsCallback; - TsMuxer2(){ - init(); - } - ~TsMuxer2(){ - uninit(); - } - - bool addTrack(int track,int codec_id){ - lock_guard lck(_mtx); - auto it = _allTrackMap.find(track); - if(it != _allTrackMap.end()){ -// WarnL << "Track:" << track << "已经存在!"; - return false; - } - _allTrackMap[track] = codec_id; - resetAllTracks(); - return true; - } - - bool removeTrack(int track){ - lock_guard lck(_mtx); - auto it = _allTrackMap.find(track); - if(it == _allTrackMap.end()){ -// WarnL << "Track:" << track << "不存在!"; - return false; - } - DebugL << "删除Track:" << track; - _allTrackMap.erase(it); - resetAllTracks(); - return true; - } - - bool inputTrackData(int track, const char *data, int length, int64_t pts, int64_t dts, int flags){ - lock_guard lck(_mtx); - auto it = _track_id_to_stream_id.find(track); - if(it == _track_id_to_stream_id.end()){ - WarnL << "Track:" << track << "不存在!"; - return false; - } - mpeg_ts_write(_context,it->second,flags,pts,dts,data,length); - return true; - } - - void setOnTsCallback(const onTsCallback &cb) { - lock_guard lck(_mtx); - _onts = cb; - } - - bool saveToFile(const string &file){ - lock_guard lck(_mtx); - FILE *fp = File::createfile_file(file.data(),"ab"); - if(!fp){ - WarnL << "打开文件失败:" << file << " " << get_uv_errmsg(); - return false; - } - setvbuf(fp, _file_buf, _IOFBF, sizeof(_file_buf)); - _file.reset(fp,[](FILE *fp){ - fclose(fp); - }); - return true; - } -private: - void init() { - lock_guard lck(_mtx); - static mpeg_ts_func_t s_func= { - [](void* param, size_t bytes){ - TsMuxer2 *muxer = (TsMuxer2 *)param; - assert(sizeof(TsMuxer2::_tsbuf) >= bytes); - return (void *)muxer->_tsbuf; - }, - [](void* param, void* packet){ - //do nothing - }, - [](void* param, const void* packet, size_t bytes){ - TsMuxer2 *muxer = (TsMuxer2 *)param; - muxer->onTs(packet, bytes); - } - }; - if(_context == nullptr){ - _context = mpeg_ts_create(&s_func,this); - } - } - - void uninit() { - lock_guard lck(_mtx); - if(_context){ - mpeg_ts_destroy(_context); - _context = nullptr; - } - _track_id_to_stream_id.clear(); - } - - void resetAllTracks(){ - lock_guard lck(_mtx); - uninit(); - init(); - - //添加Track - for (auto &pr : _allTrackMap){ - InfoL << "添加Track:" << pr.first << " " << pr.second; - _track_id_to_stream_id[pr.first] = mpeg_ts_add_stream(_context,pr.second, nullptr,0); - } - } - - void onTs(const void *packet, int bytes) { - lock_guard lck(_mtx); - if(_onts){ - _onts(packet,bytes); - } - - if(_file){ - fwrite(packet,bytes,1,_file.get()); - } - } -private: - void *_context = nullptr; - char *_tsbuf[188]; - unordered_map _track_id_to_stream_id; - unordered_map _allTrackMap; - recursive_mutex _mtx; - onTsCallback _onts; - - char _file_buf[64 * 1024]; - std::shared_ptr _file; -}; - }//namespace mediakit #endif //TSMUXER_H diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6301f927..9cabf288 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -5,7 +5,7 @@ find_package(SDL2 QUIET) if (SDL2_FOUND) include_directories(${SDL2_INCLUDE_DIR}) list(APPEND LINK_LIB_LIST ${SDL2_LIBRARY}) - message(STATUS "found SDL2:${SDL2_INCLUDE_DIR},${SDL2_LIBRARY}") + message(STATUS "found library:${SDL2_LIBRARY}") endif (SDL2_FOUND) #查找ffmpeg/libutil是否安装 @@ -13,7 +13,7 @@ find_package(AVUTIL QUIET) if(AVUTIL_FOUND) include_directories(${AVUTIL_INCLUDE_DIR}) list(APPEND LINK_LIB_LIST ${AVUTIL_LIBRARIES}) - message(STATUS "found libutil ${AVUTIL_LIBRARIES}") + message(STATUS "found library:${AVUTIL_LIBRARIES}") endif() #查找ffmpeg/libavcodec是否安装 @@ -21,13 +21,12 @@ find_package(AVCODEC QUIET) if(AVCODEC_FOUND) include_directories(${AVCODEC_INCLUDE_DIR}) list(APPEND LINK_LIB_LIST ${AVCODEC_LIBRARIES}) - message(STATUS "found libavcodec ${AVCODEC_LIBRARIES}") + message(STATUS "found library:${AVCODEC_LIBRARIES}") endif() aux_source_directory(. TEST_SRC_LIST) #如果ffmpeg/libavcodec ffmpeg/libavcodec SDL 都安装了则编译 test_player if(SDL2_FOUND AND AVCODEC_FOUND AND AVUTIL_FOUND) - message(STATUS "test_player will be compiled") else() message(STATUS "test_player ingored, please install sdl2 ffmpeg/libavcodec ffmpeg/libavutil") list(REMOVE_ITEM TEST_SRC_LIST ./test_player.cpp)