├── .gitignore ├── .vscode ├── c_cpp_properties.json └── settings.json ├── CMakeLists.txt ├── README.md ├── buildroot ├── aarch64.toolchain.cmake ├── add_buildroot_project.md └── bedrock_encoder │ ├── Config.in │ └── bedrock_encoder.mk ├── control └── bedrock_control.c ├── encoder ├── CMakeLists.txt ├── Readme.md ├── bedrock_mpi_enc.cpp ├── inc │ ├── argparse.h │ ├── bedrock_enc.h │ ├── filecfg.h │ ├── loadbmp.h │ └── mpi_test_utils.h └── src │ ├── CMakeLists.txt │ ├── argparse.cpp │ ├── filecfg.cpp │ ├── loadbmp.cpp │ └── mpi_test_utils.cpp ├── inc ├── bedrock_control.h ├── bedrock_log.h └── bedrock_test.h ├── lib ├── libconfig │ ├── include │ │ ├── libconfig.h │ │ └── libconfig.h++ │ └── lib │ │ ├── libconfig++.la │ │ ├── libconfig++.so │ │ ├── libconfig++.so.9 │ │ ├── libconfig++.so.9.2.0 │ │ ├── libconfig.la │ │ ├── libconfig.so │ │ ├── libconfig.so.9 │ │ ├── libconfig.so.9.2.0 │ │ └── pkgconfig │ │ ├── libconfig++.pc │ │ └── libconfig.pc └── rockit │ ├── include │ ├── rk_comm_adec.h │ ├── rk_comm_aenc.h │ ├── rk_comm_aio.h │ ├── rk_comm_mb.h │ ├── rk_comm_rc.h │ ├── rk_comm_rgn.h │ ├── rk_comm_sys.h │ ├── rk_comm_tde.h │ ├── rk_comm_vdec.h │ ├── rk_comm_venc.h │ ├── rk_comm_vgs.h │ ├── rk_comm_vi.h │ ├── rk_comm_video.h │ ├── rk_comm_vo.h │ ├── rk_comm_vpss.h │ ├── rk_common.h │ ├── rk_debug.h │ ├── rk_defines.h │ ├── rk_errno.h │ ├── rk_mpi_adec.h │ ├── rk_mpi_aenc.h │ ├── rk_mpi_ai.h │ ├── rk_mpi_ao.h │ ├── rk_mpi_cal.h │ ├── rk_mpi_mb.h │ ├── rk_mpi_mmz.h │ ├── rk_mpi_rgn.h │ ├── rk_mpi_sys.h │ ├── rk_mpi_tde.h │ ├── rk_mpi_vdec.h │ ├── rk_mpi_venc.h │ ├── rk_mpi_vgs.h │ ├── rk_mpi_vi.h │ ├── rk_mpi_vo.h │ ├── rk_mpi_vpss.h │ └── rk_type.h │ └── lib │ └── librockit.so ├── main.cpp ├── rtsp ├── CMakeLists.txt ├── bedrock_rtsp.cpp ├── inc │ └── bedrock_rtsp.h └── src │ ├── 3rdpart │ └── md5 │ │ ├── COPYING │ │ └── md5.hpp │ ├── net │ ├── Acceptor.cpp │ ├── Acceptor.h │ ├── BufferReader.cpp │ ├── BufferReader.h │ ├── BufferWriter.cpp │ ├── BufferWriter.h │ ├── Channel.h │ ├── EpollTaskScheduler.cpp │ ├── EpollTaskScheduler.h │ ├── EventLoop.cpp │ ├── EventLoop.h │ ├── Logger.cpp │ ├── Logger.h │ ├── MemoryManager.cpp │ ├── MemoryManager.h │ ├── NetInterface.cpp │ ├── NetInterface.h │ ├── Pipe.cpp │ ├── Pipe.h │ ├── RingBuffer.h │ ├── SelectTaskScheduler.cpp │ ├── SelectTaskScheduler.h │ ├── Socket.h │ ├── SocketUtil.cpp │ ├── SocketUtil.h │ ├── TaskScheduler.cpp │ ├── TaskScheduler.h │ ├── TcpConnection.cpp │ ├── TcpConnection.h │ ├── TcpServer.cpp │ ├── TcpServer.h │ ├── TcpSocket.cpp │ ├── TcpSocket.h │ ├── Timer.cpp │ ├── Timer.h │ ├── Timestamp.cpp │ ├── Timestamp.h │ └── log.h │ └── xop │ ├── AACSource.cpp │ ├── AACSource.h │ ├── DigestAuthentication.cpp │ ├── DigestAuthentication.h │ ├── G711ASource.cpp │ ├── G711ASource.h │ ├── H264Parser.cpp │ ├── H264Parser.h │ ├── H264Source.cpp │ ├── H264Source.h │ ├── H265Source.cpp │ ├── H265Source.h │ ├── MediaSession.cpp │ ├── MediaSession.h │ ├── MediaSource.h │ ├── RtpConnection.cpp │ ├── RtpConnection.h │ ├── RtspConnection.cpp │ ├── RtspConnection.h │ ├── RtspMessage.cpp │ ├── RtspMessage.h │ ├── RtspPusher.cpp │ ├── RtspPusher.h │ ├── RtspServer.cpp │ ├── RtspServer.h │ ├── VP8Source.cpp │ ├── VP8Source.h │ ├── media.h │ ├── rtp.h │ └── rtsp.h └── test ├── CMakeLists.txt ├── bedrock_cfgf_test.cpp ├── bedrock_mpi.cfg ├── bedrock_mpi_vi_test.cpp ├── bedrock_test.cpp └── debug_log_test.c /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/* 2 | build 3 | lib/* 4 | 5 | -------------------------------------------------------------------------------- /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Linux", 5 | "includePath": [ 6 | "${workspaceFolder}/**", 7 | "${workspaceFolder}/inc" 8 | ], 9 | "defines": [], 10 | "compilerPath": "/home/jie/tools/llvm/bin/clang", 11 | "cStandard": "c11", 12 | "cppStandard": "c++14", 13 | "intelliSenseMode": "linux-clang-x64", 14 | "configurationProvider": "ms-vscode.cmake-tools" 15 | } 16 | ], 17 | "version": 4 18 | } -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.1.0) 2 | set(CMAKE_CXX_STANDARD 11) 3 | 4 | project(Bedrock_encoder) 5 | 6 | include_directories(inc) 7 | include_directories(encoder/inc) 8 | include_directories(rtsp/inc) 9 | 10 | set(ROCKIT_INCLUDE ${CMAKE_CURRENT_LIST_DIR}/lib/rockit/include 11 | ${CMAKE_CURRENT_LIST_DIR}/lib/libconfig/include 12 | ) 13 | 14 | include_directories(${ROCKIT_INCLUDE}) 15 | 16 | add_subdirectory(encoder) 17 | add_subdirectory(rtsp) 18 | add_subdirectory(test) 19 | 20 | set(LIB_SOURCE 21 | control/bedrock_control.c 22 | test/debug_log_test.c) 23 | # 生成动态库 24 | add_library(bedrock_lib SHARED ${LIB_SOURCE}) 25 | 26 | set(BEDROCK_DEPENDENT_LIBS 27 | bedrock_lib 28 | bedrock_encoder 29 | bedrock_rtsp 30 | ) 31 | 32 | set(SOURCES main.cpp) 33 | 34 | 35 | add_executable(${PROJECT_NAME} ${SOURCES}) 36 | target_link_libraries(${PROJECT_NAME} ${BEDROCK_DEPENDENT_LIBS}) 37 | 38 | 39 | 40 | install(TARGETS Bedrock_encoder DESTINATION bin) 41 | install(TARGETS bedrock_lib DESTINATION lib) 42 | install(FILES ./test/bedrock_mpi.cfg DESTINATION bin) 43 | # install(FILES ./inc/bedrock_control.h DESTINATION include) 44 | # install(DIRECTORY ./inc DESTINATION include FILES_MATCHING PATTERN "*.h") 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 12 | # RK356*编码网络推流软件包 13 | 使用rockchip硬件mpp进行编码,使用软件已经封装好的一层rockit接口来调用mpp进行编码。 14 | 15 | 16 | 文件目录说明: 17 | ``` 18 | ├── build 本机build环境,可直接离线编译,所需动态库都已依赖到lib目录,不依赖SDK。 19 | ├── buildroot Buildroot编译相关配置,参考其中说明加入buildroot工程进行编译。 20 | ├── CMakeLists.txt 项目配置文件,Buidroot调用入口,同时也使用Cmake来进行独立编译。cmake version >= 3.1.0 21 | ├── control 控制程序目录 22 | ├── encoder 编码程序目录 23 | ├── inc 头文件目录(外部依赖,非外部依赖不对外可见) 24 | ├── lib 使用软件包(库)目录 25 | ├── main.cpp 程序入口 26 | ├── README.md 27 | ├── rtsp 推流程序入口 28 | └── test 相关测试程序(调试) 29 | ``` 30 | 31 | # 项目生成文件 32 | main - Bedrock_encoder 33 | bedrock_control debug_log_test - libbedrock_lib.so 34 | libbedrock_encoder.a 编码函数生成的动态库文件(test中未直接使用) 35 | 36 | 37 | # 依赖软件包 38 | 注意:因为作者已经将项目软件包包含在项目中了,所以不需要在文件系统中重新编译软件包,软件包更新后可能会导致项目无法执行,作者推荐将本仓库中编译出来的动态库直接拷贝到目标板文件系统中(原因是作者没有在工程中指定安装该动态库到目标板) 39 | - rockit 40 | - libconfig 41 | 42 | # 有关test的说明 43 | 在后面的测试中因为间隔时间较长,所以不对源码做太多修改,所以使用本地的软件包进行功能测试验证。 44 | ``` 45 | ├── bedrock_cfgf_test.cpp libconfig库的测试test 46 | ├── bedrock_mpi.cfg 读取的cfg文件,会被安装在/usr/bin/目录下 47 | ├── bedrock_mpi_vi_test.cpp 采集camera并编码推流文件 48 | ├── bedrock_test.cpp 读取本地文件并推流 49 | ├── CMakeLists.txt 50 | └── debug_log_test.c 创建仓库时的文件 51 | ``` 52 | # RK平台的命令行测试工具 53 | v4l2-ctl -d /dev/video0 --set-fmt-video=width=1920,height=1080,pixelformat=NV12 --stream-mmap=3 --stream-skip=3 --stream-to=rk628.yuv --stream-count=1 --stream-poll 54 | 55 | media-ctl -d /dev/media0 --set-v4l2 '"rkisp-isp-subdev":2[fmt:UYVY8_2X8/1920x1080]' 56 | 57 | # 启动测试方法 58 | 本项目编译出了许多个文件,其中详情见test说明。 -------------------------------------------------------------------------------- /buildroot/aarch64.toolchain.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_SYSTEM_NAME Linux) 2 | set(CMAKE_SYSTEM_PROCESSOR aarch64) 3 | 4 | if(DEFINED ENV{aarch64_TOOLCHAIN_ROOT_PATH}) 5 | file(TO_CMAKE_PATH $ENV{aarch64_TOOLCHAIN_ROOT_PATH} TOOLCHAIN_ROOT_PATH) 6 | else() 7 | message(FATAL_ERROR "aarch64_TOOLCHAIN_ROOT_PATH env must be defined") 8 | endif() 9 | 10 | 11 | set(CMAKE_C_COMPILER "${TOOLCHAIN_ROOT_PATH}/bin/aarch64-buildroot-linux-gnu-gcc") 12 | set(CMAKE_CXX_COMPILER "${TOOLCHAIN_ROOT_PATH}/bin/aarch64-buildroot-linux-gnu-g++") 13 | 14 | set(CMAKE_TOOLCHAIN_FILE "${TOOLCHAIN_ROOT_PATH}") 15 | 16 | set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) 17 | set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) 18 | set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) 19 | 20 | #set(CMAKE_C_FLAGS "-march=rv64gcvxtheadc -mabi=lp64d -mtune=c906 -DRVV_SPEC_0_7 -D__riscv_zfh=1 -static -lm") 21 | #set(CMAKE_CXX_FLAGS "-march=rv64gcvxtheadc -mabi=lp64d -mtune=c906 -DRVV_SPEC_0_7 -D__riscv_zfh=1 -static") 22 | 23 | # cache flags 24 | #set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}" CACHE STRING "c flags") 25 | #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE STRING "c++ flags") 26 | 27 | # export RISCV_ROOT_PATH=/home/nihui/osd/riscv64-linux-x86_64-20210512 28 | # cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/c906.toolchain.cmake -DCMAKE_BUILD_TYPE=relwithdebinfo -DNCNN_BUILD_TESTS=ON -DNCNN_OPENMP=OFF -DNCNN_THREADS=OFF -DNCNN_RUNTIME_CPU=OFF -DNCNN_RVV=ON -DNCNN_SIMPLEOCV=ON -DNCNN_BUILD_EXAMPLES=ON .. 29 | -------------------------------------------------------------------------------- /buildroot/add_buildroot_project.md: -------------------------------------------------------------------------------- 1 | # 在FireflySDK中添加工程编译路径 2 | 3 | 4 | 1. buildroot/package/rockchip 中添加 bedrock_encoder 文件夹 复制文件夹到该位置 5 | 6 | 添加 7 | ```shell 8 | source "package/rockchip/bedrock_encoder/Config.in" 9 | ``` 10 | 到buildroot/package/rockchip/Config.in -------------------------------------------------------------------------------- /buildroot/bedrock_encoder/Config.in: -------------------------------------------------------------------------------- 1 | config BR2_PACKAGE_BEDROCK_ENCODER 2 | bool "Bedrock using mpp_lib for encoder Media file" 3 | help 4 | This is a demo to encoder app(lib?) 5 | -------------------------------------------------------------------------------- /buildroot/bedrock_encoder/bedrock_encoder.mk: -------------------------------------------------------------------------------- 1 | ################################################## 2 | ########### 3 | # 4 | ### bedrock_encoder 5 | # 6 | ################################################## 7 | ########### 8 | ifeq ($(BR2_PACKAGE_BEDROCK_ENCODER), y) 9 | 10 | BEDROCK_ENCODER_VERSION:=1.0.0 11 | BEDROCK_ENCODER_SITE=$(TOPDIR)/../external/bedrock_encoder 12 | BEDROCK_ENCODER_SITE_METHOD=local 13 | BEDROCK_ENCODER_INSTALL_STAGING = YES 14 | UVC_APP_DEPENDENCIES = libdrm mpp 15 | # 16 | # 框架编译时自动执行 17 | # 18 | # define BEDROCK_ENCODER_BUILD_CMDS 19 | # $(TARGET_MAKE_ENV) $(MAKE) CC=$(TARGET_CC) CXX=$(TARGET_CXX) -C $(@D) 20 | # endef 21 | # # 22 | # # clean时自动执行 23 | # # 24 | # define BEDROCK_ENCODER_CLEAN_CMDS 25 | # $(TARGET_MAKE_ENV) $(MAKE) -C $(@D) clean 26 | # endef 27 | # # 28 | # # 编译完成安装自动执行 29 | # # 30 | # define BEDROCK_ENCODER_INSTALL_TARGET_CMDS 31 | # $(TARGET_MAKE_ENV) $(MAKE) -C $(@D) install 32 | # endef 33 | 34 | # # 35 | # # buildroot卸载package执行的参数 36 | # # 37 | # define BEDROCK_ENCODER_UNINSTALL_TARGET_CMDS 38 | # $(TARGET_MAKE_ENV) $(MAKE) -C $(@D) uninstall 39 | # endef 40 | 41 | $(eval $(cmake-package)) 42 | endif 43 | -------------------------------------------------------------------------------- /control/bedrock_control.c: -------------------------------------------------------------------------------- 1 | /* 2 | * @Description: 3 | * @Version: 2.0 4 | * @Autor: Bedrock 5 | * @Date: 2021-12-30 21:29:01 6 | * @LastEditors: Bedrock 7 | * @LastEditTime: 2022-01-01 10:19:37 8 | */ 9 | 10 | #include "bedrock_control.h" 11 | 12 | int enable_minilog; 13 | int bedrock_app_log_level; -------------------------------------------------------------------------------- /encoder/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # add_library 3 | # 4 | include_directories(inc) 5 | include_directories(${ROCKIT_INCLUDE}) 6 | 7 | 8 | add_subdirectory(src) 9 | 10 | set(ROCKIT_DEP_COMMON_LIBS 11 | ${CMAKE_CURRENT_LIST_DIR}/../lib/rockit/lib/librockit.so 12 | ${CMAKE_CURRENT_LIST_DIR}/../lib/libconfig/lib/libconfig.so) 13 | 14 | if(CMAKE_SYSTEM_NAME MATCHES "Linux") 15 | set(ROCKIT_DEP_COMMON_LIBS 16 | ${ROCKIT_DEP_COMMON_LIBS} 17 | -lpthread 18 | ) 19 | message(STATUS "Build WITH linker libs for Linux") 20 | endif() 21 | #静态库 22 | add_library(bedrock_encoder STATIC 23 | bedrock_mpi_enc.cpp 24 | ${RK_MPI_BEDROCK_COMMON_SRC} 25 | ) 26 | 27 | 28 | target_link_libraries(bedrock_encoder ${ROCKIT_DEP_COMMON_LIBS}) -------------------------------------------------------------------------------- /encoder/Readme.md: -------------------------------------------------------------------------------- 1 | 12 | 13 | 14 | 目录: 15 | ``` 16 | ├── bedrock_mpi_enc.cpp 17 | ├── CMakeLists.txt 18 | ├── inc 19 | │ ├── argparse.h 20 | │ ├── bedrock_enc.h 21 | │ ├── loadbmp.h 22 | │ └── mpi_test_utils.h 23 | ├── Readme.md 24 | └── src 25 | ├── argparse.cpp 26 | ├── argparse.h 27 | ├── CMakeLists.txt 28 | ├── loadbmp.cpp 29 | └── mpi_test_utils.cpp 30 | ``` 31 | 32 | 相关参数说明: 33 | 34 | 1. opt 宏说明 35 | -------------------------------------------------------------------------------- /encoder/inc/argparse.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2012-2015 Yecheng Fu 3 | * All rights reserved. 4 | * 5 | * Use of this source code is governed by a MIT-style license that can be found 6 | * in the LICENSE file. 7 | * 8 | * module: argarse, developped by cofyc.jackson 9 | * project: https://github.com/cofyc/argparse 10 | * 11 | */ 12 | 13 | #ifndef ARGPARSE_H 14 | #define ARGPARSE_H 15 | 16 | /* for c++ compatibility */ 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | #include 22 | 23 | struct argparse; 24 | struct argparse_option; 25 | 26 | typedef int argparse_callback (struct argparse *self, 27 | const struct argparse_option *option); 28 | 29 | enum argparse_flag { 30 | ARGPARSE_STOP_AT_NON_OPTION = 1, 31 | }; 32 | 33 | enum argparse_option_type { 34 | /* special */ 35 | ARGPARSE_OPT_END, 36 | ARGPARSE_OPT_GROUP, 37 | /* options with no arguments */ 38 | ARGPARSE_OPT_BOOLEAN, 39 | ARGPARSE_OPT_BIT, 40 | /* options with arguments (optional or required) */ 41 | ARGPARSE_OPT_INTEGER, 42 | ARGPARSE_OPT_FLOAT, 43 | ARGPARSE_OPT_STRING, 44 | }; 45 | 46 | enum argparse_option_flags { 47 | OPT_NONEG = 1, /* disable negation */ 48 | }; 49 | 50 | /** 51 | * argparse option 52 | * 53 | * `type`: 54 | * holds the type of the option, you must have an ARGPARSE_OPT_END last in your 55 | * array. 56 | * 57 | * `short_name`: 58 | * the character to use as a short option name, '\0' if none. 59 | * 60 | * `long_name`: 61 | * the long option name, without the leading dash, NULL if none. 62 | * 63 | * `value`: 64 | * stores pointer to the value to be filled. 65 | * 66 | * `help`: 67 | * the short help message associated to what the option does. 68 | * Must never be NULL (except for ARGPARSE_OPT_END). 69 | * 70 | * `callback`: 71 | * function is called when corresponding argument is parsed. 72 | * 73 | * `data`: 74 | * associated data. Callbacks can use it like they want. 75 | * 76 | * `flags`: 77 | * option flags. 78 | */ 79 | struct argparse_option { 80 | enum argparse_option_type type; 81 | const char short_name; 82 | const char *long_name; 83 | void *value; 84 | const char *help; 85 | argparse_callback *callback; 86 | intptr_t data; 87 | int flags; 88 | }; 89 | 90 | 91 | /** 92 | * argparse 参数解析 93 | */ 94 | struct argparse { 95 | // user supplied 96 | const struct argparse_option *options; 97 | const char *const *usages; 98 | int flags; 99 | const char *description; // a description after usage 100 | const char *epilog; // a description at the end 101 | // internal context 102 | int argc; 103 | const char **argv; 104 | const char **out; 105 | int cpidx; 106 | const char *optvalue; // current option value 107 | }; 108 | 109 | // built-in callbacks 110 | int argparse_help_cb(struct argparse *self, 111 | const struct argparse_option *option); 112 | 113 | // built-in option macros 114 | #define OPT_END() { ARGPARSE_OPT_END, 0, NULL, NULL, 0, NULL, 0, 0 } 115 | #define OPT_BOOLEAN(...) { ARGPARSE_OPT_BOOLEAN, __VA_ARGS__ } 116 | #define OPT_BIT(...) { ARGPARSE_OPT_BIT, __VA_ARGS__ } 117 | #define OPT_INTEGER(...) { ARGPARSE_OPT_INTEGER, __VA_ARGS__ } 118 | #define OPT_FLOAT(...) { ARGPARSE_OPT_FLOAT, __VA_ARGS__ } 119 | #define OPT_STRING(...) { ARGPARSE_OPT_STRING, __VA_ARGS__ } 120 | #define OPT_GROUP(h) { ARGPARSE_OPT_GROUP, 0, NULL, NULL, h, NULL, 0, 0 } 121 | #define OPT_HELP() OPT_BOOLEAN('\0', "help", NULL, \ 122 | "show this help message and exit", \ 123 | argparse_help_cb, 0, OPT_NONEG) 124 | 125 | int argparse_init(struct argparse *self, struct argparse_option *options, 126 | const char *const *usages, int flags); 127 | int argparse_describe(struct argparse *self, const char *description, 128 | const char *epilog); 129 | int argparse_parse(struct argparse *self, int argc, const char **argv); 130 | int argparse_usage(struct argparse *self); 131 | 132 | #ifdef __cplusplus 133 | } 134 | #endif 135 | 136 | #endif 137 | -------------------------------------------------------------------------------- /encoder/inc/bedrock_enc.h: -------------------------------------------------------------------------------- 1 | /*** 2 | * @Description: 3 | * @Version: 2.0 4 | * @Autor: Bedrock 5 | * @Date: 2022-01-02 15:46:02 6 | * @LastEditors: Bedrock 7 | * @LastEditTime: 2022-01-21 20:00:20 8 | * @Author: Bedrock 9 | * @FilePath: /bedrock_encoder/encoder/inc/bedrock_enc.h 10 | * @版权声明 11 | */ 12 | 13 | #ifndef __BEDROCK_ENC_H__ 14 | #define __BEDROCK_ENC_H__ 15 | 16 | #include "rk_debug.h" 17 | #include "rk_mpi_cal.h" 18 | #include "rk_mpi_mb.h" 19 | #include "rk_mpi_sys.h" 20 | #include "rk_mpi_venc.h" 21 | 22 | typedef struct _rkMpiVENCCtx { 23 | const char *srcFileUri; 24 | const char *dstFilePath; 25 | RK_U32 u32SrcWidth; 26 | RK_U32 u32SrcHeight; 27 | RK_U32 u32srcVirWidth; 28 | RK_U32 u32srcVirHeight; 29 | RK_S32 s32LoopCount; 30 | RK_U32 u32ChnIndex; 31 | RK_U32 u32ChNum; 32 | RK_U32 u32SrcPixFormat; 33 | RK_U32 u32DstCodec; 34 | RK_U32 u32BufferSize; 35 | RK_U32 u32StreamBufCnt; 36 | RK_BOOL threadExit; 37 | MB_POOL vencPool; 38 | } Bedrock_VENC_CTX_S; 39 | 40 | int bedrock_main(int argc, char const* argv[]); 41 | RK_S32 mpi_enc_init(Bedrock_VENC_CTX_S *ctx); 42 | RK_S32 mpi_enc_remove(); 43 | RK_S32 read_image(RK_U8 *pVirAddr, RK_U32 u32Width, RK_U32 u32Height, 44 | RK_U32 u32VirWidth, RK_U32 u32VirHeight, RK_U32 u32PixFormat, FILE *fp); 45 | 46 | #endif // BEDROCK_ENC_H 47 | -------------------------------------------------------------------------------- /encoder/inc/filecfg.h: -------------------------------------------------------------------------------- 1 | /*** 2 | * @Description: 文件传递参数解析 3 | * @Version: 2.0 4 | * @Autor: Bedrock 5 | * @Date: 2022-01-13 11:47:08 6 | * @LastEditors: Bedrock 7 | * @LastEditTime: 2022-01-22 16:14:34 8 | * @Author: Bedrock 9 | * @FilePath: /bedrock_encoder/encoder/inc/filecfg.h 10 | * @版权声明 11 | */ 12 | 13 | 14 | 15 | #ifndef __FILECFG_H__ 16 | #define __FILECFG_H__ 17 | 18 | 19 | #include "bedrock_enc.h" 20 | 21 | int init_argc_for_cfg(Bedrock_VENC_CTX_S *p, const char *cfg_filename); 22 | 23 | #endif // FILECFG_H -------------------------------------------------------------------------------- /encoder/inc/loadbmp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | #ifndef __LOAD_BMP_H__ 19 | #define __LOAD_BMP_H__ 20 | 21 | #include "rk_type.h" 22 | 23 | #ifdef __cplusplus 24 | #if __cplusplus 25 | extern "C" { 26 | #endif 27 | #endif /* End of #ifdef __cplusplus */ 28 | 29 | 30 | /* the color format OSD supported */ 31 | typedef enum rkOSD_COLOR_FMT_E { 32 | OSD_COLOR_FMT_RGB444 = 0, 33 | OSD_COLOR_FMT_RGB4444 = 1, 34 | OSD_COLOR_FMT_RGB555 = 2, 35 | OSD_COLOR_FMT_RGB565 = 3, 36 | OSD_COLOR_FMT_ARGB1555 = 4, 37 | OSD_COLOR_FMT_BGRA5551 = 5, 38 | OSD_COLOR_FMT_RGB888 = 6, 39 | OSD_COLOR_FMT_BGR888 = 7, 40 | OSD_COLOR_FMT_ARGB8888 = 8, 41 | OSD_COLOR_FMT_BGRA8888 = 9, 42 | OSD_COLOR_FMT_BUTT 43 | } OSD_COLOR_FMT_E; 44 | 45 | typedef struct rkOSD_RGB_S { 46 | RK_U8 u8B; 47 | RK_U8 u8G; 48 | RK_U8 u8R; 49 | RK_U8 u8Reserved; 50 | } OSD_RGB_S; 51 | 52 | typedef struct rkOSD_SURFACE_S { 53 | OSD_COLOR_FMT_E enColorFmt; /* color format */ 54 | RK_U8 *pu8PhyAddr; /* physical address */ 55 | RK_U16 u16Height; /* operation height */ 56 | RK_U16 u16Width; /* operation width */ 57 | RK_U16 u16Stride; /* surface stride */ 58 | RK_U16 u16Reserved; 59 | } OSD_SURFACE_S; 60 | 61 | typedef struct tag_OSD_Logo { 62 | RK_U32 width; /* out */ 63 | RK_U32 height; /* out */ 64 | RK_U32 stride; /* in */ 65 | RK_U8 * pRGBBuffer; /* in/out */ 66 | } OSD_LOGO_T; 67 | 68 | typedef struct tag_OSD_BITMAPINFOHEADER { 69 | RK_U16 biSize; 70 | RK_U32 biWidth; 71 | RK_S32 biHeight; 72 | RK_U16 biPlanes; 73 | RK_U16 biBitCount; 74 | RK_U32 biCompression; 75 | RK_U32 biSizeImage; 76 | RK_U32 biXPelsPerMeter; 77 | RK_U32 biYPelsPerMeter; 78 | RK_U32 biClrUsed; 79 | RK_U32 biClrImportant; 80 | } OSD_BITMAPINFOHEADER; 81 | 82 | typedef struct tag_OSD_BITMAPFILEHEADER { 83 | RK_U32 bfSize; 84 | RK_U16 bfReserved1; 85 | RK_U16 bfReserved2; 86 | RK_U32 bfOffBits; 87 | } OSD_BITMAPFILEHEADER; 88 | 89 | typedef struct tag_OSD_RGBQUAD { 90 | RK_U8 rgbBlue; 91 | RK_U8 rgbGreen; 92 | RK_U8 rgbRed; 93 | RK_U8 rgbReserved; 94 | } OSD_RGBQUAD; 95 | 96 | typedef struct tag_OSD_BITMAPINFO { 97 | OSD_BITMAPINFOHEADER bmiHeader; 98 | OSD_RGBQUAD bmiColors[1]; 99 | } OSD_BITMAPINFO; 100 | 101 | typedef struct rkOSD_COMPONENT_INFO_S { 102 | RK_S32 alen; 103 | RK_S32 rlen; 104 | RK_S32 glen; 105 | RK_S32 blen; 106 | } OSD_COMP_INFO; 107 | 108 | RK_S32 load_image(const char *filename, OSD_LOGO_T *pVideoLogo); 109 | RK_S32 load_bitmap_2_surface(const char *pstFileName, const OSD_SURFACE_S *pstSurface, RK_U8 *pu8Virt); 110 | RK_S32 create_surface_by_bitmap(const char *pstFileName, OSD_SURFACE_S *pstSurface, RK_U8 *pu8Virt); 111 | RK_S32 get_bmp_info(const char *filename, OSD_BITMAPFILEHEADER *pBmpFileHeader, OSD_BITMAPINFO *pBmpInfo); 112 | 113 | #ifdef __cplusplus 114 | #if __cplusplus 115 | } 116 | #endif 117 | #endif /* End of #ifdef __cplusplus */ 118 | 119 | #endif /* End of #ifndef __LOAD_BMP_H__*/ 120 | 121 | -------------------------------------------------------------------------------- /encoder/inc/mpi_test_utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef SRC_TESTS_RT_MPI_MPI_TEST_UTILS_H_ 18 | #define SRC_TESTS_RT_MPI_MPI_TEST_UTILS_H_ 19 | 20 | #include "rk_type.h" 21 | #include "rk_debug.h" 22 | #include "rk_comm_video.h" 23 | 24 | #define RK_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) 25 | #define RK_ALIGN_16(x) RK_ALIGN(x, 16) 26 | #define RK_ALIGN_64(x) RK_ALIGN(x, 64) 27 | #define RK_ALIGN_256(x) RK_ALIGN(x, 256) 28 | #define RK_ALIGN_256_ODD(x) (RK_ALIGN(x, 256) | 256) 29 | 30 | typedef enum _RTUriSchemeType { 31 | RK_URI_SCHEME_NONE = 0, 32 | RK_URI_SCHEME_LOCAL, 33 | RK_URI_SCHEME_HTTP, 34 | RK_URI_SCHEME_RTSP, 35 | RK_URI_SCHEME_SAMBA, 36 | RK_URI_SCHEME_HLS, 37 | RK_URI_SCHEME_RTP, 38 | RK_URI_SCHEME_UDP, 39 | RK_URI_SCHEME_MMS, 40 | RK_URI_SCHEME_MMSH, 41 | RK_URI_SCHEME_MMST, 42 | RK_URI_SCHEME_TTS, 43 | RK_URI_SCHEME_IPTV, 44 | } URI_SCHEME_TYPE; 45 | 46 | RK_U64 mpi_test_utils_get_now_us(); 47 | URI_SCHEME_TYPE get_uri_scheme_type(const char* uri); 48 | RK_S32 utils_av_to_rk_codec(RK_S32 s32Id); 49 | 50 | #endif // SRC_TESTS_RT_MPI_MPI_TEST_UTILS_H_ 51 | 52 | -------------------------------------------------------------------------------- /encoder/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required( VERSION 3.1.0 ) 2 | 3 | add_definitions(-fno-rtti) 4 | 5 | add_compile_options(-std=c++11) 6 | 7 | add_definitions(-std=c++11 -Wno-attributes -Wno-deprecated-declarations -DANDROID_STL=c++_shared) 8 | 9 | 10 | set(RK_MPI_BEDROCK_COMMON_SRC 11 | ${CMAKE_CURRENT_SOURCE_DIR}/argparse.cpp 12 | ${CMAKE_CURRENT_SOURCE_DIR}/mpi_test_utils.cpp 13 | ${CMAKE_CURRENT_SOURCE_DIR}/loadbmp.cpp 14 | ${CMAKE_CURRENT_SOURCE_DIR}/filecfg.cpp 15 | PARENT_SCOPE 16 | ) -------------------------------------------------------------------------------- /encoder/src/mpi_test_utils.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #include 17 | #include 18 | #include 19 | 20 | #include "mpi_test_utils.h" 21 | 22 | #define SIZE_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0])) 23 | 24 | RK_U64 mpi_test_utils_get_now_us() { 25 | struct timespec time = {0, 0}; 26 | clock_gettime(CLOCK_MONOTONIC, &time); 27 | return (RK_U64)time.tv_sec * 1000000 + (RK_U64)time.tv_nsec / 1000; /* microseconds */ 28 | } 29 | 30 | URI_SCHEME_TYPE get_uri_scheme_type(const char* uri) { 31 | URI_SCHEME_TYPE schemeType = RK_URI_SCHEME_LOCAL; 32 | if ((RK_NULL == uri) || (strlen(uri) < 4)) { 33 | return schemeType; 34 | } 35 | 36 | if (!strncmp("http://", uri, 7) || !strncmp("https://", uri, 8)) { 37 | schemeType = RK_URI_SCHEME_HTTP; 38 | } else if (!strncmp("rtsp://", uri, 7) || !strncmp("rtmp://", uri, 7)) { 39 | RK_LOGD("uri is with rtsp or rtmp scheme type"); 40 | schemeType = RK_URI_SCHEME_RTSP; 41 | } else if (!strncmp("/data/smb/", uri, 10)) { 42 | RK_LOGD("uri is with /data/smb scheme type"); 43 | schemeType = RK_URI_SCHEME_SAMBA; 44 | } else if (!strncmp("/data/nfs/", uri, 10)) { 45 | RK_LOGD("uri is with /data/nfs schemeType (signed as samba)"); 46 | schemeType = RK_URI_SCHEME_SAMBA; 47 | } else if (strstr(uri, "m3u8")) { 48 | RK_LOGD("uri is with m3u8 scheme type"); 49 | schemeType = RK_URI_SCHEME_HLS; 50 | } else if (!strncmp("rtp:", uri, 4)) { 51 | RK_LOGD("uri is with rtp scheme type"); 52 | schemeType = RK_URI_SCHEME_RTP; 53 | } else if (!strncmp("udp:", uri, 4)) { 54 | RK_LOGD("uri is with udp scheme type"); 55 | schemeType = RK_URI_SCHEME_UDP; 56 | } else if (!strncmp("mms://", uri, 6)) { 57 | RK_LOGD("uri is with mms scheme type"); 58 | schemeType = RK_URI_SCHEME_MMS; 59 | } else if (!strncmp("mmsh://", uri, 7)) { 60 | RK_LOGD("uri is with mmsh scheme type"); 61 | schemeType = RK_URI_SCHEME_MMSH; 62 | } else if (!strncmp("mmst://", uri, 7)) { 63 | RK_LOGD("uri is with mmst scheme type"); 64 | schemeType = RK_URI_SCHEME_MMST; 65 | } else if (strstr(uri, "app_tts-cache")) { 66 | RK_LOGD("uri is with tts scheme type"); 67 | schemeType = RK_URI_SCHEME_TTS; 68 | } else if (strstr(uri, "cache://")) { 69 | schemeType = RK_URI_SCHEME_IPTV; 70 | } 71 | return schemeType; 72 | } 73 | 74 | -------------------------------------------------------------------------------- /inc/bedrock_control.h: -------------------------------------------------------------------------------- 1 | /*** 2 | * @Description: 3 | * @Version: 2.0 4 | * @Autor: Bedrock 5 | * @Date: 2022-01-10 18:01:58 6 | * @LastEditors: Bedrock 7 | * @LastEditTime: 2022-01-10 18:07:27 8 | * @Author: Bedrock 9 | * @FilePath: /bedrock_encoder/inc/bedrock_control.h 10 | * @版权声明 11 | */ 12 | #ifndef __BEDROCK_CONTROL_H__ 13 | #define __BEDROCK_CONTROL_H__ 14 | 15 | extern int enable_minilog; 16 | extern int bedrock_app_log_level; 17 | 18 | #define BEDROCK_DEBUG_LOG "/tmp/bedrock_log_debug" 19 | 20 | enum { 21 | LOG_ERROR, 22 | LOG_WARN, 23 | LOG_INFO, 24 | LOG_DEBUG 25 | }; 26 | 27 | #endif // BEDROCK_CONTROL_H -------------------------------------------------------------------------------- /inc/bedrock_log.h: -------------------------------------------------------------------------------- 1 | #ifndef __BEDROCK_LOG_H__ 2 | #define __BEDROCK_LOG_H__ 3 | 4 | #ifdef ENABLE_MINILOGGER 5 | #include "minilogger/log.h" 6 | #else 7 | #define minilog_warn(...) 8 | #define minilog_error(...) 9 | #define minilog_info(...) 10 | #define minilog_debug(...) 11 | #define __minilog_log_init(...) 12 | #endif 13 | 14 | extern int enable_minilog; 15 | extern int bedrock_app_log_level; 16 | 17 | #define LOG_LEVEL_ERROR 0 18 | #define LOG_LEVEL_WARN 1 19 | #define LOG_LEVEL_INFO 2 20 | #define LOG_LEVEL_DEBUG 3 21 | 22 | #ifndef LOG_TAG 23 | #define LOG_TAG "bedrock_app" 24 | #endif // LOG_TAG 25 | 26 | //#define ENABLE_BUFFER_TIME_DEBUG 27 | 28 | #define LOG_INFO(format, ...) \ 29 | do { \ 30 | if (bedrock_app_log_level < LOG_LEVEL_INFO) \ 31 | break; \ 32 | if (enable_minilog) \ 33 | minilog_info("[%s][%s][INFO]:" format, LOG_TAG, __FUNCTION__, ##__VA_ARGS__); \ 34 | else \ 35 | fprintf(stderr, "[%s][%s][INFO]:" format, LOG_TAG, __FUNCTION__, \ 36 | ##__VA_ARGS__); \ 37 | } while (0) 38 | 39 | #define LOG_WARN(format, ...) \ 40 | do { \ 41 | if (bedrock_app_log_level < LOG_LEVEL_WARN) \ 42 | break; \ 43 | if (enable_minilog) \ 44 | minilog_warn("[%s][%s][WARN]:" format, LOG_TAG, __FUNCTION__, ##__VA_ARGS__); \ 45 | else \ 46 | fprintf(stderr, "[%s][%s][WARN]:" format, LOG_TAG, __FUNCTION__, \ 47 | ##__VA_ARGS__); \ 48 | } while (0) 49 | 50 | #define LOG_ERROR(format, ...) \ 51 | do { \ 52 | if (bedrock_app_log_level < LOG_LEVEL_ERROR) \ 53 | break; \ 54 | if (enable_minilog) \ 55 | minilog_error("[%s][%s][ERROR]:" format, LOG_TAG, __FUNCTION__, ##__VA_ARGS__); \ 56 | else \ 57 | fprintf(stderr, "[%s][%s][ERROR]:" format, LOG_TAG, __FUNCTION__, \ 58 | ##__VA_ARGS__); \ 59 | } while (0) 60 | 61 | #define LOG_DEBUG(format, ...) \ 62 | do { \ 63 | if (bedrock_app_log_level < LOG_LEVEL_DEBUG) \ 64 | break; \ 65 | if (enable_minilog) \ 66 | minilog_debug("[%s][%s][DEBUG]:" format, LOG_TAG, __FUNCTION__, ##__VA_ARGS__); \ 67 | else \ 68 | fprintf(stderr, "[%s][%s][DEBUG]:" format, LOG_TAG, __FUNCTION__, \ 69 | ##__VA_ARGS__); \ 70 | } while (0) 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /inc/bedrock_test.h: -------------------------------------------------------------------------------- 1 | /*** 2 | * @Description: 3 | * @Version: 2.0 4 | * @Autor: Bedrock 5 | * @Date: 2022-01-10 18:08:26 6 | * @LastEditors: Bedrock 7 | * @LastEditTime: 2022-01-10 20:19:21 8 | * @Author: Bedrock 9 | * @FilePath: /bedrock_encoder/inc/bedrock_test.h 10 | * @版权声明 11 | */ 12 | 13 | #ifndef __BEDROCK_TEST_H__ 14 | #define __BEDROCK_TEST_H__ 15 | 16 | #ifdef __cplusplus 17 | extern "C" { 18 | #endif 19 | 20 | void dlog_level_test(); 21 | void adjust_log_level(); 22 | 23 | #ifdef __cplusplus 24 | } 25 | #endif 26 | 27 | #endif // BEDROCK_TEST_H -------------------------------------------------------------------------------- /lib/libconfig/lib/libconfig++.la: -------------------------------------------------------------------------------- 1 | # libconfig++.la - a libtool library file 2 | # Generated by libtool (GNU libtool) 2.4.2 3 | # 4 | # Please DO NOT delete this file! 5 | # It is necessary for linking the library. 6 | 7 | # The name that we can dlopen(3). 8 | dlname='libconfig++.so.9' 9 | 10 | # Names of this library. 11 | library_names='libconfig++.so.9.2.0 libconfig++.so.9 libconfig++.so' 12 | 13 | # The name of the static archive. 14 | old_library='' 15 | 16 | # Linker flags that can not go in dependency_libs. 17 | inherited_linker_flags='' 18 | 19 | # Libraries that this one depends upon. 20 | dependency_libs=' /home/jie/workspace/rk356x_nova/rk356x_linux_release_20211019/buildroot/output/rockchip_rk3566/host/lib/gcc/aarch64-buildroot-linux-gnu/9.3.0/../../../../aarch64-buildroot-linux-gnu/lib/../lib64/libstdc++.la' 21 | 22 | # Names of additional weak libraries provided by this library 23 | weak_library_names='' 24 | 25 | # Version information for libconfig++. 26 | current=11 27 | age=2 28 | revision=0 29 | 30 | # Is this an already installed library? 31 | installed=yes 32 | 33 | # Should we warn about portability when linking against -modules? 34 | shouldnotlink=no 35 | 36 | # Files to dlopen/dlpreopen 37 | dlopen='' 38 | dlpreopen='' 39 | 40 | # Directory that this library needs to be installed in: 41 | libdir='/usr/lib' 42 | -------------------------------------------------------------------------------- /lib/libconfig/lib/libconfig++.so: -------------------------------------------------------------------------------- 1 | libconfig++.so.9.2.0 -------------------------------------------------------------------------------- /lib/libconfig/lib/libconfig++.so.9: -------------------------------------------------------------------------------- 1 | libconfig++.so.9.2.0 -------------------------------------------------------------------------------- /lib/libconfig/lib/libconfig++.so.9.2.0: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BedRockJie/Rockchip_camera_encoder_Networkput/9d116f4c510b194a03c9199f2c45e0cd798ed496/lib/libconfig/lib/libconfig++.so.9.2.0 -------------------------------------------------------------------------------- /lib/libconfig/lib/libconfig.la: -------------------------------------------------------------------------------- 1 | # libconfig.la - a libtool library file 2 | # Generated by libtool (GNU libtool) 2.4.2 3 | # 4 | # Please DO NOT delete this file! 5 | # It is necessary for linking the library. 6 | 7 | # The name that we can dlopen(3). 8 | dlname='libconfig.so.9' 9 | 10 | # Names of this library. 11 | library_names='libconfig.so.9.2.0 libconfig.so.9 libconfig.so' 12 | 13 | # The name of the static archive. 14 | old_library='' 15 | 16 | # Linker flags that can not go in dependency_libs. 17 | inherited_linker_flags='' 18 | 19 | # Libraries that this one depends upon. 20 | dependency_libs='' 21 | 22 | # Names of additional weak libraries provided by this library 23 | weak_library_names='' 24 | 25 | # Version information for libconfig. 26 | current=11 27 | age=2 28 | revision=0 29 | 30 | # Is this an already installed library? 31 | installed=yes 32 | 33 | # Should we warn about portability when linking against -modules? 34 | shouldnotlink=no 35 | 36 | # Files to dlopen/dlpreopen 37 | dlopen='' 38 | dlpreopen='' 39 | 40 | # Directory that this library needs to be installed in: 41 | libdir='/usr/lib' 42 | -------------------------------------------------------------------------------- /lib/libconfig/lib/libconfig.so: -------------------------------------------------------------------------------- 1 | libconfig.so.9.2.0 -------------------------------------------------------------------------------- /lib/libconfig/lib/libconfig.so.9: -------------------------------------------------------------------------------- 1 | libconfig.so.9.2.0 -------------------------------------------------------------------------------- /lib/libconfig/lib/libconfig.so.9.2.0: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BedRockJie/Rockchip_camera_encoder_Networkput/9d116f4c510b194a03c9199f2c45e0cd798ed496/lib/libconfig/lib/libconfig.so.9.2.0 -------------------------------------------------------------------------------- /lib/libconfig/lib/pkgconfig/libconfig++.pc: -------------------------------------------------------------------------------- 1 | prefix=/usr 2 | exec_prefix=/usr 3 | libdir=${exec_prefix}/lib 4 | includedir=${prefix}/include 5 | 6 | Name: libconfig++ 7 | Description: C++ Configuration File Library 8 | Version: 1.5 9 | URL: http://www.hyperrealm.com/main.php?s=libconfig 10 | Requires: 11 | Conflicts: 12 | Libs: -L${libdir} -lconfig++ 13 | Libs.private: 14 | Cflags: -I${includedir} 15 | -------------------------------------------------------------------------------- /lib/libconfig/lib/pkgconfig/libconfig.pc: -------------------------------------------------------------------------------- 1 | prefix=/usr 2 | exec_prefix=/usr 3 | libdir=${exec_prefix}/lib 4 | includedir=${prefix}/include 5 | 6 | Name: libconfig 7 | Description: C Configuration File Library 8 | Version: 1.5 9 | URL: http://www.hyperrealm.com/main.php?s=libconfig 10 | Requires: 11 | Conflicts: 12 | Libs: -L${libdir} -lconfig 13 | Libs.private: 14 | Cflags: -I${includedir} 15 | -------------------------------------------------------------------------------- /lib/rockit/include/rk_comm_aenc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | #ifndef INCLUDE_RT_MPI_RK_COMM_AENC_H_ 19 | #define INCLUDE_RT_MPI_RK_COMM_AENC_H_ 20 | 21 | #include "rk_type.h" 22 | #include "rk_common.h" 23 | #include "rk_comm_aio.h" 24 | 25 | #ifdef __cplusplus 26 | #if __cplusplus 27 | extern "C" { 28 | #endif 29 | #endif /* End of #ifdef __cplusplus */ 30 | 31 | typedef struct rkAENC_ATTR_CODEC_S { 32 | RK_U32 u32Channels; 33 | RK_U32 u32SampleRate; 34 | AUDIO_BIT_WIDTH_E enBitwidth; 35 | } AENC_ATTR_CODEC_S; 36 | 37 | typedef struct rkAENC_CHN_ATTR_S { 38 | RK_CODEC_ID_E enType; /* audio codec id */ 39 | AENC_ATTR_CODEC_S stAencCodec; /* channel count & samplerate */ 40 | RK_U32 u32BufCount; /* encode buffer count */ 41 | MB_BLK extraData; /* encode key parameters */ 42 | RK_U32 extraDataSize; /* key parameters size */ 43 | } AENC_CHN_ATTR_S; 44 | 45 | typedef enum rkEN_AENC_ERR_CODE_E { 46 | AENC_ERR_ENCODER_ERR = 64 , 47 | AENC_ERR_VQE_ERR = 65 , 48 | } EN_AENC_ERR_CODE_E; 49 | 50 | /* invlalid device ID */ 51 | #define RK_ERR_AENC_INVALID_DEVID RK_DEF_ERR(RK_ID_AENC, RK_ERR_LEVEL_ERROR, RK_ERR_INVALID_DEVID) 52 | /* invlalid channel ID */ 53 | #define RK_ERR_AENC_INVALID_CHNID RK_DEF_ERR(RK_ID_AENC, RK_ERR_LEVEL_ERROR, RK_ERR_INVALID_CHNID) 54 | /* at lease one parameter is illagal ,eg, an illegal enumeration value */ 55 | #define RK_ERR_AENC_ILLEGAL_PARAM RK_DEF_ERR(RK_ID_AENC, RK_ERR_LEVEL_ERROR, RK_ERR_ILLEGAL_PARAM) 56 | /* channel exists */ 57 | #define RK_ERR_AENC_EXIST RK_DEF_ERR(RK_ID_AENC, RK_ERR_LEVEL_ERROR, RK_ERR_EXIST) 58 | /* channel unexists */ 59 | #define RK_ERR_AENC_UNEXIST RK_DEF_ERR(RK_ID_AENC, RK_ERR_LEVEL_ERROR, RK_ERR_UNEXIST) 60 | /* using a NULL point */ 61 | #define RK_ERR_AENC_NULL_PTR RK_DEF_ERR(RK_ID_AENC, RK_ERR_LEVEL_ERROR, RK_ERR_NULL_PTR) 62 | /* try to enable or initialize system,device or channel, before configing attribute */ 63 | #define RK_ERR_AENC_NOT_CONFIG RK_DEF_ERR(RK_ID_AENC, RK_ERR_LEVEL_ERROR, RK_ERR_NOT_CONFIG) 64 | /* operation is not supported by NOW */ 65 | #define RK_ERR_AENC_NOT_SUPPORT RK_DEF_ERR(RK_ID_AENC, RK_ERR_LEVEL_ERROR, RK_ERR_NOT_SUPPORT) 66 | /* operation is not permitted ,eg, try to change static attribute */ 67 | #define RK_ERR_AENC_NOT_PERM RK_DEF_ERR(RK_ID_AENC, RK_ERR_LEVEL_ERROR, RK_ERR_NOT_PERM) 68 | /* failure caused by malloc memory */ 69 | #define RK_ERR_AENC_NOMEM RK_DEF_ERR(RK_ID_AENC, RK_ERR_LEVEL_ERROR, RK_ERR_NOMEM) 70 | /* failure caused by malloc buffer */ 71 | #define RK_ERR_AENC_NOBUF RK_DEF_ERR(RK_ID_AENC, RK_ERR_LEVEL_ERROR, RK_ERR_NOBUF) 72 | /* no data in buffer */ 73 | #define RK_ERR_AENC_BUF_EMPTY RK_DEF_ERR(RK_ID_AENC, RK_ERR_LEVEL_ERROR, RK_ERR_BUF_EMPTY) 74 | /* no buffer for new data */ 75 | #define RK_ERR_AENC_BUF_FULL RK_DEF_ERR(RK_ID_AENC, RK_ERR_LEVEL_ERROR, RK_ERR_BUF_FULL) 76 | /* system is not ready,had not initialed or loaded*/ 77 | #define RK_ERR_AENC_SYS_NOTREADY RK_DEF_ERR(RK_ID_AENC, RK_ERR_LEVEL_ERROR, RK_ERR_SYS_NOTREADY) 78 | /* encoder internal err */ 79 | #define RK_ERR_AENC_ENCODER_ERR RK_DEF_ERR(RK_ID_AENC, RK_ERR_LEVEL_ERROR, AENC_ERR_ENCODER_ERR) 80 | /* vqe internal err */ 81 | #define RK_ERR_AENC_VQE_ERR RK_DEF_ERR(RK_ID_AENC, RK_ERR_LEVEL_ERROR, AENC_ERR_VQE_ERR) 82 | 83 | 84 | #ifdef __cplusplus 85 | #if __cplusplus 86 | } 87 | #endif 88 | #endif /* End of #ifdef __cplusplus */ 89 | 90 | #endif/* End of #ifndef INCLUDE_RT_MPI_RK_COMM_AENC_H_*/ 91 | -------------------------------------------------------------------------------- /lib/rockit/include/rk_comm_mb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | #ifndef INCLUDE_RT_MPI_RK_COMM_MB_H_ 19 | #define INCLUDE_RT_MPI_RK_COMM_MB_H_ 20 | 21 | #include "rk_type.h" 22 | #include "rk_errno.h" 23 | #include "rk_defines.h" 24 | 25 | #ifdef __cplusplus 26 | #if __cplusplus 27 | extern "C" { 28 | #endif 29 | #endif 30 | 31 | typedef enum rkMB_UID_E { 32 | MB_UID_VI = 0, 33 | MB_UID_VO = 1, 34 | MB_UID_VGS = 2, 35 | MB_UID_VENC = 3, 36 | MB_UID_VDEC = 4, 37 | MB_UID_VPSS = 5, 38 | MB_UID_AI = 6, 39 | MB_UID_AENC = 7, 40 | MB_UID_ADEC = 8, 41 | MB_UID_BUTT = 9 42 | } MB_UID_E; 43 | 44 | #define MB_INVALID_POOLID (-1U) 45 | #define MB_INVALID_HANDLE NULL 46 | 47 | #define MB_MAX_COMM_POOLS 16 48 | #define MB_MAX_MOD_COMM_POOLS 16 49 | 50 | #define MB_MAX_COUNT 10240 51 | 52 | /* Generall common pool use this owner id, module common pool use VB_UID as owner id */ 53 | #define POOL_OWNER_COMMON -1 54 | 55 | /* Private pool use this owner id */ 56 | #define POOL_OWNER_PRIVATE -2 57 | 58 | typedef RK_U32 MB_POOL; 59 | typedef void * MB_BLK; 60 | 61 | typedef enum rkMB_BUF_SYNC_TYPE_E { 62 | MB_BUF_SYNC_TYPE_READ = 0, 63 | MB_BUF_SYNC_TYPE_WRITE, 64 | MB_BUF_SYNC_TYPE_RW, 65 | } MB_BUF_SYNC_TYPE_E; 66 | 67 | typedef enum rkMB_ALLOC_TYPE { 68 | MB_ALLOC_TYPE_UNUSED = -1, 69 | MB_ALLOC_TYPE_DMA = 0, 70 | MB_ALLOC_TYPE_MALLOC, 71 | MB_ALLOC_TYPE_MAX, 72 | } MB_ALLOC_TYPE_E; 73 | 74 | typedef enum rkMB_REMAP_MODE_E { 75 | MB_REMAP_MODE_NONE = 0, /* no remap */ 76 | MB_REMAP_MODE_NOCACHE = 1 << 8, /* no cache remap */ 77 | MB_REMAP_MODE_CACHED = 1 << 9, /* cache remap, if you use this mode, you should flush cache by yourself */ 78 | MB_REMAP_MODE_BUTT 79 | } MB_REMAP_MODE_E; 80 | 81 | typedef enum rkMB_DMA_TYPE_E { 82 | MB_DMA_TYPE_NONE = 0, /* Physically Non-Continuous memory default */ 83 | MB_DMA_TYPE_CMA = 1 << 12, /* Physically Continuous memory */ 84 | MB_DMA_TYPE_BUTT 85 | } MB_DMA_TYPE_E; 86 | 87 | typedef struct rkMB_POOL_CONFIG_S { 88 | RK_U64 u64MBSize; 89 | RK_U32 u32MBCnt; 90 | MB_REMAP_MODE_E enRemapMode; 91 | MB_ALLOC_TYPE_E enAllocType; 92 | MB_DMA_TYPE_E enDmaType; 93 | RK_BOOL bPreAlloc; 94 | } MB_POOL_CONFIG_S; 95 | 96 | typedef struct rkMB_CONFIG_S { 97 | RK_U32 u32MaxPoolCnt; 98 | MB_POOL_CONFIG_S astCommPool[MB_MAX_COMM_POOLS]; 99 | } MB_CONFIG_S; 100 | 101 | typedef RK_S32 (*RK_MPI_MB_FREE_CB)(void *); 102 | 103 | typedef struct _rkMB_EXT_CONFIG_S { 104 | RK_U8 *pu8VirAddr; 105 | RK_U64 u64PhyAddr; 106 | RK_S32 s32Fd; 107 | RK_U64 u64Size; 108 | RK_MPI_MB_FREE_CB pFreeCB; 109 | RK_VOID *pOpaque; 110 | } MB_EXT_CONFIG_S; 111 | 112 | #define RK_ERR_MB_NULL_PTR RK_DEF_ERR(RK_ID_MB, RK_ERR_LEVEL_ERROR, RK_ERR_NULL_PTR) 113 | #define RK_ERR_MB_NOMEM RK_DEF_ERR(RK_ID_MB, RK_ERR_LEVEL_ERROR, RK_ERR_NOMEM) 114 | #define RK_ERR_MB_NOBUF RK_DEF_ERR(RK_ID_MB, RK_ERR_LEVEL_ERROR, RK_ERR_NOBUF) 115 | #define RK_ERR_MB_UNEXIST RK_DEF_ERR(RK_ID_MB, RK_ERR_LEVEL_ERROR, RK_ERR_UNEXIST) 116 | #define RK_ERR_MB_ILLEGAL_PARAM RK_DEF_ERR(RK_ID_MB, RK_ERR_LEVEL_ERROR, RK_ERR_ILLEGAL_PARAM) 117 | #define RK_ERR_MB_NOTREADY RK_DEF_ERR(RK_ID_MB, RK_ERR_LEVEL_ERROR, RK_ERR_SYS_NOTREADY) 118 | #define RK_ERR_MB_BUSY RK_DEF_ERR(RK_ID_MB, RK_ERR_LEVEL_ERROR, RK_ERR_BUSY) 119 | #define RK_ERR_MB_NOT_PERM RK_DEF_ERR(RK_ID_MB, RK_ERR_LEVEL_ERROR, RK_ERR_NOT_PERM) 120 | #define RK_ERR_MB_SIZE_NOT_ENOUGH RK_DEF_ERR(RK_ID_MB, RK_ERR_LEVEL_ERROR, RK_ERR_SIZE_NOT_ENOUGH) 121 | 122 | #define RK_ERR_MB_2MPOOLS RK_DEF_ERR(RK_ID_MB, RK_ERR_LEVEL_ERROR, RK_ERR_BUTT + 1) 123 | 124 | #ifdef __cplusplus 125 | #if __cplusplus 126 | } 127 | #endif 128 | #endif /* __cplusplus */ 129 | 130 | #endif /* INCLUDE_RT_MPI_RK_COMM_MB_H_ */ 131 | 132 | -------------------------------------------------------------------------------- /lib/rockit/include/rk_debug.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | #ifndef INCLUDE_RT_MPI_RK_DEBUG_H_ 19 | #define INCLUDE_RT_MPI_RK_DEBUG_H_ 20 | 21 | #include 22 | #include 23 | 24 | #include "rk_type.h" 25 | #include "rk_common.h" 26 | 27 | #ifdef __cplusplus 28 | #if __cplusplus 29 | extern "C" { 30 | #endif 31 | #endif /* __cplusplus */ 32 | 33 | #define RK_DBG_FATAL 1 /* fatal error */ 34 | #define RK_DBG_ERR 2 /* error conditions */ 35 | #define RK_DBG_WARN 3 /* warning conditions */ 36 | #define RK_DBG_INFO 4 /* informational */ 37 | #define RK_DBG_DEBUG 5 /* debug-level messages */ 38 | #define RK_DBG_VERBOSE 6 /* verbose */ 39 | 40 | typedef struct rkLOG_LEVEL_CONF_S { 41 | MOD_ID_E enModId; 42 | RK_S32 s32Level; 43 | RK_CHAR cModName[16]; 44 | } LOG_LEVEL_CONF_S; 45 | 46 | /****************************************************************************** 47 | ** For User Mode : RK_PRINT, RK_ASSERT, RK_TRACE 48 | ******************************************************************************/ 49 | 50 | #define RK_PRINT printf 51 | 52 | #define CONFIG_RK_LOG_TRACE_SUPPORT 1 53 | #ifndef DBG_LEVEL 54 | #define DBG_LEVEL RK_DBG_DEBUG 55 | #endif 56 | #ifndef DBG_MOD_ID 57 | #define DBG_MOD_ID RK_ID_CMPI 58 | #endif 59 | 60 | /* #ifdef RK_DEBUG */ 61 | #ifdef CONFIG_RK_LOG_TRACE_SUPPORT 62 | /* Using samples: RK_ASSERT(x>y); */ 63 | #define RK_ASSERT(expr) \ 64 | do { \ 65 | if (!(expr)) { \ 66 | printf("\nASSERT at:\n" \ 67 | " >Function : %s\n" \ 68 | " >Line No. : %d\n" \ 69 | " >Condition: %s\n", \ 70 | __FUNCTION__, __LINE__, #expr);\ 71 | _exit(-1);\ 72 | } \ 73 | } while (0) 74 | 75 | /* Using samples: 76 | * RK_TRACE(RK_DBG_DEBUG, "Test %d, %s\n", 12, "Test"); 77 | */ 78 | #define RK_TRACE(level, fmt, ...) \ 79 | do { \ 80 | RK_LOG(level, DBG_MOD_ID, fmt, __FUNCTION__, \ 81 | __LINE__, ##__VA_ARGS__); \ 82 | } while (0) 83 | #else 84 | #define RK_ASSERT(expr) 85 | #define RK_TRACE(level, fmt, ...) 86 | #endif 87 | 88 | #define RK_LOGE(fmt, ...) RK_TRACE(RK_DBG_ERR, fmt, ##__VA_ARGS__) 89 | #define RK_LOGW(fmt, ...) RK_TRACE(RK_DBG_WARN, fmt, ##__VA_ARGS__) 90 | #define RK_LOGI(fmt, ...) RK_TRACE(RK_DBG_INFO, fmt, ##__VA_ARGS__) 91 | #define RK_LOGD(fmt, ...) RK_TRACE(RK_DBG_DEBUG, fmt, ##__VA_ARGS__) 92 | #define RK_LOGV(fmt, ...) RK_TRACE(RK_DBG_VERBOSE, fmt, ##__VA_ARGS__) 93 | 94 | void RK_LOG(RK_S32 level, RK_S32 modId, const char *fmt, 95 | const char *fname, const RK_U32 row, ...); 96 | 97 | #ifdef __cplusplus 98 | #if __cplusplus 99 | } 100 | #endif 101 | #endif /* __cplusplus */ 102 | 103 | #endif // INCLUDE_RT_MPI_RK_DEBUG_H_ 104 | -------------------------------------------------------------------------------- /lib/rockit/include/rk_errno.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | #ifndef INCLUDE_RT_MPI_RK_ERRNO_H_ 19 | #define INCLUDE_RT_MPI_RK_ERRNO_H_ 20 | 21 | #include 22 | 23 | #include "rk_type.h" 24 | 25 | #ifdef __cplusplus 26 | #if __cplusplus 27 | extern "C" { 28 | #endif 29 | #endif /* __cplusplus */ 30 | 31 | typedef enum rkERR_LEVEL_E { 32 | RK_ERR_LEVEL_DEBUG = 0, /* debug-level */ 33 | RK_ERR_LEVEL_INFO, /* informational */ 34 | RK_ERR_LEVEL_NOTICE, /* normal but significant condition */ 35 | RK_ERR_LEVEL_WARNING, /* warning conditions */ 36 | RK_ERR_LEVEL_ERROR, /* error conditions */ 37 | RK_ERR_LEVEL_CRIT, /* critical conditions */ 38 | RK_ERR_LEVEL_ALERT, /* action must be taken immediately */ 39 | RK_ERR_LEVEL_FATAL, /* just for compatibility with previous version */ 40 | RK_ERR_LEVEL_BUTT 41 | } ERR_LEVEL_E; 42 | 43 | #define RK_ERR_APPID (0x80000000L + 0x20000000L) 44 | 45 | /****************************************************************************** 46 | |----------------------------------------------------------------| 47 | | 1 | APP_ID | MOD_ID | ERR_LEVEL | ERR_ID | 48 | |----------------------------------------------------------------| 49 | |<--><--7bits----><----8bits---><--3bits---><------13bits------->| 50 | ******************************************************************************/ 51 | 52 | #define RK_DEF_ERR(module, level, errid) \ 53 | ((RK_S32)((RK_ERR_APPID) | ((module) << 16 ) | ((level) << 13) | (errid))) 54 | 55 | /* NOTE! the following defined all common error code, 56 | ** all module must reserved 0~63 for their common error code 57 | */ 58 | typedef enum rkEN_ERR_CODE_E { 59 | // invlalid device ID 60 | RK_ERR_INVALID_DEVID = 1, 61 | // invlalid channel ID 62 | RK_ERR_INVALID_CHNID = 2, 63 | /* 64 | * at lease one parameter is illagal 65 | * eg, an illegal enumeration value 66 | */ 67 | RK_ERR_ILLEGAL_PARAM = 3, 68 | // resource exists 69 | RK_ERR_EXIST = 4, 70 | // resource unexists 71 | RK_ERR_UNEXIST = 5, 72 | // using a NULL point 73 | RK_ERR_NULL_PTR = 6, 74 | /* 75 | * try to enable or initialize system, device 76 | * or channel, before configing attribute 77 | */ 78 | RK_ERR_NOT_CONFIG = 7, 79 | // operation or type is not supported by NOW 80 | RK_ERR_NOT_SUPPORT = 8, 81 | /* 82 | * operation is not permitted 83 | * eg, try to change static attribute 84 | */ 85 | RK_ERR_NOT_PERM = 9, 86 | // invlalid pipe ID 87 | RK_ERR_INVALID_PIPEID = 10, 88 | // invlalid stitch group ID 89 | RK_ERR_INVALID_STITCHGRPID = 11, 90 | // failure caused by malloc memory 91 | RK_ERR_NOMEM = 12, 92 | // failure caused by malloc buffer 93 | RK_ERR_NOBUF = 13, 94 | // no data in buffer 95 | RK_ERR_BUF_EMPTY = 14, 96 | // no buffer for new data 97 | RK_ERR_BUF_FULL = 15, 98 | /* 99 | * System is not ready,maybe not initialed or 100 | * loaded. Returning the error code when opening 101 | * a device file failed. 102 | */ 103 | RK_ERR_SYS_NOTREADY = 16, 104 | /* 105 | * bad address, 106 | * eg. used for copy_from_user & copy_to_user 107 | */ 108 | RK_ERR_BADADDR = 17, 109 | /* 110 | * resource is busy, 111 | * eg. destroy a venc chn without unregister it 112 | */ 113 | RK_ERR_BUSY = 18, 114 | // buffer size is smaller than the actual size required 115 | RK_ERR_SIZE_NOT_ENOUGH = 19, 116 | /* 117 | * maxium code, private error code of all modules 118 | * must be greater than it 119 | */ 120 | RK_ERR_BUTT = 63, 121 | }RK_ERR_CODE_E; 122 | 123 | #ifdef __cplusplus 124 | #if __cplusplus 125 | } 126 | #endif 127 | #endif /* __cplusplus */ 128 | 129 | #endif // INCLUDE_RT_MPI_RK_ERRNO_H_ 130 | -------------------------------------------------------------------------------- /lib/rockit/include/rk_mpi_adec.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | #ifndef INCLUDE_RT_MPI_MPI_ADEC_H_ 19 | #define INCLUDE_RT_MPI_MPI_ADEC_H_ 20 | 21 | #include "rk_common.h" 22 | #include "rk_comm_adec.h" 23 | #include "rk_comm_aio.h" 24 | 25 | #ifdef __cplusplus 26 | #if __cplusplus 27 | extern "C" { 28 | #endif 29 | #endif /* __cplusplus */ 30 | 31 | 32 | RK_S32 RK_MPI_ADEC_CreateChn(ADEC_CHN AdChn, const ADEC_CHN_ATTR_S *pstAttr); 33 | RK_S32 RK_MPI_ADEC_DestroyChn(ADEC_CHN AdChn); 34 | RK_S32 RK_MPI_ADEC_SendStream(ADEC_CHN AdChn, const AUDIO_STREAM_S *pstStream, RK_BOOL bBlock); 35 | RK_S32 RK_MPI_ADEC_ClearChnBuf(ADEC_CHN AdChn); 36 | RK_S32 RK_MPI_ADEC_GetFrame(ADEC_CHN AdChn, AUDIO_FRAME_INFO_S *pstFrmInfo, RK_BOOL bBlock); 37 | RK_S32 RK_MPI_ADEC_ReleaseFrame(ADEC_CHN AdChn, AUDIO_FRAME_INFO_S *pstFrmInfo); 38 | RK_S32 RK_MPI_ADEC_SendEndOfStream(ADEC_CHN AdChn, RK_BOOL bInstant); 39 | RK_S32 RK_MPI_ADEC_QueryChnStat(ADEC_CHN AdChn, ADEC_CHN_STATE_S *pstBufferStatus); 40 | 41 | #ifdef __cplusplus 42 | #if __cplusplus 43 | } 44 | #endif 45 | #endif /* End of #ifdef __cplusplus */ 46 | 47 | #endif /* End of INCLUDE_RT_MPI_MPI_ADEC_H_ */ 48 | -------------------------------------------------------------------------------- /lib/rockit/include/rk_mpi_aenc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | #ifndef INCLUDE_RT_MPI_MPI_AENC_H_ 19 | #define INCLUDE_RT_MPI_MPI_AENC_H_ 20 | 21 | #include "rk_common.h" 22 | #include "rk_comm_aenc.h" 23 | #include "rk_comm_aio.h" 24 | 25 | #ifdef __cplusplus 26 | #if __cplusplus 27 | extern "C" { 28 | #endif 29 | #endif /* __cplusplus */ 30 | 31 | RK_S32 RK_MPI_AENC_CreateChn(AENC_CHN AeChn, const AENC_CHN_ATTR_S *pstAttr); 32 | RK_S32 RK_MPI_AENC_DestroyChn(AENC_CHN AeChn); 33 | RK_S32 RK_MPI_AENC_SendFrame(AENC_CHN AeChn, const AUDIO_FRAME_S *pstFrm, 34 | const AEC_FRAME_S *pstAecFrm, RK_S32 s32MilliSec); 35 | RK_S32 RK_MPI_AENC_GetStream(AENC_CHN AeChn, AUDIO_STREAM_S *pstStream, RK_S32 s32MilliSec); 36 | RK_S32 RK_MPI_AENC_ReleaseStream(AENC_CHN AeChn, const AUDIO_STREAM_S *pstStream); 37 | RK_S32 RK_MPI_AENC_SaveFile(AENC_CHN AeChn, const AUDIO_SAVE_FILE_INFO_S *pstSaveFileInfo); 38 | RK_S32 RK_MPI_AENC_QueryFileStatus(AENC_CHN AeChn, AUDIO_FILE_STATUS_S* pstFileStatus); 39 | 40 | #ifdef __cplusplus 41 | #if __cplusplus 42 | } 43 | #endif 44 | #endif /* End of #ifdef __cplusplus */ 45 | 46 | #endif /* End of INCLUDE_RT_MPI_MPI_AENC_H_ */ 47 | -------------------------------------------------------------------------------- /lib/rockit/include/rk_mpi_ai.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | #ifndef INCLUDE_RT_MPI_MPI_AI_H_ 19 | #define INCLUDE_RT_MPI_MPI_AI_H_ 20 | 21 | #include "rk_common.h" 22 | #include "rk_comm_aio.h" 23 | 24 | #ifdef __cplusplus 25 | #if __cplusplus 26 | extern "C" { 27 | #endif 28 | #endif /* __cplusplus */ 29 | 30 | RK_S32 RK_MPI_AI_SetPubAttr(AUDIO_DEV AiDevId, const AIO_ATTR_S *pstAttr); 31 | RK_S32 RK_MPI_AI_GetPubAttr(AUDIO_DEV AiDevId, AIO_ATTR_S *pstAttr); 32 | RK_S32 RK_MPI_AI_Enable(AUDIO_DEV AiDevId); 33 | RK_S32 RK_MPI_AI_Disable(AUDIO_DEV AiDevId); 34 | RK_S32 RK_MPI_AI_EnableChn(AUDIO_DEV AiDevId, AI_CHN AiChn); 35 | RK_S32 RK_MPI_AI_DisableChn(AUDIO_DEV AiDevId, AI_CHN AiChn); 36 | RK_S32 RK_MPI_AI_GetFrame(AUDIO_DEV AiDevId, AI_CHN AiChn, 37 | AUDIO_FRAME_S *pstFrm, AEC_FRAME_S *pstAecFrm , RK_S32 s32MilliSec); 38 | RK_S32 RK_MPI_AI_ReleaseFrame(AUDIO_DEV AiDevId, AI_CHN AiChn, 39 | const AUDIO_FRAME_S *pstFrm, const AEC_FRAME_S *pstAecFrm); 40 | RK_S32 RK_MPI_AI_SetChnParam(AUDIO_DEV AiDevId, AI_CHN AiChn, const AI_CHN_PARAM_S *pstChnParam); 41 | RK_S32 RK_MPI_AI_GetChnParam(AUDIO_DEV AiDevId, AI_CHN AiChn, AI_CHN_PARAM_S *pstChnParam); 42 | RK_S32 RK_MPI_AI_EnableReSmp(AUDIO_DEV AiDevId, AI_CHN AiChn, AUDIO_SAMPLE_RATE_E enOutSampleRate); 43 | RK_S32 RK_MPI_AI_DisableReSmp(AUDIO_DEV AiDevId, AI_CHN AiChn); 44 | RK_S32 RK_MPI_AI_SetTrackMode(AUDIO_DEV AiDevId, AUDIO_TRACK_MODE_E enTrackMode); 45 | RK_S32 RK_MPI_AI_GetTrackMode(AUDIO_DEV AiDevId, AUDIO_TRACK_MODE_E *penTrackMode); 46 | RK_S32 RK_MPI_AI_ClrPubAttr(AUDIO_DEV AiDevId); 47 | RK_S32 RK_MPI_AI_SaveFile(AUDIO_DEV AiDevId, AI_CHN AiChn, 48 | const AUDIO_SAVE_FILE_INFO_S *pstSaveFileInfo); 49 | RK_S32 RK_MPI_AI_QueryFileStatus(AUDIO_DEV AiDevId, AI_CHN AiChn, 50 | AUDIO_FILE_STATUS_S *pstFileStatus); 51 | 52 | #ifdef __cplusplus 53 | #if __cplusplus 54 | } 55 | #endif 56 | #endif /* End of #ifdef __cplusplus */ 57 | 58 | #endif /* End of INCLUDE_RT_MPI_MPI_AI_H_ */ 59 | -------------------------------------------------------------------------------- /lib/rockit/include/rk_mpi_ao.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | #ifndef INCLUDE_RT_MPI_MPI_AO_H_ 19 | #define INCLUDE_RT_MPI_MPI_AO_H_ 20 | 21 | #include "rk_common.h" 22 | #include "rk_comm_aio.h" 23 | 24 | #ifdef __cplusplus 25 | #if __cplusplus 26 | extern "C" { 27 | #endif 28 | #endif /* __cplusplus */ 29 | 30 | 31 | RK_S32 RK_MPI_AO_SetPubAttr(AUDIO_DEV AoDevId, const AIO_ATTR_S *pstAttr); 32 | RK_S32 RK_MPI_AO_GetPubAttr(AUDIO_DEV AoDevId, AIO_ATTR_S *pstAttr); 33 | RK_S32 RK_MPI_AO_Enable(AUDIO_DEV AoDevId); 34 | RK_S32 RK_MPI_AO_Disable(AUDIO_DEV AoDevId); 35 | RK_S32 RK_MPI_AO_EnableChn(AUDIO_DEV AoDevId, AO_CHN AoChn); 36 | RK_S32 RK_MPI_AO_DisableChn(AUDIO_DEV AoDevId, AO_CHN AoChn); 37 | RK_S32 RK_MPI_AO_SendFrame(AUDIO_DEV AoDevId, AO_CHN AoChn, const AUDIO_FRAME_S *pstData, RK_S32 s32MilliSec); 38 | RK_S32 RK_MPI_AO_EnableReSmp(AUDIO_DEV AoDevId, AO_CHN AoChn, AUDIO_SAMPLE_RATE_E enInSampleRate); 39 | RK_S32 RK_MPI_AO_DisableReSmp(AUDIO_DEV AoDevId, AO_CHN AoChn); 40 | RK_S32 RK_MPI_AO_PauseChn(AUDIO_DEV AoDevId, AO_CHN AoChn); 41 | RK_S32 RK_MPI_AO_ResumeChn(AUDIO_DEV AoDevId, AO_CHN AoChn); 42 | RK_S32 RK_MPI_AO_ClearChnBuf(AUDIO_DEV AoDevId, AO_CHN AoChn); 43 | RK_S32 RK_MPI_AO_QueryChnStat(AUDIO_DEV AoDevId, AO_CHN AoChn, AO_CHN_STATE_S *pstStatus); 44 | RK_S32 RK_MPI_AO_SetTrackMode(AUDIO_DEV AoDevId, AUDIO_TRACK_MODE_E enTrackMode); 45 | RK_S32 RK_MPI_AO_GetTrackMode(AUDIO_DEV AoDevId, AUDIO_TRACK_MODE_E* penTrackMode); 46 | RK_S32 RK_MPI_AO_SetVolume(AUDIO_DEV AoDevId, RK_S32 s32VolumeDb); 47 | RK_S32 RK_MPI_AO_GetVolume(AUDIO_DEV AoDevId, RK_S32 *ps32VolumeDb); 48 | RK_S32 RK_MPI_AO_SetMute(AUDIO_DEV AoDevId, RK_BOOL bEnable, const AUDIO_FADE_S *pstFade); 49 | RK_S32 RK_MPI_AO_GetMute(AUDIO_DEV AoDevId, RK_BOOL *pbEnable, AUDIO_FADE_S *pstFade); 50 | // RK_S32 RK_MPI_AO_GetFd(AUDIO_DEV AoDevId, AO_CHN AoChn); 51 | RK_S32 RK_MPI_AO_SaveFile(AUDIO_DEV AoDevId, AO_CHN AoChn, AUDIO_SAVE_FILE_INFO_S* pstSaveFileInfo); 52 | RK_S32 RK_MPI_AO_QueryFileStatus(AUDIO_DEV AoDevId, AO_CHN AoChn, AUDIO_FILE_STATUS_S* pstFileStatus); 53 | RK_S32 RK_MPI_AO_ClrPubAttr(AUDIO_DEV AoDevId); 54 | // RK_S32 RK_MPI_AO_SetVqeAttr(AUDIO_DEV AoDevId, AO_CHN AoChn, AO_VQE_CONFIG_S *pstVqeConfig); 55 | // RK_S32 RK_MPI_AO_GetVqeAttr(AUDIO_DEV AoDevId, AO_CHN AoChn, AO_VQE_CONFIG_S *pstVqeConfig); 56 | // RK_S32 RK_MPI_AO_EnableVqe(AUDIO_DEV AoDevId, AO_CHN AoChn); 57 | // RK_S32 RK_MPI_AO_DisableVqe(AUDIO_DEV AoDevId, AO_CHN AoChn); 58 | RK_S32 RK_MPI_AO_WaitEos(AUDIO_DEV AoDevId, AO_CHN AoChn, RK_S32 s32MilliSec); 59 | 60 | #ifdef __cplusplus 61 | #if __cplusplus 62 | } 63 | #endif 64 | #endif /* End of #ifdef __cplusplus */ 65 | 66 | #endif /* End of INCLUDE_RT_MPI_MPI_AO_H_ */ 67 | -------------------------------------------------------------------------------- /lib/rockit/include/rk_mpi_cal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | #ifndef INCLUDE_RT_MPI_RK_MPI_BUFFER_H_ 19 | #define INCLUDE_RT_MPI_RK_MPI_BUFFER_H_ 20 | 21 | #include "rk_type.h" 22 | #include "rk_common.h" 23 | #include "rk_comm_video.h" 24 | 25 | #ifdef __cplusplus 26 | #if __cplusplus 27 | extern "C" { 28 | #endif 29 | #endif /* __cplusplus */ 30 | 31 | RK_S32 RK_MPI_CAL_COMM_GetPicBufferSize(const PIC_BUF_ATTR_S *pstBufAttr, MB_PIC_CAL_S *pstPicCal); 32 | RK_S32 RK_MPI_CAL_TDE_GetPicBufferSize(const PIC_BUF_ATTR_S *pstBufAttr, MB_PIC_CAL_S *pstPicCal); 33 | RK_S32 RK_MPI_CAL_VGS_GetPicBufferSize(const PIC_BUF_ATTR_S *pstBufAttr, MB_PIC_CAL_S *pstPicCal); 34 | RK_S32 RK_MPI_CAL_VDEC_GetPicBufferSize(const VDEC_PIC_BUF_ATTR_S *pstBufAttr, MB_PIC_CAL_S *pstPicCal); 35 | 36 | RK_U32 RK_MPI_CAL_VGS_GetPicVirWidth(RK_U32 u32Width, PIXEL_FORMAT_E enPixelFormat); 37 | RK_U32 RK_MPI_CAL_VGS_GetPicVirHeight(RK_U32 u32Height, PIXEL_FORMAT_E enPixelFormat); 38 | 39 | RK_U32 RK_MPI_CAL_VDEC_GetVirWidth(const VDEC_PIC_BUF_ATTR_S *pstBufAttr); 40 | RK_U32 RK_MPI_CAL_VDEC_GetVirHeight(const VDEC_PIC_BUF_ATTR_S *pstBufAttr); 41 | 42 | RK_U32 RK_MPI_CAL_COMM_GetHorStride(RK_U32 u32VirWidth, PIXEL_FORMAT_E enPixelFormat); 43 | RK_U32 RK_MPI_CAL_COMM_GetVirWidth(RK_U32 u32HorStride, PIXEL_FORMAT_E enPixelFormat); 44 | 45 | #ifdef __cplusplus 46 | #if __cplusplus 47 | } 48 | #endif 49 | #endif /* __cplusplus */ 50 | 51 | #endif // INCLUDE_RT_MPI_RK_MPI_BUFFER_H_ 52 | 53 | -------------------------------------------------------------------------------- /lib/rockit/include/rk_mpi_mb.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 Rockchip Electronics Co. LTD 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * 15 | */ 16 | 17 | #ifndef INCLUDE_RT_MPI_RK_MPI_MB_H__ 18 | #define INCLUDE_RT_MPI_RK_MPI_MB_H__ 19 | 20 | #include "rk_comm_mb.h" 21 | 22 | #ifdef __cplusplus 23 | #if __cplusplus 24 | extern "C" { 25 | #endif 26 | #endif /* __cplusplus */ 27 | 28 | MB_POOL RK_MPI_MB_CreatePool(MB_POOL_CONFIG_S *pstMbPoolCfg); 29 | RK_S32 RK_MPI_MB_DestroyPool(MB_POOL pool); 30 | 31 | MB_BLK RK_MPI_MB_GetMB(MB_POOL pool, RK_U64 u64Size, RK_BOOL block); 32 | RK_S32 RK_MPI_MB_ReleaseMB(MB_BLK mb); 33 | 34 | RK_U64 RK_MPI_MB_Handle2PhysAddr(MB_BLK mb); 35 | RK_VOID *RK_MPI_MB_Handle2VirAddr(MB_BLK mb); 36 | RK_S32 RK_MPI_MB_Handle2Fd(MB_BLK mb); 37 | MB_POOL RK_MPI_MB_Handle2PoolId(MB_BLK mb); 38 | RK_S32 RK_MPI_MB_Handle2UniqueId(MB_BLK mb); 39 | RK_U64 RK_MPI_MB_GetSize(MB_BLK mb); 40 | 41 | MB_BLK RK_MPI_MB_VirAddr2Handle(RK_VOID *pstVirAddr); 42 | RK_S32 RK_MPI_MB_UniqueId2Fd(RK_S32 s32UniqueId); 43 | 44 | RK_S32 RK_MPI_MB_InquireUserCnt(MB_BLK mb); 45 | 46 | RK_S32 RK_MPI_MB_SetModPoolConfig(MB_UID_E enMbUid, const MB_CONFIG_S *pstMbConfig); 47 | RK_S32 RK_MPI_MB_GetModPoolConfig(MB_UID_E enMbUid, MB_CONFIG_S *pstMbConfig); 48 | 49 | RK_S32 RK_MPI_MB_SetBufferStride(MB_BLK mb, RK_U32 u32HorStride, RK_U32 u32VerStride); 50 | 51 | #ifdef __cplusplus 52 | #if __cplusplus 53 | } 54 | #endif 55 | #endif /* __cplusplus */ 56 | 57 | #endif /* INCLUDE_RT_MPI_RK_MPI_MB_H__ */ 58 | -------------------------------------------------------------------------------- /lib/rockit/include/rk_mpi_mmz.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2019 Rockchip Electronics Co. LTD 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * 15 | */ 16 | 17 | #ifndef INCLUDE_RT_MPI_RK_MPI_MMZ_H__ 18 | #define INCLUDE_RT_MPI_RK_MPI_MMZ_H__ 19 | 20 | #include "rk_comm_mb.h" 21 | 22 | #ifdef __cplusplus 23 | #if __cplusplus 24 | extern "C" { 25 | #endif 26 | #endif /* __cplusplus */ 27 | 28 | #define RK_MMZ_ALLOC_TYPE_IOMMU 0x00000000 29 | #define RK_MMZ_ALLOC_TYPE_CMA 0x00000001 30 | 31 | #define RK_MMZ_ALLOC_CACHEABLE 0x00000000 32 | #define RK_MMZ_ALLOC_UNCACHEABLE 0x00000010 33 | 34 | #define RK_MMZ_SYNC_READONLY 0x00000000 35 | #define RK_MMZ_SYNC_WRITEONLY 0x00000001 36 | #define RK_MMZ_SYNC_RW 0x00000002 37 | 38 | RK_S32 RK_MPI_MMZ_Alloc(MB_BLK *pBlk, RK_U32 u32Length, RK_U32 u32Flags); 39 | RK_S32 RK_MPI_MMZ_Free(MB_BLK blk); 40 | 41 | RK_U64 RK_MPI_MMZ_Handle2PhysAddr(MB_BLK blk); 42 | RK_VOID *RK_MPI_MMZ_Handle2VirAddr(MB_BLK blk); 43 | RK_S32 RK_MPI_MMZ_Handle2Fd(MB_BLK blk); 44 | RK_U64 RK_MPI_MMZ_GetSize(MB_BLK blk); 45 | MB_BLK RK_MPI_MMZ_Fd2Handle(RK_S32 u32Fd); 46 | MB_BLK RK_MPI_MMZ_VirAddr2Handle(RK_VOID *pVirAddr); 47 | MB_BLK RK_MPI_MMZ_PhyAddr2Handle(RK_U64 u64phyAddr); 48 | 49 | RK_S32 RK_MPI_MMZ_IsCacheable(MB_BLK blk); 50 | RK_S32 RK_MPI_MMZ_FlushCacheStart(MB_BLK blk, RK_U32 u32Offset, RK_U32 u32Length, RK_U32 u32Flags); 51 | RK_S32 RK_MPI_MMZ_FlushCacheEnd(MB_BLK blk, RK_U32 u32Offset, RK_U32 u32Length, RK_U32 u32Flags); 52 | RK_S32 RK_MPI_MMZ_FlushCacheVaddrStart(RK_VOID *pVirAddr, RK_U32 u32Length, RK_U32 u32Flags); 53 | RK_S32 RK_MPI_MMZ_FlushCacheVaddrEnd(RK_VOID *pVirAddr, RK_U32 u32Length, RK_U32 u32Flags); 54 | RK_S32 RK_MPI_MMZ_FlushCachePaddrStart(RK_U64 u64phyAddr, RK_U32 u32Length, RK_U32 u32Flags); 55 | RK_S32 RK_MPI_MMZ_FlushCachePaddrEnd(RK_U64 u64phyAddr, RK_U32 u32Length, RK_U32 u32Flags); 56 | 57 | #ifdef __cplusplus 58 | #if __cplusplus 59 | } 60 | #endif 61 | #endif /* __cplusplus */ 62 | 63 | #endif /* INCLUDE_RT_MPI_RK_MPI_MMZ_H__ */ 64 | -------------------------------------------------------------------------------- /lib/rockit/include/rk_mpi_rgn.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | #ifndef INCLUDE_RT_MPI_RK_MPI_RGN_H__ 19 | #define INCLUDE_RT_MPI_RK_MPI_RGN_H__ 20 | 21 | #include "rk_comm_rgn.h" 22 | 23 | #ifdef __cplusplus 24 | #if __cplusplus 25 | extern "C" { 26 | #endif 27 | #endif /* End of #ifdef __cplusplus */ 28 | 29 | RK_S32 RK_MPI_RGN_Create(RGN_HANDLE Handle, const RGN_ATTR_S *pstRegion); 30 | RK_S32 RK_MPI_RGN_Destroy(RGN_HANDLE Handle); 31 | 32 | RK_S32 RK_MPI_RGN_GetAttr(RGN_HANDLE Handle, RGN_ATTR_S *pstRegion); 33 | RK_S32 RK_MPI_RGN_SetAttr(RGN_HANDLE Handle, const RGN_ATTR_S *pstRegion); 34 | 35 | RK_S32 RK_MPI_RGN_SetBitMap(RGN_HANDLE Handle, const BITMAP_S *pstBitmap); 36 | 37 | RK_S32 RK_MPI_RGN_AttachToChn(RGN_HANDLE Handle, const MPP_CHN_S *pstChn, const RGN_CHN_ATTR_S *pstChnAttr); 38 | RK_S32 RK_MPI_RGN_DetachFromChn(RGN_HANDLE Handle, const MPP_CHN_S *pstChn); 39 | 40 | RK_S32 RK_MPI_RGN_SetDisplayAttr(RGN_HANDLE Handle, const MPP_CHN_S *pstChn, const RGN_CHN_ATTR_S *pstChnAttr); 41 | RK_S32 RK_MPI_RGN_GetDisplayAttr(RGN_HANDLE Handle, const MPP_CHN_S *pstChn, RGN_CHN_ATTR_S *pstChnAttr); 42 | 43 | RK_S32 RK_MPI_RGN_GetCanvasInfo(RGN_HANDLE Handle, RGN_CANVAS_INFO_S *pstCanvasInfo); 44 | RK_S32 RK_MPI_RGN_UpdateCanvas(RGN_HANDLE Handle); 45 | 46 | #ifdef __cplusplus 47 | #if __cplusplus 48 | } 49 | #endif 50 | #endif /* End of #ifdef __cplusplus */ 51 | 52 | #endif /* INCLUDE_RT_MPI_RK_MPI_RGN_H__ */ 53 | 54 | -------------------------------------------------------------------------------- /lib/rockit/include/rk_mpi_sys.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | #ifndef INCLUDE_RT_MPI_RK_MPI_SYS_H__ 19 | #define INCLUDE_RT_MPI_RK_MPI_SYS_H__ 20 | 21 | #include "rk_type.h" 22 | #include "rk_debug.h" 23 | #include "rk_common.h" 24 | #include "rk_comm_sys.h" 25 | #include "rk_comm_mb.h" 26 | 27 | #ifdef __cplusplus 28 | #if __cplusplus 29 | extern "C" { 30 | #endif 31 | #endif /* End of #ifdef __cplusplus */ 32 | 33 | RK_S32 RK_MPI_SYS_Init(RK_VOID); 34 | RK_S32 RK_MPI_SYS_Exit(RK_VOID); 35 | 36 | RK_S32 RK_MPI_SYS_Bind(const MPP_CHN_S *pstSrcChn, const MPP_CHN_S *pstDestChn); 37 | RK_S32 RK_MPI_SYS_UnBind(const MPP_CHN_S *pstSrcChn, const MPP_CHN_S *pstDestChn); 38 | 39 | RK_S32 RK_MPI_SYS_GetBindbyDest(const MPP_CHN_S *pstDestChn, MPP_CHN_S *pstSrcChn); 40 | RK_S32 RK_MPI_SYS_GetBindbySrc(const MPP_CHN_S *pstSrcChn, MPP_BIND_DEST_S *pstBindDest); 41 | 42 | /* alloc mmz memory in user context */ 43 | RK_S32 RK_MPI_SYS_MmzAlloc(MB_BLK *pBlk, const RK_CHAR *pstrMmb, const RK_CHAR *pstrZone, RK_U32 u32Len); 44 | 45 | /* alloc mmz memory with cache */ 46 | RK_S32 RK_MPI_SYS_MmzAlloc_Cached(MB_BLK *pBlk, const RK_CHAR *pstrMmb, const RK_CHAR *pstrZone, RK_U32 u32Len); 47 | 48 | /* alloc mmz memory with flags */ 49 | RK_S32 RK_MPI_SYS_MmzAllocEx(MB_BLK *pBlk, const RK_CHAR *pstrMmb, const RK_CHAR *pstrZone, 50 | RK_U32 u32Len, RK_U32 u32HeapFlags); 51 | 52 | /* free mmz memory in user context */ 53 | RK_S32 RK_MPI_SYS_MmzFree(MB_BLK blk); 54 | 55 | /* flush cache */ 56 | RK_S32 RK_MPI_SYS_MmzFlushCache(MB_BLK blk, RK_BOOL bReadOnly); 57 | 58 | RK_S32 RK_MPI_SYS_Malloc(MB_BLK *pBlk, RK_U32 u32Len); 59 | RK_S32 RK_MPI_SYS_Free(MB_BLK blk); 60 | 61 | RK_S32 RK_MPI_SYS_CreateMB(MB_BLK *pBlk, MB_EXT_CONFIG_S *pstMbExtConfig); 62 | 63 | /* 64 | ** u64Base is the global PTS of the system. 65 | ** ADVICE: 66 | ** 1. Better to call RK_MPI_SYS_GetCurPTS on the host board to get u64Base. 67 | ** 2. When os start up, call RK_MPI_SYS_InitPTSBase to set the init PTS. 68 | ** 3. When media bussines is running, synchronize the PTS one time per minute 69 | ** by calling RK_MPI_SYS_SyncPTS. 70 | */ 71 | RK_S32 RK_MPI_SYS_GetCurPTS(RK_U64 *pu64CurPTS); 72 | RK_S32 RK_MPI_SYS_InitPTSBase(RK_U64 u64PTSBase); 73 | RK_S32 RK_MPI_SYS_SyncPTS(RK_U64 u64PTSBase); 74 | 75 | /* set mpp channel input stream mode */ 76 | RK_S32 RK_MPI_SYS_SetChnInputMode(const MPP_CHN_S *pstChn, CHN_INPUT_MODE_E mode); 77 | 78 | /* log level configuration */ 79 | RK_S32 RK_MPI_LOG_SetLevelConf(LOG_LEVEL_CONF_S *pstConf); 80 | RK_S32 RK_MPI_LOG_GetLevelConf(LOG_LEVEL_CONF_S *pstConf); 81 | 82 | #ifdef __cplusplus 83 | #if __cplusplus 84 | } 85 | #endif 86 | #endif /* End of #ifdef __cplusplus */ 87 | 88 | #endif /* INCLUDE_RT_MPI_RK_MPI_SYS_H__ */ 89 | 90 | -------------------------------------------------------------------------------- /lib/rockit/include/rk_mpi_tde.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | #ifndef SRC_RT_MPI_INCLUDE_RK_MPI_TDE_H_ 19 | #define SRC_RT_MPI_INCLUDE_RK_MPI_TDE_H_ 20 | 21 | #include "rk_type.h" 22 | #include "rk_comm_tde.h" 23 | 24 | #ifdef __cplusplus 25 | #if __cplusplus 26 | extern "C" { 27 | #endif /* __cplusplus */ 28 | #endif /* __cplusplus */ 29 | 30 | RK_S32 RK_TDE_Open(RK_VOID); 31 | 32 | RK_VOID RK_TDE_Close(RK_VOID); 33 | 34 | TDE_HANDLE RK_TDE_BeginJob(RK_VOID); 35 | 36 | RK_S32 RK_TDE_EndJob(TDE_HANDLE s32Handle, RK_BOOL bSync, RK_BOOL bBlock, RK_U32 u32TimeOut); 37 | 38 | RK_S32 RK_TDE_CancelJob(TDE_HANDLE s32Handle); 39 | 40 | RK_S32 RK_TDE_WaitForDone(TDE_HANDLE s32Handle); 41 | 42 | RK_S32 RK_TDE_WaitAllDone(RK_VOID); 43 | 44 | RK_S32 RK_TDE_QuickCopy(TDE_HANDLE s32Handle, 45 | const TDE_SURFACE_S *pstSrc, 46 | const TDE_RECT_S *pstSrcRect, 47 | const TDE_SURFACE_S *pstDst, 48 | const TDE_RECT_S *pstDstRect); 49 | 50 | RK_S32 RK_TDE_QuickResize(TDE_HANDLE s32Handle, 51 | const TDE_SURFACE_S *pstSrc, 52 | const TDE_RECT_S *pstSrcRect, 53 | const TDE_SURFACE_S *pstDst, 54 | const TDE_RECT_S *pstDstRect); 55 | 56 | RK_S32 RK_TDE_Bitblit(TDE_HANDLE s32Handle, 57 | const TDE_SURFACE_S *pstBackGround, 58 | const TDE_RECT_S *pstBackGroundRect, 59 | const TDE_SURFACE_S *pstForeGround, 60 | const TDE_RECT_S *pstForeGroundRect, 61 | const TDE_SURFACE_S *pstDst, 62 | const TDE_RECT_S *pstDstRect, 63 | const TDE_OPT_S *pstOpt); 64 | 65 | RK_S32 RK_TDE_QuickFill(TDE_HANDLE s32Handle, 66 | TDE_SURFACE_S *pstDst, 67 | TDE_RECT_S *pstDstRect, 68 | RK_U32 u32FillData); 69 | 70 | RK_S32 RK_TDE_Rotate(TDE_HANDLE s32Handle, 71 | TDE_SURFACE_S *pstSrc, 72 | TDE_RECT_S *pstSrcRect, 73 | TDE_SURFACE_S *pstDst, 74 | TDE_RECT_S *pstDstRect, 75 | ROTATION_E enRotateAngle); 76 | 77 | #ifdef __cplusplus 78 | #if __cplusplus 79 | } 80 | #endif /* __cplusplus */ 81 | #endif /* __cplusplus */ 82 | 83 | #endif /* SRC_RT_MPI_INCLUDE_RK_MPI_TDE_H_ */ 84 | -------------------------------------------------------------------------------- /lib/rockit/include/rk_mpi_vdec.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | #ifndef INCLUDE_RT_MPI_MPI_VDEC_H_ 19 | #define INCLUDE_RT_MPI_MPI_VDEC_H_ 20 | 21 | #include "rk_common.h" 22 | #include "rk_comm_video.h" 23 | #include "rk_comm_vdec.h" 24 | 25 | #ifdef __cplusplus 26 | #if __cplusplus 27 | extern "C" { 28 | #endif 29 | #endif /* __cplusplus */ 30 | 31 | RK_S32 RK_MPI_VDEC_CreateChn(VDEC_CHN VdChn, const VDEC_CHN_ATTR_S *pstAttr); 32 | RK_S32 RK_MPI_VDEC_DestroyChn(VDEC_CHN VdChn); 33 | 34 | RK_S32 RK_MPI_VDEC_GetChnAttr(VDEC_CHN VdChn, VDEC_CHN_ATTR_S *pstAttr); 35 | RK_S32 RK_MPI_VDEC_SetChnAttr(VDEC_CHN VdChn, const VDEC_CHN_ATTR_S *pstAttr); 36 | 37 | RK_S32 RK_MPI_VDEC_StartRecvStream(VDEC_CHN VdChn); 38 | RK_S32 RK_MPI_VDEC_StopRecvStream(VDEC_CHN VdChn); 39 | 40 | RK_S32 RK_MPI_VDEC_ResetChn(VDEC_CHN VdChn); 41 | 42 | /* s32MilliSec: -1 is block,0 is no block,other positive number is timeout */ 43 | RK_S32 RK_MPI_VDEC_SendStream(VDEC_CHN VdChn, const VDEC_STREAM_S *pstStream, RK_S32 s32MilliSec); 44 | RK_S32 RK_MPI_VDEC_GetFrame(VDEC_CHN VdChn, VIDEO_FRAME_INFO_S *pstFrameInfo, RK_S32 s32MilliSec); 45 | RK_S32 RK_MPI_VDEC_ReleaseFrame(VDEC_CHN VdChn, const VIDEO_FRAME_INFO_S *pstFrameInfo); 46 | 47 | RK_S32 RK_MPI_VDEC_SetRotation(VDEC_CHN VdChn, ROTATION_E enRotation); 48 | RK_S32 RK_MPI_VDEC_GetRotation(VDEC_CHN VdChn, ROTATION_E *penRotation); 49 | 50 | RK_S32 RK_MPI_VDEC_GetModParam(VDEC_MOD_PARAM_S *pstModParam); 51 | RK_S32 RK_MPI_VDEC_SetModParam(const VDEC_MOD_PARAM_S* pstModParam); 52 | RK_S32 RK_MPI_VDEC_SetDisplayMode(VDEC_CHN VdChn, VIDEO_DISPLAY_MODE_E enDisplayMode); 53 | RK_S32 RK_MPI_VDEC_GetDisplayMode(VDEC_CHN VdChn, VIDEO_DISPLAY_MODE_E *penDisplayMode); 54 | 55 | RK_S32 RK_MPI_VDEC_QueryStatus(VDEC_CHN VdChn, VDEC_CHN_STATUS_S *pstStatus); 56 | 57 | RK_S32 RK_MPI_VDEC_SetChnParam(VDEC_CHN VdChn, const VDEC_CHN_PARAM_S *pstParam); 58 | RK_S32 RK_MPI_VDEC_GetChnParam(VDEC_CHN VdChn, VDEC_CHN_PARAM_S *pstParam); 59 | 60 | RK_S32 RK_MPI_VDEC_GetFd(VDEC_CHN VdChn); 61 | RK_S32 RK_MPI_VDEC_CloseFd(VDEC_CHN VdChn); 62 | RK_S32 RK_MPI_VDEC_AttachMbPool(VDEC_CHN VdChn, MB_POOL hMbPool); 63 | RK_S32 RK_MPI_VDEC_DetachMbPool(VDEC_CHN VdChn); 64 | 65 | #ifdef __cplusplus 66 | #if __cplusplus 67 | } 68 | #endif 69 | #endif /* __cplusplus */ 70 | 71 | #endif // INCLUDE_RT_MPI_MPI_VDEC_H_ 72 | -------------------------------------------------------------------------------- /lib/rockit/include/rk_mpi_vi.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | #ifndef INCLUDE_RT_MPI_MPI_VI_H_ 19 | #define INCLUDE_RT_MPI_MPI_VI_H_ 20 | 21 | #include "rk_comm_vi.h" 22 | 23 | #ifdef __cplusplus 24 | #if __cplusplus 25 | extern "C" { 26 | #endif 27 | #endif /* __cplusplus */ 28 | 29 | /* 1 for vi device */ 30 | RK_S32 RK_MPI_VI_SetDevAttr(VI_DEV ViDev, const VI_DEV_ATTR_S *pstDevAttr); 31 | RK_S32 RK_MPI_VI_GetDevAttr(VI_DEV ViDev, VI_DEV_ATTR_S *pstDevAttr); 32 | RK_S32 RK_MPI_VI_EnableDev(VI_DEV ViDev); 33 | RK_S32 RK_MPI_VI_DisableDev(VI_DEV ViDev); 34 | RK_S32 RK_MPI_VI_SetDevBindPipe(VI_DEV ViDev, const VI_DEV_BIND_PIPE_S *pstDevBindPipe); 35 | RK_S32 RK_MPI_VI_GetDevBindPipe(VI_DEV ViDev, VI_DEV_BIND_PIPE_S *pstDevBindPipe); 36 | RK_S32 RK_MPI_VI_GetDevIsEnable(VI_DEV ViDev); 37 | 38 | /* 2 for vi chn */ 39 | RK_S32 RK_MPI_VI_SetChnAttr(VI_PIPE ViPipe, VI_CHN ViChn, const VI_CHN_ATTR_S *pstChnAttr); 40 | RK_S32 RK_MPI_VI_GetChnAttr(VI_PIPE ViPipe, VI_CHN ViChn, VI_CHN_ATTR_S *pstChnAttr); 41 | 42 | RK_S32 RK_MPI_VI_EnableChn(VI_PIPE ViPipe, VI_CHN ViChn); 43 | RK_S32 RK_MPI_VI_DisableChn(VI_PIPE ViPipe, VI_CHN ViChn); 44 | 45 | RK_S32 RK_MPI_VI_GetChnFrame(VI_PIPE ViPipe, VI_CHN ViChn, VI_FRAME_S *pstFrameInfo, RK_S32 s32MilliSec); 46 | RK_S32 RK_MPI_VI_ReleaseChnFrame(VI_PIPE ViPipe, VI_CHN ViChn, const VI_FRAME_S *pstFrameInfo); 47 | RK_S32 RK_MPI_VI_ChnSaveFile(VI_PIPE ViPipe, VI_CHN ViChn, VI_SAVE_FILE_INFO_S* pstSaveFileInfo); 48 | RK_S32 RK_MPI_VI_QueryChnStatus(VI_PIPE ViPipe, VI_CHN ViChn, VI_CHN_STATUS_S *pstChnStatus); 49 | RK_S32 RK_MPI_VI_GetChnFd(VI_PIPE ViPipe, VI_CHN ViChn); 50 | RK_S32 RK_MPI_VI_CloseChnFd(VI_PIPE ViPipe, VI_CHN ViChn); 51 | 52 | RK_S32 RK_MPI_VI_SetChnFreeze(VI_PIPE ViPipe, VI_CHN ViChn, RK_BOOL bFreeze); 53 | RK_S32 RK_MPI_VI_GetChnFreeze(VI_PIPE ViPipe, VI_CHN ViChn, RK_BOOL *pbFreeze); 54 | 55 | #ifdef __cplusplus 56 | #if __cplusplus 57 | } 58 | #endif 59 | #endif /* __cplusplus */ 60 | 61 | #endif /* INCLUDE_RT_MPI_MPI_VI_H_ */ 62 | 63 | 64 | -------------------------------------------------------------------------------- /lib/rockit/include/rk_mpi_vpss.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | #ifndef INCLUDE_RT_MPI_RK_MPI_VPSS_H_ 19 | #define INCLUDE_RT_MPI_RK_MPI_VPSS_H_ 20 | 21 | #include "rk_common.h" 22 | #include "rk_comm_video.h" 23 | #include "rk_comm_mb.h" 24 | #include "rk_comm_vpss.h" 25 | 26 | #ifdef __cplusplus 27 | #if __cplusplus 28 | extern "C" { 29 | #endif 30 | #endif /* __cplusplus */ 31 | 32 | /* Group Settings */ 33 | RK_S32 RK_MPI_VPSS_CreateGrp(VPSS_GRP VpssGrp, const VPSS_GRP_ATTR_S *pstGrpAttr); 34 | RK_S32 RK_MPI_VPSS_DestroyGrp(VPSS_GRP VpssGrp); 35 | 36 | RK_S32 RK_MPI_VPSS_StartGrp(VPSS_GRP VpssGrp); 37 | RK_S32 RK_MPI_VPSS_StopGrp(VPSS_GRP VpssGrp); 38 | 39 | RK_S32 RK_MPI_VPSS_ResetGrp(VPSS_GRP VpssGrp); 40 | 41 | RK_S32 RK_MPI_VPSS_GetGrpAttr(VPSS_GRP VpssGrp, VPSS_GRP_ATTR_S *pstGrpAttr); 42 | RK_S32 RK_MPI_VPSS_SetGrpAttr(VPSS_GRP VpssGrp, const VPSS_GRP_ATTR_S *pstGrpAttr); 43 | 44 | RK_S32 RK_MPI_VPSS_SetGrpCrop(VPSS_GRP VpssGrp, const VPSS_CROP_INFO_S *pstCropInfo); 45 | RK_S32 RK_MPI_VPSS_GetGrpCrop(VPSS_GRP VpssGrp, VPSS_CROP_INFO_S *pstCropInfo); 46 | 47 | RK_S32 RK_MPI_VPSS_SendFrame(VPSS_GRP VpssGrp, VPSS_GRP_PIPE VpssGrpPipe, 48 | const VIDEO_FRAME_INFO_S *pstVideoFrame, RK_S32 s32MilliSec); 49 | 50 | RK_S32 RK_MPI_VPSS_GetGrpFrame(VPSS_GRP VpssGrp, VPSS_GRP_PIPE VpssGrpPipe, 51 | VIDEO_FRAME_INFO_S *pstVideoFrame); 52 | RK_S32 RK_MPI_VPSS_ReleaseGrpFrame(VPSS_GRP VpssGrp, VPSS_GRP_PIPE VpssGrpPipe, 53 | const VIDEO_FRAME_INFO_S *pstVideoFrame); 54 | 55 | RK_S32 RK_MPI_VPSS_EnableBackupFrame(VPSS_GRP VpssGrp); 56 | RK_S32 RK_MPI_VPSS_DisableBackupFrame(VPSS_GRP VpssGrp); 57 | 58 | RK_S32 RK_MPI_VPSS_SetGrpDelay(VPSS_GRP VpssGrp, RK_U32 u32Delay); 59 | RK_S32 RK_MPI_VPSS_GetGrpDelay(VPSS_GRP VpssGrp, RK_U32 *pu32Delay); 60 | 61 | RK_S32 RK_MPI_VPSS_EnableUserFrameRateCtrl(VPSS_GRP VpssGrp); 62 | RK_S32 RK_MPI_VPSS_DisableUserFrameRateCtrl(VPSS_GRP VpssGrp); 63 | 64 | /* Chn Settings */ 65 | RK_S32 RK_MPI_VPSS_SetChnAttr(VPSS_GRP VpssGrp, VPSS_CHN VpssChn, const VPSS_CHN_ATTR_S *pstChnAttr); 66 | RK_S32 RK_MPI_VPSS_GetChnAttr(VPSS_GRP VpssGrp, VPSS_CHN VpssChn, VPSS_CHN_ATTR_S *pstChnAttr); 67 | 68 | RK_S32 RK_MPI_VPSS_EnableChn(VPSS_GRP VpssGrp, VPSS_CHN VpssChn); 69 | RK_S32 RK_MPI_VPSS_DisableChn(VPSS_GRP VpssGrp, VPSS_CHN VpssChn); 70 | 71 | RK_S32 RK_MPI_VPSS_SetChnCrop(VPSS_GRP VpssGrp, VPSS_CHN VpssChn, const VPSS_CROP_INFO_S *pstCropInfo); 72 | RK_S32 RK_MPI_VPSS_GetChnCrop(VPSS_GRP VpssGrp, VPSS_CHN VpssChn, VPSS_CROP_INFO_S *pstCropInfo); 73 | 74 | RK_S32 RK_MPI_VPSS_SetChnRotation(VPSS_GRP VpssGrp, VPSS_CHN VpssChn, ROTATION_E enRotation); 75 | RK_S32 RK_MPI_VPSS_GetChnRotation(VPSS_GRP VpssGrp, VPSS_CHN VpssChn, ROTATION_E *penRotation); 76 | 77 | RK_S32 RK_MPI_VPSS_SetChnRotationEx(VPSS_GRP VpssGrp, VPSS_CHN VpssChn, 78 | const VPSS_ROTATION_EX_ATTR_S* pstRotationExAttr); 79 | RK_S32 RK_MPI_VPSS_GetChnRotationEx(VPSS_GRP VpssGrp, VPSS_CHN VpssChn, 80 | VPSS_ROTATION_EX_ATTR_S* pstRotationExAttr); 81 | 82 | RK_S32 RK_MPI_VPSS_GetChnFrame( 83 | VPSS_GRP VpssGrp, VPSS_CHN VpssChn, VIDEO_FRAME_INFO_S *pstVideoFrame, RK_S32 s32MilliSec); 84 | RK_S32 RK_MPI_VPSS_ReleaseChnFrame(VPSS_GRP VpssGrp, VPSS_CHN VpssChn, const VIDEO_FRAME_INFO_S *pstVideoFrame); 85 | 86 | RK_S32 RK_MPI_VPSS_GetRegionLuma(VPSS_GRP VpssGrp, VPSS_CHN VpssChn, const VIDEO_REGION_INFO_S *pstRegionInfo, 87 | RK_U64 *pu64LumaData, RK_S32 s32MilliSec); 88 | 89 | RK_S32 RK_MPI_VPSS_AttachMbPool(VPSS_GRP VpssGrp, VPSS_CHN VpssChn, MB_POOL hVbPool); 90 | RK_S32 RK_MPI_VPSS_DetachMbPool(VPSS_GRP VpssGrp, VPSS_CHN VpssChn); 91 | 92 | RK_S32 RK_MPI_VPSS_EnableBufferShare(VPSS_GRP VpssGrp, VPSS_CHN VpssChn); 93 | RK_S32 RK_MPI_VPSS_DisableBufferShare(VPSS_GRP VpssGrp, VPSS_CHN VpssChn); 94 | 95 | #ifdef __cplusplus 96 | #if __cplusplus 97 | } 98 | #endif 99 | #endif /* __cplusplus */ 100 | 101 | #endif /* INCLUDE_RT_MPI_RK_MPI_VPSS_H_ */ 102 | 103 | 104 | -------------------------------------------------------------------------------- /lib/rockit/include/rk_type.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Rockchip Electronics Co. LTD 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | 18 | #ifndef INCLUDE_RT_MPI_RK_TYPE_H_ 19 | #define INCLUDE_RT_MPI_RK_TYPE_H_ 20 | 21 | #include 22 | 23 | #ifdef __cplusplus 24 | #if __cplusplus 25 | extern "C" { 26 | #endif 27 | #endif /* __cplusplus */ 28 | 29 | typedef unsigned char RK_UCHAR; 30 | typedef uint8_t RK_U8; 31 | typedef uint16_t RK_U16; 32 | typedef uint32_t RK_U32; 33 | typedef uint32_t RK_UL; 34 | typedef uintptr_t RK_UINTPTR_T; 35 | 36 | typedef char RK_CHAR; 37 | typedef int8_t RK_S8; 38 | typedef int16_t RK_S16; 39 | typedef int32_t RK_S32; 40 | typedef int32_t RK_SL; 41 | 42 | typedef float RK_FLOAT; 43 | typedef double RK_DOUBLE; 44 | 45 | typedef uint64_t RK_U64; 46 | typedef int64_t RK_S64; 47 | 48 | typedef uint32_t RK_SIZE_T; 49 | typedef uint32_t RK_LENGTH_T; 50 | 51 | typedef unsigned int RK_HANDLE; 52 | 53 | typedef enum { 54 | RK_FALSE = 0, 55 | RK_TRUE = 1, 56 | } RK_BOOL; 57 | 58 | #ifndef NULL 59 | #define NULL 0L 60 | #endif 61 | 62 | #define RK_NULL 0L 63 | #define RK_SUCCESS 0 64 | #define RK_FAILURE (-1) 65 | 66 | #define RK_VOID void 67 | 68 | #ifdef __cplusplus 69 | #if __cplusplus 70 | } 71 | #endif 72 | #endif /* __cplusplus */ 73 | 74 | #endif // INCLUDE_RT_MPI_RK_TYPE_H_ 75 | -------------------------------------------------------------------------------- /lib/rockit/lib/librockit.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BedRockJie/Rockchip_camera_encoder_Networkput/9d116f4c510b194a03c9199f2c45e0cd798ed496/lib/rockit/lib/librockit.so -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | * @Description: 3 | * @Version: 2.0 4 | * @Autor: Bedrock 5 | * @Date: 2021-12-28 19:48:06 6 | * @LastEditors: Bedrock 7 | * @LastEditTime: 2022-01-21 18:23:21 8 | * @Author: Bedrock 9 | * @FilePath: /bedrock_encoder/main.cpp 10 | * @版权声明 11 | */ 12 | 13 | #include 14 | #include 15 | #include "bedrock_log.h" 16 | #include "bedrock_control.h" 17 | #include "bedrock_test.h" 18 | #include "bedrock_enc.h" 19 | #include "bedrock_rtsp.h" 20 | /* 21 | * main-> 22 | * 1. 初始化变量及内存 23 | * 2. 调整打印等级 24 | * 3. 25 | */ 26 | int main(int argc, char const *argv[]) 27 | { 28 | /*初始化函数*/ 29 | enable_minilog = 0; 30 | bedrock_app_log_level = LOG_ERROR; 31 | dlog_level_test(); 32 | printf("Hello word!\n"); 33 | adjust_log_level(); 34 | dlog_level_test(); 35 | bedrock_main(argc, argv); 36 | //bendrock_rtsp_test(argc,argv); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /rtsp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # add_library 3 | # 4 | set(RTSP_INCLUDE ${CMAKE_CURRENT_LIST_DIR}/src 5 | ${CMAKE_CURRENT_LIST_DIR}/src/3rdpart 6 | ${CMAKE_CURRENT_LIST_DIR}/src/net 7 | ${CMAKE_CURRENT_LIST_DIR}/src/xop 8 | ) 9 | 10 | include_directories(${RTSP_INCLUDE}) 11 | include_directories(inc) 12 | 13 | 14 | if(CMAKE_SYSTEM_NAME MATCHES "Linux") 15 | set(RTSP_LIBS 16 | -lrt 17 | -lpthread 18 | ) 19 | message(STATUS "Build WITH linker libs for Linux") 20 | endif() 21 | #静态库 22 | 23 | file(GLOB_RECURSE net_src src/net/*.cpp) 24 | file(GLOB_RECURSE xop_src src/xop/*.cpp) 25 | 26 | add_library(bedrock_rtsp STATIC 27 | bedrock_rtsp.cpp 28 | ${net_src} 29 | ${xop_src} 30 | ) 31 | 32 | target_link_libraries(bedrock_rtsp ${RTSP_LIBS}) -------------------------------------------------------------------------------- /rtsp/inc/bedrock_rtsp.h: -------------------------------------------------------------------------------- 1 | /*** 2 | * @Description: 3 | * @Version: 2.0 4 | * @Autor: Bedrock 5 | * @Date: 2022-01-20 21:06:34 6 | * @LastEditors: Bedrock 7 | * @LastEditTime: 2022-01-21 17:43:33 8 | * @Author: Bedrock 9 | * @FilePath: /bedrock_encoder/rtsp/inc/bedrock_rtsp.h 10 | * @版权声明 11 | */ 12 | #ifndef __BEDROCK_RTSP_H__ 13 | #define __BEDROCK_RTSP_H__ 14 | 15 | int bendrock_rtsp_test(int argc, char const *argv[]); 16 | int bedrock_rtsp_init(); 17 | #endif // BEDROCK_RTSP_H -------------------------------------------------------------------------------- /rtsp/src/net/Acceptor.cpp: -------------------------------------------------------------------------------- 1 | #include "Acceptor.h" 2 | #include "EventLoop.h" 3 | #include "SocketUtil.h" 4 | #include "Logger.h" 5 | 6 | using namespace xop; 7 | 8 | Acceptor::Acceptor(EventLoop* eventLoop) 9 | : event_loop_(eventLoop) 10 | , tcp_socket_(new TcpSocket) 11 | { 12 | 13 | } 14 | 15 | Acceptor::~Acceptor() 16 | { 17 | 18 | } 19 | 20 | int Acceptor::Listen(std::string ip, uint16_t port) 21 | { 22 | std::lock_guard locker(mutex_); 23 | 24 | if (tcp_socket_->GetSocket() > 0) { 25 | tcp_socket_->Close(); 26 | } 27 | 28 | SOCKET sockfd = tcp_socket_->Create(); 29 | channel_ptr_.reset(new Channel(sockfd)); 30 | SocketUtil::SetReuseAddr(sockfd); 31 | SocketUtil::SetReusePort(sockfd); 32 | SocketUtil::SetNonBlock(sockfd); 33 | 34 | if (!tcp_socket_->Bind(ip, port)) { 35 | return -1; 36 | } 37 | 38 | if (!tcp_socket_->Listen(1024)) { 39 | return -1; 40 | } 41 | 42 | channel_ptr_->SetReadCallback([this]() { this->OnAccept(); }); 43 | channel_ptr_->EnableReading(); 44 | event_loop_->UpdateChannel(channel_ptr_); 45 | return 0; 46 | } 47 | 48 | void Acceptor::Close() 49 | { 50 | std::lock_guard locker(mutex_); 51 | 52 | if (tcp_socket_->GetSocket() > 0) { 53 | event_loop_->RemoveChannel(channel_ptr_); 54 | tcp_socket_->Close(); 55 | } 56 | } 57 | 58 | void Acceptor::OnAccept() 59 | { 60 | std::lock_guard locker(mutex_); 61 | 62 | SOCKET socket = tcp_socket_->Accept(); 63 | if (socket > 0) { 64 | if (new_connection_callback_) { 65 | new_connection_callback_(socket); 66 | } 67 | else { 68 | SocketUtil::Close(socket); 69 | } 70 | } 71 | } 72 | 73 | -------------------------------------------------------------------------------- /rtsp/src/net/Acceptor.h: -------------------------------------------------------------------------------- 1 | #ifndef XOP_ACCEPTOR_H 2 | #define XOP_ACCEPTOR_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "Channel.h" 8 | #include "TcpSocket.h" 9 | 10 | namespace xop 11 | { 12 | 13 | typedef std::function NewConnectionCallback; 14 | 15 | class EventLoop; 16 | 17 | class Acceptor 18 | { 19 | public: 20 | Acceptor(EventLoop* eventLoop); 21 | virtual ~Acceptor(); 22 | 23 | void SetNewConnectionCallback(const NewConnectionCallback& cb) 24 | { new_connection_callback_ = cb; } 25 | 26 | int Listen(std::string ip, uint16_t port); 27 | void Close(); 28 | 29 | private: 30 | void OnAccept(); 31 | 32 | EventLoop* event_loop_ = nullptr; 33 | std::mutex mutex_; 34 | std::unique_ptr tcp_socket_; 35 | ChannelPtr channel_ptr_; 36 | NewConnectionCallback new_connection_callback_; 37 | }; 38 | 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /rtsp/src/net/BufferReader.cpp: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #include "BufferReader.h" 5 | #include "Socket.h" 6 | #include 7 | 8 | using namespace xop; 9 | uint32_t xop::ReadUint32BE(char* data) 10 | { 11 | uint8_t* p = (uint8_t*)data; 12 | uint32_t value = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; 13 | return value; 14 | } 15 | 16 | uint32_t xop::ReadUint32LE(char* data) 17 | { 18 | uint8_t* p = (uint8_t*)data; 19 | uint32_t value = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]; 20 | return value; 21 | } 22 | 23 | uint32_t xop::ReadUint24BE(char* data) 24 | { 25 | uint8_t* p = (uint8_t*)data; 26 | uint32_t value = (p[0] << 16) | (p[1] << 8) | p[2]; 27 | return value; 28 | } 29 | 30 | uint32_t xop::ReadUint24LE(char* data) 31 | { 32 | uint8_t* p = (uint8_t*)data; 33 | uint32_t value = (p[2] << 16) | (p[1] << 8) | p[0]; 34 | return value; 35 | } 36 | 37 | uint16_t xop::ReadUint16BE(char* data) 38 | { 39 | uint8_t* p = (uint8_t*)data; 40 | uint16_t value = (p[0] << 8) | p[1]; 41 | return value; 42 | } 43 | 44 | uint16_t xop::ReadUint16LE(char* data) 45 | { 46 | uint8_t* p = (uint8_t*)data; 47 | uint16_t value = (p[1] << 8) | p[0]; 48 | return value; 49 | } 50 | 51 | const char BufferReader::kCRLF[] = "\r\n"; 52 | 53 | BufferReader::BufferReader(uint32_t initial_size) 54 | { 55 | buffer_.resize(initial_size); 56 | } 57 | 58 | BufferReader::~BufferReader() 59 | { 60 | 61 | } 62 | 63 | int BufferReader::Read(SOCKET sockfd) 64 | { 65 | uint32_t size = WritableBytes(); 66 | if(size < MAX_BYTES_PER_READ) { 67 | uint32_t bufferReaderSize = (uint32_t)buffer_.size(); 68 | if(bufferReaderSize > MAX_BUFFER_SIZE) { 69 | return 0; 70 | } 71 | 72 | buffer_.resize(bufferReaderSize + MAX_BYTES_PER_READ); 73 | } 74 | 75 | int bytes_read = ::recv(sockfd, beginWrite(), MAX_BYTES_PER_READ, 0); 76 | if(bytes_read > 0) { 77 | writer_index_ += bytes_read; 78 | } 79 | 80 | return bytes_read; 81 | } 82 | 83 | 84 | uint32_t BufferReader::ReadAll(std::string& data) 85 | { 86 | uint32_t size = ReadableBytes(); 87 | if(size > 0) { 88 | data.assign(Peek(), size); 89 | writer_index_ = 0; 90 | reader_index_ = 0; 91 | } 92 | 93 | return size; 94 | } 95 | 96 | uint32_t BufferReader::ReadUntilCrlf(std::string& data) 97 | { 98 | const char* crlf = FindLastCrlf(); 99 | if(crlf == nullptr) { 100 | return 0; 101 | } 102 | 103 | uint32_t size = (uint32_t)(crlf - Peek() + 2); 104 | data.assign(Peek(), size); 105 | Retrieve(size); 106 | return size; 107 | } 108 | 109 | -------------------------------------------------------------------------------- /rtsp/src/net/BufferReader.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #ifndef XOP_BUFFER_READER_H 5 | #define XOP_BUFFER_READER_H 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "Socket.h" 13 | 14 | namespace xop 15 | { 16 | 17 | uint32_t ReadUint32BE(char* data); 18 | uint32_t ReadUint32LE(char* data); 19 | uint32_t ReadUint24BE(char* data); 20 | uint32_t ReadUint24LE(char* data); 21 | uint16_t ReadUint16BE(char* data); 22 | uint16_t ReadUint16LE(char* data); 23 | 24 | class BufferReader 25 | { 26 | public: 27 | BufferReader(uint32_t initial_size = 2048); 28 | virtual ~BufferReader(); 29 | 30 | uint32_t ReadableBytes() const 31 | { return (uint32_t)(writer_index_ - reader_index_); } 32 | 33 | uint32_t WritableBytes() const 34 | { return (uint32_t)(buffer_.size() - writer_index_); } 35 | 36 | char* Peek() 37 | { return Begin() + reader_index_; } 38 | 39 | const char* Peek() const 40 | { return Begin() + reader_index_; } 41 | 42 | const char* FindFirstCrlf() const { 43 | const char* crlf = std::search(Peek(), BeginWrite(), kCRLF, kCRLF+2); 44 | return crlf == BeginWrite() ? nullptr : crlf; 45 | } 46 | 47 | const char* FindLastCrlf() const { 48 | const char* crlf = std::find_end(Peek(), BeginWrite(), kCRLF, kCRLF+2); 49 | return crlf == BeginWrite() ? nullptr : crlf; 50 | } 51 | 52 | const char* FindLastCrlfCrlf() const { 53 | char crlfCrlf[] = "\r\n\r\n"; 54 | const char* crlf = std::find_end(Peek(), BeginWrite(), crlfCrlf, crlfCrlf + 4); 55 | return crlf == BeginWrite() ? nullptr : crlf; 56 | } 57 | 58 | void RetrieveAll() { 59 | writer_index_ = 0; 60 | reader_index_ = 0; 61 | } 62 | 63 | void Retrieve(size_t len) { 64 | if (len <= ReadableBytes()) { 65 | reader_index_ += len; 66 | if(reader_index_ == writer_index_) { 67 | reader_index_ = 0; 68 | writer_index_ = 0; 69 | } 70 | } 71 | else { 72 | RetrieveAll(); 73 | } 74 | } 75 | 76 | void RetrieveUntil(const char* end) 77 | { Retrieve(end - Peek()); } 78 | 79 | int Read(SOCKET sockfd); 80 | uint32_t ReadAll(std::string& data); 81 | uint32_t ReadUntilCrlf(std::string& data); 82 | 83 | uint32_t Size() const 84 | { return (uint32_t)buffer_.size(); } 85 | 86 | private: 87 | char* Begin() 88 | { return &*buffer_.begin(); } 89 | 90 | const char* Begin() const 91 | { return &*buffer_.begin(); } 92 | 93 | char* beginWrite() 94 | { return Begin() + writer_index_; } 95 | 96 | const char* BeginWrite() const 97 | { return Begin() + writer_index_; } 98 | 99 | std::vector buffer_; 100 | size_t reader_index_ = 0; 101 | size_t writer_index_ = 0; 102 | 103 | static const char kCRLF[]; 104 | static const uint32_t MAX_BYTES_PER_READ = 4096; 105 | static const uint32_t MAX_BUFFER_SIZE = 1024 * 100000; 106 | }; 107 | 108 | } 109 | 110 | #endif 111 | 112 | 113 | -------------------------------------------------------------------------------- /rtsp/src/net/BufferWriter.cpp: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #include "BufferWriter.h" 5 | #include "Socket.h" 6 | #include "SocketUtil.h" 7 | 8 | using namespace xop; 9 | 10 | void xop::WriteUint32BE(char* p, uint32_t value) 11 | { 12 | p[0] = value >> 24; 13 | p[1] = value >> 16; 14 | p[2] = value >> 8; 15 | p[3] = value & 0xff; 16 | } 17 | 18 | void xop::WriteUint32LE(char* p, uint32_t value) 19 | { 20 | p[0] = value & 0xff; 21 | p[1] = value >> 8; 22 | p[2] = value >> 16; 23 | p[3] = value >> 24; 24 | } 25 | 26 | void xop::WriteUint24BE(char* p, uint32_t value) 27 | { 28 | p[0] = value >> 16; 29 | p[1] = value >> 8; 30 | p[2] = value & 0xff; 31 | } 32 | 33 | void xop::WriteUint24LE(char* p, uint32_t value) 34 | { 35 | p[0] = value & 0xff; 36 | p[1] = value >> 8; 37 | p[2] = value >> 16; 38 | } 39 | 40 | void xop::WriteUint16BE(char* p, uint16_t value) 41 | { 42 | p[0] = value >> 8; 43 | p[1] = value & 0xff; 44 | } 45 | 46 | void xop::WriteUint16LE(char* p, uint16_t value) 47 | { 48 | p[0] = value & 0xff; 49 | p[1] = value >> 8; 50 | } 51 | 52 | BufferWriter::BufferWriter(int capacity) 53 | : max_queue_length_(capacity) 54 | { 55 | 56 | } 57 | 58 | bool BufferWriter::Append(std::shared_ptr data, uint32_t size, uint32_t index) 59 | { 60 | if (size <= index) { 61 | return false; 62 | } 63 | 64 | if ((int)buffer_.size() >= max_queue_length_) { 65 | return false; 66 | } 67 | 68 | Packet pkt = { data, size, index }; 69 | buffer_.emplace(std::move(pkt)); 70 | return true; 71 | } 72 | 73 | bool BufferWriter::Append(const char* data, uint32_t size, uint32_t index) 74 | { 75 | if (size <= index) { 76 | return false; 77 | } 78 | 79 | if ((int)buffer_.size() >= max_queue_length_) { 80 | return false; 81 | } 82 | 83 | Packet pkt; 84 | pkt.data.reset(new char[size+512]); 85 | memcpy(pkt.data.get(), data, size); 86 | pkt.size = size; 87 | pkt.writeIndex = index; 88 | buffer_.emplace(std::move(pkt)); 89 | return true; 90 | } 91 | 92 | int BufferWriter::Send(SOCKET sockfd, int timeout) 93 | { 94 | if (timeout > 0) { 95 | SocketUtil::SetBlock(sockfd, timeout); 96 | } 97 | 98 | int ret = 0; 99 | int count = 1; 100 | 101 | do 102 | { 103 | if (buffer_.empty()) { 104 | return 0; 105 | } 106 | 107 | count -= 1; 108 | Packet &pkt = buffer_.front(); 109 | ret = ::send(sockfd, pkt.data.get() + pkt.writeIndex, pkt.size - pkt.writeIndex, 0); 110 | if (ret > 0) { 111 | pkt.writeIndex += ret; 112 | if (pkt.size == pkt.writeIndex) { 113 | count += 1; 114 | buffer_.pop(); 115 | } 116 | } 117 | else if (ret < 0) { 118 | #if defined(__linux) || defined(__linux__) 119 | if (errno == EINTR || errno == EAGAIN) 120 | #elif defined(WIN32) || defined(_WIN32) 121 | int error = WSAGetLastError(); 122 | if (error == WSAEWOULDBLOCK || error == WSAEINPROGRESS || error == 0) 123 | #endif 124 | { 125 | ret = 0; 126 | } 127 | } 128 | } while (count > 0); 129 | 130 | if (timeout > 0) { 131 | SocketUtil::SetNonBlock(sockfd); 132 | } 133 | 134 | return ret; 135 | } 136 | 137 | 138 | -------------------------------------------------------------------------------- /rtsp/src/net/BufferWriter.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #ifndef XOP_BUFFER_WRITER_H 5 | #define XOP_BUFFER_WRITER_H 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "Socket.h" 12 | 13 | namespace xop 14 | { 15 | 16 | void WriteUint32BE(char* p, uint32_t value); 17 | void WriteUint32LE(char* p, uint32_t value); 18 | void WriteUint24BE(char* p, uint32_t value); 19 | void WriteUint24LE(char* p, uint32_t value); 20 | void WriteUint16BE(char* p, uint16_t value); 21 | void WriteUint16LE(char* p, uint16_t value); 22 | 23 | class BufferWriter 24 | { 25 | public: 26 | BufferWriter(int capacity = kMaxQueueLength); 27 | ~BufferWriter() {} 28 | 29 | bool Append(std::shared_ptr data, uint32_t size, uint32_t index=0); 30 | bool Append(const char* data, uint32_t size, uint32_t index=0); 31 | int Send(SOCKET sockfd, int timeout=0); 32 | 33 | bool IsEmpty() const 34 | { return buffer_.empty(); } 35 | 36 | bool IsFull() const 37 | { return ((int)buffer_.size() >= max_queue_length_ ? true : false); } 38 | 39 | uint32_t Size() const 40 | { return (uint32_t)buffer_.size(); } 41 | 42 | private: 43 | typedef struct 44 | { 45 | std::shared_ptr data; 46 | uint32_t size; 47 | uint32_t writeIndex; 48 | } Packet; 49 | 50 | std::queue buffer_; 51 | int max_queue_length_ = 0; 52 | 53 | static const int kMaxQueueLength = 10000; 54 | }; 55 | 56 | } 57 | 58 | #endif 59 | 60 | -------------------------------------------------------------------------------- /rtsp/src/net/Channel.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #ifndef XOP_CHANNEL_H 5 | #define XOP_CHANNEL_H 6 | 7 | #include 8 | #include 9 | #include "Socket.h" 10 | 11 | namespace xop 12 | { 13 | 14 | enum EventType 15 | { 16 | EVENT_NONE = 0, 17 | EVENT_IN = 1, 18 | EVENT_PRI = 2, 19 | EVENT_OUT = 4, 20 | EVENT_ERR = 8, 21 | EVENT_HUP = 16, 22 | EVENT_RDHUP = 8192 23 | }; 24 | 25 | class Channel 26 | { 27 | public: 28 | typedef std::function EventCallback; 29 | 30 | Channel() = delete; 31 | 32 | Channel(SOCKET sockfd) 33 | : sockfd_(sockfd) 34 | { 35 | 36 | } 37 | 38 | virtual ~Channel() {}; 39 | 40 | void SetReadCallback(const EventCallback& cb) 41 | { read_callback_ = cb; } 42 | 43 | void SetWriteCallback(const EventCallback& cb) 44 | { write_callback_ = cb; } 45 | 46 | void SetCloseCallback(const EventCallback& cb) 47 | { close_callback_ = cb; } 48 | 49 | void SetErrorCallback(const EventCallback& cb) 50 | { error_callback_ = cb; } 51 | 52 | SOCKET GetSocket() const { return sockfd_; } 53 | 54 | int GetEvents() const { return events_; } 55 | void SetEvents(int events) { events_ = events; } 56 | 57 | void EnableReading() 58 | { events_ |= EVENT_IN; } 59 | 60 | void EnableWriting() 61 | { events_ |= EVENT_OUT; } 62 | 63 | void DisableReading() 64 | { events_ &= ~EVENT_IN; } 65 | 66 | void DisableWriting() 67 | { events_ &= ~EVENT_OUT; } 68 | 69 | bool IsNoneEvent() const { return events_ == EVENT_NONE; } 70 | bool IsWriting() const { return (events_ & EVENT_OUT) != 0; } 71 | bool IsReading() const { return (events_ & EVENT_IN) != 0; } 72 | 73 | void HandleEvent(int events) 74 | { 75 | if (events & (EVENT_PRI | EVENT_IN)) { 76 | read_callback_(); 77 | } 78 | 79 | if (events & EVENT_OUT) { 80 | write_callback_(); 81 | } 82 | 83 | if (events & EVENT_HUP) { 84 | close_callback_(); 85 | return ; 86 | } 87 | 88 | if (events & (EVENT_ERR)) { 89 | error_callback_(); 90 | } 91 | } 92 | 93 | private: 94 | EventCallback read_callback_ = []{}; 95 | EventCallback write_callback_ = []{}; 96 | EventCallback close_callback_ = []{}; 97 | EventCallback error_callback_ = []{}; 98 | 99 | SOCKET sockfd_ = 0; 100 | int events_ = 0; 101 | }; 102 | 103 | typedef std::shared_ptr ChannelPtr; 104 | 105 | } 106 | 107 | #endif 108 | 109 | -------------------------------------------------------------------------------- /rtsp/src/net/EpollTaskScheduler.cpp: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #include "EpollTaskScheduler.h" 5 | 6 | #if defined(__linux) || defined(__linux__) 7 | #include 8 | #include 9 | #endif 10 | 11 | using namespace xop; 12 | 13 | EpollTaskScheduler::EpollTaskScheduler(int id) 14 | : TaskScheduler(id) 15 | { 16 | #if defined(__linux) || defined(__linux__) 17 | epollfd_ = epoll_create(1024); 18 | #endif 19 | this->UpdateChannel(wakeup_channel_); 20 | } 21 | 22 | EpollTaskScheduler::~EpollTaskScheduler() 23 | { 24 | 25 | } 26 | 27 | void EpollTaskScheduler::UpdateChannel(ChannelPtr channel) 28 | { 29 | std::lock_guard lock(mutex_); 30 | #if defined(__linux) || defined(__linux__) 31 | int fd = channel->GetSocket(); 32 | if(channels_.find(fd) != channels_.end()) { 33 | if(channel->IsNoneEvent()) { 34 | Update(EPOLL_CTL_DEL, channel); 35 | channels_.erase(fd); 36 | } 37 | else { 38 | Update(EPOLL_CTL_MOD, channel); 39 | } 40 | } 41 | else { 42 | if(!channel->IsNoneEvent()) { 43 | channels_.emplace(fd, channel); 44 | Update(EPOLL_CTL_ADD, channel); 45 | } 46 | } 47 | #endif 48 | } 49 | 50 | void EpollTaskScheduler::Update(int operation, ChannelPtr& channel) 51 | { 52 | #if defined(__linux) || defined(__linux__) 53 | struct epoll_event event = {0}; 54 | 55 | if(operation != EPOLL_CTL_DEL) { 56 | event.data.ptr = channel.get(); 57 | event.events = channel->GetEvents(); 58 | } 59 | 60 | if(::epoll_ctl(epollfd_, operation, channel->GetSocket(), &event) < 0) { 61 | 62 | } 63 | #endif 64 | } 65 | 66 | void EpollTaskScheduler::RemoveChannel(ChannelPtr& channel) 67 | { 68 | std::lock_guard lock(mutex_); 69 | #if defined(__linux) || defined(__linux__) 70 | int fd = channel->GetSocket(); 71 | 72 | if(channels_.find(fd) != channels_.end()) { 73 | Update(EPOLL_CTL_DEL, channel); 74 | channels_.erase(fd); 75 | } 76 | #endif 77 | } 78 | 79 | bool EpollTaskScheduler::HandleEvent(int timeout) 80 | { 81 | #if defined(__linux) || defined(__linux__) 82 | struct epoll_event events[512] = {0}; 83 | int num_events = -1; 84 | 85 | num_events = epoll_wait(epollfd_, events, 512, timeout); 86 | if(num_events < 0) { 87 | if(errno != EINTR) { 88 | return false; 89 | } 90 | } 91 | 92 | for(int n=0; nHandleEvent(events[n].events); 95 | } 96 | } 97 | return true; 98 | #else 99 | return false; 100 | #endif 101 | } 102 | 103 | 104 | -------------------------------------------------------------------------------- /rtsp/src/net/EpollTaskScheduler.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #ifndef XOP_EPOLL_TASK_SCHEDULER_H 5 | #define XOP_EPOLL_TASK_SCHEDULER_H 6 | 7 | #include "TaskScheduler.h" 8 | #include 9 | #include 10 | 11 | namespace xop 12 | { 13 | class EpollTaskScheduler : public TaskScheduler 14 | { 15 | public: 16 | EpollTaskScheduler(int id = 0); 17 | virtual ~EpollTaskScheduler(); 18 | 19 | void UpdateChannel(ChannelPtr channel); 20 | void RemoveChannel(ChannelPtr& channel); 21 | 22 | // timeout: ms 23 | bool HandleEvent(int timeout); 24 | 25 | private: 26 | void Update(int operation, ChannelPtr& channel); 27 | 28 | int epollfd_ = -1; 29 | std::mutex mutex_; 30 | std::unordered_map channels_; 31 | }; 32 | 33 | } 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /rtsp/src/net/EventLoop.cpp: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2019-10-18 3 | 4 | #include "EventLoop.h" 5 | 6 | #if defined(WIN32) || defined(_WIN32) 7 | #include 8 | #endif 9 | 10 | #if defined(WIN32) || defined(_WIN32) 11 | #pragma comment(lib, "Ws2_32.lib") 12 | #pragma comment(lib,"Iphlpapi.lib") 13 | #endif 14 | 15 | using namespace xop; 16 | 17 | EventLoop::EventLoop(uint32_t num_threads) 18 | : index_(1) 19 | { 20 | num_threads_ = 1; 21 | if (num_threads > 0) { 22 | num_threads_ = num_threads; 23 | } 24 | 25 | this->Loop(); 26 | } 27 | 28 | EventLoop::~EventLoop() 29 | { 30 | this->Quit(); 31 | } 32 | 33 | std::shared_ptr EventLoop::GetTaskScheduler() 34 | { 35 | std::lock_guard locker(mutex_); 36 | if (task_schedulers_.size() == 1) { 37 | return task_schedulers_.at(0); 38 | } 39 | else { 40 | auto task_scheduler = task_schedulers_.at(index_); 41 | index_++; 42 | if (index_ >= task_schedulers_.size()) { 43 | index_ = 1; 44 | } 45 | return task_scheduler; 46 | } 47 | 48 | return nullptr; 49 | } 50 | 51 | void EventLoop::Loop() 52 | { 53 | std::lock_guard locker(mutex_); 54 | 55 | if (!task_schedulers_.empty()) { 56 | return ; 57 | } 58 | 59 | for (uint32_t n = 0; n < num_threads_; n++) 60 | { 61 | #if defined(__linux) || defined(__linux__) 62 | std::shared_ptr task_scheduler_ptr(new EpollTaskScheduler(n)); 63 | #elif defined(WIN32) || defined(_WIN32) 64 | std::shared_ptr task_scheduler_ptr(new SelectTaskScheduler(n)); 65 | #endif 66 | task_schedulers_.push_back(task_scheduler_ptr); 67 | std::shared_ptr thread(new std::thread(&TaskScheduler::Start, task_scheduler_ptr.get())); 68 | thread->native_handle(); 69 | threads_.push_back(thread); 70 | } 71 | 72 | const int priority = TASK_SCHEDULER_PRIORITY_REALTIME; 73 | 74 | for (auto iter : threads_) 75 | { 76 | #if defined(__linux) || defined(__linux__) 77 | 78 | #elif defined(WIN32) || defined(_WIN32) 79 | switch (priority) 80 | { 81 | case TASK_SCHEDULER_PRIORITY_LOW: 82 | SetThreadPriority(iter->native_handle(), THREAD_PRIORITY_BELOW_NORMAL); 83 | break; 84 | case TASK_SCHEDULER_PRIORITY_NORMAL: 85 | SetThreadPriority(iter->native_handle(), THREAD_PRIORITY_NORMAL); 86 | break; 87 | case TASK_SCHEDULER_PRIORITYO_HIGH: 88 | SetThreadPriority(iter->native_handle(), THREAD_PRIORITY_ABOVE_NORMAL); 89 | break; 90 | case TASK_SCHEDULER_PRIORITY_HIGHEST: 91 | SetThreadPriority(iter->native_handle(), THREAD_PRIORITY_HIGHEST); 92 | break; 93 | case TASK_SCHEDULER_PRIORITY_REALTIME: 94 | SetThreadPriority(iter->native_handle(), THREAD_PRIORITY_TIME_CRITICAL); 95 | break; 96 | } 97 | #endif 98 | } 99 | } 100 | 101 | void EventLoop::Quit() 102 | { 103 | std::lock_guard locker(mutex_); 104 | 105 | for (auto iter : task_schedulers_) { 106 | iter->Stop(); 107 | } 108 | 109 | for (auto iter : threads_) { 110 | iter->join(); 111 | } 112 | 113 | task_schedulers_.clear(); 114 | threads_.clear(); 115 | } 116 | 117 | void EventLoop::UpdateChannel(ChannelPtr channel) 118 | { 119 | std::lock_guard locker(mutex_); 120 | if (task_schedulers_.size() > 0) { 121 | task_schedulers_[0]->UpdateChannel(channel); 122 | } 123 | } 124 | 125 | void EventLoop::RemoveChannel(ChannelPtr& channel) 126 | { 127 | std::lock_guard locker(mutex_); 128 | if (task_schedulers_.size() > 0) { 129 | task_schedulers_[0]->RemoveChannel(channel); 130 | } 131 | } 132 | 133 | TimerId EventLoop::AddTimer(TimerEvent timerEvent, uint32_t msec) 134 | { 135 | std::lock_guard locker(mutex_); 136 | if (task_schedulers_.size() > 0) { 137 | return task_schedulers_[0]->AddTimer(timerEvent, msec); 138 | } 139 | return 0; 140 | } 141 | 142 | void EventLoop::RemoveTimer(TimerId timerId) 143 | { 144 | std::lock_guard locker(mutex_); 145 | if (task_schedulers_.size() > 0) { 146 | task_schedulers_[0]->RemoveTimer(timerId); 147 | } 148 | } 149 | 150 | bool EventLoop::AddTriggerEvent(TriggerEvent callback) 151 | { 152 | std::lock_guard locker(mutex_); 153 | if (task_schedulers_.size() > 0) { 154 | return task_schedulers_[0]->AddTriggerEvent(callback); 155 | } 156 | return false; 157 | } 158 | -------------------------------------------------------------------------------- /rtsp/src/net/EventLoop.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #ifndef XOP_EVENT_LOOP_H 5 | #define XOP_EVENT_LOOP_H 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "SelectTaskScheduler.h" 16 | #include "EpollTaskScheduler.h" 17 | #include "Pipe.h" 18 | #include "Timer.h" 19 | #include "RingBuffer.h" 20 | 21 | #define TASK_SCHEDULER_PRIORITY_LOW 0 22 | #define TASK_SCHEDULER_PRIORITY_NORMAL 1 23 | #define TASK_SCHEDULER_PRIORITYO_HIGH 2 24 | #define TASK_SCHEDULER_PRIORITY_HIGHEST 3 25 | #define TASK_SCHEDULER_PRIORITY_REALTIME 4 26 | 27 | namespace xop 28 | { 29 | 30 | class EventLoop 31 | { 32 | public: 33 | EventLoop(const EventLoop&) = delete; 34 | EventLoop &operator = (const EventLoop&) = delete; 35 | EventLoop(uint32_t num_threads =1); //std::thread::hardware_concurrency() 36 | virtual ~EventLoop(); 37 | 38 | std::shared_ptr GetTaskScheduler(); 39 | 40 | bool AddTriggerEvent(TriggerEvent callback); 41 | TimerId AddTimer(TimerEvent timerEvent, uint32_t msec); 42 | void RemoveTimer(TimerId timerId); 43 | void UpdateChannel(ChannelPtr channel); 44 | void RemoveChannel(ChannelPtr& channel); 45 | 46 | void Loop(); 47 | void Quit(); 48 | 49 | private: 50 | std::mutex mutex_; 51 | uint32_t num_threads_ = 1; 52 | uint32_t index_ = 1; 53 | std::vector> task_schedulers_; 54 | std::vector> threads_; 55 | }; 56 | 57 | } 58 | 59 | #endif 60 | 61 | -------------------------------------------------------------------------------- /rtsp/src/net/Logger.cpp: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #if defined(WIN32) || defined(_WIN32) 5 | #ifndef _CRT_SECURE_NO_WARNINGS 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #endif 8 | #endif 9 | 10 | #include "Logger.h" 11 | #include "Timestamp.h" 12 | #include 13 | #include 14 | 15 | using namespace xop; 16 | 17 | const char* Priority_To_String[] = 18 | { 19 | "DEBUG", 20 | "CONFIG", 21 | "INFO", 22 | "WARNING", 23 | "ERROR" 24 | }; 25 | 26 | Logger::Logger() 27 | { 28 | 29 | } 30 | 31 | Logger& Logger::Instance() 32 | { 33 | static Logger s_logger; 34 | return s_logger; 35 | } 36 | 37 | Logger::~Logger() 38 | { 39 | 40 | } 41 | 42 | void Logger::Init(char *pathname) 43 | { 44 | std::unique_lock lock(mutex_); 45 | 46 | if (pathname != nullptr) { 47 | ofs_.open(pathname, std::ios::out | std::ios::binary); 48 | if (ofs_.fail()) 49 | { 50 | std::cerr << "Failed to open logfile." << std::endl; 51 | } 52 | } 53 | } 54 | 55 | void Logger::Exit() 56 | { 57 | std::unique_lock lock(mutex_); 58 | 59 | if (ofs_.is_open()) { 60 | ofs_.close(); 61 | } 62 | } 63 | 64 | void Logger::Log(Priority priority, const char* __file, const char* __func, int __line, const char *fmt, ...) 65 | { 66 | std::unique_lock lock(mutex_); 67 | 68 | char buf[2048] = {0}; 69 | sprintf(buf, "[%s][%s:%s:%d] ", Priority_To_String[priority], __file, __func, __line); 70 | va_list args; 71 | va_start(args, fmt); 72 | vsprintf(buf + strlen(buf), fmt, args); 73 | va_end(args); 74 | this->Write(std::string(buf)); 75 | } 76 | 77 | void Logger::Log2(Priority priority, const char *fmt, ...) 78 | { 79 | std::unique_lock lock(mutex_); 80 | 81 | char buf[4096] = { 0 }; 82 | sprintf(buf, "[%s] ", Priority_To_String[priority]); 83 | va_list args; 84 | va_start(args, fmt); 85 | vsprintf(buf + strlen(buf), fmt, args); 86 | va_end(args); 87 | this->Write(std::string(buf)); 88 | } 89 | 90 | void Logger::Write(std::string info) 91 | { 92 | if (ofs_.is_open()) { 93 | ofs_ << "[" << Timestamp::Localtime() << "]" 94 | << info << std::endl; 95 | } 96 | 97 | std::cout << "[" << Timestamp::Localtime() << "]" 98 | << info << std::endl; 99 | } 100 | -------------------------------------------------------------------------------- /rtsp/src/net/Logger.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2020-5-15 3 | 4 | #ifndef XOP_LOGGER_H 5 | #define XOP_LOGGER_H 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | namespace xop { 16 | 17 | 18 | enum Priority 19 | { 20 | LOG_DEBUG, LOG_STATE, LOG_INFO, LOG_WARNING, LOG_ERROR, 21 | }; 22 | 23 | class Logger 24 | { 25 | public: 26 | Logger &operator=(const Logger &) = delete; 27 | Logger(const Logger &) = delete; 28 | static Logger& Instance(); 29 | ~Logger(); 30 | 31 | void Init(char *pathname = nullptr); 32 | void Exit(); 33 | 34 | void Log(Priority priority, const char* __file, const char* __func, int __line, const char *fmt, ...); 35 | void Log2(Priority priority, const char *fmt, ...); 36 | 37 | private: 38 | void Write(std::string buf); 39 | Logger(); 40 | 41 | std::mutex mutex_; 42 | std::ofstream ofs_; 43 | }; 44 | 45 | } 46 | 47 | #ifdef _DEBUG 48 | #define LOG_DEBUG(fmt, ...) xop::Logger::Instance().Log(LOG_DEBUG, __FILE__, __FUNCTION__,__LINE__, fmt, ##__VA_ARGS__) 49 | #else 50 | #define LOG_DEBUG(fmt, ...) 51 | #endif 52 | #define LOG_INFO(fmt, ...) xop::Logger::Instance().Log2(LOG_INFO, fmt, ##__VA_ARGS__) 53 | #define LOG_ERROR(fmt, ...) xop::Logger::Instance().Log(LOG_ERROR, __FILE__, __FUNCTION__,__LINE__, fmt, ##__VA_ARGS__) 54 | 55 | #endif 56 | 57 | -------------------------------------------------------------------------------- /rtsp/src/net/MemoryManager.cpp: -------------------------------------------------------------------------------- 1 | #include "MemoryManager.h" 2 | 3 | using namespace xop; 4 | 5 | void* xop::Alloc(uint32_t size) 6 | { 7 | return MemoryManager::Instance().Alloc(size); 8 | } 9 | 10 | void xop::Free(void *ptr) 11 | { 12 | return MemoryManager::Instance().Free(ptr); 13 | } 14 | 15 | MemoryPool::MemoryPool() 16 | { 17 | 18 | } 19 | 20 | MemoryPool::~MemoryPool() 21 | { 22 | if (memory_) { 23 | free(memory_); 24 | } 25 | } 26 | 27 | void MemoryPool::Init(uint32_t size, uint32_t n) 28 | { 29 | if (memory_) { 30 | return; 31 | } 32 | 33 | block_size_ = size; 34 | num_blocks_ = n; 35 | memory_ = (char*)malloc(num_blocks_ * (block_size_ + sizeof(MemoryBlock))); 36 | head_ = (MemoryBlock*)memory_; 37 | head_->block_id = 1; 38 | head_->pool = this; 39 | head_->next = nullptr; 40 | 41 | MemoryBlock* current = head_; 42 | for (uint32_t n = 1; n < num_blocks_; n++) { 43 | MemoryBlock* next = (MemoryBlock*)(memory_ + (n * (block_size_ + sizeof(MemoryBlock)))); 44 | next->block_id = n + 1; 45 | next->pool = this; 46 | next->next = nullptr; 47 | 48 | current->next = next; 49 | current = next; 50 | } 51 | } 52 | 53 | void* MemoryPool::Alloc(uint32_t size) 54 | { 55 | std::lock_guard locker(mutex_); 56 | if (head_ != nullptr) { 57 | MemoryBlock* block = head_; 58 | head_ = head_->next; 59 | return ((char*)block + sizeof(MemoryBlock)); 60 | } 61 | 62 | return nullptr; 63 | } 64 | 65 | void MemoryPool::Free(void* ptr) 66 | { 67 | MemoryBlock *block = (MemoryBlock*)((char*)ptr - sizeof(MemoryBlock)); 68 | if (block->block_id != 0) { 69 | std::lock_guard locker(mutex_); 70 | block->next = head_; 71 | head_ = block; 72 | } 73 | } 74 | 75 | MemoryManager::MemoryManager() 76 | { 77 | memory_pools_[0].Init(4096, 50); 78 | memory_pools_[1].Init(40960, 10); 79 | memory_pools_[2].Init(102400, 5); 80 | //memory_pools_[3].Init(204800, 2); 81 | } 82 | 83 | MemoryManager::~MemoryManager() 84 | { 85 | 86 | } 87 | 88 | MemoryManager& MemoryManager::Instance() 89 | { 90 | static MemoryManager s_mgr; 91 | return s_mgr; 92 | } 93 | 94 | void* MemoryManager::Alloc(uint32_t size) 95 | { 96 | for (int n = 0; n < kMaxMemoryPool; n++) { 97 | if (size <= memory_pools_[n].BolckSize()) { 98 | void* ptr = memory_pools_[n].Alloc(size); 99 | if (ptr != nullptr) { 100 | return ptr; 101 | } 102 | else { 103 | break; 104 | } 105 | } 106 | } 107 | 108 | MemoryBlock *block = (MemoryBlock*)malloc(size + sizeof(MemoryBlock)); 109 | block->block_id = 0; 110 | block->pool = nullptr; 111 | block->next = nullptr; 112 | return ((char*)block + sizeof(MemoryBlock)); 113 | } 114 | 115 | void MemoryManager::Free(void* ptr) 116 | { 117 | MemoryBlock *block = (MemoryBlock*)((char*)ptr - sizeof(MemoryBlock)); 118 | MemoryPool *pool = block->pool; 119 | 120 | if (pool != nullptr && block->block_id > 0) { 121 | pool->Free(ptr); 122 | } 123 | else { 124 | ::free(block); 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /rtsp/src/net/MemoryManager.h: -------------------------------------------------------------------------------- 1 | #ifndef XOP_MEMMORY_MANAGER_H 2 | #define XOP_MEMMORY_MANAGER_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | namespace xop 10 | { 11 | 12 | void* Alloc(uint32_t size); 13 | void Free(void *ptr); 14 | 15 | class MemoryPool; 16 | 17 | struct MemoryBlock 18 | { 19 | uint32_t block_id = 0; 20 | MemoryPool *pool = nullptr; 21 | MemoryBlock *next = nullptr; 22 | }; 23 | 24 | class MemoryPool 25 | { 26 | public: 27 | MemoryPool(); 28 | virtual ~MemoryPool(); 29 | 30 | void Init(uint32_t size, uint32_t n); 31 | void* Alloc(uint32_t size); 32 | void Free(void* ptr); 33 | 34 | size_t BolckSize() const 35 | { return block_size_; } 36 | 37 | //private: 38 | char* memory_ = nullptr; 39 | uint32_t block_size_ = 0; 40 | uint32_t num_blocks_ = 0; 41 | MemoryBlock* head_ = nullptr; 42 | std::mutex mutex_; 43 | }; 44 | 45 | class MemoryManager 46 | { 47 | public: 48 | static MemoryManager& Instance(); 49 | ~MemoryManager(); 50 | 51 | void* Alloc(uint32_t size); 52 | void Free(void* ptr); 53 | 54 | private: 55 | MemoryManager(); 56 | 57 | static const int kMaxMemoryPool = 3; 58 | MemoryPool memory_pools_[kMaxMemoryPool]; 59 | }; 60 | 61 | } 62 | #endif 63 | -------------------------------------------------------------------------------- /rtsp/src/net/NetInterface.cpp: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #include "NetInterface.h" 5 | #include "Socket.h" 6 | 7 | using namespace xop; 8 | 9 | std::string NetInterface::GetLocalIPAddress() 10 | { 11 | #if defined(__linux) || defined(__linux__) 12 | SOCKET sockfd = 0; 13 | char buf[512] = { 0 }; 14 | struct ifconf ifconf; 15 | struct ifreq *ifreq; 16 | sockfd = socket(AF_INET, SOCK_DGRAM, 0); 17 | if (sockfd == INVALID_SOCKET) 18 | { 19 | close(sockfd); 20 | return "0.0.0.0"; 21 | } 22 | 23 | ifconf.ifc_len = 512; 24 | ifconf.ifc_buf = buf; 25 | if (ioctl(sockfd, SIOCGIFCONF, &ifconf) < 0) 26 | { 27 | close(sockfd); 28 | return "0.0.0.0"; 29 | } 30 | 31 | close(sockfd); 32 | 33 | ifreq = (struct ifreq*)ifconf.ifc_buf; 34 | for (int i = (ifconf.ifc_len / sizeof(struct ifreq)); i>0; i--) 35 | { 36 | if (ifreq->ifr_flags == AF_INET) 37 | { 38 | if (strcmp(ifreq->ifr_name, "lo") != 0) 39 | { 40 | return inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr); 41 | } 42 | ifreq++; 43 | } 44 | } 45 | return "0.0.0.0"; 46 | #elif defined(WIN32) || defined(_WIN32) 47 | PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO(); 48 | unsigned long size = sizeof(IP_ADAPTER_INFO); 49 | 50 | int ret = GetAdaptersInfo(pIpAdapterInfo, &size); 51 | if (ret == ERROR_BUFFER_OVERFLOW) 52 | { 53 | delete pIpAdapterInfo; 54 | pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[size]; 55 | ret = GetAdaptersInfo(pIpAdapterInfo, &size); 56 | } 57 | 58 | if (ret != ERROR_SUCCESS) 59 | { 60 | delete pIpAdapterInfo; 61 | return "0.0.0.0"; 62 | } 63 | 64 | while (pIpAdapterInfo) 65 | { 66 | IP_ADDR_STRING *pIpAddrString = &(pIpAdapterInfo->IpAddressList); 67 | while(pIpAddrString) 68 | { 69 | if (strcmp(pIpAddrString->IpAddress.String, "127.0.0.1")!=0 70 | && strcmp(pIpAddrString->IpAddress.String, "0.0.0.0")!=0) 71 | { 72 | // pIpAddrString->IpMask.String 73 | //pIpAdapterInfo->GatewayList.IpAddress.String 74 | std::string ip(pIpAddrString->IpAddress.String); 75 | //delete pIpAdapterInfo; 76 | return ip; 77 | } 78 | pIpAddrString = pIpAddrString->Next; 79 | } while (pIpAddrString); 80 | pIpAdapterInfo = pIpAdapterInfo->Next; 81 | } 82 | 83 | delete pIpAdapterInfo; 84 | return "0.0.0.0"; 85 | #else 86 | return "0.0.0.0"; 87 | #endif 88 | } 89 | 90 | 91 | -------------------------------------------------------------------------------- /rtsp/src/net/NetInterface.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #ifndef XOP_NET_INTERFACE_H 5 | #define XOP_NET_INTERFACE_H 6 | 7 | #include 8 | 9 | namespace xop { 10 | 11 | class NetInterface 12 | { 13 | public: 14 | static std::string GetLocalIPAddress(); 15 | }; 16 | 17 | } 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /rtsp/src/net/Pipe.cpp: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #include "Pipe.h" 5 | #include "SocketUtil.h" 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace xop; 11 | 12 | Pipe::Pipe() 13 | { 14 | 15 | } 16 | 17 | bool Pipe::Create() 18 | { 19 | #if defined(WIN32) || defined(_WIN32) 20 | TcpSocket rp(socket(AF_INET, SOCK_STREAM, 0)), wp(socket(AF_INET, SOCK_STREAM, 0)); 21 | std::random_device rd; 22 | 23 | pipe_fd_[0] = rp.GetSocket(); 24 | pipe_fd_[1] = wp.GetSocket(); 25 | uint16_t port = 0; 26 | int again = 5; 27 | 28 | while(again--) { 29 | port = rd(); 30 | if (rp.Bind("127.0.0.1", port)) { 31 | break; 32 | } 33 | } 34 | 35 | if (again == 0) { 36 | return false; 37 | } 38 | 39 | if (!rp.Listen(1)) { 40 | return false; 41 | } 42 | 43 | if (!wp.Connect("127.0.0.1", port)) { 44 | return false; 45 | } 46 | 47 | pipe_fd_[0] = rp.Accept(); 48 | if (pipe_fd_[0] < 0) { 49 | return false; 50 | } 51 | 52 | SocketUtil::SetNonBlock(pipe_fd_[0]); 53 | SocketUtil::SetNonBlock(pipe_fd_[1]); 54 | #elif defined(__linux) || defined(__linux__) 55 | if (pipe2(pipe_fd_, O_NONBLOCK | O_CLOEXEC) < 0) { 56 | return false; 57 | } 58 | #endif 59 | return true; 60 | } 61 | 62 | int Pipe::Write(void *buf, int len) 63 | { 64 | #if defined(WIN32) || defined(_WIN32) 65 | return ::send(pipe_fd_[1], (char *)buf, len, 0); 66 | #elif defined(__linux) || defined(__linux__) 67 | return ::write(pipe_fd_[1], buf, len); 68 | #endif 69 | } 70 | 71 | int Pipe::Read(void *buf, int len) 72 | { 73 | #if defined(WIN32) || defined(_WIN32) 74 | return recv(pipe_fd_[0], (char *)buf, len, 0); 75 | #elif defined(__linux) || defined(__linux__) 76 | return ::read(pipe_fd_[0], buf, len); 77 | #endif 78 | } 79 | 80 | void Pipe::Close() 81 | { 82 | #if defined(WIN32) || defined(_WIN32) 83 | closesocket(pipe_fd_[0]); 84 | closesocket(pipe_fd_[1]); 85 | #elif defined(__linux) || defined(__linux__) 86 | ::close(pipe_fd_[0]); 87 | ::close(pipe_fd_[1]); 88 | #endif 89 | 90 | } 91 | -------------------------------------------------------------------------------- /rtsp/src/net/Pipe.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #ifndef XOP_PIPE_H 5 | #define XOP_PIPE_H 6 | 7 | #include "TcpSocket.h" 8 | 9 | namespace xop 10 | { 11 | 12 | class Pipe 13 | { 14 | public: 15 | Pipe(); 16 | bool Create(); 17 | int Write(void *buf, int len); 18 | int Read(void *buf, int len); 19 | void Close(); 20 | 21 | SOCKET Read() const { return pipe_fd_[0]; } 22 | SOCKET Write() const { return pipe_fd_[1]; } 23 | 24 | private: 25 | SOCKET pipe_fd_[2]; 26 | }; 27 | 28 | } 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /rtsp/src/net/RingBuffer.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #ifndef XOP_RING_BUFFER_H 5 | #define XOP_RING_BUFFER_H 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | namespace xop 13 | { 14 | 15 | template 16 | class RingBuffer 17 | { 18 | public: 19 | RingBuffer(int capacity=60) 20 | : capacity_(capacity) 21 | , num_datas_(0) 22 | , buffer_(capacity) 23 | { } 24 | 25 | virtual ~RingBuffer() { } 26 | 27 | bool Push(const T& data) 28 | { 29 | return pushData(std::forward(data)); 30 | } 31 | 32 | bool Push(T&& data) 33 | { 34 | return PushData(data); 35 | } 36 | 37 | bool Pop(T& data) 38 | { 39 | if(num_datas_ > 0) { 40 | data = std::move(buffer_[get_pos_]); 41 | Add(get_pos_); 42 | num_datas_--; 43 | return true; 44 | } 45 | 46 | return false; 47 | } 48 | 49 | bool IsFull() const 50 | { 51 | return ((num_datas_==capacity_) ? true : false); 52 | } 53 | 54 | bool IsEmpty() const 55 | { 56 | return ((num_datas_==0) ? true : false); 57 | } 58 | 59 | int Size() const 60 | { 61 | return num_datas_; 62 | } 63 | 64 | private: 65 | template 66 | bool PushData(F&& data) 67 | { 68 | if (num_datas_ < capacity_) 69 | { 70 | buffer_[put_pos_] = std::forward(data); 71 | Add(put_pos_); 72 | num_datas_++; 73 | return true; 74 | } 75 | 76 | return false; 77 | } 78 | 79 | void Add(int& pos) 80 | { 81 | pos = (((pos+1)==capacity_) ? 0 : (pos+1)); 82 | } 83 | 84 | int capacity_ = 0; 85 | int put_pos_ = 0; 86 | int get_pos_ = 0; 87 | 88 | std::atomic_int num_datas_; 89 | std::vector buffer_; 90 | }; 91 | 92 | } 93 | 94 | #endif 95 | 96 | 97 | -------------------------------------------------------------------------------- /rtsp/src/net/SelectTaskScheduler.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #ifndef XOP_SELECT_TASK_SCHEDULER_H 5 | #define XOP_SELECT_TASK_SCHEDULER_H 6 | 7 | #include "TaskScheduler.h" 8 | #include "Socket.h" 9 | #include 10 | #include 11 | 12 | #if defined(__linux) || defined(__linux__) 13 | #include 14 | #include 15 | #include 16 | #include 17 | #endif 18 | 19 | namespace xop 20 | { 21 | 22 | class SelectTaskScheduler : public TaskScheduler 23 | { 24 | public: 25 | SelectTaskScheduler(int id = 0); 26 | virtual ~SelectTaskScheduler(); 27 | 28 | void UpdateChannel(ChannelPtr channel); 29 | void RemoveChannel(ChannelPtr& channel); 30 | bool HandleEvent(int timeout); 31 | 32 | private: 33 | fd_set fd_read_backup_; 34 | fd_set fd_write_backup_; 35 | fd_set fd_exp_backup_; 36 | SOCKET maxfd_ = 0; 37 | 38 | bool is_fd_read_reset_ = false; 39 | bool is_fd_write_reset_ = false; 40 | bool is_fd_exp_reset_ = false; 41 | 42 | std::mutex mutex_; 43 | std::unordered_map channels_; 44 | }; 45 | 46 | } 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /rtsp/src/net/Socket.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #ifndef XOP_SOCKET_H 5 | #define XOP_SOCKET_H 6 | 7 | #if defined(__linux) || defined(__linux__) 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #define SOCKET int 25 | #define INVALID_SOCKET (-1) 26 | #define SOCKET_ERROR (-1) 27 | 28 | #elif defined(WIN32) || defined(_WIN32) 29 | #define FD_SETSIZE 1024 30 | #define WIN32_LEAN_AND_MEAN 31 | #define _WINSOCK_DEPRECATED_NO_WARNINGS 32 | #include 33 | #include 34 | #include 35 | #include 36 | #define SHUT_RD 0 37 | #define SHUT_WR 1 38 | #define SHUT_RDWR 2 39 | 40 | #else 41 | 42 | #endif 43 | 44 | #include 45 | #include 46 | 47 | #endif // _XOP_SOCKET_H 48 | -------------------------------------------------------------------------------- /rtsp/src/net/SocketUtil.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #ifndef XOP_SOCKET_UTIL_H 5 | #define XOP_SOCKET_UTIL_H 6 | 7 | #include "Socket.h" 8 | #include 9 | 10 | namespace xop 11 | { 12 | 13 | class SocketUtil 14 | { 15 | public: 16 | static bool Bind(SOCKET sockfd, std::string ip, uint16_t port); 17 | static void SetNonBlock(SOCKET fd); 18 | static void SetBlock(SOCKET fd, int write_timeout=0); 19 | static void SetReuseAddr(SOCKET fd); 20 | static void SetReusePort(SOCKET sockfd); 21 | static void SetNoDelay(SOCKET sockfd); 22 | static void SetKeepAlive(SOCKET sockfd); 23 | static void SetNoSigpipe(SOCKET sockfd); 24 | static void SetSendBufSize(SOCKET sockfd, int size); 25 | static void SetRecvBufSize(SOCKET sockfd, int size); 26 | static std::string GetPeerIp(SOCKET sockfd); 27 | static std::string GetSocketIp(SOCKET sockfd); 28 | static int GetSocketAddr(SOCKET sockfd, struct sockaddr_in* addr); 29 | static uint16_t GetPeerPort(SOCKET sockfd); 30 | static int GetPeerAddr(SOCKET sockfd, struct sockaddr_in *addr); 31 | static void Close(SOCKET sockfd); 32 | static bool Connect(SOCKET sockfd, std::string ip, uint16_t port, int timeout=0); 33 | }; 34 | 35 | } 36 | 37 | #endif // _SOCKET_UTIL_H 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /rtsp/src/net/TaskScheduler.cpp: -------------------------------------------------------------------------------- 1 | #include "TaskScheduler.h" 2 | #if defined(__linux) || defined(__linux__) 3 | #include 4 | #endif 5 | 6 | using namespace xop; 7 | 8 | TaskScheduler::TaskScheduler(int id) 9 | : id_(id) 10 | , is_shutdown_(false) 11 | , wakeup_pipe_(new Pipe()) 12 | , trigger_events_(new xop::RingBuffer(kMaxTriggetEvents)) 13 | { 14 | static std::once_flag flag; 15 | std::call_once(flag, [] { 16 | #if defined(WIN32) || defined(_WIN32) 17 | WSADATA wsa_data; 18 | if (WSAStartup(MAKEWORD(2, 2), &wsa_data)) { 19 | WSACleanup(); 20 | } 21 | #endif 22 | }); 23 | 24 | if (wakeup_pipe_->Create()) { 25 | wakeup_channel_.reset(new Channel(wakeup_pipe_->Read())); 26 | wakeup_channel_->EnableReading(); 27 | wakeup_channel_->SetReadCallback([this]() { this->Wake(); }); 28 | } 29 | } 30 | 31 | TaskScheduler::~TaskScheduler() 32 | { 33 | 34 | } 35 | 36 | void TaskScheduler::Start() 37 | { 38 | #if defined(__linux) || defined(__linux__) 39 | signal(SIGPIPE, SIG_IGN); 40 | signal(SIGQUIT, SIG_IGN); 41 | signal(SIGUSR1, SIG_IGN); 42 | signal(SIGTERM, SIG_IGN); 43 | signal(SIGKILL, SIG_IGN); 44 | #endif 45 | is_shutdown_ = false; 46 | while (!is_shutdown_) { 47 | this->HandleTriggerEvent(); 48 | this->timer_queue_.HandleTimerEvent(); 49 | int64_t timeout = this->timer_queue_.GetTimeRemaining(); 50 | this->HandleEvent((int)timeout); 51 | } 52 | } 53 | 54 | void TaskScheduler::Stop() 55 | { 56 | is_shutdown_ = true; 57 | char event = kTriggetEvent; 58 | wakeup_pipe_->Write(&event, 1); 59 | } 60 | 61 | TimerId TaskScheduler::AddTimer(TimerEvent timerEvent, uint32_t msec) 62 | { 63 | TimerId id = timer_queue_.AddTimer(timerEvent, msec); 64 | return id; 65 | } 66 | 67 | void TaskScheduler::RemoveTimer(TimerId timerId) 68 | { 69 | timer_queue_.RemoveTimer(timerId); 70 | } 71 | 72 | bool TaskScheduler::AddTriggerEvent(TriggerEvent callback) 73 | { 74 | if (trigger_events_->Size() < kMaxTriggetEvents) { 75 | std::lock_guard lock(mutex_); 76 | char event = kTriggetEvent; 77 | trigger_events_->Push(std::move(callback)); 78 | wakeup_pipe_->Write(&event, 1); 79 | return true; 80 | } 81 | 82 | return false; 83 | } 84 | 85 | void TaskScheduler::Wake() 86 | { 87 | char event[10] = { 0 }; 88 | while (wakeup_pipe_->Read(event, 10) > 0); 89 | } 90 | 91 | void TaskScheduler::HandleTriggerEvent() 92 | { 93 | do 94 | { 95 | TriggerEvent callback; 96 | if (trigger_events_->Pop(callback)) { 97 | callback(); 98 | } 99 | } while (trigger_events_->Size() > 0); 100 | } 101 | -------------------------------------------------------------------------------- /rtsp/src/net/TaskScheduler.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #ifndef XOP_TASK_SCHEDULER_H 5 | #define XOP_TASK_SCHEDULER_H 6 | 7 | #include "Channel.h" 8 | #include "Pipe.h" 9 | #include "Timer.h" 10 | #include "RingBuffer.h" 11 | 12 | namespace xop 13 | { 14 | 15 | typedef std::function TriggerEvent; 16 | 17 | class TaskScheduler 18 | { 19 | public: 20 | TaskScheduler(int id=1); 21 | virtual ~TaskScheduler(); 22 | 23 | void Start(); 24 | void Stop(); 25 | TimerId AddTimer(TimerEvent timerEvent, uint32_t msec); 26 | void RemoveTimer(TimerId timerId); 27 | bool AddTriggerEvent(TriggerEvent callback); 28 | 29 | virtual void UpdateChannel(ChannelPtr channel) { }; 30 | virtual void RemoveChannel(ChannelPtr& channel) { }; 31 | virtual bool HandleEvent(int timeout) { return false; }; 32 | 33 | int GetId() const 34 | { return id_; } 35 | 36 | protected: 37 | void Wake(); 38 | void HandleTriggerEvent(); 39 | 40 | int id_ = 0; 41 | std::atomic_bool is_shutdown_; 42 | std::unique_ptr wakeup_pipe_; 43 | std::shared_ptr wakeup_channel_; 44 | std::unique_ptr> trigger_events_; 45 | 46 | std::mutex mutex_; 47 | TimerQueue timer_queue_; 48 | 49 | static const char kTriggetEvent = 1; 50 | static const char kTimerEvent = 2; 51 | static const int kMaxTriggetEvents = 50000; 52 | }; 53 | 54 | } 55 | #endif 56 | 57 | -------------------------------------------------------------------------------- /rtsp/src/net/TcpConnection.cpp: -------------------------------------------------------------------------------- 1 | #include "TcpConnection.h" 2 | #include "SocketUtil.h" 3 | 4 | using namespace xop; 5 | 6 | TcpConnection::TcpConnection(TaskScheduler *task_scheduler, SOCKET sockfd) 7 | : task_scheduler_(task_scheduler) 8 | , read_buffer_(new BufferReader) 9 | , write_buffer_(new BufferWriter(500)) 10 | , channel_(new Channel(sockfd)) 11 | { 12 | is_closed_ = false; 13 | 14 | channel_->SetReadCallback([this]() { this->HandleRead(); }); 15 | channel_->SetWriteCallback([this]() { this->HandleWrite(); }); 16 | channel_->SetCloseCallback([this]() { this->HandleClose(); }); 17 | channel_->SetErrorCallback([this]() { this->HandleError(); }); 18 | 19 | SocketUtil::SetNonBlock(sockfd); 20 | SocketUtil::SetSendBufSize(sockfd, 100 * 1024); 21 | SocketUtil::SetKeepAlive(sockfd); 22 | 23 | channel_->EnableReading(); 24 | task_scheduler_->UpdateChannel(channel_); 25 | } 26 | 27 | TcpConnection::~TcpConnection() 28 | { 29 | SOCKET fd = channel_->GetSocket(); 30 | if (fd > 0) { 31 | SocketUtil::Close(fd); 32 | } 33 | } 34 | 35 | void TcpConnection::Send(std::shared_ptr data, uint32_t size) 36 | { 37 | if (!is_closed_) { 38 | mutex_.lock(); 39 | write_buffer_->Append(data, size); 40 | mutex_.unlock(); 41 | 42 | this->HandleWrite(); 43 | } 44 | } 45 | 46 | void TcpConnection::Send(const char *data, uint32_t size) 47 | { 48 | if (!is_closed_) { 49 | mutex_.lock(); 50 | write_buffer_->Append(data, size); 51 | mutex_.unlock(); 52 | 53 | this->HandleWrite(); 54 | } 55 | } 56 | 57 | void TcpConnection::Disconnect() 58 | { 59 | std::lock_guard lock(mutex_); 60 | auto conn = shared_from_this(); 61 | task_scheduler_->AddTriggerEvent([conn]() { 62 | conn->Close(); 63 | }); 64 | } 65 | 66 | void TcpConnection::HandleRead() 67 | { 68 | { 69 | std::lock_guard lock(mutex_); 70 | 71 | if (is_closed_) { 72 | return; 73 | } 74 | 75 | int ret = read_buffer_->Read(channel_->GetSocket()); 76 | if (ret <= 0) { 77 | this->Close(); 78 | return; 79 | } 80 | } 81 | 82 | if (read_cb_) { 83 | bool ret = read_cb_(shared_from_this(), *read_buffer_); 84 | if (false == ret) { 85 | std::lock_guard lock(mutex_); 86 | this->Close(); 87 | } 88 | } 89 | } 90 | 91 | void TcpConnection::HandleWrite() 92 | { 93 | if (is_closed_) { 94 | return; 95 | } 96 | 97 | //std::lock_guard lock(mutex_); 98 | if (!mutex_.try_lock()) { 99 | return; 100 | } 101 | 102 | int ret = 0; 103 | bool empty = false; 104 | do 105 | { 106 | ret = write_buffer_->Send(channel_->GetSocket()); 107 | if (ret < 0) { 108 | this->Close(); 109 | mutex_.unlock(); 110 | return; 111 | } 112 | empty = write_buffer_->IsEmpty(); 113 | } while (0); 114 | 115 | if (empty) { 116 | if (channel_->IsWriting()) { 117 | channel_->DisableWriting(); 118 | task_scheduler_->UpdateChannel(channel_); 119 | } 120 | } 121 | else if(!channel_->IsWriting()) { 122 | channel_->EnableWriting(); 123 | task_scheduler_->UpdateChannel(channel_); 124 | } 125 | 126 | mutex_.unlock(); 127 | } 128 | 129 | void TcpConnection::Close() 130 | { 131 | if (!is_closed_) { 132 | is_closed_ = true; 133 | task_scheduler_->RemoveChannel(channel_); 134 | 135 | if (close_cb_) { 136 | close_cb_(shared_from_this()); 137 | } 138 | 139 | if (disconnect_cb_) { 140 | disconnect_cb_(shared_from_this()); 141 | } 142 | } 143 | } 144 | 145 | void TcpConnection::HandleClose() 146 | { 147 | std::lock_guard lock(mutex_); 148 | this->Close(); 149 | } 150 | 151 | void TcpConnection::HandleError() 152 | { 153 | std::lock_guard lock(mutex_); 154 | this->Close(); 155 | } 156 | -------------------------------------------------------------------------------- /rtsp/src/net/TcpConnection.h: -------------------------------------------------------------------------------- 1 | #ifndef XOP_TCP_CONNECTION_H 2 | #define XOP_TCP_CONNECTION_H 3 | 4 | #include 5 | #include 6 | #include "TaskScheduler.h" 7 | #include "BufferReader.h" 8 | #include "BufferWriter.h" 9 | #include "Channel.h" 10 | #include "SocketUtil.h" 11 | 12 | namespace xop 13 | { 14 | 15 | class TcpConnection : public std::enable_shared_from_this 16 | { 17 | public: 18 | using Ptr = std::shared_ptr; 19 | using DisconnectCallback = std::function conn)> ; 20 | using CloseCallback = std::function conn)>; 21 | using ReadCallback = std::function conn, xop::BufferReader& buffer)>; 22 | 23 | TcpConnection(TaskScheduler *task_scheduler, SOCKET sockfd); 24 | virtual ~TcpConnection(); 25 | 26 | TaskScheduler* GetTaskScheduler() const 27 | { return task_scheduler_; } 28 | 29 | void SetReadCallback(const ReadCallback& cb) 30 | { read_cb_ = cb; } 31 | 32 | void SetCloseCallback(const CloseCallback& cb) 33 | { close_cb_ = cb; } 34 | 35 | void Send(std::shared_ptr data, uint32_t size); 36 | void Send(const char *data, uint32_t size); 37 | 38 | void Disconnect(); 39 | 40 | bool IsClosed() const 41 | { return is_closed_; } 42 | 43 | SOCKET GetSocket() const 44 | { return channel_->GetSocket(); } 45 | 46 | uint16_t GetPort() const 47 | { return SocketUtil::GetPeerPort(channel_->GetSocket()); } 48 | 49 | std::string GetIp() const 50 | { return SocketUtil::GetPeerIp(channel_->GetSocket()); } 51 | 52 | protected: 53 | friend class TcpServer; 54 | 55 | virtual void HandleRead(); 56 | virtual void HandleWrite(); 57 | virtual void HandleClose(); 58 | virtual void HandleError(); 59 | 60 | void SetDisconnectCallback(const DisconnectCallback& cb) 61 | { disconnect_cb_ = cb; } 62 | 63 | TaskScheduler* task_scheduler_; 64 | std::unique_ptr read_buffer_; 65 | std::unique_ptr write_buffer_; 66 | std::atomic_bool is_closed_; 67 | 68 | private: 69 | void Close(); 70 | 71 | std::shared_ptr channel_; 72 | std::mutex mutex_; 73 | DisconnectCallback disconnect_cb_; 74 | CloseCallback close_cb_; 75 | ReadCallback read_cb_; 76 | }; 77 | 78 | } 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /rtsp/src/net/TcpServer.cpp: -------------------------------------------------------------------------------- 1 | #include "TcpServer.h" 2 | #include "Acceptor.h" 3 | #include "EventLoop.h" 4 | #include "Logger.h" 5 | #include 6 | 7 | using namespace xop; 8 | using namespace std; 9 | 10 | TcpServer::TcpServer(EventLoop* event_loop) 11 | : event_loop_(event_loop) 12 | , port_(0) 13 | , acceptor_(new Acceptor(event_loop_)) 14 | , is_started_(false) 15 | { 16 | acceptor_->SetNewConnectionCallback([this](SOCKET sockfd) { 17 | TcpConnection::Ptr conn = this->OnConnect(sockfd); 18 | if (conn) { 19 | this->AddConnection(sockfd, conn); 20 | conn->SetDisconnectCallback([this](TcpConnection::Ptr conn) { 21 | auto scheduler = conn->GetTaskScheduler(); 22 | SOCKET sockfd = conn->GetSocket(); 23 | if (!scheduler->AddTriggerEvent([this, sockfd] {this->RemoveConnection(sockfd); })) { 24 | scheduler->AddTimer([this, sockfd]() {this->RemoveConnection(sockfd); return false; }, 100); 25 | } 26 | }); 27 | } 28 | }); 29 | } 30 | 31 | TcpServer::~TcpServer() 32 | { 33 | Stop(); 34 | } 35 | 36 | bool TcpServer::Start(std::string ip, uint16_t port) 37 | { 38 | Stop(); 39 | 40 | if (!is_started_) { 41 | if (acceptor_->Listen(ip, port) < 0) { 42 | return false; 43 | } 44 | 45 | port_ = port; 46 | ip_ = ip; 47 | is_started_ = true; 48 | return true; 49 | } 50 | 51 | return false; 52 | } 53 | 54 | void TcpServer::Stop() 55 | { 56 | if (is_started_) { 57 | mutex_.lock(); 58 | for (auto iter : connections_) { 59 | iter.second->Disconnect(); 60 | } 61 | mutex_.unlock(); 62 | 63 | acceptor_->Close(); 64 | is_started_ = false; 65 | 66 | while (1) { 67 | Timer::Sleep(10); 68 | if (connections_.empty()) { 69 | break; 70 | } 71 | } 72 | } 73 | } 74 | 75 | TcpConnection::Ptr TcpServer::OnConnect(SOCKET sockfd) 76 | { 77 | return std::make_shared(event_loop_->GetTaskScheduler().get(), sockfd); 78 | } 79 | 80 | void TcpServer::AddConnection(SOCKET sockfd, TcpConnection::Ptr tcpConn) 81 | { 82 | std::lock_guard locker(mutex_); 83 | connections_.emplace(sockfd, tcpConn); 84 | } 85 | 86 | void TcpServer::RemoveConnection(SOCKET sockfd) 87 | { 88 | std::lock_guard locker(mutex_); 89 | connections_.erase(sockfd); 90 | } 91 | -------------------------------------------------------------------------------- /rtsp/src/net/TcpServer.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-11-10 3 | 4 | #ifndef XOP_TCPSERVER_H 5 | #define XOP_TCPSERVER_H 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "Socket.h" 12 | #include "TcpConnection.h" 13 | 14 | namespace xop 15 | { 16 | 17 | class Acceptor; 18 | class EventLoop; 19 | 20 | class TcpServer 21 | { 22 | public: 23 | TcpServer(EventLoop* event_loop); 24 | virtual ~TcpServer(); 25 | 26 | virtual bool Start(std::string ip, uint16_t port); 27 | virtual void Stop(); 28 | 29 | std::string GetIPAddress() const 30 | { return ip_; } 31 | 32 | uint16_t GetPort() const 33 | { return port_; } 34 | 35 | protected: 36 | virtual TcpConnection::Ptr OnConnect(SOCKET sockfd); 37 | virtual void AddConnection(SOCKET sockfd, TcpConnection::Ptr tcp_conn); 38 | virtual void RemoveConnection(SOCKET sockfd); 39 | 40 | EventLoop* event_loop_; 41 | uint16_t port_; 42 | std::string ip_; 43 | std::unique_ptr acceptor_; 44 | bool is_started_; 45 | std::mutex mutex_; 46 | std::unordered_map connections_; 47 | }; 48 | 49 | } 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /rtsp/src/net/TcpSocket.cpp: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #include "TcpSocket.h" 5 | #include "Socket.h" 6 | #include "SocketUtil.h" 7 | #include "Logger.h" 8 | 9 | using namespace xop; 10 | 11 | TcpSocket::TcpSocket(SOCKET sockfd) 12 | : sockfd_(sockfd) 13 | { 14 | 15 | } 16 | 17 | TcpSocket::~TcpSocket() 18 | { 19 | 20 | } 21 | 22 | SOCKET TcpSocket::Create() 23 | { 24 | sockfd_ = ::socket(AF_INET, SOCK_STREAM, 0); 25 | return sockfd_; 26 | } 27 | 28 | bool TcpSocket::Bind(std::string ip, uint16_t port) 29 | { 30 | struct sockaddr_in addr = {0}; 31 | addr.sin_family = AF_INET; 32 | addr.sin_addr.s_addr = inet_addr(ip.c_str()); 33 | addr.sin_port = htons(port); 34 | 35 | if(::bind(sockfd_, (struct sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) { 36 | LOG_DEBUG(" bind <%s:%u> failed.\n", sockfd_, ip.c_str(), port); 37 | return false; 38 | } 39 | 40 | return true; 41 | } 42 | 43 | bool TcpSocket::Listen(int backlog) 44 | { 45 | if(::listen(sockfd_, backlog) == SOCKET_ERROR) { 46 | LOG_DEBUG(" listen failed.\n", sockfd_); 47 | return false; 48 | } 49 | 50 | return true; 51 | } 52 | 53 | SOCKET TcpSocket::Accept() 54 | { 55 | struct sockaddr_in addr = {0}; 56 | socklen_t addrlen = sizeof addr; 57 | 58 | SOCKET socket_fd = ::accept(sockfd_, (struct sockaddr*)&addr, &addrlen); 59 | return socket_fd; 60 | } 61 | 62 | bool TcpSocket::Connect(std::string ip, uint16_t port, int timeout) 63 | { 64 | if(!SocketUtil::Connect(sockfd_, ip, port, timeout)) { 65 | LOG_DEBUG(" connect failed.\n", sockfd_); 66 | return false; 67 | } 68 | 69 | return true; 70 | } 71 | 72 | void TcpSocket::Close() 73 | { 74 | #if defined(__linux) || defined(__linux__) 75 | ::close(sockfd_); 76 | #elif defined(WIN32) || defined(_WIN32) 77 | closesocket(sockfd_); 78 | #else 79 | 80 | #endif 81 | sockfd_ = 0; 82 | } 83 | 84 | void TcpSocket::ShutdownWrite() 85 | { 86 | shutdown(sockfd_, SHUT_WR); 87 | sockfd_ = 0; 88 | } 89 | -------------------------------------------------------------------------------- /rtsp/src/net/TcpSocket.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #ifndef XOP_TCP_SOCKET_H 5 | #define XOP_TCP_SOCKET_H 6 | 7 | #include 8 | #include 9 | #include "Socket.h" 10 | 11 | namespace xop 12 | { 13 | 14 | class TcpSocket 15 | { 16 | public: 17 | TcpSocket(SOCKET sockfd=-1); 18 | virtual ~TcpSocket(); 19 | 20 | SOCKET Create(); 21 | bool Bind(std::string ip, uint16_t port); 22 | bool Listen(int backlog); 23 | SOCKET Accept(); 24 | bool Connect(std::string ip, uint16_t port, int timeout = 0); 25 | void Close(); 26 | void ShutdownWrite(); 27 | SOCKET GetSocket() const { return sockfd_; } 28 | 29 | private: 30 | SOCKET sockfd_ = -1; 31 | }; 32 | 33 | } 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /rtsp/src/net/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include "Timer.h" 2 | #include 3 | 4 | using namespace xop; 5 | using namespace std; 6 | using namespace std::chrono; 7 | 8 | TimerId TimerQueue::AddTimer(const TimerEvent& event, uint32_t ms) 9 | { 10 | std::lock_guard locker(mutex_); 11 | 12 | int64_t timeout = GetTimeNow(); 13 | TimerId timer_id = ++last_timer_id_; 14 | 15 | auto timer = make_shared(event, ms); 16 | timer->SetNextTimeout(timeout); 17 | timers_.emplace(timer_id, timer); 18 | events_.emplace(std::pair(timeout + ms, timer_id), std::move(timer)); 19 | return timer_id; 20 | } 21 | 22 | void TimerQueue::RemoveTimer(TimerId timerId) 23 | { 24 | std::lock_guard locker(mutex_); 25 | 26 | auto iter = timers_.find(timerId); 27 | if (iter != timers_.end()) { 28 | int64_t timeout = iter->second->getNextTimeout(); 29 | events_.erase(std::pair(timeout, timerId)); 30 | timers_.erase(timerId); 31 | } 32 | } 33 | 34 | int64_t TimerQueue::GetTimeNow() 35 | { 36 | auto time_point = steady_clock::now(); 37 | return duration_cast(time_point.time_since_epoch()).count(); 38 | } 39 | 40 | int64_t TimerQueue::GetTimeRemaining() 41 | { 42 | std::lock_guard locker(mutex_); 43 | 44 | if (timers_.empty()) { 45 | return -1; 46 | } 47 | 48 | int64_t msec = events_.begin()->first.first - GetTimeNow(); 49 | if (msec < 0) { 50 | msec = 0; 51 | } 52 | 53 | return msec; 54 | } 55 | 56 | void TimerQueue::HandleTimerEvent() 57 | { 58 | if(!timers_.empty()) { 59 | std::lock_guard locker(mutex_); 60 | int64_t timePoint = GetTimeNow(); 61 | while(!timers_.empty() && events_.begin()->first.first<=timePoint) 62 | { 63 | TimerId timerId = events_.begin()->first.second; 64 | bool flag = events_.begin()->second->event_callback_(); 65 | if(flag == true) { 66 | events_.begin()->second->SetNextTimeout(timePoint); 67 | auto timerPtr = std::move(events_.begin()->second); 68 | events_.erase(events_.begin()); 69 | events_.emplace(std::pair(timerPtr->getNextTimeout(), timerId), timerPtr); 70 | } 71 | else { 72 | events_.erase(events_.begin()); 73 | timers_.erase(timerId); 74 | } 75 | } 76 | } 77 | } 78 | 79 | -------------------------------------------------------------------------------- /rtsp/src/net/Timer.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #ifndef _XOP_TIMER_H 5 | #define _XOP_TIMER_H 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | namespace xop 18 | { 19 | 20 | typedef std::function TimerEvent; 21 | typedef uint32_t TimerId; 22 | 23 | class Timer 24 | { 25 | public: 26 | Timer(const TimerEvent& event, uint32_t msec) 27 | : event_callback_(event) 28 | , interval_(msec) 29 | { 30 | if (interval_ == 0) { 31 | interval_ = 1; 32 | } 33 | } 34 | 35 | static void Sleep(uint32_t msec) 36 | { 37 | std::this_thread::sleep_for(std::chrono::milliseconds(msec)); 38 | } 39 | 40 | void SetEventCallback(const TimerEvent& event) 41 | { 42 | event_callback_ = event; 43 | } 44 | 45 | void Start(int64_t microseconds, bool repeat = false) 46 | { 47 | is_repeat_ = repeat; 48 | auto time_begin = std::chrono::high_resolution_clock::now(); 49 | int64_t elapsed = 0; 50 | 51 | do 52 | { 53 | std::this_thread::sleep_for(std::chrono::microseconds(microseconds - elapsed)); 54 | time_begin = std::chrono::high_resolution_clock::now(); 55 | if (event_callback_) { 56 | event_callback_(); 57 | } 58 | elapsed = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - time_begin).count(); 59 | if (elapsed < 0) { 60 | elapsed = 0; 61 | } 62 | 63 | } while (is_repeat_); 64 | } 65 | 66 | void Stop() 67 | { 68 | is_repeat_ = false; 69 | } 70 | 71 | private: 72 | friend class TimerQueue; 73 | 74 | void SetNextTimeout(int64_t time_point) 75 | { 76 | next_timeout_ = time_point + interval_; 77 | } 78 | 79 | int64_t getNextTimeout() const 80 | { 81 | return next_timeout_; 82 | } 83 | 84 | bool is_repeat_ = false; 85 | TimerEvent event_callback_ = [] { return false; }; 86 | uint32_t interval_ = 0; 87 | int64_t next_timeout_ = 0; 88 | }; 89 | 90 | class TimerQueue 91 | { 92 | public: 93 | TimerId AddTimer(const TimerEvent& event, uint32_t msec); 94 | void RemoveTimer(TimerId timerId); 95 | 96 | int64_t GetTimeRemaining(); 97 | void HandleTimerEvent(); 98 | 99 | private: 100 | int64_t GetTimeNow(); 101 | 102 | std::mutex mutex_; 103 | std::unordered_map> timers_; 104 | std::map, std::shared_ptr> events_; 105 | uint32_t last_timer_id_ = 0; 106 | }; 107 | 108 | } 109 | 110 | #endif 111 | 112 | 113 | 114 | -------------------------------------------------------------------------------- /rtsp/src/net/Timestamp.cpp: -------------------------------------------------------------------------------- 1 | #include "Timestamp.h" 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace xop; 7 | using namespace std; 8 | using namespace std::chrono; 9 | 10 | std::string Timestamp::Localtime() 11 | { 12 | std::ostringstream stream; 13 | auto now = system_clock::now(); 14 | time_t tt = system_clock::to_time_t(now); 15 | 16 | #if defined(WIN32) || defined(_WIN32) 17 | struct tm tm; 18 | localtime_s(&tm, &tt); 19 | stream << std::put_time(&tm, "%F %T"); 20 | #elif defined(__linux) || defined(__linux__) 21 | char buffer[200] = {0}; 22 | std::string timeString; 23 | std::strftime(buffer, 200, "%F %T", std::localtime(&tt)); 24 | stream << buffer; 25 | #endif 26 | return stream.str(); 27 | } -------------------------------------------------------------------------------- /rtsp/src/net/Timestamp.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-15 3 | 4 | #ifndef XOP_TIMESTAMP_H 5 | #define XOP_TIMESTAMP_H 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | namespace xop 14 | { 15 | 16 | class Timestamp 17 | { 18 | public: 19 | Timestamp() 20 | : begin_time_point_(std::chrono::high_resolution_clock::now()) 21 | { } 22 | 23 | void Reset() 24 | { 25 | begin_time_point_ = std::chrono::high_resolution_clock::now(); 26 | } 27 | 28 | int64_t Elapsed() 29 | { 30 | return std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - begin_time_point_).count(); 31 | } 32 | 33 | static std::string Localtime(); 34 | 35 | private: 36 | std::chrono::time_point begin_time_point_; 37 | }; 38 | 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /rtsp/src/net/log.h: -------------------------------------------------------------------------------- 1 | #ifndef _XOP_LOG_H 2 | #define _XOP_LOG_H 3 | 4 | #include 5 | 6 | //#ifdef _DEBUG 7 | #define LOG(format, ...) \ 8 | { \ 9 | fprintf(stderr, "[DEBUG] [%s:%s:%d] " format "", \ 10 | __FILE__, __FUNCTION__ , __LINE__, ##__VA_ARGS__); \ 11 | } 12 | //#else 13 | //#define LOG(format, ...) 14 | //#endif 15 | 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /rtsp/src/xop/AACSource.cpp: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-16 3 | 4 | #if defined(WIN32) || defined(_WIN32) 5 | #ifndef _CRT_SECURE_NO_WARNINGS 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #endif 8 | #endif 9 | #include "AACSource.h" 10 | #include 11 | #include 12 | #include 13 | #if defined(__linux) || defined(__linux__) 14 | #include 15 | #endif 16 | 17 | using namespace xop; 18 | using namespace std; 19 | 20 | AACSource::AACSource(uint32_t samplerate, uint32_t channels, bool has_adts) 21 | : samplerate_(samplerate) 22 | , channels_(channels) 23 | , has_adts_(has_adts) 24 | { 25 | payload_ = 97; 26 | media_type_ = AAC; 27 | clock_rate_ = samplerate; 28 | } 29 | 30 | AACSource* AACSource::CreateNew(uint32_t samplerate, uint32_t channels, bool has_adts) 31 | { 32 | return new AACSource(samplerate, channels, has_adts); 33 | } 34 | 35 | AACSource::~AACSource() 36 | { 37 | 38 | } 39 | 40 | string AACSource::GetMediaDescription(uint16_t port) 41 | { 42 | char buf[100] = { 0 }; 43 | sprintf(buf, "m=audio %hu RTP/AVP 97", port); // \r\nb=AS:64 44 | return string(buf); 45 | } 46 | 47 | static uint32_t AACSampleRate[16] = 48 | { 49 | 96000, 88200, 64000, 48000, 50 | 44100, 32000, 24000, 22050, 51 | 16000, 12000, 11025, 8000, 52 | 7350, 0, 0, 0 /*reserved */ 53 | }; 54 | 55 | string AACSource::GetAttribute() // RFC 3640 56 | { 57 | char buf[500] = { 0 }; 58 | sprintf(buf, "a=rtpmap:97 MPEG4-GENERIC/%u/%u\r\n", samplerate_, channels_); 59 | 60 | uint8_t index = 0; 61 | for (index = 0; index < 16; index++) { 62 | if (AACSampleRate[index] == samplerate_) { 63 | break; 64 | } 65 | } 66 | 67 | if (index == 16) { 68 | return ""; // error 69 | } 70 | 71 | uint8_t profile = 1; 72 | char config[10] = {0}; 73 | 74 | sprintf(config, "%02x%02x", (uint8_t)((profile+1) << 3)|(index >> 1), (uint8_t)((index << 7)|(channels_<< 3))); 75 | sprintf(buf+strlen(buf), 76 | "a=fmtp:97 profile-level-id=1;" 77 | "mode=AAC-hbr;" 78 | "sizelength=13;indexlength=3;indexdeltalength=3;" 79 | "config=%04u", 80 | atoi(config)); 81 | 82 | return string(buf); 83 | } 84 | 85 | 86 | 87 | bool AACSource::HandleFrame(MediaChannelId channel_id, AVFrame frame) 88 | { 89 | if (frame.size > (MAX_RTP_PAYLOAD_SIZE-AU_SIZE)) { 90 | return false; 91 | } 92 | 93 | int adts_size = 0; 94 | if (has_adts_) { 95 | adts_size = ADTS_SIZE; 96 | } 97 | 98 | uint8_t *frame_buf = frame.buffer.get() + adts_size; 99 | uint32_t frame_size = frame.size - adts_size; 100 | 101 | char AU[AU_SIZE] = { 0 }; 102 | AU[0] = 0x00; 103 | AU[1] = 0x10; 104 | AU[2] = (frame_size & 0x1fe0) >> 5; 105 | AU[3] = (frame_size & 0x1f) << 3; 106 | 107 | RtpPacket rtp_pkt; 108 | rtp_pkt.type = frame.type; 109 | rtp_pkt.timestamp = frame.timestamp; 110 | rtp_pkt.size = frame_size + RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + AU_SIZE; 111 | rtp_pkt.last = 1; 112 | 113 | rtp_pkt.data.get()[RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + 0] = AU[0]; 114 | rtp_pkt.data.get()[RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + 1] = AU[1]; 115 | rtp_pkt.data.get()[RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + 2] = AU[2]; 116 | rtp_pkt.data.get()[RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + 3] = AU[3]; 117 | 118 | memcpy(rtp_pkt.data.get()+RTP_TCP_HEAD_SIZE+RTP_HEADER_SIZE+AU_SIZE, frame_buf, frame_size); 119 | 120 | if (send_frame_callback_) { 121 | send_frame_callback_(channel_id, rtp_pkt); 122 | } 123 | 124 | return true; 125 | } 126 | 127 | uint32_t AACSource::GetTimestamp(uint32_t sampleRate) 128 | { 129 | //auto time_point = chrono::time_point_cast(chrono::high_resolution_clock::now()); 130 | //return (uint32_t)(time_point.time_since_epoch().count() * sampleRate / 1000); 131 | 132 | auto time_point = chrono::time_point_cast(chrono::steady_clock::now()); 133 | return (uint32_t)((time_point.time_since_epoch().count()+500) / 1000 * sampleRate / 1000); 134 | } 135 | -------------------------------------------------------------------------------- /rtsp/src/xop/AACSource.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-16 3 | 4 | #ifndef XOP_AAC_SOURCE_H 5 | #define XOP_AAC_SOURCE_H 6 | 7 | #include "MediaSource.h" 8 | #include "rtp.h" 9 | 10 | namespace xop 11 | { 12 | 13 | class AACSource : public MediaSource 14 | { 15 | public: 16 | static AACSource* CreateNew(uint32_t samplerate=44100, uint32_t channels=2, bool has_adts=true); 17 | virtual ~AACSource(); 18 | 19 | uint32_t GetSamplerate() const 20 | { return samplerate_; } 21 | 22 | uint32_t GetChannels() const 23 | { return channels_; } 24 | 25 | virtual std::string GetMediaDescription(uint16_t port=0); 26 | 27 | virtual std::string GetAttribute(); 28 | 29 | virtual bool HandleFrame(MediaChannelId channel_id, AVFrame frame); 30 | 31 | static uint32_t GetTimestamp(uint32_t samplerate =44100); 32 | 33 | private: 34 | AACSource(uint32_t samplerate, uint32_t channels, bool has_adts); 35 | 36 | uint32_t samplerate_ = 44100; 37 | uint32_t channels_ = 2; 38 | bool has_adts_ = true; 39 | 40 | static const int ADTS_SIZE = 7; 41 | static const int AU_SIZE = 4; 42 | }; 43 | 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /rtsp/src/xop/DigestAuthentication.cpp: -------------------------------------------------------------------------------- 1 | #include "DigestAuthentication.h" 2 | #include "md5/md5.hpp" 3 | 4 | using namespace xop; 5 | 6 | DigestAuthentication::DigestAuthentication(std::string realm, std::string username, std::string password) 7 | : realm_(realm) 8 | , username_(username) 9 | , password_(password) 10 | { 11 | 12 | } 13 | 14 | DigestAuthentication::~DigestAuthentication() 15 | { 16 | 17 | } 18 | 19 | std::string DigestAuthentication::GetNonce() 20 | { 21 | return md5::generate_nonce(); 22 | } 23 | 24 | std::string DigestAuthentication::GetResponse(std::string nonce, std::string cmd, std::string url) 25 | { 26 | //md5(md5(: : ) : : md5(:)) 27 | 28 | auto hex1 = md5::md5_hash_hex(username_ + ":" + realm_ + ":" + password_); 29 | auto hex2 = md5::md5_hash_hex(cmd + ":" + url); 30 | auto response = md5::md5_hash_hex(hex1 + ":" + nonce + ":" + hex2); 31 | return response; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /rtsp/src/xop/DigestAuthentication.h: -------------------------------------------------------------------------------- 1 | //PHZ 2 | //2019-10-6 3 | 4 | #ifndef RTSP_DIGEST_AUTHENTICATION_H 5 | #define RTSP_DIGEST_AUTHENTICATION_H 6 | 7 | #include 8 | #include 9 | 10 | namespace xop 11 | { 12 | 13 | class DigestAuthentication 14 | { 15 | public: 16 | DigestAuthentication(std::string realm, std::string username, std::string password); 17 | virtual ~DigestAuthentication(); 18 | 19 | std::string GetRealm() const 20 | { return realm_; } 21 | 22 | std::string GetUsername() const 23 | { return username_; } 24 | 25 | std::string GetPassword() const 26 | { return password_; } 27 | 28 | std::string GetNonce(); 29 | std::string GetResponse(std::string nonce, std::string cmd, std::string url); 30 | 31 | private: 32 | std::string realm_; 33 | std::string username_; 34 | std::string password_; 35 | 36 | }; 37 | 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /rtsp/src/xop/G711ASource.cpp: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-16 3 | 4 | #if defined(WIN32) || defined(_WIN32) 5 | #ifndef _CRT_SECURE_NO_WARNINGS 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #endif 8 | #endif 9 | #include "G711ASource.h" 10 | #include 11 | #include 12 | #if defined(__linux) || defined(__linux__) 13 | #include 14 | #endif 15 | 16 | using namespace xop; 17 | using namespace std; 18 | 19 | G711ASource::G711ASource() 20 | { 21 | payload_ = 8; 22 | media_type_ = PCMA; 23 | clock_rate_ = 8000; 24 | } 25 | 26 | G711ASource* G711ASource::CreateNew() 27 | { 28 | return new G711ASource(); 29 | } 30 | 31 | G711ASource::~G711ASource() 32 | { 33 | 34 | } 35 | 36 | string G711ASource::GetMediaDescription(uint16_t port) 37 | { 38 | char buf[100] = {0}; 39 | sprintf(buf, "m=audio %hu RTP/AVP 8", port); 40 | return string(buf); 41 | } 42 | 43 | string G711ASource::GetAttribute() 44 | { 45 | return string("a=rtpmap:8 PCMA/8000/1"); 46 | } 47 | 48 | bool G711ASource::HandleFrame(MediaChannelId channel_id, AVFrame frame) 49 | { 50 | if (frame.size > MAX_RTP_PAYLOAD_SIZE) { 51 | return false; 52 | } 53 | 54 | uint8_t *frame_buf = frame.buffer.get(); 55 | uint32_t frame_size = frame.size; 56 | 57 | RtpPacket rtp_pkt; 58 | rtp_pkt.type = frame.type; 59 | rtp_pkt.timestamp = frame.timestamp; 60 | rtp_pkt.size = frame_size + RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE; 61 | rtp_pkt.last = 1; 62 | 63 | memcpy(rtp_pkt.data.get()+RTP_TCP_HEAD_SIZE+RTP_HEADER_SIZE, frame_buf, frame_size); 64 | 65 | if (send_frame_callback_) { 66 | send_frame_callback_(channel_id, rtp_pkt); 67 | } 68 | 69 | return true; 70 | } 71 | 72 | uint32_t G711ASource::GetTimestamp() 73 | { 74 | auto time_point = chrono::time_point_cast(chrono::steady_clock::now()); 75 | return (uint32_t)((time_point.time_since_epoch().count()+500)/1000*8); 76 | } 77 | 78 | -------------------------------------------------------------------------------- /rtsp/src/xop/G711ASource.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-16 3 | 4 | #ifndef XOP_G711A_SOURCE_H 5 | #define XOP_G711A_SOURCE_H 6 | 7 | #include "MediaSource.h" 8 | #include "rtp.h" 9 | 10 | namespace xop 11 | { 12 | 13 | class G711ASource : public MediaSource 14 | { 15 | public: 16 | static G711ASource* CreateNew(); 17 | virtual ~G711ASource(); 18 | 19 | uint32_t GetSampleRate() const 20 | { return samplerate_; } 21 | 22 | uint32_t GetChannels() const 23 | { return channels_; } 24 | 25 | virtual std::string GetMediaDescription(uint16_t port=0); 26 | 27 | virtual std::string GetAttribute(); 28 | 29 | virtual bool HandleFrame(MediaChannelId channel_id, AVFrame frame); 30 | 31 | static uint32_t GetTimestamp(); 32 | 33 | private: 34 | G711ASource(); 35 | 36 | uint32_t samplerate_ = 8000; 37 | uint32_t channels_ = 1; 38 | }; 39 | 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /rtsp/src/xop/H264Parser.cpp: -------------------------------------------------------------------------------- 1 | #include "H264Parser.h" 2 | #include 3 | 4 | using namespace xop; 5 | 6 | Nal H264Parser::findNal(const uint8_t *data, uint32_t size) 7 | { 8 | Nal nal(nullptr, nullptr); 9 | 10 | if(size < 5) 11 | { 12 | return nal; 13 | } 14 | 15 | nal.second = const_cast(data) + (size-1); 16 | 17 | uint32_t startCode = 0; 18 | uint32_t pos = 0; 19 | uint8_t prefix[3] = {0}; 20 | memcpy(prefix, data, 3); 21 | size -= 3; 22 | data += 2; 23 | 24 | while(size--) 25 | { 26 | if ((prefix[pos % 3] == 0) && (prefix[(pos + 1) % 3] == 0) && (prefix[(pos + 2) % 3] == 1)) 27 | { 28 | if(nal.first == nullptr) // 00 00 01 29 | { 30 | nal.first = const_cast(data) + 1; 31 | startCode = 3; 32 | } 33 | else if(startCode == 3) 34 | { 35 | nal.second = const_cast(data) - 3; 36 | break; 37 | } 38 | } 39 | else if ((prefix[pos % 3] == 0) && (prefix[(pos + 1) % 3] == 0) && (prefix[(pos + 2) % 3] == 0)) 40 | { 41 | if (*(data+1) == 0x01) // 00 00 00 01 42 | { 43 | if(nal.first == nullptr) 44 | { 45 | if(size >= 1) 46 | { 47 | nal.first = const_cast(data) + 2; 48 | } 49 | else 50 | { 51 | break; 52 | } 53 | startCode = 4; 54 | } 55 | else if(startCode == 4) 56 | { 57 | nal.second = const_cast(data) - 3; 58 | break; 59 | } 60 | } 61 | } 62 | 63 | prefix[(pos++) % 3] = *(++data); 64 | } 65 | 66 | if(nal.first == nullptr) 67 | nal.second = nullptr; 68 | 69 | return nal; 70 | } 71 | 72 | 73 | -------------------------------------------------------------------------------- /rtsp/src/xop/H264Parser.h: -------------------------------------------------------------------------------- 1 | #ifndef XOP_H264_PARSER_H 2 | #define XOP_H264_PARSER_H 3 | 4 | #include 5 | #include 6 | 7 | namespace xop 8 | { 9 | 10 | typedef std::pair Nal; // 11 | 12 | class H264Parser 13 | { 14 | public: 15 | static Nal findNal(const uint8_t *data, uint32_t size); 16 | 17 | private: 18 | 19 | }; 20 | 21 | } 22 | 23 | #endif 24 | 25 | -------------------------------------------------------------------------------- /rtsp/src/xop/H264Source.cpp: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-16 3 | 4 | #if defined(WIN32) || defined(_WIN32) 5 | #ifndef _CRT_SECURE_NO_WARNINGS 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #endif 8 | #endif 9 | 10 | #include "H264Source.h" 11 | #include 12 | #include 13 | #if defined(__linux) || defined(__linux__) 14 | #include 15 | #endif 16 | 17 | using namespace xop; 18 | using namespace std; 19 | 20 | H264Source::H264Source(uint32_t framerate) 21 | : framerate_(framerate) 22 | { 23 | payload_ = 96; 24 | media_type_ = H264; 25 | clock_rate_ = 90000; 26 | } 27 | 28 | H264Source* H264Source::CreateNew(uint32_t framerate) 29 | { 30 | return new H264Source(framerate); 31 | } 32 | 33 | H264Source::~H264Source() 34 | { 35 | 36 | } 37 | 38 | string H264Source::GetMediaDescription(uint16_t port) 39 | { 40 | char buf[100] = {0}; 41 | sprintf(buf, "m=video %hu RTP/AVP 96", port); // \r\nb=AS:2000 42 | return string(buf); 43 | } 44 | 45 | string H264Source::GetAttribute() 46 | { 47 | return string("a=rtpmap:96 H264/90000"); 48 | } 49 | 50 | bool H264Source::HandleFrame(MediaChannelId channel_id, AVFrame frame) 51 | { 52 | uint8_t* frame_buf = frame.buffer.get(); 53 | uint32_t frame_size = frame.size; 54 | 55 | if (frame.timestamp == 0) { 56 | frame.timestamp = GetTimestamp(); 57 | } 58 | 59 | if (frame_size <= MAX_RTP_PAYLOAD_SIZE) { 60 | RtpPacket rtp_pkt; 61 | rtp_pkt.type = frame.type; 62 | rtp_pkt.timestamp = frame.timestamp; 63 | rtp_pkt.size = frame_size + RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE; 64 | rtp_pkt.last = 1; 65 | memcpy(rtp_pkt.data.get()+RTP_TCP_HEAD_SIZE+RTP_HEADER_SIZE, frame_buf, frame_size); 66 | 67 | if (send_frame_callback_) { 68 | if (!send_frame_callback_(channel_id, rtp_pkt)) { 69 | return false; 70 | } 71 | } 72 | } 73 | else { 74 | char FU_A[2] = {0}; 75 | 76 | FU_A[0] = (frame_buf[0] & 0xE0) | 28; 77 | FU_A[1] = 0x80 | (frame_buf[0] & 0x1f); 78 | 79 | frame_buf += 1; 80 | frame_size -= 1; 81 | 82 | while (frame_size + 2 > MAX_RTP_PAYLOAD_SIZE) { 83 | RtpPacket rtp_pkt; 84 | rtp_pkt.type = frame.type; 85 | rtp_pkt.timestamp = frame.timestamp; 86 | rtp_pkt.size = RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + MAX_RTP_PAYLOAD_SIZE; 87 | rtp_pkt.last = 0; 88 | 89 | rtp_pkt.data.get()[RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + 0] = FU_A[0]; 90 | rtp_pkt.data.get()[RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + 1] = FU_A[1]; 91 | memcpy(rtp_pkt.data.get()+RTP_TCP_HEAD_SIZE+RTP_HEADER_SIZE+2, frame_buf, MAX_RTP_PAYLOAD_SIZE-2); 92 | 93 | if (send_frame_callback_) { 94 | if (!send_frame_callback_(channel_id, rtp_pkt)) 95 | return false; 96 | } 97 | 98 | frame_buf += MAX_RTP_PAYLOAD_SIZE - 2; 99 | frame_size -= MAX_RTP_PAYLOAD_SIZE - 2; 100 | 101 | FU_A[1] &= ~0x80; 102 | } 103 | 104 | { 105 | RtpPacket rtp_pkt; 106 | rtp_pkt.type = frame.type; 107 | rtp_pkt.timestamp = frame.timestamp; 108 | rtp_pkt.size = 4 + RTP_HEADER_SIZE + 2 + frame_size; 109 | rtp_pkt.last = 1; 110 | 111 | FU_A[1] |= 0x40; 112 | rtp_pkt.data.get()[RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + 0] = FU_A[0]; 113 | rtp_pkt.data.get()[RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + 1] = FU_A[1]; 114 | memcpy(rtp_pkt.data.get()+RTP_TCP_HEAD_SIZE+RTP_HEADER_SIZE+2, frame_buf, frame_size); 115 | 116 | if (send_frame_callback_) { 117 | if (!send_frame_callback_(channel_id, rtp_pkt)) { 118 | return false; 119 | } 120 | } 121 | } 122 | } 123 | 124 | return true; 125 | } 126 | 127 | uint32_t H264Source::GetTimestamp() 128 | { 129 | /* #if defined(__linux) || defined(__linux__) 130 | struct timeval tv = {0}; 131 | gettimeofday(&tv, NULL); 132 | uint32_t ts = ((tv.tv_sec*1000)+((tv.tv_usec+500)/1000))*90; // 90: _clockRate/1000; 133 | return ts; 134 | #else */ 135 | auto time_point = chrono::time_point_cast(chrono::steady_clock::now()); 136 | return (uint32_t)((time_point.time_since_epoch().count() + 500) / 1000 * 90 ); 137 | //#endif 138 | } 139 | -------------------------------------------------------------------------------- /rtsp/src/xop/H264Source.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-16 3 | 4 | #ifndef XOP_H264_SOURCE_H 5 | #define XOP_H264_SOURCE_H 6 | 7 | #include "MediaSource.h" 8 | #include "rtp.h" 9 | 10 | namespace xop 11 | { 12 | 13 | class H264Source : public MediaSource 14 | { 15 | public: 16 | static H264Source* CreateNew(uint32_t framerate=25); 17 | ~H264Source(); 18 | 19 | void SetFramerate(uint32_t framerate) 20 | { framerate_ = framerate; } 21 | 22 | uint32_t GetFramerate() const 23 | { return framerate_; } 24 | 25 | virtual std::string GetMediaDescription(uint16_t port); 26 | 27 | virtual std::string GetAttribute(); 28 | 29 | virtual bool HandleFrame(MediaChannelId channel_id, AVFrame frame); 30 | 31 | static uint32_t GetTimestamp(); 32 | 33 | private: 34 | H264Source(uint32_t framerate); 35 | 36 | uint32_t framerate_ = 25; 37 | }; 38 | 39 | } 40 | 41 | #endif 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /rtsp/src/xop/H265Source.cpp: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-6-7 3 | 4 | #if defined(WIN32) || defined(_WIN32) 5 | #ifndef _CRT_SECURE_NO_WARNINGS 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #endif 8 | #endif 9 | 10 | #include "H265Source.h" 11 | #include 12 | #include 13 | #if defined(__linux) || defined(__linux__) 14 | #include 15 | #endif 16 | 17 | using namespace xop; 18 | using namespace std; 19 | 20 | H265Source::H265Source(uint32_t framerate) 21 | : framerate_(framerate) 22 | { 23 | payload_ = 96; 24 | media_type_ = H265; 25 | clock_rate_ = 90000; 26 | } 27 | 28 | H265Source* H265Source::CreateNew(uint32_t framerate) 29 | { 30 | return new H265Source(framerate); 31 | } 32 | 33 | H265Source::~H265Source() 34 | { 35 | 36 | } 37 | 38 | string H265Source::GetMediaDescription(uint16_t port) 39 | { 40 | char buf[100] = {0}; 41 | sprintf(buf, "m=video %hu RTP/AVP 96", port); 42 | return string(buf); 43 | } 44 | 45 | string H265Source::GetAttribute() 46 | { 47 | return string("a=rtpmap:96 H265/90000"); 48 | } 49 | 50 | bool H265Source::HandleFrame(MediaChannelId channelId, AVFrame frame) 51 | { 52 | uint8_t *frame_buf = frame.buffer.get(); 53 | uint32_t frame_size = frame.size; 54 | 55 | if (frame.timestamp == 0) { 56 | frame.timestamp = GetTimestamp(); 57 | } 58 | 59 | if (frame_size <= MAX_RTP_PAYLOAD_SIZE) { 60 | RtpPacket rtp_pkt; 61 | rtp_pkt.type = frame.type; 62 | rtp_pkt.timestamp = frame.timestamp; 63 | rtp_pkt.size = frame_size + RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE; 64 | rtp_pkt.last = 1; 65 | 66 | memcpy(rtp_pkt.data.get()+RTP_TCP_HEAD_SIZE+RTP_HEADER_SIZE, frame_buf, frame_size); 67 | 68 | if (send_frame_callback_) { 69 | if (!send_frame_callback_(channelId, rtp_pkt)) { 70 | return false; 71 | } 72 | } 73 | } 74 | else { 75 | char FU[3] = {0}; 76 | char nalUnitType = (frame_buf[0] & 0x7E) >> 1; 77 | FU[0] = (frame_buf[0] & 0x81) | (49<<1); 78 | FU[1] = frame_buf[1]; 79 | FU[2] = (0x80 | nalUnitType); 80 | 81 | frame_buf += 2; 82 | frame_size -= 2; 83 | 84 | while (frame_size + 3 > MAX_RTP_PAYLOAD_SIZE) { 85 | RtpPacket rtp_pkt; 86 | rtp_pkt.type = frame.type; 87 | rtp_pkt.timestamp = frame.timestamp; 88 | rtp_pkt.size = RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + MAX_RTP_PAYLOAD_SIZE; 89 | rtp_pkt.last = 0; 90 | 91 | rtp_pkt.data.get()[RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + 0] = FU[0]; 92 | rtp_pkt.data.get()[RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + 1] = FU[1]; 93 | rtp_pkt.data.get()[RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + 2] = FU[2]; 94 | memcpy(rtp_pkt.data.get()+RTP_TCP_HEAD_SIZE+RTP_HEADER_SIZE+3, frame_buf, MAX_RTP_PAYLOAD_SIZE-3); 95 | 96 | if (send_frame_callback_) { 97 | if (!send_frame_callback_(channelId, rtp_pkt)) { 98 | return false; 99 | } 100 | } 101 | 102 | frame_buf += (MAX_RTP_PAYLOAD_SIZE - 3); 103 | frame_size -= (MAX_RTP_PAYLOAD_SIZE - 3); 104 | 105 | FU[2] &= ~0x80; 106 | } 107 | 108 | { 109 | RtpPacket rtp_pkt; 110 | rtp_pkt.type = frame.type; 111 | rtp_pkt.timestamp = frame.timestamp; 112 | rtp_pkt.size = RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + 3 + frame_size; 113 | rtp_pkt.last = 1; 114 | 115 | FU[2] |= 0x40; 116 | rtp_pkt.data.get()[RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + 0] = FU[0]; 117 | rtp_pkt.data.get()[RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + 1] = FU[1]; 118 | rtp_pkt.data.get()[RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + 2] = FU[2]; 119 | memcpy(rtp_pkt.data.get()+RTP_TCP_HEAD_SIZE+RTP_HEADER_SIZE+3, frame_buf, frame_size); 120 | 121 | if (send_frame_callback_) { 122 | if (!send_frame_callback_(channelId, rtp_pkt)) { 123 | return false; 124 | } 125 | } 126 | } 127 | } 128 | 129 | return true; 130 | } 131 | 132 | uint32_t H265Source::GetTimestamp() 133 | { 134 | /* #if defined(__linux) || defined(__linux__) 135 | struct timeval tv = {0}; 136 | gettimeofday(&tv, NULL); 137 | uint32_t ts = ((tv.tv_sec*1000)+((tv.tv_usec+500)/1000))*90; // 90: _clockRate/1000; 138 | return ts; 139 | #else */ 140 | //auto time_point = chrono::time_point_cast(chrono::system_clock::now()); 141 | //auto time_point = chrono::time_point_cast(chrono::steady_clock::now()); 142 | auto time_point = chrono::time_point_cast(chrono::steady_clock::now()); 143 | return (uint32_t)((time_point.time_since_epoch().count() + 500) / 1000 * 90); 144 | //#endif 145 | } 146 | -------------------------------------------------------------------------------- /rtsp/src/xop/H265Source.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-16 3 | 4 | #ifndef XOP_H265_SOURCE_H 5 | #define XOP_H265_SOURCE_H 6 | 7 | #include "MediaSource.h" 8 | #include "rtp.h" 9 | 10 | namespace xop 11 | { 12 | 13 | class H265Source : public MediaSource 14 | { 15 | public: 16 | static H265Source* CreateNew(uint32_t framerate=25); 17 | ~H265Source(); 18 | 19 | void Setframerate(uint32_t framerate) 20 | { framerate_ = framerate; } 21 | 22 | uint32_t GetFramerate() const 23 | { return framerate_; } 24 | 25 | virtual std::string GetMediaDescription(uint16_t port=0); 26 | 27 | virtual std::string GetAttribute(); 28 | 29 | virtual bool HandleFrame(MediaChannelId channelId, AVFrame frame); 30 | 31 | static uint32_t GetTimestamp(); 32 | 33 | private: 34 | H265Source(uint32_t framerate); 35 | 36 | uint32_t framerate_ = 25; 37 | }; 38 | 39 | } 40 | 41 | #endif 42 | 43 | 44 | -------------------------------------------------------------------------------- /rtsp/src/xop/MediaSession.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-6-8 3 | 4 | #ifndef XOP_MEDIA_SESSION_H 5 | #define XOP_MEDIA_SESSION_H 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "media.h" 16 | #include "H264Source.h" 17 | #include "H265Source.h" 18 | #include "VP8Source.h" 19 | #include "G711ASource.h" 20 | #include "AACSource.h" 21 | #include "MediaSource.h" 22 | #include "net/Socket.h" 23 | #include "net/RingBuffer.h" 24 | 25 | namespace xop 26 | { 27 | 28 | class RtpConnection; 29 | 30 | class MediaSession 31 | { 32 | public: 33 | using Ptr = std::shared_ptr; 34 | using NotifyConnectedCallback = std::function ; 35 | using NotifyDisconnectedCallback = std::function ; 36 | 37 | static MediaSession* CreateNew(std::string url_suffix="live"); 38 | virtual ~MediaSession(); 39 | 40 | bool AddSource(MediaChannelId channel_id, MediaSource* source); 41 | bool RemoveSource(MediaChannelId channel_id); 42 | 43 | bool StartMulticast(); 44 | 45 | void AddNotifyConnectedCallback(const NotifyConnectedCallback& callback); 46 | void AddNotifyDisconnectedCallback(const NotifyDisconnectedCallback& callback); 47 | 48 | std::string GetRtspUrlSuffix() const 49 | { return suffix_; } 50 | 51 | void SetRtspUrlSuffix(std::string& suffix) 52 | { suffix_ = suffix; } 53 | 54 | std::string GetSdpMessage(std::string ip, std::string session_name =""); 55 | 56 | MediaSource* GetMediaSource(MediaChannelId channel_id); 57 | 58 | bool HandleFrame(MediaChannelId channel_id, AVFrame frame); 59 | 60 | bool AddClient(SOCKET rtspfd, std::shared_ptr rtp_conn); 61 | void RemoveClient(SOCKET rtspfd); 62 | 63 | MediaSessionId GetMediaSessionId() 64 | { return session_id_; } 65 | 66 | uint32_t GetNumClient() const 67 | { return (uint32_t)clients_.size(); } 68 | 69 | bool IsMulticast() const 70 | { return is_multicast_; } 71 | 72 | std::string GetMulticastIp() const 73 | { return multicast_ip_; } 74 | 75 | uint16_t GetMulticastPort(MediaChannelId channel_id) const 76 | { 77 | if (channel_id >= MAX_MEDIA_CHANNEL) { 78 | return 0; 79 | } 80 | return multicast_port_[channel_id]; 81 | } 82 | 83 | private: 84 | friend class MediaSource; 85 | friend class RtspServer; 86 | MediaSession(std::string url_suffxx); 87 | 88 | MediaSessionId session_id_ = 0; 89 | std::string suffix_; 90 | std::string sdp_; 91 | 92 | std::vector> media_sources_; 93 | std::vector> buffer_; 94 | 95 | std::vector notify_connected_callbacks_; 96 | std::vector notify_disconnected_callbacks_; 97 | std::mutex mutex_; 98 | std::mutex map_mutex_; 99 | std::map> clients_; 100 | 101 | bool is_multicast_ = false; 102 | uint16_t multicast_port_[MAX_MEDIA_CHANNEL]; 103 | std::string multicast_ip_; 104 | std::atomic_bool has_new_client_; 105 | 106 | static std::atomic_uint last_session_id_; 107 | }; 108 | 109 | class MulticastAddr 110 | { 111 | public: 112 | static MulticastAddr& instance() 113 | { 114 | static MulticastAddr s_multi_addr; 115 | return s_multi_addr; 116 | } 117 | 118 | std::string GetAddr() 119 | { 120 | std::lock_guard lock(mutex_); 121 | std::string addr_str; 122 | struct sockaddr_in addr = { 0 }; 123 | std::random_device rd; 124 | 125 | for (int n = 0; n <= 10; n++) { 126 | uint32_t range = 0xE8FFFFFF - 0xE8000100; 127 | addr.sin_addr.s_addr = htonl(0xE8000100 + (rd()) % range); 128 | addr_str = inet_ntoa(addr.sin_addr); 129 | 130 | if (addrs_.find(addr_str) != addrs_.end()) { 131 | addr_str.clear(); 132 | } 133 | else { 134 | addrs_.insert(addr_str); 135 | break; 136 | } 137 | } 138 | 139 | return addr_str; 140 | } 141 | 142 | void Release(std::string addr) { 143 | std::lock_guard lock(mutex_); 144 | addrs_.erase(addr); 145 | } 146 | 147 | private: 148 | std::mutex mutex_; 149 | std::unordered_set addrs_; 150 | }; 151 | 152 | } 153 | 154 | #endif 155 | -------------------------------------------------------------------------------- /rtsp/src/xop/MediaSource.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-6-8 3 | 4 | #ifndef XOP_MEDIA_SOURCE_H 5 | #define XOP_MEDIA_SOURCE_H 6 | 7 | #include "media.h" 8 | #include "rtp.h" 9 | #include "net/Socket.h" 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | namespace xop 17 | { 18 | 19 | class MediaSource 20 | { 21 | public: 22 | using SendFrameCallback = std::function; 23 | 24 | MediaSource() {} 25 | virtual ~MediaSource() {} 26 | 27 | virtual MediaType GetMediaType() const 28 | { return media_type_; } 29 | 30 | virtual std::string GetMediaDescription(uint16_t port=0) = 0; 31 | 32 | virtual std::string GetAttribute() = 0; 33 | 34 | virtual bool HandleFrame(MediaChannelId channelId, AVFrame frame) = 0; 35 | virtual void SetSendFrameCallback(const SendFrameCallback callback) 36 | { send_frame_callback_ = callback; } 37 | 38 | virtual uint32_t GetPayloadType() const 39 | { return payload_; } 40 | 41 | virtual uint32_t GetClockRate() const 42 | { return clock_rate_; } 43 | 44 | protected: 45 | MediaType media_type_ = NONE; 46 | uint32_t payload_ = 0; 47 | uint32_t clock_rate_ = 0; 48 | SendFrameCallback send_frame_callback_; 49 | }; 50 | 51 | } 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /rtsp/src/xop/RtpConnection.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-6-8 3 | 4 | #ifndef XOP_RTP_CONNECTION_H 5 | #define XOP_RTP_CONNECTION_H 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "rtp.h" 13 | #include "media.h" 14 | #include "net/Socket.h" 15 | #include "net/TcpConnection.h" 16 | 17 | namespace xop 18 | { 19 | 20 | class RtspConnection; 21 | 22 | class RtpConnection 23 | { 24 | public: 25 | RtpConnection(std::weak_ptr rtsp_connection); 26 | virtual ~RtpConnection(); 27 | 28 | void SetClockRate(MediaChannelId channel_id, uint32_t clock_rate) 29 | { media_channel_info_[channel_id].clock_rate = clock_rate; } 30 | 31 | void SetPayloadType(MediaChannelId channel_id, uint32_t payload) 32 | { media_channel_info_[channel_id].rtp_header.payload = payload; } 33 | 34 | bool SetupRtpOverTcp(MediaChannelId channel_id, uint16_t rtp_channel, uint16_t rtcp_channel); 35 | bool SetupRtpOverUdp(MediaChannelId channel_id, uint16_t rtp_port, uint16_t rtcp_port); 36 | bool SetupRtpOverMulticast(MediaChannelId channel_id, std::string ip, uint16_t port); 37 | 38 | uint32_t GetRtpSessionId() const 39 | { return (uint32_t)((size_t)(this)); } 40 | 41 | uint16_t GetRtpPort(MediaChannelId channel_id) const 42 | { return local_rtp_port_[channel_id]; } 43 | 44 | uint16_t GetRtcpPort(MediaChannelId channel_id) const 45 | { return local_rtcp_port_[channel_id]; } 46 | 47 | SOCKET GetRtpSocket(MediaChannelId channel_id) const 48 | { return rtpfd_[channel_id]; } 49 | 50 | SOCKET GetRtcpSocket(MediaChannelId channel_id) const 51 | { return rtcpfd_[channel_id]; } 52 | 53 | std::string GetIp() 54 | { return rtsp_ip_; } 55 | 56 | uint16_t GetPort() 57 | { return rtsp_port_; } 58 | 59 | bool IsMulticast() const 60 | { return is_multicast_; } 61 | 62 | bool IsSetup(MediaChannelId channel_id) const 63 | { return media_channel_info_[channel_id].is_setup; } 64 | 65 | std::string GetMulticastIp(MediaChannelId channel_id) const; 66 | 67 | void Play(); 68 | void Record(); 69 | void Teardown(); 70 | 71 | std::string GetRtpInfo(const std::string& rtsp_url); 72 | int SendRtpPacket(MediaChannelId channel_id, RtpPacket pkt); 73 | 74 | bool IsClosed() const 75 | { return is_closed_; } 76 | 77 | int GetId() const; 78 | 79 | bool HasKeyFrame() const 80 | { return has_key_frame_; } 81 | 82 | private: 83 | friend class RtspConnection; 84 | friend class MediaSession; 85 | void SetFrameType(uint8_t frameType = 0); 86 | void SetRtpHeader(MediaChannelId channel_id, RtpPacket pkt); 87 | int SendRtpOverTcp(MediaChannelId channel_id, RtpPacket pkt); 88 | int SendRtpOverUdp(MediaChannelId channel_id, RtpPacket pkt); 89 | 90 | std::weak_ptr rtsp_connection_; 91 | std::string rtsp_ip_; 92 | uint16_t rtsp_port_; 93 | 94 | TransportMode transport_mode_; 95 | bool is_multicast_ = false; 96 | 97 | bool is_closed_ = false; 98 | bool has_key_frame_ = false; 99 | 100 | uint8_t frame_type_ = 0; 101 | uint16_t local_rtp_port_[MAX_MEDIA_CHANNEL]; 102 | uint16_t local_rtcp_port_[MAX_MEDIA_CHANNEL]; 103 | SOCKET rtpfd_[MAX_MEDIA_CHANNEL]; 104 | SOCKET rtcpfd_[MAX_MEDIA_CHANNEL]; 105 | 106 | struct sockaddr_in peer_addr_; 107 | struct sockaddr_in peer_rtp_addr_[MAX_MEDIA_CHANNEL]; 108 | struct sockaddr_in peer_rtcp_sddr_[MAX_MEDIA_CHANNEL]; 109 | MediaChannelInfo media_channel_info_[MAX_MEDIA_CHANNEL]; 110 | }; 111 | 112 | } 113 | 114 | #endif 115 | -------------------------------------------------------------------------------- /rtsp/src/xop/RtspConnection.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-6-8 3 | 4 | #ifndef _RTSP_CONNECTION_H 5 | #define _RTSP_CONNECTION_H 6 | 7 | #include "net/EventLoop.h" 8 | #include "net/TcpConnection.h" 9 | #include "RtpConnection.h" 10 | #include "RtspMessage.h" 11 | #include "DigestAuthentication.h" 12 | #include "rtsp.h" 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | namespace xop 20 | { 21 | 22 | class RtspServer; 23 | class MediaSession; 24 | 25 | class RtspConnection : public TcpConnection 26 | { 27 | public: 28 | using CloseCallback = std::function; 29 | 30 | enum ConnectionMode 31 | { 32 | RTSP_SERVER, 33 | RTSP_PUSHER, 34 | //RTSP_CLIENT, 35 | }; 36 | 37 | enum ConnectionState 38 | { 39 | START_CONNECT, 40 | START_PLAY, 41 | START_PUSH 42 | }; 43 | 44 | RtspConnection() = delete; 45 | RtspConnection(std::shared_ptr rtsp_server, TaskScheduler *task_scheduler, SOCKET sockfd); 46 | virtual ~RtspConnection(); 47 | 48 | MediaSessionId GetMediaSessionId() 49 | { return session_id_; } 50 | 51 | TaskScheduler *GetTaskScheduler() const 52 | { return task_scheduler_; } 53 | 54 | void KeepAlive() 55 | { alive_count_++; } 56 | 57 | bool IsAlive() const 58 | { 59 | if (IsClosed()) { 60 | return false; 61 | } 62 | 63 | if(rtp_conn_ != nullptr) { 64 | if (rtp_conn_->IsMulticast()) { 65 | return true; 66 | } 67 | } 68 | 69 | return (alive_count_ > 0); 70 | } 71 | 72 | void ResetAliveCount() 73 | { alive_count_ = 0; } 74 | 75 | int GetId() const 76 | { return task_scheduler_->GetId(); } 77 | 78 | bool IsPlay() const 79 | { return conn_state_ == START_PLAY; } 80 | 81 | bool IsRecord() const 82 | { return conn_state_ == START_PUSH; } 83 | 84 | private: 85 | friend class RtpConnection; 86 | friend class MediaSession; 87 | friend class RtspServer; 88 | friend class RtspPusher; 89 | 90 | bool OnRead(BufferReader& buffer); 91 | void OnClose(); 92 | void HandleRtcp(SOCKET sockfd); 93 | void HandleRtcp(BufferReader& buffer); 94 | bool HandleRtspRequest(BufferReader& buffer); 95 | bool HandleRtspResponse(BufferReader& buffer); 96 | 97 | void SendRtspMessage(std::shared_ptr buf, uint32_t size); 98 | 99 | void HandleCmdOption(); 100 | void HandleCmdDescribe(); 101 | void HandleCmdSetup(); 102 | void HandleCmdPlay(); 103 | void HandleCmdTeardown(); 104 | void HandleCmdGetParamter(); 105 | bool HandleAuthentication(); 106 | 107 | void SendOptions(ConnectionMode mode= RTSP_SERVER); 108 | void SendDescribe(); 109 | void SendAnnounce(); 110 | void SendSetup(); 111 | void HandleRecord(); 112 | 113 | std::atomic_int alive_count_; 114 | std::weak_ptr rtsp_; 115 | xop::TaskScheduler *task_scheduler_ = nullptr; 116 | 117 | ConnectionMode conn_mode_ = RTSP_SERVER; 118 | ConnectionState conn_state_ = START_CONNECT; 119 | MediaSessionId session_id_ = 0; 120 | 121 | bool has_auth_ = true; 122 | std::string _nonce; 123 | std::unique_ptr auth_info_; 124 | 125 | std::shared_ptr rtp_channel_; 126 | std::shared_ptr rtcp_channels_[MAX_MEDIA_CHANNEL]; 127 | std::unique_ptr rtsp_request_; 128 | std::unique_ptr rtsp_response_; 129 | std::shared_ptr rtp_conn_; 130 | }; 131 | 132 | } 133 | 134 | #endif 135 | -------------------------------------------------------------------------------- /rtsp/src/xop/RtspPusher.cpp: -------------------------------------------------------------------------------- 1 | #include "RtspPusher.h" 2 | #include "RtspConnection.h" 3 | #include "net/Logger.h" 4 | #include "net/TcpSocket.h" 5 | #include "net/Timestamp.h" 6 | #include 7 | 8 | using namespace xop; 9 | 10 | RtspPusher::RtspPusher(xop::EventLoop *event_loop) 11 | : event_loop_(event_loop) 12 | { 13 | 14 | } 15 | 16 | RtspPusher::~RtspPusher() 17 | { 18 | this->Close(); 19 | } 20 | 21 | std::shared_ptr RtspPusher::Create(xop::EventLoop* loop) 22 | { 23 | std::shared_ptr pusher(new RtspPusher(loop)); 24 | return pusher; 25 | } 26 | 27 | void RtspPusher::AddSession(MediaSession* session) 28 | { 29 | std::lock_guard locker(mutex_); 30 | media_session_.reset(session); 31 | } 32 | 33 | void RtspPusher::RemoveSession(MediaSessionId sessionId) 34 | { 35 | std::lock_guard locker(mutex_); 36 | media_session_ = nullptr; 37 | } 38 | 39 | MediaSession::Ptr RtspPusher::LookMediaSession(MediaSessionId session_id) 40 | { 41 | return media_session_; 42 | } 43 | 44 | int RtspPusher::OpenUrl(std::string url, int msec) 45 | { 46 | std::lock_guard lock(mutex_); 47 | 48 | static xop::Timestamp timestamp; 49 | int timeout = msec; 50 | if (timeout <= 0) { 51 | timeout = 10000; 52 | } 53 | 54 | timestamp.Reset(); 55 | 56 | if (!this->ParseRtspUrl(url)) { 57 | LOG_ERROR("rtsp url(%s) was illegal.\n", url.c_str()); 58 | return -1; 59 | } 60 | 61 | if (rtsp_conn_ != nullptr) { 62 | std::shared_ptr rtspConn = rtsp_conn_; 63 | SOCKET sockfd = rtspConn->GetSocket(); 64 | task_scheduler_->AddTriggerEvent([sockfd, rtspConn]() { 65 | rtspConn->Disconnect(); 66 | }); 67 | rtsp_conn_ = nullptr; 68 | } 69 | 70 | TcpSocket tcpSocket; 71 | tcpSocket.Create(); 72 | if (!tcpSocket.Connect(rtsp_url_info_.ip, rtsp_url_info_.port, timeout)) 73 | { 74 | tcpSocket.Close(); 75 | return -1; 76 | } 77 | 78 | task_scheduler_ = event_loop_->GetTaskScheduler().get(); 79 | rtsp_conn_.reset(new RtspConnection(shared_from_this(), task_scheduler_, tcpSocket.GetSocket())); 80 | event_loop_->AddTriggerEvent([this]() { 81 | rtsp_conn_->SendOptions(RtspConnection::RTSP_PUSHER); 82 | }); 83 | 84 | timeout -= (int)timestamp.Elapsed(); 85 | if (timeout < 0) { 86 | timeout = 1000; 87 | } 88 | 89 | do 90 | { 91 | xop::Timer::Sleep(100); 92 | timeout -= 100; 93 | } while (!rtsp_conn_->IsRecord() && timeout > 0); 94 | 95 | if (!rtsp_conn_->IsRecord()) { 96 | std::shared_ptr rtspConn = rtsp_conn_; 97 | SOCKET sockfd = rtspConn->GetSocket(); 98 | task_scheduler_->AddTriggerEvent([sockfd, rtspConn]() { 99 | rtspConn->Disconnect(); 100 | }); 101 | rtsp_conn_ = nullptr; 102 | return -1; 103 | } 104 | 105 | return 0; 106 | } 107 | 108 | void RtspPusher::Close() 109 | { 110 | std::lock_guard lock(mutex_); 111 | 112 | if (rtsp_conn_ != nullptr) { 113 | std::shared_ptr rtsp_conn = rtsp_conn_; 114 | SOCKET sockfd = rtsp_conn->GetSocket(); 115 | task_scheduler_->AddTriggerEvent([sockfd, rtsp_conn]() { 116 | rtsp_conn->Disconnect(); 117 | }); 118 | rtsp_conn_ = nullptr; 119 | } 120 | } 121 | 122 | bool RtspPusher::IsConnected() 123 | { 124 | std::lock_guard lock(mutex_); 125 | 126 | if (rtsp_conn_ != nullptr) { 127 | return (!rtsp_conn_->IsClosed()); 128 | } 129 | return false; 130 | } 131 | 132 | bool RtspPusher::PushFrame(MediaChannelId channelId, AVFrame frame) 133 | { 134 | std::lock_guard locker(mutex_); 135 | if (!media_session_ || !rtsp_conn_) { 136 | return false; 137 | } 138 | 139 | return media_session_->HandleFrame(channelId, frame); 140 | } -------------------------------------------------------------------------------- /rtsp/src/xop/RtspPusher.h: -------------------------------------------------------------------------------- 1 | #ifndef XOP_RTSP_PUSHER_H 2 | #define XOP_RTSP_PUSHER_H 3 | 4 | #include 5 | #include 6 | #include "rtsp.h" 7 | 8 | namespace xop 9 | { 10 | 11 | class RtspConnection; 12 | 13 | class RtspPusher : public Rtsp 14 | { 15 | public: 16 | static std::shared_ptr Create(xop::EventLoop* loop); 17 | ~RtspPusher(); 18 | 19 | void AddSession(MediaSession* session); 20 | void RemoveSession(MediaSessionId session_id); 21 | 22 | int OpenUrl(std::string url, int msec = 3000); 23 | void Close(); 24 | bool IsConnected(); 25 | 26 | bool PushFrame(MediaChannelId channelId, AVFrame frame); 27 | 28 | private: 29 | friend class RtspConnection; 30 | 31 | RtspPusher(xop::EventLoop *event_loop); 32 | MediaSession::Ptr LookMediaSession(MediaSessionId session_id); 33 | 34 | xop::EventLoop* event_loop_ = nullptr; 35 | xop::TaskScheduler* task_scheduler_ = nullptr; 36 | std::mutex mutex_; 37 | std::shared_ptr rtsp_conn_; 38 | std::shared_ptr media_session_; 39 | }; 40 | 41 | } 42 | 43 | #endif -------------------------------------------------------------------------------- /rtsp/src/xop/RtspServer.cpp: -------------------------------------------------------------------------------- 1 | #include "RtspServer.h" 2 | #include "RtspConnection.h" 3 | #include "net/SocketUtil.h" 4 | #include "net/Logger.h" 5 | 6 | using namespace xop; 7 | using namespace std; 8 | 9 | RtspServer::RtspServer(EventLoop* loop) 10 | : TcpServer(loop) 11 | { 12 | 13 | } 14 | 15 | RtspServer::~RtspServer() 16 | { 17 | 18 | } 19 | 20 | std::shared_ptr RtspServer::Create(xop::EventLoop* loop) 21 | { 22 | std::shared_ptr server(new RtspServer(loop)); 23 | return server; 24 | } 25 | 26 | MediaSessionId RtspServer::AddSession(MediaSession* session) 27 | { 28 | std::lock_guard locker(mutex_); 29 | 30 | if (rtsp_suffix_map_.find(session->GetRtspUrlSuffix()) != rtsp_suffix_map_.end()) { 31 | return 0; 32 | } 33 | 34 | std::shared_ptr media_session(session); 35 | MediaSessionId sessionId = media_session->GetMediaSessionId(); 36 | rtsp_suffix_map_.emplace(std::move(media_session->GetRtspUrlSuffix()), sessionId); 37 | media_sessions_.emplace(sessionId, std::move(media_session)); 38 | 39 | return sessionId; 40 | } 41 | 42 | void RtspServer::RemoveSession(MediaSessionId sessionId) 43 | { 44 | std::lock_guard locker(mutex_); 45 | 46 | auto iter = media_sessions_.find(sessionId); 47 | if(iter != media_sessions_.end()) { 48 | rtsp_suffix_map_.erase(iter->second->GetRtspUrlSuffix()); 49 | media_sessions_.erase(sessionId); 50 | } 51 | } 52 | 53 | MediaSession::Ptr RtspServer::LookMediaSession(const std::string& suffix) 54 | { 55 | std::lock_guard locker(mutex_); 56 | 57 | auto iter = rtsp_suffix_map_.find(suffix); 58 | if(iter != rtsp_suffix_map_.end()) { 59 | MediaSessionId id = iter->second; 60 | return media_sessions_[id]; 61 | } 62 | 63 | return nullptr; 64 | } 65 | 66 | MediaSession::Ptr RtspServer::LookMediaSession(MediaSessionId session_Id) 67 | { 68 | std::lock_guard locker(mutex_); 69 | 70 | auto iter = media_sessions_.find(session_Id); 71 | if(iter != media_sessions_.end()) { 72 | return iter->second; 73 | } 74 | 75 | return nullptr; 76 | } 77 | 78 | bool RtspServer::PushFrame(MediaSessionId session_id, MediaChannelId channel_id, AVFrame frame) 79 | { 80 | std::shared_ptr sessionPtr = nullptr; 81 | 82 | { 83 | std::lock_guard locker(mutex_); 84 | auto iter = media_sessions_.find(session_id); 85 | if (iter != media_sessions_.end()) { 86 | sessionPtr = iter->second; 87 | } 88 | else { 89 | return false; 90 | } 91 | } 92 | 93 | if (sessionPtr!=nullptr && sessionPtr->GetNumClient()!=0) { 94 | return sessionPtr->HandleFrame(channel_id, frame); 95 | } 96 | 97 | return false; 98 | } 99 | 100 | TcpConnection::Ptr RtspServer::OnConnect(SOCKET sockfd) 101 | { 102 | return std::make_shared(shared_from_this(), event_loop_->GetTaskScheduler().get(), sockfd); 103 | } 104 | 105 | -------------------------------------------------------------------------------- /rtsp/src/xop/RtspServer.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2020-4-2 3 | 4 | #ifndef XOP_RTSP_SERVER_H 5 | #define XOP_RTSP_SERVER_H 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "net/TcpServer.h" 12 | #include "rtsp.h" 13 | 14 | namespace xop 15 | { 16 | 17 | class RtspConnection; 18 | 19 | class RtspServer : public Rtsp, public TcpServer 20 | { 21 | public: 22 | static std::shared_ptr Create(xop::EventLoop* loop); 23 | ~RtspServer(); 24 | 25 | MediaSessionId AddSession(MediaSession* session); 26 | void RemoveSession(MediaSessionId sessionId); 27 | 28 | bool PushFrame(MediaSessionId sessionId, MediaChannelId channelId, AVFrame frame); 29 | 30 | private: 31 | friend class RtspConnection; 32 | 33 | RtspServer(xop::EventLoop* loop); 34 | MediaSession::Ptr LookMediaSession(const std::string& suffix); 35 | MediaSession::Ptr LookMediaSession(MediaSessionId session_id); 36 | virtual TcpConnection::Ptr OnConnect(SOCKET sockfd); 37 | 38 | std::mutex mutex_; 39 | std::unordered_map> media_sessions_; 40 | std::unordered_map rtsp_suffix_map_; 41 | }; 42 | 43 | } 44 | 45 | #endif 46 | 47 | -------------------------------------------------------------------------------- /rtsp/src/xop/VP8Source.cpp: -------------------------------------------------------------------------------- 1 | 2 | #if defined(WIN32) || defined(_WIN32) 3 | #ifndef _CRT_SECURE_NO_WARNINGS 4 | #define _CRT_SECURE_NO_WARNINGS 5 | #endif 6 | #endif 7 | 8 | #include "VP8Source.h" 9 | #include 10 | #include 11 | #if defined(__linux) || defined(__linux__) 12 | #include 13 | #endif 14 | 15 | using namespace xop; 16 | using namespace std; 17 | 18 | VP8Source::VP8Source(uint32_t framerate) 19 | : framerate_(framerate) 20 | { 21 | payload_ = 96; 22 | clock_rate_ = 90000; 23 | } 24 | 25 | VP8Source* VP8Source::CreateNew(uint32_t framerate) 26 | { 27 | return new VP8Source(framerate); 28 | } 29 | 30 | VP8Source::~VP8Source() 31 | { 32 | 33 | } 34 | 35 | string VP8Source::GetMediaDescription(uint16_t port) 36 | { 37 | char buf[100] = { 0 }; 38 | sprintf(buf, "m=video %hu RTP/AVP 96", port); 39 | return string(buf); 40 | } 41 | 42 | string VP8Source::GetAttribute() 43 | { 44 | return string("a=rtpmap:96 VP8/90000"); 45 | } 46 | 47 | bool VP8Source::HandleFrame(MediaChannelId channel_id, AVFrame frame) 48 | { 49 | uint8_t* frame_buf = frame.buffer.get(); 50 | uint32_t frame_size = frame.size; 51 | 52 | if (frame.timestamp == 0) { 53 | frame.timestamp = GetTimestamp(); 54 | } 55 | 56 | // X = R = N = 0; PartID = 0; 57 | // S = 1 if this is the first (or only) fragment of the frame 58 | uint8_t vp8_payload_descriptor = 0x10; 59 | 60 | while (frame_size > 0) { 61 | uint32_t payload_size = MAX_RTP_PAYLOAD_SIZE; 62 | 63 | RtpPacket rtp_pkt; 64 | rtp_pkt.type = frame.type; 65 | rtp_pkt.timestamp = frame.timestamp; 66 | rtp_pkt.size = RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + RTP_VPX_HEAD_SIZE + MAX_RTP_PAYLOAD_SIZE; 67 | rtp_pkt.last = 0; 68 | 69 | if (frame_size < MAX_RTP_PAYLOAD_SIZE) { 70 | payload_size = frame_size; 71 | rtp_pkt.size = RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + RTP_VPX_HEAD_SIZE + frame_size; 72 | rtp_pkt.last = 1; 73 | } 74 | 75 | rtp_pkt.data.get()[RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + 0] = vp8_payload_descriptor; 76 | memcpy(rtp_pkt.data.get() + RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE + RTP_VPX_HEAD_SIZE, frame_buf, payload_size); 77 | 78 | if (send_frame_callback_) { 79 | if (!send_frame_callback_(channel_id, rtp_pkt)) 80 | return false; 81 | } 82 | 83 | frame_buf += payload_size; 84 | frame_size -= payload_size; 85 | vp8_payload_descriptor = 0x00; 86 | } 87 | 88 | return true; 89 | } 90 | 91 | uint32_t VP8Source::GetTimestamp() 92 | { 93 | auto time_point = chrono::time_point_cast(chrono::steady_clock::now()); 94 | return (uint32_t)((time_point.time_since_epoch().count() + 500) / 1000 * 90); 95 | } 96 | -------------------------------------------------------------------------------- /rtsp/src/xop/VP8Source.h: -------------------------------------------------------------------------------- 1 | #ifndef XOP_VP8_SOURCE_H 2 | #define XOP_VP8_SOURCE_H 3 | 4 | #include "MediaSource.h" 5 | #include "rtp.h" 6 | 7 | namespace xop 8 | { 9 | 10 | class VP8Source : public MediaSource 11 | { 12 | public: 13 | static VP8Source* CreateNew(uint32_t framerate = 25); 14 | ~VP8Source(); 15 | 16 | void Setframerate(uint32_t framerate) 17 | { 18 | framerate_ = framerate; 19 | } 20 | 21 | uint32_t GetFramerate() const 22 | { 23 | return framerate_; 24 | } 25 | 26 | virtual std::string GetMediaDescription(uint16_t port = 0); 27 | 28 | virtual std::string GetAttribute(); 29 | 30 | virtual bool HandleFrame(MediaChannelId channelId, AVFrame frame); 31 | 32 | static uint32_t GetTimestamp(); 33 | 34 | private: 35 | VP8Source(uint32_t framerate); 36 | 37 | uint32_t framerate_ = 25; 38 | }; 39 | 40 | } 41 | 42 | 43 | #endif -------------------------------------------------------------------------------- /rtsp/src/xop/media.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-5-16 3 | 4 | #ifndef XOP_MEDIA_H 5 | #define XOP_MEDIA_H 6 | 7 | #include 8 | 9 | namespace xop 10 | { 11 | 12 | /* RTSP服务支持的媒体类型 */ 13 | enum MediaType 14 | { 15 | //PCMU = 0, 16 | PCMA = 8, 17 | H264 = 96, 18 | AAC = 37, 19 | H265 = 265, 20 | NONE 21 | }; 22 | 23 | enum FrameType 24 | { 25 | VIDEO_FRAME_I = 0x01, 26 | VIDEO_FRAME_P = 0x02, 27 | VIDEO_FRAME_B = 0x03, 28 | AUDIO_FRAME = 0x11, 29 | }; 30 | 31 | struct AVFrame 32 | { 33 | AVFrame(uint32_t size = 0) 34 | :buffer(new uint8_t[size + 1], std::default_delete< uint8_t[]>()) 35 | { 36 | this->size = size; 37 | type = 0; 38 | timestamp = 0; 39 | } 40 | 41 | std::shared_ptr buffer; /* 帧数据 */ 42 | uint32_t size; /* 帧大小 */ 43 | uint8_t type; /* 帧类型 */ 44 | uint32_t timestamp; /* 时间戳 */ 45 | }; 46 | 47 | static const int MAX_MEDIA_CHANNEL = 2; 48 | 49 | enum MediaChannelId 50 | { 51 | channel_0, 52 | channel_1 53 | }; 54 | 55 | typedef uint32_t MediaSessionId; 56 | 57 | } 58 | 59 | #endif 60 | 61 | -------------------------------------------------------------------------------- /rtsp/src/xop/rtp.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2021-9-2 3 | 4 | #ifndef XOP_RTP_H 5 | #define XOP_RTP_H 6 | 7 | #include 8 | #include 9 | 10 | #define RTP_HEADER_SIZE 12 11 | #define MAX_RTP_PAYLOAD_SIZE 1420 //1460 1500-20-12-8 12 | #define RTP_VERSION 2 13 | #define RTP_TCP_HEAD_SIZE 4 14 | #define RTP_VPX_HEAD_SIZE 1 15 | 16 | #define RTP_HEADER_BIG_ENDIAN 0 17 | 18 | namespace xop 19 | { 20 | 21 | enum TransportMode 22 | { 23 | RTP_OVER_TCP = 1, 24 | RTP_OVER_UDP = 2, 25 | RTP_OVER_MULTICAST = 3, 26 | }; 27 | 28 | typedef struct _RTP_header 29 | { 30 | #if RTP_HEADER_BIG_ENDIAN 31 | /* 大端序 */ 32 | unsigned char version : 2; 33 | unsigned char padding : 1; 34 | unsigned char extension : 1; 35 | unsigned char csrc : 4; 36 | unsigned char marker : 1; 37 | unsigned char payload : 7; 38 | #else 39 | /* 小端序 */ 40 | unsigned char csrc : 4; 41 | unsigned char extension : 1; 42 | unsigned char padding : 1; 43 | unsigned char version : 2; 44 | unsigned char payload : 7; 45 | unsigned char marker : 1; 46 | #endif 47 | unsigned short seq; 48 | unsigned int ts; 49 | unsigned int ssrc; 50 | } RtpHeader; 51 | 52 | struct MediaChannelInfo 53 | { 54 | RtpHeader rtp_header; 55 | 56 | // tcp 57 | uint16_t rtp_channel; 58 | uint16_t rtcp_channel; 59 | 60 | // udp 61 | uint16_t rtp_port; 62 | uint16_t rtcp_port; 63 | uint16_t packet_seq; 64 | uint32_t clock_rate; 65 | 66 | // rtcp 67 | uint64_t packet_count; 68 | uint64_t octet_count; 69 | uint64_t last_rtcp_ntp_time; 70 | 71 | bool is_setup; 72 | bool is_play; 73 | bool is_record; 74 | }; 75 | 76 | struct RtpPacket 77 | { 78 | RtpPacket() 79 | : data(new uint8_t[1600], std::default_delete()) 80 | { 81 | type = 0; 82 | size = 0; 83 | timestamp = 0; 84 | last = 0; 85 | } 86 | 87 | std::shared_ptr data; 88 | uint32_t size; 89 | uint32_t timestamp; 90 | uint8_t type; 91 | uint8_t last; 92 | }; 93 | 94 | } 95 | 96 | #endif 97 | -------------------------------------------------------------------------------- /rtsp/src/xop/rtsp.h: -------------------------------------------------------------------------------- 1 | // PHZ 2 | // 2018-6-8 3 | 4 | #ifndef XOP_RTSP_H 5 | #define XOP_RTSP_H 6 | 7 | #include 8 | #include 9 | #include "MediaSession.h" 10 | #include "net/Acceptor.h" 11 | #include "net/EventLoop.h" 12 | #include "net/Socket.h" 13 | #include "net/Timer.h" 14 | 15 | namespace xop 16 | { 17 | 18 | struct RtspUrlInfo 19 | { 20 | std::string url; 21 | std::string ip; 22 | uint16_t port; 23 | std::string suffix; 24 | }; 25 | 26 | class Rtsp : public std::enable_shared_from_this 27 | { 28 | public: 29 | Rtsp() : has_auth_info_(false) {} 30 | virtual ~Rtsp() {} 31 | 32 | virtual void SetAuthConfig(std::string realm, std::string username, std::string password) 33 | { 34 | realm_ = realm; 35 | username_ = username; 36 | password_ = password; 37 | has_auth_info_ = true; 38 | 39 | if (realm_=="" || username=="") { 40 | has_auth_info_ = false; 41 | } 42 | } 43 | 44 | virtual void SetVersion(std::string version) // SDP Session Name 45 | { version_ = std::move(version); } 46 | 47 | virtual std::string GetVersion() 48 | { return version_; } 49 | 50 | virtual std::string GetRtspUrl() 51 | { return rtsp_url_info_.url; } 52 | 53 | bool ParseRtspUrl(std::string url) 54 | { 55 | char ip[100] = { 0 }; 56 | char suffix[100] = { 0 }; 57 | uint16_t port = 0; 58 | #if defined(__linux) || defined(__linux__) 59 | if (sscanf(url.c_str() + 7, "%[^:]:%hu/%s", ip, &port, suffix) == 3) 60 | #elif defined(WIN32) || defined(_WIN32) 61 | if (sscanf_s(url.c_str() + 7, "%[^:]:%hu/%s", ip, 100, &port, suffix, 100) == 3) 62 | #endif 63 | { 64 | rtsp_url_info_.port = port; 65 | } 66 | #if defined(__linux) || defined(__linux__) 67 | else if (sscanf(url.c_str() + 7, "%[^/]/%s", ip, suffix) == 2) 68 | #elif defined(WIN32) || defined(_WIN32) 69 | else if (sscanf_s(url.c_str() + 7, "%[^/]/%s", ip, 100, suffix, 100) == 2) 70 | #endif 71 | { 72 | rtsp_url_info_.port = 554; 73 | } 74 | else 75 | { 76 | //LOG("%s was illegal.\n", url.c_str()); 77 | return false; 78 | } 79 | 80 | rtsp_url_info_.ip = ip; 81 | rtsp_url_info_.suffix = suffix; 82 | rtsp_url_info_.url = url; 83 | return true; 84 | } 85 | 86 | protected: 87 | friend class RtspConnection; 88 | 89 | virtual MediaSession::Ptr LookMediaSession(const std::string& suffix) 90 | { return nullptr; } 91 | 92 | virtual MediaSession::Ptr LookMediaSession(MediaSessionId sessionId) 93 | { return nullptr; } 94 | 95 | bool has_auth_info_ = false; 96 | std::string realm_; 97 | std::string username_; 98 | std::string password_; 99 | std::string version_; 100 | struct RtspUrlInfo rtsp_url_info_; 101 | }; 102 | 103 | } 104 | 105 | #endif 106 | 107 | 108 | -------------------------------------------------------------------------------- /test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(BEDROCK_DEPENDENT_LIBS 2 | bedrock_lib 3 | bedrock_encoder 4 | bedrock_rtsp 5 | ) 6 | set(RTSP_INCLUDE ${CMAKE_CURRENT_LIST_DIR}/../rtsp/src 7 | ${CMAKE_CURRENT_LIST_DIR}/../rtsp/src/3rdpart 8 | ${CMAKE_CURRENT_LIST_DIR}/../rtsp/src/net 9 | ${CMAKE_CURRENT_LIST_DIR}/../rtsp/src/xop 10 | ) 11 | 12 | include_directories(${RTSP_INCLUDE}) 13 | 14 | add_executable(bedrock_test bedrock_test.cpp) 15 | add_executable(bedrock_cfg_test bedrock_cfgf_test.cpp) 16 | add_executable(bedrock_mpi_vi_test bedrock_mpi_vi_test.cpp) 17 | 18 | target_link_libraries(bedrock_test ${BEDROCK_DEPENDENT_LIBS}) 19 | target_link_libraries(bedrock_cfg_test ${BEDROCK_DEPENDENT_LIBS}) 20 | target_link_libraries(bedrock_mpi_vi_test ${BEDROCK_DEPENDENT_LIBS}) 21 | 22 | install(TARGETS bedrock_test DESTINATION bin) 23 | install(TARGETS bedrock_cfg_test DESTINATION bin) 24 | install(TARGETS bedrock_mpi_vi_test DESTINATION bin) -------------------------------------------------------------------------------- /test/bedrock_cfgf_test.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | * @Description: 3 | * @Version: 2.0 4 | * @Autor: Bedrock 5 | * @Date: 2022-01-21 19:51:51 6 | * @LastEditors: Bedrock 7 | * @LastEditTime: 2022-03-19 10:51:21 8 | * @Author: Bedrock 9 | * @FilePath: /bedrock_encoder/test/bedrock_cfgf_test.cpp 10 | * @版权声明 11 | */ 12 | 13 | #include 14 | #include "filecfg.h" 15 | #include "bedrock_enc.h" 16 | 17 | static void mpi_venc_test_show_options(const Bedrock_VENC_CTX_S *ctx) { 18 | RK_PRINT("cmd parse result:\n"); 19 | RK_PRINT("input file name : %s\n", ctx->srcFileUri); 20 | RK_PRINT("output file name : %s\n", ctx->dstFilePath); 21 | RK_PRINT("src width : %d\n", ctx->u32SrcWidth); 22 | RK_PRINT("src height : %d\n", ctx->u32SrcHeight); 23 | RK_PRINT("src virWidth : %d\n", ctx->u32srcVirWidth); 24 | RK_PRINT("src virHeight : %d\n", ctx->u32srcVirHeight); 25 | RK_PRINT("src pixel format : %d\n", ctx->u32SrcPixFormat); 26 | RK_PRINT("encode codec type : %d\n", ctx->u32DstCodec); 27 | RK_PRINT("loop count : %d\n", ctx->s32LoopCount); 28 | RK_PRINT("channel index : %d\n", ctx->u32ChnIndex); 29 | RK_PRINT("channel num : %d\n", ctx->u32ChNum); 30 | RK_PRINT("output buffer count : %d\n", ctx->u32StreamBufCnt); 31 | RK_PRINT("one picture size : %d\n", ctx->u32BufferSize); 32 | return; 33 | } 34 | 35 | int main(int argc, char const *argv[]) 36 | { 37 | RK_S32 ret = RK_SUCCESS; 38 | Bedrock_VENC_CTX_S ctx; 39 | const char *file = NULL; 40 | if (argc == 1) { 41 | printf("Using defaule cfg~!\n"); 42 | } 43 | else if (argc == 2) { 44 | printf("Please Input file name \n"); 45 | printf("bedrock_cfg_test [-c] filename \n"); 46 | } 47 | memset(&ctx, 0, sizeof(Bedrock_VENC_CTX_S)); 48 | ret = init_argc_for_cfg(&ctx, file); 49 | mpi_venc_test_show_options(&ctx); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /test/bedrock_mpi.cfg: -------------------------------------------------------------------------------- 1 | name = "bedrock_mpi" 2 | 3 | mpiarg = 4 | { 5 | test = ({ 6 | s32LoopCount = 1; 7 | u32StreamBufCnt = 8; 8 | u32ChNum = 1; 9 | u32SrcPixFormat = 0; 10 | u32DstCodec = 8; 11 | srcFileUri = "/nfs/bridge-far_cif.yuv"; 12 | dstFilePath = "/nfs/bride"; 13 | u32SrcWidth = 352; 14 | u32SrcHeight = 288; 15 | }) 16 | } -------------------------------------------------------------------------------- /test/debug_log_test.c: -------------------------------------------------------------------------------- 1 | /*** 2 | * @Description: 3 | * @Version: 2.0 4 | * @Autor: Bedrock 5 | * @Date: 2022-01-01 09:57:51 6 | * @LastEditors: Bedrock 7 | * @LastEditTime: 2022-01-01 11:26:23 8 | * @Author: Bedrock 9 | * @FilePath: /bedrock_encoder/test/debug_log_test.c 10 | * @版权声明 11 | */ 12 | 13 | #include "bedrock_control.h" 14 | #include "bedrock_log.h" 15 | #include 16 | #include 17 | #include 18 | 19 | /*** 20 | * @description: 21 | * @param {*} 22 | * @return {*} 23 | * @author: Bedrock 24 | * @use: 打印等级测试 25 | */ 26 | void dlog_level_test() 27 | { 28 | LOG_ERROR("0Main Start\n"); 29 | LOG_WARN("1Main Start\n"); 30 | LOG_INFO("2Main Start\n"); 31 | LOG_DEBUG("3Main Start\n"); 32 | } 33 | /*** 34 | * @description: 35 | * @param {*} 36 | * @return {*} 37 | * @author: Bedrock 38 | * @use: log打印等级调整 39 | */ 40 | extern int bedrock_app_log_level; 41 | 42 | void adjust_log_level() 43 | { 44 | if (!access(BEDROCK_DEBUG_LOG, 0)) 45 | { 46 | bedrock_app_log_level = LOG_DEBUG; 47 | } 48 | char *log_level = getenv("bedrock_app_log_level"); 49 | if (log_level) 50 | { 51 | LOG_INFO("bedrock_app_log=%d", atoi(log_level)); 52 | bedrock_app_log_level = atoi(log_level); 53 | } 54 | } --------------------------------------------------------------------------------