Skip to content

Commit

Permalink
Implement skeleton of native litr-muxers module (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
IanDBird committed Jan 13, 2023
1 parent 62d18bb commit ccdd48a
Show file tree
Hide file tree
Showing 23 changed files with 287 additions and 2 deletions.
1 change: 1 addition & 0 deletions litr-demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ android {
dependencies {
implementation project(':litr')
implementation project(':litr-filters')
implementation project(':litr-muxers')

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.recyclerview:recyclerview:1.2.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.os.Handler
import android.os.Looper
import android.provider.MediaStore
import android.util.Log
import android.webkit.MimeTypeMap
import androidx.annotation.ChecksSdkIntAtLeast
import androidx.annotation.WorkerThread
import java.io.File
Expand Down Expand Up @@ -49,9 +50,11 @@ class SharedMediaStoragePublisher @JvmOverloads constructor(
if (!file.exists()) return null

val newFileName = file.name
val extension = MimeTypeMap.getFileExtensionFromUrl(newFileName)
val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension) ?: MIME_TYPE_MPEG_4

val values = ContentValues().apply {
put(MediaStore.Video.Media.MIME_TYPE, MIME_TYPE_MPEG_4)
put(MediaStore.Video.Media.MIME_TYPE, mimeType)
put(MediaStore.Video.Media.TITLE, newFileName)
put(MediaStore.Video.Media.DISPLAY_NAME, newFileName)
put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis())
Expand Down
4 changes: 4 additions & 0 deletions litr-muxers/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/build

# FFmpeg symlink
src/main/cpp/ffmpeg
58 changes: 58 additions & 0 deletions litr-muxers/README.md
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}"
```
36 changes: 36 additions & 0 deletions litr-muxers/build.gradle
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')
}
4 changes: 4 additions & 0 deletions litr-muxers/src/main/AndroidManifest.xml
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>
52 changes: 52 additions & 0 deletions litr-muxers/src/main/cpp/CMakeLists.txt
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")
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)
19 changes: 19 additions & 0 deletions litr-muxers/src/main/cpp/FFmpeg.h
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" {
#include "libavutil/log.h"
#include "libavformat/avformat.h"
}

#endif //LITR_FFMPEG_H
22 changes: 22 additions & 0 deletions litr-muxers/src/main/cpp/Logging.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__))
#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
86 changes: 86 additions & 0 deletions litr-muxers/src/main/cpp/build_ffmpeg.sh
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="
--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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion settings.gradle
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'

0 comments on commit ccdd48a

Please sign in to comment.