Commit 3f2137db by xiongziliang

基本废弃 AsyncTaskThread的代码

parent bf974d5b
Subproject commit a8e3772977b953c123c3e6129fb142032db50aec
Subproject commit 6d2163ca273012f9d5eafe5b5e5baf6e642d7bbc
MediaKitWrapper
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/c_wrapper.iml" filepath="$PROJECT_DIR$/.idea/c_wrapper.iml" />
</modules>
</component>
</project>
\ No newline at end of file
cmake_minimum_required(VERSION 2.8)
project(MediaKitWrapper)
#加载自定义模块
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
#设置库文件路径
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
#设置可执行程序路径
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
#设置需要链接的库
set(LINK_LIB_LIST)
#安装目录
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()
#查找openssl是否安装
find_package(OpenSSL QUIET)
if(OPENSSL_FOUND)
message(STATUS "找到openssl库:\"${OPENSSL_INCLUDE_DIR}\",ENABLE_OPENSSL宏已打开")
include_directories(${OPENSSL_INCLUDE_DIR})
add_definitions(-DENABLE_OPENSSL)
list(APPEND LINK_LIB_LIST ${OPENSSL_LIBRARIES})
endif()
#查找mysql是否安装
find_package(MYSQL QUIET)
if(MYSQL_FOUND)
message(STATUS "找到mysqlclient库:\"${MYSQL_INCLUDE_DIR}\",ENABLE_MYSQL宏已打开")
include_directories(${MYSQL_INCLUDE_DIR})
add_definitions(-DENABLE_MYSQL)
list(APPEND LINK_LIB_LIST ${MYSQL_LIBRARIES})
endif()
#查找mp4v2是否安装
find_package(MP4V2 QUIET)
if(MP4V2_FOUND)
message(STATUS "找到mp4v2库:\"${MP4V2_INCLUDE_DIR}\",ENABLE_MP4V2宏已打开")
include_directories(${MP4V2_INCLUDE_DIR})
add_definitions(-DENABLE_MP4V2)
list(APPEND LINK_LIB_LIST ${MP4V2_LIBRARIES})
endif()
#查找x264是否安装
find_package(X264 QUIET)
if(X264_FOUND)
message(STATUS "找到x264库:\"${X264_INCLUDE_DIRS}\",ENABLE_X264宏已打开")
include_directories(${X264_INCLUDE_DIRS})
add_definitions(-DENABLE_X264)
list(APPEND LINK_LIB_LIST ${X264_LIBRARIES})
endif()
#查找faac是否安装
find_package(FAAC QUIET)
if(FAAC_FOUND)
message(STATUS "找到faac库:\"${FAAC_INCLUDE_DIR}\",ENABLE_FAAC宏已打开")
include_directories(${FAAC_INCLUDE_DIR})
add_definitions(-DENABLE_FAAC)
list(APPEND LINK_LIB_LIST ${FAAC_LIBRARIES})
endif()
#设置工程源码根目录
set(ToolKit_Root ${CMAKE_SOURCE_DIR}/../ZLToolKit/src)
set(MediaKit_Root ${CMAKE_SOURCE_DIR}/../src)
#设置头文件目录
INCLUDE_DIRECTORIES(${ToolKit_Root})
INCLUDE_DIRECTORIES(${MediaKit_Root})
#收集源代码
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)
else()
#防止Windows.h包含Winsock.h
add_definitions(-DWIN32_LEAN_AND_MEAN -DMP4V2_NO_STDINT_DEFS)
endif ()
#使能GOP缓存
add_definitions(-DENABLE_RING_USEBUF)
#引用头文件路径
include_directories(${CMAKE_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(NOT WIN32)
#收集源文件
file(GLOB SRC_LIST src/*.cpp src/*.h)
if (WIN32)
list(APPEND LINK_LIB_LIST WS2_32 Iphlpapi shlwapi)
elseif(NOT ANDROID OR IOS)
list(APPEND LINK_LIB_LIST pthread)
endif ()
add_library(zltoolkit STATIC ${ToolKit_src_list})
add_library(zlmediakit STATIC ${MediaKit_src_list})
add_library(${CMAKE_PROJECT_NAME}_shared SHARED ${SRC_LIST})
target_link_libraries(${CMAKE_PROJECT_NAME}_shared zlmediakit zltoolkit ${LINK_LIB_LIST})
#测试程序
if(NOT IOS)
add_subdirectory(tests)
endif(NOT IOS)
# Copyright (c) 2014, Pavel Rojtberg
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ------------------------------------------------------------------------------
# Usage:
# 1. place AndroidNdkGdb.cmake somewhere inside ${CMAKE_MODULE_PATH}
# 2. inside your project add
#
# include(AndroidNdkGdb)
# android_ndk_gdb_enable()
# # for each target
# add_library(MyLibrary ...)
# android_ndk_gdb_debuggable(MyLibrary)
# add gdbserver and general gdb configuration to project
# also create a mininal NDK skeleton so ndk-gdb finds the paths
#
# the optional parameter defines the path to the android project.
# uses PROJECT_SOURCE_DIR by default.
macro(android_ndk_gdb_enable)
if(ANDROID)
# create custom target that depends on the real target so it gets executed afterwards
add_custom_target(NDK_GDB ALL)
if(${ARGC})
set(ANDROID_PROJECT_DIR ${ARGV0})
else()
set(ANDROID_PROJECT_DIR ${PROJECT_SOURCE_DIR})
endif()
set(NDK_GDB_SOLIB_PATH ${ANDROID_PROJECT_DIR}/obj/local/${ANDROID_NDK_ABI_NAME}/)
file(MAKE_DIRECTORY ${NDK_GDB_SOLIB_PATH})
# 1. generate essential Android Makefiles
file(MAKE_DIRECTORY ${ANDROID_PROJECT_DIR}/jni)
if(NOT EXISTS ${ANDROID_PROJECT_DIR}/jni/Android.mk)
file(WRITE ${ANDROID_PROJECT_DIR}/jni/Android.mk "APP_ABI := ${ANDROID_NDK_ABI_NAME}\n")
endif()
if(NOT EXISTS ${ANDROID_PROJECT_DIR}/jni/Application.mk)
file(WRITE ${ANDROID_PROJECT_DIR}/jni/Application.mk "APP_ABI := ${ANDROID_NDK_ABI_NAME}\n")
endif()
# 2. generate gdb.setup
get_directory_property(PROJECT_INCLUDES DIRECTORY ${PROJECT_SOURCE_DIR} INCLUDE_DIRECTORIES)
string(REGEX REPLACE ";" " " PROJECT_INCLUDES "${PROJECT_INCLUDES}")
file(WRITE ${LIBRARY_OUTPUT_PATH}/gdb.setup "set solib-search-path ${NDK_GDB_SOLIB_PATH}\n")
file(APPEND ${LIBRARY_OUTPUT_PATH}/gdb.setup "directory ${PROJECT_INCLUDES}\n")
# 3. copy gdbserver executable
file(COPY ${ANDROID_NDK}/prebuilt/android-${ANDROID_ARCH_NAME}/gdbserver/gdbserver DESTINATION ${LIBRARY_OUTPUT_PATH})
endif()
endmacro()
# register a target for remote debugging
# copies the debug version to NDK_GDB_SOLIB_PATH then strips symbols of original
macro(android_ndk_gdb_debuggable TARGET_NAME)
if(ANDROID)
get_property(TARGET_LOCATION TARGET ${TARGET_NAME} PROPERTY LOCATION)
# create custom target that depends on the real target so it gets executed afterwards
add_dependencies(NDK_GDB ${TARGET_NAME})
# 4. copy lib to obj
add_custom_command(TARGET NDK_GDB POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${TARGET_LOCATION} ${NDK_GDB_SOLIB_PATH})
# 5. strip symbols
add_custom_command(TARGET NDK_GDB POST_BUILD COMMAND ${CMAKE_STRIP} ${TARGET_LOCATION})
endif()
endmacro()
# Copyright (c) 2014, Pavel Rojtberg
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
macro(android_ndk_import_module_cpufeatures)
if(ANDROID)
include_directories(${ANDROID_NDK}/sources/android/cpufeatures)
add_library(cpufeatures ${ANDROID_NDK}/sources/android/cpufeatures/cpu-features.c)
target_link_libraries(cpufeatures dl)
endif()
endmacro()
macro(android_ndk_import_module_native_app_glue)
if(ANDROID)
include_directories(${ANDROID_NDK}/sources/android/native_app_glue)
add_library(native_app_glue ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)
target_link_libraries(native_app_glue log)
endif()
endmacro()
macro(android_ndk_import_module_ndk_helper)
if(ANDROID)
android_ndk_import_module_cpufeatures()
android_ndk_import_module_native_app_glue()
include_directories(${ANDROID_NDK}/sources/android/ndk_helper)
file(GLOB _NDK_HELPER_SRCS ${ANDROID_NDK}/sources/android/ndk_helper/*.cpp ${ANDROID_NDK}/sources/android/ndk_helper/gl3stub.c)
add_library(ndk_helper ${_NDK_HELPER_SRCS})
target_link_libraries(ndk_helper log android EGL GLESv2 cpufeatures native_app_glue)
unset(_NDK_HELPER_SRCS)
endif()
endmacro()
\ No newline at end of file
find_path(AVCODEC_INCLUDE_DIR
NAMES libavcodec/avcodec.h
PATHS $ENV{HOME}/ffmpeg/include)
find_library(AVCODEC_LIBRARY
NAMES avcodec
PATHS $ENV{HOME}/ffmpeg/lib)
set(AVCODEC_LIBRARIES ${AVCODEC_LIBRARY})
set(AVCODEC_INCLUDE_DIRS ${AVCODEC_INCLUDE_DIR})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(AVCODEC DEFAULT_MSG AVCODEC_LIBRARY AVCODEC_INCLUDE_DIR)
find_path(AVUTIL_INCLUDE_DIR
NAMES libavutil/avutil.h
PATHS $ENV{HOME}/ffmpeg/include)
find_library(AVUTIL_LIBRARY
NAMES avutil
PATHS $ENV{HOME}/ffmpeg/lib)
set(AVUTIL_LIBRARIES ${AVUTIL_LIBRARY})
set(AVUTIL_INCLUDE_DIRS ${AVUTIL_INCLUDE_DIR})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(AVUTIL DEFAULT_MSG AVUTIL_LIBRARY AVUTIL_INCLUDE_DIR)
find_path(FAAC_INCLUDE_DIR
NAMES faac.h
)
find_library(FAAC_LIBRARY
NAMES faac
)
set(FAAC_INCLUDE_DIRS ${FAAC_INCLUDE_DIR})
set(FAAC_LIBRARIES ${FAAC_LIBRARY})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(FAAC DEFAULT_MSG FAAC_LIBRARY FAAC_INCLUDE_DIR)
find_path(MP4V2_INCLUDE_DIR
NAMES mp4v2/mp4v2.h)
find_library(MP4V2_LIBRARY
NAMES mp4v2)
set(MP4V2_LIBRARIES ${MP4V2_LIBRARY})
set(MP4V2_INCLUDE_DIRS ${MP4V2_INCLUDE_DIR})
include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set MP4V2_FOUND to TRUE
# if all listed variables are TRUE
find_package_handle_standard_args(MP4V2 DEFAULT_MSG MP4V2_LIBRARY MP4V2_INCLUDE_DIR)
# - Try to find MySQL / MySQL Embedded library
# Find the MySQL includes and client library
# This module defines
# MYSQL_INCLUDE_DIR, where to find mysql.h
# MYSQL_LIBRARIES, the libraries needed to use MySQL.
# MYSQL_LIB_DIR, path to the MYSQL_LIBRARIES
# MYSQL_EMBEDDED_LIBRARIES, the libraries needed to use MySQL Embedded.
# MYSQL_EMBEDDED_LIB_DIR, path to the MYSQL_EMBEDDED_LIBRARIES
# MYSQL_FOUND, If false, do not try to use MySQL.
# MYSQL_EMBEDDED_FOUND, If false, do not try to use MySQL Embedded.
# Copyright (c) 2006-2008, Jarosław Staniek <staniek@kde.org>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
include(CheckCXXSourceCompiles)
if(WIN32)
find_path(MYSQL_INCLUDE_DIR mysql.h
PATHS
$ENV{MYSQL_INCLUDE_DIR}
$ENV{MYSQL_DIR}/include
$ENV{ProgramFiles}/MySQL/*/include
$ENV{SystemDrive}/MySQL/*/include
$ENV{ProgramW6432}/MySQL/*/include
)
else(WIN32)
find_path(MYSQL_INCLUDE_DIR mysql/mysql.h
PATHS
$ENV{MYSQL_INCLUDE_DIR}
$ENV{MYSQL_DIR}/include
/usr/local/mysql/include
/opt/mysql/mysql/include
PATH_SUFFIXES
mysql
)
endif(WIN32)
if(WIN32)
if (${CMAKE_BUILD_TYPE})
string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_TOLOWER)
endif()
# path suffix for debug/release mode
# binary_dist: mysql binary distribution
# build_dist: custom build
if(CMAKE_BUILD_TYPE_TOLOWER MATCHES "debug")
set(binary_dist debug)
set(build_dist Debug)
else(CMAKE_BUILD_TYPE_TOLOWER MATCHES "debug")
ADD_DEFINITIONS(-DDBUG_OFF)
set(binary_dist opt)
set(build_dist Release)
endif(CMAKE_BUILD_TYPE_TOLOWER MATCHES "debug")
# find_library(MYSQL_LIBRARIES NAMES mysqlclient
set(MYSQL_LIB_PATHS
$ENV{MYSQL_DIR}/lib/${binary_dist}
$ENV{MYSQL_DIR}/libmysql/${build_dist}
$ENV{MYSQL_DIR}/client/${build_dist}
$ENV{ProgramFiles}/MySQL/*/lib/${binary_dist}
$ENV{SystemDrive}/MySQL/*/lib/${binary_dist}
$ENV{MYSQL_DIR}/lib/opt
$ENV{MYSQL_DIR}/client/release
$ENV{ProgramFiles}/MySQL/*/lib/opt
$ENV{ProgramFiles}/MySQL/*/lib/
$ENV{SystemDrive}/MySQL/*/lib/opt
$ENV{ProgramW6432}/MySQL/*/lib
)
find_library(MYSQL_LIBRARIES NAMES libmysql
PATHS
${MYSQL_LIB_PATHS}
)
else(WIN32)
# find_library(MYSQL_LIBRARIES NAMES mysqlclient
set(MYSQL_LIB_PATHS
$ENV{MYSQL_DIR}/libmysql_r/.libs
$ENV{MYSQL_DIR}/lib
$ENV{MYSQL_DIR}/lib/mysql
/usr/local/mysql/lib
/opt/mysql/mysql/lib
$ENV{MYSQL_DIR}/libmysql_r/.libs
$ENV{MYSQL_DIR}/lib
$ENV{MYSQL_DIR}/lib/mysql
/usr/local/mysql/lib
/opt/mysql/mysql/lib
PATH_SUFFIXES
mysql
)
find_library(MYSQL_LIBRARIES NAMES mysqlclient
PATHS
${MYSQL_LIB_PATHS}
)
endif(WIN32)
find_library(MYSQL_EMBEDDED_LIBRARIES NAMES mysqld
PATHS
${MYSQL_LIB_PATHS}
)
if(MYSQL_LIBRARIES)
get_filename_component(MYSQL_LIB_DIR ${MYSQL_LIBRARIES} PATH)
endif(MYSQL_LIBRARIES)
if(MYSQL_EMBEDDED_LIBRARIES)
get_filename_component(MYSQL_EMBEDDED_LIB_DIR ${MYSQL_EMBEDDED_LIBRARIES} PATH)
endif(MYSQL_EMBEDDED_LIBRARIES)
set( CMAKE_REQUIRED_INCLUDES ${MYSQL_INCLUDE_DIR} )
set( CMAKE_REQUIRED_LIBRARIES ${MYSQL_EMBEDDED_LIBRARIES} )
check_cxx_source_compiles( "#include <mysql.h>\nint main() { int i = MYSQL_OPT_USE_EMBEDDED_CONNECTION; }" HAVE_MYSQL_OPT_EMBEDDED_CONNECTION )
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.")
endif(MYSQL_INCLUDE_DIR AND MYSQL_LIBRARIES)
if(MYSQL_INCLUDE_DIR AND MYSQL_EMBEDDED_LIBRARIES AND HAVE_MYSQL_OPT_EMBEDDED_CONNECTION)
set(MYSQL_EMBEDDED_FOUND TRUE)
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)
find_path(SDL2_INCLUDE_DIR
NAMES SDL2/SDL.h
HINTS SDL2
PATHS $ENV{HOME}/sdl2/include)
find_library(SDL2_LIBRARY
NAMES SDL2
PATHS $ENV{HOME}/sdl2/lib/x86)
set(SDL2_LIBRARIES ${SDL2_LIBRARY})
set(SDL2_INCLUDE_DIRS ${SDL2_INCLUDE_DIR})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(SDL2 DEFAULT_MSG SDL2_LIBRARY SDL2_INCLUDE_DIR)
\ No newline at end of file
############################################################################
# FindX264.txt
# Copyright (C) 2015 Belledonne Communications, Grenoble France
#
############################################################################
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
############################################################################
#
# - Find the x264 include file and library
#
# X264_FOUND - system has x264
# X264_INCLUDE_DIRS - the x264 include directory
# X264_LIBRARIES - The libraries needed to use x264
include(CMakePushCheckState)
include(CheckCXXSymbolExists)
set(_X264_ROOT_PATHS
${CMAKE_INSTALL_PREFIX}
)
find_path(X264_INCLUDE_DIRS
NAMES x264.h
HINTS _X264_ROOT_PATHS
PATH_SUFFIXES include
)
if(X264_INCLUDE_DIRS)
set(HAVE_X264_H 1)
endif()
find_library(X264_LIBRARIES
NAMES x264
HINTS _X264_ROOT_PATHS
PATH_SUFFIXES bin lib
)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(X264
DEFAULT_MSG
X264_INCLUDE_DIRS X264_LIBRARIES HAVE_X264_H
)
mark_as_advanced(X264_INCLUDE_DIRS X264_LIBRARIES HAVE_X264_H)
\ No newline at end of file
find_path(ZLMEDIAKIT_INCLUDE_DIR
NAMES Rtsp/Rtsp.h
PATHS
${PROJECT_SOURCE_DIR}/../ZLMediaKit/src
$ENV{HOME}/ZLMediaKit/include)
find_library(ZLMEDIAKIT_LIBRARY
NAMES ZLMediaKit
PATHS
${PROJECT_SOURCE_DIR}/../ZLMediaKit/build/lib
$ENV{HOME}/ZLMediaKit/lib)
set(ZLMEDIAKIT_LIBRARIES ${ZLMEDIAKIT_LIBRARY})
set(ZLMEDIAKIT_INCLUDE_DIRS ${ZLMEDIAKIT_INCLUDE_DIR})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(ZLMEDIAKIT DEFAULT_MSG ZLMEDIAKIT_LIBRARY ZLMEDIAKIT_INCLUDE_DIR)
find_path(ZLTOOLKIT_INCLUDE_DIR
NAMES Network/Socket.h
PATHS
${PROJECT_SOURCE_DIR}/../ZLToolKit/src
$ENV{HOME}/ZLToolKit/include)
find_library(ZLTOOLKIT_LIBRARY
NAMES ZLToolKit
PATHS
${PROJECT_SOURCE_DIR}/../ZLToolKit/build/lib
$ENV{HOME}/ZLToolKit/lib)
set(ZLTOOLKIT_LIBRARIES ${ZLTOOLKIT_LIBRARY})
set(ZLTOOLKIT_INCLUDE_DIRS ${ZLTOOLKIT_INCLUDE_DIR})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(ZLTOOLKIT DEFAULT_MSG ZLTOOLKIT_LIBRARY ZLTOOLKIT_INCLUDE_DIR)
# This file is based off of the Platform/Darwin.cmake and Platform/UnixPaths.cmake
# files which are included with CMake 2.8.4
# It has been altered for iOS development
# Options:
#
# IOS_PLATFORM = OS (default) or SIMULATOR or SIMULATOR64
# This decides if SDKS will be selected from the iPhoneOS.platform or iPhoneSimulator.platform folders
# OS - the default, used to build for iPhone and iPad physical devices, which have an arm arch.
# SIMULATOR - used to build for the Simulator platforms, which have an x86 arch.
#
# CMAKE_IOS_DEVELOPER_ROOT = automatic(default) or /path/to/platform/Developer folder
# By default this location is automatcially chosen based on the IOS_PLATFORM value above.
# If set manually, it will override the default location and force the user of a particular Developer Platform
#
# CMAKE_IOS_SDK_ROOT = automatic(default) or /path/to/platform/Developer/SDKs/SDK folder
# By default this location is automatcially chosen based on the CMAKE_IOS_DEVELOPER_ROOT value.
# In this case it will always be the most up-to-date SDK found in the CMAKE_IOS_DEVELOPER_ROOT path.
# If set manually, this will force the use of a specific SDK version
# Macros:
#
# set_xcode_property (TARGET XCODE_PROPERTY XCODE_VALUE)
# A convenience macro for setting xcode specific properties on targets
# example: set_xcode_property (myioslib IPHONEOS_DEPLOYMENT_TARGET "3.1")
#
# find_host_package (PROGRAM ARGS)
# A macro used to find executable programs on the host system, not within the iOS environment.
# Thanks to the android-cmake project for providing the command
# Standard settings
set (CMAKE_SYSTEM_NAME Darwin)
set (CMAKE_SYSTEM_VERSION 1)
set (UNIX True)
set (APPLE True)
set (IOS True)
# Required as of cmake 2.8.10
set (CMAKE_OSX_DEPLOYMENT_TARGET "" CACHE STRING "Force unset of the deployment target for iOS" FORCE)
# Determine the cmake host system version so we know where to find the iOS SDKs
find_program (CMAKE_UNAME uname /bin /usr/bin /usr/local/bin)
if (CMAKE_UNAME)
exec_program(uname ARGS -r OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_VERSION)
string (REGEX REPLACE "^([0-9]+)\\.([0-9]+).*$" "\\1" DARWIN_MAJOR_VERSION "${CMAKE_HOST_SYSTEM_VERSION}")
endif (CMAKE_UNAME)
# Force the compilers to gcc for iOS
include (CMakeForceCompiler)
#CMAKE_FORCE_C_COMPILER (/usr/bin/gcc Apple)
set(CMAKE_C_COMPILER /usr/bin/clang)
#CMAKE_FORCE_CXX_COMPILER (/usr/bin/g++ Apple)
set(CMAKE_CXX_COMPILER /usr/bin/clang++)
set(CMAKE_AR ar CACHE FILEPATH "" FORCE)
# Skip the platform compiler checks for cross compiling
set (CMAKE_CXX_COMPILER_WORKS TRUE)
set (CMAKE_C_COMPILER_WORKS TRUE)
# All iOS/Darwin specific settings - some may be redundant
set (CMAKE_SHARED_LIBRARY_PREFIX "lib")
set (CMAKE_SHARED_LIBRARY_SUFFIX ".dylib")
set (CMAKE_SHARED_MODULE_PREFIX "lib")
set (CMAKE_SHARED_MODULE_SUFFIX ".so")
set (CMAKE_MODULE_EXISTS 1)
set (CMAKE_DL_LIBS "")
set (CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG "-compatibility_version ")
set (CMAKE_C_OSX_CURRENT_VERSION_FLAG "-current_version ")
set (CMAKE_CXX_OSX_COMPATIBILITY_VERSION_FLAG "${CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG}")
set (CMAKE_CXX_OSX_CURRENT_VERSION_FLAG "${CMAKE_C_OSX_CURRENT_VERSION_FLAG}")
# Hidden visibilty is required for cxx on iOS
set (CMAKE_C_FLAGS_INIT "")
set (CMAKE_CXX_FLAGS_INIT "-fvisibility=hidden -fvisibility-inlines-hidden")
set (CMAKE_C_LINK_FLAGS "-Wl,-search_paths_first ${CMAKE_C_LINK_FLAGS}")
set (CMAKE_CXX_LINK_FLAGS "-Wl,-search_paths_first ${CMAKE_CXX_LINK_FLAGS}")
set (CMAKE_PLATFORM_HAS_INSTALLNAME 1)
set (CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-dynamiclib -headerpad_max_install_names")
set (CMAKE_SHARED_MODULE_CREATE_C_FLAGS "-bundle -headerpad_max_install_names")
set (CMAKE_SHARED_MODULE_LOADER_C_FLAG "-Wl,-bundle_loader,")
set (CMAKE_SHARED_MODULE_LOADER_CXX_FLAG "-Wl,-bundle_loader,")
set (CMAKE_FIND_LIBRARY_SUFFIXES ".dylib" ".so" ".a")
# hack: if a new cmake (which uses CMAKE_INSTALL_NAME_TOOL) runs on an old build tree
# (where install_name_tool was hardcoded) and where CMAKE_INSTALL_NAME_TOOL isn't in the cache
# and still cmake didn't fail in CMakeFindBinUtils.cmake (because it isn't rerun)
# hardcode CMAKE_INSTALL_NAME_TOOL here to install_name_tool, so it behaves as it did before, Alex
if (NOT DEFINED CMAKE_INSTALL_NAME_TOOL)
find_program(CMAKE_INSTALL_NAME_TOOL install_name_tool)
endif (NOT DEFINED CMAKE_INSTALL_NAME_TOOL)
# Setup iOS platform unless specified manually with IOS_PLATFORM
if (NOT DEFINED IOS_PLATFORM)
set (IOS_PLATFORM "OS")
endif (NOT DEFINED IOS_PLATFORM)
set (IOS_PLATFORM ${IOS_PLATFORM} CACHE STRING "Type of iOS Platform")
# Setup building for arm64 or not
if (NOT DEFINED BUILD_ARM64)
set (BUILD_ARM64 true)
endif (NOT DEFINED BUILD_ARM64)
set (BUILD_ARM64 ${BUILD_ARM64} CACHE STRING "Build arm64 arch or not")
# Check the platform selection and setup for developer root
if (${IOS_PLATFORM} STREQUAL "OS")
set (IOS_PLATFORM_LOCATION "iPhoneOS.platform")
# This causes the installers to properly locate the output libraries
set (CMAKE_XCODE_EFFECTIVE_PLATFORMS "-iphoneos")
elseif (${IOS_PLATFORM} STREQUAL "SIMULATOR")
set (SIMULATOR true)
set (IOS_PLATFORM_LOCATION "iPhoneSimulator.platform")
# This causes the installers to properly locate the output libraries
set (CMAKE_XCODE_EFFECTIVE_PLATFORMS "-iphonesimulator")
elseif (${IOS_PLATFORM} STREQUAL "SIMULATOR64")
set (SIMULATOR true)
set (IOS_PLATFORM_LOCATION "iPhoneSimulator.platform")
# This causes the installers to properly locate the output libraries
set (CMAKE_XCODE_EFFECTIVE_PLATFORMS "-iphonesimulator")
else (${IOS_PLATFORM} STREQUAL "OS")
message (FATAL_ERROR "Unsupported IOS_PLATFORM value selected. Please choose OS or SIMULATOR")
endif (${IOS_PLATFORM} STREQUAL "OS")
# Setup iOS developer location unless specified manually with CMAKE_IOS_DEVELOPER_ROOT
# Note Xcode 4.3 changed the installation location, choose the most recent one available
exec_program(/usr/bin/xcode-select ARGS -print-path OUTPUT_VARIABLE CMAKE_XCODE_DEVELOPER_DIR)
set (XCODE_POST_43_ROOT "${CMAKE_XCODE_DEVELOPER_DIR}/Platforms/${IOS_PLATFORM_LOCATION}/Developer")
set (XCODE_PRE_43_ROOT "/Developer/Platforms/${IOS_PLATFORM_LOCATION}/Developer")
if (NOT DEFINED CMAKE_IOS_DEVELOPER_ROOT)
if (EXISTS ${XCODE_POST_43_ROOT})
set (CMAKE_IOS_DEVELOPER_ROOT ${XCODE_POST_43_ROOT})
elseif(EXISTS ${XCODE_PRE_43_ROOT})
set (CMAKE_IOS_DEVELOPER_ROOT ${XCODE_PRE_43_ROOT})
endif (EXISTS ${XCODE_POST_43_ROOT})
endif (NOT DEFINED CMAKE_IOS_DEVELOPER_ROOT)
set (CMAKE_IOS_DEVELOPER_ROOT ${CMAKE_IOS_DEVELOPER_ROOT} CACHE PATH "Location of iOS Platform")
# Find and use the most recent iOS sdk unless specified manually with CMAKE_IOS_SDK_ROOT
if (NOT DEFINED CMAKE_IOS_SDK_ROOT)
file (GLOB _CMAKE_IOS_SDKS "${CMAKE_IOS_DEVELOPER_ROOT}/SDKs/*")
if (_CMAKE_IOS_SDKS)
list (SORT _CMAKE_IOS_SDKS)
list (REVERSE _CMAKE_IOS_SDKS)
list (GET _CMAKE_IOS_SDKS 0 CMAKE_IOS_SDK_ROOT)
else (_CMAKE_IOS_SDKS)
message (FATAL_ERROR "No iOS SDK's found in default search path ${CMAKE_IOS_DEVELOPER_ROOT}. Manually set CMAKE_IOS_SDK_ROOT or install the iOS SDK.")
endif (_CMAKE_IOS_SDKS)
message (STATUS "Toolchain using default iOS SDK: ${CMAKE_IOS_SDK_ROOT}")
endif (NOT DEFINED CMAKE_IOS_SDK_ROOT)
set (CMAKE_IOS_SDK_ROOT ${CMAKE_IOS_SDK_ROOT} CACHE PATH "Location of the selected iOS SDK")
# Set the sysroot default to the most recent SDK
set (CMAKE_OSX_SYSROOT ${CMAKE_IOS_SDK_ROOT} CACHE PATH "Sysroot used for iOS support")
# set the architecture for iOS
if (${IOS_PLATFORM} STREQUAL "OS")
set (IOS_ARCH armv7 armv7s arm64)
elseif (${IOS_PLATFORM} STREQUAL "SIMULATOR")
set (IOS_ARCH i386)
elseif (${IOS_PLATFORM} STREQUAL "SIMULATOR64")
set (IOS_ARCH x86_64)
endif (${IOS_PLATFORM} STREQUAL "OS")
set (CMAKE_OSX_ARCHITECTURES ${IOS_ARCH} CACHE string "Build architecture for iOS")
# Set the find root to the iOS developer roots and to user defined paths
set (CMAKE_FIND_ROOT_PATH ${CMAKE_IOS_DEVELOPER_ROOT} ${CMAKE_IOS_SDK_ROOT} ${CMAKE_PREFIX_PATH} CACHE string "iOS find search path root")
# default to searching for frameworks first
set (CMAKE_FIND_FRAMEWORK FIRST)
# set up the default search directories for frameworks
set (CMAKE_SYSTEM_FRAMEWORK_PATH
${CMAKE_IOS_SDK_ROOT}/System/Library/Frameworks
${CMAKE_IOS_SDK_ROOT}/System/Library/PrivateFrameworks
${CMAKE_IOS_SDK_ROOT}/Developer/Library/Frameworks
)
# only search the iOS sdks, not the remainder of the host filesystem
set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY)
set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
# This little macro lets you set any XCode specific property
macro (set_xcode_property TARGET XCODE_PROPERTY XCODE_VALUE)
set_property (TARGET ${TARGET} PROPERTY XCODE_ATTRIBUTE_${XCODE_PROPERTY} ${XCODE_VALUE})
endmacro (set_xcode_property)
# This macro lets you find executable programs on the host system
macro (find_host_package)
set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER)
set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER)
set (IOS FALSE)
find_package(${ARGN})
set (IOS TRUE)
set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY)
set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
endmacro (find_host_package)
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef SRC_CLEANER_H_
#define SRC_CLEANER_H_
#include <list>
#include <mutex>
#include <functional>
using namespace std;
class cleaner {
public:
cleaner(){}
virtual ~cleaner(){
lock_guard<recursive_mutex> lck(_mtx);
for(auto &fun : _cleanInvokerList){
fun();
}
_cleanInvokerList.clear();
}
static cleaner &Instance(){
static cleaner *instance(new cleaner);
return *instance;
}
static void Destory(){
delete &Instance();
}
template<typename FUN>
void push_front(FUN &&fun){
lock_guard<recursive_mutex> lck(_mtx);
_cleanInvokerList.push_front(fun);
}
template<typename FUN>
void push_back(FUN &&fun){
lock_guard<recursive_mutex> lck(_mtx);
_cleanInvokerList.push_back(fun);
}
private:
recursive_mutex _mtx;
list<function<void()> > _cleanInvokerList;
};
#endif /* SRC_CLEANER_H_ */
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "common.h"
#include <stdarg.h>
#include <unordered_map>
#include "Util/logger.h"
#include "Util/onceToken.h"
#include "Util/TimeTicker.h"
#include "Network/TcpServer.h"
#include "Poller/EventPoller.h"
#include "Rtsp/UDPServer.h"
#include "Rtsp/RtspSession.h"
#include "Rtmp/RtmpSession.h"
#include "Http/HttpSession.h"
#include "cleaner.h"
using namespace std;
using namespace toolkit;
static TcpServer::Ptr s_pRtspSrv;
static TcpServer::Ptr s_pRtmpSrv;
static TcpServer::Ptr s_pHttpSrv;
//////////////////////////environment init///////////////////////////
API_EXPORT void API_CALL onAppStart(){
static onceToken s_token([](){
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
cleaner::Instance().push_back([](){
s_pRtspSrv.reset();
s_pRtmpSrv.reset();
s_pHttpSrv.reset();
WorkThreadPool::Destory();
UDPServer::Destory();
AsyncTaskThread::Destory();
EventPoller::Destory();
DebugL << "clear common" << endl;
Logger::Destory();
});
},nullptr);
}
API_EXPORT void API_CALL onAppExit(){
cleaner::Destory();
}
API_EXPORT void API_CALL setGlobalOptionString(const char *key,const char *val){
if(mINI::Instance().find(key) == mINI::Instance().end()){
WarnL << "key:" << key << " not existed!";
}
mINI::Instance()[key] = val;
}
API_EXPORT unsigned short API_CALL initHttpServer(unsigned short port){
s_pHttpSrv.reset(new TcpServer());
try {
s_pHttpSrv->start<HttpSession>(port);
return s_pHttpSrv->getPort();
} catch (std::exception &ex) {
s_pHttpSrv.reset();
WarnL << ex.what();
return 0;
}
}
API_EXPORT unsigned short API_CALL initRtspServer(unsigned short port) {
s_pRtspSrv.reset(new TcpServer());
try {
s_pRtspSrv->start<RtspSession>(port);
return s_pRtspSrv->getPort();
} catch (std::exception &ex) {
s_pRtspSrv.reset();
WarnL << ex.what();
return 0;
}
}
API_EXPORT unsigned short API_CALL initRtmpServer(unsigned short port) {
s_pRtmpSrv.reset(new TcpServer());
try {
s_pRtmpSrv->start<RtmpSession>(port);
return s_pRtmpSrv->getPort();
} catch (std::exception &ex) {
s_pRtmpSrv.reset();
WarnL << ex.what();
return 0;
}
}
API_EXPORT void API_CALL log_printf(LogType level,const char* file, const char* function, int line,const char *fmt,...){
LogInfoMaker info((LogLevel)level,file,function,line);
va_list pArg;
va_start(pArg, fmt);
char buf[4096];
int n = vsprintf(buf, fmt, pArg);
buf[n] = '\0';
va_end(pArg);
info << buf;
}
API_EXPORT void API_CALL log_setLevel(LogType level){
Logger::Instance().setLevel((LogLevel)level);
}
class LogoutChannel: public LogChannel {
public:
LogoutChannel(const string &name, onLogOut cb, LogLevel level = LDebug)
:LogChannel(name, level){
_cb = cb;
}
virtual ~LogoutChannel(){}
void write(const LogInfoPtr &logInfo){
if (level() > logInfo->_level) {
return;
}
stringstream strStream;
logInfo->format(strStream, false);
auto strTmp = strStream.str();
if (_cb) {
_cb(strTmp.data(), strTmp.size());
}
}
private:
onLogOut _cb = nullptr;
};
API_EXPORT void API_CALL log_setOnLogOut(onLogOut cb){
std::shared_ptr<LogoutChannel> chn(new LogoutChannel("LogoutChannel",cb,LTrace));
Logger::Instance().add(chn);
}
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef SRC_COMMON_H_
#define SRC_COMMON_H_
#include <stdint.h>
#if defined(_WIN32)
#if defined(MediaKitWrapper_EXPORTS)
#define API_EXPORT __declspec(dllexport)
#else
#define API_EXPORT __declspec(dllimport)
#endif
#define API_CALL __cdecl
#else
#define API_EXPORT
#define API_CALL
#endif
#ifdef __cplusplus
extern "C" {
#endif
/////////////////////////environment init////////////////////////////////
API_EXPORT void API_CALL onAppStart();
API_EXPORT void API_CALL onAppExit();
API_EXPORT void API_CALL setGlobalOptionString(const char *key,const char *val);
/*
* 描述:创建Http服务器
* 参数:port:htt监听端口,推荐80,传入0则随机分配
* 返回值:0:失败,非0:端口号
*/
API_EXPORT unsigned short API_CALL initHttpServer(unsigned short port);
/*
* 描述:创建RTSP服务器
* 参数:port:rtsp监听端口,推荐554,传入0则随机分配
* 返回值:0:失败,非0:端口号
*/
API_EXPORT unsigned short API_CALL initRtspServer(unsigned short port);
/*
* 描述:创建RTMP服务器
* 参数:port:rtmp监听端口,推荐1935,传入0则随机分配
* 返回值:0:失败,非0:端口号
*/
API_EXPORT unsigned short API_CALL initRtmpServer(unsigned short port);
/////////////////////////日志////////////////////////////////
typedef enum {
//日志级别
LogTrace = 0, LogDebug, LogInfo, LogWarn, LogError, LogFatal,
} LogType;
typedef void(API_CALL *onLogOut)(const char *strLog, int iLogLen);
/*
* 描述:设置Log输出回调
* 参数:onLogOut:回调函数
* 返回值:无
*/
API_EXPORT void API_CALL log_setOnLogOut(onLogOut);
/*
* 描述:设在日志显示级别
* 参数:level:日志级别
* 返回值:无
*/
API_EXPORT void API_CALL log_setLevel(LogType level);
/*
* 描述:打印日志
* 参数:level:日志级别;file:文件名称,function:函数名称,line:所在行数,fmt:格式化字符串
* 返回值:无
*/
API_EXPORT void API_CALL log_printf(LogType level, const char* file, const char* function, int line, const char *fmt, ...);
#define log_trace(fmt,...) log_printf(LogTrace,__FILE__,__FUNCTION__,__LINE__,fmt,__VA_ARGS__)
#define log_debug(fmt,...) log_printf(LogDebug,__FILE__,__FUNCTION__,__LINE__,fmt,__VA_ARGS__)
#define log_info(fmt,...) log_printf(LogInfo,__FILE__,__FUNCTION__,__LINE__,fmt,__VA_ARGS__)
#define log_warn(fmt,...) log_printf(LogWarn,__FILE__,__FUNCTION__,__LINE__,fmt,__VA_ARGS__)
#define log_error(fmt,...) log_printf(LogError,__FILE__,__FUNCTION__,__LINE__,fmt,__VA_ARGS__)
#define log_fatal(fmt,...) log_printf(LogFatal,__FILE__,__FUNCTION__,__LINE__,fmt,__VA_ARGS__)
#ifdef __cplusplus
}
#endif
#endif /* SRC_COMMON_H_ */
//
// Created by xzl on 2018/8/31.
//
#include "flvrecorder.h"
#include "Rtmp/FlvMuxer.h"
using namespace toolkit;
API_EXPORT FlvRecorderContex API_CALL createFlvRecorder(){
DebugL;
FlvRecorder::Ptr *ret = new FlvRecorder::Ptr(new FlvRecorder);
return ret;
}
API_EXPORT void API_CALL releaseFlvRecorder(FlvRecorderContex ctx){
DebugL;
FlvRecorder::Ptr *record = (FlvRecorder::Ptr *)(ctx);
delete record;
}
API_EXPORT int API_CALL flvRecorder_start(FlvRecorderContex ctx,const char *appName,const char *streamName, const char *file_path){
DebugL << appName << " " << streamName << " " << file_path;
FlvRecorder::Ptr *record = (FlvRecorder::Ptr *)(ctx);
try {
(*record)->startRecord(DEFAULT_VHOST,appName,streamName,file_path);
return 0;
}catch (std::exception &ex){
WarnL << ex.what();
return -1;
}
}
\ No newline at end of file
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef MEDIAKITWRAPPER_FLVMUXER_H
#define MEDIAKITWRAPPER_FLVMUXER_H
#include "common.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef void* FlvRecorderContex;
/**
* 创建flv录制器
* @return
*/
API_EXPORT FlvRecorderContex API_CALL createFlvRecorder();
/**
* 释放flv录制器
* @param ctx
*/
API_EXPORT void API_CALL releaseFlvRecorder(FlvRecorderContex ctx);
/**
* 开始录制flv
* @param ctx flv录制器
* @param appName 绑定的RtmpMediaSource的 app名
* @param streamName 绑定的RtmpMediaSource的 stream名
* @param file_path 文件存放地址
* @return 0:开始超过,-1:失败,打开文件失败或该RtmpMediaSource不存在
*/
API_EXPORT int API_CALL flvRecorder_start(FlvRecorderContex ctx,const char *appName,const char *streamName, const char *file_path);
#ifdef __cplusplus
}
#endif
#endif /* MEDIAKITWRAPPER_FLVMUXER_H */
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "httpdownloader.h"
#include "Util/logger.h"
#include "Util/TimeTicker.h"
#include "Util/onceToken.h"
#include "Http/HttpDownloader.h"
#include "cleaner.h"
using namespace std;
using namespace toolkit;
static recursive_mutex s_mtxMapDownloader;
static unordered_map<void *, HttpDownloader::Ptr> s_mapDownloader;
static onceToken s_token([](){
cleaner::Instance().push_front([](){
lock_guard<recursive_mutex> lck(s_mtxMapDownloader);
s_mapDownloader.clear();
DebugL << "clear httpdownloader" << endl;
});
},nullptr);
API_EXPORT HttpDownloaderContex API_CALL createDownloader(){
HttpDownloader::Ptr ret(new HttpDownloader());
lock_guard<recursive_mutex> lck(s_mtxMapDownloader);
s_mapDownloader.emplace(ret.get(),ret);
return ret.get();
}
API_EXPORT void API_CALL downloader_startDownload(HttpDownloaderContex ctx,const char *url,downloader_onResult cb,void *userData){
downloader_startDownload_l(ctx,url,"",cb,userData);
}
API_EXPORT void API_CALL downloader_startDownload_l(HttpDownloaderContex ctx,const char *url,const char *file,downloader_onResult cb,void *userData){
HttpDownloader *ptr = (HttpDownloader *)ctx;
string urlTmp(url);
ptr->setOnResult([cb,userData,urlTmp](int code,const char *errMsg,const char *filePath){
if(cb){
InfoL << code << " " << errMsg << " " << filePath << " " << urlTmp;
cb(userData,code,errMsg,filePath);
}
});
ptr->startDownload(url,file,false);
}
API_EXPORT void API_CALL releaseDownloader(HttpDownloaderContex ctx){
lock_guard<recursive_mutex> lck(s_mtxMapDownloader);
s_mapDownloader.erase(ctx);
}
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef SRC_HTTPDOWNLOADER_H_
#define SRC_HTTPDOWNLOADER_H_
#include "common.h"
#ifdef __cplusplus
extern "C" {
#endif
////////////////////////////////////////Httpdownloader/////////////////////////////////////////////////
typedef void * HttpDownloaderContex;
typedef void(API_CALL *downloader_onResult)(void *userData,int code,const char *errMsg,const char *filePath);
API_EXPORT HttpDownloaderContex API_CALL createDownloader();
API_EXPORT void API_CALL downloader_startDownload(HttpDownloaderContex ctx,const char *url,downloader_onResult cb,void *userData);
API_EXPORT void API_CALL downloader_startDownload_l(HttpDownloaderContex ctx,const char *url,const char *file,downloader_onResult cb,void *userData);
API_EXPORT void API_CALL releaseDownloader(HttpDownloaderContex ctx);
#ifdef __cplusplus
}
#endif
#endif /* SRC_HTTPDOWNLOADER_H_ */
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "media.h"
#include "Util/logger.h"
#include "Util/TimeTicker.h"
#include "Util/onceToken.h"
#include "Device/Device.h"
#include "cleaner.h"
using namespace std;
using namespace toolkit;
static recursive_mutex s_mtxMapMedia;
static unordered_map<void *, DevChannel::Ptr> s_mapMedia;
static onceToken s_token([](){
cleaner::Instance().push_front([](){
lock_guard<recursive_mutex> lck(s_mtxMapMedia);
s_mapMedia.clear();
DebugL << "clear media" << endl;
});
},nullptr);
//////////////////////////Rtsp media///////////////////////////
API_EXPORT MediaContext API_CALL createMedia(const char *appName,const char *mediaName , float fDuration,int bEanbleHls, int bEnableMp4) {
DevChannel::Ptr ret(new DevChannel(DEFAULT_VHOST,appName,mediaName,fDuration,bEanbleHls,bEnableMp4));
lock_guard<recursive_mutex> lck(s_mtxMapMedia);
s_mapMedia.emplace((void *) (ret.get()), ret);
return ret.get();
}
API_EXPORT void API_CALL releaseMedia(MediaContext ctx) {
lock_guard<recursive_mutex> lck(s_mtxMapMedia);
s_mapMedia.erase(ctx);
}
API_EXPORT void API_CALL media_initVideo(MediaContext ctx, int width, int height, int frameRate) {
DevChannel *ptr = (DevChannel *) ctx;
VideoInfo info;
info.iFrameRate = frameRate;
info.iWidth = width;
info.iHeight = height;
ptr->initVideo(info);
}
API_EXPORT void API_CALL media_initAudio(MediaContext ctx, int channel, int sampleBit, int sampleRate,int profile) {
DevChannel *ptr = (DevChannel *) ctx;
AudioInfo info;
info.iSampleRate = sampleRate;
info.iChannel = channel;
info.iSampleBit = sampleBit;
info.iProfile = profile;
ptr->initAudio(info);
}
API_EXPORT void API_CALL media_inputH264(MediaContext ctx, void *data, int len, unsigned long stamp) {
//TimeTicker();
DevChannel *ptr = (DevChannel *) ctx;
ptr->inputH264((char *) data, len, stamp);
}
API_EXPORT void API_CALL media_inputAAC(MediaContext ctx, void *data, int len,unsigned long stamp,int withAdtsHeader) {
//TimeTicker();
DevChannel *ptr = (DevChannel *) ctx;
ptr->inputAAC((char *) data, len, stamp,withAdtsHeader);
}
API_EXPORT void API_CALL media_inputAAC1(MediaContext ctx, void *data, int len, unsigned long stamp,void *adts){
DevChannel *ptr = (DevChannel *) ctx;
ptr->inputAAC((char *) data, len, stamp,(char *)adts);
}
API_EXPORT void API_CALL media_inputAAC2(MediaContext ctx, void *data, int len, unsigned long stamp,void *aac_cfg){
DevChannel *ptr = (DevChannel *) ctx;
AdtsFrame frame;
makeAdtsHeader((char*)aac_cfg, frame);
char adts[8];
writeAdtsHeader(frame, (uint8_t*)adts);
ptr->inputAAC((char *) data, len, stamp,(char *)adts);
}
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef SRC_MEDIA_H_
#define SRC_MEDIA_H_
#include "common.h"
#ifdef __cplusplus
extern "C" {
#endif
////////////////////////////////Rtsp media////////////////////////////////////////////
typedef void* MediaContext;
/*
* 描述:创建一个媒体源
* 参数:mediaName:媒体名称,url地址的一部分,fDuration:文件长度(单位秒),直播输入0,bEanbleHls:是否启用hls,bEnableMp4:是否录制mp4
* 返回值:媒体源句柄
*/
API_EXPORT MediaContext API_CALL createMedia(const char *appName,const char *mediaName,float fDuration,int bEanbleHls, int bEnableMp4);
/*
* 描述:销毁媒体源
* 参数:ctx:媒体源句柄
* 返回值:无
*/
API_EXPORT void API_CALL releaseMedia(MediaContext ctx);
/*
* 描述:初始化媒体源的视频信息
* 参数:ctx:媒体源句柄;width:视频宽度;height:视频高度;frameRate:视频fps
* 返回值:无
*/
API_EXPORT void API_CALL media_initVideo(MediaContext ctx, int width, int height, int frameRate);
/*
* 描述:初始化媒体源的音频信息
* 参数:ctx:媒体源句柄;channel:声道数;sampleBit:音频采样位数,支持16bit;sampleRate:音频采样率;profile:aac编码profile,在不输入adts头时用于生产adts头
* 返回值:无
*/
API_EXPORT void API_CALL media_initAudio(MediaContext ctx, int channel, int sampleBit, int sampleRate , int profile);
/*
* 描述:输入单帧H264视频,需要输入SPS和PPS帧,帧起始字节00 00 01,00 00 00 01均可
* 参数:ctx:媒体源句柄;data:单帧H264数据;len:单帧H264数据字节数;stamp:时间戳,毫秒
* 返回值:无
*/
API_EXPORT void API_CALL media_inputH264(MediaContext ctx, void *data, int len, unsigned long stamp);
/*
* 描述:输入单帧AAC音频(有adts头)
* 参数:ctx:媒体源句柄;data:单帧AAC数据;len:单帧AAC数据字节数;stamp:时间戳,毫秒 ,withAdtsHeader:是否有adts头
* 返回值:无
*/
API_EXPORT void API_CALL media_inputAAC(MediaContext ctx, void *data, int len, unsigned long stamp,int withAdtsHeader);
/*
* 描述:输入单帧AAC音频(单独指定adts头)
* 参数:ctx:媒体源句柄;data:单帧AAC数据;len:单帧AAC数据字节数;stamp:时间戳,毫秒;adts:adts头指针
* 返回值:无
*/
API_EXPORT void API_CALL media_inputAAC1(MediaContext ctx, void *data, int len, unsigned long stamp,void *adts);
/*
* 描述:输入单帧AAC音频(指定2个字节的aac配置)
* 参数:ctx:媒体源句柄;data:单帧AAC数据;len:单帧AAC数据字节数;stamp:时间戳,毫秒;adts:adts头指针;aac_cfg:aac配置
* 返回值:无
*/
API_EXPORT void API_CALL media_inputAAC2(MediaContext ctx, void *data, int len, unsigned long stamp,void *aac_cfg);
#ifdef __cplusplus
}
#endif
#endif /* SRC_MEDIA_H_ */
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef SRC_RTSPAPI_H_
#define SRC_RTSPAPI_H_
#include "common.h"
#include "httpdownloader.h"
#include "media.h"
#include "player.h"
#include "proxyplayer.h"
#include "flvrecorder.h"
#endif /* SRC_RTSPAPI_H_ */
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "player.h"
#include "Util/logger.h"
#include "Util/TimeTicker.h"
#include "Util/onceToken.h"
#include "Thread/ThreadPool.h"
#include "Poller/EventPoller.h"
#include "Player/MediaPlayer.h"
#include "H264/H264Parser.h"
#include "cleaner.h"
using namespace std;
using namespace toolkit;
static recursive_mutex s_mtxMapPlayer;
static unordered_map<void *, MediaPlayer::Ptr> s_mapPlayer;
static onceToken s_token([](){
cleaner::Instance().push_front([](){
lock_guard<recursive_mutex> lck(s_mtxMapPlayer);
s_mapPlayer.clear();
DebugL << "clear player" << endl;
});
},nullptr);
////////////////////////rtsp player/////////////////////////////////////////
#define getPlayer(ctx) \
MediaPlayer::Ptr player;\
{\
lock_guard<recursive_mutex> lck(s_mtxMapPlayer);\
auto it = s_mapPlayer.find(ctx);\
if(it != s_mapPlayer.end()){\
player = it->second;\
}\
}
API_EXPORT PlayerContext API_CALL createPlayer() {
lock_guard<recursive_mutex> lck(s_mtxMapPlayer);
MediaPlayer::Ptr ret(new MediaPlayer());
s_mapPlayer.emplace(ret.get(), ret);
if(s_mapPlayer.size() > 16){
ErrorL << s_mapPlayer.size();
}
return ret.get();
}
API_EXPORT void API_CALL releasePlayer(PlayerContext ctx) {
getPlayer(ctx);
if (!player) {
return;
}
player_setOnGetAudio(ctx, nullptr, nullptr);
player_setOnGetVideo(ctx, nullptr, nullptr);
player_setOnPlayResult(ctx, nullptr, nullptr);
player_setOnShutdown(ctx, nullptr, nullptr);
lock_guard<recursive_mutex> lck(s_mtxMapPlayer);
s_mapPlayer.erase(ctx);
ASYNC_TRACE([player]() {
lock_guard<recursive_mutex> lck(s_mtxMapPlayer);
player->teardown();
});
}
API_EXPORT void API_CALL player_setOptionInt(PlayerContext ctx,const char* key,int val){
string keyTmp(key);
ASYNC_TRACE([ctx,keyTmp,val](){
getPlayer(ctx);
if (!player) {
return;
}
(*player)[keyTmp] = val;
});
}
API_EXPORT void API_CALL player_setOptionString(PlayerContext ctx,const char* key,const char *val){
string keyTmp(key);
string valTmp(val);
ASYNC_TRACE([ctx,keyTmp,valTmp](){
getPlayer(ctx);
if (!player) {
return;
}
(*player)[keyTmp] = valTmp;
});
}
API_EXPORT void API_CALL player_play(PlayerContext ctx, const char* url) {
string urlTmp(url);
ASYNC_TRACE([ctx,urlTmp](){
getPlayer(ctx);
if (!player) {
return;
}
player->play(urlTmp.data());
});
}
API_EXPORT void API_CALL player_pause(PlayerContext ctx, int pause) {
ASYNC_TRACE([ctx,pause](){
getPlayer(ctx);
if (!player) {
return;
}
player->pause(pause);
});
}
API_EXPORT void API_CALL player_seekTo(PlayerContext ctx, float fProgress) {
ASYNC_TRACE([ctx,fProgress]() {
getPlayer(ctx);
if (!player) {
return;
}
return player->seekTo(fProgress);
});
}
API_EXPORT void API_CALL player_setOnShutdown(PlayerContext ctx, player_onResult cb, void *userData) {
getPlayer(ctx);
if (!player) {
return;
}
if(cb){
SYNC_TRACE([&](){
player->setOnShutdown([cb,userData](const SockException &ex) {
cb(userData,ex.getErrCode(),ex.what());
});
});
}else{
SYNC_TRACE([&](){
player->setOnShutdown(nullptr);
});
}
}
API_EXPORT void API_CALL player_setOnPlayResult(PlayerContext ctx, player_onResult cb, void *userData) {
getPlayer(ctx);
if (!player) {
return;
}
if (cb) {
SYNC_TRACE([&](){
player->setOnPlayResult([cb,userData](const SockException &ex) {
cb(userData,ex.getErrCode(),ex.what());
});
});
} else {
SYNC_TRACE([&](){
player->setOnPlayResult(nullptr);
});
}
}
API_EXPORT void API_CALL player_setOnGetVideo(PlayerContext ctx, player_onGetH264 cb,
void *userData) {
getPlayer(ctx);
if (!player) {
return;
}
if (cb) {
std::shared_ptr<H264Parser> pParser(new H264Parser());
SYNC_TRACE([&](){
player->setOnVideoCB([cb,userData,pParser](const H264Frame &frame) {
pParser->inputH264(frame.data, frame.timeStamp);
cb(userData, (void *)frame.data.data(), frame.data.size(), frame.timeStamp,pParser->getPts());
});
});
} else {
SYNC_TRACE([&](){
player->setOnVideoCB(nullptr);
});
}
}
API_EXPORT void API_CALL player_setOnGetAudio(PlayerContext ctx, player_onGetAAC cb,
void *userData) {
getPlayer(ctx);
if (!player) {
return;
}
if (cb) {
SYNC_TRACE([&](){
player->setOnAudioCB([cb,userData](const AdtsFrame &frame) {
cb(userData, (void *)frame.data, frame.aac_frame_length, frame.timeStamp);
});
});
} else {
SYNC_TRACE([&](){
player->setOnAudioCB(nullptr);
});
}
}
API_EXPORT int API_CALL player_getVideoWidth(PlayerContext ctx) {
getPlayer(ctx);
if (!player) {
return -1;
}
return player->getVideoWidth();
}
API_EXPORT int API_CALL player_getVideoHeight(PlayerContext ctx) {
getPlayer(ctx);
if (!player) {
return -1;
}
return player->getVideoHeight();
}
API_EXPORT int API_CALL player_getVideoFps(PlayerContext ctx) {
getPlayer(ctx);
if (!player) {
return -1;
}
return player->getVideoFps();
}
API_EXPORT int API_CALL player_getAudioSampleRate(PlayerContext ctx) {
getPlayer(ctx);
if (!player) {
return -1;
}
return player->getAudioSampleRate();
}
API_EXPORT int API_CALL player_getAudioSampleBit(PlayerContext ctx) {
getPlayer(ctx);
if (!player) {
return -1;
}
return player->getAudioSampleBit();
}
API_EXPORT int API_CALL player_getAudioChannel(PlayerContext ctx) {
getPlayer(ctx);
if (!player) {
return -1;
}
return player->getAudioChannel();
}
API_EXPORT int API_CALL player_getH264PPS(PlayerContext ctx, char *buf, int bufsize) {
getPlayer(ctx);
if (!player) {
return -1;
}
if (bufsize < (int) player->getPps().size() || player->getPps().empty()) {
return -1;
}
memcpy(buf, player->getPps().data(), player->getPps().size());
return player->getPps().size();
}
API_EXPORT int API_CALL player_getH264SPS(PlayerContext ctx, char *buf, int bufsize) {
getPlayer(ctx);
if (!player) {
return -1;
}
if (bufsize < (int) player->getSps().size() || player->getSps().empty()) {
return -1;
}
memcpy(buf, player->getSps().data(), player->getSps().size());
return player->getSps().size();
}
API_EXPORT int API_CALL player_getAacCfg(PlayerContext ctx, char *buf, int bufsize) {
getPlayer(ctx);
if (!player) {
return -1;
}
if (bufsize < (int) player->getAudioCfg().size()) {
return -1;
}
memcpy(buf, player->getAudioCfg().data(), player->getAudioCfg().size());
return player->getAudioCfg().size();
}
API_EXPORT int API_CALL player_containAudio(PlayerContext ctx) {
getPlayer(ctx);
if (!player) {
return -1;
}
return player->containAudio();
}
API_EXPORT int API_CALL player_containVideo(PlayerContext ctx) {
getPlayer(ctx);
if (!player) {
return -1;
}
return player->containVideo();
}
API_EXPORT int API_CALL player_isInited(PlayerContext ctx) {
getPlayer(ctx);
if (!player) {
return -1;
}
return player->isInited();
}
API_EXPORT float API_CALL player_getDuration(PlayerContext ctx) {
getPlayer(ctx);
if (!player) {
return -1;
}
return player->getDuration();
}
API_EXPORT float API_CALL player_getProgress(PlayerContext ctx) {
getPlayer(ctx);
if (!player) {
return -1;
}
return player->getProgress();
}
API_EXPORT float API_CALL player_getLossRate(PlayerContext ctx, int trackId) {
getPlayer(ctx);
if (!player) {
return -1;
}
return player->getRtpLossRate(trackId);
}
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef SRC_PLAYER_H_
#define SRC_PLAYER_H_
#include "common.h"
#ifdef __cplusplus
extern "C" {
#endif
////////////////////////////////////////RTSP Player/////////////////////////////////////////////////
typedef void* PlayerContext;
typedef void(API_CALL *player_onResult)(void *userData,int errCode,const char *errMsg);
typedef void(API_CALL *player_onGetAAC)(void *userData,void *data,int len,unsigned long timeStamp);
typedef void(API_CALL *player_onGetH264)(void *userData,void *data,int len,unsigned long dts,unsigned long pts);
/*
* 描述:创建一个Rtsp播放器
* 参数:无
* 返回值:Rtsp播放器句柄
*/
API_EXPORT PlayerContext API_CALL createPlayer();
/*
* 描述:销毁一个播放器
* 参数:ctx:播放器句柄
* 返回值:无
*/
API_EXPORT void API_CALL releasePlayer(PlayerContext ctx);
/*
* 描述:设置播放器配置选项
* 参数: ctx:播放器句柄
* key:配置项键,例如 rtp_type
* val:值,例如 //设置rtp传输类型,可选项有0(tcp,默认)、1(udp)、2(组播)
* 返回值:无
*/
API_EXPORT void API_CALL player_setOptionInt(PlayerContext ctx,const char* key,int val);
API_EXPORT void API_CALL player_setOptionString(PlayerContext ctx,const char* key,const char *val);
/*
* 描述:播放rtsp链接(仅支持H264与AAC负载)
* 参数: ctx:播放器句柄
* url:rtsp链接
* 返回值:无
*/
API_EXPORT void API_CALL player_play(PlayerContext ctx,const char* url);
/*
* 描述:暂停播放RTSP
* 参数:ctx:播放器句柄;pause:1:暂停播放,0:恢复播放
* 返回值:无
*/
API_EXPORT void API_CALL player_pause(PlayerContext ctx,int pause);
/*
* 描述:设置播放器异常停止回调函数
* 参数:ctx:播放器句柄;cb:回调函数指针;userData:用户数据指针
* 返回值:无
*/
API_EXPORT void API_CALL player_setOnShutdown(PlayerContext ctx,player_onResult cb,void *userData);
/*
* 描述:设置播放器播放结果回调函数
* 参数:ctx:播放器句柄,cb:回调函数指针;userData:用户数据指针
* 返回值:无
*/
API_EXPORT void API_CALL player_setOnPlayResult(PlayerContext ctx,player_onResult cb,void *userData);
/*
* 描述:设置播放器收到视频帧回调,I帧前为SPS,PPS帧;每帧包含00 00 00 01的帧头
* 参数:ctx:播放器句柄,cb:回调函数指针;userData:用户数据指针
* 返回值:无
*/
API_EXPORT void API_CALL player_setOnGetVideo(PlayerContext ctx,player_onGetH264 cb,void *userData);
/*
* 描述:设置播放器收到音频帧回调,每帧数据包含ADTS头
* 参数:ctx:播放器句柄,cb:回调函数指针;userData:用户数据指针
* 返回值:无
*/
API_EXPORT void API_CALL player_setOnGetAudio(PlayerContext ctx,player_onGetAAC cb,void *userData);
/*
* 描述:获取视频宽度
* 参数:ctx:播放器句柄
* 返回值:视频宽度
*/
API_EXPORT int API_CALL player_getVideoWidth(PlayerContext ctx);
/*
* 描述:获取视频高度
* 参数:ctx:播放器句柄
* 返回值:视频高度
*/
API_EXPORT int API_CALL API_CALL player_getVideoHeight(PlayerContext ctx);
/*
* 描述:获取视频帧率
* 参数:ctx:播放器句柄
* 返回值:视频帧率
*/
API_EXPORT int API_CALL player_getVideoFps(PlayerContext ctx);
/*
* 描述:获取音频采样率
* 参数:ctx:播放器句柄
* 返回值:音频采样率
*/
API_EXPORT int API_CALL player_getAudioSampleRate(PlayerContext ctx);
/*
* 描述:获取音频采样位数(8bit或16bit)
* 参数:ctx:播放器句柄
* 返回值:音频采样位数
*/
API_EXPORT int API_CALL player_getAudioSampleBit(PlayerContext ctx);
/*
* 描述:获取音频通道数(单声道1,双声道2)
* 参数:ctx:播放器句柄
* 返回值:音频通道数
*/
API_EXPORT int API_CALL player_getAudioChannel(PlayerContext ctx);
/*
* 描述:获取H264的PPS帧
* 参数:ctx:播放器句柄,buf:存放PPS数据的缓存;bufsize:缓存大小
* 返回值:帧数据长度
*/
API_EXPORT int API_CALL player_getH264PPS(PlayerContext ctx,char *buf,int bufsize);
/*
* 描述:获取H264的SPS帧
* 参数:ctx:播放器句柄,buf:存放SPS数据的缓存;bufsize:缓存大小
* 返回值:帧数据长度
*/
API_EXPORT int API_CALL player_getH264SPS(PlayerContext ctx,char *buf,int bufsize);
/*
* 描述:获取AAC编码配置信息
* 参数:ctx:播放器句柄;buf:存放CFG数据的缓存;bufsize:缓存大小
* 返回值:CFG数据长度
*/
API_EXPORT int API_CALL player_getAacCfg(PlayerContext ctx,char *buf,int bufsize);
/*
* 描述:是否包含音频数据
* 参数:ctx:播放器句柄
* 返回值:1:包含,0:不包含
*/
API_EXPORT int API_CALL player_containAudio(PlayerContext ctx);
/*
* 描述:是否包含视频数据
* 参数:ctx:播放器句柄
* 返回值:1:包含,0:不包含
*/
API_EXPORT int API_CALL player_containVideo(PlayerContext ctx);
/*
* 描述:是否已经初始化完成(获取完整的播放信息)
* 参数:ctx:播放器句柄
* 返回值:1:初始化完成,0:未完成
*/
API_EXPORT int API_CALL player_isInited(PlayerContext ctx);
/*
* 描述:获取点播的时间长度,单位为秒(小于等于0,说明是直播,否则为点播)
* 参数:ctx:播放器句柄
* 返回值:点播的时间长度,单位秒
*/
API_EXPORT float API_CALL player_getDuration(PlayerContext ctx);
/*
* 描述:获取点播播放进度
* 参数:ctx:播放器句柄
* 返回值:点播播放进度,取值范围未 0.0~1.0
*/
API_EXPORT float API_CALL player_getProgress(PlayerContext ctx);
/*
* 描述:设置点播播放进度
* 参数:ctx:播放器句柄;fProgress:播放进度,取值范围未 0.0~1.0
* 返回值:无
*/
API_EXPORT void API_CALL player_seekTo(PlayerContext ctx, float fProgress);
/*
* 描述:获取丢包率
* 参数:ctx:播放器句柄;trackId:如果是-1,则返回总丢包率,否则返回视频或者音频的丢包率
* 返回值:丢包率
*/
API_EXPORT float API_CALL player_getLossRate(PlayerContext ctx,int trackId);
#ifdef __cplusplus
}
#endif
#endif /* SRC_PLAYER_H_ */
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "cleaner.h"
#include "proxyplayer.h"
#include "Device/PlayerProxy.h"
#include "Util/onceToken.h"
using namespace toolkit;
static recursive_mutex s_mtxMapProxyPlayer;
static unordered_map<void *, PlayerProxy::Ptr> s_mapProxyPlayer;
static onceToken s_token([](){
cleaner::Instance().push_front([](){
lock_guard<recursive_mutex> lck(s_mtxMapProxyPlayer);
s_mapProxyPlayer.clear();
DebugL << "clear proxyplayer" << endl;
});
},nullptr);
API_EXPORT ProxyPlayerContext API_CALL createProxyPlayer(const char *app,const char *stream,int rtp_type){
PlayerProxy::Ptr ret(new PlayerProxy(DEFAULT_VHOST,app,stream));
(*ret)[RtspPlayer::kRtpType] = rtp_type;
lock_guard<recursive_mutex> lck(s_mtxMapProxyPlayer);
s_mapProxyPlayer.emplace(ret.get(),ret);
return ret.get();
}
API_EXPORT void API_CALL releaseProxyPlayer(ProxyPlayerContext ctx){
lock_guard<recursive_mutex> lck(s_mtxMapProxyPlayer);
s_mapProxyPlayer.erase(ctx);
}
API_EXPORT void API_CALL proxyPlayer_play(ProxyPlayerContext ctx,const char *url){
PlayerProxy *ptr = (PlayerProxy *)ctx;
ptr->play(url);
}
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef SRC_PROXYPLAYER_H_
#define SRC_PROXYPLAYER_H_
#include "common.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef void* ProxyPlayerContext;
/*
* 描述:创建一个代理播放器
* 参数:app:应用名,生成url的一部分,rtsp://127.0.0.1/app/xxxx
* stream:媒体流名,生成url的一部分,rtsp://127.0.0.1/xxxx/stream
* rtp_type:如果播放的是rtsp连接则通过该参数配置设置rtp传输方式:RTP_TCP = 0, RTP_UDP = 1, RTP_MULTICAST = 2
* 返回值:代理播放器句柄
*/
API_EXPORT ProxyPlayerContext API_CALL createProxyPlayer(const char *app,const char *stream,int rtp_type);
/*
* 描述:销毁代理播放器
* 参数:ctx:代理播放器句柄
* 返回值:无
*/
API_EXPORT void API_CALL releaseProxyPlayer(ProxyPlayerContext ctx);
/*
* 描述:开始播放
* 参数:url:rtsp/rtmp连接
* 返回值:无
*/
API_EXPORT void API_CALL proxyPlayer_play(ProxyPlayerContext ctx,const char *url);
#ifdef __cplusplus
}
#endif
#endif /* SRC_PROXYPLAYER_H_ */
aux_source_directory(. TEST_SRC_LIST)
foreach(TEST_SRC ${TEST_SRC_LIST})
STRING(REGEX REPLACE "^\\./|\\.c[a-zA-Z0-9_]*$" "" TEST_EXE_NAME ${TEST_SRC})
message(STATUS "添加测试程序:${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()
endforeach(TEST_SRC ${TEST_SRC_LIST})
\ No newline at end of file
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "mediakit.h"
#include <unistd.h>
#include <signal.h>
void *media = nullptr;
int main(int argc,char *argv[]){
//设置退出信号处理函数
signal(SIGINT, [](int){});
////////init environment////////////////////////////////////////////////////////////////
onAppStart();
////////init service/////////////////////////////////////////////////////////////////////
initHttpServer(80);
initRtmpServer(1935);
initRtspServer(554);
////////ProxyPlayer////////////////////////////////////////////////////////////////////////
auto proxy = createProxyPlayer("app","proxy",0);
proxyPlayer_play(proxy,"rtmp://live.hkstv.hk.lxdns.com/live/hks");
log_warn("%s","请打开vlc播放:rtsp://127.0.0.1/app/proxy "
"rtmp://127.0.0.1/app/proxy "
"http://127.0.0.1/app/proxy/hls.m3u8");
////////http downloader////////////////////////////////////////////////////////////////////
auto downloader = createDownloader();
downloader_startDownload(downloader,
"http://pic4.nipic.com/20091121/3764872_215617048242_2.jpg",
[](void *userData,int code,const char *errMsg,const char *filePath){
log_info("下载结果:%d-%s:%s",code,errMsg,filePath);
}, downloader);
////////player//////////////////////////////////////////////////////////////////////////////
auto player = createPlayer();
player_setOnPlayResult(player,[](void *userData,int errCode,const char *errMsg){
log_info("播放结果:%d-%s",errCode,errMsg);
////////media/////////////////////////////////////////////////////////////////////////////////
if(errCode){
//play failed
return;
}
auto player = userData;
media = createMedia("app","media",0,0,0);
if(player_containAudio(player) == 1){
media_initAudio(media,
player_getAudioChannel(player),
player_getAudioSampleBit(player),
player_getAudioSampleRate(player),
0);
}
if(player_containVideo(player) == 1){
media_initVideo(media,
player_getVideoWidth(player),
player_getVideoHeight(player),
player_getVideoFps(player));
}
log_warn("%s","请打开vlc播放:rtsp://127.0.0.1/app/media "
"rtmp://127.0.0.1/app/media "
"http://127.0.0.1/app/media/hls.m3u8");
},player);
player_setOnShutdown(player,[](void *userData,int errCode,const char *errMsg){
log_info("播放器异常断开:%d-%s",errCode,errMsg);
if(media){
releaseMedia(media);
}
},player);
player_setOnGetAudio(player,[](void *userData,void *data,int len,unsigned long timeStamp){
//在此解码视频
//TO-DO
//log_trace("audio:%d-%d",len,timeStamp);
////////输入aac///////////
if(media){
media_inputAAC(media,data,len,timeStamp,1);
}
},player);
player_setOnGetVideo(player,[](void *userData,void *data,int len,unsigned long dts,unsigned long pts){
//在此解码音频
//TO-DO
//log_trace("video:%d-%d-%d",len,dts,pts);
////////输入264///////////
if(media){
media_inputH264(media,data,len,dts);
}
},player);
player_play(player,"rtmp://live.hkstv.hk.lxdns.com/live/hks");
//sleep forever
sleep(UINT32_MAX);
////////uninit environment////////////////////////////////////////////////////////////////////
onAppExit();
return 0;
}
......@@ -152,9 +152,10 @@ void MediaReader::startReadMP4() {
auto strongSelf = shared_from_this();
GET_CONFIG_AND_REGISTER(uint32_t,sampleMS,Record::kSampleMS);
AsyncTaskThread::Instance().DoTaskDelay(reinterpret_cast<uint64_t>(this), sampleMS, [strongSelf](){
_timer = std::make_shared<Timer>(sampleMS / 1000.0f,[strongSelf](){
return strongSelf->readSample(0,false);
});
}, nullptr);
//先读sampleMS毫秒的数据用于产生MediaSouce
readSample(sampleMS, false);
_mediaMuxer->setListener(strongSelf);
......@@ -164,7 +165,7 @@ void MediaReader::startReadMP4() {
return true;
}
bool MediaReader::close(){
AsyncTaskThread::Instance().CancelTask(reinterpret_cast<uint64_t>(this));
_timer.reset();
return true;
}
......@@ -292,7 +293,7 @@ void MediaReader::seek(uint32_t iSeekTime,bool bReStart){
_mediaMuxer->setTimeStamp(_iSeekTime);
if(bReStart){
AsyncTaskThread::Instance().CancelTask(reinterpret_cast<uint64_t>(this));
_timer.reset();
startReadMP4();
}
}
......
......@@ -93,6 +93,7 @@ private:
Ticker _ticker;
Ticker _alive;
recursive_mutex _mtx;
Timer::Ptr _timer;
#endif //ENABLE_MP4V2
};
......
......@@ -29,7 +29,6 @@
#include "Util/mini.h"
#include "Util/MD5.h"
#include "Util/logger.h"
#include "Thread/AsyncTaskThread.h"
using namespace toolkit;
......@@ -114,15 +113,13 @@ void PlayerProxy::play(const char* strUrl) {
}
PlayerProxy::~PlayerProxy() {
auto iTaskId = reinterpret_cast<uint64_t>(this);
AsyncTaskThread::Instance().CancelTask(iTaskId);
_timer.reset();
}
void PlayerProxy::rePlay(const string &strUrl,int iFailedCnt){
auto iTaskId = reinterpret_cast<uint64_t>(this);
auto iDelay = MAX(2 * 1000, MIN(iFailedCnt * 3000,60*1000));
weak_ptr<PlayerProxy> weakSelf = shared_from_this();
AsyncTaskThread::Instance().CancelTask(iTaskId);
AsyncTaskThread::Instance().DoTaskDelay(iTaskId, iDelay, [weakSelf,strUrl,iFailedCnt]() {
_timer = std::make_shared<Timer>(iDelay / 1000.0f,[weakSelf,strUrl,iFailedCnt]() {
//播放失败次数越多,则延时越长
auto strongPlayer = weakSelf.lock();
if(!strongPlayer) {
......@@ -131,7 +128,7 @@ void PlayerProxy::rePlay(const string &strUrl,int iFailedCnt){
WarnL << "重试播放[" << iFailedCnt << "]:" << strUrl;
strongPlayer->MediaPlayer::play(strUrl.data());
return false;
});
}, nullptr);
}
bool PlayerProxy::close() {
//通知其停止推流
......
......@@ -68,6 +68,7 @@ private:
string _strVhost;
string _strApp;
string _strSrc;
Timer::Ptr _timer;
};
} /* namespace mediakit */
......
......@@ -58,8 +58,9 @@ int main(int argc, char *argv[]) {
list<MediaPlayer::Ptr> playerList;
auto playerCnt = atoi(argv[1]);//启动的播放器个数
atomic_int alivePlayerCnt(0);
//每隔若干毫秒启动一个播放器(如果一次性全部启动,服务器和客户端可能都承受不了)
AsyncTaskThread::Instance().DoTaskDelay(0, atoi(argv[2]), [&]() {
Timer timer0(atoi(argv[2])/1000.0f,[&]() {
MediaPlayer::Ptr player(new MediaPlayer());
player->setOnPlayResult([&](const SockException &ex) {
if (!ex) {
......@@ -73,12 +74,13 @@ int main(int argc, char *argv[]) {
player->play(argv[3]);
playerList.push_back(player);
return playerCnt--;
});
}, nullptr);
AsyncTaskThread::Instance().DoTaskDelay(0, 1000, [&]() {
Timer timer1(1,[&]() {
InfoL << "存活播放器个数:" << alivePlayerCnt.load();
return true;
});
}, nullptr);
sem.wait();
return 0;
......
......@@ -66,18 +66,16 @@ void createPusher(const string &app, const string &stream, const string &url) {
pusher->publish(url.data());
}
Timer::Ptr g_timer;
//推流失败或断开延迟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]() {
g_timer = std::make_shared<Timer>(2,[app, stream, url]() {
InfoL << "Re-Publishing...";
//重新推流
createPusher(app, stream, url);
//此任务不重复
return false;
});
}, nullptr);
}
//这里才是真正执行main函数,你可以把函数名(domain)改成main,然后就可以输入自定义url了
......
......@@ -75,18 +75,16 @@ void createPusher(const string &app,const string &stream,const string &url){
pusher->publish(url.data());
}
Timer::Ptr g_timer;
//推流失败或断开延迟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]() {
void rePushDelay(const string &app, const string &stream, const string &url) {
g_timer = std::make_shared<Timer>(2,[app, stream, url]() {
InfoL << "Re-Publishing...";
//重新推流
createPusher(app,stream,url);
createPusher(app, stream, url);
//此任务不重复
return false;
});
}, nullptr);
}
//这里才是真正执行main函数,你可以把函数名(domain)改成main,然后就可以输入自定义url了
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论