Skip to content
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

Merged
merged 1 commit into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor Author

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.

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")
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

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" {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
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__))
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
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="
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
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'