编译Android系统源码中screenrecord
本文是上一篇编译 Android 系统源码中 screencap的姐妹篇,而且比上一篇的步骤稍微复杂一些。
下载源码
和上一篇一样,下面只会下载需要用到的部分,使用 android10 分支。可以从android.googlesource.com下载各模块源码的压缩包。
https://android.googlesource.com/platform/system/core/+archive/refs/heads/android10-release.tar.gz
https://android.googlesource.com/platform/frameworks/av/+archive/refs/heads/android10-release.tar.gz
https://android.googlesource.com/platform/frameworks/native/+archive/refs/heads/android10-release.tar.gz
https://android.googlesource.com/platform/prebuilts/vndk/v30/+archive/refs/heads/main.tar.gz
然后将对应模块的源码解压到对应的目录。
tree /root/android10/ -L 3
/root/android10/
├── platform
│ ├── frameworks
│ │ ├── av
│ │ └── native
│ ├── prebuilts
│ │ └── vndk
│ └── system
│ └── core
最后,还需要screenrecord
的源码,这次是一个目录中的多个源码文件。
https://android.googlesource.com/platform/frameworks/av/+archive/refs/heads/android10-release/cmds/screenrecord.tar.gz
配置项目
创建一个 CMake 项目。
cmake_minimum_required(VERSION 3.15)
set(CMAKE_CXX_STANDARD 17)
# 获取当前文件夹名
string(REGEX REPLACE ".*/(.*)" "\\1" CURRENT_FOLDER ${CMAKE_CURRENT_SOURCE_DIR})
project(${CURRENT_FOLDER})
# ##############################################################################
set(subject ${PROJECT_NAME})
include_directories(${subject} include/
build/aidl/include
${ANDROID_PLATFORM_DIR}/system/core/include
${ANDROID_PLATFORM_DIR}/system/core/base/include
${ANDROID_PLATFORM_DIR}/frameworks/av/include
${ANDROID_PLATFORM_DIR}/frameworks/av/media/libstagefright
${ANDROID_PLATFORM_DIR}/frameworks/av/media/libstagefright/include
${ANDROID_PLATFORM_DIR}/frameworks/av/media/libmedia
${ANDROID_PLATFORM_DIR}/frameworks/av/media/libmedia/include
${ANDROID_PLATFORM_DIR}/frameworks/native/include
${ANDROID_PLATFORM_DIR}/frameworks/native/include/media/openmax
${ANDROID_PLATFORM_DIR}/frameworks/native/libs/math/include
${ANDROID_PLATFORM_DIR}/frameworks/native/libs/nativebase/include
${ANDROID_PLATFORM_DIR}/frameworks/native/libs/nativewindow/include
${ANDROID_PLATFORM_DIR}/frameworks/native/libs/binder/include
${ANDROID_PLATFORM_DIR}/frameworks/native/libs/gui/include
${ANDROID_PLATFORM_DIR}/frameworks/native/libs/ui/include
${ANDROID_PLATFORM_DIR}/prebuilts/vndk/v30/${PREBUILT}/include/system/libhidl/base/include
${ANDROID_PLATFORM_DIR}/prebuilts/vndk/v30/${PREBUILT}/include/hardware/libhardware/include
${ANDROID_PLATFORM_DIR}/prebuilts/vndk/v30/${PREBUILT}/include/system/libhidl/transport/token/1.0/utils/include
${ANDROID_PLATFORM_DIR}/prebuilts/vndk/v30/${PREBUILT}/include/generated-headers/hardware/interfaces/graphics/common/1.0/android.hardware.graphics.common@1.0_genc++_headers/gen
${ANDROID_PLATFORM_DIR}/prebuilts/vndk/v30/${PREBUILT}/include/generated-headers/hardware/interfaces/graphics/common/1.1/android.hardware.graphics.common@1.1_genc++_headers/gen
${ANDROID_PLATFORM_DIR}/prebuilts/vndk/v30/${PREBUILT}/include/generated-headers/hardware/interfaces/graphics/common/1.2/android.hardware.graphics.common@1.2_genc++_headers/gen
${ANDROID_PLATFORM_DIR}/prebuilts/vndk/v30/${PREBUILT}/include/generated-headers/hardware/interfaces/graphics/bufferqueue/1.0/android.hardware.graphics.bufferqueue@1.0_genc++_headers/gen
${ANDROID_PLATFORM_DIR}/prebuilts/vndk/v30/${PREBUILT}/include/generated-headers/hardware/interfaces/graphics/bufferqueue/2.0/android.hardware.graphics.bufferqueue@2.0_genc++_headers/gen
${ANDROID_PLATFORM_DIR}/prebuilts/vndk/v30/${PREBUILT}/include/generated-headers/system/libhidl/transport/base/1.0/android.hidl.base@1.0_genc++_headers/gen
${ANDROID_PLATFORM_DIR}/prebuilts/vndk/v30/${PREBUILT}/include/generated-headers/system/libhidl/transport/manager/1.0/android.hidl.manager@1.0_genc++_headers/gen
${ANDROID_PLATFORM_DIR}/prebuilts/vndk/v30/${PREBUILT}/include/generated-headers/hardware/interfaces/media/1.0/android.hardware.media@1.0_genc++_headers/gen
)
message(STATUS "Include directories:")
get_property(include_dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
foreach(dir ${include_dirs})
message(STATUS " - ${dir}")
endforeach()
link_directories(${subject} lib/${ANDROID_ABI})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/bin/${platform}/${ANDROID_ABI}/debug)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/bin/${platform}/${ANDROID_ABI}/release)
# 查找目录下所有源文件并保存到变量
# aux_source_directory(src SRCS)
# 搜索源码文件保存到变量
FILE(GLOB SRCS src/*.h src/*.cpp)
add_executable(${subject} ${SRCS})
target_compile_options(${subject} PRIVATE
-fno-rtti
-Wno-inconsistent-missing-override
)
target_link_options(${subject} PRIVATE
-Wl,--allow-shlib-undefined
)
target_link_libraries(${subject}
stagefright
media
media_omx
utils
binder
stagefright_foundation
jpeg
ui
gui
cutils
log
EGL
GLESv2
)
set_property(TARGET ${subject} PROPERTY DEBUG_POSTFIX _${ANDROID_ABI}_d)
set_property(TARGET ${subject} PROPERTY RELEASE_POSTFIX _${ANDROID_ABI}_r)
# ##############################################################################
项目需要链接stagefright
、media
、media_omx
、utils
、binder
、stagefright_foundation
、jpeg
、ui
、gui
、cutils
、log
、EGL
、GLESv2
这些库,直接从安卓机上拷贝到lib
目录就好。
使用一个脚本,一键编译。
export ANDROID_NDK_ROOT=/root/android-ndk-r20b
export ANDROID_PLATFORM_DIR=/root/android10/platform
export PREBUILT=arm64
export platform=android-29
export abi=arm64-v8a
export BUILDPATH=$(pwd)/build
export INSTALLPATH=$(pwd)/bin
rm -r $BUILDPATH
rm -r $INSTALLPATH
aidl -h build/aidl/include -o build/aidl/cpp --lang=cpp $ANDROID_PLATFORM_DIR/frameworks/av/media/libmedia/aidl/android/IOMXNode.aidl
aidl -I $ANDROID_PLATFORM_DIR/frameworks/av/media/libmedia/aidl -h build/aidl/include -o build/aidl/cpp --lang=cpp $ANDROID_PLATFORM_DIR/frameworks/av/media/libmedia/aidl/android/IGraphicBufferSource.aidl
export buildparams="-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_ROOT/build/cmake/android.toolchain.cmake -DCMAKE_MAKE_PROGRAM=$ANDROID_NDK_ROOT/prebuilt/linux-x86_64/bin/make"
cmake -G "Unix Makefiles" -S . -B $BUILDPATH \
-DANDROID_NDK=$ANDROID_NDK_ROOT $buildparams \
-DCMAKE_BUILD_TYPE=release -DANDROID_ABI=$abi -DANDROID_PLATFORM=$platform \
-DANDROID_PLATFORM_DIR=$ANDROID_PLATFORM_DIR -DPREBUILT=$PREBUILT
cmake --build $BUILDPATH --clean-first --config release --target all -- -j1
其中从 aidl 文件生成头文件的命令很关键,因为这部分的头文件只能通过 aidl 生成。
整个项目的布局。
tree /root/AnalysisAVP/example/android/record/screenrecord/ -L 2
/root/AnalysisAVP/example/android/record/screenrecord/
├── build_android.sh
├── CMakeLists.txt
├── lib
│ ├── arm64-v8a
│ └── armeabi-v7a
└── src
├── EglWindow.cpp
├── EglWindow.h
├── FontBitmap.h
├── FrameOutput.cpp
├── FrameOutput.h
├── Overlay.cpp
├── Overlay.h
├── Program.cpp
├── Program.h
├── screenrecord.cpp
├── screenrecord.h
├── TextRenderer.cpp
└── TextRenderer.h
编译运行
root@gongluck:~/AnalysisAVP/example/android/record/screenrecord# ./build_android.sh
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /root/android-ndk-r20b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /root/android-ndk-r20b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Include directories:
-- - /root/AnalysisAVP/example/android/record/screenrecord/screenrecord
-- - /root/AnalysisAVP/example/android/record/screenrecord/include
-- - /root/AnalysisAVP/example/android/record/screenrecord/build/aidl/include
-- - /root/android10/platform/system/core/include
-- - /root/android10/platform/system/core/base/include
-- - /root/android10/platform/frameworks/av/include
-- - /root/android10/platform/frameworks/av/media/libstagefright
-- - /root/android10/platform/frameworks/av/media/libstagefright/include
-- - /root/android10/platform/frameworks/av/media/libmedia
-- - /root/android10/platform/frameworks/av/media/libmedia/include
-- - /root/android10/platform/frameworks/native/include
-- - /root/android10/platform/frameworks/native/include/media/openmax
-- - /root/android10/platform/frameworks/native/libs/math/include
-- - /root/android10/platform/frameworks/native/libs/nativebase/include
-- - /root/android10/platform/frameworks/native/libs/nativewindow/include
-- - /root/android10/platform/frameworks/native/libs/binder/include
-- - /root/android10/platform/frameworks/native/libs/gui/include
-- - /root/android10/platform/frameworks/native/libs/ui/include
-- - /root/android10/platform/prebuilts/vndk/v30/arm64/include/system/libhidl/base/include
-- - /root/android10/platform/prebuilts/vndk/v30/arm64/include/hardware/libhardware/include
-- - /root/android10/platform/prebuilts/vndk/v30/arm64/include/system/libhidl/transport/token/1.0/utils/include
-- - /root/android10/platform/prebuilts/vndk/v30/arm64/include/generated-headers/hardware/interfaces/graphics/common/1.0/android.hardware.graphics.common@1.0_genc++_headers/gen
-- - /root/android10/platform/prebuilts/vndk/v30/arm64/include/generated-headers/hardware/interfaces/graphics/common/1.1/android.hardware.graphics.common@1.1_genc++_headers/gen
-- - /root/android10/platform/prebuilts/vndk/v30/arm64/include/generated-headers/hardware/interfaces/graphics/common/1.2/android.hardware.graphics.common@1.2_genc++_headers/gen
-- - /root/android10/platform/prebuilts/vndk/v30/arm64/include/generated-headers/hardware/interfaces/graphics/bufferqueue/1.0/android.hardware.graphics.bufferqueue@1.0_genc++_headers/gen
-- - /root/android10/platform/prebuilts/vndk/v30/arm64/include/generated-headers/hardware/interfaces/graphics/bufferqueue/2.0/android.hardware.graphics.bufferqueue@2.0_genc++_headers/gen
-- - /root/android10/platform/prebuilts/vndk/v30/arm64/include/generated-headers/system/libhidl/transport/base/1.0/android.hidl.base@1.0_genc++_headers/gen
-- - /root/android10/platform/prebuilts/vndk/v30/arm64/include/generated-headers/system/libhidl/transport/manager/1.0/android.hidl.manager@1.0_genc++_headers/gen
-- - /root/android10/platform/prebuilts/vndk/v30/arm64/include/generated-headers/hardware/interfaces/media/1.0/android.hardware.media@1.0_genc++_headers/gen
-- Configuring done
-- Generating done
-- Build files have been written to: /root/AnalysisAVP/example/android/record/screenrecord/build
[ 14%] Building CXX object CMakeFiles/screenrecord.dir/src/EglWindow.cpp.o
[ 28%] Building CXX object CMakeFiles/screenrecord.dir/src/FrameOutput.cpp.o
[ 42%] Building CXX object CMakeFiles/screenrecord.dir/src/Overlay.cpp.o
[ 57%] Building CXX object CMakeFiles/screenrecord.dir/src/Program.cpp.o
[ 71%] Building CXX object CMakeFiles/screenrecord.dir/src/TextRenderer.cpp.o
[ 85%] Building CXX object CMakeFiles/screenrecord.dir/src/screenrecord.cpp.o
[100%] Linking CXX executable ../bin/arm64-v8a/release/screenrecord_arm64-v8a_r
[100%] Built target screenrecord
root@gongluck:~/AnalysisAVP/example/android/record/screenrecord# adb push bin/arm64-v8a/release/screenrecord_arm64-v8a_r /data/local/tmp
bin/arm64-v8a/release/screenrecord_arm64-v8a_r: 1 file pushed. 0.5 MB/s (2162456 bytes in 4.167s)
root@gongluck:~/AnalysisAVP/example/android/record/screenrecord# adb shell "data/local/tmp/screenrecord_arm64-v8a_r --time-limit 5 /data/local/tmp/test.mp4"
root@gongluck:~/AnalysisAVP/example/android/record/screenrecord# adb shell "file /data/local/tmp/test.mp4"
/data/local/tmp/test.mp4: data
完整代码
https://github.com/gongluck/AnalysisAVP/tree/master/example/android/record/screenrecord
Last modified on 2024-09-21