From 0739b1ddd20f927f2d48870328d459ea0f7f7bb1 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Wed, 25 May 2022 14:50:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90FFmpeg=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=BF=81=E7=A7=BB=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 110 +++++++++++++++++++++++++++++++++++++--- player/AudioSRC.h | 1 - player/CMakeLists.txt | 79 +++-------------------------- player/SDLAudioDevice.h | 2 +- player/test_player.cpp | 4 +- 5 files changed, 113 insertions(+), 83 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index eb1e06f9..24d91b50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,6 +102,7 @@ option(ENABLE_MEM_DEBUG "Enable Memory Debug" false) option(ENABLE_ASAN "Enable Address Sanitize" false) option(ENABLE_WEBRTC "Enable WebRTC" true) option(ENABLE_PLAYER "Enable Player" true) +option(ENABLE_FFMPEG "Enable FFmpeg" true) option(ENABLE_MSVC_MT "Enable MSVC Mt/Mtd lib" true) option(ENABLE_API_STATIC_LIB "Enable mk_api static lib" false) option(USE_SOLUTION_FOLDERS "Enable solution dir supported" ON) @@ -200,13 +201,108 @@ endif () set(LINK_LIB_LIST zlmediakit zltoolkit) -##默认链接jemalloc库避免内存碎片 -#find_package(JEMALLOC QUIET) -#if (JEMALLOC_FOUND) -# message(STATUS "found library:\"${JEMALLOC_LIBRARIES}\"") -# include_directories(${JEMALLOC_INCLUDE_DIR}) -# list(APPEND LINK_LIB_LIST ${JEMALLOC_LIBRARIES}) -#endif () +if (ENABLE_FFMPEG) + find_package(PkgConfig QUIET) + #查找ffmpeg/libutil是否安装 + if (PKG_CONFIG_FOUND) + pkg_check_modules(AVUTIL QUIET IMPORTED_TARGET libavutil) + if (AVUTIL_FOUND) + include_directories(${AVUTIL_INCLUDE_DIRS}) + link_directories(${AVUTIL_LIBRARY_DIRS}) + list(APPEND LINK_LIB_LIST ${AVUTIL_LIBRARIES}) + message(STATUS "found library:${AVUTIL_LIBRARIES}") + endif () + endif () + + #查找ffmpeg/libavcodec是否安装 + if (PKG_CONFIG_FOUND) + pkg_check_modules(AVCODEC QUIET IMPORTED_TARGET libavcodec) + if (AVCODEC_FOUND) + include_directories(${AVCODEC_INCLUDE_DIRS}) + link_directories(${AVCODEC_LIBRARY_DIRS}) + list(APPEND LINK_LIB_LIST ${AVCODEC_LIBRARIES}) + message(STATUS "found library:${AVCODEC_LIBRARIES}") + endif () + endif () + + #查找ffmpeg/libswscale是否安装 + if (PKG_CONFIG_FOUND) + pkg_check_modules(SWSCALE QUIET IMPORTED_TARGET libswscale) + if (SWSCALE_FOUND) + include_directories(${SWSCALE_INCLUDE_DIRS}) + link_directories(${SWSCALE_LIBRARY_DIRS}) + list(APPEND LINK_LIB_LIST ${SWSCALE_LIBRARIES}) + message(STATUS "found library:${SWSCALE_LIBRARIES}") + endif () + endif () + + #查找ffmpeg/libswresample是否安装 + if (PKG_CONFIG_FOUND) + pkg_check_modules(SWRESAMPLE QUIET IMPORTED_TARGET libswresample) + if (SWRESAMPLE_FOUND) + include_directories(${SWRESAMPLE_INCLUDE_DIRS}) + link_directories(${SWRESAMPLE_LIBRARY_DIRS}) + list(APPEND LINK_LIB_LIST ${SWRESAMPLE_LIBRARIES}) + message(STATUS "found library:${SWRESAMPLE_LIBRARIES}") + endif () + endif () + + #查找ffmpeg/libutil是否安装 + if (NOT AVUTIL_FOUND) + find_package(AVUTIL QUIET) + if (AVUTIL_FOUND) + include_directories(${AVUTIL_INCLUDE_DIR}) + list(APPEND LINK_LIB_LIST ${AVUTIL_LIBRARIES}) + message(STATUS "found library:${AVUTIL_LIBRARIES}") + endif () + endif () + + #查找ffmpeg/libavcodec是否安装 + if (NOT AVCODEC_FOUND) + find_package(AVCODEC QUIET) + if (AVCODEC_FOUND) + include_directories(${AVCODEC_INCLUDE_DIR}) + list(APPEND LINK_LIB_LIST ${AVCODEC_LIBRARIES}) + message(STATUS "found library:${AVCODEC_LIBRARIES}") + endif () + endif () + + #查找ffmpeg/libswscale是否安装 + if (NOT SWSCALE_FOUND) + find_package(SWSCALE QUIET) + if (SWSCALE_FOUND) + include_directories(${SWSCALE_INCLUDE_DIR}) + list(APPEND LINK_LIB_LIST ${SWSCALE_LIBRARIES}) + message(STATUS "found library:${SWSCALE_LIBRARIES}") + endif () + endif () + + #查找ffmpeg/libswresample是否安装 + if (NOT SWRESAMPLE_FOUND) + find_package(SWRESAMPLE QUIET) + if (SWRESAMPLE_FOUND) + include_directories(${SWRESAMPLE_INCLUDE_DIRS}) + list(APPEND LINK_LIB_LIST ${SWRESAMPLE_LIBRARIES}) + message(STATUS "found library:${SWRESAMPLE_LIBRARIES}") + endif () + endif () + + if (AVUTIL_FOUND AND AVCODEC_FOUND AND SWSCALE_FOUND AND SWRESAMPLE_FOUND) + add_definitions(-DENABLE_FFMPEG) + else () + set(ENABLE_FFMPEG off) + message(WARNING "ffmpeg相关功能未找到") + endif () + +endif () + +#默认链接jemalloc库避免内存碎片 +find_package(JEMALLOC QUIET) +if (JEMALLOC_FOUND) + message(STATUS "found library:\"${JEMALLOC_LIBRARIES}\"") + include_directories(${JEMALLOC_INCLUDE_DIR}) + list(APPEND LINK_LIB_LIST ${JEMALLOC_LIBRARIES}) +endif () #查找openssl是否安装 find_package(OpenSSL QUIET) diff --git a/player/AudioSRC.h b/player/AudioSRC.h index 0a631750..3180f4e8 100644 --- a/player/AudioSRC.h +++ b/player/AudioSRC.h @@ -28,7 +28,6 @@ extern "C" { #include "Network/Buffer.h" #include "SDLAudioDevice.h" -#include "FFMpegDecoder.h" class AudioSRCDelegate { public: diff --git a/player/CMakeLists.txt b/player/CMakeLists.txt index 26298b39..d2993dc2 100644 --- a/player/CMakeLists.txt +++ b/player/CMakeLists.txt @@ -1,5 +1,8 @@ -find_package(PkgConfig QUIET) +if (NOT ENABLE_FFMPEG) + return() +endif () +find_package(PkgConfig QUIET) #查找SDL2是否安装 if (PKG_CONFIG_FOUND) pkg_check_modules(SDL2 QUIET IMPORTED_TARGET sdl2) @@ -18,70 +21,16 @@ else () endif (SDL2_FOUND) endif () -#查找ffmpeg/libutil是否安装 -if (PKG_CONFIG_FOUND) - pkg_check_modules(AVUTIL QUIET IMPORTED_TARGET libavutil) - if (AVUTIL_FOUND) - include_directories(${AVUTIL_INCLUDE_DIRS}) - link_directories(${AVUTIL_LIBRARY_DIRS}) - list(APPEND LINK_LIB_LIST ${AVUTIL_LIBRARIES}) - message(STATUS "found library:${AVUTIL_LIBRARIES}") - endif () -else () - find_package(AVUTIL QUIET) - if (AVUTIL_FOUND) - include_directories(${AVUTIL_INCLUDE_DIR}) - list(APPEND LINK_LIB_LIST ${AVUTIL_LIBRARIES}) - message(STATUS "found library:${AVUTIL_LIBRARIES}") - endif () -endif () - -#查找ffmpeg/libswresample是否安装 -if (PKG_CONFIG_FOUND) - pkg_check_modules(SWRESAMPLE QUIET IMPORTED_TARGET libswresample) - if (SWRESAMPLE_FOUND) - include_directories(${SWRESAMPLE_INCLUDE_DIRS}) - link_directories(${SWRESAMPLE_LIBRARY_DIRS}) - list(APPEND LINK_LIB_LIST ${SWRESAMPLE_LIBRARIES}) - message(STATUS "found library:${SWRESAMPLE_LIBRARIES}") - endif () -else () - find_package(SWRESAMPLE QUIET) - if (SWRESAMPLE_FOUND) - include_directories(${SWRESAMPLE_INCLUDE_DIR}) - list(APPEND LINK_LIB_LIST ${SWRESAMPLE_LIBRARIES}) - message(STATUS "found library:${SWRESAMPLE_LIBRARIES}") - endif () -endif () - -#查找ffmpeg/libavcodec是否安装 -if (PKG_CONFIG_FOUND) - pkg_check_modules(AVCODEC QUIET IMPORTED_TARGET libavcodec) - if (AVCODEC_FOUND) - include_directories(${AVCODEC_INCLUDE_DIRS}) - link_directories(${AVCODEC_LIBRARY_DIRS}) - list(APPEND LINK_LIB_LIST ${AVCODEC_LIBRARIES}) - message(STATUS "found library:${AVCODEC_LIBRARIES}") - endif () -else () - find_package(AVCODEC QUIET) - if (AVCODEC_FOUND) - include_directories(${AVCODEC_INCLUDE_DIR}) - list(APPEND LINK_LIB_LIST ${AVCODEC_LIBRARIES}) - message(STATUS "found library:${AVCODEC_LIBRARIES}") - endif () -endif () - set(PLAYER_NAME "test_player") #如果ffmpeg/libavcodec ffmpeg/libavcodec SDL 都安装了则编译播放器 -if (SDL2_FOUND AND AVCODEC_FOUND AND AVUTIL_FOUND AND SWRESAMPLE_FOUND) - message(STATUS "${PLAYER_NAME} enabled") -else () +if (NOT SDL2_FOUND) message(STATUS "${PLAYER_NAME} disabled, please install sdl2 ffmpeg/libavcodec ffmpeg/libavutil ffmpeg/libswresample") return() endif () +message(STATUS "${PLAYER_NAME} enabled") + aux_source_directory(. SRC_LIST) add_executable(${PLAYER_NAME} ${SRC_LIST}) @@ -95,17 +44,3 @@ if (CMAKE_SYSTEM_NAME MATCHES "Linux") else () target_link_libraries(${PLAYER_NAME} ${LINK_LIB_LIST}) endif () - - - - - - - - - - - - - - diff --git a/player/SDLAudioDevice.h b/player/SDLAudioDevice.h index 703ed2ee..901c638b 100644 --- a/player/SDLAudioDevice.h +++ b/player/SDLAudioDevice.h @@ -19,7 +19,7 @@ #define DEFAULT_SAMPLERATE 48000 #define DEFAULT_FORMAT AUDIO_S16 #define DEFAULT_CHANNEL 2 -#define DEFAULT_SAMPLES 1024 +#define DEFAULT_SAMPLES 2048 class AudioSRC; diff --git a/player/test_player.cpp b/player/test_player.cpp index b3ee1891..7fa68bf2 100644 --- a/player/test_player.cpp +++ b/player/test_player.cpp @@ -14,7 +14,7 @@ #include "Rtsp/UDPServer.h" #include "Player/MediaPlayer.h" #include "Util/onceToken.h" -#include "FFMpegDecoder.h" +#include "Codec/Transcode.h" #include "YuvDisplayer.h" #include "AudioSRC.h" using namespace std; @@ -103,7 +103,7 @@ int main(int argc, char *argv[]) { } auto pcm = swr->inputFrame(frame); auto len = pcm->get()->nb_samples * pcm->get()->channels * av_get_bytes_per_sample((enum AVSampleFormat)pcm->get()->format); - audio_player->playPCM((const char *) (pcm->get()->data[0]), len); + audio_player->playPCM((const char *) (pcm->get()->data[0]), MIN(len, frame->get()->linesize[0])); }); auto audio_delegate = std::make_shared( [decoder](const Frame::Ptr &frame) { return decoder->inputFrame(frame, false);