-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement skeleton of native litr-muxers module #239
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/build | ||
|
||
# FFmpeg symlink | ||
src/main/cpp/ffmpeg |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Litr Muxers module | ||
|
||
The Litr Muxers module provides `NativeMediaMuxerMediaTarget`, which uses FFmpeg for muxing | ||
individual streams into a target file container. | ||
|
||
## Build instructions (Linux, macOS) | ||
|
||
It is necessary to manually build the FFmpeg library, so that gradle can bundle the FFmpeg binaries | ||
in the APK: | ||
|
||
* Set the following shell variable: | ||
|
||
``` | ||
cd "<path to project checkout>" | ||
FFMPEG_MODULE_PATH="$(pwd)/litr-muxers/src/main" | ||
``` | ||
|
||
* Download the [Android NDK][] and set its location in a shell variable. | ||
This build configuration has been tested on NDK r22b. | ||
|
||
``` | ||
NDK_PATH="<path to Android NDK>" | ||
``` | ||
|
||
* Set the host platform (use "darwin-x86_64" for Mac OS X): | ||
|
||
``` | ||
HOST_PLATFORM="linux-x86_64" | ||
``` | ||
|
||
* Fetch FFmpeg and checkout an appropriate branch. We cannot guarantee | ||
compatibility with all versions of FFmpeg. We currently recommend version 4.2: | ||
|
||
``` | ||
cd "<preferred location for ffmpeg>" && \ | ||
git clone git://source.ffmpeg.org/ffmpeg && \ | ||
cd ffmpeg && \ | ||
git checkout release/4.2 && \ | ||
FFMPEG_PATH="$(pwd)" | ||
``` | ||
|
||
* Add a link to the FFmpeg source code in the FFmpeg module `cpp` directory. | ||
|
||
``` | ||
cd "${FFMPEG_MODULE_PATH}/cpp" && \ | ||
ln -s "$FFMPEG_PATH" ffmpeg | ||
``` | ||
|
||
* Execute `build_ffmpeg.sh` to build FFmpeg for `armeabi-v7a`, `arm64-v8a`, | ||
`x86` and `x86_64`. The script can be edited if you need to build for | ||
different architectures: | ||
|
||
``` | ||
cd "${FFMPEG_MODULE_PATH}/cpp" && \ | ||
chmod +x build_ffmpeg.sh && \ | ||
./build_ffmpeg.sh \ | ||
"${FFMPEG_MODULE_PATH}" "${NDK_PATH}" "${HOST_PLATFORM}" | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
plugins { | ||
id 'com.android.library' | ||
id 'org.jetbrains.kotlin.android' | ||
} | ||
|
||
android { | ||
namespace 'com.linkedin.android.litr.muxers' | ||
|
||
compileSdkVersion rootProject.ext.compileSdkVersion | ||
buildToolsVersion rootProject.ext.buildToolsVersion | ||
|
||
defaultConfig { | ||
minSdkVersion rootProject.ext.minSdkVersion | ||
targetSdkVersion rootProject.ext.targetSdkVersion | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
|
||
kotlinOptions { | ||
freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn" | ||
} | ||
|
||
externalNativeBuild { | ||
cmake { | ||
path file('src/main/cpp/CMakeLists.txt') | ||
version '3.10.2' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation project(':litr') | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
</manifest> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
cmake_minimum_required(VERSION 3.10.2) | ||
|
||
# Enable C++11 features. | ||
set(CMAKE_CXX_STANDARD 11) | ||
|
||
project(LiTrMuxers C CXX) | ||
|
||
# Additional flags needed for "arm64-v8a" from NDK 23.1.7779620 and above. | ||
if(${ANDROID_ABI} MATCHES "arm64-v8a") | ||
set(CMAKE_CXX_FLAGS "-Wl,-Bsymbolic") | ||
endif() | ||
|
||
# If locally built ffmpeg binaries are available, we will prioritise these. This is preferable as | ||
# ensures they are built to match exactly what the consumer requires. However, we will allow the | ||
# build to fallback to those bundled with the repository. | ||
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/ffmpeg") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @izzytwosheds - After our chat yesterday, I added this condition. If we detect locally built binaries, we will use them. However, I have included versions that i've built myself that it will fall back too. If you're happy with this, you may want to replace those bundled binaries with versions you've built yourself. |
||
set(ffmpeg_location "${CMAKE_CURRENT_SOURCE_DIR}/ffmpeg") | ||
set(ffmpeg_binaries "${ffmpeg_location}/android-libs/${ANDROID_ABI}") | ||
else() | ||
set(ffmpeg_location "${CMAKE_CURRENT_SOURCE_DIR}/ffmpeg_bundled") | ||
set(ffmpeg_binaries "${ffmpeg_location}/${ANDROID_ABI}") | ||
endif() | ||
|
||
foreach(ffmpeg_lib avutil avcodec avformat) | ||
set(ffmpeg_lib_filename lib${ffmpeg_lib}.so) | ||
set(ffmpeg_lib_file_path ${ffmpeg_binaries}/${ffmpeg_lib_filename}) | ||
add_library( | ||
${ffmpeg_lib} | ||
SHARED | ||
IMPORTED) | ||
set_target_properties( | ||
${ffmpeg_lib} PROPERTIES | ||
IMPORTED_LOCATION | ||
${ffmpeg_lib_file_path}) | ||
endforeach() | ||
|
||
include_directories(${ffmpeg_location}) | ||
find_library(log-lib log) | ||
|
||
add_library(litr-muxers SHARED | ||
FFmpeg.h | ||
Logging.h | ||
) | ||
|
||
target_link_libraries(litr-muxers | ||
PRIVATE android | ||
PRIVATE avcodec | ||
PRIVATE avutil | ||
PRIVATE avformat | ||
PRIVATE ${log-lib}) | ||
|
||
set_property(TARGET litr-muxers PROPERTY LINKER_LANGUAGE CXX) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was added because after removing all the CPP files, cmake didn't know how to link :D |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright 2022 LinkedIn Corporation | ||
* All Rights Reserved. | ||
* | ||
* Licensed under the BSD 2-Clause License (the "License"). See License in the project root for | ||
* license information. | ||
* | ||
* Author: Ian Bird | ||
*/ | ||
|
||
#ifndef LITR_FFMPEG_H | ||
#define LITR_FFMPEG_H | ||
|
||
extern "C" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the code in this module will be C++, and libavformat (and deps) are C, I created this header to make it easy to import/include. |
||
#include "libavutil/log.h" | ||
#include "libavformat/avformat.h" | ||
} | ||
|
||
#endif //LITR_FFMPEG_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2022 LinkedIn Corporation | ||
* All Rights Reserved. | ||
* | ||
* Licensed under the BSD 2-Clause License (the "License"). See License in the project root for | ||
* license information. | ||
* | ||
* Author: Ian Bird | ||
*/ | ||
|
||
#ifndef LITR_LOGGING_H | ||
#define LITR_LOGGING_H | ||
|
||
#include <android/log.h> | ||
|
||
#define LOG_TAG "LiTrMuxers_JNI" | ||
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logging functions that can be used from C++ that will end up in Android's logcat. |
||
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) | ||
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) | ||
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) | ||
|
||
#endif //LITR_LOGGING_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#!/bin/bash | ||
|
||
FFMPEG_MODULE_PATH=$1 | ||
NDK_PATH=$2 | ||
HOST_PLATFORM=$3 | ||
ENABLED_DECODERS=("${@:4}") | ||
JOBS=$(nproc 2> /dev/null || sysctl -n hw.ncpu 2> /dev/null || echo 4) | ||
echo "Using $JOBS jobs for make" | ||
COMMON_OPTIONS=" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This script is based upon ExoPlayer's ffmpeg module, except with everything disabled. I've only enabled what is required to match the capabilities of Android's MediaMuxer, with the addition of segmented support. |
||
--target-os=android | ||
--enable-shared | ||
--disable-static | ||
--disable-doc | ||
--disable-all | ||
--enable-avcodec | ||
--enable-avformat | ||
--enable-protocol=file | ||
--enable-protocol=hls | ||
--enable-muxer=mp4 | ||
--enable-muxer=matroska | ||
--enable-muxer=hls | ||
--enable-muxer=stream_segment | ||
--enable-parser=aac | ||
--enable-parser=aac_latm | ||
--enable-parser=h264 | ||
--enable-parser=hevc | ||
--extra-ldexeflags=-pie | ||
" | ||
TOOLCHAIN_PREFIX="${NDK_PATH}/toolchains/llvm/prebuilt/${HOST_PLATFORM}/bin" | ||
cd "${FFMPEG_MODULE_PATH}/cpp/ffmpeg" | ||
./configure \ | ||
--libdir=android-libs/armeabi-v7a \ | ||
--arch=arm \ | ||
--cpu=armv7-a \ | ||
--cross-prefix="${TOOLCHAIN_PREFIX}/armv7a-linux-androideabi16-" \ | ||
--nm="${TOOLCHAIN_PREFIX}/llvm-nm" \ | ||
--ar="${TOOLCHAIN_PREFIX}/llvm-ar" \ | ||
--ranlib="${TOOLCHAIN_PREFIX}/llvm-ranlib" \ | ||
--strip="${TOOLCHAIN_PREFIX}/llvm-strip" \ | ||
--extra-cflags="-march=armv7-a -mfloat-abi=softfp" \ | ||
--extra-ldflags="-Wl,--fix-cortex-a8" \ | ||
${COMMON_OPTIONS} | ||
make -j$JOBS | ||
make install-libs | ||
make clean | ||
./configure \ | ||
--libdir=android-libs/arm64-v8a \ | ||
--arch=aarch64 \ | ||
--cpu=armv8-a \ | ||
--cross-prefix="${TOOLCHAIN_PREFIX}/aarch64-linux-android21-" \ | ||
--nm="${TOOLCHAIN_PREFIX}/llvm-nm" \ | ||
--ar="${TOOLCHAIN_PREFIX}/llvm-ar" \ | ||
--ranlib="${TOOLCHAIN_PREFIX}/llvm-ranlib" \ | ||
--strip="${TOOLCHAIN_PREFIX}/llvm-strip" \ | ||
${COMMON_OPTIONS} | ||
make -j$JOBS | ||
make install-libs | ||
make clean | ||
./configure \ | ||
--libdir=android-libs/x86 \ | ||
--arch=x86 \ | ||
--cpu=i686 \ | ||
--cross-prefix="${TOOLCHAIN_PREFIX}/i686-linux-android16-" \ | ||
--nm="${TOOLCHAIN_PREFIX}/llvm-nm" \ | ||
--ar="${TOOLCHAIN_PREFIX}/llvm-ar" \ | ||
--ranlib="${TOOLCHAIN_PREFIX}/llvm-ranlib" \ | ||
--strip="${TOOLCHAIN_PREFIX}/llvm-strip" \ | ||
--disable-asm \ | ||
${COMMON_OPTIONS} | ||
make -j$JOBS | ||
make install-libs | ||
make clean | ||
./configure \ | ||
--libdir=android-libs/x86_64 \ | ||
--arch=x86_64 \ | ||
--cpu=x86_64 \ | ||
--cross-prefix="${TOOLCHAIN_PREFIX}/x86_64-linux-android21-" \ | ||
--nm="${TOOLCHAIN_PREFIX}/llvm-nm" \ | ||
--ar="${TOOLCHAIN_PREFIX}/llvm-ar" \ | ||
--ranlib="${TOOLCHAIN_PREFIX}/llvm-ranlib" \ | ||
--strip="${TOOLCHAIN_PREFIX}/llvm-strip" \ | ||
--disable-asm \ | ||
${COMMON_OPTIONS} | ||
make -j$JOBS | ||
make install-libs | ||
make clean |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
include ':litr-demo', ':litr', ':litr-filters' | ||
include ':litr-demo', ':litr', ':litr-filters', ':litr-muxers' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found this issue when I tested muxing to
mkv
. It probably happens today if you mux to webm. This small change just means that we build a demo app filepath that has the correct extension. It only impacts the demo app.